Shenzhen I/O: Diagnostic Pulse Generator

The third task we get is playing solitaire, but ignoring that the third programming puzzle we get is that we need to test some manufacturing equipment. We can do that by building a diagnostic pulse generator that sends a pulsing signal to the output whenever we hold a button.

The board looks like this where the button is a simple input. When it goes high we should generate a pulse to the pulse output with a certain characteristic.

Shenzhen IO - diagnostic pulse generator

The verification signals look like

Shenzhen IO - diagnostic pulse generator - signals

So basically we need to send a pulses when ever we press the button. I have no less than three different solutions.

The straight forward solution

The first solution is the solution you should be able to make based on the knowledge you have from the game and the manual by now. It uses conditional expressions for the first time in the game.

Let us first see how we can generate a pulsing signal. After that we will deal with when to make it. There is a small trick to this. Which requires us to use an arithmetic instruction

That is the not operation – which will flip the acc from 0 to 100 or vice versa.

This is a pretty useful in this case, as we can just trigger not, and then move acc to p1 then we have a pulsing signal!

For the condition around when to generate the pulse we need to use conditionals we can use the following

tgt R/I R/I – this will test if the first operand is greater than the second operand. If that is the case any instruction marked with a + afterwards will be executed. If that is false any operation marked with a – will be triggered.

Which leads us to the following code

  tgt p0 0
+ not
- mov 0 acc
  mov acc p1
  slp 1

which in the game looks like

Shenzhen IO - diagnostic pulse generator - Solution 1

We test to see if the button is pressed. If that is the case we start sending the pulsing signal. If the button is unpressed we reset the acc to zero. Then we move the acc to p1 and sleep.

Second diagnostic pulse generator solution

When we analyse the output we will see that we cannot unpress the button and then press it again to reset the signal. The game is simply not made that way.

This means that we can just generate a pulse that is high and then low and send that if the button is pressed and then after two cycles check if the button is still pressed. Otherwise we should just sleep. Yes that means we will not check to see if the button is pressed every cycle, but that is still perfectly valid.

This solution lowers the lines of code as well as energy consumption of the solution.

Shenzhen IO - diagnostic pulse generator - Solution 2

Third option

The last option is more expensive than the first two. But we can solve the puzzle with one line of code

We add a DX300 to the solution so we make an xbus signal (which I haven’t dived into yet) that will be 1 when the button is pressed otherwise it is zero. Then we just read that and use the gen instruction to make a pulse pulse of either 0 or 1 in length and then a low output for 1 cycle as well. Tada – minimum possible lines of code I have managed for the diagnostic pulse generator.

Shenzhen IO - diagnostic pulse generator - Solution 3


Posted by Kristian

Leave a Reply