OSDN Git Service

08a36a793100fd3a82886146a0faa1d255ebef37
[csp-qt/common_source_project-fm7.git] / source / src / vm / msx / msx_ex.cpp
1 /*
2         Common Source Code Project
3         MSX Series (experimental)
4
5         Origin : src/vm/msx/msx.cpp
6
7         modified by umaiboux
8         Date   : 2016.03.xx-
9
10         [ virtual machine ]
11 */
12
13 //#define USE_PORT_F4
14
15 #include "msx_ex.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(LDC_SLOT)
24 #include "../ld700.h"
25 #endif
26 #include "../noise.h"
27 #include "../not.h"
28 //#include "../ym2203.h"
29 #include "../ay_3_891x.h"
30 #include "../pcm1bit.h"
31 #if !defined(_MSX1_VARIANTS)
32 #include "../rp5c01.h"
33 #if defined(_MSX_VDP_MESS)
34 #include "../v9938.h"
35 #else
36 #include "../v99x8.h"
37 #endif
38 #else
39 #include "../tms9918a.h"
40 #endif
41 #include "../z80.h"
42
43 #ifdef USE_DEBUGGER
44 #include "../debugger.h"
45 #endif
46
47 #include "joystick.h"
48 #include "keyboard.h"
49 #include "memory_ex.h"
50 #if !defined(_MSX1_VARIANTS)
51 #include "rtcif.h"
52 #endif
53 #include "kanjirom.h"
54 #include "sound_cart.h"
55 #ifdef USE_PRINTER
56 #include "printer.h"
57 //#include "../pcpr201.h"
58 #include "../prnfile.h"
59 #if defined(_MZP)
60 // test
61 #include "../mz1p17.h"
62 #endif
63 #endif
64 #include "../ym2413.h"
65
66 #if defined(MSX_PSG_STEREO)
67 #include "psg_stereo.h"
68 #endif
69
70 #ifdef USE_PORT_F4
71 class PORT_F4 : public DEVICE
72 {
73 private:
74         uint8_t port;
75         
76 public:
77         PORT_F4(VM_TEMPLATE* parent_vm, EMU* parent_emu) : DEVICE(parent_vm, parent_emu) {
78                 port = 0;
79         }
80         ~PORT_F4() {}
81         
82         // common functions
83         void write_io8(uint32_t addr, uint32_t data) {
84                 port = data & 0xFF;
85         }
86         uint32_t read_io8(uint32_t addr) {
87                 return port&0xFF;
88         }
89         void reset() {
90                 port = 0;
91         }
92 };
93 #endif
94
95 // ----------------------------------------------------------------------------
96 // initialize
97 // ----------------------------------------------------------------------------
98
99 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
100 {
101         // create devices
102         first_device = last_device = NULL;
103         dummy = new DEVICE(this, emu);  // must be 1st device
104         event = new EVENT(this, emu);   // must be 2nd device
105         
106         drec = new DATAREC(this, emu);
107         drec->set_context_noise_play(new NOISE(this, emu));
108         drec->set_context_noise_stop(new NOISE(this, emu));
109         drec->set_context_noise_fast(new NOISE(this, emu));
110         pio = new I8255(this, emu);
111         io = new IO(this, emu);
112 #if defined(LDC_SLOT)
113         ldp = new LD700(this, emu);
114 #endif
115         not_remote = new NOT(this, emu);
116 //      psg = new YM2203(this, emu);
117         psg = new AY_3_891X(this, emu);
118         pcm = new PCM1BIT(this, emu);
119 #if !defined(_MSX1_VARIANTS)
120         rtc = new RP5C01(this, emu);
121         vdp = new V99X8(this, emu);
122 #else
123         vdp = new TMS9918A(this, emu);
124 #endif
125         cpu = new Z80(this, emu);
126         
127         joystick = new JOYSTICK(this, emu);
128         keyboard = new KEYBOARD(this, emu);
129         memory = new MEMORY_EX(this, emu);
130 #if !defined(_MSX1_VARIANTS)
131         rtcif = new RTCIF(this, emu);
132 #endif
133 //      slot0 = new SLOT0(this, emu);   // #0: main memory
134 //      slot1 = new SLOT1(this, emu);   // #1: rom-cartridge or msx-dos
135 //      slot2 = new SLOT2(this, emu);   // #2: fdd-cartridge or p-basic
136 //      slot3 = new SLOT3(this, emu);   // #3: rom-cartridge or ram-cartridge
137         slot_mainrom = new SLOT_MAINROM(this, emu);
138         slot_cart[0] = new SLOT_CART(this, emu);
139         slot_cart[1] = new SLOT_CART(this, emu);
140         sound_cart[0] = new SOUND_CART(this, emu);
141         sound_cart[1] = new SOUND_CART(this, emu);
142         kanjirom = new KANJIROM(this, emu);
143 #ifdef USE_PRINTER
144         printer = new PRINTER(this, emu);
145 #endif
146 #if defined(LDC_SLOT)
147         slot_ldc = new SLOT_LDC(this, emu);
148 #endif
149 #if defined(MAPPERRAM_SLOT)
150         slot_ram = new SLOT_MAPPERRAM(this, emu);
151 #endif
152 #if defined(RAM64K_SLOT)
153         slot_ram = new SLOT_RAM64K(this, emu);
154 #endif
155 #if defined(SUBROM_SLOT)
156         slot_subrom = new SLOT_SUBROM(this, emu);
157 #endif
158 #if defined(FIRMWARE32K1_SLOT)
159         slot_firmware32k1 = new SLOT_FIRMWARE32K(this, emu);
160 #endif
161 #if defined(FIRMWARE32K2_SLOT)
162         slot_firmware32k2 = new SLOT_FIRMWARE32K(this, emu);
163 #endif
164 #if defined(FDD_PATCH_SLOT)
165         slot_fdd_patch = new SLOT_FDD_PATCH(this, emu);
166 #endif
167 #if defined(MSXDOS2_SLOT)
168         slot_msxdos2 = new SLOT_MSXDOS2(this, emu);
169 #endif
170         ym2413 = new YM2413(this, emu);
171 #if defined(MSXMUSIC_SLOT)
172         slot_msxmusic = new SLOT_MSXMUSIC(this, emu);
173 #endif
174 #if defined(MSX_PSG_STEREO)
175         psg_stereo = new PSG_STEREO(this, emu);
176 #endif
177         
178         // set contexts
179         event->set_context_cpu(cpu);
180 #if defined(MSX_PSG_STEREO)
181         event->set_context_sound(psg_stereo);
182 #else
183         event->set_context_sound(psg);
184 #endif
185         event->set_context_sound(pcm);
186         event->set_context_sound(drec);
187 #if defined(LDC_SLOT)
188         event->set_context_sound(ldp);
189 #endif
190         event->set_context_sound(ym2413);
191         event->set_context_sound(sound_cart[0]);
192         event->set_context_sound(sound_cart[1]);
193         event->set_context_sound(drec->get_context_noise_play());
194         event->set_context_sound(drec->get_context_noise_stop());
195         event->set_context_sound(drec->get_context_noise_fast());
196         slot_cart[0]->set_context_sound(sound_cart[0]);
197         slot_cart[1]->set_context_sound(sound_cart[1]);
198         
199         drec->set_context_ear(psg, SIG_AY_3_891X_PORT_A, 0x80);
200         pio->set_context_port_a(memory, SIG_MEMORY_SEL, 0xff, 0);
201         pio->set_context_port_c(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);
202         pio->set_context_port_c(not_remote, SIG_NOT_INPUT, 0x10, 0);
203         not_remote->set_context_out(drec, SIG_DATAREC_REMOTE, 1);
204         pio->set_context_port_c(drec, SIG_DATAREC_MIC, 0x20, 0);
205         pio->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x80, 0);
206         psg->set_context_port_b(joystick, SIG_JOYSTICK_SEL, 0x40, 0);
207         vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
208 #if defined(LDC_SLOT)
209         pio->set_context_port_c(slot_ldc, SIG_LDC_MUTE, 0x10, 0);
210         ldp->set_context_exv(slot_ldc, SIG_LDC_EXV, 1);
211         ldp->set_context_ack(slot_ldc, SIG_LDC_ACK, 1);
212         ldp->set_context_sound(psg, SIG_AY_3_891X_PORT_A, 0x80);
213 #endif
214         
215         joystick->set_context_psg(psg);
216 //      keyboard->set_context_cpu(cpu);
217         keyboard->set_context_pio(pio);
218 //      memory->set_context_slot(0, slot0);
219 //      memory->set_context_slot(1, slot1);
220 //      memory->set_context_slot(2, slot2);
221 //      memory->set_context_slot(3, slot3);
222         memory->set_context_slot_dummy(dummy);
223         memory->set_context_slot(MAINROM_SLOT, slot_mainrom);
224         memory->set_context_slot(CART1_SLOT, slot_cart[0]);
225 #if defined(CART2_SLOT)
226         memory->set_context_slot(CART2_SLOT, slot_cart[1]);
227 #endif
228 #if defined(LDC_SLOT)
229         memory->set_context_slot(LDC_SLOT, slot_ldc);
230 #endif
231 #if defined(MAPPERRAM_SLOT)
232         memory->set_context_slot(MAPPERRAM_SLOT, slot_ram);
233         memory->set_context_mapper(slot_ram);
234 #endif
235 #if defined(RAM64K_SLOT)
236         memory->set_context_slot(RAM64K_SLOT, slot_ram);
237 #endif
238 #if defined(SUBROM_SLOT)
239         memory->set_context_slot(SUBROM_SLOT, slot_subrom);
240 #endif
241 #if defined(FIRMWARE32K1_SLOT)
242         slot_firmware32k1->set_context_filename(FIRMWARE32K1_FILENAME);
243         memory->set_context_slot(FIRMWARE32K1_SLOT, slot_firmware32k1);
244 #endif
245 #if defined(FIRMWARE32K2_SLOT)
246         slot_firmware32k2->set_context_filename(FIRMWARE32K2_FILENAME);
247         memory->set_context_slot(FIRMWARE32K2_SLOT, slot_firmware32k2);
248 #endif
249 #if defined(FDD_PATCH_SLOT)
250         memory->set_context_slot(FDD_PATCH_SLOT, slot_fdd_patch);
251         memory->set_context_fdd_patch(slot_fdd_patch);
252 #endif
253 #if defined(MSXDOS2_SLOT)
254         memory->set_context_slot(MSXDOS2_SLOT, slot_msxdos2);
255 #endif
256 #if defined(MSXMUSIC_SLOT)
257         memory->set_context_slot(MSXMUSIC_SLOT, slot_msxmusic);
258 #endif
259 #if defined(_MSX_VDP_MESS) && defined(FDD_PATCH_SLOT)
260         memory->set_context_vdp(vdp);
261 #endif
262
263 #if !defined(_MSX1_VARIANTS)
264         rtcif->set_context_rtc(rtc);
265 #endif
266 #if defined(LDC_SLOT)
267         slot_ldc->set_context_cpu(cpu);
268         slot_ldc->set_context_ldp(ldp);
269         slot_ldc->set_context_vdp(vdp);
270 #endif
271         
272 #ifdef USE_PRINTER
273         if(config.printer_type == 0) {  
274                 printer->set_context_prn(new PRNFILE(this, emu));
275 #if defined(_MZP)
276         } else if(config.printer_type == 1) {
277                 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
278                 mz1p17->mode = MZ1P17_MODE_X1;
279                 printer->set_context_prn(mz1p17);
280         } else if(config.printer_type == 2) {
281                 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
282                 mz1p17->mode = MZ1P17_MODE_MZ80P4;
283                 printer->set_context_prn(mz1p17);
284 #else
285 //      } else if(config.printer_type == 1) {
286 //              HXP560 *hxp560 = new HXP560(this, emu);
287 //              printer->set_context_prn(hxp560);
288 //      } else if(config.printer_type == 2) {
289 //              printer->set_context_prn(new PCPR201(this, emu));
290 #endif
291         } else {
292 //              printer->set_context_prn(dummy);
293                 printer->set_context_prn(printer);
294         }
295 #endif
296
297         // cpu bus
298         cpu->set_context_mem(memory);
299         cpu->set_context_io(io);
300         cpu->set_context_intr(dummy);
301 #if defined(FDD_PATCH_SLOT)
302         cpu->set_context_bios(memory);
303 #endif
304 #ifdef USE_DEBUGGER
305         cpu->set_context_debugger(new DEBUGGER(this, emu));
306 #endif
307         
308         // i/o bus
309 #if !defined(_MSX1_VARIANTS)
310         io->set_iomap_range_rw(0xb4, 0xb5, rtcif);
311         io->set_iomap_range_rw(0x98, 0x9b, vdp);
312 #else
313         io->set_iomap_range_rw(0x98, 0x99, vdp);
314 #endif
315         io->set_iomap_range_rw(0xa8, 0xab, pio);
316 #if defined(MSX_PSG_STEREO)
317         psg_stereo->set_context_psg(psg);
318         io->set_iomap_alias_w(0xa0, psg_stereo, 0);     // PSG ch
319         io->set_iomap_alias_w(0xa1, psg_stereo, 1);     // PSG data
320         io->set_iomap_alias_r(0xa2, psg_stereo, 1);     // PSG data
321 #else
322         io->set_iomap_alias_w(0xa0, psg, 0);    // PSG ch
323         io->set_iomap_alias_w(0xa1, psg, 1);    // PSG data
324         io->set_iomap_alias_r(0xa2, psg, 1);    // PSG data
325 #endif
326         io->set_iomap_range_rw(0xfc, 0xff, memory);
327         io->set_iomap_range_rw(0xd8, 0xdb, kanjirom);
328 #ifdef USE_PRINTER
329         io->set_iomap_range_rw(0x90, 0x91, printer);
330 #endif
331         io->set_iomap_range_rw(0x7c, 0x7d, ym2413);
332         
333 #ifdef USE_PORT_F4
334         static PORT_F4 *port_f4;
335         port_f4 = new PORT_F4(this, emu);
336         io->set_iomap_range_rw(0xf4, 0xf4, port_f4);
337 #endif
338
339         // initialize all devices
340         for(DEVICE* device = first_device; device; device = device->next_device) {
341                 device->initialize();
342         }
343         decl_state();
344 }
345
346 VM::~VM()
347 {
348         // delete all devices
349         for(DEVICE* device = first_device; device;) {
350                 DEVICE *next_device = device->next_device;
351                 device->release();
352                 delete device;
353                 device = next_device;
354         }
355 }
356
357 DEVICE* VM::get_device(int id)
358 {
359         for(DEVICE* device = first_device; device; device = device->next_device) {
360                 if(device->this_device_id == id) {
361                         return device;
362                 }
363         }
364         return NULL;
365 }
366
367 // ----------------------------------------------------------------------------
368 // drive virtual machine
369 // ----------------------------------------------------------------------------
370
371 void VM::reset()
372 {
373         // reset all devices
374         for(DEVICE* device = first_device; device; device = device->next_device) {
375                 device->reset();
376         }
377 }
378
379 void VM::run()
380 {
381         event->drive();
382 }
383
384 // ----------------------------------------------------------------------------
385 // debugger
386 // ----------------------------------------------------------------------------
387
388 #ifdef USE_DEBUGGER
389 DEVICE *VM::get_cpu(int index)
390 {
391         if(index == 0) {
392                 return cpu;
393         }
394         return NULL;
395 }
396 #endif
397
398 // ----------------------------------------------------------------------------
399 // draw screen
400 // ----------------------------------------------------------------------------
401
402 void VM::draw_screen()
403 {
404         vdp->draw_screen();
405 }
406
407 // ----------------------------------------------------------------------------
408 // soud manager
409 // ----------------------------------------------------------------------------
410
411 void VM::initialize_sound(int rate, int samples)
412 {
413         // init sound manager
414         event->initialize_sound(rate, samples);
415         
416         // init sound gen
417 #if defined(MSX_PSG_STEREO)
418         psg_stereo->initialize_sound(rate, 3579545, samples, 0, 0);
419 #else
420         psg->initialize_sound(rate, 3579545, samples, 0, 0);
421 #endif
422         pcm->initialize_sound(rate, 8000);
423 #if defined(LDC_SLOT)
424         ldp->initialize_sound(rate, samples);
425 #endif
426         ym2413->initialize_sound(rate, 3579545, samples);
427         sound_cart[0]->initialize_sound(rate, 3579545, samples);
428         sound_cart[1]->initialize_sound(rate, 3579545, samples);
429 }
430
431 uint16_t* VM::create_sound(int* extra_frames)
432 {
433         return event->create_sound(extra_frames);
434 }
435
436 int VM::get_sound_buffer_ptr()
437 {
438         return event->get_sound_buffer_ptr();
439 }
440
441 #if defined(LDC_SLOT)
442 void VM::movie_sound_callback(uint8_t *buffer, long size)
443 {
444         ldp->movie_sound_callback(buffer, size);
445 }
446 #endif
447
448 #ifdef USE_SOUND_VOLUME
449 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
450 {
451         if(ch == 0) {
452 #if defined(MSX_PSG_STEREO)
453         psg_stereo->set_volume(1, decibel_l, decibel_r);
454 #else
455         psg->set_volume(1, decibel_l, decibel_r);
456 #endif
457         } else if(ch == 1) {
458                 pcm->set_volume(0, decibel_l, decibel_r);
459         } else if(ch == 2) {
460                 drec->set_volume(0, decibel_l, decibel_r);
461 #if defined(_PX7)
462         } else if(ch-- == 3) {
463                 ldp->set_volume(0, decibel_l, decibel_r);
464 #endif
465         } else if(ch == 3) {
466                 sound_cart[0]->set_volume(0, decibel_l, decibel_r);
467         } else if(ch == 4) {
468                 sound_cart[1]->set_volume(0, decibel_l, decibel_r);
469         } else if(ch == 5) {
470                 ym2413->set_volume(0, decibel_l, decibel_r);
471         } else if(ch == 6) {
472                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
473                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
474                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
475         }
476 }
477 #endif
478
479 // ----------------------------------------------------------------------------
480 // user interface
481 // ----------------------------------------------------------------------------
482
483 void VM::open_cart(int drv, const _TCHAR* file_path)
484 {
485         if(drv < 2) {
486                 slot_cart[drv]->open_cart(file_path);
487         }
488         reset();
489 }
490
491 void VM::close_cart(int drv)
492 {
493         if(drv < 2) {
494                 slot_cart[drv]->close_cart();
495         }
496         reset();
497 }
498
499 bool VM::is_cart_inserted(int drv)
500 {
501         if(drv < 2) {
502                 return slot_cart[drv]->is_cart_inserted();
503         } else {
504                 return false;
505         }
506 }
507
508 void VM::play_tape(int drv, const _TCHAR* file_path)
509 {
510         drec->play_tape(file_path);
511 //      drec->set_remote(true);
512 }
513
514 void VM::rec_tape(int drv, const _TCHAR* file_path)
515 {
516         drec->rec_tape(file_path);
517 //      drec->set_remote(true);
518 }
519
520 void VM::close_tape(int drv)
521 {
522         emu->lock_vm();
523         drec->close_tape();
524         emu->unlock_vm();
525 //      drec->set_remote(false);
526 }
527
528 bool VM::is_tape_inserted(int drv)
529 {
530         return drec->is_tape_inserted();
531 }
532
533 bool VM::is_tape_playing(int drv)
534 {
535         return drec->is_tape_playing();
536 }
537
538 bool VM::is_tape_recording(int drv)
539 {
540         return drec->is_tape_recording();
541 }
542
543 int VM::get_tape_position(int drv)
544 {
545         return drec->get_tape_position();
546 }
547
548 const _TCHAR* VM::get_tape_message(int drv)
549 {
550         return drec->get_message();
551 }
552
553 void VM::push_play(int drv)
554 {
555         drec->set_ff_rew(0);
556         drec->set_remote(true);
557 }
558
559 void VM::push_stop(int drv)
560 {
561         drec->set_remote(false);
562 }
563
564 void VM::push_fast_forward(int drv)
565 {
566         drec->set_ff_rew(1);
567         drec->set_remote(true);
568 }
569
570 void VM::push_fast_rewind(int drv)
571 {
572         drec->set_ff_rew(-1);
573         drec->set_remote(true);
574 }
575
576 #if defined(LDC_SLOT)
577 void VM::open_laser_disc(int drv, const _TCHAR* file_path)
578 {
579         ldp->open_disc(file_path);
580 }
581
582 void VM::close_laser_disc(int drv)
583 {
584         ldp->close_disc();
585 }
586
587 bool VM::is_laser_disc_inserted(int drv)
588 {
589         return ldp->is_disc_inserted();
590 }
591
592 uint32_t VM::is_laser_disc_accessed()
593 {
594         return ldp->read_signal(0);
595 }
596 #endif
597
598 #if defined(FDD_PATCH_SLOT)
599 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
600 {
601         memory->open_disk(drv, file_path, bank);
602 }
603
604 void VM::close_floppy_disk(int drv)
605 {
606         memory->close_disk(drv);
607 }
608
609 bool VM::is_floppy_disk_inserted(int drv)
610 {
611         return memory->is_disk_inserted(drv);
612 }
613
614 void VM::is_floppy_disk_protected(int drv, bool value)
615 {
616         memory->is_disk_protected(drv, value);
617 }
618
619 bool VM::is_floppy_disk_protected(int drv)
620 {
621         return memory->is_disk_protected(drv);
622 }
623
624 uint32_t VM::is_floppy_disk_accessed()
625 {
626         return memory->read_signal(0);
627 }
628 #endif
629
630 bool VM::is_frame_skippable()
631 {
632         return event->is_frame_skippable();
633 }
634
635 void VM::update_config()
636 {
637         for(DEVICE* device = first_device; device; device = device->next_device) {
638                 device->update_config();
639         }
640 }
641
642 #define STATE_VERSION   5
643
644 #include "../../statesub.h"
645 #include "../../qt/gui/csp_logger.h"
646 extern CSP_Logger DLL_PREFIX_I *csp_logger;
647
648 void VM::decl_state(void)
649 {
650         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MSX_SERIES_HEAD")), csp_logger);
651         for(DEVICE* device = first_device; device; device = device->next_device) {
652                 device->decl_state();
653         }
654 }
655
656 void VM::save_state(FILEIO* state_fio)
657 {
658         //state_fio->FputUint32(STATE_VERSION);
659         if(state_entry != NULL) {
660                 state_entry->save_state(state_fio);
661         }
662         for(DEVICE* device = first_device; device; device = device->next_device) {
663                 device->save_state(state_fio);
664         }
665 }
666
667 bool VM::load_state(FILEIO* state_fio)
668 {
669         //if(state_fio->FgetUint32() != STATE_VERSION) {
670         //      return false;
671         //}
672         bool mb = false;
673         if(state_entry != NULL) {
674                 mb = state_entry->load_state(state_fio);
675         }
676         if(!mb) {
677                 emu->out_debug_log("INFO: HEADER DATA ERROR");
678                 return false;
679         }
680         for(DEVICE* device = first_device; device; device = device->next_device) {
681                 if(!device->load_state(state_fio)) {
682                         return false;
683                 }
684         }
685         return true;
686 }
687