Aaron's Notes

DBNZ CPU

I’ve decided to build a CPU that supports only one instruction: DBNZ. That is, Decrement and Branch if Not Zero. Each instruction consists of two bytes. The first gives a memory location whose value should be decremented, and the second consists of the memory location from which to fetch the next instruction if the result is non-zero. Using only this instruction and some self-modifying code cleverness, you can write a surprising number of programs.

Following examples I saw in Computer Organization and Design, I’ve sketched out a data path for the CPU:

DBNZ CPU data path

Some relevant pieces (all registers, adders, and buses are 8 bits wide):

Here’s the control logic:

DBNZ CPU control flow

  1. Set up the memory to read from the address in PC, and prepare PC to be incremented. (The value from the adder will be stored on the rising edge of PCWrite, asserted in the next state.)

  2. Store the first byte of the instruction into Ins A and increment PC. (Assuming no large clock skew, there’s no race condition here because the hold time required for storing into Ins A is much lower than the propogation delay of PC, much less that of the SRAM.) Keep MemRead asserted with AddrSel=1 so that the memory outputs the next byte.

  3. Store the second byte of the instruction into Ins B and increment PC again. Set up the memory to read from the address in the first byte of the instruction.

  4. Store the value pointed at by Ins A into Val. The adder will then output that value minus one. Continue to set AddrSel=0 so that this can be written back to the memory.

  5. Write the decremented value back to memory. Set up PC to read from Ins B in case we jump. If the decrementing logic says that the result is zero, fall through to the next instruction in memory by going to state 1.

  6. Store the jump target into PC.

I think this can be implemented with a resettable decade counter (actually a count-to-6 chip, if such a thing exists) and a few logic gates. The only thing that remains is a clock generator and a system to load a program into memory and reset the CPU. I’ll probably set up an Atmel MCU to do both of these, reading the program from a Micro SD card.

TODO: Check on some timing issues: