|
Rocket 68
A Motorola 68000 CPU emulator in C
|
Public CPU core API for Rocket 68. More...
#include <assert.h>#include <setjmp.h>#include <stdalign.h>#include <stdbool.h>#include <stddef.h>#include <stdint.h>Go to the source code of this file.
Classes | |
| union | M68kRegister |
| 68000 general register view. More... | |
| struct | M68kCpu |
| Full CPU context for one emulated 68000 instance. More... | |
Macros | |
| #define | M68K_INT_ACK_AUTOVECTOR 0xffffffff |
| INT ACK callback return value for autovector. | |
| #define | M68K_INT_ACK_SPURIOUS 0xfffffffe |
| INT ACK callback return value for spurious interrupt. | |
| #define | M68K_FC_USER_DATA 1 |
| Function code for user data access. | |
| #define | M68K_FC_USER_PROG 2 |
| Function code for user program access. | |
| #define | M68K_FC_SUPV_DATA 5 |
| Function code for supervisor data access. | |
| #define | M68K_FC_SUPV_PROG 6 |
| Function code for supervisor program access. | |
| #define | M68K_FC_INT_ACK 7 |
| Function code for interrupt acknowledge cycle. | |
| #define | M68K_SR_C (1 << 0) |
| Carry flag bit mask in SR. | |
| #define | M68K_SR_V (1 << 1) |
| Overflow flag bit mask in SR. | |
| #define | M68K_SR_Z (1 << 2) |
| Zero flag bit mask in SR. | |
| #define | M68K_SR_N (1 << 3) |
| Negative flag bit mask in SR. | |
| #define | M68K_SR_X 0x0010 |
| Extend flag bit mask in SR. | |
| #define | M68K_SR_S 0x2000 |
| Supervisor state bit mask in SR. | |
Typedefs | |
| typedef uint8_t | u8 |
| Unsigned 8-bit integer type. | |
| typedef uint16_t | u16 |
| Unsigned 16-bit integer type. | |
| typedef uint32_t | u32 |
| Unsigned 32-bit integer type. | |
| typedef int8_t | s8 |
| Signed 8-bit integer type. | |
| typedef int16_t | s16 |
| Signed 16-bit integer type. | |
| typedef int32_t | s32 |
| Signed 32-bit integer type. | |
| typedef struct M68kCpu | M68kCpu |
| Forward declaration of the CPU context. | |
| typedef int(* | M68kWaitBusCallback) (M68kCpu *cpu, u32 address, M68kSize size, bool is_write) |
| Bus wait callback. | |
| typedef int(* | M68kIntAckCallback) (M68kCpu *cpu, int level) |
| Interrupt acknowledge callback. | |
| typedef void(* | M68kFcCallback) (M68kCpu *cpu, unsigned int new_fc) |
| Function code callback emitted for each bus/program access context. | |
| typedef void(* | M68kInstrHookCallback) (M68kCpu *cpu, u32 pc) |
| Instruction hook callback executed before instruction decode/execute. | |
| typedef void(* | M68kPcChangedCallback) (M68kCpu *cpu, u32 new_pc) |
| Program counter changed callback. | |
| typedef void(* | M68kResetCallback) (M68kCpu *cpu) |
| Reset callback. | |
| typedef int(* | M68kTasCallback) (M68kCpu *cpu) |
| TAS callback. Return nonzero to allow write-back, or zero to block it. | |
| typedef int(* | M68kIllgCallback) (M68kCpu *cpu, int opcode) |
| Illegal instruction callback. Return nonzero to suppress vector 4 and continue execution. Line-A and line-F exceptions do not invoke this callback. | |
| typedef u8(* | M68kRead8Callback) (M68kCpu *cpu, u32 address) |
| Host memory read callback (8-bit). | |
| typedef u16(* | M68kRead16Callback) (M68kCpu *cpu, u32 address) |
| Host memory read callback (16-bit). | |
| typedef u32(* | M68kRead32Callback) (M68kCpu *cpu, u32 address) |
| Host memory read callback (32-bit). | |
| typedef void(* | M68kWrite8Callback) (M68kCpu *cpu, u32 address, u8 value) |
| Host memory write callback (8-bit). | |
| typedef void(* | M68kWrite16Callback) (M68kCpu *cpu, u32 address, u16 value) |
| Host memory write callback (16-bit). | |
| typedef void(* | M68kWrite32Callback) (M68kCpu *cpu, u32 address, u32 value) |
| Host memory write callback (32-bit). | |
Enumerations | |
| enum | M68kSize { SIZE_BYTE = 1 , SIZE_WORD = 2 , SIZE_LONG = 4 } |
| Operand size in bytes. More... | |
| enum | M68kModel { M68K_MODEL_68000 = 0 , M68K_MODEL_68010 = 1 } |
| CPU model selection. The 68000 is the default. More... | |
| enum | M68kAddrMode { ADDR_MODE_DATA_REG_DIRECT , ADDR_MODE_ADDR_REG_DIRECT , ADDR_MODE_ADDR_REG_INDIRECT , ADDR_MODE_ARI_POSTINC , ADDR_MODE_ARI_PREDEC , ADDR_MODE_ARI_DISP , ADDR_MODE_ARI_INDEX , ADDR_MODE_ABS_SHORT , ADDR_MODE_ABS_LONG , ADDR_MODE_PC_DISP , ADDR_MODE_PC_INDEX , ADDR_MODE_IMMEDIATE } |
| Effective address mode encoding used by internal helpers and opcode decoding. More... | |
Functions | |
| void | m68k_init (M68kCpu *cpu, u8 *memory, u32 memory_size) |
| Initialize a CPU context and bind memory. | |
| void | m68k_reset (M68kCpu *cpu) |
| Reset CPU state and load initial vectors from memory. | |
| void | m68k_set_pc (M68kCpu *cpu, u32 pc) |
| Set the program counter. | |
| u32 | m68k_get_pc (M68kCpu *cpu) |
| Get the current program counter. | |
| void | m68k_set_model (M68kCpu *cpu, M68kModel model) |
| Select the CPU model profile. | |
| M68kModel | m68k_get_model (M68kCpu *cpu) |
| Read the selected CPU model profile. | |
| void | m68k_set_dr (M68kCpu *cpu, int reg, u32 value) |
| Set data register value. | |
| u32 | m68k_get_dr (M68kCpu *cpu, int reg) |
| Get data register value. | |
| void | m68k_set_ar (M68kCpu *cpu, int reg, u32 value) |
| Set address register value. | |
| u32 | m68k_get_ar (M68kCpu *cpu, int reg) |
| Get address register value. | |
| void | m68k_step_ex (M68kCpu *cpu, bool check_exceptions) |
| Execute one instruction with optional interrupt and trace checks. | |
| void | m68k_step (M68kCpu *cpu) |
| Execute one instruction step. | |
| int | m68k_execute (M68kCpu *cpu, int cycles) |
| Execute instructions for a cycle budget. | |
| int | m68k_cycles_run (M68kCpu *cpu) |
| Get cycles consumed in the active timeslice accounting model. | |
| int | m68k_cycles_remaining (M68kCpu *cpu) |
| Get remaining cycles in the current timeslice. | |
| void | m68k_modify_timeslice (M68kCpu *cpu, int cycles) |
| Add cycles to current timeslice. | |
| void | m68k_end_timeslice (M68kCpu *cpu) |
| End current timeslice immediately. | |
| void | m68k_set_sr (M68kCpu *cpu, u16 new_sr) |
| Set the status register. | |
| void | m68k_set_irq (M68kCpu *cpu, int level) |
| Set the asserted interrupt level (the virtual IPL lines). | |
| u8 | m68k_read_8 (M68kCpu *cpu, u32 address) |
| Read one byte from emulated memory. | |
| u16 | m68k_read_16 (M68kCpu *cpu, u32 address) |
| Read one word from emulated memory. | |
| u32 | m68k_read_32 (M68kCpu *cpu, u32 address) |
| Read one longword from emulated memory. | |
| void | m68k_write_8 (M68kCpu *cpu, u32 address, u8 value) |
| Write one byte to emulated memory. | |
| void | m68k_write_16 (M68kCpu *cpu, u32 address, u16 value) |
| Write one word to emulated memory. | |
| void | m68k_write_32 (M68kCpu *cpu, u32 address, u32 value) |
| Write one longword to emulated memory. | |
| void | m68k_set_wait_bus_callback (M68kCpu *cpu, M68kWaitBusCallback callback) |
| Install wait bus callback. | |
| void | m68k_set_int_ack_callback (M68kCpu *cpu, M68kIntAckCallback callback) |
| Install interrupt acknowledge callback. | |
| void | m68k_set_fc_callback (M68kCpu *cpu, M68kFcCallback callback) |
| Install function code callback. | |
| void | m68k_set_instr_hook_callback (M68kCpu *cpu, M68kInstrHookCallback callback) |
| Install instruction hook callback. | |
| void | m68k_set_pc_changed_callback (M68kCpu *cpu, M68kPcChangedCallback callback) |
| Install PC-changed callback. | |
| void | m68k_set_reset_callback (M68kCpu *cpu, M68kResetCallback callback) |
| Install reset callback. | |
| void | m68k_set_tas_callback (M68kCpu *cpu, M68kTasCallback callback) |
| Install TAS callback. | |
| void | m68k_set_illg_callback (M68kCpu *cpu, M68kIllgCallback callback) |
| Install illegal opcode callback. | |
| void | m68k_set_read8_callback (M68kCpu *cpu, M68kRead8Callback callback) |
| Install host memory read callback (8-bit). | |
| void | m68k_set_read16_callback (M68kCpu *cpu, M68kRead16Callback callback) |
| Install host memory read callback (16-bit). | |
| void | m68k_set_read32_callback (M68kCpu *cpu, M68kRead32Callback callback) |
| Install host memory read callback (32-bit). | |
| void | m68k_set_write8_callback (M68kCpu *cpu, M68kWrite8Callback callback) |
| Install host memory write callback (8-bit). | |
| void | m68k_set_write16_callback (M68kCpu *cpu, M68kWrite16Callback callback) |
| Install host memory write callback (16-bit). | |
| void | m68k_set_write32_callback (M68kCpu *cpu, M68kWrite32Callback callback) |
| Install host memory write callback (32-bit). | |
| void | m68k_raise_bus_error (M68kCpu *cpu) |
| Report a bus error from the current host memory callback. | |
| unsigned int | m68k_context_size (void) |
| Get serialized context size. | |
| void | m68k_get_context (M68kCpu *cpu, void *dst) |
| Copy CPU context into an external buffer. | |
| void | m68k_set_context (M68kCpu *cpu, const void *src) |
| Restore CPU context from an external buffer. | |
| size_t | m68k_serialize (const M68kCpu *cpu, u8 *buffer, size_t capacity) |
| Serialize architectural CPU state into a portable save state. | |
| bool | m68k_deserialize (M68kCpu *cpu, const u8 *buffer, size_t length) |
| Restore architectural CPU state from a portable save state. | |
Public CPU core API for Rocket 68.
| #define M68K_FC_INT_ACK 7 |
Function code for interrupt acknowledge cycle.
| #define M68K_FC_SUPV_DATA 5 |
Function code for supervisor data access.
| #define M68K_FC_SUPV_PROG 6 |
Function code for supervisor program access.
| #define M68K_FC_USER_DATA 1 |
Function code for user data access.
| #define M68K_FC_USER_PROG 2 |
Function code for user program access.
| #define M68K_INT_ACK_AUTOVECTOR 0xffffffff |
INT ACK callback return value for autovector.
| #define M68K_INT_ACK_SPURIOUS 0xfffffffe |
INT ACK callback return value for spurious interrupt.
| #define M68K_SR_C (1 << 0) |
Carry flag bit mask in SR.
| #define M68K_SR_N (1 << 3) |
Negative flag bit mask in SR.
| #define M68K_SR_S 0x2000 |
Supervisor state bit mask in SR.
| #define M68K_SR_V (1 << 1) |
Overflow flag bit mask in SR.
| #define M68K_SR_X 0x0010 |
Extend flag bit mask in SR.
| #define M68K_SR_Z (1 << 2) |
Zero flag bit mask in SR.
| typedef struct M68kCpu M68kCpu |
Forward declaration of the CPU context.
Full CPU context for one emulated 68000 instance.
| typedef void(* M68kFcCallback) (M68kCpu *cpu, unsigned int new_fc) |
Function code callback emitted for each bus/program access context.
| typedef int(* M68kIllgCallback) (M68kCpu *cpu, int opcode) |
Illegal instruction callback. Return nonzero to suppress vector 4 and continue execution. Line-A and line-F exceptions do not invoke this callback.
Instruction hook callback executed before instruction decode/execute.
| typedef int(* M68kIntAckCallback) (M68kCpu *cpu, int level) |
Interrupt acknowledge callback.
Return a vector number, M68K_INT_ACK_AUTOVECTOR, or M68K_INT_ACK_SPURIOUS.
| typedef void(* M68kResetCallback) (M68kCpu *cpu) |
Reset callback.
| typedef int(* M68kTasCallback) (M68kCpu *cpu) |
TAS callback. Return nonzero to allow write-back, or zero to block it.
Bus wait callback.
Called on every memory access (reads, writes, and instruction fetches) before the access occurs. Returns the number of extra M68K cycles to deduct for bus contention or wait states. Return 0 for no delay.
| cpu | CPU context. |
| address | 24-bit physical address. |
| size | Operand size (byte, word, or long). |
| is_write | true for write accesses, false for reads and fetches. |
Host memory write callback (16-bit).
Host memory write callback (32-bit).
Host memory write callback (8-bit).
| typedef int16_t s16 |
Signed 16-bit integer type.
| typedef int32_t s32 |
Signed 32-bit integer type.
| typedef int8_t s8 |
Signed 8-bit integer type.
| typedef uint16_t u16 |
Unsigned 16-bit integer type.
| typedef uint32_t u32 |
Unsigned 32-bit integer type.
| typedef uint8_t u8 |
Unsigned 8-bit integer type.
| enum M68kAddrMode |
Effective address mode encoding used by internal helpers and opcode decoding.
| enum M68kModel |
| enum M68kSize |
| unsigned int m68k_context_size | ( | void | ) |
Get serialized context size.
| int m68k_cycles_remaining | ( | M68kCpu * | cpu | ) |
Get remaining cycles in the current timeslice.
| cpu | CPU context. |
| int m68k_cycles_run | ( | M68kCpu * | cpu | ) |
Get cycles consumed in the active timeslice accounting model.
| cpu | CPU context. |
Restore architectural CPU state from a portable save state.
| cpu | CPU context; its memory binding and callbacks are kept. |
| buffer | Serialized state produced by m68k_serialize. |
| length | Length of the serialized state in bytes. |
Fields with unknown tags are skipped, so states written by newer library versions restore their known fields.
| void m68k_end_timeslice | ( | M68kCpu * | cpu | ) |
End current timeslice immediately.
| cpu | CPU context. |
| int m68k_execute | ( | M68kCpu * | cpu, |
| int | cycles ) |
Execute instructions for a cycle budget.
| cpu | CPU context. |
| cycles | Requested number of cycles to add to the current timeslice. |
Get address register value.
| cpu | CPU context. |
| reg | Address register index [0,7]. |
| void m68k_get_context | ( | M68kCpu * | cpu, |
| void * | dst ) |
Copy CPU context into an external buffer.
Memory pointer and size are copied as part of the context payload.
| cpu | CPU context. |
| dst | Destination buffer with at least m68k_context_size bytes. |
Get data register value.
| cpu | CPU context. |
| reg | Data register index [0,7]. |
Read the selected CPU model profile.
| cpu | CPU context. |
Get the current program counter.
| cpu | CPU context. |
Initialize a CPU context and bind memory.
| cpu | CPU context to initialize. |
| memory | Pointer to emulated memory. |
| memory_size | Size of memory in bytes. |
| void m68k_modify_timeslice | ( | M68kCpu * | cpu, |
| int | cycles ) |
Add cycles to current timeslice.
| cpu | CPU context. |
| cycles | Number of cycles to add. |
| void m68k_raise_bus_error | ( | M68kCpu * | cpu | ) |
Report a bus error from the current host memory callback.
| cpu | CPU context supplied to the callback. |
Return immediately after this call (read callbacks may return any value). The core processes vector 2 after the callback returns, using the current access address and direction. This function does not unwind the callback. Calls outside a CPU memory access, including disassembly and internal peeks, have no effect. Callbacks must not reenter execution or memory access helpers on the same CPU. Earlier bus side effects are not rolled back.
Read one word from emulated memory.
| cpu | CPU context. |
| address | Emulated address. |
Read one longword from emulated memory.
| cpu | CPU context. |
| address | Emulated address. |
Read one byte from emulated memory.
| cpu | CPU context. |
| address | Emulated address. |
| void m68k_reset | ( | M68kCpu * | cpu | ) |
Reset CPU state and load initial vectors from memory.
| cpu | CPU context. |
Serialize architectural CPU state into a portable save state.
| cpu | CPU context. |
| buffer | Output buffer, or NULL to query the required size. |
| capacity | Size of the output buffer in bytes. |
The format is versioned, tagged, and big-endian, so it is stable across builds, compilers, and host architectures, unlike the raw m68k_get_context blob. Host bindings (memory, callbacks) and transient fault latches are not serialized. Serializing in the middle of exception processing is not supported.
Set address register value.
| cpu | CPU context. |
| reg | Address register index [0,7]. |
| value | New 32-bit value. |
| void m68k_set_context | ( | M68kCpu * | cpu, |
| const void * | src ) |
Restore CPU context from an external buffer.
The bound memory pointer and memory size from the destination context are preserved.
| cpu | CPU context. |
| src | Source buffer with at least m68k_context_size bytes. |
Set data register value.
| cpu | CPU context. |
| reg | Data register index [0,7]. |
| value | New 32-bit value. |
| void m68k_set_fc_callback | ( | M68kCpu * | cpu, |
| M68kFcCallback | callback ) |
Install function code callback.
| cpu | CPU context. |
| callback | Callback function pointer or NULL. |
The callback is invoked before memory/program accesses with the active function code.
| void m68k_set_illg_callback | ( | M68kCpu * | cpu, |
| M68kIllgCallback | callback ) |
Install illegal opcode callback.
| cpu | CPU context. |
| callback | Callback function pointer or NULL. |
| void m68k_set_instr_hook_callback | ( | M68kCpu * | cpu, |
| M68kInstrHookCallback | callback ) |
Install instruction hook callback.
| cpu | CPU context. |
| callback | Callback function pointer or NULL. |
| void m68k_set_int_ack_callback | ( | M68kCpu * | cpu, |
| M68kIntAckCallback | callback ) |
Install interrupt acknowledge callback.
| cpu | CPU context. |
| callback | Callback function pointer or NULL. |
| void m68k_set_irq | ( | M68kCpu * | cpu, |
| int | level ) |
Set the asserted interrupt level (the virtual IPL lines).
Levels 1-6 are level-sensitive. Without an INT ACK callback the request clears automatically when serviced; with a callback installed the level stays asserted until the host calls this function with 0 (or a new level). Level 7 is edge-sensitive: each transition to 7 latches one non-maskable interrupt.
| cpu | CPU context. |
| level | Interrupt level in range [0,7]. |
Select the CPU model profile.
| cpu | CPU context. |
| model | Model profile; the default after m68k_init is the 68000. |
On the 68000 profile the later-family instructions MOVEC, MOVES, RTD, and BKPT raise illegal-instruction exceptions, matching real hardware.
Set the program counter.
| cpu | CPU context. |
| pc | New program counter value. |
| void m68k_set_pc_changed_callback | ( | M68kCpu * | cpu, |
| M68kPcChangedCallback | callback ) |
Install PC-changed callback.
| cpu | CPU context. |
| callback | Callback function pointer or NULL. |
| void m68k_set_read16_callback | ( | M68kCpu * | cpu, |
| M68kRead16Callback | callback ) |
Install host memory read callback (16-bit).
| cpu | CPU context. |
| callback | Callback function pointer or NULL. |
| void m68k_set_read32_callback | ( | M68kCpu * | cpu, |
| M68kRead32Callback | callback ) |
Install host memory read callback (32-bit).
| cpu | CPU context. |
| callback | Callback function pointer or NULL. |
| void m68k_set_read8_callback | ( | M68kCpu * | cpu, |
| M68kRead8Callback | callback ) |
Install host memory read callback (8-bit).
| cpu | CPU context. |
| callback | Callback function pointer or NULL. |
| void m68k_set_reset_callback | ( | M68kCpu * | cpu, |
| M68kResetCallback | callback ) |
Install reset callback.
| cpu | CPU context. |
| callback | Callback function pointer or NULL. |
Set the status register.
| cpu | CPU context. |
| new_sr | New status register value. |
| void m68k_set_tas_callback | ( | M68kCpu * | cpu, |
| M68kTasCallback | callback ) |
Install TAS callback.
| cpu | CPU context. |
| callback | Callback function pointer or NULL. |
| void m68k_set_wait_bus_callback | ( | M68kCpu * | cpu, |
| M68kWaitBusCallback | callback ) |
Install wait bus callback.
| cpu | CPU context. |
| callback | Callback function pointer or NULL. |
| void m68k_set_write16_callback | ( | M68kCpu * | cpu, |
| M68kWrite16Callback | callback ) |
Install host memory write callback (16-bit).
| cpu | CPU context. |
| callback | Callback function pointer or NULL. |
| void m68k_set_write32_callback | ( | M68kCpu * | cpu, |
| M68kWrite32Callback | callback ) |
Install host memory write callback (32-bit).
| cpu | CPU context. |
| callback | Callback function pointer or NULL. |
| void m68k_set_write8_callback | ( | M68kCpu * | cpu, |
| M68kWrite8Callback | callback ) |
Install host memory write callback (8-bit).
| cpu | CPU context. |
| callback | Callback function pointer or NULL. |
| void m68k_step | ( | M68kCpu * | cpu | ) |
Execute one instruction step.
| cpu | CPU context. |
| void m68k_step_ex | ( | M68kCpu * | cpu, |
| bool | check_exceptions ) |
Execute one instruction with optional interrupt and trace checks.
| cpu | CPU context. |
| check_exceptions | If true, check interrupts and deliver trace exceptions. Instruction-generated exceptions are processed regardless of this flag. |
Write one word to emulated memory.
| cpu | CPU context. |
| address | Emulated address. |
| value | Value to write. |
Write one longword to emulated memory.
| cpu | CPU context. |
| address | Emulated address. |
| value | Value to write. |