mossprescott / pynand

Nand2Tetris in Python.
Other
12 stars 3 forks source link

Add two alternative CPUs #25

Closed mossprescott closed 4 years ago

mossprescott commented 4 years ago

This is where it gets interesting.

Two new CPUs are implemented:

  1. SPComputer, which adds a register to hold SP, making push/pop machine instructions. This adds a lot of complexity to decoding the instruction, but saves a lot of cycles, and a fair amount of ROM.
  2. ThreadedComputer, which adds a register to hold a return address, and CALL/RTN instructions making it very cheap to jump in and out of a library of opcode handlers. That saves a ton of ROM, allowing very large programs to be translated, but takes more cycles to run them.

Each one is implemented in a single module with new CPU and Computer components, a new VM translator, and a main function that can run programs interactively, as well as printing the generated assembly for inspection.

Some adjustments were made to tests to accommodate varying implementations: