comparison src/console/Hes_Cpu.h @ 316:fb513e10174e trunk

[svn] - merge libconsole-blargg into mainline libconsole: + obsoletes plugins-ugly:sapplug
author nenolod
date Thu, 30 Nov 2006 19:54:33 -0800
parents
children 986f098da058
comparison
equal deleted inserted replaced
315:2294f3a6f136 316:fb513e10174e
1 // PC Engine CPU emulator for use with HES music files
2
3 // Game_Music_Emu 0.5.1
4 #ifndef HES_CPU_H
5 #define HES_CPU_H
6
7 #include "blargg_common.h"
8
9 typedef blargg_long hes_time_t; // clock cycle count
10 typedef unsigned hes_addr_t; // 16-bit address
11 enum { future_hes_time = LONG_MAX / 2 + 1 };
12
13 class Hes_Cpu {
14 public:
15 typedef BOOST::uint8_t uint8_t;
16
17 void reset();
18
19 enum { page_size = 0x2000 };
20 enum { page_shift = 13 };
21 enum { page_count = 8 };
22 void set_mmr( int reg, int bank );
23
24 uint8_t const* get_code( hes_addr_t );
25
26 uint8_t ram [page_size];
27
28 // not kept updated during a call to run()
29 struct registers_t {
30 BOOST::uint16_t pc;
31 uint8_t a;
32 uint8_t x;
33 uint8_t y;
34 uint8_t status;
35 uint8_t sp;
36 };
37 registers_t r;
38
39 // page mapping registers
40 uint8_t mmr [page_count + 1];
41
42 // Set end_time and run CPU from current time. Returns true if any illegal
43 // instructions were encountered.
44 bool run( hes_time_t end_time );
45
46 // Time of beginning of next instruction to be executed
47 hes_time_t time() const { return state->time + state->base; }
48 void set_time( hes_time_t t ) { state->time = t - state->base; }
49 void adjust_time( int delta ) { state->time += delta; }
50
51 hes_time_t irq_time() const { return irq_time_; }
52 void set_irq_time( hes_time_t );
53
54 hes_time_t end_time() const { return end_time_; }
55 void set_end_time( hes_time_t );
56
57 void end_frame( hes_time_t );
58
59 // Attempt to execute instruction here results in CPU advancing time to
60 // lesser of irq_time() and end_time() (or end_time() if IRQs are
61 // disabled)
62 enum { idle_addr = 0x1FFF };
63
64 // Can read this many bytes past end of a page
65 enum { cpu_padding = 8 };
66
67 public:
68 Hes_Cpu() { state = &state_; }
69 enum { irq_inhibit = 0x04 };
70 private:
71 // noncopyable
72 Hes_Cpu( const Hes_Cpu& );
73 Hes_Cpu& operator = ( const Hes_Cpu& );
74
75 struct state_t {
76 uint8_t const* code_map [page_count + 1];
77 hes_time_t base;
78 blargg_long time;
79 };
80 state_t* state; // points to state_ or a local copy within run()
81 state_t state_;
82 hes_time_t irq_time_;
83 hes_time_t end_time_;
84
85 void set_code_page( int, void const* );
86 inline int update_end_time( hes_time_t end, hes_time_t irq );
87 };
88
89 inline BOOST::uint8_t const* Hes_Cpu::get_code( hes_addr_t addr )
90 {
91 return state->code_map [addr >> page_shift] + addr
92 #if !BLARGG_NONPORTABLE
93 % (unsigned) page_size
94 #endif
95 ;
96 }
97
98 inline int Hes_Cpu::update_end_time( hes_time_t t, hes_time_t irq )
99 {
100 if ( irq < t && !(r.status & irq_inhibit) ) t = irq;
101 int delta = state->base - t;
102 state->base = t;
103 return delta;
104 }
105
106 inline void Hes_Cpu::set_irq_time( hes_time_t t )
107 {
108 state->time += update_end_time( end_time_, (irq_time_ = t) );
109 }
110
111 inline void Hes_Cpu::set_end_time( hes_time_t t )
112 {
113 state->time += update_end_time( (end_time_ = t), irq_time_ );
114 }
115
116 inline void Hes_Cpu::end_frame( hes_time_t t )
117 {
118 assert( state == &state_ );
119 state_.base -= t;
120 if ( irq_time_ < future_hes_time ) irq_time_ -= t;
121 if ( end_time_ < future_hes_time ) end_time_ -= t;
122 }
123
124 #endif