summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libgme/hes_cpu.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libgme/hes_cpu.h')
-rw-r--r--lib/rbcodec/codecs/libgme/hes_cpu.h132
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
10typedef int hes_time_t; // clock cycle count
11typedef int hes_addr_t; // 16-bit address
12
13struct Hes_Emu;
14
15enum { future_time = INT_MAX/2 + 1 };
16enum { page_bits = 13 };
17enum { page_size = 1 << page_bits };
18enum { page_count = 0x10000 / page_size };
19
20// Can read this many bytes past end of a page
21enum { cpu_padding = 8 };
22enum { irq_inhibit_mask = 0x04 };
23enum { idle_addr = 0x1FFF };
24
25// Cpu state
26struct 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.
33struct registers_t {
34 uint16_t pc;
35 byte a;
36 byte x;
37 byte y;
38 byte flags;
39 byte sp;
40};
41
42struct 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
57static inline void Cpu_init( struct Hes_Cpu* this )
58{
59 this->cpu_state = &this->cpu_state_;
60}
61
62// Reset hes cpu
63void 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.
67bool Cpu_run( struct Hes_Emu* this, hes_time_t end_time );
68
69// Time of ning of next instruction to be executed
70static inline hes_time_t Cpu_time( struct Hes_Cpu* this )
71{
72 return this->cpu_state->time + this->cpu_state->base;
73}
74
75static inline void Cpu_set_time( struct Hes_Cpu* this, hes_time_t t ) { this->cpu_state->time = t - this->cpu_state->base; }
76static 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
86static 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
91static 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
100static inline hes_time_t Cpu_end_time( struct Hes_Cpu* this ) { return this->end_time_; }
101
102static 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
108static 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
114static 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
122static 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