头图

// 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/3/a/PC.hdl
/**
 * A 16-bit counter.
 * if      reset(t): out(t+1) = 0
 * else if load(t):  out(t+1) = in(t)
 * else if inc(t):   out(t+1) = out(t) + 1
 * else              out(t+1) = out(t)
 */
CHIP PC {
    IN in[16], reset, load, inc;
    OUT out[16];
    
    PARTS:
    // Step 1: 内部信号regOut,保存当前PC的值
    Register(in=muxOut, load=true, out=regOut);

    // Step 2: Increment logic - 对PC值进行自增
    Inc16(in=regOut, out=incOut);

    // Step 3: inc 选择
    Mux16(a=regOut, b=incOut, sel=inc, out=incMuxOut);

    // Step 4: load选择
    Mux16(a=incMuxOut, b=in, sel=load, out=loadMuxOut);

    // Step 5: reset选择
    Mux16(a=loadMuxOut, b=false, sel=reset, out=muxOut);

    // Step 6: 最终值存入寄存器,并输出到out
    Register(in=muxOut, load=true, out=out);
    
}

一直没搞懂怎么保存上一步的out,看了论坛才明白,老师说HDL不是过程语言,不存在先后顺序,因为电路也没有绝对的顺序。这里学到的是:

  1. 多使用DMux和Mux
  2. out不能直接作为输入,所以要保存当前out,可以用Register(in=muxOut, load=true, out=regOut),通过将load永远设为true,得到上一步的out-> regOut,最后经过一些列select将新的out保存入muxOut中,再将新的muxOut作为输出传入out,这样就不会出现circle

hai_detto
1 声望0 粉丝

引用和评论

0 条评论