pico6809 Developer's Guide

pico6809 Developer's Guide

This guide is for developers working on the pico6809 firmware: where the code lives, how the 6809 port differs from the 6502 and Z80 ports it is modelled on, how a persona driver plugs in, how to build and test, and how to run the three simulators. It assumes the Technical Guide and the shared framework described in the pico6502 Developer's Guide.
All paths are relative to projects/tzpuPico in the tzpuPico repository unless shown otherwise. The 6809 work is new (October 2026) and has so far been developed and verified in simulation only.

Source Tree

Piece 6502 equivalent 6809 files
CPU core M6502.c, M6502.h src/M6809.c, src/include/M6809.h: one core; cpu->h6309 and cpu->native (MD bit 0) select the instruction set and cycle tables
Routing and persona glue M6502CPU.c/.h src/M6809CPU.c, src/include/M6809CPU.h: membank map, PSRAM, physical/virtual routing, drivers, dbgsh hooks
Bus layer (in M6502CPU.c) src/M6809CPU_bus.inl, included by the target section of M6809CPU.c
Read shadow (in M6502CPU.c) src/M6809CPU_shadow.inl
PIO programs M6502.pio src/M6809.pio
Board pin map M6502CPU.h src/include/M6809pins.h (PICO6809 v2.3)
Disassembler / assembler M6502dis.c src/M6809dis.c, src/include/M6809dis.h: indexed post-byte decode, page 10/11 prefixes, the 6309 additions
Debug shell dbgsh.c ops table m6809Ops in src/dbgsh.c
Generic persona Open6502.c src/drivers/Other/Open6809.c
Dragon personas (BBC drivers) src/drivers/Dragon/Dragon.c, DragonDOS.c, DragonCMT.c, src/include/drivers/Dragon/Dragon.h
Floppy controller WD177x.c the same src/drivers/WD177x.c, with the WD177X_2797 variant; also the WD2797 catalogue device in src/devices.c
Disc images (in the BBC FDC) src/drivers/FDImage.c, src/include/drivers/FDImage.h: boot mount, sector write-back, run-time change/eject, Floppy-menu publishing; shared by DragonDOS and the WD2797 device
Firmware models Base6502, BBC6502 src/model/Base6809, src/model/Dragon6809, built by add_m6809_model_targets() in src/model/CMakeLists.txt
Core test suite src/test/M6502 src/test/M6809 (XRoar oracle)
Simulators sim/6502 sim/6809 (stage A), sim/xroar (stage B), sim/pio6809 (PIO bench)
The design record, with every decision and measurement, is docs/PICO6809_DESIGN.md.

Key Differences from the 6502 Port

  • The core emits cycles, not just results. M6809.c calls the bus layer once per bus cycle with a 32-bit descriptor (kind, address, data, status flags). Any code that adds a memory access to the core must add it as the cycle the real chip performs, in the right place in the cycle script, or the bus trace against XRoar will fail.
  • Lateness is safe. The PIO pads with $FFFF dummy cycles when the C side is late, so there is no equivalent of the 6502's dangerous stale-address repeat. Slow code costs speed, visible as the late-dummy count in regs and cpustat, not correctness.
  • Read kinds are the only synchronisation point. In the bus-layer API, busCycle queues WRITE, DUMMY and DISCARD without waiting; read kinds block for their data. With the read shadow on, most reads are served locally, so C runs ahead of the bus.
  • Lines are latched per cycle. Do not sample /IRQ, /FIRQ, /NMI or /HALT anywhere but through the core's line contract (linesCaps in M6809.h). The board's bus layer declares M6809_LINECAP_NMIEDGE | M6809_LINECAP_POSTLIC in busAttach.
  • No I/O space, no io array. As on the 6502, all peripherals are memory-mapped, through FUNC blocks or a physical snoop.
  • Not carried over from the 6502: the BBC bus-timing machinery (address ladder, burst streaming, pacing). The 6809's dummy cycles and the read shadow made it unnecessary.

Bus-Layer API

M6809CPU.h defines the interface between the routing layer and a bus layer. The board implementation is M6809CPU_bus.inl; the simulators provide their own (sim/src/bus6809_sim.c, sim/src/bus6809_xroar.c) that consume the same descriptors, which is how the descriptor stream was exercised before any PIO existed.
Function Purpose
busCycle Issue one cycle descriptor; blocks only for read kinds
busFlush Drain queued cycles (used at SYNC/HALT transitions and mode changes)
busLines Sample /IRQ /FIRQ /NMI /HALT (GPIO32–36) and the NMI edge catcher
busResetAsserted / busHostReset Host /RESET handling
busEnable Drive BUSFLOAT; switches at an idle late-dummy
busAttach Connect the bus layer and declare its line-sampling capabilities
The crystal-mode controls M6809CPU_busSetCrystal and M6809CPU_busSetTiming exist and are exercised by the PIO bench, but no persona key calls them yet (phase 8).

Writing a Persona Driver

A 6809 driver follows the pico6502 driver framework (see Writing a Driver): it registers under a name used in the persona's drivers[].name, parses its own JSON keys, sets up memory blocks and FUNC handlers at init, and does slow work (SD card, flash) on Core 0 through messages and its poll. Three framework additions were made for the Dragon and are available to any 6809 driver:
  • Physical snoop. A memio handler on a PHYSICAL address is called after the host cycle; on a read its value replaces the host's byte, on a write it sees the byte. Use it to merge virtual bits into a real chip's register without a second bus cycle (the Dragon persona does this for PIA1). Cost: one SRAM bitmap test per PHYSICAL cycle.
  • t_VirtualFuncMap config column. Lets a persona parse its own JSON keys while the persona JSON is still in memory.
  • g_m6809StartHold. Core 1 does not start the CPU until the persona's Core 0 start-up job has finished, for example loading disc images.
A driver that needs a soft interrupt line (a virtual device's /FIRQ or /NMI) drives it through g_m6809DrvLines; M6809CPU_lines merges these with the real lines and turns a rising soft NMI into M6809_LINE_NMIEDGE.

The Dragon Driver as an Example
Dragon.c registers Dragon32, Dragon64 and Dragon (with a model key). In physical mode it shadows the SAM register writes and PIA1; in virtual mode it builds the 64K RAM and ROM maps in PSRAM and re-maps them on SAM map-type, page-1 and Dragon 64 ROM-select changes. Its interface cards are separate files: DragonDOS.c (the DOS ROM, the WD2797 via WD177x.c and the drive latch, with the PIA1 CB1 flag model for FIRQ; disc images through FDImage.c) and DragonCMT.c (play and record, clocked from the core's E-cycle count). DragonDOS_Status and DragonCMT_Status exist for a future debug-shell command but are not yet called.

Web GUI Media Commands
The run-time media menus reach the 6809 firmware as ESP32 commands handled in src/model/Base6809/main.c (the Dragon6809 model builds the same file). Each holds the 6809 while Core 0 changes driver state, then releases it.
Command Menu Action
CHGD,<drive>,<file> Floppy Change the disc in a DragonDOS or WD2797 drive (empty file = eject) through FDImage_ChangeDisk
CHGC,<file> Cartridge Fit a DOS ROM image, or remove the cartridge (empty file); the Dragon cold-starts on release
CMTA,<file> / CMTS,<idx> / CMTD,<idx> CMT Add a tape to the queue / select (and load) a tape / remove a tape
CMTM,<0\|1> CMT Deck mode (virtual or real)
The web GUI needs a filepack rebuild (esp32/make_filepack.sh) before these menus reach a board.

Adding a Firmware Model
Firmware models are declared in src/model/CMakeLists.txt with add_m6809_model_targets(), which generates the four variants (_0x10020000, _0x10020000_DBGSH, _0x10520000, _0x10520000_DBGSH). Dragon6809 adds its drivers with the INCLUDE_DRAGON6809_DRIVERS define; a new machine family would add its own define and model directory the same way, plus its personas in esp32/personas/generate_combos.sh (the MACHINES_6809 list), which generates every partition pairing.

Build and Test

# from the repository root
./build_tzpuPico.sh 6809            # Base6809
./build_tzpuPico.sh dragon          # Dragon6809
./build_tzpuPico.sh dragon DEBUG    # debug build
./build_tzpuPico.sh dragon ALL      # plus the ESP32 firmware and filepack

# core test suite against the XRoar oracle (from projects/tzpuPico)
make -C src/test/M6809 run          # differential run, must report 0 failures
make -C src/test/M6809 soak         # longer run
The test suite needs the XRoar sources, by default at /srv/dvlp/Projects/ref/xroar (XROAR ?= in src/test/M6809/Makefile). It compares registers and the per-cycle bus trace, including dummy cycles and the LIC/AVMA/BUSY/BA/BS state of each cycle, with XRoar's mc6809.c and hd6309.c, on both CPUs and both 6309 modes, with the interrupt lines changed at random mid-instruction cycles. Run it after any change to M6809.c.

Simulators

Stage A: pico6809sim (Synthetic SBC)
The real Base6809 RP2350 firmware (core, routing, debug shell) and the real ESP32 firmware, built natively on the picohal/ and espidfhal/ shims, with a minimal SBC as the host: 32 KB RAM at $0000–$7FFF, a 6850 ACIA at $A000 on a terminal, and an 8 KB ROM at $E000–$FFFF holding the SBC09MON monitor. The build also builds sim/tools/asm6809 (a two-pass wrapper around the firmware's one-line assembler) to assemble the monitor.
cd sim/6809 && mkdir -p build && cd build && cmake .. && make
sim/run.sh 6809sbc     # MC6809E, physical:  web 8120, dbgsh 5441, terminal 5442
sim/run.sh 6309sbc     # HD6309E, physical:  web 8121, dbgsh 5451, terminal 5452
sim/run.sh 6809sbcv    # MC6809E, virtual:   web 8122, dbgsh 5461, terminal 5462

Stage B: pico6809-xroar (Dragon 32 / 64)
The firmware in the CPU socket of an unmodified XRoar, run headless. XRoar's own MC6809/HD6309 part has its run() and reset() replaced at link time; XRoar's thread lends the machine to the firmware's unmodified Core 1 loop, and each descriptor becomes exactly one XRoar mem_cycle (reads, dummies and HALT/grant cycles read their address or $FFFF, writes write). In virtual mode, cycles served from PSRAM are passed to the Dragon as idle $FFFF cycles, so the host runs 1:1 with the core. XRoar is GPLv3+, compatible with the project's GPLv3.
cd sim/xroar && mkdir -p build && cd build && cmake .. && make   # optional: -DXROAR_SRC=<path>
sim/run.sh dragon32          # also dragon32v, dragon32-6309, dragon32v-6309
sim/run.sh dragon64          # also dragon64v, dragon64-6309, dragon64v-6309
sim/xroar/test_stage_b.sh    # regression against XRoar's own CPU and DragonDOS
ROMs are read from DRAGON_ROMS (default /dvlp/Projects/Dragon/roms): d32.rom, d64rom1.rom, d64rom2.rom and ddos12a.rom. Web GUI ports are 8130–8137. Environment variables:
Variable Effect
DRAGON_VDOS, DRAGON_VDISK, DRAGON_VDISK2..4 Enable the virtual DragonDOS and its drive images
DRAGON_VDOS_SPEED, DRAGON_VDOS_TRACE The interface’s speed and trace keys
DRAGON_CAS, DRAGON_CASREC, DRAGON_CASPAD Tape to play, record file, leader pad
DRAGON_VIDEO Virtual-mode video key
DRAGON_DOS, DRAGON_DISK Use XRoar’s own DragonDOS cartridge instead (for comparison)
DRAGON_ROM=test Boot the self-written test ROM dragontest.asm
PICO6809_PERSONA=dragon Write the Dragon persona shape (implied by the DOS and tape variables)
PICO6809_NATIVE=1 Run XRoar’s own CPU, the oracle for comparison runs
PICO6809_SHADOW Read shadow mode (verify for coherency runs)
PICO6809_VPAD, PICO6809_NMIEDGE, PICO6809_NOAPPLY Virtual-mode pacing, NMI contract, skip config apply
The harness also takes --pico-seconds, --pico-keys (typed input), --pico-trace (bus trace), --pico-report and --pico-wall. Test tapes are generated by sim/xroar/make_cas.py and checked against XRoar first. On the first boot after switching machines, the sim's web GUI can show the previous machine's processor, because the ESP32's NVS is shared between runs.

The PIO Bench
sim/pio6809 runs the real M6809.pio, the real bus layer, read shadow and core through a cycle-accurate PIO emulator (sim/pioemu) and its SDK adapter, against a modelled Dragon (SAM) host with board-delay corners. It measures every socket margin and checks that the host's cycle stream equals the core's descriptor stream.
make -C sim/pio6809 test      # 36 scenarios, 1021 checks, about 2 minutes
make -C sim/pio6809 report    # full sweeps, 3912 checks, about 15 minutes

Debug Shell Integration

The generic debug shell reaches the 6809 through the m6809Ops table in src/dbgsh.c: register display and set (with the 6309 registers and MD), disassembly, assembly, instruction length and call detection through M6809dis (model-aware), and the step, next, breakpoint and trace engine. M6809CPU_linesPeek and M6809CPU_busLinesPeek let the register view show the lines without consuming an NMI edge. Not yet ported: busdiag and piodbg, and Dragon disc and tape commands.

Pitfalls

  • Never let an idle or late cycle show any address but $FFFF. On the Dragon, an access to $FFC0–$FFDF toggles a SAM bit.
  • Keep Core 1's path in SRAM (__not_in_flash_func); a flash XIP stall from Core 0 traffic corrupts bus timing.
  • One aligned load per per-instruction check (soft IRQs as one halfword, device timers in a union); put new features behind measured gates.
  • Sample lines live, never from a latched slice value, and only through the line contract.
  • Simulator builds: PIO inline copies need BUILD_SIM guards, core1Main() must never return, and the raw SIO address must be honoured.
  • Device timing in E cycles. The Dragon devices count the core's E cycles (DRAGON_E_HZ = 894886). That is exact while the SAM runs slow; anything that depends on wall-clock time in virtual mode still needs a decision.