Diagnostic Pulse Generator

2022-02-14

Part of my journey through Shenzhen I/O

Problem: When the button is pressed (simple I/O on pin p0), send an alternating signal out on p1. If the button is not pressed, send 0 on p1.

First Attempt

My initial solution stored the state of alternation in acc, but used more power than most:

  teq p0 100
+ not
+ mov acc p1
- mov 0 acc
- mov 0 p1
  slp 1

I saw the issue: for the negative case, the program is needlessly issuing mov 0 p1 and mov 0 acc on every pass through the program.

Cost is ¥3, power usage is 240, and 6 lines of code are necessary.

Second Attempt

The key insight is that rather than thinking of each loop through the program as a single cycle, the conditional can embed two cycles in the positive case, and falling through efficiently in the negative case.

  teq p0 100
+ mov 100 p1
+ slp 1
+ mov 0 p1
  slp 1

Cost for this solution is ¥3, power usage is 142 (!), and 5 lines of code are necessary.

One solution I ran across in discussions online involved the use of the gen command, which is not documented in the version of the game I got from GOG! I'll have to keep progressing to find out if it is introduced later on in the game.