Syscall 2: Firmware, home

BenchChapter 8: Scarcity

B60H BRIDGE

The task

P0 reads a direction knob on CH1. P1 and P2 drive the two halves of a motor's H bridge.

Over 50, drive P1 with twice the reading's distance from 50; under 50, drive P2 that way.

The other half stays at 0. CH2 and CH3 show the two duties, smoothed.

The board

The chip of H BRIDGEChip u1 with 3 ports: P0 analog in, reaching V1, CH1; P1 pwm out, reaching R1, C1, CH2; P2 pwm out, reaching R2, C2, CH3.u1CHIP SC-2P0ANALOG INV1, CH1P1PWM OUTR1, C1, CH2P2PWM OUTR2, C2, CH3
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 10 kΩ and R2 10 kΩ
Capacitors
C1 1 µF and C2 1 µF
Probes
CH1, CH2, and CH3

Key idea

The distance from 50, doubled, runs from 0 at the middle to 100 at either end.

Hints

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

Hint 1Show

A PWM pin treats a duty under 0 as 0, so a negative value turns its half off by itself.

Hint 2Show

Write (reading minus 50) times 2 to P1, then the same value negated to P2.

Solution

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

One signed value serves both halves, since the PWM pins clamp at 0.

u1SC-2 standard

IN P0
SUB 50
MUL 2
OUT P1
NEG
OUT P2
CYCLES
1600
at par
ENERGY
3200
at par
NODES
1
at par
INSTRUCTIONS
6
at par

Another way: Branch

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

Tests the side first and writes 0 to the other half.

u1SC-2 standard

L: IN P0
SUB 50
JL R
MUL 2
OUT P1
MOV A, 0
OUT P2
JMP L
R: MUL -2
OUT P2
MOV A, 0
OUT P1
CYCLES
1600
at par
ENERGY
3200
at par
NODES
1
at par
INSTRUCTIONS
12
over par