OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[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 #if defined(__GIT_REPO_VERSION)
341         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
342 #endif
343         for(DEVICE* device = first_device; device; device = device->next_device) {
344                 device->initialize();
345         }
346         decl_state();
347 }
348
349 VM::~VM()
350 {
351         // delete all devices
352         for(DEVICE* device = first_device; device;) {
353                 DEVICE *next_device = device->next_device;
354                 device->release();
355                 delete device;
356                 device = next_device;
357         }
358 }
359
360 DEVICE* VM::get_device(int id)
361 {
362         for(DEVICE* device = first_device; device; device = device->next_device) {
363                 if(device->this_device_id == id) {
364                         return device;
365                 }
366         }
367         return NULL;
368 }
369
370 // ----------------------------------------------------------------------------
371 // drive virtual machine
372 // ----------------------------------------------------------------------------
373
374 void VM::reset()
375 {
376         // reset all devices
377         for(DEVICE* device = first_device; device; device = device->next_device) {
378                 device->reset();
379         }
380 }
381
382 void VM::run()
383 {
384         event->drive();
385 }
386
387 // ----------------------------------------------------------------------------
388 // debugger
389 // ----------------------------------------------------------------------------
390
391 #ifdef USE_DEBUGGER
392 DEVICE *VM::get_cpu(int index)
393 {
394         if(index == 0) {
395                 return cpu;
396         }
397         return NULL;
398 }
399 #endif
400
401 // ----------------------------------------------------------------------------
402 // draw screen
403 // ----------------------------------------------------------------------------
404
405 void VM::draw_screen()
406 {
407         vdp->draw_screen();
408 }
409
410 // ----------------------------------------------------------------------------
411 // soud manager
412 // ----------------------------------------------------------------------------
413
414 void VM::initialize_sound(int rate, int samples)
415 {
416         // init sound manager
417         event->initialize_sound(rate, samples);
418         
419         // init sound gen
420 #if defined(MSX_PSG_STEREO)
421         psg_stereo->initialize_sound(rate, 3579545, samples, 0, 0);
422 #else
423         psg->initialize_sound(rate, 3579545, samples, 0, 0);
424 #endif
425         pcm->initialize_sound(rate, 8000);
426 #if defined(LDC_SLOT)
427         ldp->initialize_sound(rate, samples);
428 #endif
429         ym2413->initialize_sound(rate, 3579545, samples);
430         sound_cart[0]->initialize_sound(rate, 3579545, samples);
431         sound_cart[1]->initialize_sound(rate, 3579545, samples);
432 }
433
434 uint16_t* VM::create_sound(int* extra_frames)
435 {
436         return event->create_sound(extra_frames);
437 }
438
439 int VM::get_sound_buffer_ptr()
440 {
441         return event->get_sound_buffer_ptr();
442 }
443
444 #if defined(LDC_SLOT)
445 void VM::movie_sound_callback(uint8_t *buffer, long size)
446 {
447         ldp->movie_sound_callback(buffer, size);
448 }
449 #endif
450
451 #ifdef USE_SOUND_VOLUME
452 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
453 {
454         if(ch == 0) {
455 #if defined(MSX_PSG_STEREO)
456         psg_stereo->set_volume(1, decibel_l, decibel_r);
457 #else
458         psg->set_volume(1, decibel_l, decibel_r);
459 #endif
460         } else if(ch == 1) {
461                 pcm->set_volume(0, decibel_l, decibel_r);
462         } else if(ch == 2) {
463                 drec->set_volume(0, decibel_l, decibel_r);
464 #if defined(_PX7)
465         } else if(ch-- == 3) {
466                 ldp->set_volume(0, decibel_l, decibel_r);
467 #endif
468         } else if(ch == 3) {
469                 sound_cart[0]->set_volume(0, decibel_l, decibel_r);
470         } else if(ch == 4) {
471                 sound_cart[1]->set_volume(0, decibel_l, decibel_r);
472         } else if(ch == 5) {
473                 ym2413->set_volume(0, decibel_l, decibel_r);
474         } else if(ch == 6) {
475                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
476                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
477                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
478         }
479 }
480 #endif
481
482 // ----------------------------------------------------------------------------
483 // user interface
484 // ----------------------------------------------------------------------------
485
486 void VM::open_cart(int drv, const _TCHAR* file_path)
487 {
488         if(drv < 2) {
489                 slot_cart[drv]->open_cart(file_path);
490         }
491         reset();
492 }
493
494 void VM::close_cart(int drv)
495 {
496         if(drv < 2) {
497                 slot_cart[drv]->close_cart();
498         }
499         reset();
500 }
501
502 bool VM::is_cart_inserted(int drv)
503 {
504         if(drv < 2) {
505                 return slot_cart[drv]->is_cart_inserted();
506         } else {
507                 return false;
508         }
509 }
510
511 void VM::play_tape(int drv, const _TCHAR* file_path)
512 {
513         drec->play_tape(file_path);
514 //      drec->set_remote(true);
515 }
516
517 void VM::rec_tape(int drv, const _TCHAR* file_path)
518 {
519         drec->rec_tape(file_path);
520 //      drec->set_remote(true);
521 }
522
523 void VM::close_tape(int drv)
524 {
525         emu->lock_vm();
526         drec->close_tape();
527         emu->unlock_vm();
528 //      drec->set_remote(false);
529 }
530
531 bool VM::is_tape_inserted(int drv)
532 {
533         return drec->is_tape_inserted();
534 }
535
536 bool VM::is_tape_playing(int drv)
537 {
538         return drec->is_tape_playing();
539 }
540
541 bool VM::is_tape_recording(int drv)
542 {
543         return drec->is_tape_recording();
544 }
545
546 int VM::get_tape_position(int drv)
547 {
548         return drec->get_tape_position();
549 }
550
551 const _TCHAR* VM::get_tape_message(int drv)
552 {
553         return drec->get_message();
554 }
555
556 void VM::push_play(int drv)
557 {
558         drec->set_ff_rew(0);
559         drec->set_remote(true);
560 }
561
562 void VM::push_stop(int drv)
563 {
564         drec->set_remote(false);
565 }
566
567 void VM::push_fast_forward(int drv)
568 {
569         drec->set_ff_rew(1);
570         drec->set_remote(true);
571 }
572
573 void VM::push_fast_rewind(int drv)
574 {
575         drec->set_ff_rew(-1);
576         drec->set_remote(true);
577 }
578
579 #if defined(LDC_SLOT)
580 void VM::open_laser_disc(int drv, const _TCHAR* file_path)
581 {
582         ldp->open_disc(file_path);
583 }
584
585 void VM::close_laser_disc(int drv)
586 {
587         ldp->close_disc();
588 }
589
590 bool VM::is_laser_disc_inserted(int drv)
591 {
592         return ldp->is_disc_inserted();
593 }
594
595 uint32_t VM::is_laser_disc_accessed()
596 {
597         return ldp->read_signal(0);
598 }
599 #endif
600
601 #if defined(FDD_PATCH_SLOT)
602 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
603 {
604         memory->open_disk(drv, file_path, bank);
605 }
606
607 void VM::close_floppy_disk(int drv)
608 {
609         memory->close_disk(drv);
610 }
611
612 bool VM::is_floppy_disk_inserted(int drv)
613 {
614         return memory->is_disk_inserted(drv);
615 }
616
617 void VM::is_floppy_disk_protected(int drv, bool value)
618 {
619         memory->is_disk_protected(drv, value);
620 }
621
622 bool VM::is_floppy_disk_protected(int drv)
623 {
624         return memory->is_disk_protected(drv);
625 }
626
627 uint32_t VM::is_floppy_disk_accessed()
628 {
629         return memory->read_signal(0);
630 }
631 #endif
632
633 bool VM::is_frame_skippable()
634 {
635         return event->is_frame_skippable();
636 }
637
638 void VM::update_config()
639 {
640         for(DEVICE* device = first_device; device; device = device->next_device) {
641                 device->update_config();
642         }
643 }
644
645 #define STATE_VERSION   5
646
647 #include "../../statesub.h"
648 #include "../../qt/gui/csp_logger.h"
649 extern CSP_Logger DLL_PREFIX_I *csp_logger;
650
651 void VM::decl_state(void)
652 {
653         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MSX_SERIES_HEAD")), csp_logger);
654         for(DEVICE* device = first_device; device; device = device->next_device) {
655                 device->decl_state();
656         }
657 }
658
659 void VM::save_state(FILEIO* state_fio)
660 {
661         //state_fio->FputUint32(STATE_VERSION);
662         if(state_entry != NULL) {
663                 state_entry->save_state(state_fio);
664         }
665         for(DEVICE* device = first_device; device; device = device->next_device) {
666                 device->save_state(state_fio);
667         }
668 }
669
670 bool VM::load_state(FILEIO* state_fio)
671 {
672         //if(state_fio->FgetUint32() != STATE_VERSION) {
673         //      return false;
674         //}
675         bool mb = false;
676         if(state_entry != NULL) {
677                 mb = state_entry->load_state(state_fio);
678         }
679         if(!mb) {
680                 emu->out_debug_log("INFO: HEADER DATA ERROR");
681                 return false;
682         }
683         for(DEVICE* device = first_device; device; device = device->next_device) {
684                 if(!device->load_state(state_fio)) {
685                         return false;
686                 }
687         }
688         return true;
689 }
690