summaryrefslogtreecommitdiff
path: root/z80.h
blob: b6dd90253300dbe865511803a6a40f6ac4812d21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*

    z80 - Z80 emulation

    Copyright (C) 2006  Ian Cowburn <ianc@noddybox.co.uk>

    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 */