diff options
Diffstat (limited to 'lib/rbcodec/codecs/libgme/z80_cpu.h')
-rw-r--r-- | lib/rbcodec/codecs/libgme/z80_cpu.h | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libgme/z80_cpu.h b/lib/rbcodec/codecs/libgme/z80_cpu.h new file mode 100644 index 0000000000..341119b6b1 --- /dev/null +++ b/lib/rbcodec/codecs/libgme/z80_cpu.h | |||
@@ -0,0 +1,116 @@ | |||
1 | // Z80 CPU emulator | ||
2 | |||
3 | // Game_Music_Emu 0.6-pre | ||
4 | #ifndef Z80_CPU_H | ||
5 | #define Z80_CPU_H | ||
6 | |||
7 | #include "blargg_source.h" | ||
8 | #include "blargg_endian.h" | ||
9 | |||
10 | typedef int cpu_time_t; | ||
11 | typedef int addr_t; | ||
12 | |||
13 | enum { page_bits = 10 }; | ||
14 | enum { page_size = 1 << page_bits }; | ||
15 | enum { page_count = 0x10000 / page_size }; | ||
16 | |||
17 | // Can read this far past end of memory | ||
18 | enum { cpu_padding = 0x100 }; | ||
19 | |||
20 | // Can read this many bytes past end of a page | ||
21 | enum { page_padding = 4 }; | ||
22 | |||
23 | #ifdef BLARGG_BIG_ENDIAN | ||
24 | struct regs_t { byte b,c, d,e, h,l, flags,a; }; | ||
25 | #else | ||
26 | struct regs_t { byte c,b, e,d, l,h, a,flags; }; | ||
27 | #endif | ||
28 | // BOOST_STATIC_ASSERT( sizeof (regs_t) == 8 ); | ||
29 | |||
30 | struct pairs_t { uint16_t bc, de, hl, fa; }; | ||
31 | |||
32 | // Registers are not updated until run() returns | ||
33 | struct registers_t { | ||
34 | uint16_t pc; | ||
35 | uint16_t sp; | ||
36 | uint16_t ix; | ||
37 | uint16_t iy; | ||
38 | union { | ||
39 | struct regs_t b; // b.b, b.c, b.d, b.e, b.h, b.l, b.flags, b.a | ||
40 | struct pairs_t w; // w.bc, w.de, w.hl. w.fa | ||
41 | }; | ||
42 | union { | ||
43 | struct regs_t b; | ||
44 | struct pairs_t w; | ||
45 | } alt; | ||
46 | byte iff1; | ||
47 | byte iff2; | ||
48 | byte r; | ||
49 | byte i; | ||
50 | byte im; | ||
51 | }; | ||
52 | |||
53 | struct cpu_state_t { | ||
54 | byte const* read [page_count + 1]; | ||
55 | byte * write [page_count + 1]; | ||
56 | cpu_time_t base; | ||
57 | cpu_time_t time; | ||
58 | }; | ||
59 | |||
60 | struct Z80_Cpu { | ||
61 | byte szpc [0x200]; | ||
62 | cpu_time_t end_time_; | ||
63 | |||
64 | struct cpu_state_t* cpu_state; // points to cpu_state_ or a local copy within run() | ||
65 | struct cpu_state_t cpu_state_; | ||
66 | |||
67 | struct registers_t r; | ||
68 | }; | ||
69 | |||
70 | void Z80_init( struct Z80_Cpu* this ); | ||
71 | |||
72 | // Clears registers and maps all pages to unmapped | ||
73 | void Z80_reset( struct Z80_Cpu* this, void* unmapped_write, void const* unmapped_read ); | ||
74 | |||
75 | // TODO: split mapping out of CPU | ||
76 | |||
77 | // Maps memory. Start and size must be multiple of page_size. | ||
78 | void Z80_map_mem( struct Z80_Cpu* this, addr_t addr, int size, void* write, void const* read ); | ||
79 | |||
80 | // Time of beginning of next instruction | ||
81 | static inline cpu_time_t Z80_time( struct Z80_Cpu* this ) { return this->cpu_state->time + this->cpu_state->base; } | ||
82 | |||
83 | // Alter current time | ||
84 | static inline void Z80_set_time( struct Z80_Cpu* this, cpu_time_t t ) { this->cpu_state->time = t - this->cpu_state->base; } | ||
85 | static inline void Z80_adjust_time( struct Z80_Cpu* this, int delta ) { this->cpu_state->time += delta; } | ||
86 | |||
87 | #ifdef BLARGG_NONPORTABLE | ||
88 | #define Z80_CPU_OFFSET( addr ) (addr) | ||
89 | #else | ||
90 | #define Z80_CPU_OFFSET( addr ) ((addr) & (page_size - 1)) | ||
91 | #endif | ||
92 | |||
93 | // Maps address to pointer to that byte | ||
94 | static inline byte* Z80_write( struct Z80_Cpu* this, addr_t addr ) | ||
95 | { | ||
96 | return this->cpu_state->write [(unsigned) addr >> page_bits] + Z80_CPU_OFFSET( addr ); | ||
97 | } | ||
98 | |||
99 | static inline byte const* Z80_read( struct Z80_Cpu* this, addr_t addr ) | ||
100 | { | ||
101 | return this->cpu_state->read [(unsigned) addr >> page_bits] + Z80_CPU_OFFSET( addr ); | ||
102 | } | ||
103 | |||
104 | static inline void Z80_map_mem_rw( struct Z80_Cpu* this, addr_t addr, int size, void* p ) | ||
105 | { | ||
106 | Z80_map_mem( this, addr, size, p, p ); | ||
107 | } | ||
108 | |||
109 | static inline void Z80_set_end_time( struct Z80_Cpu* this, cpu_time_t t ) | ||
110 | { | ||
111 | cpu_time_t delta = this->cpu_state->base - t; | ||
112 | this->cpu_state->base = t; | ||
113 | this->cpu_state->time += delta; | ||
114 | } | ||
115 | |||
116 | #endif | ||