summaryrefslogtreecommitdiff
path: root/src/z80_private.h
blob: d031497819cf4d07dac66a73de2c20317707f18b (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*

    z80 - Z80 emulation

    Copyright (C) 2006  Ian Cowburn (ianc@noddybox.demon.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$

    Private macros for Z80

*/

#ifndef Z80_PRIVATE_H
#define Z80_PRIVATE_H "$Id$"

#include "z80_config.h"

#ifndef TRUE
#define TRUE	1
#endif

#ifndef FALSE
#define FALSE	0
#endif

#define MAX_PER_CALLBACK	10


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

typedef signed short	sword;

typedef union
{
    Z80Word		w;
    Z80Byte		b[2];
} Z80Reg;

struct Z80
{
    Z80Val		cycle;

    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;
    int			halt;

    Z80Byte		shift;

    int			raise;
    Z80Byte		devbyte;
    int			nmi;

#ifndef ENABLE_ARRAY_MEMORY
    Z80ReadMemory	disread;

    Z80ReadMemory	mread;
    Z80WriteMemory	mwrite;
#endif

    Z80ReadPort		pread;
    Z80WritePort	pwrite;

    Z80Callback		callback[eZ80_NO_CALLBACK][MAX_PER_CALLBACK];

    int			last_cb;
};


/* ---------------------------------------- ARRAY MEMORY
*/

#ifdef ENABLE_ARRAY_MEMORY
extern Z80Byte	Z80_MEMORY[];
#endif


/* ---------------------------------------- MACROS
*/

/* NOTE: A lot of these macros assume you have a variable called 'cpu'
         which is a pointer to Z80
*/


/* Invoke a callback class
*/
#define CALLBACK(r,d)	do					\
			{					\
			int f;					\
								\
			for(f=0;f<MAX_PER_CALLBACK;f++)		\
			    if (cpu->callback[r][f])		\
				cpu->last_cb &=			\
				    cpu->callback[r][f](cpu,d);	\
			} while(0)

/* Flag register
*/
#define C_Z80			0x01
#define N_Z80			0x02
#define P_Z80			0x04
#define V_Z80			P_Z80
#define H_Z80			0x10
#define Z_Z80			0x40
#define S_Z80			0x80

#define B3_Z80			0x08
#define B5_Z80			0x20


#define SET(v,b)		(v)|=b
#define CLR(v,b)		(v)&=~(b)

#define SETFLAG(f)		SET(cpu->AF.b[LO],f)
#define CLRFLAG(f)		CLR(cpu->AF.b[LO],f)

#ifdef ENABLE_ARRAY_MEMORY

#define PEEK(addr)		Z80_MEMORY[addr]

static inline Z80Word PEEKW(Z80Word addr)
{
    return (PEEK(addr) | (Z80Word)PEEK(addr+1)<<8);
}

#define POKE(addr,val)		do					\
				{					\
				    Z80Word ba=addr;			\
				    if (ba>=RAMBOT && ba<=RAMTOP)	\
				    	Z80_MEMORY[ba]=val;		\
				} while(0)

#define POKEW(addr,val)		do					\
				{					\
				    Z80Word wa=addr;			\
				    Z80Word wv=val;			\
				    POKE(wa,wv);			\
				    POKE(wa+1,wv>>8);			\
				} while(0)


#define FETCH_BYTE		(Z80_MEMORY[cpu->PC++])
#define FETCH_WORD              (cpu->PC+=2,				\
				    Z80_MEMORY[cpu->PC-2]|		\
					((Z80Word)Z80_MEMORY[cpu->PC-1]<<8))

#else

#define PEEK(addr)		(cpu->mread(cpu,addr))
#define PEEKW(addr)		FPEEKW(cpu,addr)

#define POKE(addr,val)		cpu->mwrite(cpu,addr,val)
#define POKEW(addr,val)		FPOKEW(cpu,addr,val)

#define FETCH_BYTE		(cpu->mread(cpu,cpu->PC++))
#define FETCH_WORD		(cpu->PC+=2,FPEEKW(cpu,cpu->PC-2))

#endif


#define IS_C			(cpu->AF.b[LO]&C_Z80)
#define IS_N			(cpu->AF.b[LO]&N_Z80)
#define IS_P			(cpu->AF.b[LO]&P_Z80)
#define IS_H			(cpu->AF.b[LO]&H_Z80)
#define IS_Z			(cpu->AF.b[LO]&Z_Z80)
#define IS_S			(cpu->AF.b[LO]&S_Z80)

#define CARRY			IS_C

#define IS_IX_IY		(cpu->shift==0xdd || cpu->shift==0xfd)
#define OFFSET(off)		off=(IS_IX_IY ? (Z80Relative)FETCH_BYTE:0)

#define TSTATE(n)		cpu->cycle+=n

#define ADD_R(v)		cpu->R=((cpu->R&0x80)|((cpu->R+(v))&0x7f))
#define INC_R			ADD_R(1)

#ifdef ENABLE_ARRAY_MEMORY

#define PUSH(REG)		do					\
				{					\
				    Z80Word pv=REG;			\
				    cpu->SP-=2;				\
				    POKE(cpu->SP,pv);			\
				    POKE(cpu->SP+1,pv>>8);		\
				} while(0)

#else

#define PUSH(REG)		do					\
				{					\
				    Z80Word pushv=REG;			\
				    cpu->SP-=2;				\
				    cpu->mwrite(cpu,cpu->SP,pushv);	\
				    cpu->mwrite(cpu,cpu->SP+1,pushv>>8);\
				} while(0)
#endif

#define POP(REG)		do					\
				{					\
				    REG=PEEK(cpu->SP) |			\
				    	(Z80Word)PEEK(cpu->SP+1)<<8;	\
				    cpu->SP+=2;				\
				} while(0)

#define SETHIDDEN(res)		cpu->AF.b[LO]=(cpu->AF.b[LO]&~(B3_Z80|B5_Z80))|\
					((res)&(B3_Z80|B5_Z80))

#define CALL			do				\
				{				\
				    PUSH(cpu->PC+2);		\
				    cpu->PC=PEEKW(cpu->PC);	\
				} while(0)

#define NOCALL			cpu->PC+=2
#define JP			cpu->PC=PEEKW(cpu->PC)
#define NOJP			cpu->PC+=2
#define JR			cpu->PC+=(Z80Relative)PEEK(cpu->PC)+1
#define NOJR			cpu->PC++

#define OUT(P,V)		do				\
				{				\
				    if (cpu->pwrite)		\
				    	cpu->pwrite(cpu,P,V);	\
				} while(0)

#define IN(P)			(cpu->pread?cpu->pread(cpu,P):0)



/* ---------------------------------------- LABELS
*/
extern Z80Label			*z80_labels;


/* ---------------------------------------- GLOBAL GENERAL OPCODES/ROUTINES
*/
void Z80_Decode(Z80 *cpu, Z80Byte opcode);
void Z80_InitialiseInternals(void);


/* ---------------------------------------- DISASSEMBLY
*/
#ifdef ENABLE_DISASSEM
typedef void		(*DIS_OP_CALLBACK)(Z80 *z80, Z80Byte op, Z80Word *pc);

extern DIS_OP_CALLBACK	dis_CB_opcode[];
extern DIS_OP_CALLBACK	dis_DD_opcode[];
extern DIS_OP_CALLBACK	dis_DD_CB_opcode[];
extern DIS_OP_CALLBACK	dis_ED_opcode[];
extern DIS_OP_CALLBACK	dis_FD_opcode[];
extern DIS_OP_CALLBACK	dis_FD_CB_opcode[];
extern DIS_OP_CALLBACK	dis_opcode_z80[];

const char	*Z80_Dis_Printf(const char *format, ...);

Z80Byte		Z80_Dis_FetchByte(Z80 *cpu, Z80Word *pc);
Z80Word		Z80_Dis_FetchWord(Z80 *cpu, Z80Word *pc);

void		Z80_Dis_Set(const char *op, const char *arg);
const char	*Z80_Dis_GetOp(void);
const char	*Z80_Dis_GetArg(void);
#endif	/* ENABLE_DISASSEM */

#endif	/* Z80_PRIVATE_H */

/* END OF FILE */