summaryrefslogtreecommitdiff
path: root/src/z80.h
blob: a00c8b096dc4ee7a549f12e6357f69c4fd9a3edd (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
248
249
250
251
252
253
254
255
256
257
/*

    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: z80.h 61 2008-11-03 17:07:32Z ianc $

*/

#ifndef Z80_H
#define Z80_H "$Id: z80.h 61 2008-11-03 17:07:32Z ianc $"

#include <stdio.h>

/* Configuration
*/
#include "z80_config.h"


/* ---------------------------------------- TYPES
*/

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


/* A Z80 16-bit register.  To access the HI/LO component use the indexes
   Z80_HI_WORD and Z80_LO_WORD which will be initialised once Z80Init has been
   called.
*/
typedef union
{
    Z80Word		w;
    Z80Byte		b[2];
} Z80Reg;

extern int Z80_HI_WORD;
extern int Z80_LO_WORD;


/* The processor
*/
struct Z80Private;

typedef struct
{
    Z80Word		PC;

    Z80Reg		AF;
    Z80Reg		BC;
    Z80Reg		DE;
    Z80Reg		HL;

    Z80Word		AF_;
    Z80Word		BC_;
    Z80Word		DE_;
    Z80Word		HL_;

    Z80Reg		IX;
    Z80Reg		IY;

    Z80Word		SP;

    Z80Byte		IFF1;
    Z80Byte		IFF2;
    Z80Byte		IM;
    Z80Byte		I;
    Z80Byte		R;

    struct Z80Private	*priv;
} Z80;


/* Interfaces used to handle memory
*/
typedef	Z80Byte	(*Z80ReadMemory)(Z80 *cpu, Z80Word address);
typedef	void	(*Z80WriteMemory)(Z80 *cpu, Z80Word address, Z80Byte value);


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


/* 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.  
*/
#ifdef ENABLE_ARRAY_MEMORY
Z80	*Z80Init(Z80ReadPort read_port,
		 Z80WritePort write_port);
#else
Z80	*Z80Init(Z80ReadMemory read_memory,
		 Z80WriteMemory write_memory,
		 Z80ReadPort read_port,
		 Z80WritePort write_port,
		 Z80ReadMemory read_for_disassem);
#endif


/* Resets the processor.
*/
void	Z80Reset(Z80 *cpu);


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


/* Manipulate the cylce count of the Z80
*/
Z80Val	Z80Cycles(Z80 *cpu);
void	Z80ResetCycles(Z80 *cpu, Z80Val cycles);


/* Set address to label mappings for the disassembler
*/
void	Z80SetLabels(Z80Label labels[]);


/* Simple disassembly of memory accessed through read_for_disassem, or 
   Z80_MEMORY as appropriate.  addr is updated on exit.
*/
const char *Z80Disassemble(Z80 *cpu, Z80Word *addr);

/* Allows the CPU state to be saved/loaded from a stream
*/
void	Z80SaveSnapshot(Z80 *cpu, FILE *fp);
void	Z80LoadSnapshot(Z80 *cpu, FILE *fp);

#endif

/* END OF FILE */