Emulator Core Architecture, Hot-Reload ABI & 3-Tier Acceleration
This document is the engineering reference for the from-scratch C11 Ivory CPU core, memory subsystem, Weitek W3x64 FPA, hot-reloadable libivory_core shared-library boundary, and three-tier execution engine (portable, avx512_c, avx512) under emu/.
For peripheral hardware models (Merlin-II motherboard, WD33C93A SCSI, SCN68562 DUSCC, MK48T02 NVRAM, Intel 82586 Ethernet) see Peripherals & Backends; for the SDL console, s7 scripting, and debug IPC see GUI, Scripting & Debug.
1. Core Module Map (emu/)
Every file in libivory_core is portable C11 (or OS-independent x86-64 GAS assembly) compiled with -std=c11 -Wall -Wextra -Werror:
| File | Responsibility |
|---|---|
emu/include/ivory_hw.h | Architectural 40-bit IvoryWord, opcodes, trap vectors, Rev 4 microstates, IvoryPhtEntry, ivory_pht_hash(), Merlin-II PMA constants (synced 1:1 with docs/hardware-reference/ivory_hw.h) |
emu/include/ivory_emu.h | Frozen page-aligned state structs (IvoryCpu, IvoryMem, MerlinBoard, IvoryDcache, IvoryDecodedSlot) and internal APIs |
emu/include/ivory_core_api.h | Hot-reload function-table contract (IVORY_CORE_ABI_VERSION = 4) |
emu/include/ivory_asm_offsets.h | Single source of truth for struct field offsets shared by C (_Static_assert verified in cpu.c) and cpu_zen4_loop.S |
emu/src/asm_abi.h | OS-independent x86-64 GAS macros (ABI_FUNC_BEGIN, SysV CFI + Win64 .seh_* .xdata/.pdata, universal callee-saved GPR mapping) |
emu/src/core_api.c | Exports read-only k_ivory_core_api via sole dynamic symbol ivory_core_get_api() |
emu/src/core_loader.c | Host shell dlopen/LoadLibrary loader with temp-copy hot reload and ABI validation |
emu/src/mem.c | Zone 31 / PHT / DTP_PHYSICAL_ADDRESS translation, Table-13 cycle LUTs, Map Cache, map_epoch, dirty bitmaps, hardware watchpoints |
emu/src/cpu.c | Reset/init, 128-word stack cache, internal registers #o000..#o1077, pre/post-trap dispatcher, 64-entry L0 I-cache, 256-µs timer batching, direct-threaded Tier 0 runner (cpu_run_threaded), Tier 2 slow-path trampoline |
emu/src/cpu_decode.c | Pure leaf decoded instruction page cache (IvoryDcache: 256 pages, 128×2-way, 1 KiB tag directory) and scalar 256-word page decoder |
emu/src/isa_call.c | Fullword & packed StartCall, FinishCall, Entry (#o176/#o177), Return*, Catch*, BindLocative* |
emu/src/isa_stack.c | Stack ops, branches, loops, TYPE-MEMBER, predicates (incl. -NO-POP trap pop rule), STACK-BLT |
emu/src/isa_arith.c | Fixnum, single-float FPA fast path, bignum step, ALU & rotator, LDB/DPB |
emu/src/isa_lisp.c | CAR/CDR/ENDP, RPLACA/RPLACD, MEMBER/ASSOC/RGETF, arrays, lexical/instance vars, logic ops, generic/message dispatch |
emu/src/isa_sys.c | BLOCK-0..3-*, READ/WRITE-INTERNAL-REGISTER, MEMORY-READ/WRITE, STORE-CONDITIONAL, COPROCESSOR-READ/WRITE, HALT |
emu/src/fpa_w3x64.c | Complete Weitek W3164/W3364 IEEE-754 single/double FPA coprocessor (SR0..SR11, rounding modes, status nibbles, wrapped underflow) |
emu/src/cpu_zen4_avx512.c | Isolated -march=znver4 -fno-lto TU: stateless CPUID/XGETBV ivory_host_select_tier(), 4-ZMM VBMI page decoder, 4-insn vpshufbitqmb PHT hash, 64B 4-way PHT bucket probe |
emu/src/cpu_zen4_loop.S | OS-independent Zen 4 x86-64 threaded hot execution loop (~45 inlined opcodes, 64-entry L0 decoded-page switcher, single C slow-path trampoline) |
2. Hot-Reloadable libivory_core ABI (v4)
The thin host shell (ivory-xl1200: SDL console, s7 scripting, debug IPC, snapshot persistence, media loaders) owns all guest state in heap/mmap buffers and invokes the core exclusively through const IvoryCoreApi *api returned by ivory_core_get_api(). This allows live rebuilding and hot-reloading of libivory_core.so (Linux) or libivory_core.dll (Windows) mid-session without rebooting Genera.
ABI Invariants (_Static_assert Enforced)
- ABI Version:
IVORY_CORE_ABI_VERSION == 4U. - Frozen Page-Aligned State Sizes:
sizeof(IvoryCpu) == 4096U(4 KiB)sizeof(IvoryMem) == 32768U(32 KiB)sizeof(MerlinBoard) == 278528U(272 KiB)sizeof(IvoryDecodedSlot) == 8U
- Stateless Core DSO: Zero writable
.dataand zero.bssinsidelibivory_core.so, sole dynamic exportivory_core_get_api(-fvisibility=hidden), and non-executable stack (GNU_STACK RW,.section .note.GNU-stack,"",@progbitsincpu_zen4_loop.S). - Automatic Reload Coherence (
core_cookie):IvoryDcachestorescore_cookie = (uintptr_t)&ivory_dcache_flush(a function pointer inside the active DSO). On the first execution after adlopenhot reload, the address mismatch automatically flushesIvoryDcacheand re-selectsaccel_tierwithout requiring explicit shell hooks.
3. Fast-Path Execution Primitives
3.1 Decoded Instruction Page Cache (IvoryDcache, cpu_decode.c)
Instead of decoding 40-bit instruction words (CDR sequence codes, Even/Odd halfword extraction, fullword classification, opcode validation) on every instruction:
- Geometry: 256 decoded pages (~1.06 MiB total, fitting in L2/L3 cache) organized as 128 sets × 2 ways with 2-way LRU replacement.
- Full 4 GW Scaling in 1 KiB: Rather than an 8 MW-only bitmap,
IvoryDcachemaintains a fixed 1 KiB L1d-resident tag directory (uint64_t ppn_pair[128]), folding all 24 PPN bits ((ppn ^ (ppn >> 7) ^ (ppn >> 14) ^ (ppn >> 21)) & 127). Both physical RAM PPNs (0..2^24-1) and Boot PROM pages (IVORY_DCACHE_PROM_SENTINEL | page) share the same O(1) lookup with zero resizing up to 4 GigaWords. - 516-Slot Strided Page Mirror: Each 256-word page decodes into 512 halfword slots (
slot = ((vma & 0xFF) << 1) | is_odd) bracketed byHANDLER_PAGE_EXITsentinels at slot-1(raw_slots[0]) and slots512..514(raw_slots[513..515]). Every 8-byteIvoryDecodedSlotholds{ uint16_t handler_id, int8_t stride, uint8_t aux, uint32_t imm }— using reload-safe integer IDs (0..511) rather than raw code pointers. - O(1) Write Coherence: Every RAM write invokes
ivory_mem_note_ram_write_pma()->ivory_dcache_invalidate_ppn()in a single cache-line check ofppn_pair[set], keeping self-modifying code, world loads, and Lisp compiler output bit-exact.
3.2 64-Entry L0 I-Cache, Table-13 LUTs & Event-Horizon Timer Batching
- 64-Entry L0 Decoded-Page Cache: Direct-mapped by
vpn & 63, caching(vpn, map_epoch, decoded_page *)so inter-page branches, calls, and returns stay inside the hot loop without re-enteringmem.c. Any Map Cache mutation bumpsmem->map_epochfor instant O(1) invalidation. - Table-13 LUTs &
ivory_fast_data_read: Compile-time lookup tables inmem.cclassify forwarding/trap behavior by(cycle, dtp)in one load;ivory_fast_data_read()inlines non-forwarding, non-oldspace Zone 31 RAM reads directly. - Capped 256-µs Event-Horizon Timer Batching: Rather than calling
merlin_tick()every 4 instructions,ivory_cpu_run()queriesmerlin_us_until_next_timer_irq()and computes a capped batch budget (at most 256 µs = 1,024 instructions in deterministic mode), ticking timers cleanly before the next scheduled interrupt or on any clock/I/O-register access (%REGISTER-MICROSECOND-CLOCK,COPROCESSOR-READ #o1002). Interrupt delivery remains 100% instruction-exact.
4. Three-Tier Runtime Acceleration (ISA_MODE & IVORY_ACCEL)
Build-Time (ISA_MODE) and Runtime (IVORY_ACCEL) Controls
| Knob | Values | Behavior |
|---|---|---|
make -C emu ISA_MODE=... | runtime (default), portable, native | runtime compiles baseline portable C11 plus isolated Zen 4 AVX-512 C & ASM units selected dynamically via CPUID/XGETBV; portable builds pure C11 only (any 64-bit LE arch); native adds -march=native |
IVORY_ACCEL=... (env var) | auto (default), portable, avx512_c, avx512 | Overrides runtime tier selection at startup (useful for lockstep A/B verification and benchmarking) |
Tier Breakdown
- Tier 0 —
IVORY_ACCEL_PORTABLE64(cpu.c,cpu_decode.c):- Pure C11/GNU C direct-threaded loop (
cpu_run_threaded(),goto *k_dispatch[slot->handler_id]) withIVORY_NOINLINEcold helpers to preserve register allocation across hot handlers. - Runs on every 64-bit little-endian CPU & OS (
x86_64,aarch64,riscv64, Linux, Windows, macOS/BSD).
- Pure C11/GNU C direct-threaded loop (
- Tier 1 —
IVORY_ACCEL_AVX512_C(cpu_zen4_avx512.c):- Compiled in an isolated
-march=znver4 -fno-ltotranslation unit (preventing AVX-512 codegen from leaking into baseline startup code). ivory_host_select_tier()checksCPUID+XGETBV(OSXSAVE, ZMM state0xE6,AVX512F/DQ/BW/VL/VBMI/VBMI2/BITALG,BMI1/2) strictly inside baseline code.- Provides 4-ZMM VBMI 256-word page decoding (
ivory_decode_page_avx512()), 4-instructionvpshufbitqmbPHT hashing (ivory_pht_hash_avx512()), and 64-byte single-ZMM 4-way PHT bucket probing (ivory_pht_probe_bucket_avx512()).
- Compiled in an isolated
- Tier 2 —
IVORY_ACCEL_AVX512_ASM(cpu_zen4_loop.S+asm_abi.h+ivory_asm_offsets.h):- OS-independent x86-64 GAS assembly targeting both Linux System V AMD64 (
.cfi_*) and Microsoft Windows x64 (_WIN64.seh_*.xdata/.pdata) from a single source file. - Uses universal callee-saved GPRs (
%rbx,%rbp,%r12..%r15) and zeroXMMregisters (no Win64 XMM6–XMM15 save/restore overhead). - Uses a relocation-free 2 KiB 32-bit PC-relative jump table (
512 × int32_t, PIC-safe insidelibivory_core.so/.dll). - Maintains an in-loop 64-entry L0 decoded-page switcher with O(1) generation-counter (
l0_gen) invalidation so cross-page branches, calls, and returns stay in assembly. - Inlines ~45 hottest opcodes (stack movement, branches, loops,
CAR/CDR, Zone-31EVCP,ADD/SUB/LOG*,TYPE-MEMBER,EQ/EQL/EQUAL-NUMBER/LESSP/GREATERP,LDB,PUSH/POP/MOVEM, the full Call/Entry/Return quartet, and exact pre-instructionOP_COPROCESSOR_READ#o1002clock reads), falling back to a single C slow-path trampoline for complex/trapping ops.
- OS-independent x86-64 GAS assembly targeting both Linux System V AMD64 (
5. Verified Benchmarks & Bit-Exact Lockstep
Measured on AMD Ryzen 9 7940HS (Zen 4) booting genuine 128 KiB fep330/rom.dump deterministically for 45,000,000 instructions (reaching FEP> prompt):
Tier (IVORY_ACCEL) | Throughput (45M fep330 Boot) | Relative Speedup | Lockstep vs portable |
|---|---|---|---|
portable (Tier 0, GNU C threaded) | ~64.9 – 73.5 MIPS | 1.00× | Baseline |
avx512_c (Tier 1, C threaded + AVX-512 decode/PHT) | ~62.2 – 70.5 MIPS | ~0.96× (decode-cold workload) | 100% bit-exact |
avx512 (Tier 2, Zen 4 ASM + AVX-512 decode/PHT) | 149.7 – 151.4 MIPS | 2.31× | 100% bit-exact |
- Lockstep Verification:
emu/tests/test_cpu_core.c: Verifies scalar vs. AVX-512 page decode, PHT hash, PHT bucket probe, and exact Tier 0 vs. Tier 1 vs. Tier 2 execution parity.emu/tests/test_snapshot_reload.c: Verifies 10M-step + 45M-step bit-exact register/memory/cycle parity across all tiers including mid-rundlopenhot-reload oflibivory_core.so(passing natively on Linux and via MinGW-w64 cross-compile under Wine 11).
6. Build, Override & Test Commands
# Default build (ISA_MODE=runtime: portable baseline + dynamic Zen 4 AVX-512)
nix develop --command make -C emu -j all
# Pure portable C11 build (zero x86 intrinsics/assembly)
nix develop --command make -C emu -j ISA_MODE=portable all
# Run unit tests (exit 77 = SKIP for ROM-dependent checks when ROMs are absent)
nix develop --command make -C emu test
# Force a specific execution tier at runtime
IVORY_ACCEL=portable ./emu/build/ivory-xl1200 --headless --rom 115988-e.bin --rom-hi 115989-e.bin
IVORY_ACCEL=avx512 ./emu/build/ivory-xl1200 --headless --rom 115988-e.bin --rom-hi 115989-e.bin
- Intel 82586 —
intel_82586_lan_coprocessor.pdf(mirror) - Intel 82586 —
intel_82586_reference_manual_1983.pdf(mirror) - Signetics SCN68562 / SCN26562 —
signetics_1991_IC019_data_comm_SCN68562.pdf(mirror) - Mostek MK48T02 / ST M48T02 —
st_M48T02_MK48T02_timekeeper.pdf - Western Digital WD33C93A —
wd_WD33C93A_datasheet_and_appnotes_1990.pdf(mirror) - Western Digital WD33C93A —
wd_WD33C93A_scsi_datasheet.pdf(mirror) - Weitek WTL1164/1165 (W3x64 family) —
weitek_WTL1164_1165_64bit_ieee_1986.pdf(mirror) - Weitek WTL3132/3332 (W3x64 family) —
weitek_WTL3132_3332_fpdp_1988.pdf(mirror) - Weitek WTL3167 (W3x64 core) —
weitek_WTL3167_coprocessor_1988.pdf(mirror)