Syscall 2: Firmware, home

BenchChapter 9: Special nodes

B71SERVO

The task

P0 reads a knob on CH1. Each frame, P1 sends a pulse, then rests low for 4 ticks of P2.

The pulse lasts 1 plus the reading DIV 20 ticks. P2 ticks every 20 cycles.

Read the knob once, at the start of each frame.

The board

The chip of SERVOChip u1 with 3 ports: P0 analog in, reaching V1, CH1; P1 digital out, reaching R1, D1, CH2; P2 timer 20 cycles.u1CHIP SC-2P0ANALOG INV1, CH1P1DIGITAL OUTR1, D1, CH2P2TIMER 20 CYCLES
The chip's ports, each with its kind and the parts its signal reaches. Cyan reads, lemon writes. On a narrow screen, scroll the board sideways to see all of it.
Sources
V1 3.5 V, V2 2 V square at 12.5 Hz, and V3 1 V square at 6.25 Hz
Resistors
R1 330 Ω
LEDs
D1 white
Probes
CH1 and CH2

Key idea

A reading of 45 makes a pulse of 3 ticks, and the frame is then 7 ticks long.

Hints

Each hint goes a step further than the one before. Open them one at a time.

Hint 1Show

Count B down from the pulse length; P1 is high while the count is above zero.

Hint 2Show

When the count reaches minus 4, the frame is over: read the knob again.

Solution

Reveal the Full SolutionThe complete program for SERVO. Open it only when you want the answer.

The reference program

The game's own solution. It passes every test and scores exactly par on every benchmark.

A pulse as long as the knob says, then a rest of 4 ticks.

u1SC-2 standard

F: IN P0
DIV 20
ADD 1
L: MOV B, A
JG H
MOV A, 0
H: OUT P1
IN P2
MOV A, B
SUB 1
ADD 4
JZ F
SUB 4
JMP L
CYCLES
860
at par
ENERGY
1720
at par
NODES
1
at par
INSTRUCTIONS
14
at par

Another way: Two loops

A second program that also passes. It beats par on CYCLES and ENERGY and gives some back on INSTRUCTIONS.

A loop for the pulse, then four sleeps for the rest.

u1SC-2 standard

IN P0
DIV 20
ADD 1
MOV B, A
OUT P1
H: IN P2
MOV A, B
SUB 1
MOV B, A
JNZ H
OUT P1
IN P2
IN P2
IN P2
IN P2
CYCLES
274
under par
ENERGY
548
under par
NODES
1
at par
INSTRUCTIONS
15
over par