2 Common Source Code Project
3 MSX Series (experimental)
5 Origin : src/vm/msx/msx.cpp
16 #include "../../emu.h"
17 #include "../device.h"
20 #include "../datarec.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)
39 #include "../tms9918a.h"
44 #include "../debugger.h"
49 #include "memory_ex.h"
50 #if !defined(_MSX1_VARIANTS)
54 #include "sound_cart.h"
57 //#include "../pcpr201.h"
58 #include "../prnfile.h"
61 #include "../mz1p17.h"
64 #include "../ym2413.h"
66 #if defined(MSX_PSG_STEREO)
67 #include "psg_stereo.h"
73 #if !defined(_MSX1_VARIANTS)
76 using MSX::SLOT_MAINROM;
78 using MSX::SOUND_CART;
80 #if defined(MSX_PSG_STEREO)
81 using MSX::PSG_STEREO;
89 #if defined(MAPPERRAM_SLOT)
90 using MSX::SLOT_MAPPERRAM;
92 #if defined(RAM64K_SLOT)
93 using MSX::SLOT_RAM64K;
95 #if defined(SUBROM_SLOT)
96 using MSX::SLOT_SUBROM;
98 #if defined(FIRMWARE32K1_SLOT)
99 using MSX::SLOT_FIRMWARE32K;
101 #if defined(FIRMWARE32K2_SLOT)
102 using MSX::SLOT_FIRMWARE32K;
104 #if defined(FDD_PATCH_SLOT)
105 using MSX::SLOT_FDD_PATCH;
107 #if defined(MSXDOS2_SLOT)
108 using MSX::SLOT_MSXDOS2;
110 #if defined(MSXMUSIC_SLOT)
111 using MSX::SLOT_MSXMUSIC;
116 class PORT_F4 : public DEVICE
122 PORT_F4(VM_TEMPLATE* parent_vm, EMU* parent_emu) : DEVICE(parent_vm, parent_emu) {
128 void write_io8(uint32_t addr, uint32_t data) {
131 uint32_t read_io8(uint32_t addr) {
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
147 first_device = last_device = NULL;
148 dummy = new DEVICE(this, emu); // must be 1st device
149 event = new EVENT(this, emu); // must be 2nd device
151 drec = new DATAREC(this, emu);
152 drec->set_context_noise_play(new NOISE(this, emu));
153 drec->set_context_noise_stop(new NOISE(this, emu));
154 drec->set_context_noise_fast(new NOISE(this, emu));
155 pio = new I8255(this, emu);
156 io = new IO(this, emu);
157 #if defined(LDC_SLOT)
158 ldp = new LD700(this, emu);
160 not_remote = new NOT(this, emu);
161 // psg = new YM2203(this, emu);
162 psg = new AY_3_891X(this, emu);
163 pcm = new PCM1BIT(this, emu);
164 #if !defined(_MSX1_VARIANTS)
165 rtc = new RP5C01(this, emu);
166 vdp = new V99X8(this, emu);
168 vdp = new TMS9918A(this, emu);
170 cpu = new Z80(this, emu);
172 joystick = new JOYSTICK(this, emu);
173 keyboard = new KEYBOARD(this, emu);
174 memory = new MEMORY_EX(this, emu);
175 #if !defined(_MSX1_VARIANTS)
176 rtcif = new RTCIF(this, emu);
178 // slot0 = new SLOT0(this, emu); // #0: main memory
179 // slot1 = new SLOT1(this, emu); // #1: rom-cartridge or msx-dos
180 // slot2 = new SLOT2(this, emu); // #2: fdd-cartridge or p-basic
181 // slot3 = new SLOT3(this, emu); // #3: rom-cartridge or ram-cartridge
182 slot_mainrom = new SLOT_MAINROM(this, emu);
183 slot_cart[0] = new SLOT_CART(this, emu);
184 slot_cart[1] = new SLOT_CART(this, emu);
185 sound_cart[0] = new SOUND_CART(this, emu);
186 sound_cart[1] = new SOUND_CART(this, emu);
187 kanjirom = new KANJIROM(this, emu);
189 printer = new PRINTER(this, emu);
191 #if defined(LDC_SLOT)
192 slot_ldc = new SLOT_LDC(this, emu);
194 #if defined(MAPPERRAM_SLOT)
195 slot_ram = new SLOT_MAPPERRAM(this, emu);
197 #if defined(RAM64K_SLOT)
198 slot_ram = new SLOT_RAM64K(this, emu);
200 #if defined(SUBROM_SLOT)
201 slot_subrom = new SLOT_SUBROM(this, emu);
203 #if defined(FIRMWARE32K1_SLOT)
204 slot_firmware32k1 = new SLOT_FIRMWARE32K(this, emu);
206 #if defined(FIRMWARE32K2_SLOT)
207 slot_firmware32k2 = new SLOT_FIRMWARE32K(this, emu);
209 #if defined(FDD_PATCH_SLOT)
210 slot_fdd_patch = new SLOT_FDD_PATCH(this, emu);
212 #if defined(MSXDOS2_SLOT)
213 slot_msxdos2 = new SLOT_MSXDOS2(this, emu);
215 ym2413 = new YM2413(this, emu);
216 #if defined(MSXMUSIC_SLOT)
217 slot_msxmusic = new SLOT_MSXMUSIC(this, emu);
219 #if defined(MSX_PSG_STEREO)
220 psg_stereo = new PSG_STEREO(this, emu);
224 event->set_context_cpu(cpu);
225 #if defined(MSX_PSG_STEREO)
226 event->set_context_sound(psg_stereo);
228 event->set_context_sound(psg);
230 event->set_context_sound(pcm);
231 event->set_context_sound(drec);
232 #if defined(LDC_SLOT)
233 event->set_context_sound(ldp);
235 event->set_context_sound(ym2413);
236 event->set_context_sound(sound_cart[0]);
237 event->set_context_sound(sound_cart[1]);
238 event->set_context_sound(drec->get_context_noise_play());
239 event->set_context_sound(drec->get_context_noise_stop());
240 event->set_context_sound(drec->get_context_noise_fast());
241 slot_cart[0]->set_context_sound(sound_cart[0]);
242 slot_cart[1]->set_context_sound(sound_cart[1]);
244 drec->set_context_ear(psg, SIG_AY_3_891X_PORT_A, 0x80);
245 pio->set_context_port_a(memory, SIG_MEMORY_SEL, 0xff, 0);
246 pio->set_context_port_c(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);
247 pio->set_context_port_c(not_remote, SIG_NOT_INPUT, 0x10, 0);
248 not_remote->set_context_out(drec, SIG_DATAREC_REMOTE, 1);
249 pio->set_context_port_c(drec, SIG_DATAREC_MIC, 0x20, 0);
250 pio->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x80, 0);
251 psg->set_context_port_b(joystick, SIG_JOYSTICK_SEL, 0x40, 0);
252 vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
253 #if defined(LDC_SLOT)
254 pio->set_context_port_c(slot_ldc, SIG_LDC_MUTE, 0x10, 0);
255 ldp->set_context_exv(slot_ldc, SIG_LDC_EXV, 1);
256 ldp->set_context_ack(slot_ldc, SIG_LDC_ACK, 1);
257 ldp->set_context_sound(psg, SIG_AY_3_891X_PORT_A, 0x80);
260 joystick->set_context_psg(psg);
261 // keyboard->set_context_cpu(cpu);
262 keyboard->set_context_pio(pio);
263 // memory->set_context_slot(0, slot0);
264 // memory->set_context_slot(1, slot1);
265 // memory->set_context_slot(2, slot2);
266 // memory->set_context_slot(3, slot3);
267 memory->set_context_slot_dummy(dummy);
268 memory->set_context_slot(MAINROM_SLOT, slot_mainrom);
269 memory->set_context_slot(CART1_SLOT, slot_cart[0]);
270 #if defined(CART2_SLOT)
271 memory->set_context_slot(CART2_SLOT, slot_cart[1]);
273 #if defined(LDC_SLOT)
274 memory->set_context_slot(LDC_SLOT, slot_ldc);
276 #if defined(MAPPERRAM_SLOT)
277 memory->set_context_slot(MAPPERRAM_SLOT, slot_ram);
278 memory->set_context_mapper(slot_ram);
280 #if defined(RAM64K_SLOT)
281 memory->set_context_slot(RAM64K_SLOT, slot_ram);
283 #if defined(SUBROM_SLOT)
284 memory->set_context_slot(SUBROM_SLOT, slot_subrom);
286 #if defined(FIRMWARE32K1_SLOT)
287 slot_firmware32k1->set_context_filename(FIRMWARE32K1_FILENAME);
288 memory->set_context_slot(FIRMWARE32K1_SLOT, slot_firmware32k1);
290 #if defined(FIRMWARE32K2_SLOT)
291 slot_firmware32k2->set_context_filename(FIRMWARE32K2_FILENAME);
292 memory->set_context_slot(FIRMWARE32K2_SLOT, slot_firmware32k2);
294 #if defined(FDD_PATCH_SLOT)
295 memory->set_context_slot(FDD_PATCH_SLOT, slot_fdd_patch);
296 memory->set_context_fdd_patch(slot_fdd_patch);
298 #if defined(MSXDOS2_SLOT)
299 memory->set_context_slot(MSXDOS2_SLOT, slot_msxdos2);
301 #if defined(MSXMUSIC_SLOT)
302 memory->set_context_slot(MSXMUSIC_SLOT, slot_msxmusic);
304 #if defined(_MSX_VDP_MESS) && defined(FDD_PATCH_SLOT)
305 memory->set_context_vdp(vdp);
308 #if !defined(_MSX1_VARIANTS)
309 rtcif->set_context_rtc(rtc);
311 #if defined(LDC_SLOT)
312 slot_ldc->set_context_cpu(cpu);
313 slot_ldc->set_context_ldp(ldp);
314 slot_ldc->set_context_vdp(vdp);
318 if(config.printer_type == 0) {
319 printer->set_context_prn(new PRNFILE(this, emu));
321 } else if(config.printer_type == 1) {
322 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
323 mz1p17->mode = MZ1P17_MODE_X1;
324 printer->set_context_prn(mz1p17);
325 } else if(config.printer_type == 2) {
326 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
327 mz1p17->mode = MZ1P17_MODE_MZ80P4;
328 printer->set_context_prn(mz1p17);
330 // } else if(config.printer_type == 1) {
331 // HXP560 *hxp560 = new HXP560(this, emu);
332 // printer->set_context_prn(hxp560);
333 // } else if(config.printer_type == 2) {
334 // printer->set_context_prn(new PCPR201(this, emu));
337 // printer->set_context_prn(dummy);
338 printer->set_context_prn(printer);
343 cpu->set_context_mem(memory);
344 cpu->set_context_io(io);
345 cpu->set_context_intr(dummy);
346 #if defined(FDD_PATCH_SLOT)
347 cpu->set_context_bios(memory);
350 cpu->set_context_debugger(new DEBUGGER(this, emu));
354 #if !defined(_MSX1_VARIANTS)
355 io->set_iomap_range_rw(0xb4, 0xb5, rtcif);
356 io->set_iomap_range_rw(0x98, 0x9b, vdp);
358 io->set_iomap_range_rw(0x98, 0x99, vdp);
360 io->set_iomap_range_rw(0xa8, 0xab, pio);
361 #if defined(MSX_PSG_STEREO)
362 psg_stereo->set_context_psg(psg);
363 io->set_iomap_alias_w(0xa0, psg_stereo, 0); // PSG ch
364 io->set_iomap_alias_w(0xa1, psg_stereo, 1); // PSG data
365 io->set_iomap_alias_r(0xa2, psg_stereo, 1); // PSG data
367 io->set_iomap_alias_w(0xa0, psg, 0); // PSG ch
368 io->set_iomap_alias_w(0xa1, psg, 1); // PSG data
369 io->set_iomap_alias_r(0xa2, psg, 1); // PSG data
371 io->set_iomap_range_rw(0xfc, 0xff, memory);
372 io->set_iomap_range_rw(0xd8, 0xdb, kanjirom);
374 io->set_iomap_range_rw(0x90, 0x91, printer);
376 io->set_iomap_range_rw(0x7c, 0x7d, ym2413);
379 static PORT_F4 *port_f4;
380 port_f4 = new PORT_F4(this, emu);
381 io->set_iomap_range_rw(0xf4, 0xf4, port_f4);
384 // initialize all devices
385 #if defined(__GIT_REPO_VERSION)
386 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
388 for(DEVICE* device = first_device; device; device = device->next_device) {
389 device->initialize();
395 // delete all devices
396 for(DEVICE* device = first_device; device;) {
397 DEVICE *next_device = device->next_device;
400 device = next_device;
404 DEVICE* VM::get_device(int id)
406 for(DEVICE* device = first_device; device; device = device->next_device) {
407 if(device->this_device_id == id) {
414 // ----------------------------------------------------------------------------
415 // drive virtual machine
416 // ----------------------------------------------------------------------------
421 for(DEVICE* device = first_device; device; device = device->next_device) {
431 // ----------------------------------------------------------------------------
433 // ----------------------------------------------------------------------------
436 DEVICE *VM::get_cpu(int index)
445 // ----------------------------------------------------------------------------
447 // ----------------------------------------------------------------------------
449 void VM::draw_screen()
454 // ----------------------------------------------------------------------------
456 // ----------------------------------------------------------------------------
458 void VM::initialize_sound(int rate, int samples)
460 // init sound manager
461 event->initialize_sound(rate, samples);
464 #if defined(MSX_PSG_STEREO)
465 psg_stereo->initialize_sound(rate, 3579545, samples, 0, 0);
467 psg->initialize_sound(rate, 3579545, samples, 0, 0);
469 pcm->initialize_sound(rate, 8000);
470 #if defined(LDC_SLOT)
471 ldp->initialize_sound(rate, samples);
473 ym2413->initialize_sound(rate, 3579545, samples);
474 sound_cart[0]->initialize_sound(rate, 3579545, samples);
475 sound_cart[1]->initialize_sound(rate, 3579545, samples);
478 uint16_t* VM::create_sound(int* extra_frames)
480 return event->create_sound(extra_frames);
483 int VM::get_sound_buffer_ptr()
485 return event->get_sound_buffer_ptr();
488 #if defined(LDC_SLOT)
489 void VM::movie_sound_callback(uint8_t *buffer, long size)
491 ldp->movie_sound_callback(buffer, size);
495 #ifdef USE_SOUND_VOLUME
496 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
499 #if defined(MSX_PSG_STEREO)
500 psg_stereo->set_volume(1, decibel_l, decibel_r);
502 psg->set_volume(1, decibel_l, decibel_r);
505 pcm->set_volume(0, decibel_l, decibel_r);
507 drec->set_volume(0, decibel_l, decibel_r);
509 } else if(ch-- == 3) {
510 ldp->set_volume(0, decibel_l, decibel_r);
513 sound_cart[0]->set_volume(0, decibel_l, decibel_r);
515 sound_cart[1]->set_volume(0, decibel_l, decibel_r);
517 ym2413->set_volume(0, decibel_l, decibel_r);
519 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
520 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
521 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
526 // ----------------------------------------------------------------------------
528 // ----------------------------------------------------------------------------
530 void VM::open_cart(int drv, const _TCHAR* file_path)
533 slot_cart[drv]->open_cart(file_path);
538 void VM::close_cart(int drv)
541 slot_cart[drv]->close_cart();
546 bool VM::is_cart_inserted(int drv)
549 return slot_cart[drv]->is_cart_inserted();
555 void VM::play_tape(int drv, const _TCHAR* file_path)
557 drec->play_tape(file_path);
558 // drec->set_remote(true);
561 void VM::rec_tape(int drv, const _TCHAR* file_path)
563 drec->rec_tape(file_path);
564 // drec->set_remote(true);
567 void VM::close_tape(int drv)
572 // drec->set_remote(false);
575 bool VM::is_tape_inserted(int drv)
577 return drec->is_tape_inserted();
580 bool VM::is_tape_playing(int drv)
582 return drec->is_tape_playing();
585 bool VM::is_tape_recording(int drv)
587 return drec->is_tape_recording();
590 int VM::get_tape_position(int drv)
592 return drec->get_tape_position();
595 const _TCHAR* VM::get_tape_message(int drv)
597 return drec->get_message();
600 void VM::push_play(int drv)
603 drec->set_remote(true);
606 void VM::push_stop(int drv)
608 drec->set_remote(false);
611 void VM::push_fast_forward(int drv)
614 drec->set_remote(true);
617 void VM::push_fast_rewind(int drv)
619 drec->set_ff_rew(-1);
620 drec->set_remote(true);
623 #if defined(LDC_SLOT)
624 void VM::open_laser_disc(int drv, const _TCHAR* file_path)
626 ldp->open_disc(file_path);
629 void VM::close_laser_disc(int drv)
634 bool VM::is_laser_disc_inserted(int drv)
636 return ldp->is_disc_inserted();
639 uint32_t VM::is_laser_disc_accessed()
641 return ldp->read_signal(0);
645 #if defined(FDD_PATCH_SLOT)
646 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
648 memory->open_disk(drv, file_path, bank);
651 void VM::close_floppy_disk(int drv)
653 memory->close_disk(drv);
656 bool VM::is_floppy_disk_inserted(int drv)
658 return memory->is_disk_inserted(drv);
661 void VM::is_floppy_disk_protected(int drv, bool value)
663 memory->is_disk_protected(drv, value);
666 bool VM::is_floppy_disk_protected(int drv)
668 return memory->is_disk_protected(drv);
671 uint32_t VM::is_floppy_disk_accessed()
673 return memory->read_signal(0);
677 bool VM::is_frame_skippable()
679 return event->is_frame_skippable();
682 void VM::update_config()
684 for(DEVICE* device = first_device; device; device = device->next_device) {
685 device->update_config();
689 #define STATE_VERSION 5
691 bool VM::process_state(FILEIO* state_fio, bool loading)
693 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
696 for(DEVICE* device = first_device; device; device = device->next_device) {
697 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
698 // const char *name = typeid(*device).name();
699 // But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
700 const char *name = device->get_device_name();
701 int len = strlen(name);
703 if(!state_fio->StateCheckInt32(len)) {
705 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
709 if(!state_fio->StateCheckBuffer(name, len, 1)) {
711 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
715 if(!device->process_state(state_fio, loading)) {
717 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
722 // Machine specified.