OSDN Git Service

37f2a060ca158f20a7d3283f33f73e1027c42375
[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 #if defined(__GIT_REPO_VERSION)
72         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
73 #endif
74         for(DEVICE* device = first_device; device; device = device->next_device) {
75                 device->initialize();
76         }
77         decl_state();
78 }
79
80 VM::~VM()
81 {
82         // delete all devices
83         for(DEVICE* device = first_device; device;) {
84                 DEVICE *next_device = device->next_device;
85                 device->release();
86                 delete device;
87                 device = next_device;
88         }
89 }
90
91 DEVICE* VM::get_device(int id)
92 {
93         for(DEVICE* device = first_device; device; device = device->next_device) {
94                 if(device->this_device_id == id) {
95                         return device;
96                 }
97         }
98         return NULL;
99 }
100
101 // ----------------------------------------------------------------------------
102 // drive virtual machine
103 // ----------------------------------------------------------------------------
104
105 void VM::reset()
106 {
107         // reset all devices
108         for(DEVICE* device = first_device; device; device = device->next_device) {
109                 device->reset();
110         }
111 }
112
113 void VM::run()
114 {
115         event->drive();
116 }
117
118 // ----------------------------------------------------------------------------
119 // debugger
120 // ----------------------------------------------------------------------------
121
122 #ifdef USE_DEBUGGER
123 DEVICE *VM::get_cpu(int index)
124 {
125         if(index == 0) {
126                 return cpu;
127         }
128         return NULL;
129 }
130 #endif
131
132 // ----------------------------------------------------------------------------
133 // draw screen
134 // ----------------------------------------------------------------------------
135
136 void VM::draw_screen()
137 {
138         vdp->draw_screen();
139 }
140
141 // ----------------------------------------------------------------------------
142 // soud manager
143 // ----------------------------------------------------------------------------
144
145 void VM::initialize_sound(int rate, int samples)
146 {
147         // init sound manager
148         event->initialize_sound(rate, samples);
149         
150         // init sound gen
151         psg->initialize_sound(rate, 3579545, 8000);
152 }
153
154 uint16_t* VM::create_sound(int* extra_frames)
155 {
156         return event->create_sound(extra_frames);
157 }
158
159 int VM::get_sound_buffer_ptr()
160 {
161         return event->get_sound_buffer_ptr();
162 }
163
164 #ifdef USE_SOUND_VOLUME
165 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
166 {
167         if(ch == 0) {
168                 psg->set_volume(0, decibel_l, decibel_r);
169         } else if(ch == 1) {
170                 drec->set_volume(0, decibel_l, decibel_r);
171         } else if(ch == 2) {
172                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
173                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
174                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
175         }
176 }
177 #endif
178
179 // ----------------------------------------------------------------------------
180 // user interface
181 // ----------------------------------------------------------------------------
182
183 void VM::open_cart(int drv, const _TCHAR* file_path)
184 {
185         if(drv == 0) {
186                 memory->open_cart(file_path);
187                 reset();
188         }
189 }
190
191 void VM::close_cart(int drv)
192 {
193         if(drv == 0) {
194                 memory->close_cart();
195                 reset();
196         }
197 }
198
199 bool VM::is_cart_inserted(int drv)
200 {
201         if(drv == 0) {
202                 return memory->is_cart_inserted();
203         } else {
204                 return false;
205         }
206 }
207
208 void VM::play_tape(int drv, const _TCHAR* file_path)
209 {
210         drec->play_tape(file_path);
211 //      drec->set_remote(true);
212 }
213
214 void VM::rec_tape(int drv, const _TCHAR* file_path)
215 {
216         drec->rec_tape(file_path);
217 //      drec->set_remote(true);
218 }
219
220 void VM::close_tape(int drv)
221 {
222         emu->lock_vm();
223         drec->close_tape();
224         emu->unlock_vm();
225 //      drec->set_remote(false);
226 }
227
228 bool VM::is_tape_inserted(int drv)
229 {
230         return drec->is_tape_inserted();
231 }
232
233 bool VM::is_tape_playing(int drv)
234 {
235         return drec->is_tape_playing();
236 }
237
238 bool VM::is_tape_recording(int drv)
239 {
240         return drec->is_tape_recording();
241 }
242
243 int VM::get_tape_position(int drv)
244 {
245         return drec->get_tape_position();
246 }
247
248 const _TCHAR* VM::get_tape_message(int drv)
249 {
250         return drec->get_message();
251 }
252
253 void VM::push_play(int drv)
254 {
255         drec->set_ff_rew(0);
256         drec->set_remote(true);
257 }
258
259 void VM::push_stop(int drv)
260 {
261         drec->set_remote(false);
262 }
263
264 void VM::push_fast_forward(int drv)
265 {
266         drec->set_ff_rew(1);
267         drec->set_remote(true);
268 }
269
270 void VM::push_fast_rewind(int drv)
271 {
272         drec->set_ff_rew(-1);
273         drec->set_remote(true);
274 }
275
276 bool VM::is_frame_skippable()
277 {
278         return event->is_frame_skippable();
279 }
280
281 void VM::update_config()
282 {
283         for(DEVICE* device = first_device; device; device = device->next_device) {
284                 device->update_config();
285         }
286 }
287
288 #define STATE_VERSION   4
289
290 #include "../../statesub.h"
291 #include "../../qt/gui/csp_logger.h"
292 extern CSP_Logger DLL_PREFIX_I *csp_logger;
293
294 void VM::decl_state(void)
295 {
296         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PYUTA_HEAD")), csp_logger);
297
298         for(DEVICE* device = first_device; device; device = device->next_device) {
299                 device->decl_state();
300         }
301 }
302
303 void VM::save_state(FILEIO* state_fio)
304 {
305         //state_fio->FputUint32(STATE_VERSION);
306         
307         if(state_entry != NULL) {
308                 state_entry->save_state(state_fio);
309         }
310         for(DEVICE* device = first_device; device; device = device->next_device) {
311                 device->save_state(state_fio);
312         }
313 }
314
315 bool VM::load_state(FILEIO* state_fio)
316 {
317         //if(state_fio->FgetUint32() != STATE_VERSION) {
318         //      return false;
319         //}
320         
321         bool mb = false;
322         if(state_entry != NULL) {
323                 mb = state_entry->load_state(state_fio);
324         }
325         if(!mb) {
326                 emu->out_debug_log("INFO: HEADER DATA ERROR");
327                 return false;
328         }
329         for(DEVICE* device = first_device; device; device = device->next_device) {
330                 if(!device->load_state(state_fio)) {
331                         return false;
332                 }
333         }
334         return true;
335 }
336