2

On the basis of the Nand2tetris CPU, as shown below, I would like to know the following:

  1. (What/How much) happens in each clock cycle? (see IMG_1 and IMG_2)

  2. As a follow-up question to question 1, when should the program counter change?


Note that I know what a clock is and when a

IMG_1

enter image description here


IMG_2

enter image description here

Sebastian Nielsen
  • 3,835
  • 5
  • 27
  • 43

2 Answers2

2

A DFF will “latch on to” an input in the tick phase. Then output it in the tock phase.

Say you have an A instruction. In the tick phase, the A register latches onto the value in the instruction. But the old A register value is still the output. In the tock phase the A register finally outputs the new value. You can verify this in the hardware simulator.

The reason we need the tick tocks is circuit feedback.

Say you want to do A=A+D. And you already have A and D values. And there’s no tick tock in the clock:

  • The A register value rushes to the ALU
  • The D register value does the same
  • The ALU output returns to the A register
  • And we start from the top again with a new A value...

... until we decide to read the value. You thus can’t determine how many times the add ALU instruction will be performed.

But with a tick tock clock, in the tick phase the A value leaves the A register and goes into the ALU and then the ALU value reaches back to the A register. But only in the "tock" does the A register start to output this new value. You can thus use the tick tock of the clock to determine the state of the CPU.

And the program counter is a DFF too. So in the tick it will latch onto a new value (an increment or where to jump to). And only in the tock will it output that to the ROM.

I had to study flip flops using youtube tutorials and a few answers of stack overflow for electronics to understand all this.

mmm111mmm
  • 3,607
  • 4
  • 27
  • 44
  • 1
    Can you further explain this sentence? "And we start from the top again with a new A value for however long the clock cycle is. You thus can’t determine how many times the add ALU instruction will be performed." Why do we start from the top again? You also said in this scenario we have no notion of tick-tock, so what's the clock you're talking about here? – HelpMe Oct 26 '21 at 13:34
  • You're right. "clock cycle" is confusing. I mean it will continue until we decide to read the value. I've edited it. – mmm111mmm Oct 26 '21 at 18:00
  • Thank you. As for - "And we start from the top again with a new A value until we decide to read the value." - Why is that? Why does the ALU keep calculating until we actually read the A value he has just modified? And as for - "But with a tick tock clock, in the tick phase the new A value leaves the A register and goes into the ALU and then the ALU value reach back to the A register." - Did you mean to say "the old A value leaves..."? So you say that in the tick-tock clock, the tick phase "processes and calculates" the data, and the tock phase displays the results of this process? – HelpMe Oct 26 '21 at 18:14
  • Well, it loops because there's nothing that says "and then stop": it's just an electrical circuit. The ALU is just reading values and outputting values in a circuit. It just happens that what it outputs then becomes its input again. – mmm111mmm Oct 26 '21 at 19:21
  • I've removed "new" from the sentence. Perhaps it's clearer now. – mmm111mmm Oct 26 '21 at 19:24
  • Something to say to ALU to stop? Sounds peculiar... I've actually done the NAND2Tetris course but this topic is the vaguest one I've encountered there. So, you actually say that the tick-tock mechanism somehow tells the ALU to stop reading-calculating the same input. What is the thing in this mechanism that stops the ALU from looping this way? @mmm111mmm – HelpMe Oct 26 '21 at 20:45
  • It's less saying "stop" but more "when" to read the value. And that when is on the "tock". And you can determine what that value will be. Because you know the values rush into the ALU on the 'tick', but only arrives at the A register from the ALU again on the 'tock. – mmm111mmm Oct 26 '21 at 23:19
  • Yes, but in case we don't have this tick-tock mechanism, why would the ALU calculate the same thing multiple times? What does it stem from? – HelpMe Oct 28 '21 at 11:43
1

The Nand2Tetris emulation glosses over a lot of the subtleties of clocking. There are no tick/tock phases. Instead, there is a special DFF (data flip-flop) component that addresses the need for memory.

The way to visualize it is to divide components into those without an internal state (those which are pure functions of their inputs, such as anything built without DFFs). These just instantly change their output state when their inputs change.

A DFF basically computes its new state as a pure function of its inputs, but delays outputting that state for one cycle. So it has an internal memory of its state in the previous cycle. This turns out to be all you need to implement the equivalent of a tick/tock clock.

See https://docs.wixstatic.com/ugd/56440f_e458602dcb0c4af9aaeb7fdaa34bb2b4.pdf for much more detail on this.

MadOverlord
  • 1,034
  • 6
  • 11