#ifndef INTUITIONENGINE_H
#define INTUITIONENGINE_H

/*
 * Intuition Engine's freestanding hardware interface.  Include the selected
 * compiler's <stdint.h> before this file. The bundled IE64 toolchain ships
 * its freestanding standard headers; external toolchains provide their own.
 */
#include <stdint.h>

#if (defined(IE_TARGET_IE64) + defined(IE_TARGET_M68K) + defined(IE_TARGET_Z80) + defined(IE_TARGET_6502) + defined(IE_TARGET_X86)) != 1
#error "define exactly one IE_TARGET_IE64, IE_TARGET_M68K, IE_TARGET_Z80, IE_TARGET_6502 or IE_TARGET_X86"
#endif

#if defined(IE_TARGET_IE64)
# if !defined(__ie64__)
#  error "IE_TARGET_IE64 requires ie64-cproc"
# endif
# define IE_HAS_BANK_WINDOWS 0
# define IE_HAS_X86_PORT_IO 0
# define IE_HAS_IE64_CONTROL_REGISTERS 1
# define IE_HAS_IE64_ATOMICS 1
# define IE_HAS_IE64_FPU 1
# define IE_ADDRESS_BITS 64
# define IE_BIG_ENDIAN 0
#elif defined(IE_TARGET_M68K)
# if !defined(__GNUC__) && !defined(__VBCC__)
#  error "IE_TARGET_M68K requires GCC or VBCC"
# endif
# define IE_HAS_BANK_WINDOWS 0
# define IE_HAS_X86_PORT_IO 0
# define IE_HAS_IE64_CONTROL_REGISTERS 0
# define IE_HAS_IE64_ATOMICS 0
# define IE_HAS_IE64_FPU 0
# define IE_ADDRESS_BITS 32
# define IE_BIG_ENDIAN 1
#elif defined(IE_TARGET_Z80)
# if !defined(__VBCC__)
#  error "IE_TARGET_Z80 requires VBCC"
# endif
# define IE_HAS_BANK_WINDOWS 1
# define IE_HAS_X86_PORT_IO 0
# define IE_HAS_IE64_CONTROL_REGISTERS 0
# define IE_HAS_IE64_ATOMICS 0
# define IE_HAS_IE64_FPU 0
# define IE_ADDRESS_BITS 16
# define IE_BIG_ENDIAN 0
#elif defined(IE_TARGET_6502)
# if !defined(__VBCC__) && !defined(__SDCC)
#  error "IE_TARGET_6502 requires VBCC or SDCC"
# endif
# define IE_HAS_BANK_WINDOWS 1
# define IE_HAS_X86_PORT_IO 0
# define IE_HAS_IE64_CONTROL_REGISTERS 0
# define IE_HAS_IE64_ATOMICS 0
# define IE_HAS_IE64_FPU 0
# define IE_ADDRESS_BITS 16
# define IE_BIG_ENDIAN 0
#elif defined(IE_TARGET_X86)
# if !defined(__GNUC__)
#  error "IE_TARGET_X86 requires GCC"
# endif
# define IE_HAS_BANK_WINDOWS 0
# define IE_HAS_X86_PORT_IO 1
# define IE_HAS_IE64_CONTROL_REGISTERS 0
# define IE_HAS_IE64_ATOMICS 0
# define IE_HAS_IE64_FPU 0
# define IE_ADDRESS_BITS 32
# define IE_BIG_ENDIAN 0
#endif

#if !defined(UINT8_MAX) || !defined(UINT16_MAX) || !defined(UINT32_MAX)
#error "the selected compiler must provide uint8_t, uint16_t and uint32_t"
#endif
#if IE_HAS_IE64_CONTROL_REGISTERS && !defined(UINT64_MAX)
#error "ie64-cproc must provide uint64_t"
#endif

#define IE_REG8(address)  (*(volatile uint8_t *)(uintptr_t)(address))
#define IE_REG16(address) (*(volatile uint16_t *)(uintptr_t)(address))
#define IE_REG32(address) (*(volatile uint32_t *)(uintptr_t)(address))

/* Common, target-normalised hardware map.  See architecture.md for access semantics. */
#define IE_PROGRAM_START 0x001000u
#define IE_VIDEO_CTRL 0x0f0000u
/* VIDEO_CTRL bits. Bit 1 retains the completed framebuffer until it is cleared. */
#define IE_VIDEO_CTRL_ENABLE 0x01u
#define IE_VIDEO_CTRL_PRESENT_HOLD 0x02u
#define IE_VIDEO_MODE 0x0f0004u
#define IE_VIDEO_STATUS 0x0f0008u
#define IE_VIDEO_STATUS_VBLANK 0x02u
#define IE_VIDEO_STATUS_FB_ERROR 0x04u
#define IE_VIDEO_COPPER_CTRL 0x0f000cu
#define IE_VIDEO_COPPER_PTR 0x0f0010u
#define IE_VIDEO_COPPER_PC 0x0f0014u
#define IE_VIDEO_COPPER_STATUS 0x0f0018u
#if defined(IE_TARGET_6502) || defined(IE_TARGET_Z80)
# define IE_VIDEO_COPPER_CTRL_TARGET 0xf00cu
# define IE_VIDEO_COPPER_PTR0_TARGET 0xf010u
# define IE_VIDEO_COPPER_PTR1_TARGET 0xf011u
# define IE_VIDEO_COPPER_PTR2_TARGET 0xf012u
# define IE_VIDEO_COPPER_PTR3_TARGET 0xf013u
#else
# define IE_VIDEO_COPPER_CTRL_TARGET IE_VIDEO_COPPER_CTRL
# define IE_VIDEO_COPPER_PTR_TARGET IE_VIDEO_COPPER_PTR
#endif
#define IE_VIDEO_BLITTER_CTRL 0x0f001cu
#define IE_VIDEO_BLITTER_OP 0x0f0020u
#define IE_VIDEO_BLITTER_SRC 0x0f0024u
#define IE_VIDEO_BLITTER_DST 0x0f0028u
#define IE_VIDEO_BLITTER_WIDTH 0x0f002cu
#define IE_VIDEO_BLITTER_HEIGHT 0x0f0030u
#define IE_VIDEO_BLITTER_SOURCE_STRIDE 0x0f0034u
#define IE_VIDEO_BLITTER_DESTINATION_STRIDE 0x0f0038u
#define IE_VIDEO_BLITTER_COLOUR 0x0f003cu
#define IE_VIDEO_BLITTER_MASK 0x0f0040u
#define IE_VIDEO_BLITTER_STATUS 0x0f0044u
#define IE_VIDEO_RASTER_Y 0x0f0048u
#define IE_VIDEO_RASTER_HEIGHT 0x0f004cu
#define IE_VIDEO_RASTER_COLOUR 0x0f0050u
#define IE_VIDEO_RASTER_CONTROL 0x0f0054u
#define IE_VIDEO_MODE7_U0 0x0f0058u
#define IE_VIDEO_MODE7_V0 0x0f005cu
#define IE_VIDEO_MODE7_DU_COLUMN 0x0f0060u
#define IE_VIDEO_MODE7_DV_COLUMN 0x0f0064u
#define IE_VIDEO_MODE7_DU_ROW 0x0f0068u
#define IE_VIDEO_MODE7_DV_ROW 0x0f006cu
#define IE_VIDEO_MODE7_TEXTURE_WIDTH 0x0f0070u
#define IE_VIDEO_MODE7_TEXTURE_HEIGHT 0x0f0074u
#define IE_VIDEO_BLITTER_OP_COPY 0x00u
#define IE_VIDEO_BLITTER_OP_FILL 0x01u
#define IE_VIDEO_BLITTER_OP_LINE 0x02u
#define IE_VIDEO_BLITTER_OP_MASKED 0x03u
#define IE_VIDEO_BLITTER_OP_ALPHA 0x04u
#define IE_VIDEO_BLITTER_OP_MODE7 0x05u
#define IE_VIDEO_BLITTER_OP_COLOUR_EXPAND 0x06u
#define IE_VIDEO_BLITTER_CTRL_START 0x01u
#define IE_VIDEO_BLITTER_CTRL_BUSY 0x02u
#define IE_VIDEO_BLITTER_CTRL_IRQ 0x04u
#define IE_BLT_CTRL IE_VIDEO_BLITTER_CTRL
#define IE_BLT_OP IE_VIDEO_BLITTER_OP
#define IE_BLT_SRC IE_VIDEO_BLITTER_SRC
#define IE_BLT_DST IE_VIDEO_BLITTER_DST
#define IE_BLT_WIDTH IE_VIDEO_BLITTER_WIDTH
#define IE_BLT_HEIGHT IE_VIDEO_BLITTER_HEIGHT
#define IE_BLT_SRC_STRIDE IE_VIDEO_BLITTER_SOURCE_STRIDE
#define IE_BLT_DST_STRIDE IE_VIDEO_BLITTER_DESTINATION_STRIDE
#define IE_BLT_COLOR IE_VIDEO_BLITTER_COLOUR
#define IE_BLT_MASK IE_VIDEO_BLITTER_MASK
#define IE_BLT_STATUS IE_VIDEO_BLITTER_STATUS
#define IE_BLT_MODE7_U0 IE_VIDEO_MODE7_U0
#define IE_BLT_MODE7_V0 IE_VIDEO_MODE7_V0
#define IE_BLT_MODE7_DU_COL IE_VIDEO_MODE7_DU_COLUMN
#define IE_BLT_MODE7_DV_COL IE_VIDEO_MODE7_DV_COLUMN
#define IE_BLT_MODE7_DU_ROW IE_VIDEO_MODE7_DU_ROW
#define IE_BLT_MODE7_DV_ROW IE_VIDEO_MODE7_DV_ROW
#define IE_BLT_MODE7_TEX_W IE_VIDEO_MODE7_TEXTURE_WIDTH
#define IE_BLT_MODE7_TEX_H IE_VIDEO_MODE7_TEXTURE_HEIGHT
#define IE_BLT_OP_COPY IE_VIDEO_BLITTER_OP_COPY
#define IE_BLT_OP_FILL IE_VIDEO_BLITTER_OP_FILL
#define IE_BLT_OP_LINE IE_VIDEO_BLITTER_OP_LINE
#define IE_BLT_OP_MODE7 IE_VIDEO_BLITTER_OP_MODE7
#define IE_BLT_CTRL_START IE_VIDEO_BLITTER_CTRL_START
#define IE_BLT_CTRL_BUSY IE_VIDEO_BLITTER_CTRL_BUSY
#define IE_VIDEO_PALETTE_INDEX 0x0f0078u
#define IE_VIDEO_PALETTE_DATA 0x0f007cu
#define IE_VIDEO_COLOR_MODE 0x0f0080u
#define IE_VIDEO_FB_BASE 0x0f0084u
#if defined(IE_TARGET_6502) || defined(IE_TARGET_Z80)
# define IE_VIDEO_CTRL_TARGET 0xf000u
# define IE_VIDEO_MODE_TARGET 0xf004u
# define IE_VIDEO_STATUS_TARGET 0xf008u
# define IE_VIDEO_PALETTE_INDEX_TARGET 0xf078u
# define IE_VIDEO_PALETTE_DATA_TARGET 0xf07cu
# define IE_VIDEO_FB_BASE_TARGET 0xf084u
#else
# define IE_VIDEO_CTRL_TARGET IE_VIDEO_CTRL
# define IE_VIDEO_MODE_TARGET IE_VIDEO_MODE
# define IE_VIDEO_STATUS_TARGET IE_VIDEO_STATUS
# define IE_VIDEO_PALETTE_INDEX_TARGET IE_VIDEO_PALETTE_INDEX
# define IE_VIDEO_PALETTE_DATA_TARGET IE_VIDEO_PALETTE_DATA
# define IE_VIDEO_FB_BASE_TARGET IE_VIDEO_FB_BASE
#endif

/* ZX Spectrum ULA. The paged data register transfers one byte at a time. */
#define IE_ULA_MMIO_BASE 0x0f2000u
#define IE_ULA_MMIO_END 0x0f2017u
#define IE_ULA_CTRL_ENABLE 0x01u
#define IE_ULA_CTRL_VBLANK_IRQ_ENABLE 0x02u
#define IE_ULA_CTRL_AUTO_INCREMENT 0x04u
#define IE_ULA_STATUS_VBLANK 0x01u
#define IE_ULA_BITMAP_SIZE 0x1800u
#define IE_ULA_ATTRIBUTE_OFFSET 0x1800u
#define IE_ULA_ATTRIBUTE_SIZE 0x0300u
#define IE_ULA_VRAM_SIZE 0x1b00u
#define IE_ULA_DISPLAY_WIDTH 0x0100u
#define IE_ULA_DISPLAY_HEIGHT 0x00c0u
#define IE_ULA_BORDER_SIZE 0x0020u
#define IE_ULA_FRAME_WIDTH 0x0140u
#define IE_ULA_FRAME_HEIGHT 0x0100u
#define IE_ULA_CELLS_X 0x0020u
#define IE_ULA_CELLS_Y 0x0018u
#define IE_ULA_VRAM_APERTURE 0x0fa000u
#define IE_ULA_VRAM_APERTURE_END 0x0fbaffu
#define IE_6502_ULA_BASE 0x00d800u
#define IE_6502_ULA_DATA 0x00d814u
#define IE_Z80_ULA_PORT_BORDER 0x00feu
#define IE_Z80_ULA_PORT_CONTROL 0x00fdu
#define IE_Z80_ULA_PORT_STATUS 0x00beu
#define IE_Z80_ULA_PORT_ADDRESS_LOW 0x00fau
#define IE_Z80_ULA_PORT_ADDRESS_HIGH 0x00fbu
#define IE_Z80_ULA_PORT_DATA 0x00fcu

#if defined(IE_TARGET_6502)
# define IE_ULA_BASE 0xd800u
# define IE_ULA_BORDER 0xd800u
# define IE_ULA_CONTROL 0xd804u
# define IE_ULA_STATUS 0xd808u
# define IE_ULA_ADDRESS_LOW 0xd80cu
# define IE_ULA_ADDRESS_HIGH 0xd810u
# define IE_ULA_DATA 0xd814u
#elif defined(IE_TARGET_Z80)
# define IE_ULA_PORT_BORDER IE_Z80_ULA_PORT_BORDER
# define IE_ULA_PORT_CONTROL IE_Z80_ULA_PORT_CONTROL
# define IE_ULA_PORT_STATUS IE_Z80_ULA_PORT_STATUS
# define IE_ULA_PORT_ADDRESS_LOW IE_Z80_ULA_PORT_ADDRESS_LOW
# define IE_ULA_PORT_ADDRESS_HIGH IE_Z80_ULA_PORT_ADDRESS_HIGH
# define IE_ULA_PORT_DATA IE_Z80_ULA_PORT_DATA
# define IE_ULA_PORT IE_ULA_PORT_BORDER
#else
# define IE_ULA_BASE IE_ULA_MMIO_BASE
# define IE_ULA_BORDER 0x0f2000u
# define IE_ULA_CONTROL 0x0f2004u
# define IE_ULA_STATUS 0x0f2008u
# define IE_ULA_ADDRESS_LOW 0x0f200cu
# define IE_ULA_ADDRESS_HIGH 0x0f2010u
# define IE_ULA_DATA 0x0f2014u
#endif

#if !defined(IE_TARGET_Z80)
# define IE_ULA_CTRL IE_ULA_CONTROL
# define IE_ULA_ADDR_LO IE_ULA_ADDRESS_LOW
# define IE_ULA_ADDR_HI IE_ULA_ADDRESS_HIGH
#endif
#define IE_ULA_CTRL_VBLANK_IRQ_EN IE_ULA_CTRL_VBLANK_IRQ_ENABLE
#define IE_ULA_CTRL_AUTO_INC IE_ULA_CTRL_AUTO_INCREMENT
#define IE_ULA_VRAM_AP IE_ULA_VRAM_APERTURE
#define IE_ULA_VRAM_AP_END IE_ULA_VRAM_APERTURE_END
#define IE_ULA_VRAM IE_ULA_VRAM_APERTURE
#define IE_ULA_ATTR_OFFSET IE_ULA_ATTRIBUTE_OFFSET
#define IE_ULA_ATTR_SIZE IE_ULA_ATTRIBUTE_SIZE
#define IE_ULA_DISPLAY_W IE_ULA_DISPLAY_WIDTH
#define IE_ULA_DISPLAY_H IE_ULA_DISPLAY_HEIGHT
#define IE_ULA_FRAME_W IE_ULA_FRAME_WIDTH
#define IE_ULA_FRAME_H IE_ULA_FRAME_HEIGHT

/* Atari ANTIC and GTIA video register maps. */
#define IE_ANTIC_BASE_DIRECT 0x0f2100u
#define IE_ANTIC_END_DIRECT 0x0f213fu
#define IE_ANTIC_DMA_CONTROL 0x0f2100u
#define IE_ANTIC_CHARACTER_CONTROL 0x0f2104u
#define IE_ANTIC_DISPLAY_LIST_LOW 0x0f2108u
#define IE_ANTIC_DISPLAY_LIST_HIGH 0x0f210cu
#define IE_ANTIC_HORIZONTAL_SCROLL 0x0f2110u
#define IE_ANTIC_VERTICAL_SCROLL 0x0f2114u
#define IE_ANTIC_PLAYER_MISSILE_BASE 0x0f2118u
#define IE_ANTIC_CHARACTER_BASE 0x0f211cu
#define IE_ANTIC_WAIT_SYNC 0x0f2120u
#define IE_ANTIC_VERTICAL_COUNT 0x0f2124u
#define IE_ANTIC_LIGHT_PEN_H 0x0f2128u
#define IE_ANTIC_LIGHT_PEN_V 0x0f212cu
#define IE_ANTIC_NMI_ENABLE 0x0f2130u
#define IE_ANTIC_NMI_STATUS 0x0f2134u
#define IE_ANTIC_ENABLE 0x0f2138u
#define IE_ANTIC_STATUS 0x0f213cu
#define IE_ANTIC_DMA_NARROW 0x01u
#define IE_ANTIC_DMA_NORMAL 0x02u
#define IE_ANTIC_DMA_WIDE 0x03u
#define IE_ANTIC_DMA_MISSILE 0x04u
#define IE_ANTIC_DMA_PLAYER 0x08u
#define IE_ANTIC_DMA_PLAYER_MISSILE_RESOLUTION 0x10u
#define IE_ANTIC_DMA_DISPLAY_LIST 0x20u
#define IE_ANTIC_NMI_DISPLAY_LIST 0x80u
#define IE_ANTIC_NMI_VERTICAL_BLANK 0x40u
#define IE_ANTIC_STATUS_VBLANK 0x01u
#define IE_GTIA_BASE_DIRECT 0x0f2140u
#define IE_GTIA_END_DIRECT 0x0f21fbu
#define IE_GTIA_PLAYFIELD_COLOUR0 0x0f2140u
#define IE_GTIA_PLAYFIELD_COLOUR1 0x0f2144u
#define IE_GTIA_PLAYFIELD_COLOUR2 0x0f2148u
#define IE_GTIA_PLAYFIELD_COLOUR3 0x0f214cu
#define IE_GTIA_BACKGROUND_COLOUR 0x0f2150u
#define IE_GTIA_PLAYER_COLOUR0 0x0f2154u
#define IE_GTIA_PLAYER_COLOUR1 0x0f2158u
#define IE_GTIA_PLAYER_COLOUR2 0x0f215cu
#define IE_GTIA_PLAYER_COLOUR3 0x0f2160u
#define IE_GTIA_PRIORITY 0x0f2164u
#define IE_GTIA_GRAPHICS_CONTROL 0x0f2168u
#define IE_GTIA_CONSOLE 0x0f216cu
#define IE_GTIA_HPOS_PLAYER0 0x0f2170u
#define IE_GTIA_HPOS_PLAYER1 0x0f2174u
#define IE_GTIA_HPOS_PLAYER2 0x0f2178u
#define IE_GTIA_HPOS_PLAYER3 0x0f217cu
#define IE_GTIA_HPOS_MISSILE0 0x0f2180u
#define IE_GTIA_HPOS_MISSILE1 0x0f2184u
#define IE_GTIA_HPOS_MISSILE2 0x0f2188u
#define IE_GTIA_HPOS_MISSILE3 0x0f218cu
#define IE_GTIA_SIZE_PLAYER0 0x0f2190u
#define IE_GTIA_SIZE_PLAYER1 0x0f2194u
#define IE_GTIA_SIZE_PLAYER2 0x0f2198u
#define IE_GTIA_SIZE_PLAYER3 0x0f219cu
#define IE_GTIA_SIZE_MISSILES 0x0f21a0u
#define IE_GTIA_GRAPHICS_PLAYER0 0x0f21a4u
#define IE_GTIA_GRAPHICS_PLAYER1 0x0f21a8u
#define IE_GTIA_GRAPHICS_PLAYER2 0x0f21acu
#define IE_GTIA_GRAPHICS_PLAYER3 0x0f21b0u
#define IE_GTIA_GRAPHICS_MISSILES 0x0f21b4u
#define IE_GTIA_COLLISION_CLEAR 0x0f21f8u
#define IE_Z80_ANTIC_PORT_SELECT 0xd4u
#define IE_Z80_ANTIC_PORT_DATA 0xd5u
#define IE_Z80_GTIA_PORT_SELECT 0xd6u
#define IE_Z80_GTIA_PORT_DATA 0xd7u

/* Assembly-compatible short spellings for the direct ANTIC/GTIA map. */
#define IE_ANTIC_BASE IE_ANTIC_BASE_DIRECT
#define IE_ANTIC_DMACTL IE_ANTIC_DMA_CONTROL
#define IE_ANTIC_CHACTL IE_ANTIC_CHARACTER_CONTROL
#define IE_ANTIC_DLISTL IE_ANTIC_DISPLAY_LIST_LOW
#define IE_ANTIC_DLISTH IE_ANTIC_DISPLAY_LIST_HIGH
#define IE_ANTIC_HSCROL IE_ANTIC_HORIZONTAL_SCROLL
#define IE_ANTIC_VSCROL IE_ANTIC_VERTICAL_SCROLL
#define IE_ANTIC_PMBASE IE_ANTIC_PLAYER_MISSILE_BASE
#define IE_ANTIC_CHBASE IE_ANTIC_CHARACTER_BASE
#define IE_ANTIC_WSYNC IE_ANTIC_WAIT_SYNC
#define IE_ANTIC_VCOUNT IE_ANTIC_VERTICAL_COUNT
#define IE_ANTIC_PENH IE_ANTIC_LIGHT_PEN_H
#define IE_ANTIC_PENV IE_ANTIC_LIGHT_PEN_V
#define IE_ANTIC_NMIEN IE_ANTIC_NMI_ENABLE
#define IE_ANTIC_NMIST IE_ANTIC_NMI_STATUS
#define IE_ANTIC_ENABLE_VIDEO IE_ANTIC_ENABLE
#define IE_ANTIC_FRAME_W 384u
#define IE_ANTIC_FRAME_H 240u
#define IE_ANTIC_DISPLAY_W 320u
#define IE_ANTIC_DISPLAY_H 192u
#define IE_ANTIC_CHACTL_BLANK 0x01u
#define IE_ANTIC_CHACTL_INVERT 0x02u
#define IE_ANTIC_CHACTL_REFLECT 0x04u
#define IE_ANTIC_DMA_PMRES IE_ANTIC_DMA_PLAYER_MISSILE_RESOLUTION
#define IE_ANTIC_DMA_DL IE_ANTIC_DMA_DISPLAY_LIST
#define IE_ANTIC_NMIEN_DLI IE_ANTIC_NMI_DISPLAY_LIST
#define IE_ANTIC_NMIEN_VBI IE_ANTIC_NMI_VERTICAL_BLANK
#define IE_GTIA_BASE IE_GTIA_BASE_DIRECT
#define IE_GTIA_END IE_GTIA_END_DIRECT
#define IE_GTIA_COLPF0 IE_GTIA_PLAYFIELD_COLOUR0
#define IE_GTIA_COLPF1 IE_GTIA_PLAYFIELD_COLOUR1
#define IE_GTIA_COLPF2 IE_GTIA_PLAYFIELD_COLOUR2
#define IE_GTIA_COLPF3 IE_GTIA_PLAYFIELD_COLOUR3
#define IE_GTIA_COLBK IE_GTIA_BACKGROUND_COLOUR
#define IE_GTIA_COLPM0 IE_GTIA_PLAYER_COLOUR0
#define IE_GTIA_COLPM1 IE_GTIA_PLAYER_COLOUR1
#define IE_GTIA_COLPM2 IE_GTIA_PLAYER_COLOUR2
#define IE_GTIA_COLPM3 IE_GTIA_PLAYER_COLOUR3
#define IE_GTIA_PRIOR IE_GTIA_PRIORITY
#define IE_GTIA_GRACTL IE_GTIA_GRAPHICS_CONTROL
#define IE_GTIA_CONSOL IE_GTIA_CONSOLE
#define IE_GTIA_HPOSP0 IE_GTIA_HPOS_PLAYER0
#define IE_GTIA_HPOSP1 IE_GTIA_HPOS_PLAYER1
#define IE_GTIA_HPOSP2 IE_GTIA_HPOS_PLAYER2
#define IE_GTIA_HPOSP3 IE_GTIA_HPOS_PLAYER3
#define IE_GTIA_HPOSM0 IE_GTIA_HPOS_MISSILE0
#define IE_GTIA_HPOSM1 IE_GTIA_HPOS_MISSILE1
#define IE_GTIA_HPOSM2 IE_GTIA_HPOS_MISSILE2
#define IE_GTIA_HPOSM3 IE_GTIA_HPOS_MISSILE3
#define IE_GTIA_SIZEP0 IE_GTIA_SIZE_PLAYER0
#define IE_GTIA_SIZEP1 IE_GTIA_SIZE_PLAYER1
#define IE_GTIA_SIZEP2 IE_GTIA_SIZE_PLAYER2
#define IE_GTIA_SIZEP3 IE_GTIA_SIZE_PLAYER3
#define IE_GTIA_SIZEM IE_GTIA_SIZE_MISSILES
#define IE_GTIA_GRAFP0 IE_GTIA_GRAPHICS_PLAYER0
#define IE_GTIA_GRAFP1 IE_GTIA_GRAPHICS_PLAYER1
#define IE_GTIA_GRAFP2 IE_GTIA_GRAPHICS_PLAYER2
#define IE_GTIA_GRAFP3 IE_GTIA_GRAPHICS_PLAYER3
#define IE_GTIA_GRAFM IE_GTIA_GRAPHICS_MISSILES
#define IE_GTIA_HITCLR IE_GTIA_COLLISION_CLEAR
#define IE_GTIA_M0PF 0x0f21b8u
#define IE_GTIA_M1PF 0x0f21bcu
#define IE_GTIA_M2PF 0x0f21c0u
#define IE_GTIA_M3PF 0x0f21c4u
#define IE_GTIA_P0PF 0x0f21c8u
#define IE_GTIA_P1PF 0x0f21ccu
#define IE_GTIA_P2PF 0x0f21d0u
#define IE_GTIA_P3PF 0x0f21d4u
#define IE_GTIA_M0PL 0x0f21d8u
#define IE_GTIA_M1PL 0x0f21dcu
#define IE_GTIA_M2PL 0x0f21e0u
#define IE_GTIA_M3PL 0x0f21e4u
#define IE_GTIA_P0PL 0x0f21e8u
#define IE_GTIA_P1PL 0x0f21ecu
#define IE_GTIA_P2PL 0x0f21f0u
#define IE_GTIA_P3PL 0x0f21f4u
#define IE_GTIA_PRIOR_P03 0x01u
#define IE_GTIA_PRIOR_P01 0x02u
#define IE_GTIA_PRIOR_P23 0x04u
#define IE_GTIA_PRIOR_MULTI 0x10u
#define IE_GTIA_PRIOR_FIFTH 0x20u
#define IE_GTIA_PRIOR_GTIA1 0x40u
#define IE_GTIA_PRIOR_GTIA2 0x80u
#define IE_GTIA_GRACTL_MISSLE 0x01u
#define IE_GTIA_GRACTL_PLAYER 0x02u
#define IE_GTIA_GRACTL_LATCH 0x04u

/* IBM VGA memory registers and target access routes. */
#define IE_VGA_BASE_DIRECT 0x0f1000u
#define IE_VGA_END_DIRECT 0x0f13ffu
#define IE_VGA_MODE 0x0f1000u
#define IE_VGA_STATUS 0x0f1004u
#define IE_VGA_CONTROL 0x0f1008u
#define IE_VGA_SEQUENCER_INDEX 0x0f1010u
#define IE_VGA_SEQUENCER_DATA 0x0f1014u
#define IE_VGA_SEQUENCER_MAP_MASK 0x0f1018u
#define IE_VGA_CRTC_INDEX 0x0f1020u
#define IE_VGA_CRTC_DATA 0x0f1024u
#define IE_VGA_CRTC_START_HIGH 0x0f1028u
#define IE_VGA_CRTC_START_LOW 0x0f102cu
#define IE_VGA_GRAPHICS_INDEX 0x0f1030u
#define IE_VGA_GRAPHICS_DATA 0x0f1034u
#define IE_VGA_GRAPHICS_READ_MAP 0x0f1038u
#define IE_VGA_GRAPHICS_BIT_MASK 0x0f103cu
#define IE_VGA_ATTRIBUTE_INDEX 0x0f1040u
#define IE_VGA_ATTRIBUTE_DATA 0x0f1044u
#define IE_VGA_DAC_MASK 0x0f1050u
#define IE_VGA_DAC_READ_INDEX 0x0f1054u
#define IE_VGA_DAC_WRITE_INDEX 0x0f1058u
#define IE_VGA_DAC_DATA 0x0f105cu
#define IE_VGA_PALETTE 0x0f1100u
#define IE_VGA_PALETTE_END 0x0f13ffu
#define IE_VGA_VRAM_WINDOW 0x0a0000u
#define IE_VGA_VRAM_SIZE 0x10000u
#define IE_VGA_TEXT_WINDOW 0x0b8000u
#define IE_VGA_TEXT_SIZE 0x8000u
#define IE_VGA_MODE_TEXT 0x03u
#define IE_VGA_MODE_12H 0x12u
#define IE_VGA_MODE_13H 0x13u
#define IE_VGA_MODE_X 0x14u
#define IE_VGA_STATUS_VSYNC 0x01u
#define IE_VGA_STATUS_RETRACE 0x08u
#define IE_VGA_CONTROL_ENABLE 0x01u
#define IE_Z80_VGA_PORT_MODE 0xa0u
#define IE_Z80_VGA_PORT_STATUS 0xa1u
#define IE_Z80_VGA_PORT_CONTROL 0xa2u
#define IE_Z80_VGA_PORT_SEQUENCER_INDEX 0xa3u
#define IE_Z80_VGA_PORT_SEQUENCER_DATA 0xa4u
#define IE_Z80_VGA_PORT_CRTC_INDEX 0xa5u
#define IE_Z80_VGA_PORT_CRTC_DATA 0xa6u
#define IE_Z80_VGA_PORT_GRAPHICS_INDEX 0xa7u
#define IE_Z80_VGA_PORT_GRAPHICS_DATA 0xa8u
#define IE_Z80_VGA_PORT_DAC_WRITE_INDEX 0xa9u
#define IE_Z80_VGA_PORT_DAC_DATA 0xaau
#define IE_Z80_VGA_PORT_DAC_READ_INDEX 0xabu
#define IE_Z80_VGA_PORT_DAC_MASK 0xacu
#define IE_Z80_VGA_PORT_VRAM_BANK 0xadu
#define IE_6502_VGA_BASE 0xd700u
#define IE_6502_VGA_MODE 0xd700u
#define IE_6502_VGA_STATUS 0xd701u
#define IE_6502_VGA_CONTROL 0xd702u

/* Full direct VGA register block. */
#define IE_VGA_BASE 0x0f1000u
#define IE_VGA_SEQ_INDEX 0x0f1010u
#define IE_VGA_SEQ_DATA 0x0f1014u
#define IE_VGA_SEQ_MAPMASK 0x0f1018u
#define IE_VGA_CRTC_INDEX 0x0f1020u
#define IE_VGA_CRTC_DATA 0x0f1024u
#define IE_VGA_CRTC_STARTHI 0x0f1028u
#define IE_VGA_CRTC_STARTLO 0x0f102cu
#define IE_VGA_GC_INDEX 0x0f1030u
#define IE_VGA_GC_DATA 0x0f1034u
#define IE_VGA_GC_READMAP 0x0f1038u
#define IE_VGA_GC_BITMASK 0x0f103cu
#define IE_VGA_ATTR_INDEX 0x0f1040u
#define IE_VGA_ATTR_DATA 0x0f1044u
#define IE_VGA_DAC_MASK 0x0f1050u
#define IE_VGA_DAC_RINDEX 0x0f1054u
#define IE_VGA_DAC_WINDEX 0x0f1058u
#define IE_VGA_DAC_DATA 0x0f105cu
#define IE_VGA_PALETTE 0x0f1100u
#define IE_VGA_VRAM 0x0a0000u

#if defined(IE_TARGET_6502)
# define IE_VGA_TARGET_MODE IE_6502_VGA_MODE
# define IE_VGA_TARGET_STATUS IE_6502_VGA_STATUS
# define IE_VGA_TARGET_CONTROL IE_6502_VGA_CONTROL
#elif defined(IE_TARGET_Z80)
# define IE_VGA_TARGET_MODE IE_Z80_VGA_PORT_MODE
# define IE_VGA_TARGET_STATUS IE_Z80_VGA_PORT_STATUS
# define IE_VGA_TARGET_CONTROL IE_Z80_VGA_PORT_CONTROL
#else
# define IE_VGA_TARGET_MODE IE_VGA_MODE
# define IE_VGA_TARGET_STATUS IE_VGA_STATUS
# define IE_VGA_TARGET_CONTROL IE_VGA_CONTROL
#endif

/* TED video register block.  6502 uses the documented stride-one aliases. */
#define IE_TED_VIDEO_BASE_DIRECT 0x0f0f20u
#define IE_TED_VIDEO_END_DIRECT 0x0f0f6bu
#define IE_TED_VIDEO_CONTROL1 0x0f0f20u
#define IE_TED_VIDEO_CONTROL2 0x0f0f24u
#define IE_TED_VIDEO_CHARACTER_BASE 0x0f0f28u
#define IE_TED_VIDEO_MATRIX_BASE 0x0f0f2cu
#define IE_TED_VIDEO_BACKGROUND0 0x0f0f30u
#define IE_TED_VIDEO_BACKGROUND1 0x0f0f34u
#define IE_TED_VIDEO_BACKGROUND2 0x0f0f38u
#define IE_TED_VIDEO_BACKGROUND3 0x0f0f3cu
#define IE_TED_VIDEO_BORDER 0x0f0f40u
#define IE_TED_VIDEO_CURSOR_HIGH 0x0f0f44u
#define IE_TED_VIDEO_CURSOR_LOW 0x0f0f48u
#define IE_TED_VIDEO_CURSOR_COLOUR 0x0f0f4cu
#define IE_TED_VIDEO_RASTER_LOW 0x0f0f50u
#define IE_TED_VIDEO_RASTER_HIGH 0x0f0f54u
#define IE_TED_VIDEO_ENABLE 0x0f0f58u
#define IE_TED_VIDEO_STATUS 0x0f0f5cu
#define IE_TED_VIDEO_RASTER_COMPARE_LOW 0x0f0f60u
#define IE_TED_VIDEO_RASTER_COMPARE_HIGH 0x0f0f64u
#define IE_TED_VIDEO_RASTER_STATUS 0x0f0f68u
#define IE_TED_VIDEO_CONTROL1_ECM 0x40u
#define IE_TED_VIDEO_CONTROL1_BMM 0x20u
#define IE_TED_VIDEO_CONTROL1_ENABLE 0x10u
#define IE_TED_VIDEO_CONTROL1_ROW_SELECT 0x08u
#define IE_TED_VIDEO_CONTROL1_SCROLL_MASK 0x07u
#define IE_TED_VIDEO_CONTROL2_RESET 0x20u
#define IE_TED_VIDEO_CONTROL2_MULTICOLOUR 0x10u
#define IE_TED_VIDEO_CONTROL2_COLUMN_SELECT 0x08u
#define IE_TED_VIDEO_CONTROL2_SCROLL_MASK 0x07u
#define IE_TED_VIDEO_STATUS_VBLANK 0x01u
#define IE_TED_VIDEO_RASTER_PENDING 0x80u
#define IE_6502_TED_VIDEO_BASE 0xd620u
#define IE_Z80_TED_VIDEO_PORT_SELECT 0xf2u
#define IE_Z80_TED_VIDEO_PORT_DATA 0xf3u
#if defined(IE_TARGET_6502)
# define IE_TED_VIDEO_TARGET_CONTROL1 0xd620u
# define IE_TED_VIDEO_TARGET_CONTROL2 0xd621u
# define IE_TED_VIDEO_TARGET_CHARACTER_BASE 0xd622u
# define IE_TED_VIDEO_TARGET_MATRIX_BASE 0xd623u
# define IE_TED_VIDEO_TARGET_BACKGROUND0 0xd624u
# define IE_TED_VIDEO_TARGET_BACKGROUND1 0xd625u
# define IE_TED_VIDEO_TARGET_BACKGROUND2 0xd626u
# define IE_TED_VIDEO_TARGET_BACKGROUND3 0xd627u
# define IE_TED_VIDEO_TARGET_BORDER 0xd628u
# define IE_TED_VIDEO_TARGET_CURSOR_HIGH 0xd629u
# define IE_TED_VIDEO_TARGET_CURSOR_LOW 0xd62au
# define IE_TED_VIDEO_TARGET_CURSOR_COLOUR 0xd62bu
# define IE_TED_VIDEO_TARGET_RASTER_LOW 0xd62cu
# define IE_TED_VIDEO_TARGET_RASTER_HIGH 0xd62du
# define IE_TED_VIDEO_TARGET_ENABLE 0xd62eu
# define IE_TED_VIDEO_TARGET_STATUS 0xd62fu
# define IE_TED_VIDEO_TARGET_RASTER_COMPARE_LOW 0xd630u
# define IE_TED_VIDEO_TARGET_RASTER_COMPARE_HIGH 0xd631u
# define IE_TED_VIDEO_TARGET_RASTER_STATUS 0xd632u
#elif defined(IE_TARGET_Z80)
# define IE_TED_VIDEO_TARGET_CONTROL1 0x20u
# define IE_TED_VIDEO_TARGET_CONTROL2 0x21u
# define IE_TED_VIDEO_TARGET_CHARACTER_BASE 0x22u
# define IE_TED_VIDEO_TARGET_MATRIX_BASE 0x23u
# define IE_TED_VIDEO_TARGET_BACKGROUND0 0x24u
# define IE_TED_VIDEO_TARGET_BACKGROUND1 0x25u
# define IE_TED_VIDEO_TARGET_BACKGROUND2 0x26u
# define IE_TED_VIDEO_TARGET_BACKGROUND3 0x27u
# define IE_TED_VIDEO_TARGET_BORDER 0x28u
# define IE_TED_VIDEO_TARGET_CURSOR_HIGH 0x29u
# define IE_TED_VIDEO_TARGET_CURSOR_LOW 0x2au
# define IE_TED_VIDEO_TARGET_CURSOR_COLOUR 0x2bu
# define IE_TED_VIDEO_TARGET_RASTER_LOW 0x2cu
# define IE_TED_VIDEO_TARGET_RASTER_HIGH 0x2du
# define IE_TED_VIDEO_TARGET_ENABLE 0x2eu
# define IE_TED_VIDEO_TARGET_STATUS 0x2fu
# define IE_TED_VIDEO_TARGET_RASTER_COMPARE_LOW 0x30u
# define IE_TED_VIDEO_TARGET_RASTER_COMPARE_HIGH 0x31u
# define IE_TED_VIDEO_TARGET_RASTER_STATUS 0x32u
#else
# define IE_TED_VIDEO_TARGET_CONTROL1 IE_TED_VIDEO_CONTROL1
# define IE_TED_VIDEO_TARGET_CONTROL2 IE_TED_VIDEO_CONTROL2
# define IE_TED_VIDEO_TARGET_CHARACTER_BASE IE_TED_VIDEO_CHARACTER_BASE
# define IE_TED_VIDEO_TARGET_MATRIX_BASE IE_TED_VIDEO_MATRIX_BASE
# define IE_TED_VIDEO_TARGET_BACKGROUND0 IE_TED_VIDEO_BACKGROUND0
# define IE_TED_VIDEO_TARGET_BACKGROUND1 IE_TED_VIDEO_BACKGROUND1
# define IE_TED_VIDEO_TARGET_BACKGROUND2 IE_TED_VIDEO_BACKGROUND2
# define IE_TED_VIDEO_TARGET_BACKGROUND3 IE_TED_VIDEO_BACKGROUND3
# define IE_TED_VIDEO_TARGET_BORDER IE_TED_VIDEO_BORDER
# define IE_TED_VIDEO_TARGET_CURSOR_HIGH IE_TED_VIDEO_CURSOR_HIGH
# define IE_TED_VIDEO_TARGET_CURSOR_LOW IE_TED_VIDEO_CURSOR_LOW
# define IE_TED_VIDEO_TARGET_CURSOR_COLOUR IE_TED_VIDEO_CURSOR_COLOUR
# define IE_TED_VIDEO_TARGET_RASTER_LOW IE_TED_VIDEO_RASTER_LOW
# define IE_TED_VIDEO_TARGET_RASTER_HIGH IE_TED_VIDEO_RASTER_HIGH
# define IE_TED_VIDEO_TARGET_ENABLE IE_TED_VIDEO_ENABLE
# define IE_TED_VIDEO_TARGET_STATUS IE_TED_VIDEO_STATUS
# define IE_TED_VIDEO_TARGET_RASTER_COMPARE_LOW IE_TED_VIDEO_RASTER_COMPARE_LOW
# define IE_TED_VIDEO_TARGET_RASTER_COMPARE_HIGH IE_TED_VIDEO_RASTER_COMPARE_HIGH
# define IE_TED_VIDEO_TARGET_RASTER_STATUS IE_TED_VIDEO_RASTER_STATUS
#endif
#define IE_INPUT_TERM_OUT 0x0f0700u

/* Assembly-compatible TED video spellings. */
#define IE_TED_VIDEO_BASE IE_TED_VIDEO_BASE_DIRECT
#define IE_TED_V_CTRL1 IE_TED_VIDEO_CONTROL1
#define IE_TED_V_CTRL2 IE_TED_VIDEO_CONTROL2
#define IE_TED_V_CHAR_BASE IE_TED_VIDEO_CHARACTER_BASE
#define IE_TED_V_VIDEO_BASE IE_TED_VIDEO_MATRIX_BASE
#define IE_TED_V_BG_COLOR0 IE_TED_VIDEO_BACKGROUND0
#define IE_TED_V_BG_COLOR1 IE_TED_VIDEO_BACKGROUND1
#define IE_TED_V_BG_COLOR2 IE_TED_VIDEO_BACKGROUND2
#define IE_TED_V_BG_COLOR3 IE_TED_VIDEO_BACKGROUND3
#define IE_TED_V_BORDER IE_TED_VIDEO_BORDER
#define IE_TED_V_CURSOR_HI IE_TED_VIDEO_CURSOR_HIGH
#define IE_TED_V_CURSOR_LO IE_TED_VIDEO_CURSOR_LOW
#define IE_TED_V_CURSOR_CLR IE_TED_VIDEO_CURSOR_COLOUR
#define IE_TED_V_RASTER_LO IE_TED_VIDEO_RASTER_LOW
#define IE_TED_V_RASTER_HI IE_TED_VIDEO_RASTER_HIGH
#define IE_TED_V_ENABLE IE_TED_VIDEO_ENABLE
#define IE_TED_V_STATUS IE_TED_VIDEO_STATUS
#define IE_TED_V_CTRL1_ECM IE_TED_VIDEO_CONTROL1_ECM
#define IE_TED_V_CTRL1_BMM IE_TED_VIDEO_CONTROL1_BMM
#define IE_TED_V_CTRL1_DEN IE_TED_VIDEO_CONTROL1_ENABLE
#define IE_TED_V_CTRL1_RSEL IE_TED_VIDEO_CONTROL1_ROW_SELECT
#define IE_TED_V_CTRL1_YSCROLL IE_TED_VIDEO_CONTROL1_SCROLL_MASK
#define IE_TED_V_CTRL2_RES IE_TED_VIDEO_CONTROL2_RESET
#define IE_TED_V_CTRL2_MCM IE_TED_VIDEO_CONTROL2_MULTICOLOUR
#define IE_TED_V_CTRL2_CSEL IE_TED_VIDEO_CONTROL2_COLUMN_SELECT
#define IE_TED_V_CTRL2_XSCROLL IE_TED_VIDEO_CONTROL2_SCROLL_MASK
#define IE_TED_V_STATUS_VBLANK IE_TED_VIDEO_STATUS_VBLANK

#define IE_VIDEO_PAL_INDEX IE_VIDEO_PALETTE_INDEX
#define IE_VIDEO_PAL_DATA IE_VIDEO_PALETTE_DATA
#define IE_VIDEO_RASTER_COLOR IE_VIDEO_RASTER_COLOUR
#define IE_VIDEO_RASTER_CTRL IE_VIDEO_RASTER_CONTROL
#define IE_INPUT_TERM_STATUS 0x0f0704u
#define IE_INPUT_TERM_IN 0x0f0708u
#define IE_INPUT_TERM_LINE_STATUS 0x0f070cu
#define IE_INPUT_TERM_ECHO 0x0f0710u
#define IE_INPUT_SCAN_CODE 0x0f0740u
#define IE_INPUT_SCAN_STATUS 0x0f0744u
#define IE_INPUT_SCAN_MODIFIERS 0x0f0748u
#define IE_INPUT_MOUSE_CTRL 0x0f074cu
#define IE_INPUT_MOUSE_DX 0x0f0754u
#define IE_INPUT_MOUSE_DY 0x0f0758u
#define IE_SYSTEM_RTC_EPOCH 0x0f0750u
#define IE_SYSTEM_RTC_MONO_USEC_LO 0x0f075cu
#define IE_SYSTEM_RTC_MONO_USEC_HI 0x0f0760u
#define IE_SYSTEM_WAIT_VBLANK 0x0f2580u
#define IE_AUDIO_BASE 0x0f0800u
#define IE_AUDIO_CONTROL 0x0f0800u
#define IE_AUDIO_ENVELOPE_SHAPE 0x0f0804u
#define IE_AUDIO_FILTER_CUTOFF 0x0f0820u
#define IE_AUDIO_FILTER_RESONANCE 0x0f0824u
#define IE_AUDIO_FILTER_TYPE 0x0f0828u
#define IE_AUDIO_FILTER_OFF 0u
#define IE_AUDIO_FILTER_LOWPASS 1u
#define IE_AUDIO_FILTER_HIGHPASS 2u
#define IE_AUDIO_FILTER_BANDPASS 3u
#define IE_AUDIO_SQUARE_FREQUENCY 0x0f0900u
#define IE_AUDIO_SQUARE_VOLUME 0x0f0904u
#define IE_AUDIO_SQUARE_CONTROL 0x0f0908u
#define IE_AUDIO_TRIANGLE_FREQUENCY 0x0f0940u
#define IE_AUDIO_TRIANGLE_VOLUME 0x0f0944u
#define IE_AUDIO_TRIANGLE_CONTROL 0x0f0948u
#define IE_AUDIO_SINE_FREQUENCY 0x0f0980u
#define IE_AUDIO_SINE_VOLUME 0x0f0984u
#define IE_AUDIO_SINE_CONTROL 0x0f0988u
#define IE_AUDIO_NOISE_FREQUENCY 0x0f09c0u
#define IE_AUDIO_NOISE_VOLUME 0x0f09c4u
#define IE_AUDIO_NOISE_CONTROL 0x0f09c8u
#define IE_AUDIO_SQUARE_DUTY 0x0f090cu
#define IE_AUDIO_SQUARE_SWEEP 0x0f0910u
#define IE_AUDIO_SQUARE_PWM_CONTROL 0x0f0922u
#define IE_AUDIO_SQUARE_ATTACK 0x0f0930u
#define IE_AUDIO_SQUARE_DECAY 0x0f0934u
#define IE_AUDIO_SQUARE_SUSTAIN 0x0f0938u
#define IE_AUDIO_SQUARE_RELEASE 0x0f093cu
#define IE_AUDIO_TRIANGLE_SWEEP 0x0f0914u
#define IE_AUDIO_TRIANGLE_ATTACK 0x0f0960u
#define IE_AUDIO_TRIANGLE_DECAY 0x0f0964u
#define IE_AUDIO_TRIANGLE_SUSTAIN 0x0f0968u
#define IE_AUDIO_TRIANGLE_RELEASE 0x0f096cu
#define IE_AUDIO_SINE_SWEEP 0x0f0918u
#define IE_AUDIO_SINE_ATTACK 0x0f0990u
#define IE_AUDIO_SINE_DECAY 0x0f0994u
#define IE_AUDIO_SINE_SUSTAIN 0x0f0998u
#define IE_AUDIO_SINE_RELEASE 0x0f099cu
#define IE_AUDIO_NOISE_ATTACK 0x0f09d0u
#define IE_AUDIO_NOISE_DECAY 0x0f09d4u
#define IE_AUDIO_NOISE_SUSTAIN 0x0f09d8u
#define IE_AUDIO_NOISE_RELEASE 0x0f09dcu
#define IE_AUDIO_NOISE_MODE 0x0f09e0u
#define IE_AUDIO_SYNC_SOURCE_CH0 0x0f0a00u
#define IE_AUDIO_SYNC_SOURCE_CH1 0x0f0a04u
#define IE_AUDIO_SYNC_SOURCE_CH2 0x0f0a08u
#define IE_AUDIO_SYNC_SOURCE_CH3 0x0f0a0cu
#define IE_AUDIO_RING_MOD_SOURCE_CH0 0x0f0a10u
#define IE_AUDIO_RING_MOD_SOURCE_CH1 0x0f0a14u
#define IE_AUDIO_RING_MOD_SOURCE_CH2 0x0f0a18u
#define IE_AUDIO_RING_MOD_SOURCE_CH3 0x0f0a1cu
#define IE_AUDIO_SAW_FREQUENCY 0x0f0a20u
#define IE_AUDIO_SAW_VOLUME 0x0f0a24u
#define IE_AUDIO_SAW_CONTROL 0x0f0a28u
#define IE_AUDIO_SAW_SWEEP 0x0f0a2cu
#define IE_AUDIO_SAW_ATTACK 0x0f0a30u
#define IE_AUDIO_SAW_DECAY 0x0f0a34u
#define IE_AUDIO_SAW_SUSTAIN 0x0f0a38u
#define IE_AUDIO_SAW_RELEASE 0x0f0a3cu
#define IE_AUDIO_OVERDRIVE_CONTROL 0x0f0a40u
#define IE_AUDIO_SAW_SYNC_SOURCE 0x0f0a60u
#define IE_AUDIO_SAW_RING_MOD_SOURCE 0x0f0a64u
#define IE_AUDIO_NOISE_WHITE 0u
#define IE_AUDIO_NOISE_PERIODIC 1u
#define IE_AUDIO_NOISE_METALLIC 2u
#define IE_AUDIO_NOISE_PSG 3u

/* Custom synthesiser effects and flexible channels. */
#define IE_FLEX_STRIDE 0x40u
#define IE_FLEX_OFFSET_FREQUENCY 0x00u
#define IE_FLEX_OFFSET_VOLUME 0x04u
#define IE_FLEX_OFFSET_CONTROL 0x08u
#define IE_FLEX_OFFSET_DUTY 0x0cu
#define IE_FLEX_OFFSET_SWEEP 0x10u
#define IE_FLEX_OFFSET_ATTACK 0x14u
#define IE_FLEX_OFFSET_DECAY 0x18u
#define IE_FLEX_OFFSET_SUSTAIN 0x1cu
#define IE_FLEX_OFFSET_RELEASE 0x20u
#define IE_FLEX_OFFSET_WAVE_TYPE 0x24u
#define IE_FLEX_OFFSET_PWM_CONTROL 0x28u
#define IE_FLEX_OFFSET_NOISE_MODE 0x2cu
#define IE_FLEX_OFFSET_PHASE 0x30u
#define IE_FLEX_OFFSET_RING_MODULATION 0x34u
#define IE_FLEX_OFFSET_SYNC 0x38u
#define IE_FLEX_OFFSET_DAC 0x3cu
#define IE_FLEX_CH_BASE IE_FLEX_BASE
#define IE_FLEX_CH_STRIDE IE_FLEX_STRIDE
#define IE_FLEX_WAVE_SQUARE 0x00u
#define IE_FLEX_WAVE_TRIANGLE 0x01u
#define IE_FLEX_WAVE_SINE 0x02u
#define IE_FLEX_WAVE_NOISE 0x03u
#define IE_FLEX_WAVE_SAWTOOTH 0x04u
#define IE_AUDIO_CONTROL_ENABLE 0x01u
#define IE_AUDIO_CONTROL_FREEZE 0x02u
#define IE_AUDIO_CTRL IE_AUDIO_CONTROL
#define IE_AUDIO_ENABLE IE_AUDIO_CONTROL_ENABLE
#define IE_AUDIO_FREEZE IE_AUDIO_CONTROL_FREEZE
#define IE_FLEX_CONTROL_GATE 0x02u
#define IE_FLEX_CONTROL_ENABLE_GATE 0x03u
#define IE_FLEX_PWM_ENABLE 0x80u
#define IE_FLEX_PWM_RATE_MASK 0x7fu
#define IE_FLEX_SWEEP_ENABLE 0x80u
#define IE_FLEX_SWEEP_DIRECTION_UP 0x00u
#define IE_FLEX_SWEEP_DIRECTION_DOWN 0x08u
#define IE_FLEX_SWEEP_PERIOD_MASK 0x70u
#define IE_FLEX_SWEEP_SHIFT_MASK 0x07u

#if defined(IE_TARGET_6502) || defined(IE_TARGET_Z80)
# define IE_REVERB_MIX 0xfa50u
# define IE_REVERB_DECAY 0xfa54u
# define IE_FLEX_BASE 0xfa80u
# define IE_FLEX_END 0xfb7fu
# define IE_FLEX_CH0_BASE 0xfa80u
# define IE_FLEX_CH1_BASE 0xfac0u
# define IE_FLEX_CH2_BASE 0xfb00u
# define IE_FLEX_CH3_BASE 0xfb40u
# define IE_MIDI_PLAY_POINTER 0xfba0u
# define IE_MIDI_PLAY_LENGTH 0xfba4u
# define IE_MIDI_PLAY_CONTROL 0xfba8u
# define IE_MIDI_PLAY_STATUS 0xfbacu
# define IE_MIDI_POSITION 0xfbb0u
# define IE_MIDI_VOLUME 0xfbb4u
# define IE_MIDI_TEMPO_BPM 0xfbb8u
# define IE_MIDI_END 0xfbbfu
# define IE_MIDI_LIVE_DATA 0xfbf4u
# define IE_MIDI_LIVE_STATUS 0xfbf5u
# define IE_MIDI_LIVE_CONTROL 0xfbf6u
# define IE_MIDI_LIVE_END 0xfbf6u
#else
# define IE_REVERB_MIX 0x0f0a50u
# define IE_REVERB_DECAY 0x0f0a54u
# define IE_FLEX_BASE 0x0f0a80u
# define IE_FLEX_END 0x0f0b7fu
# define IE_FLEX_CH0_BASE 0x0f0a80u
# define IE_FLEX_CH1_BASE 0x0f0ac0u
# define IE_FLEX_CH2_BASE 0x0f0b00u
# define IE_FLEX_CH3_BASE 0x0f0b40u
# define IE_MIDI_PLAY_POINTER 0x0f0ba0u
# define IE_MIDI_PLAY_LENGTH 0x0f0ba4u
# define IE_MIDI_PLAY_CONTROL 0x0f0ba8u
# define IE_MIDI_PLAY_STATUS 0x0f0bacu
# define IE_MIDI_POSITION 0x0f0bb0u
# define IE_MIDI_VOLUME 0x0f0bb4u
# define IE_MIDI_TEMPO_BPM 0x0f0bb8u
# define IE_MIDI_END 0x0f0bbfu
# define IE_MIDI_LIVE_DATA 0x0f0bf4u
# define IE_MIDI_LIVE_STATUS 0x0f0bf5u
# define IE_MIDI_LIVE_CONTROL 0x0f0bf6u
# define IE_MIDI_LIVE_END 0x0f0bf6u
#endif

#define IE_FLEX_OFF_FREQ IE_FLEX_OFFSET_FREQUENCY
#define IE_FLEX_OFF_VOL IE_FLEX_OFFSET_VOLUME
#define IE_FLEX_OFF_CTRL IE_FLEX_OFFSET_CONTROL
#define IE_FLEX_OFF_DUTY IE_FLEX_OFFSET_DUTY
#define IE_FLEX_OFF_SWEEP IE_FLEX_OFFSET_SWEEP
#define IE_FLEX_OFF_ATK IE_FLEX_OFFSET_ATTACK
#define IE_FLEX_OFF_DEC IE_FLEX_OFFSET_DECAY
#define IE_FLEX_OFF_SUS IE_FLEX_OFFSET_SUSTAIN
#define IE_FLEX_OFF_REL IE_FLEX_OFFSET_RELEASE
#define IE_FLEX_OFF_WAVE_TYPE IE_FLEX_OFFSET_WAVE_TYPE
#define IE_FLEX_OFF_PWM_CTRL IE_FLEX_OFFSET_PWM_CONTROL
#define IE_FLEX_OFF_NOISEMODE IE_FLEX_OFFSET_NOISE_MODE
#define IE_FLEX_OFF_PHASE IE_FLEX_OFFSET_PHASE
#define IE_FLEX_OFF_RINGMOD IE_FLEX_OFFSET_RING_MODULATION
#define IE_FLEX_OFF_SYNC IE_FLEX_OFFSET_SYNC
#define IE_FLEX_OFF_DAC IE_FLEX_OFFSET_DAC
#define IE_MIDI_PLAY_PTR IE_MIDI_PLAY_POINTER
#define IE_MIDI_PLAY_LEN IE_MIDI_PLAY_LENGTH
#define IE_MIDI_PLAY_CTRL IE_MIDI_PLAY_CONTROL
#define IE_MIDI_STATUS_BUSY 0x01u
#define IE_MIDI_STATUS_ERROR 0x02u
#define IE_MIDI_STATUS_PAUSED 0x04u
#define IE_MIDI_STATUS_LOADING 0x08u
#define IE_MIDI_CONTROL_START 0x01u
#define IE_MIDI_CONTROL_STOP 0x02u
#define IE_MIDI_CONTROL_LOOP 0x04u
#define IE_MIDI_CONTROL_PAUSE 0x08u
#define IE_MIDI_CONTROL_START_LOOP 0x05u
#define IE_MIDI_LIVE_STATUS_ACTIVE 0x01u
#define IE_MIDI_LIVE_CONTROL_RESET 0x01u
#define IE_MIDI_LIVE_CTRL IE_MIDI_LIVE_CONTROL
#define IE_MIDI_LIVE_CTRL_RESET IE_MIDI_LIVE_CONTROL_RESET

/* Additional media players and dedicated sound chips. */
#define IE_AHX_BASE 0x0f0b80u
#define IE_AHX_PLUS_CONTROL 0x0f0b80u
#define IE_AHX_PLAY_POINTER 0x0f0b84u
#define IE_AHX_PLAY_LENGTH 0x0f0b88u
#define IE_AHX_PLAY_CONTROL 0x0f0b8cu
#define IE_AHX_PLAY_STATUS 0x0f0b90u
#define IE_AHX_SUBSONG 0x0f0b91u
#define IE_MOD_PLAY_POINTER 0x0f0bc0u
#define IE_MOD_PLAY_LENGTH 0x0f0bc4u
#define IE_MOD_PLAY_CONTROL 0x0f0bc8u
#define IE_MOD_PLAY_STATUS 0x0f0bccu
#define IE_MOD_FILTER_MODEL 0x0f0bd0u
#define IE_MOD_POSITION 0x0f0bd4u
#define IE_MOD_END 0x0f0bd7u
#define IE_WAV_PLAY_POINTER 0x0f0bd8u
#define IE_WAV_PLAY_LENGTH 0x0f0bdcu
#define IE_WAV_PLAY_CONTROL 0x0f0be0u
#define IE_WAV_PLAY_STATUS 0x0f0be4u
#define IE_WAV_POSITION 0x0f0be8u
#define IE_WAV_PLAY_POINTER_HIGH 0x0f0becu
#define IE_WAV_CHANNEL_BASE 0x0f0bf0u
#define IE_WAV_VOLUME_LEFT 0x0f0bf1u
#define IE_WAV_VOLUME_RIGHT 0x0f0bf2u
#define IE_WAV_FLAGS 0x0f0bf3u
#define IE_WAV_END 0x0f0bf3u
#define IE_PSG_BASE 0x0f0c00u
#define IE_PSG_END 0x0f0c0fu
#define IE_PSG_PLAY_POINTER 0x0f0c10u
#define IE_PSG_PLAY_LENGTH 0x0f0c14u
#define IE_PSG_PLAY_CONTROL 0x0f0c18u
#define IE_PSG_PLAY_STATUS 0x0f0c1cu
#define IE_PSG_PLUS_CONTROL 0x0f0c20u
#define IE_POKEY_BASE 0x0f0d00u
#define IE_POKEY_CHANNEL1_FREQUENCY 0x0f0d00u
#define IE_POKEY_CHANNEL1_CONTROL 0x0f0d01u
#define IE_POKEY_CHANNEL2_FREQUENCY 0x0f0d02u
#define IE_POKEY_CHANNEL2_CONTROL 0x0f0d03u
#define IE_POKEY_CHANNEL3_FREQUENCY 0x0f0d04u
#define IE_POKEY_CHANNEL3_CONTROL 0x0f0d05u
#define IE_POKEY_CHANNEL4_FREQUENCY 0x0f0d06u
#define IE_POKEY_CHANNEL4_CONTROL 0x0f0d07u
#define IE_POKEY_AUDIO_CONTROL 0x0f0d08u
#define IE_POKEY_PLUS_CONTROL 0x0f0d09u
#define IE_POKEY_RANDOM 0x0f0d0au
#define IE_SID_BASE 0x0f0e00u
#define IE_SID_VOICE1_FREQUENCY_LOW 0x0f0e00u
#define IE_SID_VOICE1_CONTROL 0x0f0e04u
#define IE_SID_VOICE2_FREQUENCY_LOW 0x0f0e07u
#define IE_SID_VOICE2_CONTROL 0x0f0e0bu
#define IE_SID_VOICE3_FREQUENCY_LOW 0x0f0e0eu
#define IE_SID_VOICE3_CONTROL 0x0f0e12u
#define IE_SID_VOICE1_FREQUENCY_HIGH 0x0f0e01u
#define IE_SID_VOICE1_PULSE_WIDTH_LOW 0x0f0e02u
#define IE_SID_VOICE1_PULSE_WIDTH_HIGH 0x0f0e03u
#define IE_SID_VOICE1_ATTACK_DECAY 0x0f0e05u
#define IE_SID_VOICE1_SUSTAIN_RELEASE 0x0f0e06u
#define IE_SID_VOICE2_FREQUENCY_HIGH 0x0f0e08u
#define IE_SID_VOICE2_PULSE_WIDTH_LOW 0x0f0e09u
#define IE_SID_VOICE2_PULSE_WIDTH_HIGH 0x0f0e0au
#define IE_SID_VOICE2_ATTACK_DECAY 0x0f0e0cu
#define IE_SID_VOICE2_SUSTAIN_RELEASE 0x0f0e0du
#define IE_SID_VOICE3_FREQUENCY_HIGH 0x0f0e0fu
#define IE_SID_VOICE3_PULSE_WIDTH_LOW 0x0f0e10u
#define IE_SID_VOICE3_PULSE_WIDTH_HIGH 0x0f0e11u
#define IE_SID_VOICE3_ATTACK_DECAY 0x0f0e13u
#define IE_SID_VOICE3_SUSTAIN_RELEASE 0x0f0e14u
#define IE_SID_FILTER_CUTOFF_LOW 0x0f0e15u
#define IE_SID_FILTER_CUTOFF_HIGH 0x0f0e16u
#define IE_SID_FILTER_RESONANCE 0x0f0e17u
#define IE_SID_FILTER_MODE_VOLUME 0x0f0e18u
#define IE_SID_PULSE_WIDTH_MODULATION 0x0f0e19u
#define IE_SID_RING_MODULATION 0x0f0e1au
#define IE_SID_SYNC 0x0f0e1bu
#define IE_SID_MODE 0x0f0e1cu
#define IE_TED_AUDIO_BASE 0x0f0f00u
#define IE_TED_AUDIO_FREQUENCY1_LOW 0x0f0f00u
#define IE_TED_AUDIO_FREQUENCY2_LOW 0x0f0f01u
#define IE_TED_AUDIO_FREQUENCY2_HIGH 0x0f0f02u
#define IE_TED_AUDIO_CONTROL 0x0f0f03u
#define IE_TED_AUDIO_FREQUENCY1_HIGH 0x0f0f04u
#define IE_TED_AUDIO_PLUS_CONTROL 0x0f0f05u
#define IE_TED_AUDIO_CONTROL_DIRECT_DAC 0x80u
#define IE_TED_AUDIO_CONTROL_VOICE2_NOISE 0x40u
#define IE_TED_AUDIO_CONTROL_VOICE2_ENABLE 0x20u
#define IE_TED_AUDIO_CONTROL_VOICE1_ENABLE 0x10u
#define IE_TED_AUDIO_CONTROL_VOLUME_MASK 0x0fu
#define IE_Z80_PSG_PORT_SELECT 0xf0u
#define IE_Z80_PSG_PORT_DATA 0xf1u
#define IE_Z80_POKEY_PORT_SELECT 0xd0u
#define IE_Z80_POKEY_PORT_DATA 0xd1u
#define IE_Z80_TED_AUDIO_PORT_SELECT 0xf2u
#define IE_Z80_TED_AUDIO_PORT_DATA 0xf3u
#if defined(IE_TARGET_6502) || defined(IE_TARGET_Z80)
# define IE_AHX_TARGET_BASE 0xfb80u
# define IE_MOD_TARGET_BASE 0xfbc0u
# define IE_WAV_TARGET_BASE 0xfbd8u
# define IE_PSG_TARGET_BASE 0xfc00u
# define IE_POKEY_TARGET_BASE 0xfd00u
# define IE_SID_TARGET_BASE 0xfe00u
# define IE_TED_AUDIO_TARGET_BASE 0xff00u
#else
# define IE_AHX_TARGET_BASE IE_AHX_BASE
# define IE_MOD_TARGET_BASE IE_MOD_PLAY_POINTER
# define IE_WAV_TARGET_BASE IE_WAV_PLAY_POINTER
# define IE_PSG_TARGET_BASE IE_PSG_BASE
# define IE_POKEY_TARGET_BASE IE_POKEY_BASE
# define IE_SID_TARGET_BASE IE_SID_BASE
# define IE_TED_AUDIO_TARGET_BASE IE_TED_AUDIO_BASE
#endif

/* Target access aliases for the player control blocks. */
#if defined(IE_TARGET_6502) || defined(IE_TARGET_Z80)
# define IE_AHX_TARGET_PLUS_CONTROL 0xfb80u
# define IE_AHX_TARGET_PLAY_POINTER 0xfb84u
# define IE_AHX_TARGET_PLAY_LENGTH 0xfb88u
# define IE_AHX_TARGET_PLAY_CONTROL 0xfb8cu
# define IE_AHX_TARGET_PLAY_STATUS 0xfb90u
# define IE_AHX_TARGET_SUBSONG 0xfb91u
# define IE_MOD_TARGET_PLAY_POINTER 0xfbc0u
# define IE_MOD_TARGET_PLAY_LENGTH 0xfbc4u
# define IE_MOD_TARGET_PLAY_CONTROL 0xfbc8u
# define IE_MOD_TARGET_PLAY_STATUS 0xfbccu
# define IE_MOD_TARGET_FILTER_MODEL 0xfbd0u
# define IE_MOD_TARGET_POSITION 0xfbd4u
# define IE_WAV_TARGET_PLAY_POINTER 0xfbd8u
# define IE_WAV_TARGET_PLAY_LENGTH 0xfbdcu
# define IE_WAV_TARGET_PLAY_CONTROL 0xfbe0u
# define IE_WAV_TARGET_PLAY_STATUS 0xfbe4u
# define IE_WAV_TARGET_CHANNEL_BASE 0xfbf0u
# define IE_WAV_TARGET_FLAGS 0xfbf3u
# define IE_PSG_TARGET_PLAY_POINTER 0xfc10u
# define IE_PSG_TARGET_PLAY_LENGTH 0xfc14u
# define IE_PSG_TARGET_PLAY_CONTROL 0xfc18u
# define IE_PSG_TARGET_PLUS_CONTROL 0xfc20u
#else
# define IE_AHX_TARGET_PLUS_CONTROL IE_AHX_PLUS_CONTROL
# define IE_AHX_TARGET_PLAY_POINTER IE_AHX_PLAY_POINTER
# define IE_AHX_TARGET_PLAY_LENGTH IE_AHX_PLAY_LENGTH
# define IE_AHX_TARGET_PLAY_CONTROL IE_AHX_PLAY_CONTROL
# define IE_AHX_TARGET_PLAY_STATUS IE_AHX_PLAY_STATUS
# define IE_AHX_TARGET_SUBSONG IE_AHX_SUBSONG
# define IE_MOD_TARGET_PLAY_POINTER IE_MOD_PLAY_POINTER
# define IE_MOD_TARGET_PLAY_LENGTH IE_MOD_PLAY_LENGTH
# define IE_MOD_TARGET_PLAY_CONTROL IE_MOD_PLAY_CONTROL
# define IE_MOD_TARGET_PLAY_STATUS IE_MOD_PLAY_STATUS
# define IE_MOD_TARGET_FILTER_MODEL IE_MOD_FILTER_MODEL
# define IE_MOD_TARGET_POSITION IE_MOD_POSITION
# define IE_WAV_TARGET_PLAY_POINTER IE_WAV_PLAY_POINTER
# define IE_WAV_TARGET_PLAY_LENGTH IE_WAV_PLAY_LENGTH
# define IE_WAV_TARGET_PLAY_CONTROL IE_WAV_PLAY_CONTROL
# define IE_WAV_TARGET_PLAY_STATUS IE_WAV_PLAY_STATUS
# define IE_WAV_TARGET_CHANNEL_BASE IE_WAV_CHANNEL_BASE
# define IE_WAV_TARGET_FLAGS IE_WAV_FLAGS
# define IE_PSG_TARGET_PLAY_POINTER IE_PSG_PLAY_POINTER
# define IE_PSG_TARGET_PLAY_LENGTH IE_PSG_PLAY_LENGTH
# define IE_PSG_TARGET_PLAY_CONTROL IE_PSG_PLAY_CONTROL
# define IE_PSG_TARGET_PLUS_CONTROL IE_PSG_PLUS_CONTROL
#endif

/* SFX trigger channels and SAP player controls. */
#define IE_SFX_CH_BASE 0x0f0e80u
#define IE_SFX_CH_STRIDE 0x20u
#define IE_SFX_LEGACY_CHANNELS 4u
#define IE_SFX_CHANNELS 32u
#define IE_SFX_CH0_BASE 0x0f0e80u
#define IE_SFX_CH1_BASE 0x0f0ea0u
#define IE_SFX_CH2_BASE 0x0f0ec0u
#define IE_SFX_CH3_BASE 0x0f0ee0u
#define IE_SFX_EXT_REGION_BASE 0x0f2600u
#define IE_SFX_EXT_REGION_END 0x0f29ffu
#define IE_SFX_EXT_CH_BASE 0x0f2600u
#define IE_SFX_EXT_CH0_BASE 0x0f2600u
#define IE_SFX_EXT_CH31_BASE 0x0f29e0u
#define IE_SFX_PTR 0x00u
#define IE_SFX_LEN 0x04u
#define IE_SFX_LOOP_PTR 0x08u
#define IE_SFX_LOOP_LEN 0x0cu
#define IE_SFX_FREQ 0x10u
#define IE_SFX_VOL 0x14u
#define IE_SFX_PAN_RESERVED 0x16u
#define IE_SFX_FORMAT 0x18u
#define IE_SFX_CTRL 0x1cu
#define IE_SFX_FORMAT_SIGNED8 0u
#define IE_SFX_FORMAT_UNSIGNED8 1u
#define IE_SFX_FORMAT_SIGNED16 2u
#define IE_SFX_CTRL_TRIGGER 0x01u
#define IE_SFX_CTRL_STOP 0x02u
#define IE_SFX_CTRL_LOOP_EN 0x04u
#define IE_SFX_STATUS_PLAYING 0x01u
#define IE_SFX_STATUS_ERROR 0x02u
#define IE_SAP_PLAY_PTR 0x0f0d10u
#define IE_SAP_PLAY_LEN 0x0f0d14u
#define IE_SAP_PLAY_CTRL 0x0f0d18u
#define IE_SAP_PLAY_STATUS 0x0f0d1cu
#define IE_SAP_SUBSONG 0x0f0d20u
#define IE_SN_PORT_WRITE 0x0f0c30u
#define IE_SN_PORT_READY 0x0f0c31u
#define IE_SN_PORT_MODE 0x0f0c32u
#define IE_SN76489_MODE_LFSR_15 0u
#define IE_SN76489_MODE_LFSR_16 1u
#define IE_Z80_SN_PORT_DATA 0xe4u
#define IE_Z80_SN_PORT_STATUS 0xe5u
#if defined(IE_TARGET_6502)
# define IE_SFX_TARGET_CH_BASE 0xfe80u
# define IE_SFX_TARGET_EXT_BANK 0x79u
# define IE_SFX_TARGET_EXT_WINDOW_BASE 0x2600u
# define IE_SFX_TARGET_EXT_WINDOW_END 0x29ffu
# define IE_SN_TARGET_PORT_WRITE 0xfc30u
# define IE_SN_TARGET_PORT_READY 0xfc31u
# define IE_SN_TARGET_PORT_MODE 0xfc32u
#elif defined(IE_TARGET_Z80)
# define IE_SFX_TARGET_CH_BASE IE_SFX_CH_BASE
# define IE_SFX_TARGET_EXT_BANK 0x79u
# define IE_SFX_TARGET_EXT_WINDOW_BASE 0x2600u
# define IE_SFX_TARGET_EXT_WINDOW_END 0x29ffu
# define IE_SN_TARGET_PORT_WRITE IE_SN_PORT_WRITE
# define IE_SN_TARGET_PORT_READY IE_SN_PORT_READY
# define IE_SN_TARGET_PORT_MODE IE_SN_PORT_MODE
#else
# define IE_SFX_TARGET_CH_BASE IE_SFX_CH_BASE
# define IE_SN_TARGET_PORT_WRITE IE_SN_PORT_WRITE
# define IE_SN_TARGET_PORT_READY IE_SN_PORT_READY
# define IE_SN_TARGET_PORT_MODE IE_SN_PORT_MODE
#endif

/* Input, clock and gamepad blocks. Eight-bit targets use bank 1 aliases. */
#define IE_GAMEPAD_REGION_BASE 0x0f25c0u
#define IE_GAMEPAD_REGION_END 0x0f25ffu
#define IE_GAMEPAD_STATUS 0x0f25c0u
#define IE_GAMEPAD_PAD0_BASE 0x0f25d0u
#define IE_GAMEPAD_PAD_STRIDE 0x0cu
#define IE_GAMEPAD_BUTTONS_OFFSET 0x00u
#define IE_GAMEPAD_LEFT_AXIS_OFFSET 0x04u
#define IE_GAMEPAD_RIGHT_AXIS_OFFSET 0x08u
#define IE_GAMEPAD_IO_BANK 0x79u
#define IE_JOY_UP 0x00000001u
#define IE_JOY_DOWN 0x00000002u
#define IE_JOY_LEFT 0x00000004u
#define IE_JOY_RIGHT 0x00000008u
#define IE_JOY_A 0x00000010u
#define IE_JOY_B 0x00000020u
#define IE_JOY_X 0x00000040u
#define IE_JOY_Y 0x00000080u
#define IE_JOY_LB 0x00000100u
#define IE_JOY_RB 0x00000200u
#define IE_JOY_LT 0x00000400u
#define IE_JOY_RT 0x00000800u
#define IE_JOY_SELECT 0x00001000u
#define IE_JOY_START 0x00002000u
#define IE_JOY_L3 0x00004000u
#define IE_JOY_R3 0x00008000u
#define IE_JOY_HOME 0x00010000u
#define IE_GAMEPAD_BUTTONS_OFF IE_GAMEPAD_BUTTONS_OFFSET
#define IE_GAMEPAD_AXIS_LXY_OFF IE_GAMEPAD_LEFT_AXIS_OFFSET
#define IE_GAMEPAD_AXIS_RXY_OFF IE_GAMEPAD_RIGHT_AXIS_OFFSET
#if defined(IE_TARGET_6502) || defined(IE_TARGET_Z80)
# define IE_TERM_IO_BANK 0x78u
# define IE_HOST_SOCKET_BANK 0x79u
# define IE_INPUT_TERM_OUT_TARGET 0x2700u
# define IE_INPUT_TERM_STATUS_TARGET 0x2704u
# define IE_INPUT_TERM_IN_TARGET 0x2708u
# define IE_INPUT_SCAN_CODE_TARGET 0x2740u
# define IE_INPUT_SCAN_STATUS_TARGET 0x2744u
# define IE_INPUT_SCAN_MODIFIERS_TARGET 0x2748u
# define IE_INPUT_RTC_EPOCH_TARGET 0x2750u
# define IE_GAMEPAD_STATUS_TARGET 0x25c0u
# define IE_GAMEPAD_PAD0_TARGET 0x25d0u
#else
# define IE_INPUT_TERM_OUT_TARGET IE_INPUT_TERM_OUT
# define IE_INPUT_TERM_STATUS_TARGET IE_INPUT_TERM_STATUS
# define IE_INPUT_TERM_IN_TARGET IE_INPUT_TERM_IN
# define IE_INPUT_SCAN_CODE_TARGET IE_INPUT_SCAN_CODE
# define IE_INPUT_SCAN_STATUS_TARGET IE_INPUT_SCAN_STATUS
# define IE_INPUT_SCAN_MODIFIERS_TARGET IE_INPUT_SCAN_MODIFIERS
# define IE_INPUT_RTC_EPOCH_TARGET IE_SYSTEM_RTC_EPOCH
# define IE_GAMEPAD_STATUS_TARGET IE_GAMEPAD_STATUS
# define IE_GAMEPAD_PAD0_TARGET IE_GAMEPAD_PAD0_BASE
#endif
#define IE_FILE_BASE 0x0f2200u
#define IE_FILE_END 0x0f221fu
#define IE_FILE_NAME_POINTER 0x0f2200u
#define IE_FILE_DATA_POINTER 0x0f2204u
#define IE_FILE_DATA_LENGTH 0x0f2208u
#define IE_FILE_CONTROL 0x0f220cu
#define IE_FILE_STATUS 0x0f2210u
#define IE_FILE_RESULT_LENGTH 0x0f2214u
#define IE_FILE_ERROR_CODE 0x0f2218u
#define IE_FILE_READ_MAX 0x0f221cu
#define IE_FILE_DATA_POINTER64 0x0f22b0u
#define IE_FILE_DATA_POINTER64_END 0x0f22b7u
#define IE_FILE_OP_READ 0x01u
#define IE_FILE_OP_WRITE 0x02u
#define IE_FILE_OP_LIST 0x03u
#define IE_FILE_ERROR_OK 0x00u
#define IE_FILE_ERROR_NOT_FOUND 0x01u
#define IE_FILE_ERROR_PERMISSION 0x02u
#define IE_FILE_ERROR_PATH_TRAVERSAL 0x03u
#define IE_FILE_ERROR_RANGE 0x04u
#define IE_FILE_NAME_PTR IE_FILE_NAME_POINTER
#define IE_FILE_DATA_PTR IE_FILE_DATA_POINTER
#define IE_FILE_DATA_LEN IE_FILE_DATA_LENGTH
#define IE_FILE_CTRL IE_FILE_CONTROL
#define IE_FILE_RESULT_LEN IE_FILE_RESULT_LENGTH
#define IE_FILE_ERROR IE_FILE_ERROR_CODE
#define IE_COPROC_BASE 0x0f2340u
#define IE_COPROC_END 0x0f238fu
#define IE_COPROC_COMMAND 0x0f2340u
#define IE_COPROC_CPU_TYPE 0x0f2344u
#define IE_COPROC_COMMAND_STATUS 0x0f2348u
#define IE_COPROC_COMMAND_ERROR 0x0f234cu
#define IE_COPROC_TICKET 0x0f2350u
#define IE_COPROC_TICKET_STATUS 0x0f2354u
#define IE_COPROC_OPERATION 0x0f2358u
#define IE_COPROC_REQUEST_POINTER 0x0f235cu
#define IE_COPROC_REQUEST_LENGTH 0x0f2360u
#define IE_COPROC_RESPONSE_POINTER 0x0f2364u
#define IE_COPROC_RESPONSE_CAPACITY 0x0f2368u
#define IE_COPROC_TIMEOUT 0x0f236cu
#define IE_COPROC_NAME_POINTER 0x0f2370u
#define IE_COPROC_WORKER_STATE 0x0f2374u
#define IE_COPROC_STATS_OPERATIONS 0x0f2378u
#define IE_COPROC_STATS_BYTES 0x0f237cu
#define IE_COPROC_IRQ_CONTROL 0x0f2380u
#define IE_COPROC_DISPATCH_OVERHEAD 0x0f2384u
#define IE_COPROC_COMPLETED_TICKET 0x0f2388u
#define IE_COPROC_EXT_BASE 0x0f23b0u
#define IE_COPROC_RING_DEPTH 0x0f23b0u
#define IE_COPROC_WORKER_UPTIME 0x0f23b4u
#define IE_COPROC_STATS_RESET 0x0f23b8u
#define IE_COPROC_BUSY_PERCENT 0x0f23bcu
#define IE_COPROC_EXT_END 0x0f23bfu
#define IE_COPROC_EXT2_BASE 0x0f25a0u
#define IE_COPROC_INSTANCE_LIMIT 0x0f25a0u
#define IE_COPROC_SELECTED_STATE 0x0f25a4u
#define IE_COPROC_MAILBOX_VERSION 0x0f25a8u
#define IE_COPROC_WORKER_BASE 0x0f25acu
#define IE_COPROC_WORKER_END 0x0f25b0u
#define IE_COPROC_WORKER_RING 0x0f25b4u
#define IE_COPROC_INSTANCE_STATE 0x0f25b8u
#define IE_COPROC_EXT2_END 0x0f25bfu
#define IE_COPROC_COMMAND_START 0x01u
#define IE_COPROC_COMMAND_STOP 0x02u
#define IE_COPROC_COMMAND_ENQUEUE 0x03u
#define IE_COPROC_COMMAND_POLL 0x04u
#define IE_COPROC_COMMAND_WAIT 0x05u
#define IE_COPROC_COMMAND_START_MEMORY 0x06u
#define IE_COPROC_CMD IE_COPROC_COMMAND
#define IE_COPROC_OP IE_COPROC_OPERATION
#define IE_COPROC_REQ_PTR IE_COPROC_REQUEST_POINTER
#define IE_COPROC_REQ_LEN IE_COPROC_REQUEST_LENGTH
#define IE_COPROC_RESP_PTR IE_COPROC_RESPONSE_POINTER
#define IE_COPROC_RESP_CAP IE_COPROC_RESPONSE_CAPACITY
#define IE_COPROC_STATUS_OK 0x00u
#define IE_COPROC_STATUS_ERROR 0x01u
#define IE_COPROC_TICKET_PENDING 0x00u
#define IE_COPROC_TICKET_RUNNING 0x01u
#define IE_COPROC_TICKET_OK 0x02u
#define IE_COPROC_TICKET_ERROR 0x03u
#define IE_COPROC_TICKET_TIMEOUT 0x04u
#define IE_COPROC_TICKET_WORKER_DOWN 0x05u
#define IE_COPROC_GATEWAY_BASE 0xf200u
#define IE_COPROC_GATEWAY_END 0xf24fu
#define IE_EXEC_NAME_POINTER 0x0f2320u
#define IE_EXEC_CONTROL 0x0f2324u
#define IE_EXEC_STATUS 0x0f2328u
#define IE_EXEC_TYPE 0x0f232cu
#define IE_EXEC_ERROR 0x0f2330u
#define IE_EXEC_SESSION 0x0f2334u
#define IE_EXEC_END 0x0f233fu
#define IE_EXEC_OP_EXECUTE 0x01u
#define IE_EXEC_OP_EMUTOS 0x02u
#define IE_EXEC_OP_AROS 0x03u
#define IE_EXEC_OP_IEXEC 0x04u
#define IE_EXEC_OP_HARD_RESET 0x05u
#define IE_EXEC_STATUS_IDLE 0x00u
#define IE_EXEC_STATUS_LOADING 0x01u
#define IE_EXEC_STATUS_RUNNING 0x02u
#define IE_EXEC_STATUS_ERROR 0x03u
#define IE_EXEC_TYPE_NONE 0x00u
#define IE_EXEC_TYPE_IE32 0x01u
#define IE_EXEC_TYPE_IE64 0x02u
#define IE_EXEC_TYPE_6502 0x03u
#define IE_EXEC_TYPE_M68K 0x04u
#define IE_EXEC_TYPE_Z80 0x05u
#define IE_EXEC_TYPE_X86 0x06u
#define IE_EXEC_TYPE_EMUTOS 0x07u
#define IE_EXEC_TYPE_SCRIPT 0x08u
#define IE_EXEC_TYPE_AROS 0x09u
#define IE_EXEC_TYPE_IEXEC 0x0au
#define IE_EXEC_ERROR_OK 0x00u
#define IE_EXEC_ERROR_NOT_FOUND 0x01u
#define IE_EXEC_ERROR_UNSUPPORTED 0x02u
#define IE_EXEC_ERROR_PATH_INVALID 0x03u
#define IE_EXEC_ERROR_LOAD_FAILED 0x04u
#define IE_EXEC_NAME_PTR IE_EXEC_NAME_POINTER
#define IE_EXEC_CTRL IE_EXEC_CONTROL
#define IE_EXEC_ERR IE_EXEC_ERROR
#define IE_EXEC_ERR_OK IE_EXEC_ERROR_OK
#define IE_EXEC_ERR_NOT_FOUND IE_EXEC_ERROR_NOT_FOUND
#define IE_EXEC_ERR_UNSUPPORTED IE_EXEC_ERROR_UNSUPPORTED
#define IE_EXEC_ERR_PATH_INVALID IE_EXEC_ERROR_PATH_INVALID
#define IE_EXEC_ERR_LOAD_FAILED IE_EXEC_ERROR_LOAD_FAILED
#define IE_VOODOO_BASE 0x0f8000u
#define IE_VOODOO_END 0x0f87ffu
#define IE_VOODOO_STATUS 0x0f8000u
#define IE_VOODOO_ENABLE 0x0f8004u
#define IE_VOODOO_VERTEX_AX 0x0f8008u
#define IE_VOODOO_VERTEX_AY 0x0f800cu
#define IE_VOODOO_VERTEX_BX 0x0f8010u
#define IE_VOODOO_VERTEX_BY 0x0f8014u
#define IE_VOODOO_VERTEX_CX 0x0f8018u
#define IE_VOODOO_VERTEX_CY 0x0f801cu
#define IE_VOODOO_TRIANGLE_COMMAND 0x0f8080u
#define IE_VOODOO_FAST_FILL_COMMAND 0x0f8124u
#define IE_VOODOO_SWAP_BUFFER_COMMAND 0x0f8128u
#define IE_VOODOO_TEXTURE_MODE 0x0f8300u
#define IE_VOODOO_TEXTURE_BASE0 0x0f830cu
#define IE_VOODOO_TEXTURE_WIDTH 0x0f8330u
#define IE_VOODOO_TEXTURE_HEIGHT 0x0f8334u
#define IE_VOODOO_TRIANGLE_CMD IE_VOODOO_TRIANGLE_COMMAND
#define IE_VOODOO_FAST_FILL_CMD IE_VOODOO_FAST_FILL_COMMAND
#define IE_VOODOO_SWAP_BUFFER_CMD IE_VOODOO_SWAP_BUFFER_COMMAND
#define IE_VOODOO_TEX_MODE IE_VOODOO_TEXTURE_MODE
#define IE_VOODOO_TEX_BASE0 IE_VOODOO_TEXTURE_BASE0
#define IE_VOODOO_COLOR_SELECT 0x0f8088u
#define IE_VOODOO_FBZCOLOR_PATH 0x0f8104u
#define IE_VOODOO_ALPHA_MODE 0x0f810cu
#define IE_VOODOO_NOP_CMD 0x0f8120u
#define IE_VOODOO_VIDEO_DIM 0x0f8214u
#define IE_VOODOO_TEX_SRC_PTR 0x0f8348u
#define IE_VOODOO_TEX_SRC_BYTES 0x0f834cu
#define IE_VOODOO_TEX_SLOT 0x0f8350u
#define IE_VOODOO_TEX_BIND 0x0f8354u
#define IE_VOODOO_TEX_SLOT_NONE 0xffffffffu
#define IE_VOODOO_PALETTE_BASE 0x0f8400u
#define IE_VOODOO_COMMAND_POINTER 0x0f833cu
#define IE_VOODOO_COMMAND_COUNT 0x0f8340u
#define IE_VOODOO_COMMAND_SUBMIT 0x0f8344u
#define IE_VOODOO_COMMAND_SUBMIT_REPLAY 0x00000001u
#define IE_VOODOO_COMMAND_SUBMIT_REPLAY_LE 0x00000002u
#define IE_VOODOO_STATUS_FBI_BUSY 0x01u
#define IE_VOODOO_STATUS_TMU_BUSY 0x02u
#define IE_VOODOO_STATUS_SST_BUSY 0x04u
#define IE_VOODOO_STATUS_VRETRACE 0x40u
#define IE_VOODOO_STATUS_SWAPBUF 0x80u
#define IE_VOODOO_FBZ_CLIPPING 0x0001u
#define IE_VOODOO_FBZ_CHROMAKEY 0x0002u
#define IE_VOODOO_FBZ_DEPTH_ENABLE 0x0010u
#define IE_VOODOO_FBZ_DEPTH_FUNC 0x00e0u
#define IE_VOODOO_FBZ_RGB_WRITE 0x0200u
#define IE_VOODOO_FBZ_DEPTH_WRITE 0x0400u
#define IE_VOODOO_ALPHA_TEST_EN 0x0001u
#define IE_VOODOO_ALPHA_FUNC_MASK 0x000eu
#define IE_VOODOO_ALPHA_FUNC_SHIFT 1u
#define IE_VOODOO_ALPHA_BLEND_EN 0x0010u
#define IE_VOODOO_ALPHA_SRC_RGB 0x0f00u
#define IE_VOODOO_ALPHA_DST_RGB 0xf000u
#define IE_VOODOO_ALPHA_REF_SHIFT 24u
#define IE_VOODOO_SWAP_VSYNC 0x01u
#define IE_VOODOO_SWAP_CLEAR 0x02u
#define IE_VOODOO_TEX_ENABLE 0x0001u
#define IE_VOODOO_TEX_MINIFY 0x000eu
#define IE_VOODOO_TEX_MAGNIFY 0x0010u
#define IE_VOODOO_TEX_CLAMP_S 0x0020u
#define IE_VOODOO_TEX_CLAMP_T 0x0040u
#define IE_VOODOO_TEX_FORMAT 0x0f00u
#define IE_VOODOO_TEX_FORMAT_SHIFT 0x08u
#define IE_VOODOO_TEX_FMT_PALETTE 0u
#define IE_VOODOO_TEX_FMT_P8 5u
#define IE_VOODOO_TEX_FMT_ARGB1555 8u
#define IE_VOODOO_TEX_FMT_ARGB4444 9u
#define IE_VOODOO_TEX_FMT_ARGB8888 10u
#define IE_VOODOO_CLIP_MASK 0xffffu
#define IE_VOODOO_FIXED_12_4 4u
#define IE_VOODOO_FIXED_12_12 12u
#define IE_VOODOO_START_R 0x0f8020u
#define IE_VOODOO_START_G 0x0f8024u
#define IE_VOODOO_START_B 0x0f8028u
#define IE_VOODOO_START_Z 0x0f802cu
#define IE_VOODOO_START_A 0x0f8030u
#define IE_VOODOO_START_S 0x0f8034u
#define IE_VOODOO_START_T 0x0f8038u
#define IE_VOODOO_START_W 0x0f803cu
#define IE_VOODOO_DRDX 0x0f8040u
#define IE_VOODOO_DGDX 0x0f8044u
#define IE_VOODOO_DBDX 0x0f8048u
#define IE_VOODOO_DZDX 0x0f804cu
#define IE_VOODOO_DADX 0x0f8050u
#define IE_VOODOO_DSDX 0x0f8054u
#define IE_VOODOO_DTDX 0x0f8058u
#define IE_VOODOO_DWDX 0x0f805cu
#define IE_VOODOO_DRDY 0x0f8060u
#define IE_VOODOO_DGDY 0x0f8064u
#define IE_VOODOO_DBDY 0x0f8068u
#define IE_VOODOO_DZDY 0x0f806cu
#define IE_VOODOO_DADY 0x0f8070u
#define IE_VOODOO_DSDY 0x0f8074u
#define IE_VOODOO_DTDY 0x0f8078u
#define IE_VOODOO_DWDY 0x0f807cu
#define IE_VOODOO_FTRIANGLE_COMMAND 0x0f8084u
#define IE_VOODOO_COLOUR_SELECT 0x0f8088u
#define IE_VOODOO_FBZ_COLOUR_PATH 0x0f8104u
#define IE_VOODOO_FOG_MODE 0x0f8108u
#define IE_VOODOO_ALPHA_MODE 0x0f810cu
#define IE_VOODOO_FBZ_MODE 0x0f8110u
#define IE_VOODOO_LFB_MODE 0x0f8114u
#define IE_VOODOO_CLIP_LEFT_RIGHT 0x0f8118u
#define IE_VOODOO_CLIP_LOW_Y_HIGH 0x0f811cu
#define IE_VOODOO_NOP_COMMAND 0x0f8120u
#define IE_VOODOO_FOG_TABLE_BASE 0x0f8140u
#define IE_VOODOO_FOG_COLOUR 0x0f81c4u
#define IE_VOODOO_ZA_COLOUR 0x0f81c8u
#define IE_VOODOO_CHROMA_KEY 0x0f81ccu
#define IE_VOODOO_CHROMA_RANGE 0x0f81d0u
#define IE_VOODOO_STIPPLE 0x0f81d4u
#define IE_VOODOO_COLOUR0 0x0f81d8u
#define IE_VOODOO_COLOUR1 0x0f81dcu
#define IE_VOODOO_FBI_INIT0 0x0f8200u
#define IE_VOODOO_FBI_INIT1 0x0f8204u
#define IE_VOODOO_FBI_INIT2 0x0f8208u
#define IE_VOODOO_FBI_INIT3 0x0f820cu
#define IE_VOODOO_FBI_INIT4 0x0f8210u
#define IE_VOODOO_VIDEO_DIMENSIONS 0x0f8214u
#define IE_VOODOO_BACK_PORCH 0x0f8218u
#define IE_VOODOO_VIDEO_DIMENSIONS_VERTICAL 0x0f821cu
#define IE_VOODOO_H_SYNC 0x0f8220u
#define IE_VOODOO_V_SYNC 0x0f8224u
#define IE_VOODOO_DAC_DATA 0x0f822cu
#define IE_VOODOO_TLOD 0x0f8304u
#define IE_VOODOO_TDETAIL 0x0f8308u
#define IE_VOODOO_TEXTURE_BASE1 0x0f8310u
#define IE_VOODOO_TEXTURE_BASE2 0x0f8314u
#define IE_VOODOO_TEXTURE_BASE3 0x0f8318u
#define IE_VOODOO_TEXTURE_BASE4 0x0f831cu
#define IE_VOODOO_TEXTURE_BASE5 0x0f8320u
#define IE_VOODOO_TEXTURE_BASE6 0x0f8324u
#define IE_VOODOO_TEXTURE_BASE7 0x0f8328u
#define IE_VOODOO_TEXTURE_BASE8 0x0f832cu
#define IE_VOODOO_TEXTURE_UPLOAD 0x0f8338u
#define IE_VOODOO_TEXTURE_SOURCE_POINTER 0x0f8348u
#define IE_VOODOO_TEXTURE_SOURCE_BYTES 0x0f834cu
#define IE_VOODOO_TEXTURE_SLOT 0x0f8350u
#define IE_VOODOO_TEXTURE_BIND 0x0f8354u
#define IE_VOODOO_SWAP_HASH_SEQUENCE 0x0f8358u
#define IE_VOODOO_SWAP_HASH_QUERY 0x0f835cu
#define IE_VOODOO_SWAP_HASH_VALUE 0x0f8360u
#define IE_VOODOO_PALETTE_BASE 0x0f8400u
#define IE_VOODOO_TEXTURE_MEMORY_BASE 0x0d0000u
#define IE_VOODOO_TEXTURE_MEMORY_SIZE 0x10000u
#define IE_6502_VOODOO_WINDOW_BASE 0xe000u
#define IE_6502_VOODOO_WINDOW_SIZE 0x1000u
#define IE_6502_VOODOO_BANK_HIGH 0xf7f2u
#define IE_6502_VOODOO_BANK_PAGE_HIGH 0xf7f3u
#define IE_Z80_VOODOO_PORT_ADDRESS_LOW 0xb0u
#define IE_Z80_VOODOO_PORT_ADDRESS_HIGH 0xb1u
#define IE_EXEC_BASE 0x0f2320u
#define IE_FILE_OPEN_READ 0x01u
#define IE_FILE_OPEN_WRITE 0x02u
#define IE_FILE_OPEN_CREATE 0x04u
#define IE_FILE_OPEN_TRUNCATE 0x08u
#define IE_FILE_OPEN_APPEND 0x10u
#define IE_FILE_OPEN_EXCLUSIVE 0x20u
#define IE_FILE_SEEK_SET 0
#define IE_FILE_SEEK_CUR 1
#define IE_FILE_SEEK_END 2
/* Legacy IE64 spellings retained for source compatibility. */
#define IE64_OPEN_READ IE_FILE_OPEN_READ
#define IE64_OPEN_WRITE IE_FILE_OPEN_WRITE
#define IE64_OPEN_CREATE IE_FILE_OPEN_CREATE
#define IE64_OPEN_TRUNCATE IE_FILE_OPEN_TRUNCATE
#define IE64_OPEN_APPEND IE_FILE_OPEN_APPEND
#define IE64_OPEN_EXCLUSIVE IE_FILE_OPEN_EXCLUSIVE
#define IE64_SEEK_SET IE_FILE_SEEK_SET
#define IE64_SEEK_CUR IE_FILE_SEEK_CUR
#define IE64_SEEK_END IE_FILE_SEEK_END
/* Media loader. Control registers are write-only; status, type and error are read-only. */
#define IE_MEDIA_BASE 0x0f2300u
#define IE_MEDIA_END 0x0f231fu
#define IE_MEDIA_NAME_PTR 0x0f2300u
#define IE_MEDIA_SUBSONG 0x0f2304u
#define IE_MEDIA_CTRL 0x0f2308u
#define IE_MEDIA_STATUS 0x0f230cu
#define IE_MEDIA_TYPE 0x0f2310u
#define IE_MEDIA_ERROR 0x0f2314u
#define IE_MEDIA_OP_PLAY 0x01u
#define IE_MEDIA_OP_STOP 0x02u
#define IE_MEDIA_STATUS_IDLE 0x00u
#define IE_MEDIA_STATUS_LOADING 0x01u
#define IE_MEDIA_STATUS_PLAYING 0x02u
#define IE_MEDIA_STATUS_ERROR 0x03u
#define IE_MEDIA_TYPE_NONE 0x00u
#define IE_MEDIA_TYPE_SID 0x01u
#define IE_MEDIA_TYPE_PSG 0x02u
#define IE_MEDIA_TYPE_TED 0x03u
#define IE_MEDIA_TYPE_AHX 0x04u
#define IE_MEDIA_TYPE_POKEY 0x05u
#define IE_MEDIA_TYPE_MOD 0x06u
#define IE_MEDIA_TYPE_WAV 0x07u
#define IE_MEDIA_TYPE_MIDI 0x08u
#define IE_MEDIA_ERROR_OK 0x00u
#define IE_MEDIA_ERROR_NOT_FOUND 0x01u
#define IE_MEDIA_ERROR_BAD_FORMAT 0x02u
#define IE_MEDIA_ERROR_UNSUPPORTED 0x03u
#define IE_MEDIA_ERROR_PATH_INVALID 0x04u
#define IE_MEDIA_ERROR_TOO_LARGE 0x05u
#define IE_NET_SOCKET_BASE 0x0f2500u
#define IE_NET_SOCKET_COMMAND (IE_NET_SOCKET_BASE + 0x00u)
#define IE_NET_SOCKET_REQUEST (IE_NET_SOCKET_BASE + 0x04u)
#define IE_NET_SOCKET_REQUEST_LENGTH (IE_NET_SOCKET_BASE + 0x08u)
#define IE_NET_SOCKET_RESULT1 (IE_NET_SOCKET_BASE + 0x0cu)
#define IE_NET_SOCKET_RESULT2 (IE_NET_SOCKET_BASE + 0x10u)
#define IE_NET_SOCKET_ERRNO (IE_NET_SOCKET_BASE + 0x14u)
#define IE_NET_SOCKET_HOSTERRNO (IE_NET_SOCKET_BASE + 0x18u)
#define IE_NET_SOCKET_STATUS (IE_NET_SOCKET_BASE + 0x1cu)
#define IE_NET_SOCKET_EVENTS (IE_NET_SOCKET_BASE + 0x20u)
#define IE_NET_CMD_SOCKET 1u
#define IE_NET_CMD_BIND 2u
#define IE_NET_CMD_LISTEN 3u
#define IE_NET_CMD_ACCEPT 4u
#define IE_NET_CMD_CONNECT 5u
#define IE_NET_CMD_SENDTO 6u
#define IE_NET_CMD_RECVFROM 7u
#define IE_NET_CMD_SHUTDOWN 8u
#define IE_NET_CMD_SETSOCKOPT 9u
#define IE_NET_CMD_GETSOCKOPT 10u
#define IE_NET_CMD_GETSOCKNAME 11u
#define IE_NET_CMD_GETPEERNAME 12u
#define IE_NET_CMD_IOCTL 13u
#define IE_NET_CMD_CLOSE 14u
#define IE_NET_CMD_WAITSELECT 15u
#define IE_NET_CMD_GETHOSTBYNAME 16u
#define IE_NET_CMD_GETHOSTBYADDR 17u
#define IE_NET_CMD_GETHOSTNAME 18u
#define IE_NET_CMD_DUP2 19u
#define IE_NET_CMD_GETEVENTS 20u
#define IE_NET_CMD_RELEASE 21u
#define IE_NET_CMD_RELEASECOPY 22u
#define IE_NET_CMD_OBTAIN 23u
#define IE_NET_REQ_DOMAIN 0u
#define IE_NET_REQ_TYPE 4u
#define IE_NET_REQ_PROTOCOL 8u
#define IE_NET_REQ_DATA_PTR 12u
#define IE_NET_REQ_DATA_LEN 16u
#define IE_NET_REQ_ADDR_PTR 20u
#define IE_NET_REQ_ADDR_LEN 24u
#define IE_NET_REQ_FLAGS 28u
#define IE_NET_REQ_LEVEL 32u
#define IE_NET_REQ_OPTNAME 36u
#define IE_NET_REQ_TIMEOUT_PTR 40u
#define IE_NET_REQ_SIGMASK_PTR 44u
#define IE_NET_REQ_ARG1 48u
#define IE_NET_REQ_ARG2 52u
#define IE_NET_REQ_EXCEPT_PTR 56u
#define IE_NET_REQ_HOSTENT_NAME_PTR 64u
#define IE_NET_REQ_HOSTENT_NAME_CAP 68u
#define IE_NET_REQ_HOSTENT_ADDRS_PTR 72u
#define IE_NET_REQ_HOSTENT_ADDRS_MAX 76u
#define IE_NET_REQ_WORDS 24u
#define IE_NET_REQ_SIZE 96u
#define IE_NET_MAX_IO 65536u
#define IE_NET_MAX_ADDR 128u
#define IE_NET_DTABLE 64u
#define IE_NET_FDSET_LEN 8u
#define IE_NET_SOCKET_CMD IE_NET_SOCKET_COMMAND
#define IE_NET_SOCKET_REQ_PTR IE_NET_SOCKET_REQUEST
#define IE_NET_SOCKET_REQ_LEN IE_NET_SOCKET_REQUEST_LENGTH
#define IE_NET_SOCKET_RES1 IE_NET_SOCKET_RESULT1
#define IE_NET_SOCKET_RES2 IE_NET_SOCKET_RESULT2
#define IE_NET_SOCKET_HERRNO IE_NET_SOCKET_HOSTERRNO
#define IE_NET_SOCKET_REQ_DOMAIN 0u
#define IE_NET_SOCKET_REQ_TYPE IE_NET_REQ_TYPE
#define IE_NET_SOCKET_REQ_PROTOCOL IE_NET_REQ_PROTOCOL
#define IE_NET_SOCKET_REQ_DATA_PTR 12u
#define IE_NET_SOCKET_REQ_DATA_LEN IE_NET_REQ_DATA_LEN
#define IE_NET_SOCKET_REQ_ADDR_PTR IE_NET_REQ_ADDR_PTR
#define IE_NET_SOCKET_REQ_ADDR_LEN IE_NET_REQ_ADDR_LEN
#define IE_NET_SOCKET_REQ_FLAGS IE_NET_REQ_FLAGS
#define IE_NET_SOCKET_REQ_LEVEL IE_NET_REQ_LEVEL
#define IE_NET_SOCKET_REQ_OPTNAME IE_NET_REQ_OPTNAME
#define IE_NET_SOCKET_REQ_TIMEOUT_PTR IE_NET_REQ_TIMEOUT_PTR
#define IE_NET_SOCKET_REQ_SIGMASK_PTR IE_NET_REQ_SIGMASK_PTR
#define IE_NET_SOCKET_REQ_ARG1 IE_NET_REQ_ARG1
#define IE_NET_SOCKET_REQ_ARG2 IE_NET_REQ_ARG2
#define IE_NET_SOCKET_REQ_EXCEPT_PTR IE_NET_REQ_EXCEPT_PTR
#define IE_NET_SOCKET_REQ_HOSTENT_NAME_PTR IE_NET_REQ_HOSTENT_NAME_PTR
#define IE_NET_SOCKET_REQ_HOSTENT_NAME_CAP IE_NET_REQ_HOSTENT_NAME_CAP
#define IE_NET_SOCKET_REQ_HOSTENT_ADDRS_PTR IE_NET_REQ_HOSTENT_ADDRS_PTR
#define IE_NET_SOCKET_REQ_HOSTENT_ADDRS_MAX 76u
#define IE_HOST_SOCKET_BASE IE_NET_SOCKET_BASE
#define IE_HOST_SOCKET_CMD IE_NET_SOCKET_COMMAND
#define IE_HOST_SOCKET_REQ_PTR IE_NET_SOCKET_REQUEST
#define IE_HOST_SOCKET_REQ_LEN IE_NET_SOCKET_REQUEST_LENGTH
#define IE_HOST_SOCKET_RES1 IE_NET_SOCKET_RESULT1
#define IE_HOST_SOCKET_RES2 IE_NET_SOCKET_RESULT2
#define IE_HOST_SOCKET_ERRNO IE_NET_SOCKET_ERRNO
#define IE_HOST_SOCKET_HERRNO IE_NET_SOCKET_HOSTERRNO
#define IE_HOST_SOCKET_STATUS IE_NET_SOCKET_STATUS
#define IE_HOST_SOCKET_EVENTS IE_NET_SOCKET_EVENTS
#define IE_HOST_SOCKET_CMD_SOCKET IE_NET_CMD_SOCKET
#define IE_HOST_SOCKET_CMD_BIND IE_NET_CMD_BIND
#define IE_HOST_SOCKET_CMD_LISTEN IE_NET_CMD_LISTEN
#define IE_HOST_SOCKET_CMD_ACCEPT IE_NET_CMD_ACCEPT
#define IE_HOST_SOCKET_CMD_CONNECT IE_NET_CMD_CONNECT
#define IE_HOST_SOCKET_CMD_SENDTO IE_NET_CMD_SENDTO
#define IE_HOST_SOCKET_CMD_RECVFROM IE_NET_CMD_RECVFROM
#define IE_HOST_SOCKET_CMD_SHUTDOWN IE_NET_CMD_SHUTDOWN
#define IE_HOST_SOCKET_CMD_SETSOCKOPT IE_NET_CMD_SETSOCKOPT
#define IE_HOST_SOCKET_CMD_GETSOCKOPT IE_NET_CMD_GETSOCKOPT
#define IE_HOST_SOCKET_CMD_GETSOCKNAME IE_NET_CMD_GETSOCKNAME
#define IE_HOST_SOCKET_CMD_GETPEERNAME IE_NET_CMD_GETPEERNAME
#define IE_HOST_SOCKET_CMD_IOCTL IE_NET_CMD_IOCTL
#define IE_HOST_SOCKET_CMD_CLOSE IE_NET_CMD_CLOSE
#define IE_HOST_SOCKET_CMD_WAITSELECT IE_NET_CMD_WAITSELECT
#define IE_HOST_SOCKET_CMD_GETEVENTS IE_NET_CMD_GETEVENTS
#define IE_HOST_SOCKET_CMD_RELEASE IE_NET_CMD_RELEASE
#define IE_HOST_SOCKET_CMD_RELEASECOPY IE_NET_CMD_RELEASECOPY
#define IE_HOST_SOCKET_CMD_OBTAIN IE_NET_CMD_OBTAIN

/* Assembly-compatible names for Copper, host control and coprocessor values. */
#define IE_COPPER_CTRL IE_VIDEO_COPPER_CTRL
#define IE_COPPER_PTR IE_VIDEO_COPPER_PTR
#define IE_HOST_MMIO_BASE 0x0f1400u
#define IE_HOST_MMIO_CMD 0x0f1400u
#define IE_HOST_MMIO_TRIGGER 0x0f1404u
#define IE_HOST_MMIO_STATUS 0x0f1408u
#define IE_HOST_MMIO_EXIT 0x0f140cu
#define IE_HOST_CMD_NET 1u
#define IE_HOST_CMD_UPDATE 2u
#define IE_HOST_CMD_REBOOT 3u
#define IE_HOST_CMD_POWEROFF 4u
#define IE_HOST_STATUS_RUNNING 0u
#define IE_HOST_STATUS_OK 1u
#define IE_HOST_STATUS_ERR 2u
#define IE_HOST_STATUS_CANCEL 3u
#define IE_HOST_STATUS_DISABLED 4u
#define IE_HOST_STATUS_IDLE 5u
#define IE_COPROC_CMD_ENQUEUE IE_COPROC_COMMAND_ENQUEUE
#define IE_COPROC_CMD_ERROR IE_COPROC_COMMAND_ERROR
#define IE_COPROC_CMD_STATUS IE_COPROC_COMMAND_STATUS
#define IE_COPROC_CMD_START IE_COPROC_COMMAND_START
#define IE_COPROC_CMD_STOP IE_COPROC_COMMAND_STOP
#define IE_COPROC_CMD_POLL IE_COPROC_COMMAND_POLL
#define IE_COPROC_CMD_WAIT_CMD IE_COPROC_COMMAND_WAIT
#define IE_COPROC_BUSY_PCT IE_COPROC_BUSY_PERCENT
#define IE_COPROC_IRQ_CTRL IE_COPROC_IRQ_CONTROL
#define IE_COPROC_NAME_PTR IE_COPROC_NAME_POINTER
#define IE_COPROC_REQ_PTR IE_COPROC_REQUEST_POINTER
#define IE_COPROC_REQ_LEN IE_COPROC_REQUEST_LENGTH
#define IE_COPROC_RESP_PTR IE_COPROC_RESPONSE_POINTER
#define IE_COPROC_RESP_CAP IE_COPROC_RESPONSE_CAPACITY
#define IE_COPROC_ST_PENDING IE_COPROC_TICKET_PENDING
#define IE_COPROC_ST_RUNNING IE_COPROC_TICKET_RUNNING
#define IE_COPROC_ST_OK IE_COPROC_TICKET_OK
#define IE_COPROC_ST_ERROR IE_COPROC_TICKET_ERROR
#define IE_COPROC_ST_TIMEOUT IE_COPROC_TICKET_TIMEOUT
#define IE_COPROC_ST_WORKER_DOWN IE_COPROC_TICKET_WORKER_DOWN
#define IE_FILE_DATA_PTR64 IE_FILE_DATA_POINTER64
#define IE_COPROC_LAYOUT_VERSION 1u
#define IE_COPROC_ERR_NONE 0u
#define IE_COPROC_ERR_INVALID_CPU 1u
#define IE_COPROC_ERR_NOT_FOUND 2u
#define IE_COPROC_ERR_PATH_INVALID 3u
#define IE_COPROC_ERR_LOAD_FAILED 4u
#define IE_COPROC_ERR_QUEUE_FULL 5u
#define IE_COPROC_ERR_NO_WORKER 6u
#define IE_COPROC_ERR_STALE_TICKET 7u
#define IE_COPROC_ERR_INVALID_INSTANCE 8u
#define IE_COPROC_ERR_STALE_WORKER 9u
#define IE_COPROC_CPU_IE32 1u
#define IE_COPROC_CPU_IE64 2u
#define IE_COPROC_CPU_6502 3u
#define IE_COPROC_CPU_M68K 4u
#define IE_COPROC_CPU_Z80 5u
#define IE_COPROC_CPU_X86 6u

#if IE_HAS_BANK_WINDOWS
# define IE_BANK1_WINDOW 0x2000u
# define IE_BANK2_WINDOW 0x4000u
# define IE_BANK3_WINDOW 0x6000u
# define IE_VRAM_WINDOW 0x8000u
# define IE_BANK_SIZE 0x2000u
# define IE_VRAM_BANK_SIZE 0x4000u
# define IE_BANK1_REG_LO 0xf700u
# define IE_BANK1_REG_HI 0xf701u
# define IE_BANK2_REG_LO 0xf702u
# define IE_BANK2_REG_HI 0xf703u
# define IE_BANK3_REG_LO 0xf704u
# define IE_BANK3_REG_HI 0xf705u
# define IE_VRAM_BANK_REG 0xf7f0u
static void ie_bank_select(volatile uint8_t *low, volatile uint8_t *high, uint16_t bank) {
	*low = (uint8_t)bank; *high = (uint8_t)(bank >> 8);
}
#endif

#if IE_HAS_X86_PORT_IO
static inline uint8_t ie_x86_in8(uint16_t port) { uint8_t value; __asm__ volatile ("inb %1, %0" : "=a"(value) : "d"(port)); return value; }
static inline void ie_x86_out8(uint16_t port, uint8_t value) { __asm__ volatile ("outb %0, %1" : : "a"(value), "d"(port)); }
# define IE_X86_PORT_PSG_SELECT 0xf0u
# define IE_X86_PORT_PSG_DATA 0xf1u
# define IE_X86_PORT_SID_SELECT 0xe0u
# define IE_X86_PORT_SID_DATA 0xe1u
# define IE_X86_PORT_POKEY_BASE 0x60u
# define IE_X86_PORT_TED_SELECT 0xf2u
# define IE_X86_PORT_TED_DATA 0xf3u
# define IE_X86_PORT_ANTIC_SELECT 0xd4u
# define IE_X86_PORT_ANTIC_DATA 0xd5u
# define IE_X86_PORT_GTIA_SELECT 0xd6u
# define IE_X86_PORT_GTIA_DATA 0xd7u
# define IE_X86_PORT_VOODOO_ADDRESS_LOW 0xb0u
# define IE_X86_PORT_VOODOO_ADDRESS_HIGH 0xb1u
# define IE_X86_PORT_VOODOO_DATA0 0xb2u
# define IE_X86_PORT_VOODOO_DATA1 0xb3u
# define IE_X86_PORT_VOODOO_DATA2 0xb4u
# define IE_X86_PORT_VOODOO_DATA3 0xb5u
#endif

#if IE_HAS_IE64_CONTROL_REGISTERS
enum ie64_control_register {
	IE64_CR_PTBR = 0, IE64_CR_FAULT_ADDR = 1, IE64_CR_FAULT_CAUSE = 2,
	IE64_CR_FAULT_PC = 3, IE64_CR_TRAP_VEC = 4, IE64_CR_MMU_CTRL = 5,
	IE64_CR_TP = 6, IE64_CR_INTR_VEC = 7, IE64_CR_KSP = 8,
	IE64_CR_TIMER_PERIOD = 9, IE64_CR_TIMER_COUNT = 10,
	IE64_CR_TIMER_CTRL = 11, IE64_CR_USP = 12, IE64_CR_PREV_MODE = 13,
	IE64_CR_SAVED_SUA = 14, IE64_CR_RAM_SIZE_BYTES = 15
};
uint64_t __builtin_ie64_mfcr(unsigned int); void __builtin_ie64_mtcr(unsigned int, uint64_t);
void __builtin_ie64_tlbinval(uint64_t); void __builtin_ie64_tlbflush(void); void __builtin_ie64_suaen(void); void __builtin_ie64_suadis(void);
_Noreturn void __builtin_ie64_eret(void); _Noreturn void __builtin_ie64_rti(void); _Noreturn void __builtin_ie64_halt(void);
void __builtin_ie64_nop(void); void __builtin_ie64_sei(void); void __builtin_ie64_cli(void); void __builtin_ie64_wait(unsigned int); uint64_t __builtin_ie64_syscall(unsigned int); uint64_t __builtin_ie64_smode(void);
#endif

#if IE_HAS_IE64_ATOMICS
uint64_t __builtin_ie64_cas(volatile uint64_t *, uint64_t, uint64_t); uint64_t __builtin_ie64_xchg(volatile uint64_t *, uint64_t);
uint64_t __builtin_ie64_faa(volatile uint64_t *, uint64_t); uint64_t __builtin_ie64_fand(volatile uint64_t *, uint64_t); uint64_t __builtin_ie64_for(volatile uint64_t *, uint64_t); uint64_t __builtin_ie64_fxor(volatile uint64_t *, uint64_t);
static uint64_t ie64_atomic_compare_exchange(volatile uint64_t *p, uint64_t a, uint64_t b) { return __builtin_ie64_cas(p, a, b); }
static uint64_t ie64_atomic_exchange(volatile uint64_t *p, uint64_t a) { return __builtin_ie64_xchg(p, a); }
static uint64_t ie64_atomic_fetch_add(volatile uint64_t *p, uint64_t a) { return __builtin_ie64_faa(p, a); }
static uint64_t ie64_atomic_fetch_and(volatile uint64_t *p, uint64_t a) { return __builtin_ie64_fand(p, a); }
static uint64_t ie64_atomic_fetch_or(volatile uint64_t *p, uint64_t a) { return __builtin_ie64_for(p, a); }
static uint64_t ie64_atomic_fetch_xor(volatile uint64_t *p, uint64_t a) { return __builtin_ie64_fxor(p, a); }
#endif

#if IE_HAS_IE64_FPU
float __builtin_ie64_fmovecr(unsigned int); double __builtin_ie64_dmovecr(unsigned int); float __builtin_ie64_fmod(float, float); double __builtin_ie64_dmod(double, double);
float __builtin_ie64_fabs(float); double __builtin_ie64_dabs(double); float __builtin_ie64_fint(float); double __builtin_ie64_dint(double); float __builtin_ie64_fcvtds(double); double __builtin_ie64_fcvtsd(float);
float __builtin_ie64_fsin(float); float __builtin_ie64_fcos(float); float __builtin_ie64_ftan(float); float __builtin_ie64_fatan(float); float __builtin_ie64_flog(float); float __builtin_ie64_fexp(float); float __builtin_ie64_fpow(float, float); float __builtin_ie64_fsqrt(float);
double __builtin_ie64_dsin(double); double __builtin_ie64_dcos(double); double __builtin_ie64_dtan(double); double __builtin_ie64_datan(double); double __builtin_ie64_dlog(double); double __builtin_ie64_dexp(double); double __builtin_ie64_dpow(double, double); double __builtin_ie64_dsqrt(double);
uint64_t __builtin_ie64_fmovsr(void); uint64_t __builtin_ie64_fmovcr(void);
void __builtin_ie64_fmovsc(unsigned int); void __builtin_ie64_fmovcc(unsigned int);
#endif

#if defined(IE_TARGET_IE64)
int __ie64_console_read(void *, unsigned long, unsigned long *); int __ie64_console_write(const void *, unsigned long, unsigned long *);
int __ie64_file_open(const char *, unsigned int, uint64_t *); int __ie64_file_read(uint64_t, void *, unsigned long, unsigned long *); int __ie64_file_write(uint64_t, const void *, unsigned long, unsigned long *);
int __ie64_file_seek(uint64_t, int64_t, int, int64_t *); int __ie64_file_close(uint64_t); int __ie64_file_remove(const char *); int __ie64_file_rename(const char *, const char *); int __ie64_file_tmp(uint64_t *); int __ie64_monotonic_ticks(uint64_t *); _Noreturn void __ie64_terminate(int);
static void ie64_nop(void) { __builtin_ie64_nop(); } static void ie64_enable_interrupts(void) { __builtin_ie64_sei(); } static void ie64_disable_interrupts(void) { __builtin_ie64_cli(); } static _Noreturn void __ie64_assert_fail(void) { __builtin_ie64_halt(); }
#endif

#endif
