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