Implement 386 instruction table, improve 8086/186/286 instruction table
[multi_emu.git] / cpu_z80.h
1 #ifndef _CPU_Z80_H
2 #define _CPU_Z80_H
3
4 #include <endian.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdlib.h>
8
9 // gcc specific
10 #ifndef ALWAYS_INLINE
11 #define ALWAYS_INLINE __attribute__((always_inline))
12 #endif
13
14 #define CPU_Z80_NMI_ADDR 0x66
15 #define CPU_Z80_RESET_ADDR 0
16 #define CPU_Z80_IRQ_ADDR 0x38
17
18 // bits within REG_F
19 #define CPU_Z80_REG_F_BIT_C 0
20 #define CPU_Z80_REG_F_BIT_N 1
21 #define CPU_Z80_REG_F_BIT_PV 2
22 #define CPU_Z80_REG_F_BIT_H 4
23 #define CPU_Z80_REG_F_BIT_Z 6
24 #define CPU_Z80_REG_F_BIT_S 7
25
26 // special memory locations (negative address)
27 // note: CPU_Z80_EA_SINK is a byte-wide sink for implementing the "in f,(c)"
28 // and DDCB/FDCB undocumented instructions, to store into and never read back
29 #define CPU_Z80_EA_PC (-0x1e)
30 #define CPU_Z80_EA_AF (-0x1c)
31 #define CPU_Z80_EA_BC (-0x1a)
32 #define CPU_Z80_EA_DE (-0x18)
33 #define CPU_Z80_EA_HL (-0x16)
34 #define CPU_Z80_EA_SP (-0x14)
35 #define CPU_Z80_EA_IX (-0x12)
36 #define CPU_Z80_EA_IY (-0x10)
37 #define CPU_Z80_EA_AF_PRIME (-0xe)
38 #define CPU_Z80_EA_BC_PRIME (-0xc)
39 #define CPU_Z80_EA_DE_PRIME (-0xa)
40 #define CPU_Z80_EA_HL_PRIME (-8)
41 #define CPU_Z80_EA_IFLAGS (-6)
42 #define CPU_Z80_EA_IV (-4)
43
44 // pc (-0x1e)
45 #define CPU_Z80_EA_F (-0x1c)
46 #define CPU_Z80_EA_A (-0x1b)
47 #define CPU_Z80_EA_C (-0x1a)
48 #define CPU_Z80_EA_B (-0x19)
49 #define CPU_Z80_EA_E (-0x18)
50 #define CPU_Z80_EA_D (-0x17)
51 #define CPU_Z80_EA_L (-0x16)
52 #define CPU_Z80_EA_H (-0x15)
53 // sp (-0x14)
54 #define CPU_Z80_EA_IXL (-0x12)
55 #define CPU_Z80_EA_IXH (-0x11)
56 #define CPU_Z80_EA_IYL (-0x10)
57 #define CPU_Z80_EA_IYH (-0xf)
58 // af_prime (-0xe)
59 // bc_prime (-0xc)
60 // de_prime (-0xa)
61 // hl_prime (-8)
62 // iflags (-6)
63 #define CPU_Z80_EA_V (-4)
64 #define CPU_Z80_EA_I (-3)
65 #define CPU_Z80_EA_R (-2)
66 #define CPU_Z80_EA_SINK (-1)
67
68 // registers, in same order as special memory locations, but reversed on
69 // big endian hardware where special memory address will be complemented
70 // (this allows special memory to always look like it is little endian)
71 union cpu_z80_regs {
72 #if __BYTE_ORDER == __BIG_ENDIAN
73   struct {
74     uint8_t _fill_sink;
75     uint8_t _fill_r;
76     uint16_t _fill_iv;
77     uint16_t _fill_iflags : 7;
78     uint16_t reti_flag : 1;
79     uint16_t ei_flag : 1;
80     uint16_t halt_flag : 1;
81     uint16_t nmi_pending : 1;
82     uint16_t irq_pending : 1;
83     uint16_t iff2 : 1;
84     uint16_t iff1 : 1;
85     uint16_t im : 2;
86     uint16_t _fill_hl_prime;
87     uint16_t _fill_de_prime;
88     uint16_t _fill_bc_prime;
89     uint16_t _fill_af_prime;
90     uint16_t _fill_iy;
91     uint16_t _fill_ix;
92     uint16_t _fill_sp;
93     uint16_t _fill_hl;
94     uint16_t _fill_de;
95     uint16_t _fill_bc;
96     uint8_t _fill_a;
97     uint8_t sf : 1;
98     uint8_t zf : 1;
99     uint8_t _fill_5f: 1;
100     uint8_t hf : 1;
101     uint8_t _fill_3f: 1;
102     uint8_t pvf: 1;
103     uint8_t nf : 1;
104     uint8_t cf : 1;
105     uint16_t _fill_pc;
106   } bit;
107   struct {
108     uint8_t sink;
109     uint8_t r;
110     uint8_t i;
111     uint8_t v;
112     uint16_t _fill_iflags;
113     uint16_t _fill_hl_prime;
114     uint16_t _fill_de_prime;
115     uint16_t _fill_bc_prime;
116     uint16_t _fill_af_prime;
117     uint8_t r;
118     uint8_t i;
119     uint8_t iyh;
120     uint8_t iyl;
121     uint8_t ixh;
122     uint8_t ixl;
123     uint16_t _fill_sp;
124     uint8_t h;
125     uint8_t l;
126     uint8_t d;
127     uint8_t e;
128     uint8_t b;
129     uint8_t c;
130     uint8_t a;
131     uint8_t f;
132     uint16_t _fill_pc;
133   } byte;
134   struct {
135     uint8_t _fill_sink;
136     uint8_t _fill_r;
137     uint16_t iv;
138     uint16_t iflags;
139     uint16_t hl_prime;
140     uint16_t de_prime;
141     uint16_t bc_prime;
142     uint16_t af_prime;
143     uint16_t iy;
144     uint16_t ix;
145     uint16_t sp;
146     uint16_t hl;
147     uint16_t de;
148     uint16_t bc;
149     uint16_t af;
150     uint16_t pc;
151   } word;
152   uint8_t mem_be[0x1e];
153 #else
154   struct {
155     uint16_t _fill_pc;
156     uint8_t cf : 1;
157     uint8_t nf : 1;
158     uint8_t pvf: 1;
159     uint8_t _fill_3f: 1;
160     uint8_t hf : 1;
161     uint8_t _fill_5f: 1;
162     uint8_t zf : 1;
163     uint8_t sf : 1;
164     uint8_t _fill_a;
165     uint16_t _fill_bc;
166     uint16_t _fill_de;
167     uint16_t _fill_hl;
168     uint16_t _fill_sp;
169     uint16_t _fill_ix;
170     uint16_t _fill_iy;
171     uint16_t _fill_af_prime;
172     uint16_t _fill_bc_prime;
173     uint16_t _fill_de_prime;
174     uint16_t _fill_hl_prime;
175     uint16_t im : 2;
176     uint16_t iff1 : 1;
177     uint16_t iff2 : 1;
178     uint16_t irq_pending : 1;
179     uint16_t nmi_pending : 1;
180     uint16_t halt_flag : 1;
181     uint16_t ei_flag : 1;
182     uint16_t reti_flag : 1;
183     uint16_t _fill_iflags : 7;
184     uint16_t _fill_iv;
185     uint8_t _fill_r;
186     uint8_t _fill_sink;
187   } bit;
188   struct {
189     uint16_t _fill_pc;
190     uint8_t f;
191     uint8_t a;
192     uint8_t c;
193     uint8_t b;
194     uint8_t e;
195     uint8_t d;
196     uint8_t l;
197     uint8_t h;
198     uint16_t _fill_sp;
199     uint8_t ixl;
200     uint8_t ixh;
201     uint8_t iyl;
202     uint8_t iyh;
203     uint16_t _fill_af_prime;
204     uint16_t _fill_bc_prime;
205     uint16_t _fill_de_prime;
206     uint16_t _fill_hl_prime;
207     uint16_t _fill_iflags;
208     uint8_t v;
209     uint8_t i;
210     uint8_t r;
211     uint8_t sink;
212   } byte;
213   struct {
214     uint16_t pc;
215     uint16_t af;
216     uint16_t bc;
217     uint16_t de;
218     uint16_t hl;
219     uint16_t sp;
220     uint16_t ix;
221     uint16_t iy;
222     uint16_t af_prime;
223     uint16_t bc_prime;
224     uint16_t de_prime;
225     uint16_t hl_prime;
226     uint16_t iflags;
227     uint16_t iv;
228     uint8_t _fill_r;
229     uint8_t _fill_sink;
230   } word;
231   uint8_t mem_le[0x1e];
232 #endif
233 };
234
235 struct cpu_z80 {
236   int cycles;
237   int (*read_byte)(void *context, int addr);
238   void *read_byte_context;
239   void (*write_byte)(void *context, int addr, int data);
240   void *write_byte_context;
241   int (*in_byte)(void *context, int addr);
242   void *in_byte_context;
243   void (*out_byte)(void *context, int addr, int data);
244   void *out_byte_context;
245   union cpu_z80_regs regs;
246 };
247
248 // memory or special memory access
249 static ALWAYS_INLINE int cpu_z80_read_byte(struct cpu_z80 *self, int addr) {
250   if (addr < 0)
251 #if __BYTE_ORDER == __BIG_ENDIAN
252     return self->regs.mem_be[~addr];
253 #else
254     return self->regs.mem_le[sizeof(union cpu_z80_regs) + addr];
255 #endif
256   self->cycles += 1;
257   return self->read_byte(self->read_byte_context, addr & 0xffff);
258 }
259
260 static ALWAYS_INLINE int cpu_z80_read_word(struct cpu_z80 *self, int addr) {
261   int data = cpu_z80_read_byte(self, addr);
262   return data | (cpu_z80_read_byte(self, addr + 1) << 8);
263 }
264
265 static ALWAYS_INLINE void cpu_z80_write_byte(struct cpu_z80 *self, int addr, int data) {
266   self->cycles += 1;
267   if (addr < 0)
268 #if __BYTE_ORDER == __BIG_ENDIAN
269     self->regs.mem_be[~addr] = data;
270 #else
271     self->regs.mem_le[sizeof(union cpu_z80_regs) + addr] = data;
272 #endif
273   else
274     self->write_byte(self->write_byte_context, addr, data);
275 }
276
277 static ALWAYS_INLINE void cpu_z80_write_word(struct cpu_z80 *self, int addr, int data) {
278   cpu_z80_write_byte(self, addr, data & 0xff);
279   cpu_z80_write_byte(self, addr + 1, data >> 8);
280 }
281
282 static ALWAYS_INLINE int cpu_z80_fetch_byte(struct cpu_z80 *self) {
283   int data = cpu_z80_read_byte(self, self->regs.word.pc++);
284   return data;
285 }
286
287 static ALWAYS_INLINE int cpu_z80_fetch_word(struct cpu_z80 *self) {
288   int data = cpu_z80_fetch_byte(self);
289   return data | (cpu_z80_fetch_byte(self) << 8);
290 }
291
292 static ALWAYS_INLINE void cpu_z80_push_word(struct cpu_z80 *self, int data) {
293   self->regs.word.sp -= 2;
294   cpu_z80_write_word(self, self->regs.word.sp, data);
295 }
296
297 static ALWAYS_INLINE int cpu_z80_pop_word(struct cpu_z80 *self) {
298   int data = cpu_z80_read_word(self, self->regs.word.sp);
299   self->regs.word.sp += 2;
300   return data;
301 }
302
303 static ALWAYS_INLINE int cpu_z80_in_byte(struct cpu_z80 *self, int addr) {
304   self->cycles += 1;
305   return self->in_byte(self->in_byte_context, addr);
306 }
307
308 static ALWAYS_INLINE void cpu_z80_out_byte(struct cpu_z80 *self, int addr, int data) {
309   self->cycles += 1;
310   self->out_byte(self->out_byte_context, addr, data);
311 }
312
313 // effective address calculation
314 static ALWAYS_INLINE int cpu_z80_relative(struct cpu_z80 *self) {
315   return (int8_t)cpu_z80_fetch_byte(self);
316 }
317
318 static ALWAYS_INLINE int cpu_z80_displacement(struct cpu_z80 *self, int base) {
319   return (base + (int8_t)cpu_z80_fetch_byte(self)) & 0xffff;
320 }
321
322 // byte-addressed ports are extended to word using a as high 8 bits
323 static ALWAYS_INLINE int cpu_z80_port_word(struct cpu_z80 *self) {
324   return cpu_z80_fetch_byte(self) | (self->regs.byte.a << 8);
325 }
326
327 // instruction execute
328 static ALWAYS_INLINE void cpu_z80_adc_byte(struct cpu_z80 *self, int lvalue, int rvalue) {
329   int data = cpu_z80_read_byte(self, lvalue);
330
331   int result0 = (data & 0xf) + (rvalue & 0xf) + self->regs.bit.cf;
332   int result1 = result0 + (data & 0x70) + (rvalue & 0x70);
333   int result2 = result1 + (data & 0x80) + (rvalue & 0x80);
334
335   cpu_z80_write_byte(self, lvalue, result2 & 0xff);
336   self->regs.bit.cf = result2 >> 8;
337   self->regs.bit.nf = false;
338   self->regs.bit.pvf = ((result1 >> 7) ^ (result2 >> 8)) & 1;
339   self->regs.bit.hf = result0 >> 4;
340   self->regs.bit.zf = (result2 & 0xff) == 0;
341   self->regs.bit.sf = (result2 >> 7) & 1;
342 }
343
344 static ALWAYS_INLINE void cpu_z80_adc_word(struct cpu_z80 *self, int lvalue, int rvalue) {
345   int data = cpu_z80_read_word(self, lvalue);
346
347   int result0 = (data & 0xfff) + (rvalue & 0xfff) + self->regs.bit.cf;
348   int result1 = result0 + (data & 0x7000) + (rvalue & 0x7000);
349   int result2 = result1 + (data & 0x8000) + (rvalue & 0x8000);
350
351   cpu_z80_write_word(self, lvalue, result2 & 0xffff);
352   self->regs.bit.cf = result2 >> 16;
353   self->regs.bit.nf = false;
354   self->regs.bit.pvf = ((result1 >> 15) ^ (result2 >> 16)) & 1;
355   self->regs.bit.hf = result0 >> 12;
356   self->regs.bit.zf = (result2 & 0xffff) == 0;
357   self->regs.bit.sf = (result2 >> 15) & 1;
358 }
359
360 static ALWAYS_INLINE void cpu_z80_add_byte(struct cpu_z80 *self, int lvalue, int rvalue) {
361   int data = cpu_z80_read_byte(self, lvalue);
362
363   int result0 = (data & 0xf) + (rvalue & 0xf);
364   int result1 = result0 + (data & 0x70) + (rvalue & 0x70);
365   int result2 = result1 + (data & 0x80) + (rvalue & 0x80);
366
367   cpu_z80_write_byte(self, lvalue, result2 & 0xff);
368   self->regs.bit.cf = result2 >> 8;
369   self->regs.bit.nf = false;
370   self->regs.bit.pvf = ((result1 >> 7) ^ (result2 >> 8)) & 1;
371   self->regs.bit.hf = result0 >> 4;
372   self->regs.bit.zf = (result2 & 0xff) == 0;
373   self->regs.bit.sf = (result2 >> 7) & 1;
374 }
375
376 static ALWAYS_INLINE void cpu_z80_add_word(struct cpu_z80 *self, int lvalue, int rvalue) {
377   int data = cpu_z80_read_word(self, lvalue);
378
379   int result0 = (data & 0xfff) + (rvalue & 0xfff);
380   int result1 = result0 + (data & 0xf000) + (rvalue & 0xf000);
381
382   cpu_z80_write_word(self, lvalue, result1 & 0xffff);
383   self->regs.bit.cf = result1 >> 16;
384   self->regs.bit.nf = false;
385   self->regs.bit.hf = result0 >> 12;
386 }
387
388 static ALWAYS_INLINE void cpu_z80_and(struct cpu_z80 *self, int rvalue) {
389   int result = self->regs.byte.a & rvalue;
390
391   int parity = result;
392   parity ^= parity >> 4;
393   parity ^= parity >> 2;
394   parity ^= parity >> 1;
395
396   self->regs.byte.a = result;
397   self->regs.bit.cf = false;
398   self->regs.bit.nf = false;
399   self->regs.bit.pvf = (parity & 1) == 0;
400   self->regs.bit.hf = true;
401   self->regs.bit.zf = result == 0;
402   self->regs.bit.sf = result >> 7;
403 }
404
405 static ALWAYS_INLINE void cpu_z80_bit(struct cpu_z80 *self, int n, int rvalue) {
406   int result = rvalue & (1 << n);
407
408   self->regs.bit.nf = false;
409   self->regs.bit.pvf = result == 0;
410   self->regs.bit.hf = true;
411   self->regs.bit.zf = result == 0;
412   self->regs.bit.sf = result >> 7;
413 }
414
415 static ALWAYS_INLINE void cpu_z80_call(struct cpu_z80 *self, bool pred, int rvalue) {
416   if (pred) {
417     cpu_z80_push_word(self, self->regs.word.pc);
418     self->regs.word.pc = rvalue;
419   }
420 }
421
422 static ALWAYS_INLINE void cpu_z80_ccf(struct cpu_z80 *self) {
423   self->regs.bit.hf = self->regs.bit.cf;
424   self->regs.bit.cf = !self->regs.bit.cf;
425   self->regs.bit.nf = false;
426 }
427
428 static ALWAYS_INLINE void cpu_z80_cp(struct cpu_z80 *self, int rvalue) {
429   int result0 = (self->regs.byte.a & 0xf) - (rvalue & 0xf);
430   int result1 = result0 + (self->regs.byte.a & 0x70) - (rvalue & 0x70);
431   int result2 = result1 + (self->regs.byte.a & 0x80) - (rvalue & 0x80);
432
433   self->regs.bit.cf = (result2 >> 8) & 1;
434   self->regs.bit.nf = true;
435   self->regs.bit.pvf = ((result1 >> 7) ^ (result2 >> 8)) & 1;
436   self->regs.bit.hf = (result0 >> 4) & 1;
437   self->regs.bit.zf = (result2 & 0xff) == 0;
438   self->regs.bit.sf = (result2 >> 7) & 1;
439 }
440
441 static ALWAYS_INLINE void cpu_z80_cpd(struct cpu_z80 *self) {
442   int data = cpu_z80_read_byte(self, self->regs.word.hl--);
443   --self->regs.word.bc;
444
445   int result0 = (self->regs.byte.a & 0xf) - (data & 0xf);
446   int result1 = result0 + (self->regs.byte.a & 0xf0) - (data & 0xf0);
447
448   self->regs.bit.nf = true;
449   self->regs.bit.pvf = self->regs.word.bc != 0;
450   self->regs.bit.hf = (result0 >> 4) & 1;
451   self->regs.bit.zf = (result1 & 0xff) == 0;
452   self->regs.bit.sf = (result1 >> 7) & 1;
453 }
454
455 static ALWAYS_INLINE void cpu_z80_cpdr(struct cpu_z80 *self) {
456   cpu_z80_cpd(self);
457   if (!self->regs.bit.zf && self->regs.word.bc)
458     self->regs.word.pc -= 2;
459 }
460
461 static ALWAYS_INLINE void cpu_z80_cpi(struct cpu_z80 *self) {
462   int data = cpu_z80_read_byte(self, self->regs.word.hl++);
463   --self->regs.word.bc;
464
465   int result0 = (self->regs.byte.a & 0xf) - (data & 0xf);
466   int result1 = result0 + (self->regs.byte.a & 0xf0) - (data & 0xf0);
467
468   self->regs.bit.nf = true;
469   self->regs.bit.pvf = self->regs.word.bc != 0;
470   self->regs.bit.hf = (result0 >> 4) & 1;
471   self->regs.bit.zf = (result1 & 0xff) == 0;
472   self->regs.bit.sf = (result1 >> 7) & 1;
473 }
474
475 static ALWAYS_INLINE void cpu_z80_cpir(struct cpu_z80 *self) {
476   cpu_z80_cpi(self);
477   if (!self->regs.bit.zf && self->regs.word.bc)
478     self->regs.word.pc -= 2;
479 }
480
481 static ALWAYS_INLINE void cpu_z80_cpl(struct cpu_z80 *self) {
482   self->regs.byte.a = ~self->regs.byte.a;
483   self->regs.bit.nf = true;
484   self->regs.bit.hf = true;
485 }
486
487 // see https://github.com/kosarev/z80.git (MIT licensed)
488 //    void do_i8080_daa() {
489 //        fast_u8 a = self().on_get_a();
490 //        flag_set flags = get_flags();
491 //        fast_u8 f = flags.get_hf_cf();
492 //
493 //        fast_u8 r = a;
494 //        fast_u8 t = r + 6;
495 //        fast_u8 hfv = a ^ t;
496 //        if((hfv | f) & hf_mask)
497 //            r = t;
498 //
499 //        fast_u16 t2 = r + 0x60;
500 //        fast_u16 w = ((t2 >> 8) | f) << 8;
501 //        if(w & 0x100)
502 //            r = mask8(t2);
503 //
504 //        self().on_set_a(r);
505 //        flags.set(hfv & hf_mask, (w | r) & 0x1ff);
506 //        set_flags(flags); }
507 //    void do_z80_daa() {
508 //        fast_u8 a = self().on_get_a();
509 //        fast_u8 f = self().on_get_f();
510 //        bool cf = f & cf_mask;
511 //        bool hf = f & hf_mask;
512 //        bool nf = f & nf_mask;
513 //
514 //        fast_u8 d = 0x00;
515 //        if(cf || a >= 0x9a) {
516 //            d |= 0x60;
517 //            f |= cf_mask;
518 //        }
519 //        if(hf || (a & 0x0f) >= 0x0a) {
520 //            d |= 0x06;
521 //        }
522 //
523 //        if(!nf) {
524 //            f = (f & cf_mask) | ((a & 0x0f) >= 0x0a ? hf_mask : 0);
525 //            a = add8(a, d);
526 //        } else {
527 //            f = (f & cf_mask) | (hf && (a & 0x0f) <= 0x05 ? hf_mask : 0) |
528 //                    nf_mask;
529 //            a = sub8(a, d);
530 //        }
531 //        f |= (a & (sf_mask | xf_mask | yf_mask)) | pf_log(a) | zf_ari(a);
532 //
533 //        self().on_set_a(a);
534 //        self().on_set_f(f); }
535 static ALWAYS_INLINE void cpu_z80_daa(struct cpu_z80 *self) {
536   int correction = 0;
537   if (self->regs.bit.hf || (self->regs.byte.a & 0xf) >= 0xa)
538     correction = 6;
539   if (self->regs.bit.cf || self->regs.byte.a >= 0x9a) {
540     correction |= 0x60;
541     self->regs.bit.cf = true;
542   }
543
544   int result0, result1;
545   if (self->regs.bit.nf) {
546     result0 = (self->regs.byte.a & 0xf) - (correction & 0xf);
547     result1 = result0 + (self->regs.byte.a & 0xf0) - (correction & 0xf0);
548   }
549   else {
550     result0 = (self->regs.byte.a & 0xf) + (correction & 0xf);
551     result1 = result0 + (self->regs.byte.a & 0xf0) + (correction & 0xf0);
552   }
553
554   int parity = result1;
555   parity ^= parity >> 4;
556   parity ^= parity >> 2;
557   parity ^= parity >> 1;
558
559   self->regs.byte.a = result1 & 0xff;
560   //self->regs.bit.cf |= (result1 >> 8) & 1;
561   self->regs.bit.pvf = (parity & 1) == 0;
562   self->regs.bit.hf = (result0 >> 4) & 1;
563   self->regs.bit.zf = (result1 & 0xff) == 0;
564   self->regs.bit.sf = (result1 >> 7) & 1;
565 }
566
567 static ALWAYS_INLINE void cpu_z80_dec_byte(struct cpu_z80 *self, int lvalue) {
568   int data = cpu_z80_read_byte(self, lvalue);
569
570   int result0 = (data & 0xf) - 1;
571   int result1 = result0 + (data & 0x70);
572   int result2 = result1 + (data & 0x80);
573
574   cpu_z80_write_byte(self, lvalue, result2 & 0xff);
575   self->regs.bit.nf = true;
576   self->regs.bit.pvf = ((result1 >> 7) ^ (result2 >> 8)) & 1;
577   self->regs.bit.hf = (result0 >> 4) & 1;
578   self->regs.bit.zf = (result2 & 0xff) == 0;
579   self->regs.bit.sf = (result2 >> 7) & 1;
580 }
581
582 static ALWAYS_INLINE void cpu_z80_dec_word(struct cpu_z80 *self, int lvalue) {
583   int result = (cpu_z80_read_word(self, lvalue) - 1) & 0xffff;
584   cpu_z80_write_word(self, lvalue, result);
585 }
586
587 static ALWAYS_INLINE void cpu_z80_di(struct cpu_z80 *self) {
588   self->regs.bit.iff1 = false;
589   self->regs.bit.iff2 = false;
590 }
591
592 static ALWAYS_INLINE void cpu_z80_djnz(struct cpu_z80 *self, int rvalue) {
593   if (--self->regs.byte.b)
594     self->regs.word.pc += rvalue;
595 }
596
597 static ALWAYS_INLINE void cpu_z80_ei(struct cpu_z80 *self) {
598   self->regs.bit.iff1 = true;
599   self->regs.bit.iff2 = true;
600   self->regs.bit.ei_flag = true; // causes one-cycle interrupt delay
601 }
602
603 static ALWAYS_INLINE void cpu_z80_ex(struct cpu_z80 *self, int lvalue0, int lvalue1) {
604   int data0 = cpu_z80_read_word(self, lvalue0);
605   int data1 = cpu_z80_read_word(self, lvalue1);
606   cpu_z80_write_word(self, lvalue0, data1);
607   cpu_z80_write_word(self, lvalue1, data0);
608 }
609
610 static ALWAYS_INLINE void cpu_z80_halt(struct cpu_z80 *self) {
611   self->regs.bit.halt_flag = true;
612   --self->regs.word.pc;
613 }
614
615 static ALWAYS_INLINE void cpu_z80_ill(struct cpu_z80 *self) {
616   abort();
617 }
618
619 static ALWAYS_INLINE void cpu_z80_im(struct cpu_z80 *self, int n) {
620   self->regs.bit.im = n;
621 }
622
623 static ALWAYS_INLINE void cpu_z80_in(struct cpu_z80 *self, int lvalue, int rvalue) {
624   cpu_z80_write_byte(self, lvalue, rvalue);
625 }
626
627 static ALWAYS_INLINE void cpu_z80_in_z80(struct cpu_z80 *self, int lvalue, int rvalue) {
628   int parity = rvalue;
629   parity ^= parity >> 4;
630   parity ^= parity >> 2;
631   parity ^= parity >> 1;
632
633   cpu_z80_write_byte(self, lvalue, rvalue);
634   self->regs.bit.nf = false;
635   self->regs.bit.pvf = (parity & 1) == 0;
636   self->regs.bit.hf = false;
637   self->regs.bit.zf = rvalue == 0;
638   self->regs.bit.sf = rvalue >> 7;
639 }
640
641 static ALWAYS_INLINE void cpu_z80_inc_byte(struct cpu_z80 *self, int lvalue) {
642   int data = cpu_z80_read_byte(self, lvalue);
643
644   int result0 = (data & 0xf) + 1;
645   int result1 = result0 + (data & 0x70);
646   int result2 = result1 + (data & 0x80);
647
648   cpu_z80_write_byte(self, lvalue, result2 & 0xff);
649   self->regs.bit.nf = false;
650   self->regs.bit.pvf = ((result1 >> 7) ^ (result2 >> 8)) & 1;
651   self->regs.bit.hf = result0 >> 4;
652   self->regs.bit.zf = (result2 & 0xff) == 0;
653   self->regs.bit.sf = (result2 >> 7) & 1;
654 }
655
656 static ALWAYS_INLINE void cpu_z80_inc_word(struct cpu_z80 *self, int lvalue) {
657   int result = (cpu_z80_read_word(self, lvalue) + 1) & 0xffff;
658   cpu_z80_write_word(self, lvalue, result);
659 }
660
661 static ALWAYS_INLINE void cpu_z80_ind(struct cpu_z80 *self) {
662   cpu_z80_write_byte(
663     self,
664     self->regs.word.hl--,
665     cpu_z80_in_byte(self, self->regs.word.bc)
666   );
667   --self->regs.byte.b;
668
669   self->regs.bit.nf = true;
670   self->regs.bit.zf = self->regs.byte.b != 0;
671 }
672
673 static ALWAYS_INLINE void cpu_z80_indr(struct cpu_z80 *self) {
674   cpu_z80_ind(self);
675   if (self->regs.byte.b)
676     self->regs.word.pc -= 2;
677 }
678
679 static ALWAYS_INLINE void cpu_z80_ini(struct cpu_z80 *self) {
680   cpu_z80_write_byte(
681     self,
682     self->regs.word.hl++,
683     cpu_z80_in_byte(self, self->regs.word.bc)
684   );
685   --self->regs.byte.b;
686
687   self->regs.bit.nf = true;
688   self->regs.bit.zf = self->regs.byte.b != 0;
689 }
690
691 static ALWAYS_INLINE void cpu_z80_inir(struct cpu_z80 *self) {
692   cpu_z80_ini(self);
693   if (self->regs.byte.b)
694     self->regs.word.pc -= 2;
695 }
696
697 static ALWAYS_INLINE void cpu_z80_jp(struct cpu_z80 *self, bool pred, int rvalue) {
698   if (pred)
699     self->regs.word.pc = rvalue;
700 }
701
702 static ALWAYS_INLINE void cpu_z80_jr(struct cpu_z80 *self, bool pred, int rvalue) {
703   if (pred)
704     self->regs.word.pc += rvalue;
705 }
706
707 static ALWAYS_INLINE void cpu_z80_ld_byte(struct cpu_z80 *self, int lvalue, int rvalue) {
708   cpu_z80_write_byte(self, lvalue, rvalue);
709 }
710
711 static ALWAYS_INLINE void cpu_z80_ld_word(struct cpu_z80 *self, int lvalue, int rvalue) {
712   cpu_z80_write_word(self, lvalue, rvalue);
713 }
714
715 static ALWAYS_INLINE void cpu_z80_ldd(struct cpu_z80 *self) {
716   cpu_z80_write_byte(
717     self,
718     self->regs.word.de++,
719     cpu_z80_read_byte(self, self->regs.word.hl--)
720   );
721   --self->regs.word.bc;
722
723   self->regs.bit.nf = false;
724   self->regs.bit.pvf = self->regs.word.bc != 0;
725   self->regs.bit.hf = false;
726 }
727
728 static ALWAYS_INLINE void cpu_z80_lddr(struct cpu_z80 *self) {
729   cpu_z80_ldd(self);
730   if (self->regs.word.bc)
731     self->regs.word.pc -= 2;
732 }
733
734 static ALWAYS_INLINE void cpu_z80_ldi(struct cpu_z80 *self) {
735   cpu_z80_write_byte(
736     self,
737     self->regs.word.de++,
738     cpu_z80_read_byte(self, self->regs.word.hl++)
739   );
740   --self->regs.word.bc;
741
742   self->regs.bit.nf = false;
743   self->regs.bit.pvf = self->regs.word.bc != 0;
744   self->regs.bit.hf = false;
745 }
746
747 static ALWAYS_INLINE void cpu_z80_ldir(struct cpu_z80 *self) {
748   cpu_z80_ldi(self);
749   if (self->regs.word.bc)
750     self->regs.word.pc -= 2;
751 }
752
753 static ALWAYS_INLINE void cpu_z80_neg(struct cpu_z80 *self) {
754   int result0 = -(self->regs.byte.a & 0xf);
755   int result1 = result0 - (self->regs.byte.a & 0x70);
756   int result2 = result1 - (self->regs.byte.a & 0x80);
757
758   self->regs.byte.a = result2 & 0xff;
759   self->regs.bit.cf = (result2 >> 8) & 1;
760   self->regs.bit.nf = true;
761   self->regs.bit.pvf = ((result1 >> 7) ^ (result2 >> 8)) & 1;
762   self->regs.bit.hf = (result0 >> 4) & 1;
763   self->regs.bit.zf = (result2 & 0xff) == 0;
764   self->regs.bit.sf = (result2 >> 7) & 1;
765 }
766
767 static ALWAYS_INLINE void cpu_z80_nop(struct cpu_z80 *self) {
768 }
769
770 static ALWAYS_INLINE void cpu_z80_or(struct cpu_z80 *self, int rvalue) {
771   int result = self->regs.byte.a | rvalue;
772
773   int parity = result;
774   parity ^= parity >> 4;
775   parity ^= parity >> 2;
776   parity ^= parity >> 1;
777
778   self->regs.byte.a = result;
779   self->regs.bit.cf = false;
780   self->regs.bit.nf = false;
781   self->regs.bit.pvf = (parity & 1) == 0;
782   self->regs.bit.hf = false;
783   self->regs.bit.zf = result == 0;
784   self->regs.bit.sf = result >> 7;
785 }
786
787 static ALWAYS_INLINE void cpu_z80_out(struct cpu_z80 *self, int lvalue, int rvalue) {
788   cpu_z80_out_byte(self, lvalue, rvalue);
789 }
790
791 static ALWAYS_INLINE void cpu_z80_outd(struct cpu_z80 *self) {
792   --self->regs.byte.b;
793   cpu_z80_out_byte(
794     self,
795     self->regs.word.bc,
796     cpu_z80_read_byte(self, self->regs.word.hl--)
797   );
798
799   self->regs.bit.nf = true;
800   self->regs.bit.zf = self->regs.byte.b != 0;
801 }
802
803 static ALWAYS_INLINE void cpu_z80_otdr(struct cpu_z80 *self) {
804   cpu_z80_outd(self);
805   if (self->regs.byte.b)
806     self->regs.word.pc -= 2;
807 }
808
809 static ALWAYS_INLINE void cpu_z80_outi(struct cpu_z80 *self) {
810   --self->regs.byte.b;
811   cpu_z80_out_byte(
812     self,
813     self->regs.word.bc,
814     cpu_z80_read_byte(self, self->regs.word.hl++)
815   );
816
817   self->regs.bit.nf = true;
818   self->regs.bit.zf = self->regs.byte.b != 0;
819 }
820
821 static ALWAYS_INLINE void cpu_z80_otir(struct cpu_z80 *self) {
822   cpu_z80_outi(self);
823   if (self->regs.byte.b)
824     self->regs.word.pc -= 2;
825 }
826
827 static ALWAYS_INLINE void cpu_z80_pop(struct cpu_z80 *self, int lvalue) {
828   cpu_z80_write_word(self, lvalue, cpu_z80_pop_word(self));
829 }
830
831 static ALWAYS_INLINE void cpu_z80_push(struct cpu_z80 *self, int rvalue) {
832   cpu_z80_push_word(self, rvalue);
833 }
834
835 static ALWAYS_INLINE void cpu_z80_res(struct cpu_z80 *self, int n, int lvalue0, int lvalue1) {
836   int result = cpu_z80_read_byte(self, lvalue0) & ~(1 << n);
837
838   cpu_z80_write_byte(self, lvalue0, result);
839   cpu_z80_write_byte(self, lvalue1, result); // for undocumented DDCB/FDCB
840 }
841
842 static ALWAYS_INLINE void cpu_z80_ret(struct cpu_z80 *self, bool pred) {
843   if (pred)
844     self->regs.word.pc = cpu_z80_pop_word(self);
845 }
846
847 static ALWAYS_INLINE void cpu_z80_reti(struct cpu_z80 *self) {
848   self->regs.bit.reti_flag = true;
849   self->regs.word.pc = cpu_z80_pop_word(self);
850 }
851
852 static ALWAYS_INLINE void cpu_z80_retn(struct cpu_z80 *self) {
853   self->regs.bit.iff1 = self->regs.bit.iff2;
854   self->regs.word.pc = cpu_z80_pop_word(self);
855 }
856
857 static ALWAYS_INLINE void cpu_z80_rl(struct cpu_z80 *self, int lvalue0, int lvalue1) {
858   int data = cpu_z80_read_byte(self, lvalue0);
859
860   int result = self->regs.bit.cf | (data << 1);
861
862   int parity = result;
863   parity ^= parity >> 4;
864   parity ^= parity >> 2;
865   parity ^= parity >> 1;
866
867   data = result & 0xff;
868   cpu_z80_write_byte(self, lvalue0, data);
869   cpu_z80_write_byte(self, lvalue1, data); // for undocumented DDCB/FDCB
870
871   self->regs.bit.cf = result >> 8;
872   self->regs.bit.nf = false;
873   self->regs.bit.pvf = (parity & 1) == 0;
874   self->regs.bit.hf = false;
875   self->regs.bit.zf = (result & 0xff) == 0;
876   self->regs.bit.sf = (result >> 7) & 1;
877 }
878
879 static ALWAYS_INLINE void cpu_z80_rla(struct cpu_z80 *self) {
880   int result = self->regs.bit.cf | (self->regs.byte.a << 1);
881
882   self->regs.byte.a = result & 0xff;
883   self->regs.bit.cf = result >> 8;
884   self->regs.bit.nf = false;
885   self->regs.bit.hf = false;
886 }
887
888 static ALWAYS_INLINE void cpu_z80_rlc(struct cpu_z80 *self, int lvalue0, int lvalue1) {
889   int data = cpu_z80_read_byte(self, lvalue0);
890
891   int result = (data >> 7) | (data << 1);
892
893   int parity = result;
894   parity ^= parity >> 4;
895   parity ^= parity >> 2;
896   parity ^= parity >> 1;
897
898   data = result & 0xff;
899   cpu_z80_write_byte(self, lvalue0, data);
900   cpu_z80_write_byte(self, lvalue1, data); // for undocumented DDCB/FDCB
901
902   self->regs.bit.cf = result >> 8;
903   self->regs.bit.nf = false;
904   self->regs.bit.pvf = (parity & 1) == 0;
905   self->regs.bit.hf = false;
906   self->regs.bit.zf = (result & 0xff) == 0;
907   self->regs.bit.sf = (result >> 7) & 1;
908 }
909
910 static ALWAYS_INLINE void cpu_z80_rlca(struct cpu_z80 *self) {
911   int result = (self->regs.byte.a >> 7) | (self->regs.byte.a << 1);
912
913   self->regs.byte.a = result & 0xff;
914   self->regs.bit.cf = result >> 8;
915   self->regs.bit.nf = false;
916   self->regs.bit.hf = false;
917 }
918
919 static ALWAYS_INLINE void cpu_z80_rld(struct cpu_z80 *self) {
920   int result =
921     (self->regs.byte.a & 0xf) |
922     (cpu_z80_read_byte(self, self->regs.word.hl) << 4);
923
924   cpu_z80_write_byte(self, self->regs.word.hl, result & 0xff);
925   result = (result >> 8) | (self->regs.byte.a & 0xf0);
926
927   int parity = result;
928   parity ^= parity >> 4;
929   parity ^= parity >> 2;
930   parity ^= parity >> 1;
931
932   self->regs.byte.a = result;
933   self->regs.bit.nf = false;
934   self->regs.bit.pvf = (parity & 1) == 0;
935   self->regs.bit.hf = false;
936   self->regs.bit.zf = result == 0;
937   self->regs.bit.sf = result >> 7;
938 }
939
940 static ALWAYS_INLINE void cpu_z80_rr(struct cpu_z80 *self, int lvalue0, int lvalue1) {
941   int data = cpu_z80_read_byte(self, lvalue0);
942
943   int result = data | (self->regs.bit.cf << 8);
944
945   int parity = result;
946   parity ^= parity >> 4;
947   parity ^= parity >> 2;
948   parity ^= parity >> 1;
949
950   data = result >> 1;
951   cpu_z80_write_byte(self, lvalue0, data);
952   cpu_z80_write_byte(self, lvalue1, data); // for undocumented DDCB/FDCB
953
954   self->regs.bit.cf = result & 1;
955   self->regs.bit.nf = false;
956   self->regs.bit.pvf = (parity & 2) == 0;
957   self->regs.bit.hf = false;
958   self->regs.bit.zf = (result & 0x1fe) == 0;
959   self->regs.bit.sf = result >> 8;
960 }
961
962 static ALWAYS_INLINE void cpu_z80_rra(struct cpu_z80 *self) {
963   int result = self->regs.byte.a | (self->regs.bit.cf << 8);
964
965   self->regs.byte.a = result >> 1;
966   self->regs.bit.cf = result & 1;
967   self->regs.bit.nf = false;
968   self->regs.bit.hf = false;
969 }
970
971 static ALWAYS_INLINE void cpu_z80_rrc(struct cpu_z80 *self, int lvalue0, int lvalue1) {
972   int data = cpu_z80_read_byte(self, lvalue0);
973
974   int result = data | ((data & 1) << 8);
975
976   int parity = result;
977   parity ^= parity >> 4;
978   parity ^= parity >> 2;
979   parity ^= parity >> 1;
980
981   data = result >> 1;
982   cpu_z80_write_byte(self, lvalue0, data);
983   cpu_z80_write_byte(self, lvalue1, data); // for undocumented DDCB/FDCB
984
985   self->regs.bit.cf = result & 1;
986   self->regs.bit.nf = false;
987   self->regs.bit.pvf = (parity & 2) == 0;
988   self->regs.bit.hf = false;
989   self->regs.bit.zf = (result & 0x1fe) == 0;
990   self->regs.bit.sf = result >> 8;
991 }
992
993 static ALWAYS_INLINE void cpu_z80_rrca(struct cpu_z80 *self) {
994   int result = self->regs.byte.a | ((self->regs.byte.a & 1) << 8);
995
996   self->regs.byte.a = result >> 1;
997   self->regs.bit.cf = result & 1;
998   self->regs.bit.nf = false;
999   self->regs.bit.hf = false;
1000 }
1001
1002 static ALWAYS_INLINE void cpu_z80_rrd(struct cpu_z80 *self) {
1003   int result =
1004     cpu_z80_read_byte(self, self->regs.word.hl) |
1005     ((self->regs.byte.a & 0xf) << 8);
1006
1007   cpu_z80_write_byte(self, self->regs.word.hl, result >> 4);
1008   result = (result & 0xf) | (self->regs.byte.a & 0xf0);
1009
1010   int parity = result;
1011   parity ^= parity >> 4;
1012   parity ^= parity >> 2;
1013   parity ^= parity >> 1;
1014
1015   self->regs.byte.a = result;
1016   self->regs.bit.nf = false;
1017   self->regs.bit.pvf = (parity & 1) == 0;
1018   self->regs.bit.hf = false;
1019   self->regs.bit.zf = result == 0;
1020   self->regs.bit.sf = result >> 7;
1021 }
1022
1023 static ALWAYS_INLINE void cpu_z80_sbc_byte(struct cpu_z80 *self, int lvalue, int rvalue) {
1024   int data = cpu_z80_read_byte(self, lvalue);
1025
1026   int result0 = (data & 0xf) - (rvalue & 0xf) - self->regs.bit.cf;
1027   int result1 = result0 + (data & 0x70) - (rvalue & 0x70);
1028   int result2 = result1 + (data & 0x80) - (rvalue & 0x80);
1029
1030   cpu_z80_write_byte(self, lvalue, result2 & 0xff);
1031   self->regs.bit.cf = (result2 >> 8) & 1;
1032   self->regs.bit.nf = true;
1033   self->regs.bit.pvf = ((result1 >> 7) ^ (result2 >> 8)) & 1;
1034   self->regs.bit.hf = (result0 >> 4) & 1;
1035   self->regs.bit.zf = (result2 & 0xff) == 0;
1036   self->regs.bit.sf = (result2 >> 7) & 1;
1037 }
1038
1039 static ALWAYS_INLINE void cpu_z80_sbc_word(struct cpu_z80 *self, int lvalue, int rvalue) {
1040   int data = cpu_z80_read_word(self, lvalue);
1041
1042   int result0 = (data & 0xfff) - (rvalue & 0xfff) - self->regs.bit.cf;
1043   int result1 = result0 + (data & 0x7000) - (rvalue & 0x7000);
1044   int result2 = result1 + (data & 0x8000) - (rvalue & 0x8000);
1045
1046   cpu_z80_write_word(self, lvalue, result2 & 0xffff);
1047   self->regs.bit.cf = (result2 >> 16) & 1;
1048   self->regs.bit.nf = true;
1049   self->regs.bit.pvf = ((result1 >> 15) ^ (result2 >> 16)) & 1;
1050   self->regs.bit.hf = (result0 >> 12) & 1;
1051   self->regs.bit.zf = (result2 & 0xffff) == 0;
1052   self->regs.bit.sf = (result2 >> 15) & 1;
1053 }
1054
1055 static ALWAYS_INLINE void cpu_z80_scf(struct cpu_z80 *self) {
1056   self->regs.bit.cf = true;
1057   self->regs.bit.nf = false;
1058   self->regs.bit.hf = false;
1059 }
1060
1061 static ALWAYS_INLINE void cpu_z80_set(struct cpu_z80 *self, int n, int lvalue0, int lvalue1) {
1062   int result = cpu_z80_read_byte(self, lvalue0) | (1 << n);
1063
1064   cpu_z80_write_byte(self, lvalue0, result);
1065   cpu_z80_write_byte(self, lvalue1, result); // for undocumented DDCB/FDCB
1066 }
1067
1068 static ALWAYS_INLINE void cpu_z80_sla(struct cpu_z80 *self, int lvalue0, int lvalue1) {
1069   int data = cpu_z80_read_byte(self, lvalue0);
1070
1071   int result = data << 1;
1072
1073   int parity = result;
1074   parity ^= parity >> 4;
1075   parity ^= parity >> 2;
1076   parity ^= parity >> 1;
1077
1078   data = result & 0xff;
1079   cpu_z80_write_byte(self, lvalue0, data);
1080   cpu_z80_write_byte(self, lvalue1, data); // for undocumented DDCB/FDCB
1081
1082   self->regs.bit.cf = result >> 8;
1083   self->regs.bit.nf = false;
1084   self->regs.bit.pvf = (parity & 1) == 0;
1085   self->regs.bit.hf = false;
1086   self->regs.bit.zf = (result & 0xff) == 0;
1087   self->regs.bit.sf = (result >> 7) & 1;
1088 }
1089
1090 static ALWAYS_INLINE void cpu_z80_sll(struct cpu_z80 *self, int lvalue0, int lvalue1) {
1091   int data = cpu_z80_read_byte(self, lvalue0);
1092
1093   int result = 1 | (data << 1);
1094
1095   int parity = result;
1096   parity ^= parity >> 4;
1097   parity ^= parity >> 2;
1098   parity ^= parity >> 1;
1099
1100   data = result & 0xff;
1101   cpu_z80_write_byte(self, lvalue0, data);
1102   cpu_z80_write_byte(self, lvalue1, data); // for undocumented DDCB/FDCB
1103
1104   self->regs.bit.cf = result >> 8;
1105   self->regs.bit.nf = false;
1106   self->regs.bit.pvf = (parity & 1) == 0;
1107   self->regs.bit.hf = false;
1108   self->regs.bit.zf = (result & 0xff) == 0;
1109   self->regs.bit.sf = (result >> 7) & 1;
1110 }
1111
1112 static ALWAYS_INLINE void cpu_z80_sra(struct cpu_z80 *self, int lvalue0, int lvalue1) {
1113   int data = cpu_z80_read_byte(self, lvalue0);
1114
1115   int result = data | ((data << 1) & 0x100); // sign extend
1116
1117   int parity = result;
1118   parity ^= parity >> 4;
1119   parity ^= parity >> 2;
1120   parity ^= parity >> 1;
1121
1122   data = result >> 1;
1123   cpu_z80_write_byte(self, lvalue0, data);
1124   cpu_z80_write_byte(self, lvalue1, data); // for undocumented DDCB/FDCB
1125
1126   self->regs.bit.cf = result & 1;
1127   self->regs.bit.nf = false;
1128   self->regs.bit.pvf = (parity & 2) == 0;
1129   self->regs.bit.hf = false;
1130   self->regs.bit.zf = (result & 0x1fe) == 0;
1131   self->regs.bit.sf = result >> 8;
1132 }
1133
1134 static ALWAYS_INLINE void cpu_z80_srl(struct cpu_z80 *self, int lvalue0, int lvalue1) {
1135   int data = cpu_z80_read_byte(self, lvalue0);
1136
1137   int result = data;
1138
1139   int parity = result;
1140   parity ^= parity >> 4;
1141   parity ^= parity >> 2;
1142   parity ^= parity >> 1;
1143
1144   data = result >> 1;
1145   cpu_z80_write_byte(self, lvalue0, data);
1146   cpu_z80_write_byte(self, lvalue1, data); // for undocumented DDCB/FDCB
1147
1148   self->regs.bit.cf = result & 1;
1149   self->regs.bit.nf = false;
1150   self->regs.bit.pvf = (parity & 2) == 0;
1151   self->regs.bit.hf = false;
1152   self->regs.bit.zf = (result & 0x1fe) == 0;
1153   self->regs.bit.sf = result >> 8;
1154 }
1155
1156 static ALWAYS_INLINE void cpu_z80_sub(struct cpu_z80 *self, int rvalue) {
1157   int result0 = (self->regs.byte.a & 0xf) - (rvalue & 0xf);
1158   int result1 = result0 + (self->regs.byte.a & 0x70) - (rvalue & 0x70);
1159   int result2 = result1 + (self->regs.byte.a & 0x80) - (rvalue & 0x80);
1160
1161   self->regs.byte.a = result2 & 0xff;
1162   self->regs.bit.cf = (result2 >> 8) & 1;
1163   self->regs.bit.nf = true;
1164   self->regs.bit.pvf = ((result1 >> 7) ^ (result2 >> 8)) & 1;
1165   self->regs.bit.hf = (result0 >> 4) & 1;
1166   self->regs.bit.zf = (result2 & 0xff) == 0;
1167   self->regs.bit.sf = (result2 >> 7) & 1;
1168 }
1169
1170 static ALWAYS_INLINE void cpu_z80_xor(struct cpu_z80 *self, int rvalue) {
1171   int result = self->regs.byte.a ^ rvalue;
1172
1173   int parity = result;
1174   parity ^= parity >> 4;
1175   parity ^= parity >> 2;
1176   parity ^= parity >> 1;
1177
1178   self->regs.byte.a = result;
1179   self->regs.bit.cf = false;
1180   self->regs.bit.nf = false;
1181   self->regs.bit.pvf = (parity & 1) == 0;
1182   self->regs.bit.hf = false;
1183   self->regs.bit.zf = result == 0;
1184   self->regs.bit.sf = result >> 7;
1185 }
1186
1187 // prototypes
1188 void cpu_z80_init(
1189   struct cpu_z80 *self,
1190   int (*read_byte)(void *context, int addr),
1191   void *read_byte_context,
1192   void (*write_byte)(void *context, int addr, int data),
1193   void *write_byte_context,
1194   int (*in_byte)(void *context, int addr),
1195   void *in_byte_context,
1196   void (*out_byte)(void *context, int addr, int data),
1197   void *out_byte_context
1198 );
1199 void cpu_z80_reset(struct cpu_z80 *self);
1200 void cpu_z80_execute(struct cpu_z80 *self);
1201 void cpu_z80_execute_cb(struct cpu_z80 *self);
1202 void cpu_z80_execute_dd(struct cpu_z80 *self);
1203 void cpu_z80_execute_dd_cb(struct cpu_z80 *self);
1204 void cpu_z80_execute_ed(struct cpu_z80 *self);
1205 void cpu_z80_execute_fd(struct cpu_z80 *self);
1206 void cpu_z80_execute_fd_cb(struct cpu_z80 *self);
1207
1208 #endif