1 /* Multi-Z80 32 Bit emulator */
2 
3 /* Copyright 1996, Neil Bradley, All rights reserved
4  *
5  * License agreement:
6  *
7  * The mZ80 emulator may be distributed in unmodified form to any medium.
8  *
9  * mZ80 May not be sold, or sold as a part of a commercial package without
10  * the express written permission of Neil Bradley (neil@synthcom.com). This
11  * includes shareware.
12  *
13  * Modified versions of mZ80 may not be publicly redistributed without author
14  * approval (neil@synthcom.com). This includes distributing via a publicly
15  * accessible LAN. You may make your own source modifications and distribute
16  * mZ80 in object only form.
17  *
18  * mZ80 Licensing for commercial applications is available. Please email
19  * neil@synthcom.com for details.
20  *
21  * Synthcom Systems, Inc, and Neil Bradley will not be held responsible for
22  * any damage done by the use of mZ80. It is purely "as-is".
23  *
24  * If you use mZ80 in a freeware application, credit in the following text:
25  *
26  * "Multi-Z80 CPU emulator by Neil Bradley (neil@synthcom.com)"
27  *
28  * must accompany the freeware application within the application itself or
29  * in the documentation.
30  *
31  * Legal stuff aside:
32  *
33  * If you find problems with mZ80, please email the author so they can get
34  * resolved. If you find a bug and fix it, please also email the author so
35  * that those bug fixes can be propogated to the installed base of mZ80
36  * users. If you find performance improvements or problems with mZ80, please
37  * email the author with your changes/suggestions and they will be rolled in
38  * with subsequent releases of mZ80.
39  *
40  * The whole idea of this emulator is to have the fastest available 32 bit
41  * Multi-z80 emulator for the PC, giving maximum performance.
42  */
43 
44 /* General z80 based defines */
45 
46 #ifndef	_MZ80_H_
47 #define	_MZ80_H_
48 
49 #include <stdint.h>
50 
51 #ifndef UINT32
52 #define UINT32  uint32_t
53 #endif
54 
55 #ifndef UINT16
56 #define UINT16  uint16_t
57 #endif
58 
59 #ifndef UINT8
60 #define UINT8   uint8_t
61 #endif
62 
63 #ifndef INT32
64 #define INT32  int32_t
65 #endif
66 
67 #ifndef INT16
68 #define INT16  int16_t
69 #endif
70 
71 #ifndef INT8
72 #define INT8   int8_t
73 #endif
74 
75 #ifndef UINTPTR
76 #define UINTPTR uintptr_t
77 #endif
78 
79 #ifdef __cplusplus
80 extern "C" {
81 #endif
82 
83 #ifndef _MEMORYREADWRITEBYTE_
84 #define _MEMORYREADWRITEBYTE_
85 
86 struct MemoryWriteByte
87 {
88 	UINT32 lowAddr;
89 	UINT32 highAddr;
90 	void (*memoryCall)(UINT32, UINT8, struct MemoryWriteByte *);
91 	void *pUserArea;
92 };
93 
94 struct MemoryReadByte
95 {
96 	UINT32 lowAddr;
97 	UINT32 highAddr;
98 	UINT8 (*memoryCall)(UINT32, struct MemoryReadByte *);
99 	void *pUserArea;
100 };
101 
102 #endif // _MEMORYREADWRITEBYTE_
103 
104 struct z80PortWrite
105 {
106 	UINT16 lowIoAddr;
107 	UINT16 highIoAddr;
108 	void (*IOCall)(UINT16, UINT8, struct z80PortWrite *);
109 	void *pUserArea;
110 };
111 
112 struct z80PortRead
113 {
114 	UINT16 lowIoAddr;
115 	UINT16 highIoAddr;
116 	UINT16 (*IOCall)(UINT16, struct z80PortRead *);
117 	void *pUserArea;
118 };
119 
120 struct z80TrapRec
121 {
122   	UINT16 trapAddr;
123 	UINT8  skipCnt;
124 	UINT8  origIns;
125 };
126 
127 typedef union
128 {
129 	UINT32 af;
130 
131 	struct
132 	{
133 #ifdef WORDS_BIGENDIAN
134 		UINT16 wFiller;
135 		UINT8 a;
136 		UINT8 f;
137 #else
138 		UINT8 f;
139 		UINT8 a;
140 		UINT16 wFiller;
141 #endif
142 	} half;
143 } reg_af;
144 
145 #define	z80AF	z80af.af
146 #define	z80A	z80af.half.a
147 #define	z80F	z80af.half.f
148 
149 typedef union
150 {
151 	UINT32 bc;
152 
153 	struct
154 	{
155 #ifdef WORDS_BIGENDIAN
156 		UINT16 wFiller;
157 		UINT8 b;
158 		UINT8 c;
159 #else
160 		UINT8 c;
161 		UINT8 b;
162 		UINT16 wFiller;
163 #endif
164 	} half;
165 } reg_bc;
166 
167 #define	z80BC	z80bc.bc
168 #define	z80B	z80bc.half.b
169 #define	z80C	z80bc.half.c
170 
171 typedef union
172 {
173 	UINT32 de;
174 
175 	struct
176 	{
177 #ifdef WORDS_BIGENDIAN
178 		UINT16 wFiller;
179 		UINT8 d;
180 		UINT8 e;
181 #else
182 		UINT8 e;
183 		UINT8 d;
184 		UINT16 wFiller;
185 #endif
186 	} half;
187 } reg_de;
188 
189 #define	z80DE	z80de.de
190 #define	z80D	z80de.half.d
191 #define	z80E	z80de.half.e
192 
193 typedef union
194 {
195 	UINT32 hl;
196 
197 	struct
198 	{
199 #ifdef WORDS_BIGENDIAN
200 		UINT16 wFiller;
201 		UINT8 h;
202 		UINT8 l;
203 #else
204 		UINT8 l;
205 		UINT8 h;
206 		UINT16 wFiller;
207 #endif
208 	} half;
209 } reg_hl;
210 
211 #define	z80HL	z80hl.hl
212 #define	z80H	z80hl.half.h
213 #define	z80L	z80hl.half.l
214 
215 #define	z80SP	z80sp.sp
216 
217 typedef union
218 {
219 	UINT32 ix;
220 
221 	struct
222 	{
223 #ifdef WORDS_BIGENDIAN
224 		UINT16 wFiller;
225 		UINT8 xh;
226 		UINT8 xl;
227 #else
228 		UINT8 xl;
229 		UINT8 xh;
230 		UINT16 wFiller;
231 #endif
232 	} half;
233 } reg_ix;
234 
235 #define	z80IX	z80ix.ix
236 #define	z80XH	z80ix.half.xh
237 #define	z80XL	z80ix.half.xl
238 
239 typedef union
240 {
241 	UINT32 iy;
242 
243 	struct
244 	{
245 #ifdef WORDS_BIGENDIAN
246 		UINT16 wFiller;
247 		UINT8 yh;
248 		UINT8 yl;
249 #else
250 		UINT8 yl;
251 		UINT8 yh;
252 		UINT16 wFiller;
253 #endif
254 	} half;
255 } reg_iy;
256 
257 #define	z80IY	z80iy.iy
258 #define	z80YH	z80iy.half.yh
259 #define	z80YL	z80iy.half.yl
260 
261 struct mz80context
262 {
263 	UINT8 *z80Base;
264 	struct MemoryReadByte *z80MemRead;
265 	struct MemoryWriteByte *z80MemWrite;
266 	struct z80PortRead *z80IoRead;
267 	struct z80PortWrite *z80IoWrite;
268 	UINT32 z80clockticks;
269 	UINT32 z80iff;
270 	UINT32 z80interruptMode;
271 	UINT32 z80halted;
272 
273 	reg_af z80af;
274 	reg_bc z80bc;
275 	reg_de z80de;
276 	reg_hl z80hl;
277 	UINT32 z80afprime;
278 	UINT32 z80bcprime;
279 	UINT32 z80deprime;
280 	UINT32 z80hlprime;
281 	reg_ix z80ix;
282 	reg_iy z80iy;
283 	UINT32 z80sp;
284 	UINT32 z80pc;
285 	UINT32 z80nmiAddr;
286 	UINT32 z80intAddr;
287 	UINT32 z80rCounter;
288 	UINT8 z80i;
289 	UINT8 z80r;
290 	UINT8 z80intPending;
291 };
292 
293 // These are the enumerations used for register access. DO NOT ALTER THEIR
294 // ORDER! It must match the same order as in the mz80.c/mz80.asm files!
295 
296 enum
297 {
298 #ifndef CPUREG_PC
299 	CPUREG_PC = 0,
300 #endif
301 	CPUREG_Z80_AF = 1,
302 	CPUREG_Z80_BC,
303 	CPUREG_Z80_DE,
304 	CPUREG_Z80_HL,
305 	CPUREG_Z80_AFPRIME,
306 	CPUREG_Z80_BCPRIME,
307 	CPUREG_Z80_DEPRIME,
308 	CPUREG_Z80_HLPRIME,
309 	CPUREG_Z80_IX,
310 	CPUREG_Z80_IY,
311 	CPUREG_Z80_SP,
312 	CPUREG_Z80_I,
313 	CPUREG_Z80_R,
314 	CPUREG_Z80_A,
315 	CPUREG_Z80_B,
316 	CPUREG_Z80_C,
317 	CPUREG_Z80_D,
318 	CPUREG_Z80_E,
319 	CPUREG_Z80_H,
320 	CPUREG_Z80_L,
321 	CPUREG_Z80_F,
322 	CPUREG_Z80_CARRY,
323 	CPUREG_Z80_NEGATIVE,
324 	CPUREG_Z80_PARITY,
325 	CPUREG_Z80_OVERFLOW,
326 	CPUREG_Z80_HALFCARRY,
327 	CPUREG_Z80_ZERO,
328 	CPUREG_Z80_SIGN,
329 	CPUREG_Z80_IFF1,
330 	CPUREG_Z80_IFF2,
331 
332 	// Leave this here!
333 
334 	CPUREG_Z80_MAX_INDEX
335 };
336 
337 extern UINT32 mz80exec(UINT32);
338 extern UINT32 mz80GetContextSize(void);
339 extern UINT32 mz80GetElapsedTicks(UINT32);
340 extern void mz80ReleaseTimeslice(void);
341 extern void mz80GetContext(void *);
342 extern void mz80SetContext(void *);
343 extern void mz80reset(void);
344 extern void mz80ClearPendingInterrupt(void);
345 extern UINT32 mz80int(UINT32);
346 extern UINT32 mz80nmi(void);
347 extern void mz80init(void);
348 extern UINT32 z80intAddr;
349 extern UINT32 z80nmiAddr;
350 
351 // Debugger useful routines
352 
353 extern UINT8 mz80SetRegisterValue(void *, UINT32, UINT32);
354 extern UINT32 mz80GetRegisterValue(void *, UINT32);
355 extern UINT32 mz80GetRegisterTextValue(void *, UINT32, UINT8 *);
356 extern UINT8 *mz80GetRegisterName(UINT32);
357 
358 // Memory/IO read/write commands
359 
360 #ifndef VALUE_BYTE
361 #define	VALUE_BYTE	0
362 #endif
363 
364 #ifndef VALUE_WORD
365 #define	VALUE_WORD	1
366 #endif
367 
368 #ifndef VALUE_DWORD
369 #define	VALUE_DWORD	2
370 #endif
371 
372 #ifndef VALUE_IO
373 #define	VALUE_IO	3
374 #endif
375 
376 extern void mz80WriteValue(UINT8 bWhat, UINT32 dwAddr, UINT32 dwData);
377 extern UINT32 mz80ReadValue(UINT8 bWhat, UINT32 dwAddr);
378 
379 // Flag definitions
380 
381 #define	Z80_FLAG_CARRY					0x01
382 #define	Z80_FLAG_NEGATIVE				0x02
383 #define	Z80_FLAG_OVERFLOW_PARITY	0x04
384 #define	Z80_FLAG_UNDEFINED1			0x08
385 #define	Z80_FLAG_HALF_CARRY			0x10
386 #define	Z80_FLAG_UNDEFINED2			0x20
387 #define	Z80_FLAG_ZERO					0x40
388 #define	Z80_FLAG_SIGN					0x80
389 
390 #define	IFF1			0x01
391 #define	IFF2			0x02
392 
393 typedef struct mz80context CONTEXTMZ80;
394 
395 #ifdef __cplusplus
396 }
397 #endif
398 
399 #endif	// _MZ80_H_
400