1 /*
2  * Z80SIM  -  a Z80-CPU simulator
3  *
4  * Copyright (C) 1987-2021 by Udo Munk
5  *
6  * History:
7  * 28-SEP-87 Development on TARGON/35 with AT&T Unix System V.3
8  * 11-JAN-89 Release 1.1
9  * 08-FEB-89 Release 1.2
10  * 13-MAR-89 Release 1.3
11  * 09-FEB-90 Release 1.4  Ported to TARGON/31 M10/30
12  * 20-DEC-90 Release 1.5  Ported to COHERENT 3.0
13  * 10-JUN-92 Release 1.6  long casting problem solved with COHERENT 3.2
14  *			  and some optimisation
15  * 25-JUN-92 Release 1.7  comments in english and ported to COHERENT 4.0
16  * 02-OCT-06 Release 1.8  modified to compile on modern POSIX OS's
17  * 18-NOV-06 Release 1.9  modified to work with CP/M sources
18  * 08-DEC-06 Release 1.10 modified MMU for working with CP/NET
19  * 17-DEC-06 Release 1.11 TCP/IP sockets for CP/NET
20  * 25-DEC-06 Release 1.12 CPU speed option
21  * 19-FEB-07 Release 1.13 various improvements
22  * 06-OCT-07 Release 1.14 bug fixes and improvements
23  * 06-AUG-08 Release 1.15 many improvements and Windows support via Cygwin
24  * 25-AUG-08 Release 1.16 console status I/O loop detection and line discipline
25  * 20-OCT-08 Release 1.17 frontpanel integrated and Altair/IMSAI emulations
26  * 24-JAN-14 Release 1.18 bug fixes and improvements
27  * 02-MAR-14 Release 1.19 source cleanup and improvements
28  * 14-MAR-14 Release 1.20 added Tarbell SD FDC and printer port to Altair
29  * 29-MAR-14 Release 1.21 many improvements
30  * 29-MAY-14 Release 1.22 improved networking and bugfixes
31  * 04-JUN-14 Release 1.23 added 8080 emulation
32  * 06-SEP-14 Release 1.24 bugfixes and improvements
33  * 18-FEB-15 Release 1.25 bugfixes, improvements, added Cromemco Z-1
34  * 18-APR-15 Release 1.26 bugfixes and improvements
35  * 18-JAN-16 Release 1.27 bugfixes and improvements
36  * 05-MAY-16 Release 1.28 improved usability
37  * 20-NOV-16 Release 1.29 bugfixes and improvements
38  * 15-DEC-16 Release 1.30 improved memory management, machine cycle correct CPUs
39  * 28-DEC-16 Release 1.31 improved memory management, reimplemented MMUs
40  * 12-JAN-17 Release 1.32 improved configurations, front panel, added IMSAI VIO
41  * 07-FEB-17 Release 1.33 bugfixes, improvements, better front panels
42  * 16-MAR-17 Release 1.34 improvements, added ProcTec VDM-1
43  * 03-AUG-17 Release 1.35 added UNIX sockets, bugfixes, improvements
44  * 21-DEC-17 Release 1.36 bugfixes and improvements
45  * 06-JAN-21 Release 1.37 bugfixes and improvements
46  */
47 
48 #include <unistd.h>
49 #include <stdio.h>
50 #include <sys/time.h>
51 #include "sim.h"
52 #include "simglb.h"
53 #include "config.h"
54 #ifdef FRONTPANEL
55 #include "../../frontpanel/frontpanel.h"
56 #endif
57 #include "memory.h"
58 
59 #ifdef WANT_GUI
60 void check_gui_break(void);
61 #endif
62 
63 static int op_nop(void), op_halt(void), op_scf(void);
64 static int op_ccf(void), op_cpl(void), op_daa(void);
65 static int op_ei(void), op_di(void);
66 static int op_in(void), op_out(void);
67 static int op_ldan(void), op_ldbn(void), op_ldcn(void);
68 static int op_lddn(void), op_lden(void);
69 static int op_ldhn(void), op_ldln(void);
70 static int op_ldabc(void), op_ldade(void), op_ldann(void);
71 static int op_ldbca(void), op_lddea(void), op_ldnna(void);
72 static int op_ldhla(void), op_ldhlb(void), op_ldhlc(void), op_ldhld(void);
73 static int op_ldhle(void), op_ldhlh(void), op_ldhll(void), op_ldhl1(void);
74 static int op_ldaa(void), op_ldab(void), op_ldac(void);
75 static int op_ldad(void), op_ldae(void);
76 static int op_ldah(void), op_ldal(void), op_ldahl(void);
77 static int op_ldba(void), op_ldbb(void), op_ldbc(void);
78 static int op_ldbd(void), op_ldbe(void);
79 static int op_ldbh(void), op_ldbl(void), op_ldbhl(void);
80 static int op_ldca(void), op_ldcb(void), op_ldcc(void);
81 static int op_ldcd(void), op_ldce(void);
82 static int op_ldch(void), op_ldcl(void), op_ldchl(void);
83 static int op_ldda(void), op_lddb(void), op_lddc(void);
84 static int op_lddd(void), op_ldde(void);
85 static int op_lddh(void), op_lddl(void), op_lddhl(void);
86 static int op_ldea(void), op_ldeb(void), op_ldec(void);
87 static int op_lded(void), op_ldee(void);
88 static int op_ldeh(void), op_ldel(void), op_ldehl(void);
89 static int op_ldha(void), op_ldhb(void), op_ldhc(void);
90 static int op_ldhd(void), op_ldhe(void);
91 static int op_ldhh(void), op_ldhl(void), op_ldhhl(void);
92 static int op_ldla(void), op_ldlb(void), op_ldlc(void);
93 static int op_ldld(void), op_ldle(void);
94 static int op_ldlh(void), op_ldll(void), op_ldlhl(void);
95 static int op_ldbcnn(void), op_lddenn(void), op_ldhlnn(void);
96 static int op_ldspnn(void), op_ldsphl(void);
97 static int op_ldhlin(void), op_ldinhl(void);
98 static int op_incbc(void), op_incde(void), op_inchl(void), op_incsp(void);
99 static int op_decbc(void), op_decde(void), op_dechl(void), op_decsp(void);
100 static int op_adhlbc(void), op_adhlde(void), op_adhlhl(void), op_adhlsp(void);
101 static int op_anda(void), op_andb(void), op_andc(void), op_andd(void), op_ande(void);
102 static int op_andh(void), op_andl(void), op_andhl(void), op_andn(void);
103 static int op_ora(void), op_orb(void), op_orc(void), op_ord(void), op_ore(void);
104 static int op_orh(void), op_orl(void), op_orhl(void), op_orn(void);
105 static int op_xora(void), op_xorb(void), op_xorc(void), op_xord(void), op_xore(void);
106 static int op_xorh(void), op_xorl(void), op_xorhl(void), op_xorn(void);
107 static int op_adda(void), op_addb(void), op_addc(void), op_addd(void), op_adde(void);
108 static int op_addh(void), op_addl(void), op_addhl(void), op_addn(void);
109 static int op_adca(void), op_adcb(void), op_adcc(void), op_adcd(void), op_adce(void);
110 static int op_adch(void), op_adcl(void), op_adchl(void), op_adcn(void);
111 static int op_suba(void), op_subb(void), op_subc(void), op_subd(void), op_sube(void);
112 static int op_subh(void), op_subl(void), op_subhl(void), op_subn(void);
113 static int op_sbca(void), op_sbcb(void), op_sbcc(void), op_sbcd(void), op_sbce(void);
114 static int op_sbch(void), op_sbcl(void), op_sbchl(void), op_sbcn(void);
115 static int op_cpa(void), op_cpb(void), op_cpc(void), op_cpd(void), op_cpe(void);
116 static int op_cph(void), op_cplr(void), op_cphl(void), op_cpn(void);
117 static int op_inca(void), op_incb(void), op_incc(void), op_incd(void), op_ince(void);
118 static int op_inch(void), op_incl(void), op_incihl(void);
119 static int op_deca(void), op_decb(void), op_decc(void), op_decd(void), op_dece(void);
120 static int op_dech(void), op_decl(void), op_decihl(void);
121 static int op_rlca(void), op_rrca(void),op_rla(void),op_rra(void);
122 static int op_exdehl(void), op_exafaf(void), op_exx(void), op_exsphl(void);
123 static int op_pushaf(void), op_pushbc(void), op_pushde(void), op_pushhl(void);
124 static int op_popaf(void), op_popbc(void), op_popde(void), op_pophl(void);
125 static int op_jp(void), op_jphl(void), op_jr(void), op_djnz(void), op_call(void), op_ret(void);
126 static int op_jpz(void), op_jpnz(void), op_jpc(void), op_jpnc(void);
127 static int op_jppe(void), op_jppo(void), op_jpm(void), op_jpp(void);
128 static int op_calz(void), op_calnz(void), op_calc(void), op_calnc(void);
129 static int op_calpe(void), op_calpo(void), op_calm(void), op_calp(void);
130 static int op_retz(void), op_retnz(void), op_retc(void), op_retnc(void);
131 static int op_retpe(void), op_retpo(void), op_retm(void), op_retp(void);
132 static int op_jrz(void), op_jrnz(void), op_jrc(void), op_jrnc(void);
133 static int op_rst00(void), op_rst08(void), op_rst10(void), op_rst18(void);
134 static int op_rst20(void), op_rst28(void), op_rst30(void), op_rst38(void);
135 extern int op_cb_handel(void), op_dd_handel(void);
136 extern int op_ed_handel(void), op_fd_handel(void);
137 
138 /*
139  *	This function builds the Z80 central processing unit.
140  *	The opcode where PC points to is fetched from the memory
141  *	and PC incremented by one. The opcode is used as an
142  *	index to an array with function pointers, to execute a
143  *	function which emulates this Z80 opcode.
144  */
cpu_z80(void)145 void cpu_z80(void)
146 {
147 	extern int time_diff(struct timeval *, struct timeval *);
148 
149 	static int (*op_sim[256]) (void) = {
150 		op_nop,				/* 0x00 */
151 		op_ldbcnn,			/* 0x01 */
152 		op_ldbca,			/* 0x02 */
153 		op_incbc,			/* 0x03 */
154 		op_incb,			/* 0x04 */
155 		op_decb,			/* 0x05 */
156 		op_ldbn,			/* 0x06 */
157 		op_rlca,			/* 0x07 */
158 		op_exafaf,			/* 0x08 */
159 		op_adhlbc,			/* 0x09 */
160 		op_ldabc,			/* 0x0a */
161 		op_decbc,			/* 0x0b */
162 		op_incc,			/* 0x0c */
163 		op_decc,			/* 0x0d */
164 		op_ldcn,			/* 0x0e */
165 		op_rrca,			/* 0x0f */
166 		op_djnz,			/* 0x10 */
167 		op_lddenn,			/* 0x11 */
168 		op_lddea,			/* 0x12 */
169 		op_incde,			/* 0x13 */
170 		op_incd,			/* 0x14 */
171 		op_decd,			/* 0x15 */
172 		op_lddn,			/* 0x16 */
173 		op_rla,				/* 0x17 */
174 		op_jr,				/* 0x18 */
175 		op_adhlde,			/* 0x19 */
176 		op_ldade,			/* 0x1a */
177 		op_decde,			/* 0x1b */
178 		op_ince,			/* 0x1c */
179 		op_dece,			/* 0x1d */
180 		op_lden,			/* 0x1e */
181 		op_rra,				/* 0x1f */
182 		op_jrnz,			/* 0x20 */
183 		op_ldhlnn,			/* 0x21 */
184 		op_ldinhl,			/* 0x22 */
185 		op_inchl,			/* 0x23 */
186 		op_inch,			/* 0x24 */
187 		op_dech,			/* 0x25 */
188 		op_ldhn,			/* 0x26 */
189 		op_daa,				/* 0x27 */
190 		op_jrz,				/* 0x28 */
191 		op_adhlhl,			/* 0x29 */
192 		op_ldhlin,			/* 0x2a */
193 		op_dechl,			/* 0x2b */
194 		op_incl,			/* 0x2c */
195 		op_decl,			/* 0x2d */
196 		op_ldln,			/* 0x2e */
197 		op_cpl,				/* 0x2f */
198 		op_jrnc,			/* 0x30 */
199 		op_ldspnn,			/* 0x31 */
200 		op_ldnna,			/* 0x32 */
201 		op_incsp,			/* 0x33 */
202 		op_incihl,			/* 0x34 */
203 		op_decihl,			/* 0x35 */
204 		op_ldhl1,			/* 0x36 */
205 		op_scf,				/* 0x37 */
206 		op_jrc,				/* 0x38 */
207 		op_adhlsp,			/* 0x39 */
208 		op_ldann,			/* 0x3a */
209 		op_decsp,			/* 0x3b */
210 		op_inca,			/* 0x3c */
211 		op_deca,			/* 0x3d */
212 		op_ldan,			/* 0x3e */
213 		op_ccf,				/* 0x3f */
214 		op_ldbb,			/* 0x40 */
215 		op_ldbc,			/* 0x41 */
216 		op_ldbd,			/* 0x42 */
217 		op_ldbe,			/* 0x43 */
218 		op_ldbh,			/* 0x44 */
219 		op_ldbl,			/* 0x45 */
220 		op_ldbhl,			/* 0x46 */
221 		op_ldba,			/* 0x47 */
222 		op_ldcb,			/* 0x48 */
223 		op_ldcc,			/* 0x49 */
224 		op_ldcd,			/* 0x4a */
225 		op_ldce,			/* 0x4b */
226 		op_ldch,			/* 0x4c */
227 		op_ldcl,			/* 0x4d */
228 		op_ldchl,			/* 0x4e */
229 		op_ldca,			/* 0x4f */
230 		op_lddb,			/* 0x50 */
231 		op_lddc,			/* 0x51 */
232 		op_lddd,			/* 0x52 */
233 		op_ldde,			/* 0x53 */
234 		op_lddh,			/* 0x54 */
235 		op_lddl,			/* 0x55 */
236 		op_lddhl,			/* 0x56 */
237 		op_ldda,			/* 0x57 */
238 		op_ldeb,			/* 0x58 */
239 		op_ldec,			/* 0x59 */
240 		op_lded,			/* 0x5a */
241 		op_ldee,			/* 0x5b */
242 		op_ldeh,			/* 0x5c */
243 		op_ldel,			/* 0x5d */
244 		op_ldehl,			/* 0x5e */
245 		op_ldea,			/* 0x5f */
246 		op_ldhb,			/* 0x60 */
247 		op_ldhc,			/* 0x61 */
248 		op_ldhd,			/* 0x62 */
249 		op_ldhe,			/* 0x63 */
250 		op_ldhh,			/* 0x64 */
251 		op_ldhl,			/* 0x65 */
252 		op_ldhhl,			/* 0x66 */
253 		op_ldha,			/* 0x67 */
254 		op_ldlb,			/* 0x68 */
255 		op_ldlc,			/* 0x69 */
256 		op_ldld,			/* 0x6a */
257 		op_ldle,			/* 0x6b */
258 		op_ldlh,			/* 0x6c */
259 		op_ldll,			/* 0x6d */
260 		op_ldlhl,			/* 0x6e */
261 		op_ldla,			/* 0x6f */
262 		op_ldhlb,			/* 0x70 */
263 		op_ldhlc,			/* 0x71 */
264 		op_ldhld,			/* 0x72 */
265 		op_ldhle,			/* 0x73 */
266 		op_ldhlh,			/* 0x74 */
267 		op_ldhll,			/* 0x75 */
268 		op_halt,			/* 0x76 */
269 		op_ldhla,			/* 0x77 */
270 		op_ldab,			/* 0x78 */
271 		op_ldac,			/* 0x79 */
272 		op_ldad,			/* 0x7a */
273 		op_ldae,			/* 0x7b */
274 		op_ldah,			/* 0x7c */
275 		op_ldal,			/* 0x7d */
276 		op_ldahl,			/* 0x7e */
277 		op_ldaa,			/* 0x7f */
278 		op_addb,			/* 0x80 */
279 		op_addc,			/* 0x81 */
280 		op_addd,			/* 0x82 */
281 		op_adde,			/* 0x83 */
282 		op_addh,			/* 0x84 */
283 		op_addl,			/* 0x85 */
284 		op_addhl,			/* 0x86 */
285 		op_adda,			/* 0x87 */
286 		op_adcb,			/* 0x88 */
287 		op_adcc,			/* 0x89 */
288 		op_adcd,			/* 0x8a */
289 		op_adce,			/* 0x8b */
290 		op_adch,			/* 0x8c */
291 		op_adcl,			/* 0x8d */
292 		op_adchl,			/* 0x8e */
293 		op_adca,			/* 0x8f */
294 		op_subb,			/* 0x90 */
295 		op_subc,			/* 0x91 */
296 		op_subd,			/* 0x92 */
297 		op_sube,			/* 0x93 */
298 		op_subh,			/* 0x94 */
299 		op_subl,			/* 0x95 */
300 		op_subhl,			/* 0x96 */
301 		op_suba,			/* 0x97 */
302 		op_sbcb,			/* 0x98 */
303 		op_sbcc,			/* 0x99 */
304 		op_sbcd,			/* 0x9a */
305 		op_sbce,			/* 0x9b */
306 		op_sbch,			/* 0x9c */
307 		op_sbcl,			/* 0x9d */
308 		op_sbchl,			/* 0x9e */
309 		op_sbca,			/* 0x9f */
310 		op_andb,			/* 0xa0 */
311 		op_andc,			/* 0xa1 */
312 		op_andd,			/* 0xa2 */
313 		op_ande,			/* 0xa3 */
314 		op_andh,			/* 0xa4 */
315 		op_andl,			/* 0xa5 */
316 		op_andhl,			/* 0xa6 */
317 		op_anda,			/* 0xa7 */
318 		op_xorb,			/* 0xa8 */
319 		op_xorc,			/* 0xa9 */
320 		op_xord,			/* 0xaa */
321 		op_xore,			/* 0xab */
322 		op_xorh,			/* 0xac */
323 		op_xorl,			/* 0xad */
324 		op_xorhl,			/* 0xae */
325 		op_xora,			/* 0xaf */
326 		op_orb,				/* 0xb0 */
327 		op_orc,				/* 0xb1 */
328 		op_ord,				/* 0xb2 */
329 		op_ore,				/* 0xb3 */
330 		op_orh,				/* 0xb4 */
331 		op_orl,				/* 0xb5 */
332 		op_orhl,			/* 0xb6 */
333 		op_ora,				/* 0xb7 */
334 		op_cpb,				/* 0xb8 */
335 		op_cpc,				/* 0xb9 */
336 		op_cpd,				/* 0xba */
337 		op_cpe,				/* 0xbb */
338 		op_cph,				/* 0xbc */
339 		op_cplr,			/* 0xbd */
340 		op_cphl,			/* 0xbe */
341 		op_cpa,				/* 0xbf */
342 		op_retnz,			/* 0xc0 */
343 		op_popbc,			/* 0xc1 */
344 		op_jpnz,			/* 0xc2 */
345 		op_jp,				/* 0xc3 */
346 		op_calnz,			/* 0xc4 */
347 		op_pushbc,			/* 0xc5 */
348 		op_addn,			/* 0xc6 */
349 		op_rst00,			/* 0xc7 */
350 		op_retz,			/* 0xc8 */
351 		op_ret,				/* 0xc9 */
352 		op_jpz,				/* 0xca */
353 		op_cb_handel,			/* 0xcb */
354 		op_calz,			/* 0xcc */
355 		op_call,			/* 0xcd */
356 		op_adcn,			/* 0xce */
357 		op_rst08,			/* 0xcf */
358 		op_retnc,			/* 0xd0 */
359 		op_popde,			/* 0xd1 */
360 		op_jpnc,			/* 0xd2 */
361 		op_out,				/* 0xd3 */
362 		op_calnc,			/* 0xd4 */
363 		op_pushde,			/* 0xd5 */
364 		op_subn,			/* 0xd6 */
365 		op_rst10,			/* 0xd7 */
366 		op_retc,			/* 0xd8 */
367 		op_exx,				/* 0xd9 */
368 		op_jpc,				/* 0xda */
369 		op_in,				/* 0xdb */
370 		op_calc,			/* 0xdc */
371 		op_dd_handel,			/* 0xdd */
372 		op_sbcn,			/* 0xde */
373 		op_rst18,			/* 0xdf */
374 		op_retpo,			/* 0xe0 */
375 		op_pophl,			/* 0xe1 */
376 		op_jppo,			/* 0xe2 */
377 		op_exsphl,			/* 0xe3 */
378 		op_calpo,			/* 0xe4 */
379 		op_pushhl,			/* 0xe5 */
380 		op_andn,			/* 0xe6 */
381 		op_rst20,			/* 0xe7 */
382 		op_retpe,			/* 0xe8 */
383 		op_jphl,			/* 0xe9 */
384 		op_jppe,			/* 0xea */
385 		op_exdehl,			/* 0xeb */
386 		op_calpe,			/* 0xec */
387 		op_ed_handel,			/* 0xed */
388 		op_xorn,			/* 0xee */
389 		op_rst28,			/* 0xef */
390 		op_retp,			/* 0xf0 */
391 		op_popaf,			/* 0xf1 */
392 		op_jpp,				/* 0xf2 */
393 		op_di,				/* 0xf3 */
394 		op_calp,			/* 0xf4 */
395 		op_pushaf,			/* 0xf5 */
396 		op_orn,				/* 0xf6 */
397 		op_rst30,			/* 0xf7 */
398 		op_retm,			/* 0xf8 */
399 		op_ldsphl,			/* 0xf9 */
400 		op_jpm,				/* 0xfa */
401 		op_ei,				/* 0xfb */
402 		op_calm,			/* 0xfc */
403 		op_fd_handel,			/* 0xfd */
404 		op_cpn,				/* 0xfe */
405 		op_rst38			/* 0xff */
406 	};
407 
408 	register int t = 0;
409 	register int states;
410 	struct timeval t1, t2;
411 	int tdiff;
412 	WORD p;
413 
414 	gettimeofday(&t1, NULL);
415 
416 	do {
417 
418 #ifdef HISIZE
419 		/* write history */
420 		his[h_next].h_adr = PC;
421 		his[h_next].h_af = (A << 8) + F;
422 		his[h_next].h_bc = (B << 8) + C;
423 		his[h_next].h_de = (D << 8) + E;
424 		his[h_next].h_hl = (H << 8) + L;
425 		his[h_next].h_ix = IX;
426 		his[h_next].h_iy = IY;
427 		his[h_next].h_sp = SP;
428 		h_next++;
429 		if (h_next == HISIZE) {
430 			h_flag = 1;
431 			h_next = 0;
432 		}
433 #endif
434 
435 #ifdef WANT_TIM
436 		/* check for start address of runtime measurement */
437 		if (PC == t_start && !t_flag) {
438 			t_flag = 1;	/* switch measurement on */
439 			t_states = 0L;	/* initialise counted T-states */
440 		}
441 #endif
442 
443 		/* CPU interrupt handling */
444 		if (int_nmi) {		/* non maskable interrupt */
445 			IFF <<= 1 & 3;
446 			memwrt(--SP, PC >> 8);
447 			memwrt(--SP, PC);
448 			PC = 0x66;
449 			int_nmi = 0;
450 			t += 11;
451 		}
452 
453 		if (int_int) {		/* maskable interrupt */
454 			if (IFF != 3)
455 				goto leave;
456 			if (int_protection)	/* protect first instr */
457 				goto leave;
458 
459 			IFF = 0;
460 
461 #ifdef BUS_8080
462 			if (!(cpu_bus & CPU_HLTA)) {
463 				cpu_bus = CPU_WO | CPU_M1 | CPU_INTA;
464 #endif
465 #ifdef FRONTPANEL
466 				fp_clock += 1000;
467 				fp_led_data = (int_data != -1) ?
468 						(BYTE) int_data : 0xff;
469 				fp_sampleData();
470 				wait_int_step();
471 				if (cpu_state & RESET)
472 					goto leave;
473 #endif
474 #ifdef BUS_8080
475 			}
476 			cpu_bus = 0;
477 #endif
478 
479 			switch (int_mode) {
480 			case 0:		/* IM 0 */
481 				memwrt(--SP, PC >> 8);
482 				memwrt(--SP, PC);
483 #ifdef FRONTPANEL
484 				if (cpu_state & RESET)
485 					goto leave;
486 #endif
487 				switch (int_data) {
488 				case 0xc7: /* RST 00H */
489 					PC = 0;
490 					break;
491 				case 0xcf: /* RST 08H */
492 					PC = 8;
493 					break;
494 				case 0xd7: /* RST 10H */
495 					PC = 0x10;
496 					break;
497 				case 0xdf: /* RST 18H */
498 					PC = 0x18;
499 					break;
500 				case 0xe7: /* RST 20H */
501 					PC = 0x20;
502 					break;
503 				case 0xef: /* RST 28H */
504 					PC = 0x28;
505 					break;
506 				case 0xf7: /* RST 30H */
507 					PC = 0x30;
508 					break;
509 				case 0xff: /* RST 38H */
510 					PC = 0x38;
511 					break;
512 				case -1: /* no data = 0xff on S100 bus */
513 					PC = 0x38;
514 					break;
515 				default: /* unsupported bus data */
516 					cpu_error = INTERROR;
517 					cpu_state = STOPPED;
518 					continue;
519 				}
520 				t += 13;
521 				break;
522 			case 1:		/* IM 1 */
523 				memwrt(--SP, PC >> 8);
524 				memwrt(--SP, PC);
525 #ifdef FRONTPANEL
526 				if (cpu_state & RESET)
527 					goto leave;
528 #endif
529 				PC = 0x38;
530 				t += 13;
531 				break;
532 			case 2:		/* IM 2 */
533 				memwrt(--SP, PC >> 8);
534 				memwrt(--SP, PC);
535 #ifdef FRONTPANEL
536 				if (cpu_state & RESET)
537 					goto leave;
538 #endif
539 				p = (I << 8) + (int_data & 0xff);
540 				PC = memrdr(p++);
541 				PC += memrdr(p) << 8;
542 				t += 19;
543 				break;
544 			}
545 			int_int = 0;
546 			int_data = -1;
547 #ifdef FRONTPANEL
548 			m1_step = 1;
549 #endif
550 		}
551 leave:
552 
553 #ifdef BUS_8080
554 		/* M1 opcode fetch */
555 		cpu_bus = CPU_WO | CPU_M1 | CPU_MEMR;
556 #endif
557 
558 		int_protection = 0;
559 		states = (*op_sim[memrdr(PC++)]) (); /* execute next opcode */
560 		t += states;
561 
562 		if (f_flag) {			/* adjust CPU speed */
563 			if (t >= tmax && !cpu_needed) {
564 				gettimeofday(&t2, NULL);
565 				tdiff = time_diff(&t1, &t2);
566 #ifndef __CYGWIN__
567 				if ((tdiff > 0) && (tdiff < 10000))
568 					SLEEP_MS(10 - (tdiff / 1000));
569 #else
570 				/* timer resolution seems to be 20ms,
571 				   sleeps < 10 won't work at all */
572 				tdiff++; /* avoid compiler warning */
573 				SLEEP_MS(10);
574 #endif
575 				t = 0;
576 				gettimeofday(&t1, NULL);
577 			}
578 		}
579 
580 		R++;			/* increment refresh register */
581 
582 					/* do runtime measurement */
583 #ifdef WANT_TIM
584 		if (t_flag) {
585 			t_states += states; /* add T-states for this opcode */
586 			if (PC == t_end)	/* check for end address */
587 				t_flag = 0;	/* if reached, switch off */
588 		}
589 #endif
590 
591 #ifdef WANT_GUI
592                 check_gui_break();
593 #endif
594 
595 	} while (cpu_state == CONTIN_RUN);
596 
597 #ifdef BUS_8080
598 	if (!(cpu_bus & CPU_INTA))
599 		cpu_bus = CPU_WO | CPU_M1 | CPU_MEMR;
600 #endif
601 #ifdef FRONTPANEL
602 	fp_led_address = PC;
603 	fp_led_data = getmem(PC);
604 	fp_clock++;
605 	fp_sampleData();
606 #endif
607 }
608 
op_nop(void)609 static int op_nop(void)			/* NOP */
610 {
611 	return(4);
612 }
613 
op_halt(void)614 static int op_halt(void)		/* HALT */
615 {
616 #ifdef BUS_8080
617 	cpu_bus = CPU_WO | CPU_HLTA | CPU_MEMR;
618 #endif
619 
620 #ifndef FRONTPANEL
621 	/* without a frontpanel DI + HALT stops the machine */
622 	if (IFF == 0)	{
623 		cpu_error = OPHALT;
624 		cpu_state = STOPPED;
625 	} else {
626 	/* else wait for INT, NMI or user interrupt */
627 		while ((int_int == 0) && (int_nmi == 0) &&
628 		       (cpu_state == CONTIN_RUN)) {
629 			SLEEP_MS(1);
630 			R += 9999;
631 		}
632 	}
633 #ifdef BUS_8080
634 	if (int_int)
635 		cpu_bus = CPU_INTA | CPU_WO | CPU_HLTA | CPU_M1;
636 #endif
637 
638 	busy_loop_cnt[0] = 0;
639 
640 #else
641 
642 	fp_led_address = 0xffff;
643 	fp_led_data = 0xff;
644 
645 	/* INT disabled, wait for NMI, frontpanel reset or user interrupt */
646 	if (IFF == 0)	{
647 		while ((int_nmi == 0) && !(cpu_state & RESET)) {
648 			fp_clock++;
649 			fp_sampleData();
650 			SLEEP_MS(1);
651 			R += 9999;
652 			if (cpu_error != NONE)
653 				break;
654 		}
655 	} else {
656 	/* else wait for INT, NMI, frontpanel reset or user interrupt */
657 		while ((int_int == 0) && (int_nmi == 0) &&
658 		       !(cpu_state & RESET)) {
659 			fp_clock++;
660 			fp_sampleData();
661 			SLEEP_MS(1);
662 			R += 9999;
663 			if (cpu_error != NONE)
664 				break;
665 		}
666 		if (int_int) {
667 			cpu_bus = CPU_INTA | CPU_WO | CPU_HLTA | CPU_M1;
668 			fp_clock++;
669 			fp_sampleLightGroup(0, 0);
670 		}
671 	}
672 #endif
673 
674 	return(4);
675 }
676 
op_scf(void)677 static int op_scf(void)			/* SCF */
678 {
679 	F |= C_FLAG;
680 	F &= ~(N_FLAG | H_FLAG);
681 	return(4);
682 }
683 
op_ccf(void)684 static int op_ccf(void)			/* CCF */
685 {
686 	if (F & C_FLAG) {
687 		F |= H_FLAG;
688 		F &= ~C_FLAG;
689 	} else {
690 		F &= ~H_FLAG;
691 		F |= C_FLAG;
692 	}
693 	F &= ~N_FLAG;
694 	return(4);
695 }
696 
op_cpl(void)697 static int op_cpl(void)			/* CPL */
698 {
699 	A = ~A;
700 	F |= H_FLAG | N_FLAG;
701 	return(4);
702 }
703 
704 /*
705  * This is my original implementation of the DAA instruction.
706  * It implements the instruction as described in Z80 data sheets
707  * and books, but it won't pass the ex.com instruction exerciser.
708  * Below is a contributed implementation active, that also passes
709  * the tests done by ex.com.
710  */
711 #if 0
712 static int op_daa(void)			/* DAA */
713 {
714 	if (F & N_FLAG) {		/* subtractions */
715 		if (((A & 0x0f) > 9) || (F & H_FLAG)) {
716 			(((A & 0x0f) - 6) < 0) ? (F |= H_FLAG) : (F &= ~H_FLAG);
717 			A -= 6;
718 		}
719 		if (((A & 0xf0) > 0x90) || (F & C_FLAG)) {
720 			if (((A & 0xf0) - 0x60) < 0)
721 				F |= C_FLAG;
722 			A -= 0x60;
723 		}
724 	} else {			/* additions */
725 		if (((A & 0x0f) > 9) || (F & H_FLAG)) {
726 			(((A & 0x0f) + 6) > 0x0f) ? (F |= H_FLAG)
727 						  : (F &= ~H_FLAG);
728 			A += 6;
729 		}
730 		if (((A & 0xf0) > 0x90) || (F & C_FLAG)) {
731 			if (((A & 0xf0) + 0x60) > 0xf0)
732 				F |= C_FLAG;
733 			A += 0x60;
734 		}
735 	}
736 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
737 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
738 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
739 	return(4);
740 }
741 #endif
742 
743 /*
744  * This implementation was contributed by Mark Garlanger,
745  * see http://heathkit.garlanger.com/
746  * It passes the instruction exerciser test from ex.com
747  * and is correct.
748  */
op_daa(void)749 static int op_daa(void)			/* DAA */
750 {
751 	int tmp_a = A;
752 	int low_nibble = A & 0x0f;
753 	int carry = (F & C_FLAG);
754 
755 	if (F & N_FLAG) {		/* subtraction */
756 		int adjustment = (carry || (tmp_a > 0x99)) ? 0x160 : 0x00;
757 
758 		if ((F & H_FLAG) || (low_nibble > 9)) {
759 			if (low_nibble > 5) {
760 				F &= ~H_FLAG;
761 			}
762 			tmp_a = (tmp_a - 6) & 0xff;
763 		}
764 		tmp_a -= adjustment;
765         } else {			/* addition */
766 		if ((low_nibble > 9) || (F & H_FLAG)) {
767 			(low_nibble > 9) ? (F |= H_FLAG) : (F &= ~H_FLAG);
768 			tmp_a += 6;
769 		}
770 		if (((tmp_a & 0x1f0) > 0x90) || carry) {
771 			tmp_a += 0x60;
772 		}
773 	}
774 
775 	(carry || (tmp_a & 0x100)) ?  (F |= C_FLAG) : (F &= ~C_FLAG);
776 	A = (tmp_a & 0xff);
777 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
778 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
779 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
780 	return(4);
781 }
782 
op_ei(void)783 static int op_ei(void)			/* EI */
784 {
785 	IFF = 3;
786 	int_protection = 1;		/* protect next instruction */
787 	return(4);
788 }
789 
op_di(void)790 static int op_di(void)			/* DI */
791 {
792 	IFF = 0;
793 	return(4);
794 }
795 
op_in(void)796 static int op_in(void)			/* IN A,(n) */
797 {
798 	BYTE io_in(BYTE, BYTE);
799 	BYTE addr;
800 
801 	addr = memrdr(PC++);
802 	A = io_in(addr, A);
803 	return(11);
804 }
805 
op_out(void)806 static int op_out(void)			/* OUT (n),A */
807 {
808 	BYTE io_out(BYTE, BYTE, BYTE);
809 	BYTE addr;
810 
811 	addr = memrdr(PC++);
812 	io_out(addr, A, A);
813 	return(11);
814 }
815 
op_ldan(void)816 static int op_ldan(void)		/* LD A,n */
817 {
818 	A = memrdr(PC++);
819 	return(7);
820 }
821 
op_ldbn(void)822 static int op_ldbn(void)		/* LD B,n */
823 {
824 	B = memrdr(PC++);
825 	return(7);
826 }
827 
op_ldcn(void)828 static int op_ldcn(void)		/* LD C,n */
829 {
830 	C = memrdr(PC++);
831 	return(7);
832 }
833 
op_lddn(void)834 static int op_lddn(void)		/* LD D,n */
835 {
836 	D = memrdr(PC++);
837 	return(7);
838 }
839 
op_lden(void)840 static int op_lden(void)		/* LD E,n */
841 {
842 	E = memrdr(PC++);
843 	return(7);
844 }
845 
op_ldhn(void)846 static int op_ldhn(void)		/* LD H,n */
847 {
848 	H = memrdr(PC++);
849 	return(7);
850 }
851 
op_ldln(void)852 static int op_ldln(void)		/* LD L,n */
853 {
854 	L = memrdr(PC++);
855 	return(7);
856 }
857 
op_ldabc(void)858 static int op_ldabc(void)		/* LD A,(BC) */
859 {
860 	A = memrdr((B << 8) + C);
861 	return(7);
862 }
863 
op_ldade(void)864 static int op_ldade(void)		/* LD A,(DE) */
865 {
866 	A = memrdr((D << 8) + E);
867 	return(7);
868 }
869 
op_ldann(void)870 static int op_ldann(void)		/* LD A,(nn) */
871 {
872 	register WORD i;
873 
874 	i = memrdr(PC++);
875 	i += memrdr(PC++) << 8;
876 	A = memrdr(i);
877 	return(13);
878 }
879 
op_ldbca(void)880 static int op_ldbca(void)		/* LD (BC),A */
881 {
882 	memwrt((B << 8) + C, A);
883 	return(7);
884 }
885 
op_lddea(void)886 static int op_lddea(void)		/* LD (DE),A */
887 {
888 	memwrt((D << 8) + E, A);
889 	return(7);
890 }
891 
op_ldnna(void)892 static int op_ldnna(void)		/* LD (nn),A */
893 {
894 	register WORD i;
895 
896 	i = memrdr(PC++);
897 	i += memrdr(PC++) << 8;
898 	memwrt(i, A);
899 	return(13);
900 }
901 
op_ldhla(void)902 static int op_ldhla(void)		/* LD (HL),A */
903 {
904 	memwrt((H << 8) + L, A);
905 	return(7);
906 }
907 
op_ldhlb(void)908 static int op_ldhlb(void)		/* LD (HL),B */
909 {
910 	memwrt((H << 8) + L, B);
911 	return(7);
912 }
913 
op_ldhlc(void)914 static int op_ldhlc(void)		/* LD (HL),C */
915 {
916 	memwrt((H << 8) + L, C);
917 	return(7);
918 }
919 
op_ldhld(void)920 static int op_ldhld(void)		/* LD (HL),D */
921 {
922 	memwrt((H << 8) + L, D);
923 	return(7);
924 }
925 
op_ldhle(void)926 static int op_ldhle(void)		/* LD (HL),E */
927 {
928 	memwrt((H << 8) + L, E);
929 	return(7);
930 }
931 
op_ldhlh(void)932 static int op_ldhlh(void)		/* LD (HL),H */
933 {
934 	memwrt((H << 8) + L, H);
935 	return(7);
936 }
937 
op_ldhll(void)938 static int op_ldhll(void)		/* LD (HL),L */
939 {
940 	memwrt((H << 8) + L, L);
941 	return(7);
942 }
943 
op_ldhl1(void)944 static int op_ldhl1(void)		/* LD (HL),n */
945 {
946 	memwrt((H << 8) + L, memrdr(PC++));
947 	return(10);
948 }
949 
op_ldaa(void)950 static int op_ldaa(void)		/* LD A,A */
951 {
952 	return(4);
953 }
954 
op_ldab(void)955 static int op_ldab(void)		/* LD A,B */
956 {
957 	A = B;
958 	return(4);
959 }
960 
op_ldac(void)961 static int op_ldac(void)		/* LD A,C */
962 {
963 	A = C;
964 	return(4);
965 }
966 
op_ldad(void)967 static int op_ldad(void)		/* LD A,D */
968 {
969 	A = D;
970 	return(4);
971 }
972 
op_ldae(void)973 static int op_ldae(void)		/* LD A,E */
974 {
975 	A = E;
976 	return(4);
977 }
978 
op_ldah(void)979 static int op_ldah(void)		/* LD A,H */
980 {
981 	A = H;
982 	return(4);
983 }
984 
op_ldal(void)985 static int op_ldal(void)		/* LD A,L */
986 {
987 	A = L;
988 	return(4);
989 }
990 
op_ldahl(void)991 static int op_ldahl(void)		/* LD A,(HL) */
992 {
993 	A = memrdr((H << 8) + L);
994 	return(7);
995 }
996 
op_ldba(void)997 static int op_ldba(void)		/* LD B,A */
998 {
999 	B = A;
1000 	return(4);
1001 }
1002 
op_ldbb(void)1003 static int op_ldbb(void)		/* LD B,B */
1004 {
1005 	return(4);
1006 }
1007 
op_ldbc(void)1008 static int op_ldbc(void)		/* LD B,C */
1009 {
1010 	B = C;
1011 	return(4);
1012 }
1013 
op_ldbd(void)1014 static int op_ldbd(void)		/* LD B,D */
1015 {
1016 	B = D;
1017 	return(4);
1018 }
1019 
op_ldbe(void)1020 static int op_ldbe(void)		/* LD B,E */
1021 {
1022 	B = E;
1023 	return(4);
1024 }
1025 
op_ldbh(void)1026 static int op_ldbh(void)		/* LD B,H */
1027 {
1028 	B = H;
1029 	return(4);
1030 }
1031 
op_ldbl(void)1032 static int op_ldbl(void)		/* LD B,L */
1033 {
1034 	B = L;
1035 	return(4);
1036 }
1037 
op_ldbhl(void)1038 static int op_ldbhl(void)		/* LD B,(HL) */
1039 {
1040 	B = memrdr((H << 8) + L);
1041 	return(7);
1042 }
1043 
op_ldca(void)1044 static int op_ldca(void)		/* LD C,A */
1045 {
1046 	C = A;
1047 	return(4);
1048 }
1049 
op_ldcb(void)1050 static int op_ldcb(void)		/* LD C,B */
1051 {
1052 	C = B;
1053 	return(4);
1054 }
1055 
op_ldcc(void)1056 static int op_ldcc(void)		/* LD C,C */
1057 {
1058 	return(4);
1059 }
1060 
op_ldcd(void)1061 static int op_ldcd(void)		/* LD C,D */
1062 {
1063 	C = D;
1064 	return(4);
1065 }
1066 
op_ldce(void)1067 static int op_ldce(void)		/* LD C,E */
1068 {
1069 	C = E;
1070 	return(4);
1071 }
1072 
op_ldch(void)1073 static int op_ldch(void)		/* LD C,H */
1074 {
1075 	C = H;
1076 	return(4);
1077 }
1078 
op_ldcl(void)1079 static int op_ldcl(void)		/* LD C,L */
1080 {
1081 	C = L;
1082 	return(4);
1083 }
1084 
op_ldchl(void)1085 static int op_ldchl(void)		/* LD C,(HL) */
1086 {
1087 	C = memrdr((H << 8) + L);
1088 	return(7);
1089 }
1090 
op_ldda(void)1091 static int op_ldda(void)		/* LD D,A */
1092 {
1093 	D = A;
1094 	return(4);
1095 }
1096 
op_lddb(void)1097 static int op_lddb(void)		/* LD D,B */
1098 {
1099 	D = B;
1100 	return(4);
1101 }
1102 
op_lddc(void)1103 static int op_lddc(void)		/* LD D,C */
1104 {
1105 	D = C;
1106 	return(4);
1107 }
1108 
op_lddd(void)1109 static int op_lddd(void)		/* LD D,D */
1110 {
1111 	return(4);
1112 }
1113 
op_ldde(void)1114 static int op_ldde(void)		/* LD D,E */
1115 {
1116 	D = E;
1117 	return(4);
1118 }
1119 
op_lddh(void)1120 static int op_lddh(void)		/* LD D,H */
1121 {
1122 	D = H;
1123 	return(4);
1124 }
1125 
op_lddl(void)1126 static int op_lddl(void)		/* LD D,L */
1127 {
1128 	D = L;
1129 	return(4);
1130 }
1131 
op_lddhl(void)1132 static int op_lddhl(void)		/* LD D,(HL) */
1133 {
1134 	D = memrdr((H << 8) + L);
1135 	return(7);
1136 }
1137 
op_ldea(void)1138 static int op_ldea(void)		/* LD E,A */
1139 {
1140 	E = A;
1141 	return(4);
1142 }
1143 
op_ldeb(void)1144 static int op_ldeb(void)		/* LD E,B */
1145 {
1146 	E = B;
1147 	return(4);
1148 }
1149 
op_ldec(void)1150 static int op_ldec(void)		/* LD E,C */
1151 {
1152 	E = C;
1153 	return(4);
1154 }
1155 
op_lded(void)1156 static int op_lded(void)		/* LD E,D */
1157 {
1158 	E = D;
1159 	return(4);
1160 }
1161 
op_ldee(void)1162 static int op_ldee(void)		/* LD E,E */
1163 {
1164 	return(4);
1165 }
1166 
op_ldeh(void)1167 static int op_ldeh(void)		/* LD E,H */
1168 {
1169 	E = H;
1170 	return(4);
1171 }
1172 
op_ldel(void)1173 static int op_ldel(void)		/* LD E,L */
1174 {
1175 	E = L;
1176 	return(4);
1177 }
1178 
op_ldehl(void)1179 static int op_ldehl(void)		/* LD E,(HL) */
1180 {
1181 	E = memrdr((H << 8) + L);
1182 	return(7);
1183 }
1184 
op_ldha(void)1185 static int op_ldha(void)		/* LD H,A */
1186 {
1187 	H = A;
1188 	return(4);
1189 }
1190 
op_ldhb(void)1191 static int op_ldhb(void)		/* LD H,B */
1192 {
1193 	H = B;
1194 	return(4);
1195 }
1196 
op_ldhc(void)1197 static int op_ldhc(void)		/* LD H,C */
1198 {
1199 	H = C;
1200 	return(4);
1201 }
1202 
op_ldhd(void)1203 static int op_ldhd(void)		/* LD H,D */
1204 {
1205 	H = D;
1206 	return(4);
1207 }
1208 
op_ldhe(void)1209 static int op_ldhe(void)		/* LD H,E */
1210 {
1211 	H = E;
1212 	return(4);
1213 }
1214 
op_ldhh(void)1215 static int op_ldhh(void)		/* LD H,H */
1216 {
1217 	return(4);
1218 }
1219 
op_ldhl(void)1220 static int op_ldhl(void)		/* LD H,L */
1221 {
1222 	H = L;
1223 	return(4);
1224 }
1225 
op_ldhhl(void)1226 static int op_ldhhl(void)		/* LD H,(HL) */
1227 {
1228 	H = memrdr((H << 8) + L);
1229 	return(7);
1230 }
1231 
op_ldla(void)1232 static int op_ldla(void)		/* LD L,A */
1233 {
1234 	L = A;
1235 	return(4);
1236 }
1237 
op_ldlb(void)1238 static int op_ldlb(void)		/* LD L,B */
1239 {
1240 	L = B;
1241 	return(4);
1242 }
1243 
op_ldlc(void)1244 static int op_ldlc(void)		/* LD L,C */
1245 {
1246 	L = C;
1247 	return(4);
1248 }
1249 
op_ldld(void)1250 static int op_ldld(void)		/* LD L,D */
1251 {
1252 	L = D;
1253 	return(4);
1254 }
1255 
op_ldle(void)1256 static int op_ldle(void)		/* LD L,E */
1257 {
1258 	L = E;
1259 	return(4);
1260 }
1261 
op_ldlh(void)1262 static int op_ldlh(void)		/* LD L,H */
1263 {
1264 	L = H;
1265 	return(4);
1266 }
1267 
op_ldll(void)1268 static int op_ldll(void)		/* LD L,L */
1269 {
1270 	return(4);
1271 }
1272 
op_ldlhl(void)1273 static int op_ldlhl(void)		/* LD L,(HL) */
1274 {
1275 	L = memrdr((H << 8) + L);
1276 	return(7);
1277 }
1278 
op_ldbcnn(void)1279 static int op_ldbcnn(void)		/* LD BC,nn */
1280 {
1281 	C = memrdr(PC++);
1282 	B = memrdr(PC++);
1283 	return(10);
1284 }
1285 
op_lddenn(void)1286 static int op_lddenn(void)		/* LD DE,nn */
1287 {
1288 	E = memrdr(PC++);
1289 	D = memrdr(PC++);
1290 	return(10);
1291 }
1292 
op_ldhlnn(void)1293 static int op_ldhlnn(void)		/* LD HL,nn */
1294 {
1295 	L = memrdr(PC++);
1296 	H = memrdr(PC++);
1297 	return(10);
1298 }
1299 
op_ldspnn(void)1300 static int op_ldspnn(void)		/* LD SP,nn */
1301 {
1302 	SP = memrdr(PC++);
1303 	SP += memrdr(PC++) << 8;
1304 	return(10);
1305 }
1306 
op_ldsphl(void)1307 static int op_ldsphl(void)		/* LD SP,HL */
1308 {
1309 	SP = (H << 8) + L;
1310 	return(6);
1311 }
1312 
op_ldhlin(void)1313 static int op_ldhlin(void)		/* LD HL,(nn) */
1314 {
1315 	register WORD i;
1316 
1317 	i = memrdr(PC++);
1318 	i += memrdr(PC++) << 8;
1319 	L = memrdr(i++);
1320 	H = memrdr(i);
1321 	return(16);
1322 }
1323 
op_ldinhl(void)1324 static int op_ldinhl(void)		/* LD (nn),HL */
1325 {
1326 	register WORD i;
1327 
1328 	i = memrdr(PC++);
1329 	i += memrdr(PC++) << 8;
1330 	memwrt(i++, L);
1331 	memwrt(i, H);
1332 	return(16);
1333 }
1334 
op_incbc(void)1335 static int op_incbc(void)		/* INC BC */
1336 {
1337 	C++;
1338 	if (!C)
1339 		B++;
1340 	return(6);
1341 }
1342 
op_incde(void)1343 static int op_incde(void)		/* INC DE */
1344 {
1345 	E++;
1346 	if (!E)
1347 		D++;
1348 	return(6);
1349 }
1350 
op_inchl(void)1351 static int op_inchl(void)		/* INC HL */
1352 {
1353 	L++;
1354 	if (!L)
1355 		H++;
1356 	return(6);
1357 }
1358 
op_incsp(void)1359 static int op_incsp(void)		/* INC SP */
1360 {
1361 	SP++;
1362 	return(6);
1363 }
1364 
op_decbc(void)1365 static int op_decbc(void)		/* DEC BC */
1366 {
1367 	C--;
1368 	if (C == 0xff)
1369 		B--;
1370 	return(6);
1371 }
1372 
op_decde(void)1373 static int op_decde(void)		/* DEC DE */
1374 {
1375 	E--;
1376 	if (E == 0xff)
1377 		D--;
1378 	return(6);
1379 }
1380 
op_dechl(void)1381 static int op_dechl(void)		/* DEC HL */
1382 {
1383 	L--;
1384 	if (L == 0xff)
1385 		H--;
1386 	return(6);
1387 }
1388 
op_decsp(void)1389 static int op_decsp(void)		/* DEC SP */
1390 {
1391 	SP--;
1392 	return(6);
1393 }
1394 
op_adhlbc(void)1395 static int op_adhlbc(void)		/* ADD HL,BC */
1396 {
1397 	register int carry;
1398 
1399 	carry = (L + C > 255) ? 1 : 0;
1400 	L += C;
1401 	((H & 0xf) + (B & 0xf) + carry > 0xf) ? (F |= H_FLAG) : (F &= ~H_FLAG);
1402 	(H + B + carry > 255) ? (F |= C_FLAG) : (F &= ~C_FLAG);
1403 	H += B + carry;
1404 	F &= ~N_FLAG;
1405 	return(11);
1406 }
1407 
op_adhlde(void)1408 static int op_adhlde(void)		/* ADD HL,DE */
1409 {
1410 	register int carry;
1411 
1412 	carry = (L + E > 255) ? 1 : 0;
1413 	L += E;
1414 	((H & 0xf) + (D & 0xf) + carry > 0xf) ? (F |= H_FLAG) : (F &= ~H_FLAG);
1415 	(H + D + carry > 255) ? (F |= C_FLAG) : (F &= ~C_FLAG);
1416 	H += D + carry;
1417 	F &= ~N_FLAG;
1418 	return(11);
1419 }
1420 
op_adhlhl(void)1421 static int op_adhlhl(void)		/* ADD HL,HL */
1422 {
1423 	register int carry;
1424 
1425 	carry = (L << 1 > 255) ? 1 : 0;
1426 	L <<= 1;
1427 	((H & 0xf) + (H & 0xf) + carry > 0xf) ? (F |= H_FLAG) : (F &= ~H_FLAG);
1428 	(H + H + carry > 255) ? (F |= C_FLAG) : (F &= ~C_FLAG);
1429 	H += H + carry;
1430 	F &= ~N_FLAG;
1431 	return(11);
1432 }
1433 
op_adhlsp(void)1434 static int op_adhlsp(void)		/* ADD HL,SP */
1435 {
1436 	register int carry;
1437 
1438 	BYTE spl = SP & 0xff;
1439 	BYTE sph = SP >> 8;
1440 
1441 	carry = (L + spl > 255) ? 1 : 0;
1442 	L += spl;
1443 	((H & 0xf) + (sph & 0xf) + carry > 0xf) ? (F |= H_FLAG)
1444 						: (F &= ~H_FLAG);
1445 	(H + sph + carry > 255) ? (F |= C_FLAG) : (F &= ~C_FLAG);
1446 	H += sph + carry;
1447 	F &= ~N_FLAG;
1448 	return(11);
1449 }
1450 
op_anda(void)1451 static int op_anda(void)		/* AND A */
1452 {
1453 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1454 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1455 	F |= H_FLAG;
1456 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1457 	F &= ~(N_FLAG | C_FLAG);
1458 	return(4);
1459 }
1460 
op_andb(void)1461 static int op_andb(void)		/* AND B */
1462 {
1463 	A &= B;
1464 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1465 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1466 	F |= H_FLAG;
1467 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1468 	F &= ~(N_FLAG | C_FLAG);
1469 	return(4);
1470 }
1471 
op_andc(void)1472 static int op_andc(void)		/* AND C */
1473 {
1474 	A &= C;
1475 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1476 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1477 	F |= H_FLAG;
1478 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1479 	F &= ~(N_FLAG | C_FLAG);
1480 	return(4);
1481 }
1482 
op_andd(void)1483 static int op_andd(void)		/* AND D */
1484 {
1485 	A &= D;
1486 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1487 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1488 	F |= H_FLAG;
1489 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1490 	F &= ~(N_FLAG | C_FLAG);
1491 	return(4);
1492 }
1493 
op_ande(void)1494 static int op_ande(void)		/* AND E */
1495 {
1496 	A &= E;
1497 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1498 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1499 	F |= H_FLAG;
1500 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1501 	F &= ~(N_FLAG | C_FLAG);
1502 	return(4);
1503 }
1504 
op_andh(void)1505 static int op_andh(void)		/* AND H */
1506 {
1507 	A &= H;
1508 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1509 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1510 	F |= H_FLAG;
1511 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1512 	F &= ~(N_FLAG | C_FLAG);
1513 	return(4);
1514 }
1515 
op_andl(void)1516 static int op_andl(void)		/* AND L */
1517 {
1518 	A &= L;
1519 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1520 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1521 	F |= H_FLAG;
1522 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1523 	F &= ~(N_FLAG | C_FLAG);
1524 	return(4);
1525 }
1526 
op_andhl(void)1527 static int op_andhl(void)		/* AND (HL) */
1528 {
1529 	A &= memrdr((H << 8) + L);
1530 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1531 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1532 	F |= H_FLAG;
1533 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1534 	F &= ~(N_FLAG | C_FLAG);
1535 	return(7);
1536 }
1537 
op_andn(void)1538 static int op_andn(void)		/* AND n */
1539 {
1540 	A &= memrdr(PC++);
1541 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1542 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1543 	F |= H_FLAG;
1544 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1545 	F &= ~(N_FLAG | C_FLAG);
1546 	return(7);
1547 }
1548 
op_ora(void)1549 static int op_ora(void)			/* OR A */
1550 {
1551 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1552 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1553 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1554 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1555 	return(4);
1556 }
1557 
op_orb(void)1558 static int op_orb(void)			/* OR B */
1559 {
1560 	A |= B;
1561 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1562 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1563 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1564 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1565 	return(4);
1566 }
1567 
op_orc(void)1568 static int op_orc(void)			/* OR C */
1569 {
1570 	A |= C;
1571 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1572 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1573 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1574 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1575 	return(4);
1576 }
1577 
op_ord(void)1578 static int op_ord(void)			/* OR D */
1579 {
1580 	A |= D;
1581 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1582 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1583 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1584 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1585 	return(4);
1586 }
1587 
op_ore(void)1588 static int op_ore(void)			/* OR E */
1589 {
1590 	A |= E;
1591 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1592 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1593 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1594 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1595 	return(4);
1596 }
1597 
op_orh(void)1598 static int op_orh(void)			/* OR H */
1599 {
1600 	A |= H;
1601 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1602 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1603 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1604 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1605 	return(4);
1606 }
1607 
op_orl(void)1608 static int op_orl(void)			/* OR L */
1609 {
1610 	A |= L;
1611 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1612 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1613 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1614 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1615 	return(4);
1616 }
1617 
op_orhl(void)1618 static int op_orhl(void)		/* OR (HL) */
1619 {
1620 	A |= memrdr((H << 8) + L);
1621 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1622 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1623 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1624 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1625 	return(7);
1626 }
1627 
op_orn(void)1628 static int op_orn(void)			/* OR n */
1629 {
1630 	A |= memrdr(PC++);
1631 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1632 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1633 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1634 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1635 	return(7);
1636 }
1637 
op_xora(void)1638 static int op_xora(void)		/* XOR A */
1639 {
1640 	A = 0;
1641 	F &= ~(S_FLAG | H_FLAG | N_FLAG | C_FLAG);
1642 	F |= Z_FLAG | P_FLAG;
1643 	return(4);
1644 }
1645 
op_xorb(void)1646 static int op_xorb(void)		/* XOR B */
1647 {
1648 	A ^= B;
1649 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1650 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1651 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1652 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1653 	return(4);
1654 }
1655 
op_xorc(void)1656 static int op_xorc(void)		/* XOR C */
1657 {
1658 	A ^= C;
1659 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1660 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1661 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1662 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1663 	return(4);
1664 }
1665 
op_xord(void)1666 static int op_xord(void)		/* XOR D */
1667 {
1668 	A ^= D;
1669 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1670 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1671 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1672 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1673 	return(4);
1674 }
1675 
op_xore(void)1676 static int op_xore(void)		/* XOR E */
1677 {
1678 	A ^= E;
1679 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1680 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1681 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1682 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1683 	return(4);
1684 }
1685 
op_xorh(void)1686 static int op_xorh(void)		/* XOR H */
1687 {
1688 	A ^= H;
1689 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1690 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1691 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1692 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1693 	return(4);
1694 }
1695 
op_xorl(void)1696 static int op_xorl(void)		/* XOR L */
1697 {
1698 	A ^= L;
1699 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1700 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1701 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1702 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1703 	return(4);
1704 }
1705 
op_xorhl(void)1706 static int op_xorhl(void)		/* XOR (HL) */
1707 {
1708 	A ^= memrdr((H << 8) + L);
1709 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1710 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1711 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1712 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1713 	return(7);
1714 }
1715 
op_xorn(void)1716 static int op_xorn(void)		/* XOR n */
1717 {
1718 	A ^= memrdr(PC++);
1719 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1720 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1721 	(parity[A]) ? (F &= ~P_FLAG) : (F |= P_FLAG);
1722 	F &= ~(H_FLAG | N_FLAG | C_FLAG);
1723 	return(7);
1724 }
1725 
op_adda(void)1726 static int op_adda(void)		/* ADD A,A */
1727 {
1728 	register int i;
1729 
1730 	((A & 0xf) + (A & 0xf) > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1731 	((A << 1) > 255) ? (F |= C_FLAG) : (F &= ~C_FLAG);
1732 	A = i = ((signed char) A) << 1;
1733 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1734 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1735 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1736 	F &= ~N_FLAG;
1737 	return(4);
1738 }
1739 
op_addb(void)1740 static int op_addb(void)		/* ADD A,B */
1741 {
1742 	register int i;
1743 
1744 	((A & 0xf) + (B & 0xf) > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1745 	(A + B > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1746 	A = i = (signed char) A + (signed char) B;
1747 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1748 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1749 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1750 	F &= ~N_FLAG;
1751 	return(4);
1752 }
1753 
op_addc(void)1754 static int op_addc(void)		/* ADD A,C */
1755 {
1756 	register int i;
1757 
1758 	((A & 0xf) + (C & 0xf) > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1759 	(A + C > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1760 	A = i = (signed char) A + (signed char) C;
1761 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1762 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1763 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1764 	F &= ~N_FLAG;
1765 	return(4);
1766 }
1767 
op_addd(void)1768 static int op_addd(void)		/* ADD A,D */
1769 {
1770 	register int i;
1771 
1772 	((A & 0xf) + (D & 0xf) > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1773 	(A + D > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1774 	A = i = (signed char) A + (signed char) D;
1775 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1776 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1777 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1778 	F &= ~N_FLAG;
1779 	return(4);
1780 }
1781 
op_adde(void)1782 static int op_adde(void)		/* ADD A,E */
1783 {
1784 	register int i;
1785 
1786 	((A & 0xf) + (E & 0xf) > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1787 	(A + E > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1788 	A = i = (signed char) A + (signed char) E;
1789 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1790 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1791 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1792 	F &= ~N_FLAG;
1793 	return(4);
1794 }
1795 
op_addh(void)1796 static int op_addh(void)		/* ADD A,H */
1797 {
1798 	register int i;
1799 
1800 	((A & 0xf) + (H & 0xf) > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1801 	(A + H > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1802 	A = i = (signed char) A + (signed char) H;
1803 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1804 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1805 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1806 	F &= ~N_FLAG;
1807 	return(4);
1808 }
1809 
op_addl(void)1810 static int op_addl(void)		/* ADD A,L */
1811 {
1812 	register int i;
1813 
1814 	((A & 0xf) + (L & 0xf) > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1815 	(A + L > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1816 	A = i = (signed char) A + (signed char) L;
1817 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1818 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1819 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1820 	F &= ~N_FLAG;
1821 	return(4);
1822 }
1823 
op_addhl(void)1824 static int op_addhl(void)		/* ADD A,(HL) */
1825 {
1826 	register int i;
1827 	register BYTE P;
1828 
1829 	P = memrdr((H << 8) + L);
1830 	((A & 0xf) + (P & 0xf) > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1831 	(A + P > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1832 	A = i = (signed char) A + (signed char) P;
1833 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1834 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1835 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1836 	F &= ~N_FLAG;
1837 	return(7);
1838 }
1839 
op_addn(void)1840 static int op_addn(void)		/* ADD A,n */
1841 {
1842 	register int i;
1843 	register BYTE P;
1844 
1845 	P = memrdr(PC++);
1846 	((A & 0xf) + (P & 0xf) > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1847 	(A + P > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1848 	A = i = (signed char) A + (signed char) P;
1849 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1850 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1851 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1852 	F &= ~N_FLAG;
1853 	return(7);
1854 }
1855 
op_adca(void)1856 static int op_adca(void)		/* ADC A,A */
1857 {
1858 	register int i, carry;
1859 
1860 	carry = (F & C_FLAG) ? 1 : 0;
1861 	((A & 0xf) + (A & 0xf) + carry > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1862 	((A << 1) + carry > 255) ? (F |= C_FLAG) : (F &= ~C_FLAG);
1863 	A = i = (((signed char) A) << 1) + carry;
1864 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1865 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1866 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1867 	F &= ~N_FLAG;
1868 	return(4);
1869 }
1870 
op_adcb(void)1871 static int op_adcb(void)		/* ADC A,B */
1872 {
1873 	register int i, carry;
1874 
1875 	carry = (F & C_FLAG) ? 1 : 0;
1876 	((A & 0xf) + (B & 0xf) + carry > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1877 	(A + B + carry > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1878 	A = i = (signed char) A + (signed char) B + carry;
1879 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1880 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1881 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1882 	F &= ~N_FLAG;
1883 	return(4);
1884 }
1885 
op_adcc(void)1886 static int op_adcc(void)		/* ADC A,C */
1887 {
1888 	register int i, carry;
1889 
1890 	carry = (F & C_FLAG) ? 1 : 0;
1891 	((A & 0xf) + (C & 0xf) + carry > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1892 	(A + C + carry > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1893 	A = i = (signed char) A + (signed char) C + carry;
1894 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1895 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1896 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1897 	F &= ~N_FLAG;
1898 	return(4);
1899 }
1900 
op_adcd(void)1901 static int op_adcd(void)		/* ADC A,D */
1902 {
1903 	register int i, carry;
1904 
1905 	carry = (F & C_FLAG) ? 1 : 0;
1906 	((A & 0xf) + (D & 0xf) + carry > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1907 	(A + D + carry > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1908 	A = i = (signed char) A + (signed char) D + carry;
1909 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1910 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1911 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1912 	F &= ~N_FLAG;
1913 	return(4);
1914 }
1915 
op_adce(void)1916 static int op_adce(void)		/* ADC A,E */
1917 {
1918 	register int i, carry;
1919 
1920 	carry = (F & C_FLAG) ? 1 : 0;
1921 	((A & 0xf) + (E & 0xf) + carry > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1922 	(A + E + carry > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1923 	A = i = (signed char) A + (signed char) E + carry;
1924 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1925 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1926 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1927 	F &= ~N_FLAG;
1928 	return(4);
1929 }
1930 
op_adch(void)1931 static int op_adch(void)		/* ADC A,H */
1932 {
1933 	register int i, carry;
1934 
1935 	carry = (F & C_FLAG) ? 1 : 0;
1936 	((A & 0xf) + (H & 0xf) + carry > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1937 	(A + H + carry > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1938 	A = i = (signed char) A + (signed char) H + carry;
1939 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1940 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1941 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1942 	F &= ~N_FLAG;
1943 	return(4);
1944 }
1945 
op_adcl(void)1946 static int op_adcl(void)		/* ADC A,L */
1947 {
1948 	register int i, carry;
1949 
1950 	carry = (F & C_FLAG) ? 1 : 0;
1951 	((A & 0xf) + (L & 0xf) + carry > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1952 	(A + L + carry > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1953 	A = i = (signed char) A + (signed char) L + carry;
1954 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1955 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1956 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1957 	F &= ~N_FLAG;
1958 	return(4);
1959 }
1960 
op_adchl(void)1961 static int op_adchl(void)		/* ADC A,(HL) */
1962 {
1963 	register int i, carry;
1964 	register BYTE P;
1965 
1966 	P = memrdr((H << 8) + L);
1967 	carry = (F & C_FLAG) ? 1 : 0;
1968 	((A & 0xf) + (P & 0xf) + carry > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1969 	(A + P + carry > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1970 	A = i = (signed char) A + (signed char) P + carry;
1971 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1972 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1973 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1974 	F &= ~N_FLAG;
1975 	return(7);
1976 }
1977 
op_adcn(void)1978 static int op_adcn(void)		/* ADC A,n */
1979 {
1980 	register int i, carry;
1981 	register BYTE P;
1982 
1983 	carry = (F & C_FLAG) ? 1 : 0;
1984 	P = memrdr(PC++);
1985 	((A & 0xf) + (P & 0xf) + carry > 0xf) ?	(F |= H_FLAG) : (F &= ~H_FLAG);
1986 	(A + P + carry > 255) ?	(F |= C_FLAG) : (F &= ~C_FLAG);
1987 	A = i = (signed char) A + (signed char) P + carry;
1988 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
1989 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
1990 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
1991 	F &= ~N_FLAG;
1992 	return(7);
1993 }
1994 
op_suba(void)1995 static int op_suba(void)		/* SUB A,A */
1996 {
1997 	A = 0;
1998 	F &= ~(S_FLAG | H_FLAG | P_FLAG | C_FLAG);
1999 	F |= Z_FLAG | N_FLAG;
2000 	return(4);
2001 }
2002 
op_subb(void)2003 static int op_subb(void)		/* SUB A,B */
2004 {
2005 	register int i;
2006 
2007 	((B & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2008 	(B > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2009 	A = i = (signed char) A - (signed char) B;
2010 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2011 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2012 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2013 	F |= N_FLAG;
2014 	return(4);
2015 }
2016 
op_subc(void)2017 static int op_subc(void)		/* SUB A,C */
2018 {
2019 	register int i;
2020 
2021 	((C & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2022 	(C > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2023 	A = i = (signed char) A - (signed char) C;
2024 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2025 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2026 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2027 	F |= N_FLAG;
2028 	return(4);
2029 }
2030 
op_subd(void)2031 static int op_subd(void)		/* SUB A,D */
2032 {
2033 	register int i;
2034 
2035 	((D & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2036 	(D > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2037 	A = i = (signed char) A - (signed char) D;
2038 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2039 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2040 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2041 	F |= N_FLAG;
2042 	return(4);
2043 }
2044 
op_sube(void)2045 static int op_sube(void)		/* SUB A,E */
2046 {
2047 	register int i;
2048 
2049 	((E & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2050 	(E > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2051 	A = i = (signed char) A - (signed char) E;
2052 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2053 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2054 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2055 	F |= N_FLAG;
2056 	return(4);
2057 }
2058 
op_subh(void)2059 static int op_subh(void)		/* SUB A,H */
2060 {
2061 	register int i;
2062 
2063 	((H & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2064 	(H > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2065 	A = i = (signed char) A - (signed char) H;
2066 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2067 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2068 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2069 	F |= N_FLAG;
2070 	return(4);
2071 }
2072 
op_subl(void)2073 static int op_subl(void)		/* SUB A,L */
2074 {
2075 	register int i;
2076 
2077 	((L & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2078 	(L > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2079 	A = i = (signed char) A - (signed char) L;
2080 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2081 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2082 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2083 	F |= N_FLAG;
2084 	return(4);
2085 }
2086 
op_subhl(void)2087 static int op_subhl(void)		/* SUB A,(HL) */
2088 {
2089 	register int i;
2090 	register BYTE P;
2091 
2092 	P = memrdr((H << 8) + L);
2093 	((P & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2094 	(P > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2095 	A = i = (signed char) A - (signed char) P;
2096 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2097 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2098 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2099 	F |= N_FLAG;
2100 	return(7);
2101 }
2102 
op_subn(void)2103 static int op_subn(void)		/* SUB A,n */
2104 {
2105 	register int i;
2106 	register BYTE P;
2107 
2108 	P = memrdr(PC++);
2109 	((P & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2110 	(P > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2111 	A = i = (signed char) A - (signed char) P;
2112 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2113 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2114 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2115 	F |= N_FLAG;
2116 	return(7);
2117 }
2118 
op_sbca(void)2119 static int op_sbca(void)		/* SBC A,A */
2120 {
2121 	if (F & C_FLAG) {
2122 		F |= S_FLAG | H_FLAG | N_FLAG | C_FLAG;
2123 		F &= ~(Z_FLAG | P_FLAG);
2124 		A = 255;
2125 	} else {
2126 		F |= Z_FLAG | N_FLAG;
2127 		F &= ~(S_FLAG | H_FLAG | P_FLAG | C_FLAG);
2128 		A = 0;
2129 	}
2130 	return(4);
2131 }
2132 
op_sbcb(void)2133 static int op_sbcb(void)		/* SBC A,B */
2134 {
2135 	register int i, carry;
2136 
2137 	carry = (F & C_FLAG) ? 1 : 0;
2138 	((B & 0xf) + carry > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2139 	(B + carry > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2140 	A = i = (signed char) A - (signed char) B - carry;
2141 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2142 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2143 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2144 	F |= N_FLAG;
2145 	return(4);
2146 }
2147 
op_sbcc(void)2148 static int op_sbcc(void)		/* SBC A,C */
2149 {
2150 	register int i, carry;
2151 
2152 	carry = (F & C_FLAG) ? 1 : 0;
2153 	((C & 0xf) + carry > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2154 	(C + carry > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2155 	A = i = (signed char) A - (signed char) C - carry;
2156 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2157 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2158 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2159 	F |= N_FLAG;
2160 	return(4);
2161 }
2162 
op_sbcd(void)2163 static int op_sbcd(void)		/* SBC A,D */
2164 {
2165 	register int i, carry;
2166 
2167 	carry = (F & C_FLAG) ? 1 : 0;
2168 	((D & 0xf) + carry > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2169 	(D + carry > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2170 	A = i = (signed char) A - (signed char) D - carry;
2171 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2172 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2173 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2174 	F |= N_FLAG;
2175 	return(4);
2176 }
2177 
op_sbce(void)2178 static int op_sbce(void)		/* SBC A,E */
2179 {
2180 	register int i, carry;
2181 
2182 	carry = (F & C_FLAG) ? 1 : 0;
2183 	((E & 0xf) + carry > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2184 	(E + carry > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2185 	A = i = (signed char) A - (signed char) E - carry;
2186 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2187 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2188 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2189 	F |= N_FLAG;
2190 	return(4);
2191 }
2192 
op_sbch(void)2193 static int op_sbch(void)		/* SBC A,H */
2194 {
2195 	register int i, carry;
2196 
2197 	carry = (F & C_FLAG) ? 1 : 0;
2198 	((H & 0xf) + carry > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2199 	(H + carry > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2200 	A = i = (signed char) A - (signed char) H - carry;
2201 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2202 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2203 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2204 	F |= N_FLAG;
2205 	return(4);
2206 }
2207 
op_sbcl(void)2208 static int op_sbcl(void)		/* SBC A,L */
2209 {
2210 	register int i, carry;
2211 
2212 	carry = (F & C_FLAG) ? 1 : 0;
2213 	((L & 0xf) + carry > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2214 	(L + carry > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2215 	A = i = (signed char) A - (signed char) L - carry;
2216 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2217 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2218 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2219 	F |= N_FLAG;
2220 	return(4);
2221 }
2222 
op_sbchl(void)2223 static int op_sbchl(void)		/* SBC A,(HL) */
2224 {
2225 	register int i, carry;
2226 	register BYTE P;
2227 
2228 	P = memrdr((H << 8) + L);
2229 	carry = (F & C_FLAG) ? 1 : 0;
2230 	((P & 0xf) + carry > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2231 	(P + carry > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2232 	A = i = (signed char) A - (signed char) P - carry;
2233 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2234 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2235 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2236 	F |= N_FLAG;
2237 	return(7);
2238 }
2239 
op_sbcn(void)2240 static int op_sbcn(void)		/* SBC A,n */
2241 {
2242 	register int i, carry;
2243 	register BYTE P;
2244 
2245 	P = memrdr(PC++);
2246 	carry = (F & C_FLAG) ? 1 : 0;
2247 	((P & 0xf) + carry > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2248 	(P + carry > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2249 	A = i = (signed char) A - (signed char) P - carry;
2250 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2251 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2252 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2253 	F |= N_FLAG;
2254 	return(7);
2255 }
2256 
op_cpa(void)2257 static int op_cpa(void)			/* CP A */
2258 {
2259 	F &= ~(S_FLAG | H_FLAG | P_FLAG | C_FLAG);
2260 	F |= Z_FLAG | N_FLAG;
2261 	return(4);
2262 }
2263 
op_cpb(void)2264 static int op_cpb(void)			/* CP B */
2265 {
2266 	register int i;
2267 
2268 	((B & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2269 	(B > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2270 	i = (signed char) A - (signed char) B;
2271 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2272 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2273 	(i) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2274 	F |= N_FLAG;
2275 	return(4);
2276 }
2277 
op_cpc(void)2278 static int op_cpc(void)			/* CP C */
2279 {
2280 	register int i;
2281 
2282 	((C & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2283 	(C > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2284 	i = (signed char) A - (signed char) C;
2285 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2286 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2287 	(i) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2288 	F |= N_FLAG;
2289 	return(4);
2290 }
2291 
op_cpd(void)2292 static int op_cpd(void)			/* CP D */
2293 {
2294 	register int i;
2295 
2296 	((D & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2297 	(D > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2298 	i = (signed char) A - (signed char) D;
2299 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2300 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2301 	(i) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2302 	F |= N_FLAG;
2303 	return(4);
2304 }
2305 
op_cpe(void)2306 static int op_cpe(void)			/* CP E */
2307 {
2308 	register int i;
2309 
2310 	((E & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2311 	(E > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2312 	i = (signed char) A - (signed char) E;
2313 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2314 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2315 	(i) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2316 	F |= N_FLAG;
2317 	return(4);
2318 }
2319 
op_cph(void)2320 static int op_cph(void)			/* CP H */
2321 {
2322 	register int i;
2323 
2324 	((H & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2325 	(H > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2326 	i = (signed char) A - (signed char) H;
2327 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2328 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2329 	(i) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2330 	F |= N_FLAG;
2331 	return(4);
2332 }
2333 
op_cplr(void)2334 static int op_cplr(void)		/* CP L */
2335 {
2336 	register int i;
2337 
2338 	((L & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2339 	(L > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2340 	i = (signed char) A - (signed char) L;
2341 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2342 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2343 	(i) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2344 	F |= N_FLAG;
2345 	return(4);
2346 }
2347 
op_cphl(void)2348 static int op_cphl(void)		/* CP (HL) */
2349 {
2350 	register int i;
2351 	register BYTE P;
2352 
2353 	P = memrdr((H << 8) + L);
2354 	((P & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2355 	(P > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2356 	i = (signed char) A - (signed char) P;
2357 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2358 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2359 	(i) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2360 	F |= N_FLAG;
2361 	return(7);
2362 }
2363 
op_cpn(void)2364 static int op_cpn(void)			/* CP n */
2365 {
2366 	register int i;
2367 	register BYTE P;
2368 
2369 	P = memrdr(PC++);
2370 	((P & 0xf) > (A & 0xf)) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2371 	(P > A) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2372 	i = (signed char) A - (signed char) P;
2373 	(i < -128 || i > 127) ?	(F |= P_FLAG) : (F &= ~P_FLAG);
2374 	(i & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2375 	(i) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2376 	F |= N_FLAG;
2377 	return(7);
2378 }
2379 
op_inca(void)2380 static int op_inca(void)		/* INC A */
2381 {
2382 	A++;
2383 	((A & 0xf) == 0) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2384 	(A == 128) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2385 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2386 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2387 	F &= ~N_FLAG;
2388 	return(4);
2389 }
2390 
op_incb(void)2391 static int op_incb(void)		/* INC B */
2392 {
2393 	B++;
2394 	((B & 0xf) == 0) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2395 	(B == 128) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2396 	(B & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2397 	(B) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2398 	F &= ~N_FLAG;
2399 	return(4);
2400 }
2401 
op_incc(void)2402 static int op_incc(void)		/* INC C */
2403 {
2404 	C++;
2405 	((C & 0xf) == 0) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2406 	(C == 128) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2407 	(C & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2408 	(C) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2409 	F &= ~N_FLAG;
2410 	return(4);
2411 }
2412 
op_incd(void)2413 static int op_incd(void)		/* INC D */
2414 {
2415 	D++;
2416 	((D & 0xf) == 0) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2417 	(D == 128) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2418 	(D & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2419 	(D) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2420 	F &= ~N_FLAG;
2421 	return(4);
2422 }
2423 
op_ince(void)2424 static int op_ince(void)		/* INC E */
2425 {
2426 	E++;
2427 	((E & 0xf) == 0) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2428 	(E == 128) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2429 	(E & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2430 	(E) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2431 	F &= ~N_FLAG;
2432 	return(4);
2433 }
2434 
op_inch(void)2435 static int op_inch(void)		/* INC H */
2436 {
2437 	H++;
2438 	((H & 0xf) == 0) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2439 	(H == 128) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2440 	(H & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2441 	(H) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2442 	F &= ~N_FLAG;
2443 	return(4);
2444 }
2445 
op_incl(void)2446 static int op_incl(void)		/* INC L */
2447 {
2448 	L++;
2449 	((L & 0xf) == 0) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2450 	(L == 128) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2451 	(L & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2452 	(L) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2453 	F &= ~N_FLAG;
2454 	return(4);
2455 }
2456 
op_incihl(void)2457 static int op_incihl(void)		/* INC (HL) */
2458 {
2459 	register BYTE P;
2460 	WORD addr;
2461 
2462 	addr = (H << 8) + L;
2463 	P = memrdr(addr);
2464 	P++;
2465 	memwrt(addr, P);
2466 	((P & 0xf) == 0) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2467 	(P == 128) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2468 	(P & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2469 	(P) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2470 	F &= ~N_FLAG;
2471 	return(11);
2472 }
2473 
op_deca(void)2474 static int op_deca(void)		/* DEC A */
2475 {
2476 	A--;
2477 	((A & 0xf) == 0xf) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2478 	(A == 127) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2479 	(A & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2480 	(A) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2481 	F |= N_FLAG;
2482 	return(4);
2483 }
2484 
op_decb(void)2485 static int op_decb(void)		/* DEC B */
2486 {
2487 	B--;
2488 	((B & 0xf) == 0xf) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2489 	(B == 127) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2490 	(B & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2491 	(B) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2492 	F |= N_FLAG;
2493 	return(4);
2494 }
2495 
op_decc(void)2496 static int op_decc(void)		/* DEC C */
2497 {
2498 	C--;
2499 	((C & 0xf) == 0xf) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2500 	(C == 127) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2501 	(C & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2502 	(C) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2503 	F |= N_FLAG;
2504 	return(4);
2505 }
2506 
op_decd(void)2507 static int op_decd(void)		/* DEC D */
2508 {
2509 	D--;
2510 	((D & 0xf) == 0xf) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2511 	(D == 127) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2512 	(D & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2513 	(D) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2514 	F |= N_FLAG;
2515 	return(4);
2516 }
2517 
op_dece(void)2518 static int op_dece(void)		/* DEC E */
2519 {
2520 	E--;
2521 	((E & 0xf) == 0xf) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2522 	(E == 127) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2523 	(E & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2524 	(E) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2525 	F |= N_FLAG;
2526 	return(4);
2527 }
2528 
op_dech(void)2529 static int op_dech(void)		/* DEC H */
2530 {
2531 	H--;
2532 	((H & 0xf) == 0xf) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2533 	(H == 127) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2534 	(H & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2535 	(H) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2536 	F |= N_FLAG;
2537 	return(4);
2538 }
2539 
op_decl(void)2540 static int op_decl(void)		/* DEC L */
2541 {
2542 	L--;
2543 	((L & 0xf) == 0xf) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2544 	(L == 127) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2545 	(L & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2546 	(L) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2547 	F |= N_FLAG;
2548 	return(4);
2549 }
2550 
op_decihl(void)2551 static int op_decihl(void)		/* DEC (HL) */
2552 {
2553 	register BYTE P;
2554 	WORD addr;
2555 
2556 	addr = (H << 8) + L;
2557 	P = memrdr(addr);
2558 	P--;
2559 	memwrt(addr, P);
2560 	((P & 0xf) == 0xf) ? (F |= H_FLAG) : (F &= ~H_FLAG);
2561 	(P == 127) ? (F |= P_FLAG) : (F &= ~P_FLAG);
2562 	(P & 128) ? (F |= S_FLAG) : (F &= ~S_FLAG);
2563 	(P) ? (F &= ~Z_FLAG) : (F |= Z_FLAG);
2564 	F |= N_FLAG;
2565 	return(11);
2566 }
2567 
op_rlca(void)2568 static int op_rlca(void)		/* RLCA */
2569 {
2570 	register int i;
2571 
2572 	i = (A & 128) ?	1 : 0;
2573 	(i) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2574 	F &= ~(H_FLAG | N_FLAG);
2575 	A <<= 1;
2576 	A |= i;
2577 	return(4);
2578 }
2579 
op_rrca(void)2580 static int op_rrca(void)		/* RRCA */
2581 {
2582 	register int i;
2583 
2584 	i = A & 1;
2585 	(i) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2586 	F &= ~(H_FLAG | N_FLAG);
2587 	A >>= 1;
2588 	if (i) A |= 128;
2589 	return(4);
2590 }
2591 
op_rla(void)2592 static int op_rla(void)			/* RLA */
2593 {
2594 	register int old_c_flag;
2595 
2596 	old_c_flag = F & C_FLAG;
2597 	(A & 128) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2598 	F &= ~(H_FLAG | N_FLAG);
2599 	A <<= 1;
2600 	if (old_c_flag) A |= 1;
2601 	return(4);
2602 }
2603 
op_rra(void)2604 static int op_rra(void)			/* RRA */
2605 {
2606 	register int i, old_c_flag;
2607 
2608 	old_c_flag = F & C_FLAG;
2609 	i = A & 1;
2610 	(i) ? (F |= C_FLAG) : (F &= ~C_FLAG);
2611 	F &= ~(H_FLAG | N_FLAG);
2612 	A >>= 1;
2613 	if (old_c_flag) A |= 128;
2614 	return(4);
2615 }
2616 
op_exdehl(void)2617 static int op_exdehl(void)		/* EX DE,HL */
2618 {
2619 	register BYTE i;
2620 
2621 	i = D;
2622 	D = H;
2623 	H = i;
2624 	i = E;
2625 	E = L;
2626 	L = i;
2627 	return(4);
2628 }
2629 
op_exafaf(void)2630 static int op_exafaf(void)		/* EX AF,AF' */
2631 {
2632 	register BYTE i;
2633 
2634 	i = A;
2635 	A = A_;
2636 	A_ = i;
2637 	i = F;
2638 	F = F_;
2639 	F_ = i;
2640 	return(4);
2641 }
2642 
op_exx(void)2643 static int op_exx(void)			/* EXX */
2644 {
2645 	register BYTE i;
2646 
2647 	i = B;
2648 	B = B_;
2649 	B_ = i;
2650 	i = C;
2651 	C = C_;
2652 	C_ = i;
2653 	i = D;
2654 	D = D_;
2655 	D_ = i;
2656 	i = E;
2657 	E = E_;
2658 	E_ = i;
2659 	i = H;
2660 	H = H_;
2661 	H_ = i;
2662 	i = L;
2663 	L = L_;
2664 	L_ = i;
2665 	return(4);
2666 }
2667 
op_exsphl(void)2668 static int op_exsphl(void)		/* EX (SP),HL */
2669 {
2670 	register BYTE i;
2671 
2672 	i = memrdr(SP);
2673 	memwrt(SP, L);
2674 	L = i;
2675 	i = memrdr(SP + 1);
2676 	memwrt(SP + 1, H);
2677 	H = i;
2678 	return(19);
2679 }
2680 
op_pushaf(void)2681 static int op_pushaf(void)		/* PUSH AF */
2682 {
2683 	memwrt(--SP, A);
2684 	memwrt(--SP, F);
2685 	return(11);
2686 }
2687 
op_pushbc(void)2688 static int op_pushbc(void)		/* PUSH BC */
2689 {
2690 	memwrt(--SP, B);
2691 	memwrt(--SP, C);
2692 	return(11);
2693 }
2694 
op_pushde(void)2695 static int op_pushde(void)		/* PUSH DE */
2696 {
2697 	memwrt(--SP, D);
2698 	memwrt(--SP, E);
2699 	return(11);
2700 }
2701 
op_pushhl(void)2702 static int op_pushhl(void)		/* PUSH HL */
2703 {
2704 	memwrt(--SP, H);
2705 	memwrt(--SP, L);
2706 	return(11);
2707 }
2708 
op_popaf(void)2709 static int op_popaf(void)		/* POP AF */
2710 {
2711 	F = memrdr(SP++);
2712 	A = memrdr(SP++);
2713 	return(10);
2714 }
2715 
op_popbc(void)2716 static int op_popbc(void)		/* POP BC */
2717 {
2718 	C = memrdr(SP++);
2719 	B = memrdr(SP++);
2720 	return(10);
2721 }
2722 
op_popde(void)2723 static int op_popde(void)		/* POP DE */
2724 {
2725 	E = memrdr(SP++);
2726 	D = memrdr(SP++);
2727 	return(10);
2728 }
2729 
op_pophl(void)2730 static int op_pophl(void)		/* POP HL */
2731 {
2732 	L = memrdr(SP++);
2733 	H = memrdr(SP++);
2734 	return(10);
2735 }
2736 
op_jp(void)2737 static int op_jp(void)			/* JP */
2738 {
2739 	register WORD i;
2740 
2741 	i = memrdr(PC++);
2742 	i += memrdr(PC) << 8;
2743 	PC = i;
2744 	return(10);
2745 }
2746 
op_jphl(void)2747 static int op_jphl(void)		/* JP (HL) */
2748 {
2749 	PC = (H << 8) + L;
2750 	return(4);
2751 }
2752 
op_jr(void)2753 static int op_jr(void)			/* JR */
2754 {
2755 	PC += (signed char) memrdr(PC) + 1;
2756 	return(12);
2757 }
2758 
op_djnz(void)2759 static int op_djnz(void)		/* DJNZ */
2760 {
2761 	if (--B) {
2762 		PC += (signed char) memrdr(PC) + 1;
2763 		return(13);
2764 	} else {
2765 		PC++;
2766 		return(8);
2767 	}
2768 }
2769 
op_call(void)2770 static int op_call(void)		/* CALL */
2771 {
2772 	register WORD i;
2773 
2774 	i = memrdr(PC++);
2775 	i += memrdr(PC++) << 8;
2776 	memwrt(--SP, PC >> 8);
2777 	memwrt(--SP, PC);
2778 	PC = i;
2779 	return(17);
2780 }
2781 
op_ret(void)2782 static int op_ret(void)			/* RET */
2783 {
2784 	register WORD i;
2785 
2786 	i = memrdr(SP++);
2787 	i += memrdr(SP++) << 8;
2788 	PC = i;
2789 	return(10);
2790 }
2791 
op_jpz(void)2792 static int op_jpz(void)			/* JP Z,nn */
2793 {
2794 	register WORD i;
2795 
2796 	if (F & Z_FLAG) {
2797 		i = memrdr(PC++);
2798 		i += memrdr(PC++) << 8;
2799 		PC = i;
2800 	} else
2801 		PC += 2;
2802 	return(10);
2803 }
2804 
op_jpnz(void)2805 static int op_jpnz(void)		/* JP NZ,nn */
2806 {
2807 	register WORD i;
2808 
2809 	if (!(F & Z_FLAG)) {
2810 		i = memrdr(PC++);
2811 		i += memrdr(PC++) << 8;
2812 		PC = i;
2813 	} else
2814 		PC += 2;
2815 	return(10);
2816 }
2817 
op_jpc(void)2818 static int op_jpc(void)			/* JP C,nn */
2819 {
2820 	register WORD i;
2821 
2822 	if (F & C_FLAG) {
2823 		i = memrdr(PC++);
2824 		i += memrdr(PC++) << 8;
2825 		PC = i;
2826 	} else
2827 		PC += 2;
2828 	return(10);
2829 }
2830 
op_jpnc(void)2831 static int op_jpnc(void)		/* JP NC,nn */
2832 {
2833 	register WORD i;
2834 
2835 	if (!(F & C_FLAG)) {
2836 		i = memrdr(PC++);
2837 		i += memrdr(PC++) << 8;
2838 		PC = i;
2839 	} else
2840 		PC += 2;
2841 	return(10);
2842 }
2843 
op_jppe(void)2844 static int op_jppe(void)		/* JP PE,nn */
2845 {
2846 	register WORD i;
2847 
2848 	if (F & P_FLAG) {
2849 		i = memrdr(PC++);
2850 		i += memrdr(PC++) << 8;
2851 		PC = i;
2852 	} else
2853 		PC += 2;
2854 	return(10);
2855 }
2856 
op_jppo(void)2857 static int op_jppo(void)		/* JP PO,nn */
2858 {
2859 	register WORD i;
2860 
2861 	if (!(F & P_FLAG)) {
2862 		i = memrdr(PC++);
2863 		i += memrdr(PC++) << 8;
2864 		PC = i;
2865 	} else
2866 		PC += 2;
2867 	return(10);
2868 }
2869 
op_jpm(void)2870 static int op_jpm(void)			/* JP M,nn */
2871 {
2872 	register WORD i;
2873 
2874 	if (F & S_FLAG) {
2875 		i = memrdr(PC++);
2876 		i += memrdr(PC++) << 8;
2877 		PC = i;
2878 	} else
2879 		PC += 2;
2880 	return(10);
2881 }
2882 
op_jpp(void)2883 static int op_jpp(void)			/* JP P,nn */
2884 {
2885 	register WORD i;
2886 
2887 	if (!(F & S_FLAG)) {
2888 		i = memrdr(PC++);
2889 		i += memrdr(PC++) << 8;
2890 		PC = i;
2891 	} else
2892 		PC += 2;
2893 	return(10);
2894 }
2895 
op_calz(void)2896 static int op_calz(void)		/* CALL Z,nn */
2897 {
2898 	register WORD i;
2899 
2900 	if (F & Z_FLAG) {
2901 		i = memrdr(PC++);
2902 		i += memrdr(PC++) << 8;
2903 		memwrt(--SP, PC >> 8);
2904 		memwrt(--SP, PC);
2905 		PC = i;
2906 		return(17);
2907 	} else {
2908 		PC += 2;
2909 		return(10);
2910 	}
2911 }
2912 
op_calnz(void)2913 static int op_calnz(void)		/* CALL NZ,nn */
2914 {
2915 	register WORD i;
2916 
2917 	if (!(F & Z_FLAG)) {
2918 		i = memrdr(PC++);
2919 		i += memrdr(PC++) << 8;
2920 		memwrt(--SP, PC >> 8);
2921 		memwrt(--SP, PC);
2922 		PC = i;
2923 		return(17);
2924 	} else {
2925 		PC += 2;
2926 		return(10);
2927 	}
2928 }
2929 
op_calc(void)2930 static int op_calc(void)		/* CALL C,nn */
2931 {
2932 	register WORD i;
2933 
2934 	if (F & C_FLAG) {
2935 		i = memrdr(PC++);
2936 		i += memrdr(PC++) << 8;
2937 #ifdef BUS_8080
2938 		cpu_bus = CPU_WO | CPU_MEMR;
2939 #endif
2940 		memwrt(--SP, PC >> 8);
2941 		memwrt(--SP, PC);
2942 		PC = i;
2943 		return(17);
2944 	} else {
2945 		PC += 2;
2946 		return(10);
2947 	}
2948 }
2949 
op_calnc(void)2950 static int op_calnc(void)		/* CALL NC,nn */
2951 {
2952 	register WORD i;
2953 
2954 	if (!(F & C_FLAG)) {
2955 		i = memrdr(PC++);
2956 		i += memrdr(PC++) << 8;
2957 		memwrt(--SP, PC >> 8);
2958 		memwrt(--SP, PC);
2959 		PC = i;
2960 		return(17);
2961 	} else {
2962 		PC += 2;
2963 		return(10);
2964 	}
2965 }
2966 
op_calpe(void)2967 static int op_calpe(void)		/* CALL PE,nn */
2968 {
2969 	register WORD i;
2970 
2971 	if (F & P_FLAG) {
2972 		i = memrdr(PC++);
2973 		i += memrdr(PC++) << 8;
2974 		memwrt(--SP, PC >> 8);
2975 		memwrt(--SP, PC);
2976 		PC = i;
2977 		return(17);
2978 	} else {
2979 		PC += 2;
2980 		return(10);
2981 	}
2982 }
2983 
op_calpo(void)2984 static int op_calpo(void)		/* CALL PO,nn */
2985 {
2986 	register WORD i;
2987 
2988 	if (!(F & P_FLAG)) {
2989 		i = memrdr(PC++);
2990 		i += memrdr(PC++) << 8;
2991 		memwrt(--SP, PC >> 8);
2992 		memwrt(--SP, PC);
2993 		PC = i;
2994 		return(17);
2995 	} else {
2996 		PC += 2;
2997 		return(10);
2998 	}
2999 }
3000 
op_calm(void)3001 static int op_calm(void)		/* CALL M,nn */
3002 {
3003 	register WORD i;
3004 
3005 	if (F & S_FLAG) {
3006 		i = memrdr(PC++);
3007 		i += memrdr(PC++) << 8;
3008 		memwrt(--SP, PC >> 8);
3009 		memwrt(--SP, PC);
3010 		PC = i;
3011 		return(17);
3012 	} else {
3013 		PC += 2;
3014 		return(10);
3015 	}
3016 }
3017 
op_calp(void)3018 static int op_calp(void)		/* CALL P,nn */
3019 {
3020 	register WORD i;
3021 
3022 	if (!(F & S_FLAG)) {
3023 		i = memrdr(PC++);
3024 		i += memrdr(PC++) << 8;
3025 		memwrt(--SP, PC >> 8);
3026 		memwrt(--SP, PC);
3027 		PC = i;
3028 		return(17);
3029 	} else {
3030 		PC += 2;
3031 		return(10);
3032 	}
3033 }
3034 
op_retz(void)3035 static int op_retz(void)		/* RET Z */
3036 {
3037 	register WORD i;
3038 
3039 	if (F & Z_FLAG) {
3040 		i = memrdr(SP++);
3041 		i += memrdr(SP++) << 8;
3042 		PC = i;
3043 		return(11);
3044 	} else {
3045 		return(5);
3046 	}
3047 }
3048 
op_retnz(void)3049 static int op_retnz(void)		/* RET NZ */
3050 {
3051 	register WORD i;
3052 
3053 	if (!(F & Z_FLAG)) {
3054 		i = memrdr(SP++);
3055 		i += memrdr(SP++) << 8;
3056 		PC = i;
3057 		return(11);
3058 	} else {
3059 		return(5);
3060 	}
3061 }
3062 
op_retc(void)3063 static int op_retc(void)		/* RET C */
3064 {
3065 	register WORD i;
3066 
3067 	if (F & C_FLAG) {
3068 		i = memrdr(SP++);
3069 		i += memrdr(SP++) << 8;
3070 		PC = i;
3071 		return(11);
3072 	} else {
3073 		return(5);
3074 	}
3075 }
3076 
op_retnc(void)3077 static int op_retnc(void)		/* RET NC */
3078 {
3079 	register WORD i;
3080 
3081 	if (!(F & C_FLAG)) {
3082 		i = memrdr(SP++);
3083 		i += memrdr(SP++) << 8;
3084 		PC = i;
3085 		return(11);
3086 	} else {
3087 		return(5);
3088 	}
3089 }
3090 
op_retpe(void)3091 static int op_retpe(void)		/* RET PE */
3092 {
3093 	register WORD i;
3094 
3095 	if (F & P_FLAG) {
3096 		i = memrdr(SP++);
3097 		i += memrdr(SP++) << 8;
3098 		PC = i;
3099 		return(11);
3100 	} else {
3101 		return(5);
3102 	}
3103 }
3104 
op_retpo(void)3105 static int op_retpo(void)		/* RET PO */
3106 {
3107 	register WORD i;
3108 
3109 	if (!(F & P_FLAG)) {
3110 		i = memrdr(SP++);
3111 		i += memrdr(SP++) << 8;
3112 		PC = i;
3113 		return(11);
3114 	} else {
3115 		return(5);
3116 	}
3117 }
3118 
op_retm(void)3119 static int op_retm(void)		/* RET M */
3120 {
3121 	register WORD i;
3122 
3123 	if (F & S_FLAG) {
3124 		i = memrdr(SP++);
3125 		i += memrdr(SP++) << 8;
3126 		PC = i;
3127 		return(11);
3128 	} else {
3129 		return(5);
3130 	}
3131 }
3132 
op_retp(void)3133 static int op_retp(void)		/* RET P */
3134 {
3135 	register WORD i;
3136 
3137 	if (!(F & S_FLAG)) {
3138 		i = memrdr(SP++);
3139 		i += memrdr(SP++) << 8;
3140 		PC = i;
3141 		return(11);
3142 	} else {
3143 		return(5);
3144 	}
3145 }
3146 
op_jrz(void)3147 static int op_jrz(void)			/* JR Z,n */
3148 {
3149 	if (F & Z_FLAG) {
3150 		PC += (signed char) memrdr(PC) + 1;
3151 		return(12);
3152 	} else {
3153 		PC++;
3154 		return(7);
3155 	}
3156 }
3157 
op_jrnz(void)3158 static int op_jrnz(void)		/* JR NZ,n */
3159 {
3160 	if (!(F & Z_FLAG)) {
3161 		PC += (signed char) memrdr(PC) + 1;
3162 		return(12);
3163 	} else {
3164 		PC++;
3165 		return(7);
3166 	}
3167 }
3168 
op_jrc(void)3169 static int op_jrc(void)			/* JR C,n */
3170 {
3171 	if (F & C_FLAG) {
3172 		PC += (signed char) memrdr(PC) + 1;
3173 		return(12);
3174 	} else {
3175 		PC++;
3176 		return(7);
3177 	}
3178 }
3179 
op_jrnc(void)3180 static int op_jrnc(void)		/* JR NC,n */
3181 {
3182 	if (!(F & C_FLAG)) {
3183 		PC += (signed char) memrdr(PC) + 1;
3184 		return(12);
3185 	} else {
3186 		PC++;
3187 		return(7);
3188 	}
3189 }
3190 
op_rst00(void)3191 static int op_rst00(void)		/* RST 00 */
3192 {
3193 	memwrt(--SP, PC >> 8);
3194 	memwrt(--SP, PC);
3195 	PC = 0;
3196 	return(11);
3197 }
3198 
op_rst08(void)3199 static int op_rst08(void)		/* RST 08 */
3200 {
3201 	memwrt(--SP, PC >> 8);
3202 	memwrt(--SP, PC);
3203 	PC = 0x08;
3204 	return(11);
3205 }
3206 
op_rst10(void)3207 static int op_rst10(void)		/* RST 10 */
3208 {
3209 	memwrt(--SP, PC >> 8);
3210 	memwrt(--SP, PC);
3211 	PC = 0x10;
3212 	return(11);
3213 }
3214 
op_rst18(void)3215 static int op_rst18(void)		/* RST 18 */
3216 {
3217 	memwrt(--SP, PC >> 8);
3218 	memwrt(--SP, PC);
3219 	PC = 0x18;
3220 	return(11);
3221 }
3222 
op_rst20(void)3223 static int op_rst20(void)		/* RST 20 */
3224 {
3225 	memwrt(--SP, PC >> 8);
3226 	memwrt(--SP, PC);
3227 	PC = 0x20;
3228 	return(11);
3229 }
3230 
op_rst28(void)3231 static int op_rst28(void)		/* RST 28 */
3232 {
3233 	memwrt(--SP, PC >> 8);
3234 	memwrt(--SP, PC);
3235 	PC = 0x28;
3236 	return(11);
3237 }
3238 
op_rst30(void)3239 static int op_rst30(void)		/* RST 30 */
3240 {
3241 	memwrt(--SP, PC >> 8);
3242 	memwrt(--SP, PC);
3243 	PC = 0x30;
3244 	return(11);
3245 }
3246 
op_rst38(void)3247 static int op_rst38(void)		/* RST 38 */
3248 {
3249 	memwrt(--SP, PC >> 8);
3250 	memwrt(--SP, PC);
3251 	PC = 0x38;
3252 	return(11);
3253 }
3254