Syscall 2: Firmware, home

BenchChapter 9: Special nodes

B64LOG SCALE

The task

P0 reads a signal on CH1 that spans a wide range. Show it on a log scale through P1.

Each doubling of the reading adds 14 to the duty: 1 gives 0, 2 and 3 give 14, 4 to 7 give 28.

R1 and C1 smooth the duty onto CH2.

The board

The chip of LOG SCALEChip u1 with 2 ports: P0 analog in, reaching V1, CH1; P1 pwm out, reaching R1, C1, CH2.u1CHIP SC-2P0ANALOG INV1, CH1P1PWM OUTR1, C1, CH2
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.4 V triangle at 4 Hz and V2 2.5 V
Resistors
R1 10 kΩ
Capacitors
C1 1 µF
Probes
CH1 and CH2

Key idea

Halve the reading with DIV 2 until it reaches 0; each halving that leaves something counts.

Hints

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

Hint 1Show

Keep the duty in B and the shrinking value in A; XCH swaps them for the add.

Hint 2Show

Clear B, read P0, then loop: DIV 2, JZ to the end, XCH, ADD 14, XCH, JMP back. Write B.

Solution

Reveal the Full SolutionThe complete program for LOG SCALE. 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 number of halvings, which is the log in base 2.

u1SC-2 standard

MOV A, 0
MOV B, A
IN P0
L: DIV 2
JZ E
XCH
ADD 14
XCH
JMP L
E: MOV A, B
OUT P1
CYCLES
2400
at par
ENERGY
4800
at par
NODES
1
at par
INSTRUCTIONS
11
at par

Another way: Compare

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

Compares against 64, 32, 16, and so on.

u1SC-2 standard

MOV A, 84
MOV B, A
IN P0
L: SUB 64
JG E
JZ E
ADD 64
ADD A
XCH
SUB 14
XCH
JG L
E: MOV A, B
OUT P1
CYCLES
2400
at par
ENERGY
4800
at par
NODES
1
at par
INSTRUCTIONS
14
over par