Syscall 2: Firmware, home

BenchChapter 12: Shared buses

B93TARE

The task

P0 reads a scale on CH1 as a percent. P1 reads its TARE button on CH2.

While TARE is pressed, the reading becomes the new zero. The zero starts at 0.

Show the reading minus the zero as the duty of P2, smoothed onto CH3.

Light P3 while the reading is below the zero, as when a tared load comes off.

The board

The chip of TAREChip u1 with 4 ports: P0 analog in, reaching V1, CH1; P1 digital in, reaching V4, CH2; P2 pwm out, reaching R1, C1, CH3; P3 digital out, reaching R2, D1, CH4.u1CHIP SC-2P0ANALOG INV1, CH1P1DIGITAL INV4, CH2P2PWM OUTR1, C1, CH3P3DIGITAL OUTR2, D1, CH4
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 1.5 V, V2 1 V square at 4.167 Hz, V3 0.5 V square at 2.222 Hz, V4 5 V, and V5 5 V square at 10 Hz
Resistors
R1 10 kΩ and R2 330 Ω
Capacitors
C1 1 µF
LEDs
D1 red
Probes
CH1, CH2, CH3, and CH4

Key idea

Keep the zero in B. A pressed button copies the reading into B.

Hints

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

Hint 1Show

The PWM pin treats a duty under 0 as 0, so the reading minus B can go out as it is.

Hint 2Show

A negative value lights a digital pin as well as 1 does: after JL, write it as it is.

Solution

Reveal the Full SolutionThe complete program for TARE. 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 zero taken while the button is down, and a lamp for a net below it.

u1SC-2 standard

L: IN P1
JZ M
IN P0
MOV B, A
M: IN P0
SUB B
OUT P2
JL N
MOV A, 0
N: OUT P3
CYCLES
3000
at par
ENERGY
6000
at par
NODES
1
at par
INSTRUCTIONS
10
at par

Another way: One read

A second program that also passes, by a different road, at a cost on INSTRUCTIONS.

Writes 0 to both pins while the button is down.

u1SC-2 standard

L: IN P1
JZ M
IN P0
MOV B, A
MOV A, 0
JMP O
M: IN P0
SUB B
O: OUT P2
JL N
MOV A, 0
N: OUT P3
CYCLES
3000
at par
ENERGY
6000
at par
NODES
1
at par
INSTRUCTIONS
12
over par