# Galactic Electronics Projects

## 4 bit CPU - Sample Programs.

These are sample programs that don't do much more than push data to the output. These are not practical useful applications and only serve to demonstrate and exercise the CPU.

The first program simply loads a value from a register, increments it, outputs the value and writes the value back to the register. The process is repeated continually. The output is connected to 4 LEDs.

    Numonic   Address    Data Instruction   Comments
    ------------------------------------------------------------------------------------------
                                            Runs in segement zero.
    LIT 1     0000 0000  0001 0000          Put 0 in the accumulator.
    REG       0000 0001  0000 0110          Select register A.
    LOAD      0000 0010  0000 0010          Load value into accumulator from register A.
    OUT       0000 0011  0000 1110          Send accumulator value to output.
    INC       0000 0100  0000 0100          Increment accumulator.
    STORE     0000 0101  0000 0011          Store accumulator back to register A.
    RST       0000 0110  0000 1001          Restart program counter.

This program stores a sequence of values in the registers and then outputs them in sequence. The resulting effect is the output lines going high in a 1, 2, 4, 8 order. The program continuously loops from segments 2 to 3. This can be seen when LEDs are connected to the outputs.

    Numonic   Address    Data Instruction   Comments
    ------------------------------------------------------------------------------------------
                                            Runs in 4 memory segments.
                                            Segment 0. Loads registers A and B.
    LIT 0     0000 0000  0000 0001          Put 0 in the accumulator.
    REG       0000 0001  0000 0110          Select register A for write.
    LIT 1     0000 0010  0001 0001          Put 1 in the accumulator.
    STORE     0000 0011  0000 0011          Put 1 in register A.
    LIT 1     0000 0100  0001 0001          Put 1 again in the accumulator.
    REG       0000 0101  0000 0110          Select register B for write.
    LIT 2     0000 0110  0010 0001          Put 2 in the accumulator.
    STORE     0000 0111  0000 0011          Put 2 in register B.
    CMPL      0000 1000  0010 0111          Compare to last accumulator value. Set equal flag.
    LIT 1     0000 1001  0001 0001          Put 1 in the accumulator.
    JUMPE     0000 1010  0000 1011          Jump to segment 1.
                                            Segment 1. Loads registers C and D.
    LIT 2     0001 0000  0010 0001          Put 2 in the accumulator.
    REG       0001 0001  0000 0110          Select register C for write.
    LIT 4     0001 0100  0100 0001          Put 4 in the accumulator.
    STORE     0001 0011  0000 0011          Put 4 in register C.
    LIT 3     0001 0100  0011 0001          Put 3 in the accumulator.
    REG       0001 0101  0000 0110          Select register D for write.
    LIT 8     0001 0110  1000 0001          Put 8 in the accumulator.
    STORE     0001 0111  0000 0011          Put 8 in register D.
    CMPL      0001 1000  1000 0111          Compare to last accumulator value. Set equal flag.
    LIT 2     0001 1001  0010 0001          Put 2 in the accumulator.
    JUMPE     0001 1010  0000 1011          Jump to segment 2.
                                            Segment 2. Sends registers A and B to output.
    LIT 0     0010 0000  0000 0001          Put 0 in the accumulator.
    REG       0010 0001  0000 0110          Select register A for read.
    LOAD      0010 0010  0000 0010          Load register A to accumulator.
    OUT       0010 0011  0000 1110          Output accumulator (register A).
    LIT 4     0010 0100  0100 0001          Put 4 in the accumulator.
    REG       0010 0101  0000 0110          Select register B for read.
    LOAD      0010 0110  0000 1010          Load register B to accumulator.
    OUT       0010 0111  0000 1110          Output accumulator (register B).
    CMPR      0010 1000  0000 1000          Compare accumulator to register. Set equal flag.
    LIT 3     0010 1001  0011 0001          Put 3 in the accumulator.
    JUMPE     0010 1010  0000 1110          Jump to segment 3.
                                            Segment 3. Sends registers C and D to output.
    LIT 8     0011 0000  1000 0001          Put 8 in the accumulator.
    REG       0011 0001  0000 0110          Select register C for read.
    LOAD      0011 0010  0000 0010          Load register C to accumulator.
    OUT       0011 0011  0000 1110          Output accumulator (register C).
    LIT 12    0011 0100  1100 0001          Put 12 in the accumulator.
    REG       0011 0101  0000 0110          Select register D for read.
    LOAD      0011 0110  0000 1010          Load register D to accumulator.
    OUT       0011 0111  0000 1110          Output accumulator (register D).
    CMPR      0011 1000  0000 1000          Compare accumulator to register. Set equal flag.
    LIT 3     0011 1001  0010 0001          Put 2 in the accumulator.
    JUMPE     0011 1010  0000 1110          Jump to back to segment 2 - loop.

Send questions and comments to WireHead@GalacticElectronics.com

(c) Jon Qualey, December 2006

Back to Simple 4 bit CPU.