Sequential Logic - Nand2Tetris Unit 03

I’m following along the course nicely and making progress every day. It feels good.

This unit, I’ll be attacking the sequential logic, where everything up to know has been combinational. We’re going from everything happening in parallel, to outputs that depends on previous state.

What I Did Built This Week

  • Bit - The 1 bit register. Store a 1 or 0. Used the DFF(Data Flip Flop)* & Mux from Unit 01.

  • Register - 16-bit register. Uses 16 Bits in parallel to store a 16-bit number.

  • RAM8, RAM64, RAM512, RAM4K, RAM16k - Memory Hierarchy

  • Program Counter - The outlier of this Unit. A single register with load/reset/increment logic.

  • The Data Flip Flop is labeled as primitive in this course and is not something we built ourselves. It’s given. The data flip flop is a logic chip that stores a single bit and outputs it on the next clock cycle.

Poorly handwritten sketch diagram of the Data Flip Flop

Data Flip Flop Diagram

Key Insight

Using a Data Flip Flop plus a Mux creates memory through sequential logic. We are now creating a clock cycle which looks like an oscillator. The current clock cycle is always dependent on the cycle before. Either through load, reset or increment.

There’s a constant feedback loop going on.

Now it is beginning to feel like a computer that we know.

My Struggle

Again, my programmers mindset.

  • bit[0] is not the leftmost value in a register. It’s the rightmost(least significant bit). This I must remind myself of.
  • Figuring out how to access the different bits in RAM arrays. How the address pointer is used to split down through the layers of RAM.

Address Reminder

000000 → register 0 in RAM8 #0 000001 → register 1 in RAM8 #0 000010 → register 2 in RAM8 #0 … 001000 → register 0 in RAM8 #1 (high bits changed)

Learning that the DMux and Mux from Unit 01 is actually write/read logic. It made sense when the RAM8 was built and understood.

Low bits select within a group, high bits select which group. Similar to apartment buildings.

  • High bits pick the building
  • Low bits pick the apartment.

After RAM8 clicked it was just layering the RAMs on top of each other to build the next one.

RAM8 Implementation

CHIP RAM8 {
    IN in[16], load, address[3];
    OUT out[16];

    PARTS:

    // Only writing if load = 1

    // Selecting which address 16-bit register to write to 
    DMux8Way(
        sel=address,
        in=load,
        a=load1,
        b=load2, 
        c=load3, 
        d=load4, 
        e=load5, 
        f=load6, 
        g=load7, 
        h=load8, 
    );
    // Writing to the selected register
    Register(in=in , load=load1 , out= regOut1);
    Register(in=in , load=load2 , out= regOut2);
    Register(in=in , load=load3 , out= regOut3);
    Register(in=in , load=load4 , out= regOut4);
    Register(in=in , load=load5 , out= regOut5);
    Register(in=in , load=load6 , out= regOut6);
    Register(in=in , load=load7 , out= regOut7);
    Register(in=in , load=load8 , out= regOut8);

    // Outputting the register that is selected
    Mux8Way16(
        a=regOut1,
        b=regOut2,
        c=regOut3,
        d=regOut4,
        e=regOut5,
        f=regOut6,
        g=regOut7,
        h=regOut8,
        sel=address,
        out=out
    );
}

Had to read a little bit up on Logarithm and why.

  • 2^3 = 8
  • 2^4 = 16
  • 2^5 = 32
  • 2^6 = 64

And so on… We have the powers of 2 everywhere in computing.

What Surprised Me

How simple memory actually is when you really dig into it. Again, the 0’s and 1’s. I know it gets more complicated than this, but it isn’t this magic that you’re sometimes led to believe. Gate with feedback loops.

Also the Program Counter. Haven’t mentioned it a lot but it’ll be a integral part of managing the CPU in upcoming chapters.

RAM Hierarchy. Just building on top of each other and slicing the address bits for really going in deep the bigger the RAM gets. Also shows how the abstractions scale. 8 -> 64 -> 512 -> and so on.. Always the building on top of each other.

Now what?

Unit 04. Machine Language. We’ll be moving on from Chips to Machine language. Attacking a language written for this course called Hack. Similar to Assembly, but some differences. Looking forward to it!

See you around!