/* z80 - Z80 emulation Copyright (C) 2006 Ian Cowburn Some of the opcode routines are based on the Z80 emulation from YAZE, Copyright (c) 1995 Frank D. Cringle. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ------------------------------------------------------------------------- $Id$ */ #ifndef Z80_H #define Z80_H "$Id$" /* ---------------------------------------- TYPES */ /* The processor */ struct Z80; typedef struct Z80 Z80; /* Large unsigned type */ typedef unsigned long Z80Val; /* 8-bit type. The emulation will exit with code 2 if this isn't 8 bits. */ typedef unsigned char Z80Byte; /* 8-bit signed type. The emulation will exit with code 2 if this isn't 8 bits. */ typedef signed char Z80Relative; /* 16-bit type. The emulation will exit with code 2 if this isn't 16 bits. */ typedef unsigned short Z80Word; /* Memory */ typedef Z80Byte Z80Memory[0x10000]; /* Memory control - a TRUE indicates that the 256-byte page is writeable. */ typedef int Z80MemoryControl[256]; /* Interfaces needed to handle ports (IN/OUT commands) */ typedef Z80Byte (*Z80ReadPort)(Z80 *cpu, Z80Word address); typedef void (*Z80WritePort)(Z80 *cpu, Z80Word address, Z80Byte value); /* Callback. Callback should return TRUE for processing to continue. */ typedef int (*Z80Callback)(Z80 *cpu, Z80Val data); /* Callback reasons eZ80_Instruction Called before the initial fetch for an instruction (called just to once no matter how many bytes the instruction is made up of). eZ80_EDHook Called when an undefined ED opcode is executed. eZ80_Halt Called when the HALT instruction is hit and released. eZ80_RETI Called when the RETI instruction is executed */ typedef enum { eZ80_Instruction, /* data = no cycles since reset */ eZ80_EDHook, /* data = byte after ED opcode (only for NOP opcodes) */ eZ80_Halt, /* data = 1 halt raised, 0 halt cleared by int */ eZ80_RETI, /* data = ignored */ eZ80_NO_CALLBACK /* leave at end */ } Z80CallbackReason; /* Get/settable state of the Z80 */ typedef struct { Z80Word PC; Z80Word SP; Z80Val cycle; Z80Word AF; Z80Word BC; Z80Word DE; Z80Word HL; Z80Word AF_; /* Alternate registers */ Z80Word BC_; Z80Word DE_; Z80Word HL_; Z80Word IX; Z80Word IY; Z80Byte IFF1; Z80Byte IFF2; Z80Byte IM; Z80Byte I; Z80Byte R; } Z80State; /* Flags in the F register */ typedef enum { eZ80_Carry =0x01, eZ80_Neg =0x02, eZ80_PV =0x04, eZ80_Hidden3 =0x08, eZ80_HalfCarry =0x10, eZ80_Hidden5 =0x20, eZ80_Zero =0x40, eZ80_Sign =0x80 } Z80FlagRegister; /* Disassembly label -- only useful if ENABLE_DISASSEMBLER is set. Labels are stored as an array, where a NULL in the label field marks the end of the list. */ typedef struct { Z80Word address; const char *label; } Z80Label; /* ---------------------------------------- INTERFACES */ /* Initialises the processor. */ Z80 *Z80Init(Z80Memory memory, Z80MemoryControl memcontrol, Z80WritePort write_port, Z80ReadPort read_port); /* Resets the processor. */ void Z80Reset(Z80 *cpu); /* Sets the PC */ void Z80SetPC(Z80 *cpu, Z80Word PC); /* Gets the PC */ Z80Word Z80GetPC(Z80 *cpu); /* Sets the cycle count to the specified count */ void Z80ResetCycles(Z80 *cpu, Z80Val cycles); /* Lodge a callback to be invoked after special events. Returns FALSE if the callback couldn't be lodged (there is a max of 10 callbacks per reason). */ int Z80LodgeCallback(Z80 *cpu, Z80CallbackReason reason, Z80Callback callback); /* Remove a callback. Does nothing if reason was not lodged with Z80LodgeCallback() */ void Z80RemoveCallback(Z80 *cpu, Z80CallbackReason reason, Z80Callback callback); /* Cause an interrupt before the next opcode. devbyte is the byte generated by the device (if any). */ void Z80Interrupt(Z80 *cpu, Z80Byte devbyte); /* Cause an NMI */ void Z80NMI(Z80 *cpu); /* Execute a single instruction. Returns FALSE if any callback returned FALSE. */ int Z80SingleStep(Z80 *cpu); /* Executes until a callback returns FALSE (never returns otherwise) */ void Z80Exec(Z80 *cpu); /* Interrogate the state of the Z80 */ Z80Val Z80Cycles(Z80 *cpu); void Z80GetState(Z80 *cpu, Z80State *state); void Z80SetState(Z80 *cpu, const Z80State *state); /* Set address to label mappings for the disassembler */ void Z80SetLabels(Z80Label labels[]); /* Simple disassembly. addr is updated on exit. */ const char *Z80Disassemble(Z80 *cpu, Z80Word *addr); #endif /* END OF FILE */