Readings
- CPU:
- N2T: Chapter 5 Intro, 5.1.3, 5.1.5, 5.1.6, 5.2, 5.3, 5.4, 5.5 (slides 49-77)
- N2T: Chapter 4 Intro, 4.1.2, 4.2.1, 4.2.2, 4.2.3, (slides 7-16, 19-23, 33-37, 41-47)
Description
This assignment will focus on the following tasks:
- building the Computer
- writing programs in Machine Code (in Binary)
Build the
Computer and
Memory circuits described in Project 5:
N2T: Project 5
Chapter 5 provides the contract for each circuit, i.e. description of its behavior, names and number inputs, names and state of outputs. The API is available here:
The Hack Chipset
Programming in Machine Code (in Binary) and Hack Assembly
vvv
vvv Scroll to the bottom. Consider starting with the Machine Code section.
vvv
Design in Logisim, Part I: Ignore Keyboard and Screen
Note the following requirements:
- arrange the circuits vertically in the given order (each below the previous one, *not* in a grid)
- label the input pins as specified in the contract
- save any images in subfolder
computer/hack-computer/png
Build
Computer as shown in Figure 5.9 on page 24. At this stage:
Memory will simply be a single RAM16K
- ignore
Screen and Keyboard
Here are additional specific requirements:
- save the Logisim files in folder
computer/hack-computer/circ
- start new Logisim project and save in file named Computer.circ
- if your
CPU worked, add it along with the other components, as a subcircuit: from Menu Bar Project=>Add Circuit...,"Open..."
- if there were problems with some of the circuits, import nand2tetris.jar: from Menu Bar Project=>Load Library=>Jar Library...
- [if using
nand2tettri.jar, re-download since there have been some updates]
RAM and ROM:
- available under
Memory library
- memory size and data bus width need to be set according to the specifications
- Computer Test with Class Examples:
- run the programs from class: count_5_1.hex | sum_5_1.hex
- to load a program: ROM Right-Click=>Load Memory Image then hit F9 to step through
- for
sum_5_1.hex need to put a value in RAM in cell 14, then observe the updates in cell 17
Design in Logisim, Part II: Memory with RAM, Keyboard, and Screen
In this part you will build only the
Memory module as specified in Figure 5.6 on p. 17.
Here are the chips you need to use or build at this stage:
Memory:
- create new Logisim file named Memory.circ
Keyboard:
- download the following circuit and paste directly in Memory.circ (do not create a subcircuit)
GCKeyboard.circ (right-click, Save As...)
- delete the pins (not their wires) and connect their wires where necessary; this
Keyboard has the same API as RAM and Screen, so it has two extra inputs
Screen:
- available via the following library:
nand2tetris.jar (from Menu Bar: Project=>Load Library=>Jar Library... as in previous assignment)
Memory Test:
- test by loading values at the following addresses and take a single screenshot of the outcome in memory-logisim.png:
| address | value | meaning |
| 16383(0x3FFF) | ABCD | last address of RAM16K |
| 16384(0x4000) , 24575(0x5FFF) | 0x7FFF | first and last address of Screen; should see thin lines at upper-left and lower-right corner |
| 24576(0x6000) | DCBA | address of Keyboard |
Design in Logisim, Part III: Integrate Computer and and Memory
In this part you will complete the construction of
Computer as shown in Figure 5.9 on page 24.
Computer:
- replace
RAM16K with Memory by pasting the contents of Memory.circ directly in Computer.circ; do not include as subcircuit since you won't be able to interact with Keyboard and Screen
Computer Screen Test:
- load the given programs and check the outcome
TwoLines.hex (should see two lines at the top-left and bottom-right corner of screen)
Rect.hex (first column should be filled halfway)
Computer Final Test:
- load the given programs and save two screenshots named cat-logisim.png and pong-logisim.png (for Pong show of your best score)
Cat.hex
Pong.hex (click inside Keyboard to activate it; move with Shift-N or Shift-M)
Design in HDL
Here are additional specific requirements:
- Implement one circuit at a time. For circuit X copy X.hdl, X.cmp, X.tst to folder computer/hack-computer/hdl/X.
Memory:
- build and test the
Memory circuit; could skip initially
Computer:
- build and test the
Computer; could consider working in stages as outlined in Logisim section, i.e. only RAM16K at first, later add complete Memory
Computer Test with Given Test Cases (no Screen):
- load the tester for the corresponding program: Computer(Add|Max|Rect).tst (ignore files that have external)
- use the blue arrow buttons to Step, Run, Reset the program
Computer Test with Class Examples (no Screen):
- download the test programs: count_5_1.hack | sum_5_1.hack
- in the right panel, under ROM, click on the Folder icon and load the one of the programs
- use the blue arrow buttons to Step, Run, Reset the program
- for
sum_5_1.hex need to put a value in RAM in cell 14, then observe the updates in cell 17
Computer Screen Test:
- download the test programs and run as in previous step:
TwoLines.hack (should see two lines at the top-left and bottom-right corner of screen)
Rect.hack (first column should be filled halfway)
Computer Final Test:
- load the given programs and save two screenshots named cat-hdl.png and pong-hdl.png (for Pong show of your best score)
Cat.hack
Pong.hack (move with Shift-N or Shift-M)
Programming in Machine Code
See at the bottom for "Notes on Machine Code".
Testing Machine Code with Logisim
- use a text editor to save in plain text the Machine Code (i.e. the Hexadecimal Code) of your program in file with extension .hex (see count_5_1.hex for the format):
- this is the second column (gray) in the Excel spreadsheet from class
- do no put spaces in the hex code
- right-click on the ROM and use "Load Memory Image" to load the program; make sure to initialize the relevant cells in the RAM by typing in the values
- step through the program by hitting F9
- before each new test run make sure the RAM is cleared (from Menu Bar: Simulate=>Reset Simulation), but then type in any parameter values in RAM
- make sure to always load the updated program with extension .hex
Testing Machine Code with N2T software
Initially use the
CPUEmulator to test the binary programs. Later test the programs on your own computer in
Logisim and in
HardwareSimulator.
Here is how to test in the
CPUEmulator:
- go to the place where you normally run
HardwareSimulator but instead run CPUEmulator
- use a text editor to save in plain text the Machine Code (i.e. the Binary Code) of your program in file called fib.hack (see count_5_1.hack for the format):
- this is the first column (purple) in the Excel spreadsheet from class
- do no put spaces in the binary code
- use "File=>Load Program" to load the program on the ROM and step through with the blue arrows
- before each new test run make sure the RAM is cleared (use the first icon above the RAM), but then type in any parameter values in RAM
- remove the spaces - if you see "illegal character" message
- make sure to always load the updated program with extension .hack
Program Folder
Save the program files in folder
computer/hack-computer/mcode.
Fibonacci in Machine Code
Write a program in machine code that computes the
n-th Fibonacci number where
f1=1, f2=1, 2, 3, 5, 8, ... .
Submit
fib.xlsx,
fib.hex,
fib.hack, i.e. Excel file with code and a program/text files that can be loaded on the ROM as given here:
machine_code_examples.xlsx | count_5_1.hex | count_5_1.hack
Show that the Fibonnacci program works in the Logisim computer by computing the 6th Fibonnacci number. Take a screenshot named
fib.png that shows the contents of the RAM and ROM after the program has finished.
The program should use the following memory cells. Try different values for
m1,m2,m3, including 0.
- cell at address 11 should be filled in manually (represents
n), the rest should be initialized by the program to the correct values
- address 11: contains
n, index of number we need to compute, not modified by the program
- address 12: contains
i, index of current Fibonaccci number
- address 13: contains
currFib, value of current Fibonaccci number; when program stops contains the answer
- address 14: contains
nextFib, value of next Fibonaccci number; when program stops has value after the answer
- address 15+: these are used for any other variables you may need
Notes on the loop:
- the program should use
while loop
- the loop condition should match the structure in the Java program; the branch instruction should compute the difference of the two variables
We are essentially converting the following Java program:
int fibonacci(int n)
{
int curFib = 1; // value of current fib number
int nextFib = 1; // value of next fib number
int i = 1; // index of curFib (n=1 computed in curFib)
while (i < n)
{
curFib, nextFib = nextFib, curFib+nextFib; // move ahead
i = i + 1;
}
return curFib;
}
Drawing in Machine Code
Write a program in machine code that draws a staircase given the number of stairs
n ≤ 15:
___
___
___
___ and so on
Submit
draw.xlsx,
draw.hex,
draw.hack, i.e. Excel file with code and a program/text file that can be loaded on the ROM as given here:
machine_code_examples.xlsx | count_5_1.hex | count_5_1.hack
The program should use the following memory cells. Try different values for
n, i.e. cell 11, including the required values plus a couple of extra ones:
- cell at address 11 (represents number of stairs) should be filled in manually, the rest should be initialized by the program to the correct values
- address 11: contains
n, not modified by the program
- address 12+: these are used for any other variables you may need
We are essentially converting the following Java program:
void draw(int n)
{
int i = 16384;
int count = n;
while (count > 0)
{
M[i] = -1;
i = i + 545; // 16*32+33: skip 16 rows + 1 cell
count = count - 1;
}
}
(Optional) Multiplication in Machine Code
Write a program in machine code that multiplies three numbers
m1,m2,m3 ≥ 0 via repeated addition.
Submit
mul.xlsx,
mul.hex,
mul.hack, i.e. Excel file with code and a program/text file that can be loaded on the ROM as given here:
machine_code_examples.xlsx | count_5_1.hex | count_5_1.hack
The program should use the following memory cells. Try different values for
n, i.e. cell 11, including the required values plus a couple of extra ones:
- cells at address 11,12,13 should be filled in manually (represent the numbers to multiply), the rest should be initialized by the program to the correct values
- address 11: contains
m1, not modified by the program
- address 12: contains
m2, not modified by the program
- address 13: contains
m3, not modified by the program
- address 14: contains the final answer
- address 15+: these are used for any other variables you may need
We are essentially converting the following Java program:
int multiply(int m1, int m2, int m3)
{
int result = 0;
int count1 = m1;
while (count1 > 0)
{
int count2 = m2;
while (count2 > 0)
{
result = result + m3;
count2 = count2 - 1;
}
count1 = count1 - 1;
}
return result;
}
Notes on Machine Code
Manipulating a memory cell is a multi-step process as shown in the following examples:
M[5]=12, i.e. store value 12 in memory cell 5
A=12 : store value 12 in A
D=A : store/move A, i.e. 12, in D
A=5 : store address 5 in A
M=D : store D, i.e. value 12, in cell 5
M[5]=M[5]+1, i.e. increment by 1 value of memory cell 5
A=5 : store address 5 in A
M=M+1 : add 1 to cell 5
M[5]=M[5]+10, increment by 10 value of memory cell 5
A=10 : store value 10 in A
D=A : store/move A, i.e. 10, in D
A=5 : store address 5 in A
M=M+D : add value 10 to cell 5
M[5]=M[5]+M[6], i.e. add memory cells 5 and 6 and store in memory cell 5
A=5 : store address 5 in A
D=M : store value of cell 5 in D
A=6 : store address 6 in A
D=D+M : compute M[5]+M[6] and store in D
A=5 : store address 5 in A
M=D : store D, i.e. the sum, in cell 5