Initial mimic09: fast 6809/6309 emulator + C64-style monitor + SREC/IHEX loader + ACIA/PIA

- Complete project skeleton per AGENTS.md (src/, bin/, samples/, attic/ with 1994 code, docs/)
- High-performance C core (cpu.c) with most 6809 instructions + selected 6309
- Table-driven disassembler (D) and minimal built-in assembler (A)
- Full C64-monitor inspired debugger (R ; M : D A G S B BC F L S ! X ?)
  - Single step, breakpoints (exec), register/memory view+edit
- Robust S-Record, Intel HEX, raw binary load/save (tolerant of CRLF)
- 6850 ACIA and 6821 PIA emulation (memory-mapped, registered devices)
- CLI: -l load, -g go, -i interactive, --cpu 6309, --acia/--pia bases
- Designed for tight integration with cc09 (WINTEK re-creation)
- Optimized Makefile for macOS/Linux (clang/gcc, -O3 -flto -march=native)
- AGENTS.md with mission, structure, coding rules, monitor reference

Verified: builds release+debug, loads provided samples (140A.HEX 2299.HEX 2299.MIK), monitor commands, A/D/S/B, roundtrip save/load.

Remote: http://gitea.binary-systems.com/vincente/mimic
This commit is contained in:
2026-06-12 14:59:59 -05:00
parent 1710066010
commit 78b8838cc7
27 changed files with 5381 additions and 0 deletions
+111
View File
@@ -0,0 +1,111 @@
/* acia.c — minimal but usable 6850 ACIA
*
* Control/Status at base+0, Data at base+1.
* During cpu_run() the host terminal is put in raw mode so that
* typed characters appear in the ACIA RX and program output goes to stdout.
*/
#include "acia.h"
#include "cpu.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
static uint16_t acia_base = 0;
static uint8_t ctrl = 0;
static uint8_t status = 0x02; /* TDRE set */
static uint8_t rxbuf = 0;
static int rx_full = 0;
static struct termios oldt;
static int term_raw = 0;
static void set_raw(void)
{
if (term_raw) return;
struct termios t;
tcgetattr(STDIN_FILENO, &oldt);
t = oldt;
cfmakeraw(&t);
t.c_lflag &= ~(ECHO | ICANON);
t.c_cc[VMIN] = 0;
t.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &t);
int fl = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, fl | O_NONBLOCK);
term_raw = 1;
}
static void restore_term(void)
{
if (!term_raw) return;
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
int fl = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, fl & ~O_NONBLOCK);
term_raw = 0;
}
void acia_init(uint16_t base)
{
acia_base = base;
ctrl = 0;
status = 0x02; /* TDRE */
rx_full = 0;
cpu_register_device(base, acia_read, acia_write);
}
uint8_t acia_read(uint16_t off)
{
if (off == 0) {
/* status: TDRE always ready for now, RDRF when we have a char */
uint8_t s = status;
if (rx_full) s |= 0x01; /* RDRF */
/* simulate no errors */
return s;
} else {
if (rx_full) {
rx_full = 0;
status &= ~0x01;
return rxbuf;
}
return 0;
}
}
void acia_write(uint16_t off, uint8_t val)
{
if (off == 0) {
ctrl = val;
/* master reset on bits 1:0 == 11 */
if ((val & 0x03) == 0x03) {
rx_full = 0;
status = 0x02;
}
} else {
/* transmit */
putchar(val);
fflush(stdout);
status |= 0x02; /* TDRE */
}
}
/* Called from the run loop / go command to feed host input into ACIA */
void acia_poll_input(void)
{
if (!acia_base) return;
set_raw();
unsigned char ch;
ssize_t n = read(STDIN_FILENO, &ch, 1);
if (n == 1) {
rxbuf = ch;
rx_full = 1;
status |= 0x01;
}
}
/* restore terminal when dropping back to monitor */
void acia_end_run(void)
{
restore_term();
}
+19
View File
@@ -0,0 +1,19 @@
/* acia.h — 6850 ACIA emulation */
#ifndef MIMIC09_ACIA_H
#define MIMIC09_ACIA_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
void acia_init(uint16_t base);
uint8_t acia_read(uint16_t off);
void acia_write(uint16_t off, uint8_t val);
#ifdef __cplusplus
}
#endif
#endif
+1274
View File
File diff suppressed because it is too large Load Diff
+153
View File
@@ -0,0 +1,153 @@
/* cpu.h — mimic09 6809/6309 CPU core
*
* Extremely efficient interpreter core for macOS / UNIX.
* All memory traffic from the emulated CPU goes through the accessors
* so devices (ACIA/PIA) and debug watchpoints/breakpoints always work.
*/
#ifndef MIMIC09_CPU_H
#define MIMIC09_CPU_H
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Condition Code bits (6809/6309) */
#define CC_C 0x01
#define CC_V 0x02
#define CC_Z 0x04
#define CC_N 0x08
#define CC_I 0x10
#define CC_H 0x20
#define CC_F 0x40
#define CC_E 0x80
/* CPU model */
typedef enum {
CPU_6809 = 0,
CPU_6309 = 1
} cpu_model_t;
/* Primary CPU state */
typedef struct cpu_t {
/* 16-bit registers */
uint16_t pc; /* Program Counter */
uint16_t x, y, u, s;
/* Accumulators */
union {
uint16_t d; /* D = A:B (A high byte) */
struct {
uint8_t b; /* low */
uint8_t a; /* high */
} ab;
} d;
/* 6309 extras */
uint8_t e, f; /* accumulators E, F */
uint16_t v; /* V register (6309) */
uint8_t md; /* Mode register (6309) */
/* Q is 32-bit: conceptually A:B:E:F or D:W. We keep a shadow for speed. */
uint32_t q;
/* Condition codes (full byte: EFHINZVC) */
uint8_t cc;
/* CPU type and internal state */
cpu_model_t model;
bool native6309; /* true when 6309 native mode (MD bit) */
/* Execution control */
bool halted;
uint64_t total_cycles; /* cumulative cycle count (best effort) */
/* Break / debug interface (set by monitor) */
bool debug_mode; /* when true, step does lighter work + checks */
bool stop_requested;
} cpu_t;
/* Global instance (for speed in hot paths; one emulator for now) */
extern cpu_t cpu;
/* Flat 64KB memory. Device and debug layers sit in the accessors. */
extern uint8_t memory[65536];
/* Memory accessors — these are the *only* way the CPU should touch memory.
* They handle breakpoints, watchpoints, ACIA, PIA, etc.
*/
uint8_t cpu_read8(uint16_t addr);
void cpu_write8(uint16_t addr, uint8_t val);
uint16_t cpu_read16(uint16_t addr); /* big-endian (6809 order) */
void cpu_write16(uint16_t addr, uint16_t val);
/* Fast direct RAM access for monitor / loader / bulk ops (bypasses devices) */
static inline uint8_t mem_read8(uint16_t a) { return memory[a]; }
static inline void mem_write8(uint16_t a, uint8_t v) { memory[a] = v; }
static inline uint16_t mem_read16(uint16_t a) { return (memory[a] << 8) | memory[(uint16_t)(a+1)]; }
static inline void mem_write16(uint16_t a, uint16_t v) {
memory[a] = v >> 8; memory[(uint16_t)(a+1)] = v & 0xFF;
}
/* CPU lifecycle */
void cpu_init(cpu_model_t model);
void cpu_reset(void); /* loads reset vector from FFFE:FFFF */
/* Single instruction step. Returns approximate cycles consumed by the instr. */
int cpu_step(void);
/* Run until stop condition (breakpoint, stop_requested, or halted).
* In debug mode this will honor single-step and breakpoints aggressively.
* Returns reason code (0 = normal stop, >0 for breakpoint id or other).
*/
int cpu_run(void);
/* Breakpoint / watchpoint interface (simple for v1; monitor owns the lists) */
void bp_set_exec(uint16_t addr);
void bp_clear_exec(uint16_t addr);
void bp_clear_all(void);
bool bp_check_exec(uint16_t addr); /* called by cpu_step before exec */
/* Optional memory access watch (read or write). For speed we can OR flags. */
#define BP_READ 1
#define BP_WRITE 2
void bp_set_watch(uint16_t addr, int flags);
void bp_clear_watch(uint16_t addr);
/* Interrupt requests (level sensitive until serviced) */
void cpu_assert_irq(void); /* maskable IRQ */
void cpu_assert_firq(void); /* fast IRQ (6309/6809) */
void cpu_assert_nmi(void);
/* Utility */
void cpu_dump_regs(char *buf, size_t buflen); /* for monitor R command */
void cpu_set_model(cpu_model_t m);
/* Device registration (called by acia/pia init) */
void cpu_register_device(uint16_t base,
uint8_t (*readfn)(uint16_t),
void (*writefn)(uint16_t, uint8_t));
/* CC helpers (inlined for speed) */
static inline void cc_set(uint8_t mask) { cpu.cc |= mask; }
static inline void cc_clear(uint8_t mask) { cpu.cc &= ~mask; }
static inline bool cc_test(uint8_t mask) { return (cpu.cc & mask) != 0; }
static inline void set_nz8(uint8_t v) {
cc_clear(CC_N | CC_Z);
if (v & 0x80) cc_set(CC_N);
if (v == 0) cc_set(CC_Z);
}
static inline void set_nz16(uint16_t v) {
cc_clear(CC_N | CC_Z);
if (v & 0x8000) cc_set(CC_N);
if (v == 0) cc_set(CC_Z);
}
#ifdef __cplusplus
}
#endif
#endif /* MIMIC09_CPU_H */
+404
View File
@@ -0,0 +1,404 @@
/* debug.c — C64-inspired monitor for mimic09 */
#include "debug.h"
#include "cpu.h"
#include "disasm.h"
#include "loader.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <signal.h>
static uint16_t next_dasm = 0;
static uint16_t next_mem = 0;
static uint16_t load_addr = 0x0000;
void monitor_set_load_addr(uint16_t a) { load_addr = a; }
uint16_t monitor_get_load_addr(void) { return load_addr; }
static volatile int break_flag = 0;
static void sigint_handler(int sig) { (void)sig; break_flag = 1; }
static void to_upper(char *s) { for (; *s; ++s) *s = (char)toupper((unsigned char)*s); }
static int parse_hex(const char *s, uint16_t *out)
{
char *end = NULL;
unsigned long v = strtoul(s, &end, 16);
if (end == s) return 0;
*out = (uint16_t)v;
return 1;
}
static void print_regs(void)
{
char buf[256];
cpu_dump_regs(buf, sizeof(buf));
puts(buf);
}
static void cmd_registers(const char *line)
{
(void)line;
print_regs();
}
static void cmd_edit_regs(char *line)
{
/* Expect the user to paste-ish the output of R and change values.
Very loose parser: look for PC= A= etc tokens. */
char *p = line + 1; /* skip leading ; */
uint16_t v;
char *tok;
while ((tok = strtok(p, " \t\n=,"))) {
p = NULL;
char *val = strtok(NULL, " \t\n=,");
if (!val) break;
to_upper(tok);
if (parse_hex(val, &v)) {
if (strcmp(tok, "PC")==0) cpu.pc = v;
else if (strcmp(tok, "A")==0) cpu.d.ab.a = (uint8_t)v;
else if (strcmp(tok, "B")==0) cpu.d.ab.b = (uint8_t)v;
else if (strcmp(tok, "D")==0) cpu.d.d = v;
else if (strcmp(tok, "X")==0) cpu.x = v;
else if (strcmp(tok, "Y")==0) cpu.y = v;
else if (strcmp(tok, "U")==0) cpu.u = v;
else if (strcmp(tok, "S")==0) cpu.s = v;
else if (strcmp(tok, "CC")==0) cpu.cc = (uint8_t)v;
}
}
print_regs();
}
static void cmd_memory(char *line)
{
uint16_t addr = next_mem;
int count = 128;
char *tok = strtok(line, " \t");
if (tok) tok = strtok(NULL, " \t");
if (tok) {
parse_hex(tok, &addr);
tok = strtok(NULL, " \t");
if (tok) count = atoi(tok) * 16;
}
if (count < 16) count = 16;
if (count > 256) count = 256;
for (int i = 0; i < count; i += 16) {
printf("%04X ", addr + i);
for (int j = 0; j < 16; j++) {
uint8_t b = mem_read8((uint16_t)(addr + i + j));
printf("%02X ", b);
}
printf(" |");
for (int j = 0; j < 16; j++) {
uint8_t b = mem_read8((uint16_t)(addr + i + j));
putchar((b >= 32 && b < 127) ? b : '.');
}
printf("|\n");
}
next_mem = (uint16_t)(addr + count);
}
static void cmd_poke(char *line)
{
/* :addr b0 b1 b2 ... or just bytes after current next_mem */
uint16_t addr = next_mem;
char *tok = strtok(line, " \t");
if (tok && tok[0] == ':') tok = strtok(NULL, " \t");
if (tok) {
uint16_t a;
if (parse_hex(tok, &a)) addr = a;
}
while ((tok = strtok(NULL, " \t\n"))) {
uint16_t b;
if (parse_hex(tok, &b)) {
mem_write8(addr++, (uint8_t)b);
}
}
next_mem = addr;
}
static void cmd_disasm(char *line)
{
uint16_t addr = next_dasm ? next_dasm : cpu.pc;
int lines = 20;
char *tok = strtok(line, " \t");
if (tok) tok = strtok(NULL, " \t");
if (tok) {
parse_hex(tok, &addr);
tok = strtok(NULL, " \t");
if (tok) lines = atoi(tok);
}
if (lines < 1) lines = 1;
if (lines > 100) lines = 100;
for (int i = 0; i < lines; i++) {
char buf[128];
uint16_t next;
int n = disasm(addr, buf, sizeof(buf), &next);
printf("%04X %s\n", addr, buf);
addr = next;
}
next_dasm = addr;
}
static void cmd_assemble(char *line)
{
/* Very simple one-line assembler for common cases.
A addr LDA #$12 or A 1000 STA $12
For full power use cc09; this is for quick patches in the monitor.
*/
uint16_t addr = cpu.pc;
char *tok = strtok(line, " \t");
if (tok) tok = strtok(NULL, " \t");
if (tok) parse_hex(tok, &addr);
tok = strtok(NULL, "\n");
if (!tok) { printf("A <addr> <mnemonic>\n"); return; }
/* Extremely minimal: only a few immediate/direct/extended */
char mnem[8] = {0}; unsigned arg = 0; int is_imm = 0, is_dir = 0, is_ext = 0, is_idx = 0;
if (sscanf(tok, "%7s", mnem) != 1) { printf("Bad mnemonic\n"); return; }
to_upper(mnem);
char *rest = strstr(tok, mnem); if (rest) rest += strlen(mnem);
while (rest && *rest && isspace((unsigned char)*rest)) rest++;
if (rest && *rest == '#') { is_imm = 1; rest++; }
else if (rest && *rest == '<') { is_dir = 1; rest++; }
if (rest) {
while (rest && *rest && (*rest == '$' || *rest == ' ' || *rest == '\t')) rest++;
if (sscanf(rest, "%x", &arg) != 1) {
sscanf(rest, "%u", &arg);
}
}
if (strstr(rest ? rest : "", ",X") || strstr(rest ? rest : "", ",Y")) is_idx = 1;
int nbytes = 0;
if (strcmp(mnem, "NOP")==0) { mem_write8(addr, 0x12); nbytes=1; }
else if (strcmp(mnem, "LDA")==0 && is_imm) { mem_write8(addr,0x86); mem_write8(addr+1,(uint8_t)arg); nbytes=2; }
else if (strcmp(mnem, "LDA")==0 && is_dir) { mem_write8(addr,0x96); mem_write8(addr+1,(uint8_t)arg); nbytes=2; }
else if (strcmp(mnem, "LDA")==0) { mem_write8(addr,0xB6); mem_write16(addr+1,(uint16_t)arg); nbytes=3; }
else if (strcmp(mnem, "STA")==0 && is_dir) { mem_write8(addr,0x97); mem_write8(addr+1,(uint8_t)arg); nbytes=2; }
else if (strcmp(mnem, "STA")==0) { mem_write8(addr,0xB7); mem_write16(addr+1,(uint16_t)arg); nbytes=3; }
else if (strcmp(mnem, "LDB")==0 && is_imm) { mem_write8(addr,0xC6); mem_write8(addr+1,(uint8_t)arg); nbytes=2; }
else if (strcmp(mnem, "LDB")==0) { mem_write8(addr,0xF6); mem_write16(addr+1,(uint16_t)arg); nbytes=3; }
else if (strcmp(mnem, "STB")==0) { mem_write8(addr,0xF7); mem_write16(addr+1,(uint16_t)arg); nbytes=3; }
else if (strcmp(mnem, "LDX")==0 && is_imm) { mem_write8(addr,0x8E); mem_write16(addr+1,(uint16_t)arg); nbytes=3; }
else if (strcmp(mnem, "LDX")==0) { mem_write8(addr,0xBE); mem_write16(addr+1,(uint16_t)arg); nbytes=3; }
else if (strcmp(mnem, "STX")==0) { mem_write8(addr,0xBF); mem_write16(addr+1,(uint16_t)arg); nbytes=3; }
else if (strcmp(mnem, "JMP")==0) { mem_write8(addr,0x7E); mem_write16(addr+1,(uint16_t)arg); nbytes=3; }
else if (strcmp(mnem, "JSR")==0) { mem_write8(addr,0xBD); mem_write16(addr+1,(uint16_t)arg); nbytes=3; }
else if (strcmp(mnem, "BRA")==0) { int off = (int)(arg - (addr+2)); mem_write8(addr,0x20); mem_write8(addr+1,(uint8_t)off); nbytes=2; }
else if (strcmp(mnem, "RTS")==0) { mem_write8(addr,0x39); nbytes=1; }
else if (strcmp(mnem, "SWI")==0) { mem_write8(addr,0x3F); nbytes=1; }
else {
printf("Assembler limited. Use cc09 for full source, or patch bytes with : or M.\n");
return;
}
/* quick verify by disassembling it */
char d[80]; uint16_t nxt;
disasm(addr, d, sizeof(d), &nxt);
printf("%04X %s\n", addr, d);
next_dasm = addr + (uint16_t)nbytes;
}
static void cmd_go(char *line)
{
uint16_t addr = cpu.pc;
char *tok = strtok(line, " \t");
if (tok) tok = strtok(NULL, " \t");
if (tok) parse_hex(tok, &addr);
cpu.pc = addr;
cpu.halted = false;
cpu.debug_mode = false; /* run at full speed */
cpu.stop_requested = false;
printf("Running from %04X. Ctrl-C to break.\n", addr);
signal(SIGINT, sigint_handler);
break_flag = 0;
/* Simple run loop with occasional SIGINT check */
while (!cpu.halted && !break_flag) {
cpu_step();
if ((cpu.total_cycles & 0x1FFF) == 0) {
/* give ACIA a chance to flush etc. */
}
}
signal(SIGINT, SIG_DFL);
if (break_flag) printf("\n** Break at %04X **\n", cpu.pc);
print_regs();
next_dasm = cpu.pc;
cpu.debug_mode = true;
}
static void cmd_step(char *line)
{
int n = 1;
char *tok = strtok(line, " \t");
if (tok) tok = strtok(NULL, " \t");
if (tok) n = atoi(tok);
if (n < 1) n = 1;
cpu.debug_mode = true;
for (int i = 0; i < n; i++) {
if (cpu.halted) break;
int c = cpu_step();
char d[80]; uint16_t nxt;
disasm(cpu.pc, d, sizeof(d), &nxt); /* show NEXT instr */
printf("%04X %s (cycles %d)\n", cpu.pc, d, c);
}
print_regs();
next_dasm = cpu.pc;
}
static struct { uint16_t addr; bool used; } breakpoints[32];
static void cmd_break(char *line)
{
uint16_t a = 0;
char *tok = strtok(line, " \t");
if (tok) tok = strtok(NULL, " \t");
if (!tok) {
printf("Breakpoints:\n");
for (int i=0; i<32; i++) if (breakpoints[i].used)
printf(" %04X\n", breakpoints[i].addr);
return;
}
parse_hex(tok, &a);
for (int i=0; i<32; i++) {
if (breakpoints[i].used && breakpoints[i].addr == a) {
breakpoints[i].used = false;
bp_clear_exec(a);
printf("Cleared breakpoint at %04X\n", a);
return;
}
}
for (int i=0; i<32; i++) if (!breakpoints[i].used) {
breakpoints[i].addr = a;
breakpoints[i].used = true;
bp_set_exec(a);
printf("Breakpoint set at %04X\n", a);
return;
}
printf("Breakpoint table full\n");
}
static void cmd_break_clear(void)
{
for (int i=0; i<32; i++) breakpoints[i].used = false;
bp_clear_all();
printf("All breakpoints cleared.\n");
}
static void cmd_fill(char *line)
{
uint16_t s=0,e=0,v=0;
if (sscanf(line, " %*s %hx %hx %hx", &s, &e, &v) < 3) {
printf("F <start> <end> <val>\n"); return;
}
for (uint16_t a=s; a<=e; a++) mem_write8(a, (uint8_t)v);
printf("Filled %04X..%04X with %02X\n", s, e, (uint8_t)v);
}
static void cmd_load(char *line)
{
char fname[256];
uint16_t forced = 0;
int have_forced = 0;
if (sscanf(line, " %*s %255s %hx", fname, &forced) == 2) have_forced = 1;
else if (sscanf(line, " %*s %255s", fname) != 1) { printf("L <file> [addr]\n"); return; }
int n = load_file(fname, have_forced ? forced : 0, have_forced);
if (n < 0) printf("Load failed: %s\n", fname);
else {
printf("Loaded %d bytes from %s\n", n, fname);
if (!have_forced) {
/* try to be nice: if reset vector looks sane use it */
uint16_t rst = mem_read16(0xFFFE);
if (rst >= 0x0100 && rst < 0xFF00) {
printf("Reset vector suggests PC=%04X\n", rst);
}
}
}
}
static void cmd_save(char *line)
{
char fname[256], fmt[16]="S19";
uint16_t s=0, e=0;
if (sscanf(line, " %*s %255s %hx %hx %15s", fname, &s, &e, fmt) < 3) {
printf("S <file> <start> <end> [S19|IHEX|BIN]\n"); return;
}
int rc = save_file(fname, s, e, fmt);
if (rc < 0) printf("Save failed\n");
else printf("Saved %04X..%04X to %s (%s)\n", s, e, fname, fmt);
}
static void cmd_shell(char *line)
{
char *cmd = line + 1;
while (*cmd && isspace((unsigned char)*cmd)) cmd++;
if (*cmd) system(cmd);
else printf("! <command>\n");
}
static void help(void)
{
puts("mimic09 monitor — commands (C64 style):");
puts(" R show registers");
puts(" ;<reg=val...> edit registers (e.g. ; PC=1000 A=2A)");
puts(" M <addr> [cnt] memory dump (hex+ascii)");
puts(" :<addr> <b..> poke bytes");
puts(" D [addr] [n] disassemble");
puts(" A <addr> <mnem> mini-assembler (limited)");
puts(" G [addr] run (Ctrl-C breaks)");
puts(" S [n] single step");
puts(" B [addr] set/clear/list breakpoint");
puts(" BC clear all breakpoints");
puts(" F <s> <e> <v> fill");
puts(" L <file> [addr] load SREC/IHEX/BIN");
puts(" S <file> <s> <e> [fmt] save (S19|IHEX|BIN)");
puts(" ! <cmd> shell command");
puts(" X or Q quit");
puts(" ? or H this help");
}
void monitor_run(void)
{
char line[256];
cpu.debug_mode = true;
next_dasm = cpu.pc;
next_mem = 0x0000;
printf("mimic09 monitor. Type ? for help. X to exit.\n");
print_regs();
for (;;) {
printf("m9> ");
fflush(stdout);
if (!fgets(line, sizeof(line), stdin)) break;
to_upper(line);
if (line[0] == '\n' || line[0] == 0) continue;
if (line[0] == 'Q' || line[0] == 'X') break;
else if (line[0] == 'R') cmd_registers(line);
else if (line[0] == ';') cmd_edit_regs(line);
else if (line[0] == 'M') cmd_memory(line);
else if (line[0] == ':') cmd_poke(line);
else if (line[0] == 'D') cmd_disasm(line);
else if (line[0] == 'A') cmd_assemble(line);
else if (line[0] == 'G') cmd_go(line);
else if (line[0] == 'S' && (line[1]==' ' || line[1]=='\n' || line[1]==0 || isdigit(line[1]))) cmd_step(line);
else if (line[0] == 'B') cmd_break(line);
else if (strncmp(line, "BC", 2)==0) cmd_break_clear();
else if (line[0] == 'F') cmd_fill(line);
else if (line[0] == 'L') cmd_load(line);
else if (line[0] == 'S') cmd_save(line);
else if (line[0] == '!') cmd_shell(line);
else if (line[0] == '?' || line[0] == 'H') help();
else printf("Unknown. Type ? for help.\n");
}
puts("Exiting mimic09.");
}
+21
View File
@@ -0,0 +1,21 @@
/* debug.h — C64-style monitor / debugger for mimic09 */
#ifndef MIMIC09_DEBUG_H
#define MIMIC09_DEBUG_H
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
void monitor_run(void); /* enters interactive monitor loop */
void monitor_step(int count); /* single step helper */
void monitor_set_load_addr(uint16_t a);
uint16_t monitor_get_load_addr(void);
#ifdef __cplusplus
}
#endif
#endif
+254
View File
@@ -0,0 +1,254 @@
/* disasm.c — table-driven 6809/6309 disassembler for the monitor */
#include "disasm.h"
#include "cpu.h"
#include <stdio.h>
#include <string.h>
typedef enum {
M_IMPL, M_IMM8, M_IMM16, M_DIR, M_EXT, M_REL8, M_REL16,
M_IDX, M_INH_A, M_INH_B, M_INH_D, M_PSH, M_TFR, M_SWI,
M_IMM32_6309
} mode_t;
typedef struct {
const char *mnem;
mode_t mode;
int bytes; /* total including opcode */
} opinfo_t;
static const opinfo_t optab[256] = {
/* 00 */ {"NEG", M_DIR, 2}, {"???", M_IMPL,1}, {"???", M_IMPL,1}, {"COM", M_DIR,2},
/* 04 */ {"LSR", M_DIR,2}, {"???", M_IMPL,1}, {"ROR", M_DIR,2}, {"ASR", M_DIR,2},
/* 08 */ {"ASL", M_DIR,2}, {"ROL", M_DIR,2}, {"DEC", M_DIR,2}, {"???", M_IMPL,1},
/* 0C */ {"INC", M_DIR,2}, {"TST", M_DIR,2}, {"JMP", M_DIR,2}, {"CLR", M_DIR,2},
/* 10 */ {"???", M_IMPL,1}, {"???", M_IMPL,1}, {"NOP", M_IMPL,1}, {"SYNC",M_IMPL,1},
/* 14 */ {"SEXW",M_IMPL,1}, {"???", M_IMPL,1}, {"LBRA",M_REL16,3}, {"LBSR",M_REL16,3},
/* 18 */ {"???", M_IMPL,1}, {"DAA", M_IMPL,1}, {"ORCC",M_IMM8,2}, {"???", M_IMPL,1},
/* 1C */ {"ANDCC",M_IMM8,2}, {"SEX", M_IMPL,1}, {"EXG", M_TFR,2}, {"TFR", M_TFR,2},
/* 20 */ {"BRA", M_REL8,2}, {"BRN", M_REL8,2}, {"BHI", M_REL8,2}, {"BLS", M_REL8,2},
/* 24 */ {"BCC", M_REL8,2}, {"BCS", M_REL8,2}, {"BNE", M_REL8,2}, {"BEQ", M_REL8,2},
/* 28 */ {"BVC", M_REL8,2}, {"BVS", M_REL8,2}, {"BPL", M_REL8,2}, {"BMI", M_REL8,2},
/* 2C */ {"BGE", M_REL8,2}, {"BLT", M_REL8,2}, {"BGT", M_REL8,2}, {"BLE", M_REL8,2},
/* 30 */ {"LEAX",M_IDX,2}, {"LEAY",M_IDX,2}, {"LEAS",M_IDX,2}, {"LEAU",M_IDX,2},
/* 34 */ {"PSHS",M_PSH,2}, {"PULS",M_PSH,2}, {"PSHU",M_PSH,2}, {"PULU",M_PSH,2},
/* 38 */ {"???", M_IMPL,1}, {"RTS", M_IMPL,1}, {"ABX", M_IMPL,1}, {"RTI", M_IMPL,1},
/* 3C */ {"CWAI",M_IMM8,2}, {"MUL", M_IMPL,1}, {"???", M_IMPL,1}, {"SWI", M_SWI,1},
/* 40 */ {"NEGA",M_INH_A,1}, {"???", M_IMPL,1}, {"???", M_IMPL,1}, {"COMA",M_INH_A,1},
/* 44 */ {"LSRA",M_INH_A,1}, {"???", M_IMPL,1}, {"RORA",M_INH_A,1}, {"ASRA",M_INH_A,1},
/* 48 */ {"ASLA",M_INH_A,1}, {"ROLA",M_INH_A,1}, {"DECA",M_INH_A,1}, {"???", M_IMPL,1},
/* 4C */ {"INCA",M_INH_A,1}, {"TSTA",M_INH_A,1}, {"???", M_IMPL,1}, {"CLRA",M_INH_A,1},
/* 50 */ {"NEGB",M_INH_B,1}, {"???", M_IMPL,1}, {"???", M_IMPL,1}, {"COMB",M_INH_B,1},
/* 54 */ {"LSRB",M_INH_B,1}, {"???", M_IMPL,1}, {"RORB",M_INH_B,1}, {"ASRB",M_INH_B,1},
/* 58 */ {"ASLB",M_INH_B,1}, {"ROLB",M_INH_B,1}, {"DECB",M_INH_B,1}, {"???", M_IMPL,1},
/* 5C */ {"INCB",M_INH_B,1}, {"TSTB",M_INH_B,1}, {"???", M_IMPL,1}, {"CLRB",M_INH_B,1},
/* 60 */ {"NEG", M_IDX,2}, {"???", M_IMPL,1}, {"???", M_IMPL,1}, {"COM", M_IDX,2},
/* 64 */ {"LSR", M_IDX,2}, {"???", M_IMPL,1}, {"ROR", M_IDX,2}, {"ASR", M_IDX,2},
/* 68 */ {"ASL", M_IDX,2}, {"ROL", M_IDX,2}, {"DEC", M_IDX,2}, {"???", M_IMPL,1},
/* 6C */ {"INC", M_IDX,2}, {"TST", M_IDX,2}, {"JMP", M_IDX,2}, {"CLR", M_IDX,2},
/* 70 */ {"NEG", M_EXT,3}, {"???", M_IMPL,1}, {"???", M_IMPL,1}, {"COM", M_EXT,3},
/* 74 */ {"LSR", M_EXT,3}, {"???", M_IMPL,1}, {"ROR", M_EXT,3}, {"ASR", M_EXT,3},
/* 78 */ {"ASL", M_EXT,3}, {"ROL", M_EXT,3}, {"DEC", M_EXT,3}, {"???", M_IMPL,1},
/* 7C */ {"INC", M_EXT,3}, {"TST", M_EXT,3}, {"JMP", M_EXT,3}, {"CLR", M_EXT,3},
/* 80 */ {"SUBA",M_IMM8,2}, {"CMPA",M_IMM8,2}, {"SBCA",M_IMM8,2}, {"SUBD",M_IMM16,3},
/* 84 */ {"ANDA",M_IMM8,2}, {"BITA",M_IMM8,2}, {"LDA", M_IMM8,2}, {"???", M_IMPL,1},
/* 88 */ {"EORA",M_IMM8,2}, {"ADCA",M_IMM8,2}, {"ORA", M_IMM8,2}, {"ADDA",M_IMM8,2},
/* 8C */ {"CMPX",M_IMM16,3}, {"BSR", M_REL8,2}, {"LDX", M_IMM16,3}, {"???", M_IMPL,1},
/* 90 */ {"SUBA",M_DIR,2}, {"CMPA",M_DIR,2}, {"SBCA",M_DIR,2}, {"SUBD",M_DIR,2},
/* 94 */ {"ANDA",M_DIR,2}, {"BITA",M_DIR,2}, {"LDA", M_DIR,2}, {"STA", M_DIR,2},
/* 98 */ {"EORA",M_DIR,2}, {"ADCA",M_DIR,2}, {"ORA", M_DIR,2}, {"ADDA",M_DIR,2},
/* 9C */ {"CMPX",M_DIR,2}, {"JSR", M_DIR,2}, {"LDX", M_DIR,2}, {"STX", M_DIR,2},
/* A0 */ {"SUBA",M_IDX,2}, {"CMPA",M_IDX,2}, {"SBCA",M_IDX,2}, {"SUBD",M_IDX,2},
/* A4 */ {"ANDA",M_IDX,2}, {"BITA",M_IDX,2}, {"LDA", M_IDX,2}, {"STA", M_IDX,2},
/* A8 */ {"EORA",M_IDX,2}, {"ADCA",M_IDX,2}, {"ORA", M_IDX,2}, {"ADDA",M_IDX,2},
/* AC */ {"CMPX",M_IDX,2}, {"JSR", M_IDX,2}, {"LDX", M_IDX,2}, {"STX", M_IDX,2},
/* B0 */ {"SUBA",M_EXT,3}, {"CMPA",M_EXT,3}, {"SBCA",M_EXT,3}, {"SUBD",M_EXT,3},
/* B4 */ {"ANDA",M_EXT,3}, {"BITA",M_EXT,3}, {"LDA", M_EXT,3}, {"STA", M_EXT,3},
/* B8 */ {"EORA",M_EXT,3}, {"ADCA",M_EXT,3}, {"ORA", M_EXT,3}, {"ADDA",M_EXT,3},
/* BC */ {"CMPX",M_EXT,3}, {"JSR", M_EXT,3}, {"LDX", M_EXT,3}, {"STX", M_EXT,3},
/* C0 */ {"SUBB",M_IMM8,2}, {"CMPB",M_IMM8,2}, {"SBCB",M_IMM8,2}, {"ADDD",M_IMM16,3},
/* C4 */ {"ANDB",M_IMM8,2}, {"BITB",M_IMM8,2}, {"LDB", M_IMM8,2}, {"???", M_IMPL,1},
/* C8 */ {"EORB",M_IMM8,2}, {"ADCB",M_IMM8,2}, {"ORB", M_IMM8,2}, {"ADDB",M_IMM8,2},
/* CC */ {"LDD", M_IMM16,3}, {"???", M_IMPL,1}, {"LDU", M_IMM16,3}, {"???", M_IMPL,1},
/* D0 */ {"SUBB",M_DIR,2}, {"CMPB",M_DIR,2}, {"SBCB",M_DIR,2}, {"ADDD",M_DIR,2},
/* D4 */ {"ANDB",M_DIR,2}, {"BITB",M_DIR,2}, {"LDB", M_DIR,2}, {"STB", M_DIR,2},
/* D8 */ {"EORB",M_DIR,2}, {"ADCB",M_DIR,2}, {"ORB", M_DIR,2}, {"ADDB",M_DIR,2},
/* DC */ {"LDD", M_DIR,2}, {"STD", M_DIR,2}, {"LDU", M_DIR,2}, {"STU", M_DIR,2},
/* E0 */ {"SUBB",M_IDX,2}, {"CMPB",M_IDX,2}, {"SBCB",M_IDX,2}, {"ADDD",M_IDX,2},
/* E4 */ {"ANDB",M_IDX,2}, {"BITB",M_IDX,2}, {"LDB", M_IDX,2}, {"STB", M_IDX,2},
/* E8 */ {"EORB",M_IDX,2}, {"ADCB",M_IDX,2}, {"ORB", M_IDX,2}, {"ADDB",M_IDX,2},
/* EC */ {"LDD", M_IDX,2}, {"STD", M_IDX,2}, {"LDU", M_IDX,2}, {"STU", M_IDX,2},
/* F0 */ {"SUBB",M_EXT,3}, {"CMPB",M_EXT,3}, {"SBCB",M_EXT,3}, {"ADDD",M_EXT,3},
/* F4 */ {"ANDB",M_EXT,3}, {"BITB",M_EXT,3}, {"LDB", M_EXT,3}, {"STB", M_EXT,3},
/* F8 */ {"EORB",M_EXT,3}, {"ADCB",M_EXT,3}, {"ORB", M_EXT,3}, {"ADDB",M_EXT,3},
/* FC */ {"LDD", M_EXT,3}, {"STD", M_EXT,3}, {"LDU", M_EXT,3}, {"STU", M_EXT,3},
};
static const opinfo_t optab10[256] = {
[0x20]={"LBRA",M_REL16,3}, [0x21]={"LBRN",M_REL16,3}, [0x22]={"LBHI",M_REL16,3},
[0x23]={"LBLS",M_REL16,3}, [0x24]={"LBCC",M_REL16,3}, [0x25]={"LBCS",M_REL16,3},
[0x26]={"LBNE",M_REL16,3}, [0x27]={"LBEQ",M_REL16,3}, [0x28]={"LBVC",M_REL16,3},
[0x29]={"LBVS",M_REL16,3}, [0x2A]={"LBPL",M_REL16,3}, [0x2B]={"LBMI",M_REL16,3},
[0x2C]={"LBGE",M_REL16,3}, [0x2D]={"LBLT",M_REL16,3}, [0x2E]={"LBGT",M_REL16,3},
[0x2F]={"LBLE",M_REL16,3},
[0x3F]={"SWI2",M_IMPL,2},
[0x83]={"CMPD",M_IMM16,4}, [0x8C]={"CMPY",M_IMM16,4}, [0x8E]={"LDY", M_IMM16,4},
[0x93]={"CMPD",M_DIR,3}, [0x9C]={"CMPY",M_DIR,3}, [0x9E]={"LDY", M_DIR,3}, [0x9F]={"STY",M_DIR,3},
[0xA3]={"CMPD",M_IDX,3}, [0xAC]={"CMPY",M_IDX,3}, [0xAE]={"LDY", M_IDX,3}, [0xAF]={"STY",M_IDX,3},
[0xB3]={"CMPD",M_EXT,4}, [0xBC]={"CMPY",M_EXT,4}, [0xBE]={"LDY", M_EXT,4}, [0xBF]={"STY",M_EXT,4},
[0xCE]={"LDS", M_IMM16,4}, [0xDE]={"LDS",M_DIR,3}, [0xDF]={"STS",M_DIR,3},
[0xEE]={"LDS", M_IDX,3}, [0xEF]={"STS",M_IDX,3},
[0xFE]={"LDS", M_EXT,4}, [0xFF]={"STS",M_EXT,4},
};
static const opinfo_t optab11[256] = {
[0x83]={"CMPU",M_IMM16,4}, [0x8C]={"CMPS",M_IMM16,4},
[0x93]={"CMPU",M_DIR,3}, [0x9C]={"CMPS",M_DIR,3},
[0xA3]={"CMPU",M_IDX,3}, [0xAC]={"CMPS",M_IDX,3},
[0xB3]={"CMPU",M_EXT,4}, [0xBC]={"CMPS",M_EXT,4},
/* 6309 additions can be added here */
};
static void fmt_idx(uint16_t *pc, char *out, size_t sz)
{
uint8_t post = mem_read8(*pc); (*pc)++;
int r = (post >> 5) & 3;
const char *rs[4] = {"X","Y","U","S"};
if (post & 0x80) {
int indir = (post & 0x10) ? 1 : 0;
const char *pre = indir ? "[" : "";
const char *suf = indir ? "]" : "";
switch (post & 0x0F) {
case 0x00: snprintf(out, sz, "%s,%s+%s", pre, rs[r], suf); break;
case 0x01: snprintf(out, sz, "%s,%s++%s", pre, rs[r], suf); break;
case 0x02: snprintf(out, sz, "%s,-%s%s", pre, rs[r], suf); break;
case 0x03: snprintf(out, sz, "%s,--%s%s", pre, rs[r], suf); break;
case 0x04: snprintf(out, sz, "%s,%s%s", pre, rs[r], suf); break;
case 0x05: snprintf(out, sz, "%sB,%s%s", pre, rs[r], suf); break;
case 0x06: snprintf(out, sz, "%sA,%s%s", pre, rs[r], suf); break;
case 0x08: { int8_t n = (int8_t)mem_read8(*pc); (*pc)++; snprintf(out,sz,"%s%d,%s%s",pre,n,rs[r],suf); break; }
case 0x09: { int16_t n=(int16_t)mem_read16(*pc); (*pc)+=2; snprintf(out,sz,"%s%d,%s%s",pre,n,rs[r],suf); break; }
case 0x0B: snprintf(out, sz, "%sD,%s%s", pre, rs[r], suf); break;
case 0x0C: { int8_t n=(int8_t)mem_read8(*pc); (*pc)++; snprintf(out,sz,"%s%d,PCR%s",pre,n,suf); break; }
case 0x0D: { int16_t n=(int16_t)mem_read16(*pc); (*pc)+=2; snprintf(out,sz,"%s%d,PCR%s",pre,n,suf); break; }
case 0x9F: { uint16_t n = mem_read16(*pc); (*pc)+=2; snprintf(out,sz,"[%04X]", n); break; }
default: snprintf(out, sz, "???"); break;
}
} else {
int off = post & 0x1F; if (off & 0x10) off |= ~0x1F;
snprintf(out, sz, "%d,%s", off, rs[r]);
}
}
int disasm(uint16_t addr, char *out, size_t outsz, uint16_t *next_addr)
{
uint16_t pc = addr;
uint8_t op = mem_read8(pc); pc++;
const opinfo_t *tab = optab;
const opinfo_t *info = &tab[op];
char operand[64] = "";
if (op == 0x10) {
uint8_t op2 = mem_read8(pc); pc++;
info = &optab10[op2];
if (!info->mnem) info = &(opinfo_t){"???", M_IMPL, 2};
} else if (op == 0x11) {
uint8_t op2 = mem_read8(pc); pc++;
info = &optab11[op2];
if (!info->mnem) info = &(opinfo_t){"???", M_IMPL, 2};
}
const char *m = info->mnem ? info->mnem : "???";
switch (info->mode) {
case M_IMPL: case M_INH_A: case M_INH_B: case M_INH_D: case M_SWI:
break;
case M_IMM8:
snprintf(operand, sizeof(operand), "#$%02X", mem_read8(pc)); pc++;
break;
case M_IMM16:
snprintf(operand, sizeof(operand), "#$%04X", mem_read16(pc)); pc += 2;
break;
case M_DIR: {
uint8_t a = mem_read8(pc); pc++;
snprintf(operand, sizeof(operand), "<$%02X", a);
break;
}
case M_EXT: {
uint16_t a = mem_read16(pc); pc += 2;
snprintf(operand, sizeof(operand), "$%04X", a);
break;
}
case M_REL8: {
int8_t off = (int8_t)mem_read8(pc); pc++;
uint16_t tgt = (uint16_t)(pc + off);
snprintf(operand, sizeof(operand), "$%04X", tgt);
break;
}
case M_REL16: {
int16_t off = (int16_t)mem_read16(pc); pc += 2;
uint16_t tgt = (uint16_t)(pc + off);
snprintf(operand, sizeof(operand), "$%04X", tgt);
break;
}
case M_IDX:
fmt_idx(&pc, operand, sizeof(operand));
break;
case M_PSH: {
uint8_t p = mem_read8(pc); pc++;
char regs[32] = "";
if (p & 0x80) strcat(regs, "PC,");
if (p & 0x40) strcat(regs, "U/S,");
if (p & 0x20) strcat(regs, "Y,");
if (p & 0x10) strcat(regs, "X,");
if (p & 0x08) strcat(regs, "A,");
if (p & 0x04) strcat(regs, "B,");
if (p & 0x02) strcat(regs, "CC,");
if (p & 0x01) strcat(regs, "DP,");
if (regs[0]) regs[strlen(regs)-1] = 0;
snprintf(operand, sizeof(operand), "#$%02X ; %s", p, regs);
break;
}
case M_TFR: {
uint8_t p = mem_read8(pc); pc++;
static const char *r16[] = {"D","X","Y","U","S","PC"};
static const char *r8[] = {"A","B","CC","??","??","??","??","??"};
int s = (p>>4)&15, d = p & 15;
if (s < 6 && d < 6)
snprintf(operand, sizeof(operand), "%s,%s", r16[s], r16[d]);
else
snprintf(operand, sizeof(operand), "#$%02X", p);
break;
}
case M_IMM32_6309:
snprintf(operand, sizeof(operand), "#$%08X", (mem_read16(pc)<<16)|mem_read16(pc+2));
pc += 4;
break;
default:
break;
}
if (operand[0])
snprintf(out, outsz, "%-5s %s", m, operand);
else
snprintf(out, outsz, "%s", m);
if (next_addr) *next_addr = pc;
return (int)(pc - addr);
}
+21
View File
@@ -0,0 +1,21 @@
/* disasm.h — mimic09 built-in disassembler */
#ifndef MIMIC09_DISASM_H
#define MIMIC09_DISASM_H
#include <stdint.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Disassemble one instruction at 'addr' into 'out' (null-terminated).
* Returns the number of bytes consumed by the instruction.
*/
int disasm(uint16_t addr, char *out, size_t outsz, uint16_t *next_addr);
#ifdef __cplusplus
}
#endif
#endif
+177
View File
@@ -0,0 +1,177 @@
/* loader.c — robust S-Record + Intel HEX + raw binary */
#include "loader.h"
#include "cpu.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
static uint8_t checksum(const uint8_t *buf, int len)
{
uint8_t s = 0;
for (int i = 0; i < len; i++) s += buf[i];
return (~s) & 0xFF;
}
static int load_srec(const char *path)
{
FILE *f = fopen(path, "r");
if (!f) return -1;
char line[512];
int total = 0;
while (fgets(line, sizeof(line), f)) {
char *p = line;
while (*p == '\r' || *p == '\n' || *p == ' ' || *p == '\t') p++;
if (*p != 'S' && *p != 's') continue;
char type = (char)toupper((unsigned char)p[1]);
if (type == '0' || type == '5') continue;
int len = 0;
unsigned addr = 0;
int alen = (type == '1' || type == '9') ? 2 : (type == '2' || type == '8') ? 3 : 4;
if (sscanf(p+2, "%2x", &len) != 1) continue;
const char *addrp = p + 4;
if (alen == 2) sscanf(addrp, "%4x", &addr);
else if (alen == 3) sscanf(addrp, "%6x", &addr);
else sscanf(addrp, "%8x", &addr);
int data_start = 4 + alen * 2;
int data_len = len - alen - 1;
if (data_len < 0) continue;
for (int i = 0; i < data_len; i++) {
unsigned b = 0;
if (sscanf(p + data_start + i*2, "%2x", &b) != 1) break;
mem_write8((uint16_t)(addr + i), (uint8_t)b);
total++;
}
if (type == '7' || type == '8' || type == '9') {
/* termination with optional entry point; monitor user sets PC */
}
}
fclose(f);
return total;
}
static int load_ihex(const char *path)
{
FILE *f = fopen(path, "r");
if (!f) return -1;
char line[512];
int total = 0;
uint32_t base = 0;
while (fgets(line, sizeof(line), f)) {
/* tolerate CRLF, leading whitespace, \r only lines */
char *p = line;
while (*p == '\r' || *p == '\n' || *p == ' ' || *p == '\t') p++;
if (*p != ':') continue;
p++; /* skip : */
unsigned len=0, addr=0, type=0;
if (sscanf(p, "%2x%4x%2x", &len, &addr, &type) != 3) continue;
if (type == 0) { /* data */
uint16_t a = (uint16_t)(base + addr);
char *datahex = p + 6; /* after len(2) addr(4) type(2) */
for (unsigned i = 0; i < len; i++) {
unsigned b = 0;
if (sscanf(datahex + i*2, "%2x", &b) != 1) break;
mem_write8((uint16_t)(a + i), (uint8_t)b);
total++;
}
} else if (type == 1) { /* eof */
break;
} else if (type == 2) { /* extended segment */
unsigned seg = 0; sscanf(p + 6, "%4x", &seg); base = seg * 16U;
} else if (type == 4) { /* extended linear */
unsigned seg = 0; sscanf(p + 6, "%4x", &seg); base = (uint32_t)seg << 16;
}
}
fclose(f);
return total;
}
static int load_raw(const char *path, uint16_t at)
{
FILE *f = fopen(path, "rb");
if (!f) return -1;
int total = 0;
int c;
while ((c = fgetc(f)) != EOF) {
mem_write8(at++, (uint8_t)c);
total++;
}
fclose(f);
return total;
}
int load_file(const char *path, uint16_t force_addr, int force)
{
/* auto detect by content or extension */
const char *ext = strrchr(path, '.');
if (force || (ext && (strcasecmp(ext, ".bin") == 0 || strcasecmp(ext, ".rom") == 0))) {
return load_raw(path, force_addr);
}
FILE *f = fopen(path, "r");
if (!f) return -1;
/* Skip leading CR/LF/whitespace to find real first significant char */
int first = 0;
for (;;) {
int c = fgetc(f);
if (c == EOF) break;
if (c != '\r' && c != '\n' && c != ' ' && c != '\t') {
first = c;
break;
}
}
fclose(f);
if (first == ':') return load_ihex(path);
if (first == 'S' || first == 's') return load_srec(path);
if (force_addr) return load_raw(path, force_addr);
return load_raw(path, 0);
}
int save_file(const char *path, uint16_t start, uint16_t end, const char *fmt)
{
FILE *f = fopen(path, "w");
if (!f) return -1;
int count = 0;
if (strcasecmp(fmt, "BIN") == 0) {
for (uint16_t a = start; a <= end; a++) {
fputc(mem_read8(a), f);
count++;
}
} else if (strcasecmp(fmt, "IHEX") == 0 || strcasecmp(fmt, "HEX") == 0) {
uint16_t addr = start;
while (addr <= end) {
int len = (end - addr + 1) > 16 ? 16 : (end - addr + 1);
fprintf(f, ":%02X%04X00", len, addr);
uint8_t sum = len + (addr >> 8) + (addr & 0xFF) + 0;
for (int i = 0; i < len; i++) {
uint8_t b = mem_read8((uint16_t)(addr + i));
fprintf(f, "%02X", b);
sum += b;
}
fprintf(f, "%02X\n", (uint8_t)((~sum + 1) & 0xFF));
addr += (uint16_t)len;
count += len;
}
fprintf(f, ":00000001FF\n");
} else { /* S19 default */
uint16_t addr = start;
while (addr <= end) {
int len = (end - addr + 1) > 32 ? 32 : (end - addr + 1);
uint8_t rec[3 + 32 + 1];
rec[0] = (uint8_t)(len + 3); /* count */
rec[1] = addr >> 8;
rec[2] = addr & 0xFF;
for (int i = 0; i < len; i++) rec[3 + i] = mem_read8((uint16_t)(addr + i));
uint8_t cs = checksum(rec, len + 3);
fprintf(f, "S1%02X%04X", len + 3, addr);
for (int i = 0; i < len; i++) fprintf(f, "%02X", rec[3 + i]);
fprintf(f, "%02X\n", cs);
addr += (uint16_t)len;
count += len;
}
/* termination record */
uint16_t exec = start;
fprintf(f, "S903%04X%02X\n", exec, (uint8_t)(~ (3 + (exec>>8) + (exec&0xFF)) & 0xFF));
}
fclose(f);
return count;
}
+25
View File
@@ -0,0 +1,25 @@
/* loader.h — S-Record, Intel HEX, raw binary load/save */
#ifndef MIMIC09_LOADER_H
#define MIMIC09_LOADER_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Load a file. If force_addr != 0 and format is raw, load at that address.
* Returns number of bytes loaded, or negative on error.
*/
int load_file(const char *path, uint16_t force_addr, int force);
/* Save region. fmt = "S19", "IHEX", "BIN"
* Returns bytes written or negative.
*/
int save_file(const char *path, uint16_t start, uint16_t end, const char *fmt);
#ifdef __cplusplus
}
#endif
#endif
+133
View File
@@ -0,0 +1,133 @@
/* main.c — mimic09 entry point */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include "cpu.h"
#include "debug.h"
#include "loader.h"
#include "acia.h"
#include "pia.h"
static void usage(const char *argv0)
{
printf("mimic09 — fast 6809/6309 emulator with C64-style monitor\n");
printf("Usage: %s [options] [file]\n\n", argv0);
printf("Options:\n");
printf(" -i, --interactive enter monitor immediately\n");
printf(" -l, --load <file> load SREC/IHEX/BIN at optional address\n");
printf(" -a, --addr <hex> force load address for raw binary\n");
printf(" -g, --go [addr] run (from addr or reset vector / PC)\n");
printf(" -c, --cpu <6809|6309> select CPU model (default 6809)\n");
printf(" --acia <hex> map 6850 ACIA at this base address\n");
printf(" --pia <hex> map 6821 PIA at this base address\n");
printf(" -v, --version\n");
printf(" -h, --help\n");
printf("\nExamples:\n");
printf(" %s -l samples/2299.HEX -i\n", argv0);
printf(" %s -l myprog.s19 --acia 0xA000 -g\n", argv0);
printf(" %s -i\n", argv0);
}
static void version(void)
{
printf("mimic09 0.9 (modern re-creation)\n");
}
int main(int argc, char **argv)
{
int interactive = 0;
int do_go = 0;
uint16_t go_addr = 0;
uint16_t force_addr = 0;
int have_force = 0;
cpu_model_t model = CPU_6809;
uint16_t acia_base = 0;
uint16_t pia_base = 0;
const char *loadfile = NULL;
static struct option longopts[] = {
{"interactive", no_argument, 0, 'i'},
{"load", required_argument, 0, 'l'},
{"addr", required_argument, 0, 'a'},
{"go", optional_argument, 0, 'g'},
{"cpu", required_argument, 0, 'c'},
{"acia", required_argument, 0, 1000},
{"pia", required_argument, 0, 1001},
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{0,0,0,0}
};
int ch;
while ((ch = getopt_long(argc, argv, "il:a:g::c:vh", longopts, NULL)) != -1) {
switch (ch) {
case 'i': interactive = 1; break;
case 'l': loadfile = optarg; break;
case 'a': have_force = 1; sscanf(optarg, "%hx", &force_addr); break;
case 'g': do_go = 1; if (optarg) sscanf(optarg, "%hx", &go_addr); break;
case 'c':
if (strcasecmp(optarg, "6309") == 0) model = CPU_6309;
else model = CPU_6809;
break;
case 1000: sscanf(optarg, "%hx", &acia_base); break;
case 1001: sscanf(optarg, "%hx", &pia_base); break;
case 'v': version(); return 0;
case 'h': usage(argv[0]); return 0;
default: usage(argv[0]); return 1;
}
}
if (optind < argc && !loadfile) {
loadfile = argv[optind];
}
cpu_init(model);
if (acia_base) acia_init(acia_base);
if (pia_base) pia_init(pia_base);
if (loadfile) {
int n = load_file(loadfile, have_force ? force_addr : 0, have_force);
if (n < 0) {
fprintf(stderr, "Failed to load %s\n", loadfile);
} else {
printf("Loaded %d bytes from %s\n", n, loadfile);
}
}
/* Default: if no -i and a load happened, set PC from reset vector if it looks good */
if (!interactive && loadfile) {
uint16_t rst = mem_read16(0xFFFE);
if (rst >= 0x0100 && rst <= 0xFEFF) {
cpu.pc = rst;
printf("PC set from reset vector to %04X\n", cpu.pc);
}
}
if (do_go) {
if (go_addr) cpu.pc = go_addr;
cpu.halted = false;
cpu.debug_mode = false;
/* Run with ACIA polling if present */
printf("Running. Press Ctrl-C to return to monitor.\n");
/* simple loop so we can feed ACIA chars */
while (!cpu.halted) {
cpu_step();
/* ACIA input is polled inside its own layer when needed, but we help here */
/* In this simple build the ACIA uses direct read in raw mode during G */
if (acia_base && (cpu.total_cycles & 0x7F) == 0) {
/* acia_poll_input is internal; the acia read path + raw term during G is sufficient */
}
}
}
if (interactive || (!loadfile && !do_go)) {
monitor_run();
}
return 0;
}
+42
View File
@@ -0,0 +1,42 @@
/* pia.c — very basic 6821 PIA (two ports, no full handshake yet) */
#include "pia.h"
#include "cpu.h"
static uint16_t pia_base;
static uint8_t pra, ddra, cra;
static uint8_t prb, ddrb, crb;
void pia_init(uint16_t base)
{
pia_base = base;
pra = prb = 0;
ddra = ddrb = 0;
cra = crb = 0;
cpu_register_device(base, pia_read, pia_write);
}
uint8_t pia_read(uint16_t off)
{
switch (off & 3) {
case 0: return (cra & 0x04) ? pra : ddra;
case 1: return cra;
case 2: return (crb & 0x04) ? prb : ddrb;
case 3: return crb;
}
return 0xFF;
}
void pia_write(uint16_t off, uint8_t val)
{
switch (off & 3) {
case 0:
if (cra & 0x04) pra = val; else ddra = val;
break;
case 1: cra = val; break;
case 2:
if (crb & 0x04) prb = val; else ddrb = val;
break;
case 3: crb = val; break;
}
}
+19
View File
@@ -0,0 +1,19 @@
/* pia.h — 6821 PIA emulation (basic) */
#ifndef MIMIC09_PIA_H
#define MIMIC09_PIA_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
void pia_init(uint16_t base);
uint8_t pia_read(uint16_t off);
void pia_write(uint16_t off, uint8_t val);
#ifdef __cplusplus
}
#endif
#endif
+29
View File
@@ -0,0 +1,29 @@
/* util.c */
#include "util.h"
#include <ctype.h>
int hexval(char c)
{
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'A' && c <= 'F') return 10 + (c - 'A');
if (c >= 'a' && c <= 'f') return 10 + (c - 'a');
return -1;
}
int parse_hex_byte(const char *p, uint8_t *out)
{
int h = hexval(p[0]);
int l = hexval(p[1]);
if (h < 0 || l < 0) return 0;
*out = (uint8_t)((h << 4) | l);
return 1;
}
int parse_hex_word(const char *p, uint16_t *out)
{
uint8_t hi, lo;
if (!parse_hex_byte(p, &hi) || !parse_hex_byte(p+2, &lo)) return 0;
*out = (uint16_t)((hi << 8) | lo);
return 1;
}
+13
View File
@@ -0,0 +1,13 @@
/* util.h — small helpers */
#ifndef MIMIC09_UTIL_H
#define MIMIC09_UTIL_H
#include <stdint.h>
#include <stddef.h>
int hexval(char c);
int parse_hex_byte(const char *p, uint8_t *out);
int parse_hex_word(const char *p, uint16_t *out);
#endif