C11 Emulator Peripherals & Host Backends
This document describes the from-scratch ISO C11 implementation of the Symbolics XL1200 / Merlin-II motherboard peripherals, host I/O backends, firmware loaders, and disk/tape tooling in emu/.
- Architectural Context: See
docs/emulator/README.mdfor overall emulator layout,docs/emulator/core-and-acceleration.mdfor the CPU/MMU/direct-threaded & ASM core, anddocs/emulator/gui-scripting-and-debug.mdfor the SDL3 GUI, s7 scripting, and debug IPC. - Hardware Specifications: See Hardware Reference Volume 4 (Merlin-II core, PIT, interrupts, VMEbus) and Volume 5 (XL1200 peripherals).
1. Core vs. Shell Boundary
To keep libivory_core.so hot-reloadable and portable across Linux, macOS, and Windows (MinGW-w64), the peripheral stack enforces a strict split between pure ISO C11 core devices (compiled into libivory_core.so) and OS-specific host backends & loaders (compiled into the ivory-xl1200 shell and wired via callback structs):
| Layer | Source Files | Responsibility |
|---|---|---|
Core Devices (libivory_core.so) | merlin.c, nvram_mk48t02.c, scsi_wd33c93.c, eth_82586.c, console_hw.c | Bus decode, 82C54 PIT, MK48T02 NVRAM/TOD, WD33C93A SCSI state machine + streaming 64-bit disk/CD/tape I/O, Intel 82586 DMA/descriptor engine, DUSCC + 8237A + VRAM + SLB protocol. Zero platform networking or GUI headers. |
Host Backends & Loader (ivory-xl1200 shell) | eth_slirp.c, eth_tap_linux.c, serial_host.c, loader.c | User-mode libslirp NAT, Linux /dev/net/tun TAP & AF_PACKET raw Ethernet, portable 2-channel serial transports (TCP listen/connect, POSIX AF_UNIX / Win32 Named Pipe, TX capture file), and IFEP promJ split/combined Boot PROM loading. |
| Standalone Media Tool | fep_convert.c (ivory_fep_tools.h) | ivory-fep-convert CLI & shared library functions for MacIvory→XL1200 FEPFS conversion, blank/FEPFS disk creation, SIMH .tap creation, and image inspection. |
2. Merlin-II Motherboard Hub (emu/src/merlin.c)
MerlinBoard (declared in emu/include/ivory_emu.h) owns the C-Bus core registers, 82C54 PIT, MK48T02 NVRAM, I/O Board 0 64-KWord DMA SRAM, WD33C93A SCSI controller, Intel 82586 Ethernet controller, DP8466 ESDI stub, and VIC068/VMEbus register storage, and dispatches console/serial/VRAM traffic to Xl1200Console.
2.1 Bus Dispatch & Lifecycle API
merlin_init/merlin_reset: Resets all sub-devices, initializesMERLIN_CACHE_CTRL_PMA(0x0A000004) to2(enabled),MERLIN_VME_TIMEOUT_PMA(0x0A000008,board->vme_timeout, VME Timeout / Board ECO register) toMERLIN_ECO_DEFAULT(0x0FF4FF11), builds the 32-byte I/O Board 0 ID ROM (with rotated-byte checksum), populates MK48T02 cookies & MAC, and clears pending interrupts.merlin_set_identity(MerlinBoard *, const MerlinIdentityConfig *): Configures board serial number, Ethernet MAC address, monitor type (0= Philips,1= Sony), manufacturing date (days since1982-01-01), and Main/IO0 ECO nibbles, automatically regenerating the I/O Board 0 ID ROM checksum, MK48T02 MAC copy (+0x20), andeth.ia_addr.merlin_io_read/merlin_io_write: Routes all Merlin-II PMAs (0x08000000PIT,0x09000000MK48T02,0x0A000000..0x0A00000FC-Bus core,0x40000000..0x4000FFFFIO0 SRAM,0x44014000..0x4402D001IO0 MMIO,0x48000000..0x4807FFFFVRAM,0x80000000..0x8FF8000FVME/VIC068). ReturnsIVORY_ERR_BUS_FAULTfor unpopulated I/O Board 1 (0x50000000..0x5FFFFFFF) and unmapped MMIO addresses.
2.2 Performance & Idle-Sleep Optimizations
- Event-Driven DMA (
dma_service_pending):merlin_tick()skips SCSI and console 8237A DMA scans on ticks where neither controller has armed transfers, keeping hot-path tick overhead minimal. - Cached Interrupt State (
irq_active_cached): Recomputed only whenint_pendingorint_enablechanges, makingmerlin_irq_asserted()a single boolean check in the CPU loop. - Idle Timer Prediction (
merlin_us_until_next_timer_irq): Computes the exact microsecond horizon until the next enabled 82C54 Counter 2 (10 MHz), cascaded Counter 1→0 (1 MHz), or C-Bus interval timer IRQ, allowing the shell to sleep cleanly during Genera idle loops instead of busy-spinning. - Run-Light Activity Latch: Tracks
MERLIN_LEDS_PMA(0x0A000000) specifically onU1bit0x02(process activity, ignoring the 2 HzU0heartbeat bit0x01) for accurate GUI status-bar activity indication.
3. Mostek MK48T02 NVRAM & BCD TOD Clock (emu/src/nvram_mk48t02.c)
Mk48t02Nvram emulates the 2,048-byte battery-backed Timekeeper at 0x09000000..0x090007FF:
- NVRAM Persistence:
nvram_mk48t02_load_file()andnvram_mk48t02_save_file()load/persist the exact 2,048-byte image (MK48T02_SIZE_BYTES).nvram_mk48t02_init()seeds Merlin cookies0x55, 0xAA, 1, 16at+1..+4and the 6-byte Symbolics MAC (08:00:05:00:00:01default) at+0x20..+0x25. - BCD TOD Latching (
0x7F8..0x7FF): Implements the fullREAD<6>freeze latch (read_latch[8]) andWRITE<7>commit latch (write_latch[8]),STOP<7>oscillator halt in Seconds,START<7>in Hours, and a C11-portablecivil_utc_to_epoch()converter (avoiding non-standardtimegm()) that maintainstime_offset_secrelative to the host UTC clock so guest clock adjustments persist smoothly.
4. WD33C93A SCSI Controller & Streaming Media (emu/src/scsi_wd33c93.c)
Wd33c93Scsi implements a multi-target SCSI bus (targets 0..6, initiator ID 7) without ever loading whole disk images into RAM:
4.1 64-Bit Streaming I/O & Fast Ring DMA
- 64-Bit Positional I/O: Uses
emu/include/emu_io.h(pread/pwriteon POSIX; 64-bitOVERLAPPEDReadFile/WriteFileor_fseeki64on Windows) so multi-gigabyte FEPFS images (>4 GiB) work cleanly across Linux, macOS, and Windows. - 4-KWord Segment-Contiguous 32-Bit DMA Fast Loops: Transfers pack/unpack 4 bytes per
DTP-FIXNUMword in bulk across contiguous segments of the 32-KiB I/O Board 0 SRAM ring (0x40005000..0x40006FFF), checking wrap, trigger (0x4401B000), and overrun (0x4401A800) boundaries at segment granularity.
4.2 Target Attachment & Multi-Chunk Phase 0x45 Continuation
- Target APIs:
scsi_wd33c93_attach_disk(scsi, target_id, path, read_only)— 1,280-byte sector FEPFS/LMFS hard disks.scsi_wd33c93_attach_cdrom(scsi, target_id, path)— 2,048-byte sector read-only ISO-9660 images.scsi_wd33c93_attach_tape(scsi, target_id, path, read_only)— Variable-record SIMH.tapsequential tape drives.scsi_wd33c93_detach_target(scsi, target_id)— Live media detach/swap support.
- Multi-Chunk DMA Continuation: Transfers exceeding 32 KiB pause with
CMD_PHASE = 0x45andSCSI_STATUS = 0x19(Data-In) or0x18(Data-Out). The active SCSI target ID is latched inWD_REG_INT_ACTIVE_TARGET(0x1D, isolated from guest register writes). On reconnect,wd_try_continue_dma_phase()runs beforeDEST_IDtarget lookup, handling Genera'sscsi-transfer-reconnectzeroing ofDEST_ID<2:0>.
5. Intel 82586 Ethernet & Host Networking Backends
5.1 Core Controller (emu/src/eth_82586.c)
Eth82586 is a pure ISO C11 implementation (zero OS headers, zero mutable statics) driven by 0x44016000 <7:5> and sharing MerlinBoard.io0_sram:
- Full Descriptor Pipeline: Parses
SCP(default byte address0x01FFF4) →ISCP→SCB→CBL(NOP,IA-SETUP,CONFIGURE,MC-SETUP,TRANSMITwith chainedTBDs,TDR,DUMP170B,DIAGNOSE) andRFA(RFD/RBDchains). - Address Filtering & Loopback: Implements unicast (
IA), broadcast, 64-bit Ethernet CRC-32 multicast hash filtering, promiscuous mode, and internal/external loopback (ETH_82586_CTRL_INHIBIT_LOOPBACK+CONFIGUREbyte 3). - Host Callback Bridge (
EthHostCallbacks): All wire TX/RX goes exclusively throughon_tx_frame(ctx, frame, len)andpoll_rx_frame(ctx, dst, cap).
5.2 User-Mode NAT Backend (emu/src/eth_slirp.c)
- Wraps
libslirpinside the shell (compiled whenHAVE_LIBSLIRPis enabled), providing zero-privilege TCP/UDP/ICMP NAT networking for Genera (e.g., Chaos-over-IP, Telnet, FTP, DNS) without root or TAP configuration. - Portable across POSIX (
poll) and Windows/MinGW-w64 (Winsock2+WSAPoll, omittingPOLLPRIwhich triggersWSAEINVALon Windows). - Uses an
EmuMutex-guarded 64-slot RX ring (2,048bytes/frame) and up to 16libslirpmonotonic timers backed byemu_mono_ns().
5.3 Linux TAP & Raw AF_PACKET Backend (emu/src/eth_tap_linux.c)
eth_tap_linux_open_tap(ifname, ...): Opens/dev/net/tunwithIFF_TAP | IFF_NO_PIin non-blocking mode for bridged L2 host networking (ideal for native Chaosnet or local LAN access).eth_tap_linux_open_packet(ifname, ...): Opens a rawAF_PACKET/SOCK_RAW(ETH_P_ALL) socket bound to a host NIC, filtering outPACKET_OUTGOINGechoes on RX.- Compiled only on Linux (
#ifdef __linux__); cleanly stubbed on non-Linux hosts solibivory_coreremains 100% portable.
6. Console, DUSCC/8237A Serial & Host Serial Backends
6.1 Console Hardware (emu/src/console_hw.c)
Xl1200Console implements:
- Monochrome VRAM (
0x48000000..0x4807FFFF): 512 KWords (1152×896visible, 64-word stride, LSB-leftmost),VIDEO_VPOS(0x44024000),VIDEO_CTRL(0x4402C000, BOW/WOB polarity, enable, forced black, speaker bit), and Am8158 timing (0x4402D000..1). Default GUI overscan crop trims to1084×812at(34, 34). - Signetics SCN26562/68562 DUSCC (
0x44017000/7100) and Intel 8237A 4-Channel DMAC (0x44017200/7300), plusMERLIN_SERIAL_CTRL_PMA(0x44014800). - SLB Keyboard/Mouse Serializer:
slb_enqueue_wire_bytes()encodes 16-bit SLB logical events into authentic 1-byte (MOUSE-SWITCH,MOUSE-MOVE,SERIAL-WINDOW) and 2-byte (KEY-DOWN,KEY-UP,ALL-KEYS-UP,STATUS,SERIAL-IN) wire frames onto DUSCC Channel A RX / DMAC Channel 0. - Serial Host Callback Interface: Routes Channel A/B TX bytes, RX polling, and live modem status (
ICTSR<3:0>: DCD/CTS/DSR) throughXl1200SerialCallbacks(console_hw_set_serial_callbacks()).
6.2 Portable 2-Channel Host Serial Backend (emu/include/ivory_serial_host.h, emu/src/serial_host.c)
Xl1200SerialHost bridges console_hw.c's Xl1200SerialCallbacks (called from emu_thread) to Main-Thread non-blocking host I/O via lock-free 4,096-byte SPSC rings (Xl1200SerialRing tx_ring/rx_ring) and emu_ipc.h. Channel 0 (DUSCC Ch.A) and Channel 1 (DUSCC Ch.B) are configured independently via --serial-a <spec> / --serial-b <spec> or s7 :serial-a / :serial-b:
| Spec | Mode (Xl1200SerialMode) | Platform Support & Behavior |
|---|---|---|
none | SERIAL_MODE_NONE | Channel disconnected (default; Ch.A remains driven by GUI/SLB console). |
tcp-listen:<[a]:port> | SERIAL_MODE_TCP_LISTEN | Dual-stack IPv4/IPv6 non-blocking single-client TCP listener (auto-reaccepts on disconnect); portable across POSIX and Win32 Winsock2. |
tcp:<[a]:port> | SERIAL_MODE_TCP_CONNECT | Non-blocking outbound TCP client with automatic reconnect inside serial_host_poll(). |
pipe:<name_or_path> | SERIAL_MODE_PIPE | Native Win32 Named Pipe (\\.\pipe\...) on Windows or POSIX AF_UNIX local server socket on Linux/macOS. |
file:<path> | SERIAL_MODE_FILE | Append-only TX capture file for logging guest serial output. |
Live modem status (XL1200_SERIAL_MODEM_DCD, CTS, DSR in atomic modem_bits) reflects active peer connection state. Public Main-Thread API: serial_host_init(), serial_host_destroy(), serial_host_configure(), serial_host_poll(), serial_host_fill_callbacks(), and serial_host_get_status() (covered by emu/tests/test_serial_host.c).
7. Firmware Boot PROM Loader (emu/src/loader.c)
loader.c loads authentic IFEP promJ / FEP Boot PROM binary images into IfepImage and installs them into IvoryMem without injecting synthetic guest code:
- Split or Combined Boot PROM (
ifep_load_rom_files,ifep_load_file,ifep_load_buffer):- Accepts either a combined 128-KiB
rom.dump(ifep_load_file) or the two raw 64-KiB EPROM chip dumps (115988low ‖115989high viaifep_load_rom_files). ifep_guess_rom_hi_path()automatically pairs a lone--rom 115988-*.binpath with its sibling115989-*.binfile.- Parses the 30-byte
"IFEP prom<ver>"header, unpacks each 5-byte-word LE section into its declared Zone 31vma_base(e.g.,0xF8001000..0xF80071F2), reads the(entry_vma, 0xFFFFFFFF)footer, and installs sections viaifep_install()(setting cold-boot entry0xF8007162for FEP 3.30). ifep_lookup_vma()supports ROM inspection/disassembly;ifep_free()releases loaded section buffers.
- Accepts either a combined 128-KiB
8. Disk & Tape Media Tooling (emu/src/fep_convert.c, emu/include/ivory_fep_tools.h)
Compiled both as the standalone CLI ivory-fep-convert and linked into the emulator shell (ivory_fep_tools.h) for GUI/s7/debug-IPC media creation without spawning subprocesses:
| CLI Mode / C API | Function |
|---|---|
ivory-fep-convert <in_macivory_or_mister_vhd> <out_xl1200.raw> [--volume <idx|name>]( fep_tools_convert_macivory / fep_tools_convert_macivory_ex) | Converts MiSTer FPGA 1,536-byte padded .vhd images (stripping the 256-byte tail per page to packed 1,280-byte .raw), raw MacIvory (LBAL, with or without 0x400 prefix), Apple Partition Map (APM) disks, or Classic HFS volumes (automatically discovering hidden fInvisible=0x4000 'IVRY' files across both Catalog and Extents Overflow B-trees, as well as raw APM FEP partitions). When multiple FEP volumes are found and no --volume filter is given (including from the GUI Convert Mac/VHD... dialog), converts and saves all discovered volumes as <base>_<vol><ext> (e.g., xl1200_fep0.raw, xl1200_fep1.raw, xl1200_fep2.raw). (Note: scsi_wd33c93.c also mounts MiSTer 1,536-byte .vhd images directly without conversion, including read-only, read-write, and sparse .ovl overlay + commit.) |
ivory-fep-convert --to-mister-vhd <in_xl1200.raw> <out_mister.vhd>( fep_tools_to_mister_vhd) | Exports a packed 1,280-byte XL1200 .raw disk image to a MiSTer FPGA 1,536-byte padded .vhd image (1,280-byte FEP page + 256-byte 0x44 tail per 3×512B SD sector slot). |
ivory-fep-convert --convert-all <in_apm_or_hfs.img> <out_prefix>( fep_tools_convert_macivory_all) | Streams and converts every discovered hidden/visible MacIvory FEPFS volume inside an APM or Classic HFS disk image to <out_prefix>_<vol>.raw (e.g., _fep0.raw, _fep1.raw, _fep2.raw). |
ivory-fep-convert --create-blank <out.raw> --size-mb <N>( fep_tools_create_blank) | Creates a sparse, 1,280-byte-aligned blank SCSI disk image (1..16384 MiB) ready for FEP Initialize Disk. |
ivory-fep-convert --create-fepfs <out.raw> --size-mb <N>( fep_tools_create_fepfs) | Formats a ready-to-mount Little-Endian XL1200 FEPFS volume with 64 redundant LABL pages, FEPF/FEPD root directory headers, allocation bitmap, and Genera Universal Time (1900-01-01 epoch) timestamps. |
ivory-fep-convert --create-tape <out.tap>( fep_tools_create_tape) | Creates an empty SIMH .tap image terminated with an EOM (0xFFFFFFFF) marker. |
ivory-fep-convert --info <path>( fep_tools_probe / fep_tools_list_macivory_volumes) | Auto-detects FEP_IMG_XL1200_FEPFS, FEP_IMG_MISTER_VHD (1,536B padded stride), FEP_IMG_MACIVORY_FEPFS, FEP_IMG_MACIVORY_APM, FEP_IMG_MACIVORY_HFS, FEP_IMG_SIMH_TAP, or FEP_IMG_BLANK_1280B, printing APM/HFS hidden MacIvory volume inventories or FEPFS geometry and directory entries. |
ivory-fep-convert --inject / --extract( fep_tools_inject_file / fep_tools_extract_file) | Offline FEPFS file injection (allocating FEPB extents, writing FEPF headers, updating FEPD directory blocks) and extraction (e.g., for custom .flod overlays). |
- VTC / Cypress VIC068A —
cypress_VIC068A_VAC068A_users_guide.pdf(mirror) - Intel 8237A —
intel_8237A_dmac.pdf - Intel 82586 —
intel_82586_lan_coprocessor.pdf(mirror) - Intel 82586 —
intel_82586_reference_manual_1983.pdf(mirror) - Intel 82C54 —
intel_82C54_pit.pdf - National DP8466 / DP8466A —
national_1988_mass_storage_DP8466.pdf(mirror) - National DP8466A (AN-0500) —
national_DP8466A_AN0500_esdi_1987.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)