Assembling an Interrupt Handler - INTERRUPT HANDLER -


&gt; READ FROM IN.1 THROUGH IN.4
&gt; WRITE THE INPUT NUMBER WHEN
  THE VALUE GOES FROM 0 TO 1
&gt; TWO INTERRUPTS WILL NEVER CHANGE
  IN THE SAME INPUT CYCLE







I was stuck on this one when I went to bed last night.  But of course, like any good programmer, sleeping is when I get most of my work done.

My primary goal here is fewer cycle counts.  If the above... erm, graph... is anything to go by, it looks like I could have quite a bit less cycle counts.  Secondary would be reducing the instruction count (but not at the expense of cycle counts).  I can imagine how to get it down to 7 nodes rather than 9, but I can't imagine that also makes the program more efficient (in terms of cycle counts).

So, how can I reduce the cycle counts here?



ROW 1, COLUMN 1 (IN.1)

MOV UP, ACC
START:
 MOV 0, DOWN
 JEZ CHECK
 JNZ CONTINUE
CHECK:
 MOV UP, ACC
 JEZ START
 MOV 1, DOWN
CONTINUE:
 MOV UP, ACC
 JMP START


ROW 1, COLUMN 2 (IN.2)

MOV UP, ACC
START:
 MOV 0, DOWN
 JEZ CHECK
 JNZ CONTINUE
CHECK:
 MOV UP, ACC
 JEZ START
 MOV 2, DOWN
CONTINUE:
 MOV UP, ACC
 JMP START


ROW 1, COLUMN 3 (IN.3)

MOV UP, ACC
START:
 MOV 0, DOWN
 JEZ CHECK
 JNZ CONTINUE
CHECK:
 MOV UP, ACC
 JEZ START
 MOV 3, DOWN
CONTINUE:
 MOV UP, ACC
 JMP START


ROW 1, COLUMN 4 (IN.4)

MOV UP, ACC
START:
 MOV 0, DOWN
 JEZ CHECK
 JNZ CONTINUE
CHECK:
 MOV UP, ACC
 JEZ START
 MOV 4, DOWN
CONTINUE:
 MOV UP, ACC
 JMP START




ROW 2, COLUMN 1

MOV UP, RIGHT


ROW 2, COLUMN 2

ADD LEFT
ADD UP
MOV ACC, RIGHT
MOV 0, ACC


ROW 2, COLUMN 3

ADD LEFT
ADD UP
ADD RIGHT
MOV ACC, DOWN
MOV 0, ACC


ROW 2, COLUMN 4

MOV UP, LEFT




ROW 3, COLUMN 3 (OUT)

MOV ANY, DOWN
You can swap JEZ and JNZ and then remove the noop jump to check, and because the instruction before start is the same as before the JMP to it at the end you can remove the two instructions:

MOV UP, ACC
START:
 MOV 0, DOWN
 JNZ CONTINUE
 MOV UP, ACC
 JEZ START
 MOV 1, DOWN
CONTINUE:


If you don't mind using more nodes you can simplify Node 2,3 by letting it ADD UP ADD RIGHT and MOV ACC, DOWN (and MOV 0, ACC)

Then Node 3,2 MOVs UP, RIGHT and node 3,3 is a copy of node 2,2. This removes 2,3 as a bottleneck that needs to gather 3 values and instead will let the other nodes help gather them.