Syscall 2: Firmware, home

BenchChapter 6: Trees

B42COUNTER

The task

Three LEDs count the ticks of P3 in binary: P0 is the ones bit, P1 the twos, P2 the fours.

Show the count of ticks so far, and start over at 0 after 7.

The board

The chip of COUNTERChip u1 with 4 ports: P0 digital out, reaching R1, D1, CH1; P1 digital out, reaching R2, D2, CH2; P2 digital out, reaching R3, D3, CH3; P3 timer 20 cycles.u1CHIP SC-2P0DIGITAL OUTR1, D1, CH1P1DIGITAL OUTR2, D2, CH2P2DIGITAL OUTR3, D3, CH3P3TIMER 20 CYCLES
The chip's ports, each with its kind and the parts its signal reaches. Cyan reads, lemon writes.
Resistors
R1 330 Ω, R2 330 Ω, and R3 330 Ω
LEDs
D1 green, D2 yellow, and D3 red
Probes
CH1, CH2, and CH3

Key idea

IN P3 gives the number of ticks so far.

Hints

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

Hint 1Show

MOD 2 of a number is its ones bit. Divide by 2 first for the twos bit, by 4 for the fours.

Hint 2Show

Keep the count in B and bring it back for each bit.

Solution

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

Three LEDs count the ticks in binary.

u1SC-2 standard

IN P3
MOV B, A
MOD 2
OUT P0
MOV A, B
DIV 2
MOD 2
OUT P1
MOV A, B
DIV 4
MOD 2
OUT P2
CYCLES
277
at par
ENERGY
554
at par
NODES
1
at par
INSTRUCTIONS
12
at par