Syscall 2: Firmware, home

BenchChapter 10: Meshes

B73DEBOUNCE

The task

P0 reads a button on CH1. A sample of 50 or more is pressed, under 50 released.

The contacts chatter as they let go. Sample on each tick of P2.

Change the LED on P1 only after three samples in a row agree.

The board

The chip of DEBOUNCEChip 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 2 V square at 12.5 Hz, V2 1 V square at 25 Hz, V3 1 V square at 125 Hz, and V4 0.9 V
Resistors
R1 330 Ω
LEDs
D1 green
Probes
CH1 and CH2

Key idea

DIV 50 turns a sample into 1 for pressed or 0 for released.

Hints

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

Hint 1Show

B can hold the last three samples as bits: double it, add the new one, keep MOD 8.

Hint 2Show

Three released samples make 0, so write 0; three pressed make 7, so write 1.

Solution

Reveal the Full SolutionThe complete program for DEBOUNCE. 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.

The last three samples as bits in B; all zero or all one moves the LED.

u1SC-2 standard

L: IN P2
IN P0
DIV 50
ADD B
ADD B
MOD 8
MOV B, A
JZ W
SUB 7
JNZ L
MOV A, 1
W: OUT P1
CYCLES
853
at par
ENERGY
1706
at par
NODES
1
at par
INSTRUCTIONS
12
at par