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"
71 class PORT_F4 : public DEVICE
77 PORT_F4(VM_TEMPLATE* parent_vm, EMU* parent_emu) : DEVICE(parent_vm, parent_emu) {
83 void write_io8(uint32_t addr, uint32_t data) {
86 uint32_t read_io8(uint32_t addr) {
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
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
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);
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);
123 vdp = new TMS9918A(this, emu);
125 cpu = new Z80(this, emu);
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);
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);
144 printer = new PRINTER(this, emu);
146 #if defined(LDC_SLOT)
147 slot_ldc = new SLOT_LDC(this, emu);
149 #if defined(MAPPERRAM_SLOT)
150 slot_ram = new SLOT_MAPPERRAM(this, emu);
152 #if defined(RAM64K_SLOT)
153 slot_ram = new SLOT_RAM64K(this, emu);
155 #if defined(SUBROM_SLOT)
156 slot_subrom = new SLOT_SUBROM(this, emu);
158 #if defined(FIRMWARE32K1_SLOT)
159 slot_firmware32k1 = new SLOT_FIRMWARE32K(this, emu);
161 #if defined(FIRMWARE32K2_SLOT)
162 slot_firmware32k2 = new SLOT_FIRMWARE32K(this, emu);
164 #if defined(FDD_PATCH_SLOT)
165 slot_fdd_patch = new SLOT_FDD_PATCH(this, emu);
167 #if defined(MSXDOS2_SLOT)
168 slot_msxdos2 = new SLOT_MSXDOS2(this, emu);
170 ym2413 = new YM2413(this, emu);
171 #if defined(MSXMUSIC_SLOT)
172 slot_msxmusic = new SLOT_MSXMUSIC(this, emu);
174 #if defined(MSX_PSG_STEREO)
175 psg_stereo = new PSG_STEREO(this, emu);
179 event->set_context_cpu(cpu);
180 #if defined(MSX_PSG_STEREO)
181 event->set_context_sound(psg_stereo);
183 event->set_context_sound(psg);
185 event->set_context_sound(pcm);
186 event->set_context_sound(drec);
187 #if defined(LDC_SLOT)
188 event->set_context_sound(ldp);
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]);
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);
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]);
228 #if defined(LDC_SLOT)
229 memory->set_context_slot(LDC_SLOT, slot_ldc);
231 #if defined(MAPPERRAM_SLOT)
232 memory->set_context_slot(MAPPERRAM_SLOT, slot_ram);
233 memory->set_context_mapper(slot_ram);
235 #if defined(RAM64K_SLOT)
236 memory->set_context_slot(RAM64K_SLOT, slot_ram);
238 #if defined(SUBROM_SLOT)
239 memory->set_context_slot(SUBROM_SLOT, slot_subrom);
241 #if defined(FIRMWARE32K1_SLOT)
242 slot_firmware32k1->set_context_filename(FIRMWARE32K1_FILENAME);
243 memory->set_context_slot(FIRMWARE32K1_SLOT, slot_firmware32k1);
245 #if defined(FIRMWARE32K2_SLOT)
246 slot_firmware32k2->set_context_filename(FIRMWARE32K2_FILENAME);
247 memory->set_context_slot(FIRMWARE32K2_SLOT, slot_firmware32k2);
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);
253 #if defined(MSXDOS2_SLOT)
254 memory->set_context_slot(MSXDOS2_SLOT, slot_msxdos2);
256 #if defined(MSXMUSIC_SLOT)
257 memory->set_context_slot(MSXMUSIC_SLOT, slot_msxmusic);
259 #if defined(_MSX_VDP_MESS) && defined(FDD_PATCH_SLOT)
260 memory->set_context_vdp(vdp);
263 #if !defined(_MSX1_VARIANTS)
264 rtcif->set_context_rtc(rtc);
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);
273 if(config.printer_type == 0) {
274 printer->set_context_prn(new PRNFILE(this, emu));
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);
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));
292 // printer->set_context_prn(dummy);
293 printer->set_context_prn(printer);
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);
305 cpu->set_context_debugger(new DEBUGGER(this, emu));
309 #if !defined(_MSX1_VARIANTS)
310 io->set_iomap_range_rw(0xb4, 0xb5, rtcif);
311 io->set_iomap_range_rw(0x98, 0x9b, vdp);
313 io->set_iomap_range_rw(0x98, 0x99, vdp);
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
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
326 io->set_iomap_range_rw(0xfc, 0xff, memory);
327 io->set_iomap_range_rw(0xd8, 0xdb, kanjirom);
329 io->set_iomap_range_rw(0x90, 0x91, printer);
331 io->set_iomap_range_rw(0x7c, 0x7d, ym2413);
334 static PORT_F4 *port_f4;
335 port_f4 = new PORT_F4(this, emu);
336 io->set_iomap_range_rw(0xf4, 0xf4, port_f4);
339 // initialize all devices
340 #if defined(__GIT_REPO_VERSION)
341 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
343 for(DEVICE* device = first_device; device; device = device->next_device) {
344 device->initialize();
351 // delete all devices
352 for(DEVICE* device = first_device; device;) {
353 DEVICE *next_device = device->next_device;
356 device = next_device;
360 DEVICE* VM::get_device(int id)
362 for(DEVICE* device = first_device; device; device = device->next_device) {
363 if(device->this_device_id == id) {
370 // ----------------------------------------------------------------------------
371 // drive virtual machine
372 // ----------------------------------------------------------------------------
377 for(DEVICE* device = first_device; device; device = device->next_device) {
387 // ----------------------------------------------------------------------------
389 // ----------------------------------------------------------------------------
392 DEVICE *VM::get_cpu(int index)
401 // ----------------------------------------------------------------------------
403 // ----------------------------------------------------------------------------
405 void VM::draw_screen()
410 // ----------------------------------------------------------------------------
412 // ----------------------------------------------------------------------------
414 void VM::initialize_sound(int rate, int samples)
416 // init sound manager
417 event->initialize_sound(rate, samples);
420 #if defined(MSX_PSG_STEREO)
421 psg_stereo->initialize_sound(rate, 3579545, samples, 0, 0);
423 psg->initialize_sound(rate, 3579545, samples, 0, 0);
425 pcm->initialize_sound(rate, 8000);
426 #if defined(LDC_SLOT)
427 ldp->initialize_sound(rate, samples);
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);
434 uint16_t* VM::create_sound(int* extra_frames)
436 return event->create_sound(extra_frames);
439 int VM::get_sound_buffer_ptr()
441 return event->get_sound_buffer_ptr();
444 #if defined(LDC_SLOT)
445 void VM::movie_sound_callback(uint8_t *buffer, long size)
447 ldp->movie_sound_callback(buffer, size);
451 #ifdef USE_SOUND_VOLUME
452 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
455 #if defined(MSX_PSG_STEREO)
456 psg_stereo->set_volume(1, decibel_l, decibel_r);
458 psg->set_volume(1, decibel_l, decibel_r);
461 pcm->set_volume(0, decibel_l, decibel_r);
463 drec->set_volume(0, decibel_l, decibel_r);
465 } else if(ch-- == 3) {
466 ldp->set_volume(0, decibel_l, decibel_r);
469 sound_cart[0]->set_volume(0, decibel_l, decibel_r);
471 sound_cart[1]->set_volume(0, decibel_l, decibel_r);
473 ym2413->set_volume(0, decibel_l, decibel_r);
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);
482 // ----------------------------------------------------------------------------
484 // ----------------------------------------------------------------------------
486 void VM::open_cart(int drv, const _TCHAR* file_path)
489 slot_cart[drv]->open_cart(file_path);
494 void VM::close_cart(int drv)
497 slot_cart[drv]->close_cart();
502 bool VM::is_cart_inserted(int drv)
505 return slot_cart[drv]->is_cart_inserted();
511 void VM::play_tape(int drv, const _TCHAR* file_path)
513 drec->play_tape(file_path);
514 // drec->set_remote(true);
517 void VM::rec_tape(int drv, const _TCHAR* file_path)
519 drec->rec_tape(file_path);
520 // drec->set_remote(true);
523 void VM::close_tape(int drv)
528 // drec->set_remote(false);
531 bool VM::is_tape_inserted(int drv)
533 return drec->is_tape_inserted();
536 bool VM::is_tape_playing(int drv)
538 return drec->is_tape_playing();
541 bool VM::is_tape_recording(int drv)
543 return drec->is_tape_recording();
546 int VM::get_tape_position(int drv)
548 return drec->get_tape_position();
551 const _TCHAR* VM::get_tape_message(int drv)
553 return drec->get_message();
556 void VM::push_play(int drv)
559 drec->set_remote(true);
562 void VM::push_stop(int drv)
564 drec->set_remote(false);
567 void VM::push_fast_forward(int drv)
570 drec->set_remote(true);
573 void VM::push_fast_rewind(int drv)
575 drec->set_ff_rew(-1);
576 drec->set_remote(true);
579 #if defined(LDC_SLOT)
580 void VM::open_laser_disc(int drv, const _TCHAR* file_path)
582 ldp->open_disc(file_path);
585 void VM::close_laser_disc(int drv)
590 bool VM::is_laser_disc_inserted(int drv)
592 return ldp->is_disc_inserted();
595 uint32_t VM::is_laser_disc_accessed()
597 return ldp->read_signal(0);
601 #if defined(FDD_PATCH_SLOT)
602 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
604 memory->open_disk(drv, file_path, bank);
607 void VM::close_floppy_disk(int drv)
609 memory->close_disk(drv);
612 bool VM::is_floppy_disk_inserted(int drv)
614 return memory->is_disk_inserted(drv);
617 void VM::is_floppy_disk_protected(int drv, bool value)
619 memory->is_disk_protected(drv, value);
622 bool VM::is_floppy_disk_protected(int drv)
624 return memory->is_disk_protected(drv);
627 uint32_t VM::is_floppy_disk_accessed()
629 return memory->read_signal(0);
633 bool VM::is_frame_skippable()
635 return event->is_frame_skippable();
638 void VM::update_config()
640 for(DEVICE* device = first_device; device; device = device->next_device) {
641 device->update_config();
645 #define STATE_VERSION 5
647 #include "../../statesub.h"
648 #include "../../qt/gui/csp_logger.h"
649 extern CSP_Logger DLL_PREFIX_I *csp_logger;
651 void VM::decl_state(void)
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();
659 void VM::save_state(FILEIO* state_fio)
661 //state_fio->FputUint32(STATE_VERSION);
662 if(state_entry != NULL) {
663 state_entry->save_state(state_fio);
665 for(DEVICE* device = first_device; device; device = device->next_device) {
666 device->save_state(state_fio);
670 bool VM::load_state(FILEIO* state_fio)
672 //if(state_fio->FgetUint32() != STATE_VERSION) {
676 if(state_entry != NULL) {
677 mb = state_entry->load_state(state_fio);
680 emu->out_debug_log("INFO: HEADER DATA ERROR");
683 for(DEVICE* device = first_device; device; device = device->next_device) {
684 if(!device->load_state(state_fio)) {