Rocket 68
A Motorola 68000 CPU emulator in C
Loading...
Searching...
No Matches
m68k.c File Reference

Core CPU state, memory access, execution, and exception handling. More...

#include <stdio.h>
#include <string.h>
#include "m68k_internal.h"

Macros

#define AERR_INSTRUCTION_NO   0x08u
#define AERR_MODE_READ   0x10u
#define M68K_STATE_VERSION   1u

Enumerations

enum  { MEMORY_CALLBACK_IDLE , MEMORY_CALLBACK_ACTIVE , MEMORY_CALLBACK_FAILED }
enum  {
  M68K_TAG_DREGS = 1 , M68K_TAG_AREGS = 2 , M68K_TAG_PC = 3 , M68K_TAG_SR = 4 ,
  M68K_TAG_USP = 5 , M68K_TAG_SSP = 6 , M68K_TAG_VBR = 7 , M68K_TAG_SFC = 8 ,
  M68K_TAG_DFC = 9 , M68K_TAG_FLAGS = 10 , M68K_TAG_DECODE = 11 , M68K_TAG_CYCLES = 12 ,
  M68K_TAG_MODEL = 13
}

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_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_illegal_ea (M68kCpu *cpu)
void m68k_raise_odd_target_fault (M68kCpu *cpu, u32 target, u32 fault_pc)
bool m68k_is_unmapped_target (M68kCpu *cpu, u32 target)
void m68k_raise_bus_target_fault (M68kCpu *cpu, u32 target, u32 fault_pc)
void m68k_raise_bus_error (M68kCpu *cpu)
 Report a bus error from the current host memory callback.
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.
u16 m68k_fetch (M68kCpu *cpu)
u32 fetch_extension (M68kCpu *cpu)
u32 m68k_read_size (M68kCpu *cpu, u32 address, M68kSize size)
void m68k_write_size (M68kCpu *cpu, u32 address, u32 value, M68kSize size)
M68kEA m68k_calc_ea (M68kCpu *cpu, int mode, int reg, M68kSize size)
M68kEA m68k_calc_ea_addr (M68kCpu *cpu, int mode, int reg, M68kSize size)
int m68k_control_ea_cycles (int mode, int reg, bool is_jmp)
M68kEA m68k_calc_ea_ctl (M68kCpu *cpu, int mode, int reg, bool is_jmp)
M68kEA m68k_calc_ea_addr_nocost (M68kCpu *cpu, int mode, int reg, M68kSize size)
int m68k_movem_ea_cycles (int mode, int reg, bool mem_to_reg)
void update_flags_add (M68kCpu *cpu, u32 src, u32 dest, u32 result, M68kSize size)
void update_flags_sub (M68kCpu *cpu, u32 src, u32 dest, u32 result, M68kSize size)
void update_flags_cmp (M68kCpu *cpu, u32 src, u32 dest, u32 result, M68kSize size)
void update_flags_logic (M68kCpu *cpu, u32 result, M68kSize size)
void m68k_push_32 (M68kCpu *cpu, u32 value)
u32 m68k_pop_32 (M68kCpu *cpu)
void m68k_push_16 (M68kCpu *cpu, u16 value)
u16 m68k_pop_16 (M68kCpu *cpu)
void m68k_set_sr (M68kCpu *cpu, u16 new_sr)
 Set the status register.
void m68k_exception (M68kCpu *cpu, int vector)
void m68k_set_irq (M68kCpu *cpu, int level)
 Set the asserted interrupt level (the virtual IPL lines).
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_requested)
 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.
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.

Detailed Description

Core CPU state, memory access, execution, and exception handling.

Macro Definition Documentation

◆ AERR_INSTRUCTION_NO

#define AERR_INSTRUCTION_NO   0x08u

◆ AERR_MODE_READ

#define AERR_MODE_READ   0x10u

◆ M68K_STATE_VERSION

#define M68K_STATE_VERSION   1u

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
MEMORY_CALLBACK_IDLE 
MEMORY_CALLBACK_ACTIVE 
MEMORY_CALLBACK_FAILED 

◆ anonymous enum

anonymous enum
Enumerator
M68K_TAG_DREGS 
M68K_TAG_AREGS 
M68K_TAG_PC 
M68K_TAG_SR 
M68K_TAG_USP 
M68K_TAG_SSP 
M68K_TAG_VBR 
M68K_TAG_SFC 
M68K_TAG_DFC 
M68K_TAG_FLAGS 
M68K_TAG_DECODE 
M68K_TAG_CYCLES 
M68K_TAG_MODEL 

Function Documentation

◆ fetch_extension()

u32 fetch_extension ( M68kCpu * cpu)

◆ m68k_calc_ea()

M68kEA m68k_calc_ea ( M68kCpu * cpu,
int mode,
int reg,
M68kSize size )

◆ m68k_calc_ea_addr()

M68kEA m68k_calc_ea_addr ( M68kCpu * cpu,
int mode,
int reg,
M68kSize size )

◆ m68k_calc_ea_addr_nocost()

M68kEA m68k_calc_ea_addr_nocost ( M68kCpu * cpu,
int mode,
int reg,
M68kSize size )

◆ m68k_calc_ea_ctl()

M68kEA m68k_calc_ea_ctl ( M68kCpu * cpu,
int mode,
int reg,
bool is_jmp )

◆ m68k_context_size()

unsigned int m68k_context_size ( void )

Get serialized context size.

Returns
Number of bytes required for context copy buffer.

◆ m68k_control_ea_cycles()

int m68k_control_ea_cycles ( int mode,
int reg,
bool is_jmp )

◆ m68k_cycles_remaining()

int m68k_cycles_remaining ( M68kCpu * cpu)

Get remaining cycles in the current timeslice.

Parameters
cpuCPU context.
Returns
Remaining cycles.

◆ m68k_cycles_run()

int m68k_cycles_run ( M68kCpu * cpu)

Get cycles consumed in the active timeslice accounting model.

Parameters
cpuCPU context.
Returns
Consumed cycle count.

◆ m68k_deserialize()

bool m68k_deserialize ( M68kCpu * cpu,
const u8 * buffer,
size_t length )

Restore architectural CPU state from a portable save state.

Parameters
cpuCPU context; its memory binding and callbacks are kept.
bufferSerialized state produced by m68k_serialize.
lengthLength of the serialized state in bytes.
Returns
true on success; false when the data is malformed, truncated, or has an unsupported version.

Fields with unknown tags are skipped, so states written by newer library versions restore their known fields.

◆ m68k_end_timeslice()

void m68k_end_timeslice ( M68kCpu * cpu)

End current timeslice immediately.

Parameters
cpuCPU context.

◆ m68k_exception()

void m68k_exception ( M68kCpu * cpu,
int vector )

◆ m68k_execute()

int m68k_execute ( M68kCpu * cpu,
int cycles )

Execute instructions for a cycle budget.

Parameters
cpuCPU context.
cyclesRequested number of cycles to add to the current timeslice.
Returns
Number of cycles consumed by this call.

◆ m68k_fetch()

u16 m68k_fetch ( M68kCpu * cpu)

◆ m68k_get_ar()

u32 m68k_get_ar ( M68kCpu * cpu,
int reg )

Get address register value.

Parameters
cpuCPU context.
regAddress register index [0,7].
Returns
Register value, or 0 for invalid index.

◆ m68k_get_context()

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.

Parameters
cpuCPU context.
dstDestination buffer with at least m68k_context_size bytes.

◆ m68k_get_dr()

u32 m68k_get_dr ( M68kCpu * cpu,
int reg )

Get data register value.

Parameters
cpuCPU context.
regData register index [0,7].
Returns
Register value, or 0 for invalid index.

◆ m68k_get_model()

M68kModel m68k_get_model ( M68kCpu * cpu)

Read the selected CPU model profile.

Parameters
cpuCPU context.
Returns
Current model profile.

◆ m68k_get_pc()

u32 m68k_get_pc ( M68kCpu * cpu)

Get the current program counter.

Parameters
cpuCPU context.
Returns
Current PC.

◆ m68k_init()

void m68k_init ( M68kCpu * cpu,
u8 * memory,
u32 memory_size )

Initialize a CPU context and bind memory.

Parameters
cpuCPU context to initialize.
memoryPointer to emulated memory.
memory_sizeSize of memory in bytes.

◆ m68k_is_unmapped_target()

bool m68k_is_unmapped_target ( M68kCpu * cpu,
u32 target )

◆ m68k_modify_timeslice()

void m68k_modify_timeslice ( M68kCpu * cpu,
int cycles )

Add cycles to current timeslice.

Parameters
cpuCPU context.
cyclesNumber of cycles to add.

◆ m68k_movem_ea_cycles()

int m68k_movem_ea_cycles ( int mode,
int reg,
bool mem_to_reg )

◆ m68k_pop_16()

u16 m68k_pop_16 ( M68kCpu * cpu)

◆ m68k_pop_32()

u32 m68k_pop_32 ( M68kCpu * cpu)

◆ m68k_push_16()

void m68k_push_16 ( M68kCpu * cpu,
u16 value )

◆ m68k_push_32()

void m68k_push_32 ( M68kCpu * cpu,
u32 value )

◆ m68k_raise_bus_error()

void m68k_raise_bus_error ( M68kCpu * cpu)

Report a bus error from the current host memory callback.

Parameters
cpuCPU 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.

◆ m68k_raise_bus_target_fault()

void m68k_raise_bus_target_fault ( M68kCpu * cpu,
u32 target,
u32 fault_pc )

◆ m68k_raise_illegal_ea()

void m68k_raise_illegal_ea ( M68kCpu * cpu)

◆ m68k_raise_odd_target_fault()

void m68k_raise_odd_target_fault ( M68kCpu * cpu,
u32 target,
u32 fault_pc )

◆ m68k_read_16()

u16 m68k_read_16 ( M68kCpu * cpu,
u32 address )

Read one word from emulated memory.

Parameters
cpuCPU context.
addressEmulated address.
Returns
Read value.

◆ m68k_read_32()

u32 m68k_read_32 ( M68kCpu * cpu,
u32 address )

Read one longword from emulated memory.

Parameters
cpuCPU context.
addressEmulated address.
Returns
Read value.

◆ m68k_read_8()

u8 m68k_read_8 ( M68kCpu * cpu,
u32 address )

Read one byte from emulated memory.

Parameters
cpuCPU context.
addressEmulated address.
Returns
Read value.

◆ m68k_read_size()

u32 m68k_read_size ( M68kCpu * cpu,
u32 address,
M68kSize size )

◆ m68k_reset()

void m68k_reset ( M68kCpu * cpu)

Reset CPU state and load initial vectors from memory.

Parameters
cpuCPU context.

◆ m68k_serialize()

size_t m68k_serialize ( const M68kCpu * cpu,
u8 * buffer,
size_t capacity )

Serialize architectural CPU state into a portable save state.

Parameters
cpuCPU context.
bufferOutput buffer, or NULL to query the required size.
capacitySize of the output buffer in bytes.
Returns
Number of bytes written, the required size when buffer is NULL, or 0 when the buffer is too small.

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.

◆ m68k_set_ar()

void m68k_set_ar ( M68kCpu * cpu,
int reg,
u32 value )

Set address register value.

Parameters
cpuCPU context.
regAddress register index [0,7].
valueNew 32-bit value.

◆ m68k_set_context()

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.

Parameters
cpuCPU context.
srcSource buffer with at least m68k_context_size bytes.

◆ m68k_set_dr()

void m68k_set_dr ( M68kCpu * cpu,
int reg,
u32 value )

Set data register value.

Parameters
cpuCPU context.
regData register index [0,7].
valueNew 32-bit value.

◆ m68k_set_fc_callback()

void m68k_set_fc_callback ( M68kCpu * cpu,
M68kFcCallback callback )

Install function code callback.

Parameters
cpuCPU context.
callbackCallback function pointer or NULL.

The callback is invoked before memory/program accesses with the active function code.

◆ m68k_set_illg_callback()

void m68k_set_illg_callback ( M68kCpu * cpu,
M68kIllgCallback callback )

Install illegal opcode callback.

Parameters
cpuCPU context.
callbackCallback function pointer or NULL.

◆ m68k_set_instr_hook_callback()

void m68k_set_instr_hook_callback ( M68kCpu * cpu,
M68kInstrHookCallback callback )

Install instruction hook callback.

Parameters
cpuCPU context.
callbackCallback function pointer or NULL.

◆ m68k_set_int_ack_callback()

void m68k_set_int_ack_callback ( M68kCpu * cpu,
M68kIntAckCallback callback )

Install interrupt acknowledge callback.

Parameters
cpuCPU context.
callbackCallback function pointer or NULL.

◆ m68k_set_irq()

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.

Parameters
cpuCPU context.
levelInterrupt level in range [0,7].

◆ m68k_set_model()

void m68k_set_model ( M68kCpu * cpu,
M68kModel model )

Select the CPU model profile.

Parameters
cpuCPU context.
modelModel 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.

◆ m68k_set_pc()

void m68k_set_pc ( M68kCpu * cpu,
u32 pc )

Set the program counter.

Parameters
cpuCPU context.
pcNew program counter value.

◆ m68k_set_pc_changed_callback()

void m68k_set_pc_changed_callback ( M68kCpu * cpu,
M68kPcChangedCallback callback )

Install PC-changed callback.

Parameters
cpuCPU context.
callbackCallback function pointer or NULL.

◆ m68k_set_read16_callback()

void m68k_set_read16_callback ( M68kCpu * cpu,
M68kRead16Callback callback )

Install host memory read callback (16-bit).

Parameters
cpuCPU context.
callbackCallback function pointer or NULL.

◆ m68k_set_read32_callback()

void m68k_set_read32_callback ( M68kCpu * cpu,
M68kRead32Callback callback )

Install host memory read callback (32-bit).

Parameters
cpuCPU context.
callbackCallback function pointer or NULL.

◆ m68k_set_read8_callback()

void m68k_set_read8_callback ( M68kCpu * cpu,
M68kRead8Callback callback )

Install host memory read callback (8-bit).

Parameters
cpuCPU context.
callbackCallback function pointer or NULL.

◆ m68k_set_reset_callback()

void m68k_set_reset_callback ( M68kCpu * cpu,
M68kResetCallback callback )

Install reset callback.

Parameters
cpuCPU context.
callbackCallback function pointer or NULL.

◆ m68k_set_sr()

void m68k_set_sr ( M68kCpu * cpu,
u16 new_sr )

Set the status register.

Parameters
cpuCPU context.
new_srNew status register value.

◆ m68k_set_tas_callback()

void m68k_set_tas_callback ( M68kCpu * cpu,
M68kTasCallback callback )

Install TAS callback.

Parameters
cpuCPU context.
callbackCallback function pointer or NULL.

◆ m68k_set_wait_bus_callback()

void m68k_set_wait_bus_callback ( M68kCpu * cpu,
M68kWaitBusCallback callback )

Install wait bus callback.

Parameters
cpuCPU context.
callbackCallback function pointer or NULL.

◆ m68k_set_write16_callback()

void m68k_set_write16_callback ( M68kCpu * cpu,
M68kWrite16Callback callback )

Install host memory write callback (16-bit).

Parameters
cpuCPU context.
callbackCallback function pointer or NULL.

◆ m68k_set_write32_callback()

void m68k_set_write32_callback ( M68kCpu * cpu,
M68kWrite32Callback callback )

Install host memory write callback (32-bit).

Parameters
cpuCPU context.
callbackCallback function pointer or NULL.

◆ m68k_set_write8_callback()

void m68k_set_write8_callback ( M68kCpu * cpu,
M68kWrite8Callback callback )

Install host memory write callback (8-bit).

Parameters
cpuCPU context.
callbackCallback function pointer or NULL.

◆ m68k_step()

void m68k_step ( M68kCpu * cpu)

Execute one instruction step.

Parameters
cpuCPU context.

◆ m68k_step_ex()

void m68k_step_ex ( M68kCpu * cpu,
bool check_exceptions )

Execute one instruction with optional interrupt and trace checks.

Parameters
cpuCPU context.
check_exceptionsIf true, check interrupts and deliver trace exceptions. Instruction-generated exceptions are processed regardless of this flag.

◆ m68k_write_16()

void m68k_write_16 ( M68kCpu * cpu,
u32 address,
u16 value )

Write one word to emulated memory.

Parameters
cpuCPU context.
addressEmulated address.
valueValue to write.

◆ m68k_write_32()

void m68k_write_32 ( M68kCpu * cpu,
u32 address,
u32 value )

Write one longword to emulated memory.

Parameters
cpuCPU context.
addressEmulated address.
valueValue to write.

◆ m68k_write_8()

void m68k_write_8 ( M68kCpu * cpu,
u32 address,
u8 value )

Write one byte to emulated memory.

Parameters
cpuCPU context.
addressEmulated address.
valueValue to write.

◆ m68k_write_size()

void m68k_write_size ( M68kCpu * cpu,
u32 address,
u32 value,
M68kSize size )

◆ update_flags_add()

void update_flags_add ( M68kCpu * cpu,
u32 src,
u32 dest,
u32 result,
M68kSize size )

◆ update_flags_cmp()

void update_flags_cmp ( M68kCpu * cpu,
u32 src,
u32 dest,
u32 result,
M68kSize size )

◆ update_flags_logic()

void update_flags_logic ( M68kCpu * cpu,
u32 result,
M68kSize size )

◆ update_flags_sub()

void update_flags_sub ( M68kCpu * cpu,
u32 src,
u32 dest,
u32 result,
M68kSize size )