OSDN Git Service

24de4ba4cdf55f25fcf0408ea2b3b701c1a8b28e
[csp-qt/common_source_project-fm7.git] / source / src / vm / pyuta / pyuta.cpp
1 /*
2         TOMY PyuTa Emulator 'ePyuTa'
3
4         Author : Takeda.Toshiya
5         Date   : 2007.07.15 -
6
7         [ virtual machine ]
8 */
9
10 #include "pyuta.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../noise.h"
17 #include "../sn76489an.h"
18 #include "../tms9918a.h"
19 #include "../tms9995.h"
20
21 #ifdef USE_DEBUGGER
22 #include "../debugger.h"
23 #endif
24
25 #include "memory.h"
26
27 // ----------------------------------------------------------------------------
28 // initialize
29 // ----------------------------------------------------------------------------
30
31 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
32 {
33         // create devices
34         first_device = last_device = NULL;
35         dummy = new DEVICE(this, emu);  // must be 1st device
36         event = new EVENT(this, emu);   // must be 2nd device
37         dummy->set_device_name(_T("1st Dummy"));
38         
39         drec = new DATAREC(this, emu);
40         drec->set_context_noise_play(new NOISE(this, emu));
41         drec->set_context_noise_stop(new NOISE(this, emu));
42         drec->set_context_noise_fast(new NOISE(this, emu));
43         psg = new SN76489AN(this, emu);
44         vdp = new TMS9918A(this, emu);
45         cpu = new TMS9995(this, emu);
46         
47         memory = new MEMORY(this, emu);
48         
49         // set contexts
50         event->set_context_cpu(cpu);
51         event->set_context_sound(psg);
52         event->set_context_sound(drec);
53         event->set_context_sound(drec->get_context_noise_play());
54         event->set_context_sound(drec->get_context_noise_stop());
55         event->set_context_sound(drec->get_context_noise_fast());
56         
57         drec->set_context_ear(memory, 0, 1);
58         memory->set_context_cmt(drec);
59         memory->set_context_cpu(cpu);
60         memory->set_context_psg(psg);
61         memory->set_context_vdp(vdp);
62         
63         // cpu bus
64         cpu->set_context_mem(memory);
65         cpu->set_context_io(memory);
66 #ifdef USE_DEBUGGER
67         cpu->set_context_debugger(new DEBUGGER(this, emu));
68 #endif
69         
70         // initialize all devices
71         for(DEVICE* device = first_device; device; device = device->next_device) {
72                 device->initialize();
73         }
74         decl_state();
75 }
76
77 VM::~VM()
78 {
79         // delete all devices
80         for(DEVICE* device = first_device; device;) {
81                 DEVICE *next_device = device->next_device;
82                 device->release();
83                 delete device;
84                 device = next_device;
85         }
86 }
87
88 DEVICE* VM::get_device(int id)
89 {
90         for(DEVICE* device = first_device; device; device = device->next_device) {
91                 if(device->this_device_id == id) {
92                         return device;
93                 }
94         }
95         return NULL;
96 }
97
98 // ----------------------------------------------------------------------------
99 // drive virtual machine
100 // ----------------------------------------------------------------------------
101
102 void VM::reset()
103 {
104         // reset all devices
105         for(DEVICE* device = first_device; device; device = device->next_device) {
106                 device->reset();
107         }
108 }
109
110 void VM::run()
111 {
112         event->drive();
113 }
114
115 // ----------------------------------------------------------------------------
116 // debugger
117 // ----------------------------------------------------------------------------
118
119 #ifdef USE_DEBUGGER
120 DEVICE *VM::get_cpu(int index)
121 {
122         if(index == 0) {
123                 return cpu;
124         }
125         return NULL;
126 }
127 #endif
128
129 // ----------------------------------------------------------------------------
130 // draw screen
131 // ----------------------------------------------------------------------------
132
133 void VM::draw_screen()
134 {
135         vdp->draw_screen();
136 }
137
138 // ----------------------------------------------------------------------------
139 // soud manager
140 // ----------------------------------------------------------------------------
141
142 void VM::initialize_sound(int rate, int samples)
143 {
144         // init sound manager
145         event->initialize_sound(rate, samples);
146         
147         // init sound gen
148         psg->initialize_sound(rate, 3579545, 8000);
149 }
150
151 uint16_t* VM::create_sound(int* extra_frames)
152 {
153         return event->create_sound(extra_frames);
154 }
155
156 int VM::get_sound_buffer_ptr()
157 {
158         return event->get_sound_buffer_ptr();
159 }
160
161 #ifdef USE_SOUND_VOLUME
162 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
163 {
164         if(ch == 0) {
165                 psg->set_volume(0, decibel_l, decibel_r);
166         } else if(ch == 1) {
167                 drec->set_volume(0, decibel_l, decibel_r);
168         } else if(ch == 2) {
169                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
170                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
171                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
172         }
173 }
174 #endif
175
176 // ----------------------------------------------------------------------------
177 // user interface
178 // ----------------------------------------------------------------------------
179
180 void VM::open_cart(int drv, const _TCHAR* file_path)
181 {
182         if(drv == 0) {
183                 memory->open_cart(file_path);
184                 reset();
185         }
186 }
187
188 void VM::close_cart(int drv)
189 {
190         if(drv == 0) {
191                 memory->close_cart();
192                 reset();
193         }
194 }
195
196 bool VM::is_cart_inserted(int drv)
197 {
198         if(drv == 0) {
199                 return memory->is_cart_inserted();
200         } else {
201                 return false;
202         }
203 }
204
205 void VM::play_tape(int drv, const _TCHAR* file_path)
206 {
207         drec->play_tape(file_path);
208 //      drec->set_remote(true);
209 }
210
211 void VM::rec_tape(int drv, const _TCHAR* file_path)
212 {
213         drec->rec_tape(file_path);
214 //      drec->set_remote(true);
215 }
216
217 void VM::close_tape(int drv)
218 {
219         emu->lock_vm();
220         drec->close_tape();
221         emu->unlock_vm();
222 //      drec->set_remote(false);
223 }
224
225 bool VM::is_tape_inserted(int drv)
226 {
227         return drec->is_tape_inserted();
228 }
229
230 bool VM::is_tape_playing(int drv)
231 {
232         return drec->is_tape_playing();
233 }
234
235 bool VM::is_tape_recording(int drv)
236 {
237         return drec->is_tape_recording();
238 }
239
240 int VM::get_tape_position(int drv)
241 {
242         return drec->get_tape_position();
243 }
244
245 const _TCHAR* VM::get_tape_message(int drv)
246 {
247         return drec->get_message();
248 }
249
250 void VM::push_play(int drv)
251 {
252         drec->set_ff_rew(0);
253         drec->set_remote(true);
254 }
255
256 void VM::push_stop(int drv)
257 {
258         drec->set_remote(false);
259 }
260
261 void VM::push_fast_forward(int drv)
262 {
263         drec->set_ff_rew(1);
264         drec->set_remote(true);
265 }
266
267 void VM::push_fast_rewind(int drv)
268 {
269         drec->set_ff_rew(-1);
270         drec->set_remote(true);
271 }
272
273 bool VM::is_frame_skippable()
274 {
275         return event->is_frame_skippable();
276 }
277
278 void VM::update_config()
279 {
280         for(DEVICE* device = first_device; device; device = device->next_device) {
281                 device->update_config();
282         }
283 }
284
285 #define STATE_VERSION   4
286
287 #include "../../statesub.h"
288 #include "../../qt/gui/csp_logger.h"
289 extern CSP_Logger DLL_PREFIX_I *csp_logger;
290
291 void VM::decl_state(void)
292 {
293         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PYUTA_HEAD")), csp_logger);
294
295         for(DEVICE* device = first_device; device; device = device->next_device) {
296                 device->decl_state();
297         }
298 }
299
300 void VM::save_state(FILEIO* state_fio)
301 {
302         //state_fio->FputUint32(STATE_VERSION);
303         
304         if(state_entry != NULL) {
305                 state_entry->save_state(state_fio);
306         }
307         for(DEVICE* device = first_device; device; device = device->next_device) {
308                 device->save_state(state_fio);
309         }
310 }
311
312 bool VM::load_state(FILEIO* state_fio)
313 {
314         //if(state_fio->FgetUint32() != STATE_VERSION) {
315         //      return false;
316         //}
317         
318         bool mb = false;
319         if(state_entry != NULL) {
320                 mb = state_entry->load_state(state_fio);
321         }
322         if(!mb) {
323                 emu->out_debug_log("INFO: HEADER DATA ERROR");
324                 return false;
325         }
326         for(DEVICE* device = first_device; device; device = device->next_device) {
327                 if(!device->load_state(state_fio)) {
328                         return false;
329                 }
330         }
331         return true;
332 }
333