Writing Checks
We will practice setting up situations that test to see if pieces of your CPU are working.
Appendix A3.3 covers testing programs on the CPU emulator.
Review
The CPU has inputs: inM
, instruction
, and reset
.
It has outputs outM
, writeM
, addressM
, and pc
.
Inside the CPU, the components are:
- Instruction memory (“ROM”) - determined by loading a program.
- Data memory (“RAM”) - visible in the emulator.
- Program counter (“PC”).
- Registers: (“ARegister”, “DRegister”).
Testing Jumps
In the CPU, we want to test the instruction D;JLE
. Our initial setup
will be A=17
, D=-23
, and RAM[17] = 31
.
The instructions to create the initial setup are:
set instruction %B1110001100000110
, where the binary comes from the lookup table in Figure 4.5 (page 95).set ARegister 17
set DRegister -23
set inM 31
set reset 0
set PC 5
After one instruction (tick/tock), the program counter should become 17 because D<0 jumps (JLE) so the next instruction executed is at PC = the value in the A register.
The first lines of the code are copied from the CPU testing script.
Use %B
to define values in binary.
Global setup
load CPU.hdl;
output-list time%S0.4.0 inM%D0.6.0 instruction%B0.16.0 reset%B2.1.2 outM%D1.6.0 writeM%B3.1.3 addressM%D0.5.0 pc%D0.5.0;
Test 1: A=17
The first test is just to load 17 into the A register. It’s good to start with a test you know will work.
set reset 0;
set PC 5;
set ARegister 1;
set DRegister -23;
set inM %B011111;
set instruction %B00010001; // load 17 into A register
tick, output;
tock, output;
After the first test, the PC is 6.
Test 2: D; JLE
Now we have D=-23 and JLE, which should be true so execution jumps to PC=17 (the value in the A register). It is not necessary to reset those values, but be aware that if your coding is wrong the values might get changed by accident.
set instruction %B1110001100000110; // D; JLE
tick, output;
tock, output;
Make sure PC is 17 after this test.
Test 3: 0; JGT
This jump should not be taken, so the PC should be 7 when it completes.
set ARegister 3;
set PC 6;
set instruction %B1110101010000001; // 0; JGT
tick, output;
tock, output;
Documentation
Notice that you need to use the variables named for the internal chips in the CPU. There is only a general mention of how to do this in the book on Figure A3.2 (page 334).
Write %B
to give a binary value. Write %X
to give a
hexadecimal value. Examples: %B011100
or %X0FA9
.