Completing the Hack Computer
CPU control, data memory, and whole-computer integration.
What Remains After the Construction Attempt?
We already have the components. The remaining task is to connect them and generate the right control signals.
Complete the CPU, Memory, and top-level Computer so that Hack machine-language programs can execute.
Next: What must the CPU expose?
The CPU Contract
Chip name: CPU
Inputs: inM[16], instruction[16], reset
Outputs: outM[16], writeM, addressM[15], pc[15]
Function: Execute the current Hack instruction. If reset=1, make the next program address 0.
inM is the value currently read from RAM[A]. The three M outputs together form a possible write request; pc selects the next ROM word.
Follow a Memory Write
The current instruction is M=D+1. Which CPU output carries each part of the request?
outM = ALU outputwriteM=1addressM=A[0..14]When writeM=0, the value appearing on outM is ignored by Memory.
The Data and Address Paths
This is only the datapath. Control logic must still decide what each multiplexor selects, which registers load, whether Memory writes, and whether the PC jumps.
Three Control Problems
Is this an A-instruction or C-instruction? What do its fields mean?
Select ALU inputs and function, then enable the requested destinations.
Increment the Program Counter, load a jump target, or reset it to 0.
Next: Derive the control signals from the instruction bits.
The First Bit Determines the Meaning
0 vvvvvvvvvvvvvvvLoad the 15-bit constant into A.
111 a cccccc ddd jjja,c: compute d: destination j: jump
For an A-instruction: A must load the instruction constant.
For a C-instruction: the fields control the ALU, destinations, and jump logic.
Derive the A-Register Controls
It loads for every A-instruction, and for a C-instruction whose A-destination bit is 1.
loadA = !i OR (i AND dA)Select the instruction constant for an A-instruction; select the ALU result for a C-instruction.
i.Compute, Then Route the Result
x = Dy = Awhena=0y = inMwhena=1
- The six
c-bits connect tozx,nx,zy,ny,f,no. - The ALU produces the result,
zr, andng.
dAloads AdDloads DdMrequests a Memory write
Destination bits are control signals only in a C-instruction. Thus writeM = i AND dM.
Trace Three Instructions
| Instruction | ALU x | ALU y | Destination |
|---|---|---|---|
D=D+A | D | A | D |
M=D+1 | D | unused | Memory at A |
A=D-1 | D | unused | A |
For D=D+M, the ALU y input is inM, so the instruction's a-bit must be 1.
Choose the Next Instruction
zr=1: result is zero
ng=1: result is negative
positive = !zr AND !ng
If the C-instruction's jump condition accepts the ALU result, load PC=A.
Otherwise, increment the PC.
If reset=1, reset wins: PC=0.
A taken jump uses the A-register value that was already present at the start of the clock cycle.
What Is the Next Program Counter?
PC=20, A=50, D;JGT, ALU result = 3
PC=50PC=20, A=50, D;JGT, ALU result = 0
PC=21PC=20, A=50, 0;JMP
PC=50PC=20, A=50, reset=1
PC=0Next: Present RAM, screen, and keyboard as one Memory chip.
One Logical Address Space, Three Devices
Chip name: Memory
Inputs: in[16], load, address[15]
Output: out[16]
Function: Read the addressed word continuously. If load=1, write in to the addressed writable device in the next cycle.
Screen behaves like writable RAM and refreshes pixels. Keyboard is read-only and continuously reports the current key code, or 0.
Design the Memory Routing
Using a demultiplexor, a multiplexor, and address bits, sketch how one Memory interface can select RAM16K, Screen, or Keyboard.
Which device receives address 20000?
ScreenWhich device receives address 24576?
KeyboardWhy must Keyboard not receive load?
What returns to out?
Next: Connect CPU, Memory, and ROM32K.
The Top-Level Contract and Wiring
Chip name: Computer
Input: reset
Function: When reset=0, execute the program stored in ROM. When reset=1, restart at instruction address 0.
address[15]Output: out[16]Function: out=ROM[address]; the program is preloaded.The Computer needs no ordinary data pins: programs communicate through its memory-mapped screen and keyboard.
Trace One Whole-Computer Step
Suppose PC=12, ROM[12] contains M=D+1, and A=100.
- Which component supplies the instruction? ROM32K
- Which component computes
D+1? The CPU's ALU - Where is the result written? Memory address 100
- What is the next instruction address? 13, because there is no jump
Verify the Computer in Layers
Check RAM, screen, and keyboard address selection and write timing.
Check A/C decoding, destinations, ALU controls, jumps, and reset.
Run known programs such as Add.hack, Max.hack, and Rect.hack.
Modern computers add more registers, caches, pipelines, parallelism, and richer instructions. Yet the same ideas remain visible: stored programs, CPU, registers, Program Counter, memory, and input/output.
Next: Reconstruct the complete hardware hierarchy.
The Hardware Platform Is Complete
Decodes each instruction, computes with the ALU, stores results, and selects the next PC.
Presents RAM16K, Screen, and Keyboard as one logical address space.
Connects ROM32K, CPU, and Memory so stored machine-language programs can run.
Nand → gates → ALU and registers → CPU and Memory → Computer