OSDN Git Service

67c945ac055d258f2d3447364b22857bfae9658f
[csp-qt/common_source_project-fm7.git] / source / src / vm / pcengine / pcengine.cpp
1 /*
2         NEC-HE PC Engine Emulator 'ePCEngine'
3
4         Author : Takeda.Toshiya
5         Date   : 2012.10.31-
6
7         [ virtual machine ]
8 */
9
10 #include "pcengine.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../huc6280.h"
16
17 #ifdef USE_DEBUGGER
18 #include "../debugger.h"
19 #endif
20
21 #include "pce.h"
22
23 // ----------------------------------------------------------------------------
24 // initialize
25 // ----------------------------------------------------------------------------
26
27 VM::VM(EMU* parent_emu) : emu(parent_emu)
28 {
29         // create devices
30         first_device = last_device = NULL;
31         dummy = new DEVICE(this, emu);  // must be 1st device
32         
33         pceevent = new EVENT(this, emu);
34 //      pceevent->set_frames_per_sec(FRAMES_PER_SEC);
35 //      pceevent->set_lines_per_frame(LINES_PER_FRAME);
36         
37         pcecpu = new HUC6280(this, emu);
38 //      pcecpu->set_context_event_manager(pceevent);
39         pce = new PCE(this, emu);
40 //      pce->set_context_event_manager(pceevent);
41         
42         pceevent->set_context_cpu(pcecpu, CPU_CLOCKS);
43         pceevent->set_context_sound(pce);
44         
45         pcecpu->set_context_mem(pce);
46         pcecpu->set_context_io(pce);
47 #ifdef USE_DEBUGGER
48         pcecpu->set_context_debugger(new DEBUGGER(this, emu));
49 #endif
50         pce->set_context_cpu(pcecpu);
51         
52         // initialize all devices
53         for(DEVICE* device = first_device; device; device = device->next_device) {
54                 device->initialize();
55         }
56 }
57
58 VM::~VM()
59 {
60         // delete all devices
61         for(DEVICE* device = first_device; device;) {
62                 DEVICE *next_device = device->next_device;
63                 device->release();
64                 delete device;
65                 device = next_device;
66         }
67 }
68
69 DEVICE* VM::get_device(int id)
70 {
71         for(DEVICE* device = first_device; device; device = device->next_device) {
72                 if(device->this_device_id == id) {
73                         return device;
74                 }
75         }
76         return NULL;
77 }
78
79 // ----------------------------------------------------------------------------
80 // drive virtual machine
81 // ----------------------------------------------------------------------------
82
83 void VM::reset()
84 {
85         // reset all devices
86         for(DEVICE* device = first_device; device; device = device->next_device) {
87                 device->reset();
88         }
89 }
90
91 void VM::run()
92 {
93         pceevent->drive();
94 }
95
96 double VM::frame_rate()
97 {
98         return pceevent->frame_rate();
99 }
100
101 // ----------------------------------------------------------------------------
102 // debugger
103 // ----------------------------------------------------------------------------
104
105 #ifdef USE_DEBUGGER
106 DEVICE *VM::get_cpu(int index)
107 {
108         if(index == 0) {
109                 return pcecpu;
110         }
111         return NULL;
112 }
113 #endif
114
115 // ----------------------------------------------------------------------------
116 // draw screen
117 // ----------------------------------------------------------------------------
118
119 void VM::draw_screen()
120 {
121         pce->draw_screen();
122 }
123
124 // ----------------------------------------------------------------------------
125 // soud manager
126 // ----------------------------------------------------------------------------
127
128 void VM::initialize_sound(int rate, int samples)
129 {
130         // init sound manager
131         pceevent->initialize_sound(rate, samples);
132         
133         // init sound gen
134         pce->initialize_sound(rate);
135 }
136
137 uint16* VM::create_sound(int* extra_frames)
138 {
139         return pceevent->create_sound(extra_frames);
140 }
141
142 int VM::sound_buffer_ptr()
143 {
144         return pceevent->sound_buffer_ptr();
145 }
146
147 // ----------------------------------------------------------------------------
148 // user interface
149 // ----------------------------------------------------------------------------
150
151 void VM::open_cart(int drv, const _TCHAR* file_path)
152 {
153         if(drv == 0) {
154                 pce->open_cart(file_path);
155                 pce->reset();
156                 pcecpu->reset();
157         }
158 }
159
160 void VM::close_cart(int drv)
161 {
162         if(drv == 0) {
163                 pce->close_cart();
164                 pce->reset();
165                 pcecpu->reset();
166         }
167 }
168
169 bool VM::cart_inserted(int drv)
170 {
171         if(drv == 0) {
172                 return pce->cart_inserted();
173         } else {
174                 return false;
175         }
176 }
177
178 void VM::update_config()
179 {
180         for(DEVICE* device = first_device; device; device = device->next_device) {
181                 device->update_config();
182         }
183 }
184
185 #define STATE_VERSION   1
186
187 void VM::save_state(FILEIO* state_fio)
188 {
189         state_fio->FputUint32(STATE_VERSION);
190         
191         for(DEVICE* device = first_device; device; device = device->next_device) {
192                 device->save_state(state_fio);
193         }
194 }
195
196 bool VM::load_state(FILEIO* state_fio)
197 {
198         if(state_fio->FgetUint32() != STATE_VERSION) {
199                 return false;
200         }
201         for(DEVICE* device = first_device; device; device = device->next_device) {
202                 if(!device->load_state(state_fio)) {
203                         return false;
204                 }
205         }
206         return true;
207 }
208