a miserable pile of opinions about cpu design
some thoughts i want to put somewhere while i've been playing Turing Complete, under the cut to save your sanity
so this game has you make a full-ass turing complete computer from first principles. like nandgame but way more polished and accessible. starts you off with a nand block, and has you build all other logic blocks out of that. you progress through, building stuff like adders and address decoders, registers and so on, and eventually you get to a point where you have an addressable ROM with a program in it. really neat stuff.
so at that point, the game has you do a bunch of tasks to make sure you understand what's actually going on (stuff like maze solvers in your special gender of assembly) and then stops and goes: hey so the design of this is actually not great because instructions are restricted to acting on only hard-set registers (ALU operations will always take the values of R1 and R2 and store the result in R3, as an example) and we wanna make it better. it tells you to start over from scratch and build a new computer architecture called LEG
the LEG architecture is where the game's expectations and my desires diverge.
so, with the previous architecture, every instruction was 1 byte long. first few bits defined the opcode - not great because it limited the number of opcodes you could actually have. this time, every instruction is to be 4 bytes (32 bits) long:
OPCODE ARG1 ARG2 STA
OPCODEis, of course, the opcode. the operation. having an entire byte to ourselves here suggests being able to have 256 discrete opcodes, but actually no for reasons i'll get into in a bit.ARG1andARG2are both inputs to the desired operation. they can be either registers, ports, or raw numbers.STA(Store To Address) designates where the output should go. can be a register or a port.
so, from that, you might already see why i'm getting a case of the opinions.
- not every operation can take two inputs. i'm wasting program space storing an unused input.
- not every operation has an output value. i'm wasting program space storing the unused output address
- not every operation even has an input. if i just do a
NOP, i'm wasting 75% of the instruction.
further complicating things is what i alluded to when talking about the number of potential opcodes above. the upper four bits of the opcode are essentially unrelated to an opcode:
Bit 5is unusedBit 6indicates that theSTAbyte is what theIPshould be set to, if the result of the operation is 1. With this bit set, the available instructions become justJE/JNE,JL/JLE, andJG/JGE.Bit 7indicates thatARG2is a raw number which should be used as an input directly, rather than referencing a port or registerBit 8is the same as above, but forARG1. Using these two bits gets us some more branching instructions:JMP(JE 0, 0),JS(JL ?, 0), andJZ/JNZ(JE ?, 0andJNE ?, 0).
so what instructions do we actually have, and what are their args? well..
for base instructions (discretely implemented):
ADD R?, R?, R? - with args 1 and 2 being settable as raw numbers if the ADD operation is OR'd with bit 7 and/or 8 SUB R?, R?, R? AND R?, R?, R? OR R?, R?, R? NOT R?, unused, R? XOR R?, R?, R? NAND R?, R?, R? NOR R?, R?, R? NEG R?, unused, R? XNOR R?, R?, R? JE R?, R?, LDA JNE R?, R?, LDA JL R?, R?, LDA JLE R?, R?, LDA JG R?, R?, LDA JGE R?, R?, LDA
for composite instructions (made of other instructions)..
MOV R?, unused, R? (eq. BIT7 | ADD R?, 0, R?) INC R?, unused, R? (eq. BIT7 | ADD R?, 1, R?) DEC R?, unused, R? (eq. BIT7 | SUB R?, 1, R?) JMP unused, unused, LDA (eq. BIT8 | BIT7 | JE 0, 0, LDA) JZ R?, unused, LDA (eq. BIT7 | JE R?, 0, LDA) JNZ R?, unused, LDA (eq. BIT7 | JNE R?, 0, LDA) JS R?, unused, LDA (eq. BIT7 | JL R?, 0, LDA)
okay so what's the big deal? well, all these unused parts of the instruction are unsightly to me and i find myself yearning for variable length instructions.
this is a lot of bullshit in order to have just said "man i wish i had variable length instructions in this video game computer i made." but i feel better for having written it all. sorry if you read this whole thing.


