diff options
author | Sean Bartell <wingedtachikoma@gmail.com> | 2011-06-25 21:32:25 -0400 |
---|---|---|
committer | Nils Wallménius <nils@rockbox.org> | 2012-04-25 22:13:20 +0200 |
commit | f40bfc9267b13b54e6379dfe7539447662879d24 (patch) | |
tree | 9b20069d5e62809ff434061ad730096836f916f2 /lib/rbcodec/codecs/libgme/hes_cpu.h | |
parent | a0009907de7a0107d49040d8a180f140e2eff299 (diff) | |
download | rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.tar.gz rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.zip |
Add codecs to librbcodec.
Change-Id: Id7f4717d51ed02d67cb9f9cb3c0ada4a81843f97
Reviewed-on: http://gerrit.rockbox.org/137
Reviewed-by: Nils Wallménius <nils@rockbox.org>
Tested-by: Nils Wallménius <nils@rockbox.org>
Diffstat (limited to 'lib/rbcodec/codecs/libgme/hes_cpu.h')
-rw-r--r-- | lib/rbcodec/codecs/libgme/hes_cpu.h | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libgme/hes_cpu.h b/lib/rbcodec/codecs/libgme/hes_cpu.h new file mode 100644 index 0000000000..0429eeaba0 --- /dev/null +++ b/lib/rbcodec/codecs/libgme/hes_cpu.h | |||
@@ -0,0 +1,132 @@ | |||
1 | // PC Engine CPU emulator for use with HES music files | ||
2 | |||
3 | // Game_Music_Emu 0.6-pre | ||
4 | #ifndef HES_CPU_H | ||
5 | #define HES_CPU_H | ||
6 | |||
7 | #include "blargg_common.h" | ||
8 | #include "blargg_source.h" | ||
9 | |||
10 | typedef int hes_time_t; // clock cycle count | ||
11 | typedef int hes_addr_t; // 16-bit address | ||
12 | |||
13 | struct Hes_Emu; | ||
14 | |||
15 | enum { future_time = INT_MAX/2 + 1 }; | ||
16 | enum { page_bits = 13 }; | ||
17 | enum { page_size = 1 << page_bits }; | ||
18 | enum { page_count = 0x10000 / page_size }; | ||
19 | |||
20 | // Can read this many bytes past end of a page | ||
21 | enum { cpu_padding = 8 }; | ||
22 | enum { irq_inhibit_mask = 0x04 }; | ||
23 | enum { idle_addr = 0x1FFF }; | ||
24 | |||
25 | // Cpu state | ||
26 | struct cpu_state_t { | ||
27 | byte const* code_map [page_count + 1]; | ||
28 | hes_time_t base; | ||
29 | int time; | ||
30 | }; | ||
31 | |||
32 | // NOT kept updated during emulation. | ||
33 | struct registers_t { | ||
34 | uint16_t pc; | ||
35 | byte a; | ||
36 | byte x; | ||
37 | byte y; | ||
38 | byte flags; | ||
39 | byte sp; | ||
40 | }; | ||
41 | |||
42 | struct Hes_Cpu { | ||
43 | struct registers_t r; | ||
44 | |||
45 | hes_time_t irq_time_; | ||
46 | hes_time_t end_time_; | ||
47 | |||
48 | struct cpu_state_t* cpu_state; // points to state_ or a local copy within run() | ||
49 | struct cpu_state_t cpu_state_; | ||
50 | |||
51 | // page mapping registers | ||
52 | uint8_t mmr [page_count + 1]; | ||
53 | uint8_t ram [page_size]; | ||
54 | }; | ||
55 | |||
56 | // Init cpu state | ||
57 | static inline void Cpu_init( struct Hes_Cpu* this ) | ||
58 | { | ||
59 | this->cpu_state = &this->cpu_state_; | ||
60 | } | ||
61 | |||
62 | // Reset hes cpu | ||
63 | void Cpu_reset( struct Hes_Cpu* this ); | ||
64 | |||
65 | // Set end_time and run CPU from current time. Returns true if any illegal | ||
66 | // instructions were encountered. | ||
67 | bool Cpu_run( struct Hes_Emu* this, hes_time_t end_time ); | ||
68 | |||
69 | // Time of ning of next instruction to be executed | ||
70 | static inline hes_time_t Cpu_time( struct Hes_Cpu* this ) | ||
71 | { | ||
72 | return this->cpu_state->time + this->cpu_state->base; | ||
73 | } | ||
74 | |||
75 | static inline void Cpu_set_time( struct Hes_Cpu* this, hes_time_t t ) { this->cpu_state->time = t - this->cpu_state->base; } | ||
76 | static inline void Cpu_adjust_time( struct Hes_Cpu* this, int delta ) { this->cpu_state->time += delta; } | ||
77 | |||
78 | #define HES_CPU_PAGE( addr ) ((unsigned) (addr) >> page_bits) | ||
79 | |||
80 | #ifdef BLARGG_NONPORTABLE | ||
81 | #define HES_CPU_OFFSET( addr ) (addr) | ||
82 | #else | ||
83 | #define HES_CPU_OFFSET( addr ) ((addr) & (page_size - 1)) | ||
84 | #endif | ||
85 | |||
86 | static inline uint8_t const* Cpu_get_code( struct Hes_Cpu* this, hes_addr_t addr ) | ||
87 | { | ||
88 | return this->cpu_state_.code_map [HES_CPU_PAGE( addr )] + HES_CPU_OFFSET( addr ); | ||
89 | } | ||
90 | |||
91 | static inline void update_end_time( struct Hes_Cpu* this, hes_time_t end, hes_time_t irq ) | ||
92 | { | ||
93 | if ( end > irq && !(this->r.flags & irq_inhibit_mask) ) | ||
94 | end = irq; | ||
95 | |||
96 | this->cpu_state->time += this->cpu_state->base - end; | ||
97 | this->cpu_state->base = end; | ||
98 | } | ||
99 | |||
100 | static inline hes_time_t Cpu_end_time( struct Hes_Cpu* this ) { return this->end_time_; } | ||
101 | |||
102 | static inline void Cpu_set_irq_time( struct Hes_Cpu* this, hes_time_t t ) | ||
103 | { | ||
104 | this->irq_time_ = t; | ||
105 | update_end_time( this, this->end_time_, t ); | ||
106 | } | ||
107 | |||
108 | static inline void Cpu_set_end_time( struct Hes_Cpu* this, hes_time_t t ) | ||
109 | { | ||
110 | this->end_time_ = t; | ||
111 | update_end_time( this, t, this->irq_time_ ); | ||
112 | } | ||
113 | |||
114 | static inline void Cpu_end_frame( struct Hes_Cpu* this, hes_time_t t ) | ||
115 | { | ||
116 | assert( this->cpu_state == &this->cpu_state_ ); | ||
117 | this->cpu_state_.base -= t; | ||
118 | if ( this->irq_time_ < future_time ) this->irq_time_ -= t; | ||
119 | if ( this->end_time_ < future_time ) this->end_time_ -= t; | ||
120 | } | ||
121 | |||
122 | static inline void Cpu_set_mmr( struct Hes_Cpu* this, int reg, int bank, void const* code ) | ||
123 | { | ||
124 | assert( (unsigned) reg <= page_count ); // allow page past end to be set | ||
125 | assert( (unsigned) bank < 0x100 ); | ||
126 | this->mmr [reg] = bank; | ||
127 | byte const* p = STATIC_CAST(byte const*,code) - HES_CPU_OFFSET( reg << page_bits ); | ||
128 | this->cpu_state->code_map [reg] = p; | ||
129 | this->cpu_state_.code_map [reg] = p; | ||
130 | } | ||
131 | |||
132 | #endif | ||