Implement missing 6809 indexed arithmetic/logic opcodes, document the parent instruction-suite commands, add Makefile.test-instr, and point AGENTS.md at the V 21-Implementation plan (SuperMon 64).
12 KiB
AGENTS.md — mimic09 (Motorola 6809/6309 Emulator)
Implementation plan
Later harness: read /Users/vincente/Mimic/.V/01-Artifacts/21-Implementation/ (latest Implementation_Plan.md). mimic09 waves: W0 (instr suite), WC (ISA + ACIA hello), WD (SuperMon 64), WE (mV/ICE/shebang), WF (6800). SuperMon 64 only. Help is ?. Do not steal H for help.
Project Mission (from originator)
Your mission is to create a Motorola 6809/6309 Emulator called "mimic09". The intention is to be extremely efficient and fast when running on a MacOS or UNIX/Linux OS.
This is a continuation/evolution of early "mimic" / "mimic09" work (1994 skeleton by Vincente D'Ingianni, II). The original partial code (Instruct.c + main.cc) is preserved under attic/ for reference.
Canonical repository: http://gitea.binary-systems.com/vincente/mimic
Required Capabilities
- Full, correct emulation of the Motorola 6809 (all documented + common undocumented behaviors).
- Strong support for the Hitachi 6309 (extra registers E/F/W/Q, additional instructions, MD register, native vs emulation mode, TFM block transfers, bit manipulation instructions, etc.).
- Integrated debugger/monitor featuring:
- Single-step execution
- Breakpoints (execution, and ideally memory read/write access)
- Built-in assembler and disassembler (two-way, so the monitor can A ssemble and D isassemble the same code).
- Debugger interface and capabilities similar to popular monitors on the Commodore 64 (SuperMon, Final Cartridge III monitor, built-in monitor, etc.). Typical commands to emulate in spirit:
- R (registers), ; (edit registers from R output)
- M (memory dump hex+ascii), editing via : or direct poke style
- D (disassemble)
- A (assemble one or more lines at address)
- G (go/run)
- S or single-step variants
- B / breakpoints
- L / S for load/save
- X / Q to exit
- Additional useful: F (fill), H (hunt/search), T (transfer), C (compare), ! (shell), etc.
- Load and save:
- Motorola S-Records (S0–S3, S5, S7–S9) — primary format
- Intel HEX (.HEX)
- Raw binary images (with explicit load address)
- Best-effort for legacy formats appearing in the tree (e.g. .MIK)
- Peripheral emulation (memory-mapped, configurable base addresses preferred):
- 6850 ACIA (serial port) — for console/terminal I/O during program execution
- 6821 PIA (parallel ports + control lines)
- Designed to interface cleanly with the cc09 project (recreation of the WINTEK 6809 Assembler and C compiler). Typical workflow:
- Use cc09 tools to produce S-Record or binary output + optional listing/map
- Load into mimic09
- Debug, single-step, set breakpoints, inspect memory/registers
- Run with emulated serial I/O for stdio-style C programs
Non-Goals / Future
- Full system emulation of specific 6809 boards (Coco, Dragon, etc.) is out of scope for v1 (but the ACIA/PIA + flexible memory map makes it usable for many).
- Cycle-accurate timing is desirable for serial/PIA realism but secondary to raw speed and correctness of instruction semantics.
- GUI: CLI / terminal only. A future external front-end can talk to a debug stub if needed.
Performance Philosophy
- "Extremely efficient and fast" is a core constraint.
- Target: tens to hundreds of millions of emulated instructions per second on Apple Silicon / modern x86_64 Linux under normal debugging load.
- Hot path (instruction fetch/decode/execute + memory) must be lean.
- Prefer direct array access for RAM; route only device ranges and debug watchpoints through accessor functions.
- Use C (C99/C11). Heavy use of inlining, small functions, computed-goto or tight switch dispatch, -O3 -flto -march=native.
- Breakpoint / device checks must not kill speed when disabled.
- Provide a "fast run" mode (no debug hooks) vs "debug/trace" mode.
Directory Structure (source tree + executables)
mimic/ # repo root (project "mimic", user-facing tool "mimic09" or "mimic")
AGENTS.md # this file — keep updated
README.md # user-facing quick start + command summary
LICENSE
Makefile # primary build system (no CMake requirement)
.gitignore
src/ # C sources and internal headers
main.c # CLI entry, option parsing, startup, REPL dispatch
cpu.c cpu.h # CPU core: registers, flags, memory, interpreter loop
disasm.c disasm.h # Table-driven disassembler (6809 + 6309)
asm.c asm.h # Built-in assembler used by monitor "A" command
debug.c debug.h # C64-monitor-style debugger / monitor command interpreter
loader.c loader.h # S-Record, Intel HEX, binary load/save (+ format detection)
acia.c acia.h # 6850 ACIA emulation
pia.c pia.h # 6821 PIA emulation
util.c util.h # hex parsing, string helpers, terminal raw mode, etc.
attic/ # Historical 1994 code (Instruct.c, main.cc, old binaries, etc.)
samples/ # Test programs, 140A.HEX, 2299.HEX, 2299.MIK, cc09 output examples
tests/ # Automated test harnesses, opcode tests, regression
docs/ # Longer docs: monitor-commands.md, 6809-notes.md, 6309-diffs.md, peripherals.md, cc09-integration.md
bin/ # Build target for the final executable (mimic09). Gitignored or committed as convenience.
build/ # Intermediate objects (gitignore)
Executables live under bin/ after a successful make. A make install target may copy to $PREFIX/bin (default /usr/local/bin).
Users run the emulator as mimic09 (or mimic for short if you symlink).
Build & Run Requirements
- POSIX environment (macOS, Linux, *BSD, etc.).
- C compiler: Apple clang, clang, or gcc (recent).
- GNU make (or BSD make with minor tweaks).
- No heavy dependencies. termios (POSIX) for interactive terminal when running code that uses the ACIA.
- Recommended compile flags (Makefile should provide):
- Release:
-O3 -flto -DNDEBUG -march=native -Wall -Wextra - Debug:
-O0 -g -DDEBUG -Wall
- Release:
make,make debug,make clean,make test(when tests exist),make install.
Coding & Contribution Rules for AI Agents and Humans
- Update AGENTS.md and relevant docs/ when adding features or changing architecture.
- Keep the hot CPU path fast. Any accessor change must be measured.
- All memory traffic from the CPU must eventually go through the read/write accessors so that:
- Breakpoints/watchpoints work
- ACIA/PIA devices are emulated
- Future MMU or banking can be inserted
- Hex numbers in the monitor: accept both bare hex (
F800) and$F800/0xF800. Be liberal in what you accept. - Command language should feel familiar to anyone who has used a C64 monitor.
- Test against real output from cc09 and trusted 6809/6309 artifacts. Cross-check key instruction semantics with another emulator or (ideally) real hardware.
- 6309 instructions and "native mode" behaviors must be clearly isolated (ifdef or runtime cpu type).
- Preserve the ability to run the original 1994 test files in samples/.
- Never check in large binaries unless they are tiny test cases. The pre-existing mimic.exe etc. belong in attic/.
- When you add a new monitor command, document it in docs/monitor-commands.md and give a one-line help in the
?/Hcommand.
Monitor Command Set (Target Behavior — C64 Inspired)
(Actual implementation may start with a useful subset and grow.)
R Show registers (PC, A/B/D, X, Y, U, S, CC with flag letters, MD if 6309)
; <vals...> Edit registers (paste the line from R and change values)
M <addr> [<count>] Hex + ASCII dump (16 bytes per line typical). Enter continues.
:<addr> <b0> <b1>... (or just type over an M display) — write bytes
D [<addr>] [<count>] Disassemble (default 20 lines or so)
A <addr> <mnemonic> Assemble one instruction at addr (advances "next" address for repeated use)
G [<addr>] Run (from addr or current PC) until breakpoint or manual break
S [<count>] Single step (count instructions). Shows state after each.
N or O Step over (treat JSR/BSR as one step)
B [<addr>] Set or toggle breakpoint at addr. No addr = list breakpoints.
BC Clear all breakpoints
F <start> <end> <val> Fill memory
H <start> <end> <bytes...> Hunt (search) memory for byte sequence
T <src> <dst> <end> Block transfer (copy)
L <filename> [<addr>] Load file (auto-detect SREC/IHEX/BIN). Optional force addr for raw.
S <filename> <start> <end> [FORMAT] Save region (FORMAT = S19 | IHEX | BIN)
! <shell command> Execute host shell command
? or H Help (brief command list + syntax)
X or Q Exit the monitor / emulator
Additional runtime control while a program is running (if using ACIA console):
- SIGINT (Ctrl-C) should drop back into the monitor with PC at the interrupted instruction.
- Optional: a "hot key" sequence to break without killing the process.
Peripheral Notes
- ACIA (6850): Control/Status at base+0, Data at base+1. Typical bases: $A000, $E000, $F000, etc. Make base configurable on CLI (
-acia 0xA000).- During
Gexecution, map ACIA TX to stdout (or a pty), RX from non-blocking stdin / cooked input. - Support setting baud rate etc. minimally (or just "as fast as host allows").
- During
- PIA (6821): Four registers per chip (PRA, DDRA/CRA, PRB, DDRB/CRB). Side effects on control lines can be stubbed or logged at first; allow user to poke values that programs expect for simple I/O handshaking.
- Device registration: a small table or range checks in the memory accessors. Keep the check cost near zero when no devices are mapped.
Integration with cc09 (Wintek Recreation)
- Primary interchange formats: S-Records and raw binaries at known load addresses.
- The emulator should be runnable from scripts:
mimic09 -l app.s19 -a 0x1000 -gor similar for "assemble + run" CI-style loops. - Future nice-to-have: load a symbol table (from cc09 .map or .lst) so that D and breakpoints can be symbolic.
- Provide a small C runtime shim or documented memory locations if cc09 programs expect specific startup (stack, argc/argv simulation via memory, exit via SWI or ACIA).
Versioning & Releases
- Start at 0.1 or 0.9 to reflect heritage.
- Use tags: v0.2, v0.3...
- The gitea at binary-systems.com is authoritative.
Safety & Correctness
- Never trust untrusted SREC/HEX files blindly (they can contain huge address claims). Clamp or warn.
- Stack pointer and PC must stay sane; detect runaway execution and offer to break.
- All emulated writes that target ROM areas should be ignored or warned (configurable).
References & Resources (for implementers)
- Motorola MC6809/MC6809E data sheets and programming manuals
- Hitachi HD6309 data (differences from 6809)
- 6850 ACIA, 6821/6822 PIA data sheets
- "The 6809 Companion", assembly programming books
- C64 monitor command lists and source (for UX inspiration, not literal port)
- Other 6809 emulators for behavioral cross-check (but mimic09 must be fast and standalone)
- cc09 project for the companion toolchain
When Working on This Codebase
- Read this file first.
- Prefer small, reviewable diffs.
- Add a regression test under tests/ for every non-trivial bug fixed.
- Keep the "fast path" obvious in cpu.c.
- If you change the monitor command grammar, update help text and docs/ at the same time.
- The old Instruct.c table-driven style is inspirational but the new implementation should be a clean-slate, high-quality rewrite (the 1994 code is incomplete and was never finished).
This project exists to run real 6809/6309 code (especially from cc09) at high speed on modern *nix with an ergonomic C64-flavored debugging experience.
Keep it fast. Keep it fun. Keep the monitor feeling like 1985 done right on a 2026 machine.