0

I'm trying to work through the CPU.cmp file, to write out the instructions and see if what's written in CPU.cmp makes sense.

On line 17 (time 8)

|time| inM  |  instruction   |reset| outM  |writeM |addre| pc  |DRegiste|
|6+  |     0|0000001111101001|  0  |*******|   0   | 1000|    6|  11111 |a @1001
|7   |     0|0000001111101001|  0  |*******|   0   | 1001|    7|  11111 |a
|7+  |     0|1110001110011000|  0  |  11110|   1   | 1001|    7|  11110 |c MD = D-1; null
|8   |     0|1110001110011000|  0  |  11109|   1   | 1001|    8|  11110 |c

As you can see, the value of the D register decrements by 1 from (decimal) 11111 to 11110, and the value of the outM reflects that. However, then outM decrements again, to 11109. Why does it do that? The instruction is MD = D-1, so it should decrement D reg once, and store the value in two locations. How does it happen that RAM[A] and D end up with different values?

I expected them to be the same...

1 Answers1

0

At tick 7+ CPU has to perform instruction MD=D-1 as you figured out already.

At this point D=11111.

Instruction is D-1 and the CPU will internally calculate this value, so outM( output bus, not actual memory) is D-1 (11110) and register D = 11110 as well. At tock 8 write to memory happens so 11110 is saved at address in register A. CPU still calculates D-1 so outM = 11109, completely correct behaviour of CPU, we don't use this value but it represents result of last instruction (still D-1).

To expand on ticks and tocks:

tick is the 1 on clock, it's the part of the cycle where all outputs and registers have time to change and stabilize their internal values. Registers being loaded output their changing internal state immediately as input changes.

tock is the part that clocked parts start emitting their internal state and won't change their internal values.

That's why DRegister has the same value as outM on tick, ALU feeds D-1 as their value. Then comes tock and D starts emitting its new value(d-1) and ALU calculates its new value (you see it as D-2). D doesn't change because in tock phase clocked parts don't change their value.

Also to answer

How does it happen that RAM[A] and D end up with different values

They don't, outM and DRegister end up with different values. outM is not clocked, it's direct output of ALU, which calculates out(D) - 1. DRegister has the same ALU output as an input but it won't update its value because of clock phase.

zubergu
  • 3,646
  • 3
  • 25
  • 38
  • "CPU still calculates D-1 so outM = 11109, completely correct behaviour of CPU" I'm not getting something still. If it's completely normal that outM line changes again, then why doesn't Dregister also change at tock 8? Isn't the Dregister fed directly by outM? I think I'm not getting something about the ticks and the tocks. Is tick when the fetching of instruction happens, and tock the execution? – aNameLikeAnyOther Aug 14 '19 at 12:44
  • Please, see the updated answer. I still recommend to go through the project. This course takes amazing incremental approach and projects are pretty much self explanatory this way. – zubergu Aug 14 '19 at 13:35