summaryrefslogtreecommitdiff
path: root/z80_private.h
blob: 08dc543a797520b19ef7d09b806f0e4f323ed0af (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
/*

    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$"

#ifndef TRUE
#define TRUE	1
#endif

#ifndef FALSE
#define FALSE	0
#endif

#define MAX_PER_CALLBACK	10


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

struct Z80
{
    Z80Val		cycle;

    Z80Word		PC;

    Z80Byte		A;
    Z80Byte		F;
    Z80Word		BC;
    Z80Word		DE;
    Z80Word		HL;

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

    Z80Word		IX;
    Z80Word		IY;

    Z80Word		SP;

    Z80Byte		IFF1;
    Z80Byte		IFF2;
    Z80Byte		IM;
    Z80Byte		I;
    Z80Byte		R;
    int			halt;

    int			use_cb_off;
    Z80Relative		cb_off;

    Z80Byte		shift;

    int			raise;
    Z80Byte		devbyte;
    int			nmi;

    Z80Byte		*memory;
    int			*memctrl;

    Z80ReadPort		pread;
    Z80WritePort	pwrite;

    Z80Callback		callback[eZ80_NO_CALLBACK][MAX_PER_CALLBACK];

    int			last_cb;
};


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

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


/* HI/LO
*/
#define SET_HI(reg,v)	reg = (reg & 0x00ff) | ((v) << 8)
#define SET_LO(reg,v)	reg = (reg & 0xff00) | ((v) & 0xff)
#define GET_HI(reg,v)	((reg & 0xff00) >> 8)
#define GET_LO(reg,v)	(reg & 0xff)

/* 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->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->F,f)
#define CLRFLAG(f)		CLR(cpu->F,f)

#define PEEK(addr)		(cpu->memory[addr])
#define PEEKW(addr)		(PEEK(addr) | (Z80Word)PEEK(addr+1)<<8)

#define POKE(addr,val)		do					\
				{					\
				    if (cpu->memctrl[(addr)/256])	\
				    {					\
				    	cpu->memory[addr]=val;		\
				    }					\
				while (0)(cpu->write(cpu,addr,val))
#define POKEW(addr,val)		do					\
				{					\
				    POKE(addr,val);			\
				    POKE(addr+1,val>>8);		\
				} while(0)

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

#define IS_C			(cpu->F&C_Z80)
#define IS_N			(cpu->F&N_Z80)
#define IS_P			(cpu->F&P_Z80)
#define IS_H			(cpu->F&H_Z80)
#define IS_Z			(cpu->F&Z_Z80)
#define IS_S			(cpu->F&S_Z80)

#define CARRY			IS_C

#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)

#define PUSH(REG)		do				\
				{				\
				    cpu->SP-=2;			\
				    POKEW(cpu->SP,REG);		\
				} while(0)
#define POP(REG)		do				\
				{				\
				    REG=PEEKW(cpu->SP);		\
				    cpu->SP+=2;			\
				} while(0)

#define SETHIDDEN(res)		cpu->F=(cpu->F&~(B3_Z80|B5_Z80))|\
					((res)&(B3_Z80|B5_Z80))



/* ---------------------------------------- FLAG TABLES
*/
extern Z80Byte			PSZtable[512];
extern Z80Byte			SZtable[512];
extern Z80Byte			Ptable[512];
extern Z80Byte			Stable[512];
extern Z80Byte			Ztable[512];


/* ---------------------------------------- GLOBAL GENERAL OPCODES/ROUTINES
*/
void Z80_Decode(Z80 *cpu);


#endif

/* END OF FILE */