I've been hacking around with Hack, and one of the things I was trying to do was a jump through a pointer in memory. The code for this would be:
@pointer
A=M;JMP
where
pointer is a RAM variable that contains a code address I want to jump to.
As per the discussion in this topic, this works in the CPU simulator but not in the HW simulator with my CPU. I thought about it a bit, and came up with this fix that makes it work as expected (and as implemented in the CPU simulator).
What we want to happen is to jump to the new value of A if it's being updated, otherwise jump to the stored value of A. This can be done by adding a single mux:
ARegister(load=a-ld, in=a-in, out=a-reg);
Mux16(sel=a-ld, a=a-reg, b=a-in, out=jmp-addr);
PC(in=jmp-addr, reset=reset, inc=true, load=pc-ld, out=pc); // was in=a-reg
I haven't tried this with my modified CPU yet, but you should be able to test for a null pointer and jump if it's OK in a single instruction.
@pointer
A=M;JNZ
// continue here if pointer is null
Happy Hack hacking!
--Mark