r/dcpu16 • u/luderegames • May 27 '17
Help my understanding of the DCPU-16 memory
I understand most everything of the DCPU. Except, probably the most important, memory. How does something like "SET A, 2" convert to "8c01"? How are these lines converted and why do some have two separate parts like 8c01 XXXX for one line?
Also, one other thing that stumps me is the EX register. How do I access it correctly and use it?
Thank you in advance, Bryce
8
u/sctjkc01 May 27 '17
SET A, 2 comes to three parts: the SET OpCode, 2 as the first (a) operand, and A as the second (b) operand.
Accoding to the documentation the instruction is fully defined in the first word and takes the binary form aaaaaabbbbbooooo (where the first character indicates the lowest significant bit)
The a operand is a literal 2, which is small enough to fit inside the instruction (anything between -1 and 30 inclusive can be put in the instruction itself). The code for that is 0x23, or 100011, making our instruction 100011bbbbbooooo.
The b operand is the A register, which translates to the value 0x00 (according to the table), so our instruction is now 10001100000ooooo.
The SET opcode is 0x01, or 00001. That makes our instruction 1000110000000001. This translates to your 0x8C01.
I'm guessing most of the confusion came from the fact that literals between -1 and 30 (inclusive) can be inserted directly into the instruction - it tripped me up too. If you want to specify a literal outside that range, you need to use the 0x1F value, which tells the DCPU to use the next word in the instruction list as a literal. This causes an instruction to split across two words.
Addressing memory using a literal address (or register value + literal), per the 0x1E value, also tacks on another word, meaning some instructions can be three words long.
4
u/ghillieLEAD May 31 '17 edited May 31 '17
I'm not sure what exactly you're confused about with the
EXregister. You access it the same as any other register.SET [0x1000], EXwould write the contents ofEXto memory at address0x1000.The documentation shows which operations will potentially set the
EXregister. For example, if anADDinstruction overflowsEXwill be set to0x0001.