-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.hdl
More file actions
55 lines (42 loc) · 2.1 KB
/
Memory.hdl
File metadata and controls
55 lines (42 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/05/Memory.hdl
/**
* The complete address space of the Hack computer's memory,
* including RAM and memory-mapped I/O.
* The chip facilitates read and write operations, as follows:
* Read: out(t) = Memory[address(t)](t)
* Write: if load(t-1) then Memory[address(t-1)](t) = in(t-1)
* In words: the chip always outputs the value stored at the memory
* location specified by address. If load==1, the in value is loaded
* into the memory location specified by address. This value becomes
* available through the out output from the next time step onward.
* Address space rules:
* Only the upper 16K+8K+1 words of the Memory chip are used.
* Access to address>0x6000 is invalid. Access to any address in
* the range 0x4000-0x5FFF results in accessing the screen memory
* map. Access to address 0x6000 results in accessing the keyboard
* memory map. The behavior in these addresses is described in the
* Screen and Keyboard chip specifications given in the book.
*/
CHIP Memory {
IN in[16], load, address[15];
OUT out[16];
PARTS:
// Put your code here:
//for deciding what is loaded
DMux(in=load, sel=address[14], a=loadRAM, b=loadOther); //14 bits needed for RAM, therefore, 15th bit used to access screen or keyboard
DMux(in=loadOther, sel=address[13], a=loadSCREEN, b=null); //14th bit is difference between 24575 and 24576th word
//RAM
RAM16K(in=in, load=loadRAM, address=address[0..13], out=RAMout);
//SCREEN
Screen(in=in, load=loadSCREEN, address=address[0..12], out=screenOut);
//KEYBOARD
Keyboard(out=KEYBOARDout);
//for deciding what comesout
Not(in=address[14], out=ram); //if the 15th bit is 0, then 'ram' will be positive, and cues that ram should be outputed
Not(in=address[13], out=screen); //if the 14th bit is 0, then the address is within the range of SCREEN, and screen will be selected.
Mux16(a=KEYBOARDout, b=screenOut, sel=screen, out=screenOrKeyboard);
Mux16(a=screenOrKeyboard, b=RAMout, sel=ram, out=out);
}