This document specifies the C application binary interface (ABI) exposed by the ColdFire emulator via include/coldfire.h.
The interface is strictly standardized to C11 / C++17 compatibility. No Nim runtime internals, garbage collector pointers, or C++ language constructs cross this boundary.
The library uses a thread-safe, idempotent initialization mechanism.
CF_MUST_CHECK int cf_runtime_init(void);
1 if the runtime is successfully initialized and ready for use; 0 if an initialization failure or timeout occurred.stderr, and returns 0 on all subsequent invocations.cf_create) query this status internally and return NULL if initialization did not complete.coldfire.h)The core execution context is opaque:
typedef struct cf_ctx cf_ctx;
typedef cf_ctx coldfire_t;
typedef enum cf_isa_variant {
CF_ISA_A = 0, /* Baseline ColdFire ISA_A (MCF5307, etc.) */
CF_ISA_A_PLUS = 1, /* ColdFire ISA_A+ (MCF5249, MCF5272, etc.) */
CF_ISA_B = 2, /* ColdFire ISA_B (MCF5407 superscalar V4 core) */
CF_ISA_C = 3, /* ColdFire ISA_C */
CF_ISA_EMAC = 0x100 /* Extended MAC / DSP instructions mask */
} cf_isa_variant;
typedef struct cf_config {
cf_isa_variant isa; /* Target ISA architecture */
uint32_t vbr_mask; /* Hardware VBR alignment mask (e.g. 0xFFF00000) */
void* user; /* Host user context pointer passed to callbacks */
cf_read_fn rd; /* Memory bus read callback */
cf_write_fn wr; /* Memory bus write callback */
cf_iack_fn iack; /* Interrupt acknowledge callback */
} cf_config;
Memory space is external to the core. The host platform supplies three callbacks governing bus read, bus write, and interrupt acknowledge cycles:
typedef enum cf_bus_status {
CF_BUS_OK = 0, /* Access completed normally */
CF_BUS_UNMAPPED = 1, /* Address not decoded by any device */
CF_BUS_SIZE_ILLEGAL = 2, /* Address decoded, but transfer size not supported */
CF_BUS_FAULT = 3 /* Target device reported an internal bus error */
} cf_bus_status;
typedef uint32_t (*cf_read_fn)(void* user, uint32_t addr, int size, cf_bus_status* status);
typedef void (*cf_write_fn)(void* user, uint32_t addr, int size, uint32_t value, cf_bus_status* status);
typedef void (*cf_iack_fn)(void* user, int level, uint8_t vector);
size: The operand transfer width in bytes: 1 (byte), 2 (word), or 4 (longword).status: Out-parameter initialized by the core to CF_BUS_OK before the callback is invoked. If the board returns any non-OK status, the core aborts the instruction and raises an access fault exception.cf_iack_fn: Called immediately after the exception stack frame is constructed and prior to executing the first handler instruction. Edge-triggered interrupt sources should be acknowledged here.cf_ctx* cf_create(const cf_config* config);
void cf_destroy(cf_ctx* ctx);
void cf_reset(cf_ctx* ctx, uint32_t initial_sp, uint32_t initial_pc);
cf_create: Allocates a new CPU instance configured by config. Returns NULL if memory allocation fails, if config is NULL, or if the runtime is uninitialized.cf_destroy: Frees all resources allocated to the context. Safe to call with NULL.cf_reset: Restores the core to hardware reset state:
a7 to initial_sp.pc to initial_pc.initial_pc.uint32_t cf_exec(cf_ctx* ctx, uint32_t max_cycles);
max_cycles specifies the execution cycle budget.max_cycles by up to the cycle duration of the final instruction executed. Callers tracking a strict timebase should carry spent - max_cycles into the subsequent invocation.0 immediately if the core is halted, if ctx is NULL, or if max_cycles is 0.int cf_set_reg(cf_ctx* ctx, int index, uint32_t value);
uint32_t cf_get_reg(const cf_ctx* ctx, int index);
coldfire.h)| Identifier | Value | Target Register | Description |
|---|---|---|---|
CF_REG_D0 .. CF_REG_D7 |
0 .. 7 |
d0 .. d7 |
32-bit Data Registers |
CF_REG_A0 .. CF_REG_A7 |
8 .. 15 |
a0 .. a7 |
32-bit Address Registers (a7 is the stack pointer) |
CF_REG_SP |
15 |
a7 / SP |
Stack Pointer alias |
CF_REG_SR |
16 |
SR |
Status Register (Condition codes + Supervisor state) |
CF_REG_PC |
17 |
PC |
Program Counter (Read-only via cf_get_reg) |
CF_REG_VBR |
18 |
VBR |
Vector Base Register |
CF_REG_CACR |
19 |
CACR |
Cache Control Register |
CF_REG_ACR0 |
20 |
ACR0 |
Access Control Register 0 (Data space) |
CF_REG_ACR1 |
21 |
ACR1 |
Access Control Register 1 (Data space) |
CF_REG_RAMBAR0 |
22 |
RAMBAR0 |
On-chip SRAM Base Address Register 0 |
CF_REG_RAMBAR1 |
23 |
RAMBAR1 |
On-chip SRAM Base Address Register 1 |
CF_REG_MBAR |
24 |
MBAR |
Module Base Address Register |
CF_REG_ACR2 |
25 |
ACR2 |
Access Control Register 2 (Instruction space) |
CF_REG_ACR3 |
26 |
ACR3 |
Access Control Register 3 (Instruction space) |
cf_set_reg returns 1 on success, or 0 if index is out of range or read-only (such as CF_REG_PC).
int cf_halted(const cf_ctx* ctx);
int cf_faulted(const cf_ctx* ctx);
cf_halted: Returns 1 if execution has halted (due to an unrecoverable fault or explicit stop condition) and cannot proceed without a reset.cf_faulted: Returns 1 if the halt condition resulted from an unhandled trap, bus fault, illegal instruction, unaligned access, or divide-by-zero.#define CF_IRQ_NONE 0
void cf_set_irq(cf_ctx* ctx, int level, uint8_t vector, int autovector);
level: Interrupt priority level 1 through 7, or CF_IRQ_NONE (0) to clear.vector: Interrupt vector index (used when autovector == 0).autovector: If non-zero, the core generates the vector automatically based on level (24 + level).size_t cf_state_size(void);
void cf_state_save(const cf_ctx* ctx, void* dst);
void cf_state_load(cf_ctx* ctx, const void* src);
cf_state_size: Returns the exact byte buffer size required to serialize core state (145 bytes payload + header/checksum).cf_state_save: Serializes internal registers, pipeline latches, interrupt states, and cycle counters into dst.cf_state_load: Deserializes saved state from src.#include "coldfire.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
uint8_t ram[1024 * 1024]; // 1MB RAM
} Machine;
static uint32_t bus_read(void* user, uint32_t addr, int size, cf_bus_status* status) {
Machine* m = (Machine*)user;
if (addr + size <= sizeof(m->ram)) {
uint32_t val = 0;
for (int i = 0; i < size; ++i) {
val = (val << 8) | m->ram[addr + i];
}
return val;
}
*status = CF_BUS_UNMAPPED;
return 0;
}
static void bus_write(void* user, uint32_t addr, int size, uint32_t value, cf_bus_status* status) {
Machine* m = (Machine*)user;
if (addr + size <= sizeof(m->ram)) {
for (int i = size - 1; i >= 0; --i) {
m->ram[addr + i] = (uint8_t)(value & 0xFF);
value >>= 8;
}
return;
}
*status = CF_BUS_UNMAPPED;
}
static void bus_iack(void* user, int level, uint8_t vector) {
printf("Acknowledged IRQ level %d, vector %u\n", level, vector);
}
int main(void) {
if (!cf_runtime_init()) {
fprintf(stderr, "Failed to initialize ColdFire runtime\n");
return 1;
}
Machine machine = {0};
// Initialize reset vector: Initial SP = 0x00080000, Initial PC = 0x00000400
machine.ram[0] = 0x00; machine.ram[1] = 0x08; machine.ram[2] = 0x00; machine.ram[3] = 0x00;
machine.ram[4] = 0x00; machine.ram[5] = 0x00; machine.ram[6] = 0x04; machine.ram[7] = 0x00;
// Place a NOP opcode at 0x400: 0x4E71
machine.ram[0x400] = 0x4E;
machine.ram[0x401] = 0x71;
cf_config cfg = {
.isa = CF_ISA_A,
.vbr_mask = 0xFFF00000,
.user = &machine,
.rd = bus_read,
.wr = bus_write,
.iack = bus_iack
};
cf_ctx* cpu = cf_create(&cfg);
if (!cpu) {
fprintf(stderr, "Failed to create CPU context\n");
return 1;
}
cf_reset(cpu, 0x00080000, 0x00000400);
uint32_t spent = cf_exec(cpu, 100);
printf("Executed %u cycles. PC = 0x%08X\n", spent, cf_get_reg(cpu, CF_REG_PC));
cf_destroy(cpu);
return 0;
}