Sebastian Nielsen
Sebastian Nielsen

Reputation: 4209

(Nand2tetris CPU) (What/How much) happens in each clock cycle?

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

Upvotes: 2

Views: 2393

Answers (2)

mmm111mmm
mmm111mmm

Reputation: 4085

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.

Upvotes: 2

Trebor the MadOverlord
Trebor the MadOverlord

Reputation: 1289

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.

Upvotes: 1

Related Questions