Nand2Tetrisもくもくの続き。5章はついにコンピュータを作るのですが、まだ途中です。
コンピュータシステムの理論と実装 ―モダンなコンピュータの作り方
Memory
nand2tetris-memo/Memory.hdl at master ・ hirak/nand2tetris-memo
3章で作ったRAM16Kという16kBのチップと、新たに入出力を操作するためのメモリマップ領域を連結して、メモリが完成となります。
ただ、3章のRAM16Kと似たような感じで簡単なはずなのにずっとテストが通らず、困っていました。
どうやら、今まで私はアドレスの数え方を間違っていたみたい。添字が逆でした。
バスに何か数字をセットしたとすると、添字の小さいほうが小さい方の桁に相当するようです。よく見たら326ページに書いてあった。今までよくテストをパスしてきたな。。
例えば0001
だったら、bus[0]==1, bus[1]==0, bus[2]==0, bus[3]==0。
例えば1000
だったら、bus[0]==0, bus[1]==0, bus[2]==0, bus[3]==1。
// 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:
DMux(in=load, sel=address[14], a=ramload, b=ioload); // 1 {
DMux(in=ioload, sel=address[13], a=scrload, b=kbload); // 2 {
RAM16K(in=in, load=ramload, address=address[0..13], out=ramout);
Screen(in=in, load=scrload, address=address[0..12], out=scrout);
Keyboard(out=kbout);
Mux16(a=scrout, b=kbout, sel=address[13], out=ioout); // } 2
Mux16(a=ramout, b=ioout, sel=address[14], out=out); // } 1
}
次はCPUですが、かつて無い複雑さなので苦戦中。。
とは言え、だいぶヒントが書いてあるので、部分ごとに実装すれば何とかなりそうな感じです。