happens in each line, and why (1 page max). CHIP Memory { IN in[16], load, address[15]; OUT out[16]; PARTS: DMux4Way(in=load, sel=address[13..14], a=loadram1, b=loadram2, c=loadscreen, d=loadkbd); Or(a=loadram1, b=loadram2, out=loadram); RAM16K(in=in, load=loadram, address=address[0..13], out=ramout); Screen(in=in, load=loadscreen, address=address[0..12], out=scrout); Keyboard(out=kbout); Mux4Way16(a=ramout, b=ramout, c=scrout, d=kbout, sel=address[13..14], out=out); }
Explain the Memory.hdl file line by line. Remember to discuss what happens in each line, and why (1 page max). CHIP Memory { IN in[16], load, address[15]; OUT out[16]; PARTS: DMux4Way(in=load, sel=address[13..14], a=loadram1, b=loadram2, c=loadscreen, d=loadkbd); Or(a=loadram1, b=loadram2, out=loadram); RAM16K(in=in, load=loadram, address=address[0..13], out=ramout); Screen(in=in, load=loadscreen, address=address[0..12], out=scrout); Keyboard(out=kbout); Mux4Way16(a=ramout, b=ramout, c=scrout, d=kbout, sel=address[13..14], out=out); }
The Correct Answer and Explanation is:
Here is a clear line-by-line explanation of the Memory.hdl
file. This chip implements a unified memory abstraction that includes regular RAM, memory-mapped screen, and keyboard input, based on the Hack platform design.
CHIP Memory {
This line defines a new chip called Memory
. It wraps RAM, screen memory, and keyboard I/O into one unified interface.
IN in[16], load, address[15];
The chip has three inputs:
in[16]
: the 16-bit data to be writtenload
: a control signal to trigger writingaddress[15]
: a 15-bit address specifying where to read/write
OUT out[16];
This is the 16-bit output, returning the value stored at the given address.
PARTS:
This begins the part list, showing how the Memory chip is built from smaller components.
DMux4Way(in=load, sel=address[13..14], a=loadram1, b=loadram2, c=loadscreen, d=loadkbd);
This line uses a 4-way demultiplexer to route the load
signal to one of four destinations based on bits 13 and 14 of the address.
- When address[13..14] is:
00
: activatesloadram1
01
: activatesloadram2
10
: activatesloadscreen
11
: activatesloadkbd
Or(a=loadram1, b=loadram2, out=loadram);
Combines loadram1
and loadram2
using an OR gate. This is because both regions 00
and 01
are used for RAM. The result loadram
controls writing to the RAM16K chip.
RAM16K(in=in, load=loadram, address=address[0..13], out=ramout);
This is the main 16K RAM.
- Uses the lowest 14 bits (0 to 13) of the address
loadram
enables writing- Returns the stored value as
ramout
Screen(in=in, load=loadscreen, address=address[0..12], out=scrout);
This chip simulates screen memory (8K).
- Uses 13-bit address (0 to 12)
- Activated when address[13..14] =
10
Keyboard(out=kbout);
Returns the current key pressed as kbout
.
- Keyboard is read-only, no input signals needed
Mux4Way16(a=ramout, b=ramout, c=scrout, d=kbout, sel=address[13..14], out=out);
This 4-way multiplexer selects the correct output based on address bits 13 and 14.
a
andb
both connected to RAM outputc
is screen outputd
is keyboard output- Final value is sent to the
out
pin
In summary, this HDL file allows unified access to memory, screen, and keyboard using one address space. The address bits determine which device to read from or write to, and the logic routes the signals accordingly.
