1 // Z80 CPU emulator
2
3 // Game_Music_Emu 0.5.5
4 #ifndef AY_CPU_H
5 #define AY_CPU_H
6
7 #include "blargg_endian.h"
8
9 typedef blargg_long cpu_time_t;
10
11 // must be defined by caller
12 void ay_cpu_out( class Ay_Cpu*, cpu_time_t, unsigned addr, int data );
13 int ay_cpu_in( class Ay_Cpu*, unsigned addr );
14
15 class Ay_Cpu {
16 public:
17 // Clear all registers and keep pointer to 64K memory passed in
18 void reset( void* mem_64k );
19
20 // Run until specified time is reached. Returns true if suspicious/unsupported
21 // instruction was encountered at any point during run.
22 bool run( cpu_time_t end_time );
23
24 // Time of beginning of next instruction
time()25 cpu_time_t time() const { return state->time + state->base; }
26
27 // Alter current time. Not supported during run() call.
set_time(cpu_time_t t)28 void set_time( cpu_time_t t ) { state->time = t - state->base; }
adjust_time(int delta)29 void adjust_time( int delta ) { state->time += delta; }
30
31 #if BLARGG_BIG_ENDIAN
32 struct regs_t { uint8_t b, c, d, e, h, l, flags, a; };
33 #else
34 struct regs_t { uint8_t c, b, e, d, l, h, a, flags; };
35 #endif
36 BOOST_STATIC_ASSERT( sizeof (regs_t) == 8 );
37
38 struct pairs_t { uint16_t bc, de, hl, fa; };
39
40 // Registers are not updated until run() returns
41 struct registers_t {
42 uint16_t pc;
43 uint16_t sp;
44 uint16_t ix;
45 uint16_t iy;
46 union {
47 regs_t b; // b.b, b.c, b.d, b.e, b.h, b.l, b.flags, b.a
48 pairs_t w; // w.bc, w.de, w.hl. w.fa
49 };
50 union {
51 regs_t b;
52 pairs_t w;
53 } alt;
54 uint8_t iff1;
55 uint8_t iff2;
56 uint8_t r;
57 uint8_t i;
58 uint8_t im;
59 };
60 //registers_t r; (below for efficiency)
61
62 // can read this far past end of memory
63 enum { cpu_padding = 0x100 };
64
65 public:
66 Ay_Cpu();
67 private:
68 uint8_t szpc [0x200];
69 uint8_t* mem;
70 cpu_time_t end_time_;
71 struct state_t {
72 cpu_time_t base;
73 cpu_time_t time;
74 };
75 state_t* state; // points to state_ or a local copy within run()
76 state_t state_;
77 void set_end_time( cpu_time_t t );
78 public:
79 registers_t r;
80 };
81
set_end_time(cpu_time_t t)82 inline void Ay_Cpu::set_end_time( cpu_time_t t )
83 {
84 cpu_time_t delta = state->base - t;
85 state->base = t;
86 state->time += delta;
87 }
88
89 #endif
90