OSDN Git Service

[VM][SOUND] Fix wrong rendering period for PCM1BIT with some VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / msx / msx.cpp
1 /*
2         ASCII MSX1 Emulator 'yaMSX1'
3         ASCII MSX2 Emulator 'yaMSX2'
4         Pioneer PX-7 Emulator 'ePX-7'
5
6         Author : tanam
7         Date   : 2013.06.29-
8
9         modified by Takeda.Toshiya
10         modified by umaiboux
11
12         [ virtual machine ]
13 */
14
15 #include "msx.h"
16 #include "../../emu.h"
17 #include "../device.h"
18 #include "../event.h"
19
20 #include "../datarec.h"
21 #include "../i8255.h"
22 #include "../io.h"
23 #if defined(_PX7)
24 #include "../ld700.h"
25 #endif
26 #include "../not.h"
27 #include "../ym2203.h"
28 #include "../pcm1bit.h"
29 #if defined(_MSX2)
30 #include "../rp5c01.h"
31 #include "../v99x8.h"
32 #else
33 #include "../tms9918a.h"
34 #endif
35 #include "../z80.h"
36
37 #ifdef USE_DEBUGGER
38 #include "../debugger.h"
39 #endif
40
41 #include "joystick.h"
42 #include "keyboard.h"
43 #include "memory.h"
44 #if defined(_MSX2)
45 #include "rtcif.h"
46 #endif
47
48 // ----------------------------------------------------------------------------
49 // initialize
50 // ----------------------------------------------------------------------------
51
52 VM::VM(EMU* parent_emu) : emu(parent_emu)
53 {
54         // create devices
55         first_device = last_device = NULL;
56         dummy = new DEVICE(this, emu);  // must be 1st device
57         event = new EVENT(this, emu);   // must be 2nd device
58 #if defined(_USE_QT)
59         dummy->set_device_name(_T("1st Dummy"));
60         event->set_device_name(_T("EVENT"));
61 #endif  
62         
63         drec = new DATAREC(this, emu);
64         pio = new I8255(this, emu);
65         io = new IO(this, emu);
66 #if defined(_USE_QT)
67         pio->set_device_name(_T("i8255 PIO"));
68         io->set_device_name(_T("I/O BUS"));
69 #endif  
70 #if defined(_PX7)
71         ldp = new LD700(this, emu);
72 #endif
73         not_remote = new NOT(this, emu);
74         psg = new YM2203(this, emu);
75         pcm = new PCM1BIT(this, emu);
76 #if defined(_USE_QT)
77         not_remote->set_device_name(_T("NOT GATE(REMOTE)"));
78         psg->set_device_name(_T("AY-3-8910 PSG"));
79         pcm->set_device_name(_T("PCM BUZZER"));
80 #endif  
81 #if defined(_MSX2)
82         rtc = new RP5C01(this, emu);
83         vdp = new V99X8(this, emu);
84   #if defined(_USE_QT)
85         rtc->set_device_name(_T("RP5C01 RTC"));
86         vdp->set_device_name(_T("V99x8 VDP"));
87   #endif
88 #else
89         vdp = new TMS9918A(this, emu);
90   #if defined(_USE_QT)
91         vdp->set_device_name(_T("TMS9918 VDP"));
92   #endif        
93 #endif
94         cpu = new Z80(this, emu);
95 #if defined(_USE_QT)
96         cpu->set_device_name(_T("CPU(Z80)"));
97 #endif  
98         joystick = new JOYSTICK(this, emu);
99         keyboard = new KEYBOARD(this, emu);
100         memory = new MEMORY(this, emu);
101 #if defined(_MSX2)
102         rtcif = new RTCIF(this, emu);
103 #endif
104 #if defined(_USE_QT)
105         joystick->set_device_name(_T("JOYSTICK I/F"));
106         keyboard->set_device_name(_T("KEYBOARD I/F"));
107         memory->set_device_name(_T("MEMORY"));
108   #if defined(_MSX2)
109         rtcif->set_device_name(_T("RTC I/F"));
110   #endif
111 #endif
112         slot0 = new SLOT0(this, emu);   // #0: main memory
113         slot1 = new SLOT1(this, emu);   // #1: rom-cartridge or msx-dos
114         slot2 = new SLOT2(this, emu);   // #2: fdd-cartridge or p-basic
115         slot3 = new SLOT3(this, emu);   // #3: rom-cartridge or ram-cartridge
116 #if defined(_USE_QT)
117         slot0->set_device_name(_T("SLOT#0 MAIN MEMORY"));
118         slot1->set_device_name(_T("SLOT#1 ROM CARTRIDGE"));
119         slot2->set_device_name(_T("SLOT#2 FDD CARTRIDGE"));
120         slot3->set_device_name(_T("SLOT#3 ROM CARTRIDGE"));
121 #endif
122         // set contexts
123         event->set_context_cpu(cpu);
124         event->set_context_sound(psg);
125         event->set_context_sound(pcm);
126         event->set_context_sound(drec);
127 #if defined(_PX7)
128         event->set_context_sound(ldp);
129 #endif
130 #if defined(USE_SOUND_FILES)
131 #if defined(USE_FD1)
132         //if(memory->load_sound_data(MEMORY_SND_TYPE_DISK_SEEK, _T("FDDSEEK.WAV"))) {
133         //      event->set_context_sound(memory);
134         //}
135 #endif
136         //drec->load_sound_data(DATAREC_SNDFILE_EJECT, _T("CMTEJECT.WAV"));
137         drec->load_sound_data(DATAREC_SNDFILE_RELAY_ON, _T("RELAY_ON.WAV"));
138         drec->load_sound_data(DATAREC_SNDFILE_RELAY_OFF, _T("RELAYOFF.WAV"));
139 #endif
140         
141         drec->set_context_ear(psg, SIG_YM2203_PORT_A, 0x80);
142         pio->set_context_port_a(memory, SIG_MEMORY_SEL, 0xff, 0);
143         pio->set_context_port_c(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);
144         pio->set_context_port_c(not_remote, SIG_NOT_INPUT, 0x10, 0);
145         not_remote->set_context_out(drec, SIG_DATAREC_REMOTE, 1);
146         pio->set_context_port_c(drec, SIG_DATAREC_MIC, 0x20, 0);
147         pio->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x80, 0);
148         psg->set_context_port_b(joystick, SIG_JOYSTICK_SEL, 0x40, 0);
149         vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
150
151         // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
152         pcm->set_realtime_render(true);
153 #if defined(_PX7)
154         pio->set_context_port_c(slot2, SIG_SLOT2_MUTE, 0x10, 0);
155         ldp->set_context_exv(slot2, SIG_SLOT2_EXV, 1);
156         ldp->set_context_ack(slot2, SIG_SLOT2_ACK, 1);
157         ldp->set_context_sound(psg, SIG_YM2203_PORT_A, 0x80);
158 #endif
159         
160         joystick->set_context_psg(psg);
161 //      keyboard->set_context_cpu(cpu);
162         keyboard->set_context_pio(pio);
163         memory->set_context_slot(0, slot0);
164         memory->set_context_slot(1, slot1);
165         memory->set_context_slot(2, slot2);
166         memory->set_context_slot(3, slot3);
167 #if defined(_MSX2)
168         rtcif->set_context_rtc(rtc);
169 #endif
170 #if defined(_PX7)
171         slot2->set_context_cpu(cpu);
172         slot2->set_context_ldp(ldp);
173         slot2->set_context_vdp(vdp);
174 #endif
175         
176         // cpu bus
177         cpu->set_context_mem(memory);
178         cpu->set_context_io(io);
179         cpu->set_context_intr(dummy);
180 #if !defined(_PX7)
181         cpu->set_context_bios(memory);
182 #endif
183 #ifdef USE_DEBUGGER
184         cpu->set_context_debugger(new DEBUGGER(this, emu));
185 #endif
186         
187         // i/o bus
188 #ifdef _MSX2
189         io->set_iomap_range_rw(0xb4, 0xb5, rtcif);
190         io->set_iomap_range_rw(0x98, 0x9b, vdp);
191 #else
192         io->set_iomap_range_rw(0x98, 0x99, vdp);
193 #endif
194         io->set_iomap_range_rw(0xa8, 0xab, pio);
195         io->set_iomap_alias_w(0xa0, psg, 0);    // PSG ch
196         io->set_iomap_alias_w(0xa1, psg, 1);    // PSG data
197         io->set_iomap_alias_r(0xa2, psg, 1);    // PSG data
198         io->set_iomap_range_rw(0xfc, 0xff, memory);
199         
200         // initialize all devices
201         for(DEVICE* device = first_device; device; device = device->next_device) {
202                 device->initialize();
203         }
204 }
205
206 VM::~VM()
207 {
208         // delete all devices
209         for(DEVICE* device = first_device; device;) {
210                 DEVICE *next_device = device->next_device;
211                 device->release();
212                 delete device;
213                 device = next_device;
214         }
215 }
216
217 DEVICE* VM::get_device(int id)
218 {
219         for(DEVICE* device = first_device; device; device = device->next_device) {
220                 if(device->this_device_id == id) {
221                         return device;
222                 }
223         }
224         return NULL;
225 }
226
227 // ----------------------------------------------------------------------------
228 // drive virtual machine
229 // ----------------------------------------------------------------------------
230
231 void VM::reset()
232 {
233         // reset all devices
234         for(DEVICE* device = first_device; device; device = device->next_device) {
235                 device->reset();
236         }
237 }
238
239 void VM::run()
240 {
241         event->drive();
242 }
243
244 // ----------------------------------------------------------------------------
245 // debugger
246 // ----------------------------------------------------------------------------
247
248 #ifdef USE_DEBUGGER
249 DEVICE *VM::get_cpu(int index)
250 {
251         if(index == 0) {
252                 return cpu;
253         }
254         return NULL;
255 }
256 #endif
257
258 // ----------------------------------------------------------------------------
259 // draw screen
260 // ----------------------------------------------------------------------------
261
262 void VM::draw_screen()
263 {
264         vdp->draw_screen();
265 }
266
267 // ----------------------------------------------------------------------------
268 // soud manager
269 // ----------------------------------------------------------------------------
270
271 void VM::initialize_sound(int rate, int samples)
272 {
273         // init sound manager
274         event->initialize_sound(rate, samples);
275         
276         // init sound gen
277         psg->initialize_sound(rate, 3579545, samples, 0, 0);
278         pcm->initialize_sound(rate, 8000);
279 #if defined(_PX7)
280         ldp->initialize_sound(rate, samples);
281 #endif
282 }
283
284 uint16_t* VM::create_sound(int* extra_frames)
285 {
286         return event->create_sound(extra_frames);
287 }
288
289 int VM::get_sound_buffer_ptr()
290 {
291         return event->get_sound_buffer_ptr();
292 }
293
294 #if defined(_PX7)
295 void VM::movie_sound_callback(uint8_t *buffer, long size)
296 {
297         ldp->movie_sound_callback(buffer, size);
298 }
299 #endif
300
301 #ifdef USE_SOUND_VOLUME
302 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
303 {
304         if(ch == 0) {
305                 psg->set_volume(1, decibel_l, decibel_r);
306         } else if(ch == 1) {
307                 pcm->set_volume(0, decibel_l, decibel_r);
308         } else if(ch == 2) {
309                 drec->set_volume(0, decibel_l, decibel_r);
310 #if defined(_PX7)
311         } else if(ch == 3) {
312                 ldp->set_volume(0, decibel_l, decibel_r);
313 #endif
314 #if defined(USE_SOUND_FILES)
315 #if defined(USE_FD1)
316                 //} else if(ch == 3) {
317                 //memory->set_volume(MEMORY_SND_TYPE_FDD_SEEK, decibel_l, decibel_r);
318 #endif
319 #if defined(_PX7)
320         } else if(ch == 4) {
321                 drec->set_volume(2 + DATAREC_SNDFILE_RELAY_ON, decibel_l, decibel_r);
322                 drec->set_volume(2 + DATAREC_SNDFILE_RELAY_OFF, decibel_l, decibel_r);
323                 //drec->set_volume(2 + DATAREC_SNDFILE_EJECT, decibel_l, decibel_r);
324 #else
325         } else if(ch == 3) {
326                 drec->set_volume(2 + DATAREC_SNDFILE_RELAY_ON, decibel_l, decibel_r);
327                 drec->set_volume(2 + DATAREC_SNDFILE_RELAY_OFF, decibel_l, decibel_r);
328                 //drec->set_volume(2 + DATAREC_SNDFILE_EJECT, decibel_l, decibel_r);
329 #endif
330 #endif
331         }
332 }
333 #endif
334
335 // ----------------------------------------------------------------------------
336 // user interface
337 // ----------------------------------------------------------------------------
338
339 void VM::open_cart(int drv, const _TCHAR* file_path)
340 {
341         if(drv == 0) {
342                 slot1->open_cart(file_path);
343         } else {
344                 slot3->open_cart(file_path);
345         }
346         reset();
347 }
348
349 void VM::close_cart(int drv)
350 {
351         if(drv == 0) {
352                 slot1->close_cart();
353         } else {
354                 slot3->close_cart();
355         }
356         reset();
357 }
358
359 bool VM::is_cart_inserted(int drv)
360 {
361         if(drv == 0) {
362                 return slot1->is_cart_inserted();
363         } else {
364                 return slot3->is_cart_inserted();
365         }
366 }
367
368 void VM::play_tape(const _TCHAR* file_path)
369 {
370         drec->play_tape(file_path);
371 }
372
373 void VM::rec_tape(const _TCHAR* file_path)
374 {
375         drec->rec_tape(file_path);
376 }
377
378 void VM::close_tape()
379 {
380 #if defined(USE_SOUND_FILES)
381         //drec->write_signal(SIG_SOUNDER_ADD + DATAREC_SNDFILE_EJECT, 1, 1);
382 #endif
383         emu->lock_vm();
384         drec->close_tape();
385         emu->unlock_vm();
386 }
387
388 bool VM::is_tape_inserted()
389 {
390         return drec->is_tape_inserted();
391 }
392
393 bool VM::is_tape_playing()
394 {
395         return drec->is_tape_playing();
396 }
397
398 bool VM::is_tape_recording()
399 {
400         return drec->is_tape_recording();
401 }
402
403 int VM::get_tape_position()
404 {
405         return drec->get_tape_position();
406 }
407
408 #if defined(_PX7)
409 void VM::open_laser_disc(const _TCHAR* file_path)
410 {
411         ldp->open_disc(file_path);
412 }
413
414 void VM::close_laser_disc()
415 {
416         ldp->close_disc();
417 }
418
419 bool VM::is_laser_disc_inserted()
420 {
421         return ldp->is_disc_inserted();
422 }
423 #else
424 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
425 {
426         memory->open_disk(drv, file_path, bank);
427 }
428
429 void VM::close_floppy_disk(int drv)
430 {
431         memory->close_disk(drv);
432 }
433
434 bool VM::is_floppy_disk_inserted(int drv)
435 {
436         return memory->is_disk_inserted(drv);
437 }
438
439 void VM::is_floppy_disk_protected(int drv, bool value)
440 {
441         memory->is_disk_protected(drv, value);
442 }
443
444 bool VM::is_floppy_disk_protected(int drv)
445 {
446         return memory->is_disk_protected(drv);
447 }
448 #endif
449
450 bool VM::is_frame_skippable()
451 {
452         return event->is_frame_skippable();
453 }
454
455 void VM::update_config()
456 {
457         for(DEVICE* device = first_device; device; device = device->next_device) {
458                 device->update_config();
459         }
460 }
461
462 #define STATE_VERSION   1
463
464 void VM::save_state(FILEIO* state_fio)
465 {
466         state_fio->FputUint32(STATE_VERSION);
467         
468         for(DEVICE* device = first_device; device; device = device->next_device) {
469                 device->save_state(state_fio);
470         }
471 }
472
473 bool VM::load_state(FILEIO* state_fio)
474 {
475         if(state_fio->FgetUint32() != STATE_VERSION) {
476                 return false;
477         }
478         for(DEVICE* device = first_device; device; device = device->next_device) {
479                 if(!device->load_state(state_fio)) {
480                         return false;
481                 }
482         }
483         return true;
484 }
485