mcf5407

ColdFire C API Specification

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.


Table of Contents

  1. Runtime Initialization
  2. ColdFire Core API (coldfire.h)
  3. Complete C Integration Example

1. Runtime Initialization

The library uses a thread-safe, idempotent initialization mechanism.

CF_MUST_CHECK int cf_runtime_init(void);

2. ColdFire Core API (coldfire.h)

Context & Configuration Types

The core execution context is opaque:

typedef struct cf_ctx cf_ctx;
typedef cf_ctx coldfire_t;

ISA Variants

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;

Core Configuration Structure

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 & Bus Callbacks

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);

Callback Semantics

Core Lifecycle

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);

Execution & Stepping

uint32_t cf_exec(cf_ctx* ctx, uint32_t max_cycles);

Register Access

int cf_set_reg(cf_ctx* ctx, int index, uint32_t value);
uint32_t cf_get_reg(const cf_ctx* ctx, int index);

Canonical Register Constants (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).

Halt & Fault Inspection

int cf_halted(const cf_ctx* ctx);
int cf_faulted(const cf_ctx* ctx);

Interrupt Controller

#define CF_IRQ_NONE 0

void cf_set_irq(cf_ctx* ctx, int level, uint8_t vector, int autovector);

State Serialization

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);

3. Complete C Integration Example

#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;
}