From fcde733087959cf2c835144058911c093d22eaab Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 6 Feb 2026 22:31:42 +0100 Subject: [PATCH] feat: memory hdl implementation --- .DS_Store | Bin 8196 -> 8196 bytes .gitignore | 1 + .../hdl/Memory.hdl | 62 ++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 .gitignore create mode 100644 project-5-computer-architecture/hdl/Memory.hdl diff --git a/.DS_Store b/.DS_Store index 22e8635f8a045edabc92d6343ed9602be3bf43df..cf8da0e7675fc28d19cca8ef1c151e11e4253ce6 100644 GIT binary patch delta 56 zcmZp1XmOa}&nUVvU^hRb=;ZSPMw{&fR%)xZY&gI+RQHT5JgCs2>=^o B5|jV{ delta 168 zcmZp1XmOa}&nU4mU^hRb#N_h=Mw{&fR-11}Ep|7Hlk(WZcXy@sKJ;i7^5IywN!> diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd5106f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_STORE diff --git a/project-5-computer-architecture/hdl/Memory.hdl b/project-5-computer-architecture/hdl/Memory.hdl new file mode 100644 index 0000000..2c6b2af --- /dev/null +++ b/project-5-computer-architecture/hdl/Memory.hdl @@ -0,0 +1,62 @@ +// 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/5/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 and reads 0. 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 lectures and the book. + */ + +// * 4-way 16-bit multiplexor: +// * out = a if sel = 00 +// * b if sel = 01 +// * c if sel = 10 +// * d if sel = 11 + + // 0 - 16383 - Ram 16K + // 16384 - 24575 Screen 8K + // 24576 - 24576 Keyboard (1 word) + + // 16383 = 0(01)1111111111111 Ram 16k + // 16384 = 0(10)0000000000000 Screen 8k + // 24576 = 0(11)0000000000000 Keyboard + +CHIP Memory { + IN in[16], load, address[15]; + OUT out[16]; + + PARTS: + // Ram16K + Mux(a=load, b=false, sel=address[14], out=loadRam); + Mux(a=true, b=false, sel=address[14], out=isRam); + // Screen + Mux(a=false, b=load, sel=address[14], out=loadScreen); + Mux(a=false, b=true, sel=address[14], out=isScreen); + // Keyboard + Mux(a=false, b=true , sel=address[13], out=keyboard1); + Mux(a=false, b=true, sel=address[14], out=keyboard2); + // isKeyboard + And(a=keyboard1, b=keyboard2, out=isKeyboard); + + // Logic + RAM16K(in=in, load=loadRam, address=address[0..13] , out=outRam); + Screen(in=in , load=loadScreen, address=address[0..12], out=outScreen); + Keyboard(out=keyboardOut); + + // Output logic + Mux4Way16(a=outRam, b=outRam, c=outScreen, d=keyboardOut , sel=address[13..14] , out=out); +} \ No newline at end of file