Syscall 2: Firmware, home

BenchChapter 6: Trees

B35TRAFFIC LIGHT

The task

P0, P1, and P2 drive the red, yellow, and green lamps. P3 ticks every 20 cycles.

Light red on the first tick. Red lasts two ticks, then green two ticks, then yellow one,

and around again. Only one lamp is lit at a time.

The board

The chip of TRAFFIC LIGHTChip 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 red, D2 yellow, and D3 green
Probes
CH1, CH2, and CH3

Key idea

Each lamp keeps its level until the next OUT on its pin.

Hints

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

Hint 1Show

At each change, light the next lamp and put the last one out.

Hint 2Show

IN P3 leaves the tick count in A, and a count is never 0, so it lights a lamp by itself.

Solution

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

Red two ticks, green two, yellow one.

u1SC-2 standard

IN P3
OUT P0
MOV A, 0
OUT P1
IN P3
IN P3
OUT P2
MOV A, 0
OUT P0
IN P3
IN P3
OUT P1
MOV A, 0
OUT P2
CYCLES
53
at par
ENERGY
106
at par
NODES
1
at par
INSTRUCTIONS
14
at par