KeithB wrote
Having finished chapter 6 (the assembler), I decided to try something I'd been thinking about for the previous few chapters; namely, using the two unused instruction bits to implement an extra register to reduce the amount of data swapping in and out of the D register. I decided to name it C for counting and to remap the instruction bits as:
1 rc rm c1 c2 c3 c4 c5 c6 da dc dd dm jlt jeq jgt
I chose that as it keeps relevant bits together, even though it shifts some of the existing bit locations.
The additions are rc (read from C register, "rm" is called "a" in the book) and dc (destination C register).
The CPU treats C similarly to M, so if the rc bit is set, C is read instead of A or M. All of the changes were made in the CPU and were fairly straightforward.
Congratulations on a job well done.
One of the things I really love about this book is how straightforward the Hack architecture is, yet it remains a usable platform that can support quite complex programs.
If you are planning to do the software chapters in the book, you might want to consider making your modified Hack computer machine language compatible with the original so that you can use your assembler to assemble the output from your VM.
I made my assembler output a listing file that showed the address, machine code and source line.
14 24576 @24576 // SCREEN + size of screen
15 58576 D=D-A
16 4 @LOOP
17 58116 D;JLT // loop if pScreen still within screen
This was quite useful while debugging the VM since I included the VM source as comments in the generated assembly code.
(It's also incredibly cool when you get to the end of the book and compile/translate/assemble your game and OS with your tools, and run in in the CPU simulator.)
--Mark
[If your assembler works like mine, oring in instruction bits as required, it may be a bit of a pain to invert the sense of the rc and dc bits. A trick that just occurred to me is to generate the instruction with the bits in their non-inverted state and xoring with 0x6000 to invert them.]