Re: Setting A in C-instruction

Posted by WBahn on
URL: http://nand2tetris-questions-and-answers-forum.52.s1.nabble.com/Setting-A-in-C-instruction-tp4036936p4036958.html

eatmorepies wrote
Thanks for your comments. I took a break to read and work a bit on the Chapter 6 project and then I came back to this. I think I have a good understanding on what you're stating; would you be able to run through my reasoning to make sure?

Suppose the first program instruction is @17. The program counter's value is 0, which is sent to the ROM. The ROM outputs the aforementioned program instruction to the CPU.
So far so good.

In the CPU, the Mux16 outputs the program instruction to the a register, and loads it based on the leftmost instruction bit.
Assuming by "the Mux16" you mean the Mux that feeds the A-register, they yes. Although the A-register doesn't change until the next clock happens.

This value is sent to the ROM, which outputs the m value at the corresponding a-value address and sends it to inM in the CPU.
This is off the rails. No value is sent to the ROM (remember, the ROM is where the program instructions are stored). The output of the A-register (which haven't changed yet, in this case) ALWAYS go to the address lines of the RAM and the value at that RAM location ALWAYS come in to one side of the Mux16 that feeds the Y input of the ALU. But, nothing happens with that value unless the control bits in the instruction cause something to be written somewhere. But since this is an A-type instruction, the ONLY write that happens (on the next clock edge) is the writing of the instruction to the A-register.

The PC is incremented and the CPU gets the next instruction.
Yes.

Suppose our next instruction is to set D=A. Since the ALU hasn't calculated a value yet for this instruction, the leftmost Mux16 doesn't have anything to output to the a-register; thus, we begin by outputting the value of that register. Our A register outputs its value set in the PREVIOUS instruction, and the relevant bit in our instruction outputs the A register value to the ALU. The ALU outputs A, which is loaded into D. Our new D value is outputted on the next tick. The PC is incremented and the CPU gets the next instruction.
Essentially correct, but the part about something happen "since the ALU hasn't calculated a value yet" is off base. At the beginning of this clock cycle, the value 17 from the prior clock cycle is actually written to the A-register. At this same time, the control bits in the new instruction configure the ALU and the Muxes so that they route the value from the A-register through the ALU. The output of the ALU is ALWAYS applied to the input of the D-register, but it will only get written (at the beginning of the next instruction) if the current instruction's control bits tell it to.

Suppose, then, our next instruction is A=D. Again, we start by outputting the a register. When we get to the ALU, it outputs the D register, which is sent to the leftmost Mux16. The Mux16 outputs this value to the A-register, and the value is set on the following tick.
The "we start by outputting the a register" part is misleading. Again, the A-register's output value is ALWAYS applied to the top input of the Mux that feeds the Y input of the ALU, it ALWAYS goes out to the address bus of the RAM, and it ALWAYS goes to the input of the PC. In the case of A=D, none of this matters because the control bits don't result in any of these having any effect on anything.