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 one after the other in the given order
- label the input pins as specified in the contract
- if there are multiple circuits on the canvas add the circuit index (for example
a0,b0,out0)
- save the Logisim files in folder
projects/5/a10
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 the chips you need to use or build at this stage:
Computer
and CPU
:
- copy CPU.circ from the previous project and save as Computer.circ
- after the previous step the main canvas will have the
CPU
circuit
- add a new subcircuit named
CPU
and copy-paste the CPU
circuit from the main canvas
- erase the main canvas and start building the
Computer
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:
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
:
- paste 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 (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 projects/5/a10.
Memory
:
- build and test the
Memory
circuit
Computer
:
- build and test the
Computer
; could consider working in stages as outlined in Logisim section
Computer Test with Given Test Cases
:
- 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
:
- 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
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
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
an 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):
- do no put spaces - this is the binary code we wrote in class formatted as instruction and in groups of 4 but without the spaces
- 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)
- remove the spaces - if you see "illegal character" message
- make sure to always load the updated program with extension .hack
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 and
fib.hex, 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
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.
Click here for a binary to hexadecimal converter.
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)
{
int newCur = nextFib; // new value for curFib
int newNext = curFib + nextFib; // new value for nextFib
curFib = newCur; // change/update curFib
nextFib = newNext; // change/update nextFib
i = i + 1;
}
return curFib;
}
Multiplication in Machine Code
Write a program in machine code that multiplies three numbers
m1,m2,m3 ≥ 0
via repeated addition.
Submit
mul.xlsx and
mul.hex, 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
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;
}
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 and
draw.hex, 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
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: contains
16384
, not modified by the program
- address 13+: these are used for any other variables you may need
We are essentially converting the following Java program:
void draw(int n, const int start=16384)
{
int i = start;
int count = n;
while (count > 0)
{
M[i] = -1;
i = i + 545; // 16*32+33: skip 16 rows + 1 cell
count = count - 1;
}
}
Notes on Machine Code
Manipulating a memory cell is a multi-step process as shown in the following examples:
- Store value 12 in cell M[5]:
M[5] = 12
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
- Increment by 1 value of memory cell M[5]:
M[5] = M[5] + 1
A=5
: store address 5 in A
M=M+1
: add 1 to cell 5
- Increment by 10 value of memory cell 5:
M[5] = M[5] + 10
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
- Add cells 5 and 6:
5 = 5 + 6
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