# Assembling an Interrupt Handler

### - INTERRUPT HANDLER -

> READ FROM IN.1 THROUGH IN.4
> WRITE THE INPUT NUMBER WHEN
THE VALUE GOES FROM 0 TO 1
> 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


## 2 Answers

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.

I have code in the top nodes that has 3 sections. In Pseudo:

Label ZERO       # is called while the input == 0
Sub UP           # ACC will still be 0 if no input,
# or -1 if we are now receiving
JMP TOGGLE_ONE   # if ACC < 0 or
Move 0 down      # no input change
JMP ZERO         # if ACC == 0

Label TOGGLE     # Is called once on input change
Move X down      # X = the node's number
JMP ONE_STANDS

Label ONE_STANDS # is called as long as the input == 1
MOVE UP, ACC     # Read input to ACC
# Note that ACC is now 0, should we jump to ZERO
MOVE 0, down     # whether input is 0 or 1, we don't send X in this case
JMP ZERO         # if ACC == 0
JMP ONE_STANDS   # if ACC != 0


By always sending out a 0 or a [1 - 4] on each cycle, we can now use the power of parallel processing! On the middel row of processors, shunt the outermost pipes to the middle two, and use

MOV UP, ACC
ADD[LEFT/RIGHT]
MOV ACC, DOWN


to sum the interrupt. Repeat for the two sums on the bottom row and MOV DOWN.

Try and build this; if you want, I can post the actual code for the top nodes.