Traffic Light Simulation in Emu8086: A Worked Example

Emu8086 ships with a built-in Traffic Lights virtual device: a four-way junction with twelve lamps, four green, four yellow and four red. It is the standard first virtual-hardware exercise because the whole interface is a single output instruction, yet it forces you to think about bit patterns, timing loops and sequencing, the same skills real embedded code needs.

How the device is controlled

Open the device from the emulator’s virtual devices menu before you run your program, so the lamps are visible while the code executes. The device is controlled by sending a word to I/O port 4. Only the low twelve bits of the word are used, one bit per lamp: a bit set to 1 turns its lamp on, and a bit set to 0 turns it off. The top four bits are unused. You write the word with the OUT instruction:

MOV AX, 0000001011110100b
OUT 4, AX

That example pattern comes from the emulator’s own documentation for the device. The same documentation points to the bundled example file traffic_lights.asm in the emulator’s examples folder, which is worth opening alongside this post: it shows a full junction sequence, and watching its patterns change is the fastest way to learn which bit drives which lamp in your copy of the device. Run a pattern, look at the lamps, and write down the mapping you observe rather than memorising a table from a web page.

Worked example: two states, alternating with a delay

A traffic light program is a loop of three steps: output a pattern, wait, output the next pattern. The delay is a nested counting loop, because the emulator has no sleep call for this device. This example alternates two patterns so you can see the mechanism clearly before building a full junction cycle:

ORG 100h

cycle:
; State 1: one pattern of lamps on.
MOV AX, 0000001011110100b
OUT 4, AX
CALL delay

; State 2: a different pattern of lamps on.
MOV AX, 0000000100001011b
OUT 4, AX
CALL delay

JMP cycle            ; run the junction forever
RET

; A simple delay: two nested counters.
delay PROC
PUSH CX
PUSH DX
MOV CX, 0FFFFh
wait_outer:
MOV DX, 0FFFFh
wait_inner:
DEC DX
JNZ wait_inner
DEC CX
JNZ wait_outer
POP DX
POP CX
RET
delay ENDP

The delay length is set by the counter values, not by seconds, so the same code runs at different visible speeds on different machines. Shorten the counters if the cycle changes too slowly, lengthen them if it changes too fast. If the lamps never change, the usual causes are that the device window was not opened before running, or that the program was assembled as a different template that never reaches the OUT instruction.

Building a full junction cycle

Once the two-state example runs, extend it one state at a time: one direction green while the others are red, then yellow as a warning, then red, then the next direction. Keep each state as one OUT followed by one delay call, and keep a written list of which pattern means which state, because twelve bits are easy to confuse once you have six or eight states. Test each new state on its own before adding it to the cycle, so a wrong pattern is caught when it appears rather than after the whole sequence is built.

From simulation to real hardware

The ideas transfer directly: a real junction controller also outputs bit patterns to lamps and waits between states, except that its outputs drive real circuits through driver hardware instead of a simulated window. The companion guide from simulation to real hardware covers what changes when the lamps stop being virtual. For the instruction set behind OUT, see the MOV and JMP and LOOP posts, and for the bit-level thinking, see addressing modes and flags.

Further reading

New to assembly? Start with Your First 8086 Program: Hello World, then return here.

Similar Posts