r/computerscience • u/zinc__88 • May 14 '25
Designing an 8-bit CPU: How to load constants?
I have been following Sebastian Lague's videos on YouTube and have started to make my own CPU in his Digital Logic Sim. Currently it is single cycle and I have registers A and B, a program counter, a basic ALU and ROM for the program.
My goal is to run a program that outputs the Fibonacci sequence. I have a very basic control unit which has output bits for:
- Write to A
- Write to B
- Output A
- Output B
- Output ALU
With this I have made an ADD instruction which adds A and B and writes the output to A.
I now need an instruction to load a constant into either A/B. I've looked online but am quite confused how to implement this. I've seen examples which have the immediate constant, e.g.: XXXXAAAA, where X is the opcode and A is the constant (ideally I want to learn how to load 8 bit numbers, so this won't work for me).
I've seen other examples where it uses microcode and 2 bytes, e.g.: the first byte is the instruction to load a constant, and the second is the actual constant (which would allow for 8 bits).
What would be the best way to implement the microcode? Would this be possible for a single cycle CPU? Do I need an instruction register? I also don't want the CPU to execute the data, so how would I correctly increment the program counter? Just increment it twice?
2
u/Obvious-Falcon-2765 May 14 '25
You’ve basically got the right idea. You can do it the Ben Eater SAP-1 way, where the 8-bits of instruction in memory consists of 4 bits of opcode + 4 bits of immediate value to load. The 4 lower value bits would go onto the bus, and then get loaded into the register that the opcode specifies. So your assembly code would look like:
LDA #5
Which would translate to:
00010101
Where 0001 is your Load A immediate opcode, and 0101 is the value (in this case 5) to put into A
If you need 8-bit opcodes and 8-bit immediate values on an 8-bit bus, you would first load the opcode into the instruction register, and then load the value into A. Assuming you only have an 8-bit bus and 8-bit wide memory, this would have to take place in two steps. Your assembly code would then look the same:
LDA #5
But would compile to:
00000001 00000101
2
u/TheThiefMaster May 14 '25
Most real 8-bit CPUs implemented multibyte/multicycle opcodes. It's quite simple, really. You just need:
Then your "load constant" opcode (e.g. opcode 0x01) would have two entries in the decoder for 0x01-0 and 0x01-1, where the first sets up a "argument fetch" into whichever register and the second finishes it with the fetched value and sets up the next instruction fetch.
6
u/thesnootbooper9000 May 14 '25
One way is to have a "load low" opcode that loads only the lowest four bits and zeros the high bits, and then either a "load high" or a shift and an or. I don't know about 8 bits, but for larger word sizes this can be quite good because most constants are small.
2
u/Falcon731 May 14 '25
Probably the easiest way is to have a few 'flag' registers which allow one instruction to change the way the next instruction gets decoded. So a two-byte instructions get fetched just the same as two single byte instructions would be.
So in cycle1 you fetch a "load_immediate" instruction. This instruction does nothing except set a "load_immediate" flag.
Then in the next cycle you fetch the next instruction as per normal. But the decoder detects the load_immediate flag and rather than decoding the instruction as an instruction it treats it as data for a load immediate instruction. And clears the load_immediate flag.
1
u/flatfinger May 15 '25
On the CDP 1802, CPU cycles generally alternated between "load the instruction register using post-increment addressing on the address register selected using (IIRC) the upper 4 bits of the P/X register" and "perform an operation specified by the instruction in the instruction register". If an instruction uses post-increment addressing on the same register used for instruction fetches, the byte following the instruction will be accessed as indicated and the next instruction will be fetched from the byte after that.
For example, if register 4 is selected for P and 3 is selected for X, then "OUT 1" will fetch a byte from the address in register 3 and indicate that I/O device 1 should output it, after which the byte following the OUT instruction would be executed. If register 4 were selected for both P and X, then OUT 5 would cause the byte after the instruction to be fetched and output to I/O device 1, and then the byte after that should be executed. The logic in the CPU doesn't care about the distinction. If the "execute" phase happens to alter the register being used for code fetches, the next instruction will be fetched from the altered address.