コンピューター理論、チョットデキルようになりたい

コンピュータサイエンスの学び直しです

学び直し24日目:5章 コンピュータアーキテクチャ④:展望、プロジェクト-Memory

記録

Hack コンピュータの Memory の HDL を実装した。既存回路の使いまわしで楽チンかと思ったけど、アドレスの扱い方の理解が足りてなくて思いのほか苦戦。。CPU とかどうなるんだろう…。またこのお方のお世話になった。
blog.tojiru.net

 

考え方

メモリマップが、下記のように割り振られている。以下、2進数で考える。

  • RAM16K:0x0000~0x3FFF(b000000000000000 ~ b011111111111111)
  • SCREEN:0x4000~0x5FFF(b100000000000000 ~ b101111111111111)
  • KEYBOARD:0x6000(b110000000000000)

まずはどのマップにアクセスするかを 15bit アドレスの上位 2bit で選択する。最上位の 14bit 目で RAM にするか IO にするかが決まる。13bit 目で SCREEN にするか KEYBOARD にするかが決まる。DMux を使ってうまいこと Load 信号を接続する。

RAM アクセスの場合は、アドレスの 0~13bit までの 14bit で、アクセス先を選ぶ。
SCREEN アクセスの場合は、アドレスの 0~12bit までの 13bit でアクセス先を選ぶ(上位 bit でマップ指定しているので、下位の bit は配線を共有してておk)。

出力するときは、load と逆の感じで RAM16K, SCREEN, KEYBOARD の各出力を Mux してあげたらよい。

Memory のコード

// 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:
    DMux(in=load, sel=address[14], a=ramload, b=ioload);
    DMux(in=ioload, sel=address[13], a=scrload, b=kbdload);

    RAM16K(in=in, load=ramload, address=address[0..13], out=ramout);
    Screen(in=in, load=scrload, address=address[0..12], out=scrout);
    Keyboard(out=kbdout);

    Mux16(a=scrout, b=kbdout, sel=address[13], out=ioout);
    Mux16(a=ramout, b=ioout, sel=address[14], out=out);
}

今日の筋トレ

今日から再開。1回休んだだけでだいぶダンベルが重くてびっくりした。継続は力なり。

  • Aメニュー(ダンベル重量 5kg)
  • リングフィット stage8 クリア