2 ASCII MSX1 Emulator 'yaMSX1'
3 ASCII MSX2 Emulator 'yaMSX2'
4 Pioneer PX-7 Emulator 'ePX-7'
9 modified by Takeda.Toshiya
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"
32 #include "../rp5c01.h"
35 #include "../tms9918a.h"
40 #include "../debugger.h"
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
68 first_device = last_device = NULL;
69 dummy = new DEVICE(this, emu); // must be 1st device
70 event = new EVENT(this, emu); // must be 2nd device
71 dummy->set_device_name(_T("1st Dummy"));
72 event->set_device_name(_T("EVENT"));
74 drec = new DATAREC(this, emu);
75 drec->set_context_noise_play(new NOISE(this, emu));
76 drec->set_context_noise_stop(new NOISE(this, emu));
77 drec->set_context_noise_fast(new NOISE(this, emu));
78 pio = new I8255(this, emu);
79 io = new IO(this, emu);
81 ldp = new LD700(this, emu);
83 not_remote = new NOT(this, emu);
84 // psg = new YM2203(this, emu);
85 psg = new AY_3_891X(this, emu);
86 pcm = new PCM1BIT(this, emu);
87 not_remote->set_device_name(_T("NOT Gate (REMOTE)"));
90 rtc = new RP5C01(this, emu);
91 vdp = new V99X8(this, emu);
93 vdp = new TMS9918A(this, emu);
95 cpu = new Z80(this, emu);
96 joystick = new JOYSTICK(this, emu);
97 keyboard = new KEYBOARD(this, emu);
98 memory = new MSX_MEMORY(this, emu);
100 rtcif = new RTCIF(this, emu);
102 slot0 = new SLOT0(this, emu); // #0: main memory
103 slot1 = new SLOT1(this, emu); // #1: rom-cartridge or msx-dos
104 slot2 = new SLOT2(this, emu); // #2: fdd-cartridge or p-basic
105 slot3 = new SLOT3(this, emu); // #3: rom-cartridge or ram-cartridge
107 slot0->set_device_name(_T("SLOT#0 MAIN MEMORY"));
108 slot1->set_device_name(_T("SLOT#1 ROM CARTRIDGE"));
109 slot2->set_device_name(_T("SLOT#2 FDD CARTRIDGE"));
110 slot3->set_device_name(_T("SLOT#3 ROM CARTRIDGE"));
113 event->set_context_cpu(cpu);
114 event->set_context_sound(psg);
115 event->set_context_sound(pcm);
116 event->set_context_sound(drec);
118 event->set_context_sound(ldp);
120 event->set_context_sound(drec->get_context_noise_play());
121 event->set_context_sound(drec->get_context_noise_stop());
122 event->set_context_sound(drec->get_context_noise_fast());
124 drec->set_context_ear(psg, SIG_AY_3_891X_PORT_A, 0x80);
125 pio->set_context_port_a(memory, SIG_MEMORY_SEL, 0xff, 0);
126 pio->set_context_port_c(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);
127 pio->set_context_port_c(not_remote, SIG_NOT_INPUT, 0x10, 0);
128 not_remote->set_context_out(drec, SIG_DATAREC_REMOTE, 1);
129 pio->set_context_port_c(drec, SIG_DATAREC_MIC, 0x20, 0);
130 pio->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x80, 0);
131 psg->set_context_port_b(joystick, SIG_JOYSTICK_SEL, 0x40, 0);
132 vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
135 pio->set_context_port_c(slot2, SIG_SLOT2_MUTE, 0x10, 0);
136 ldp->set_context_exv(slot2, SIG_SLOT2_EXV, 1);
137 ldp->set_context_ack(slot2, SIG_SLOT2_ACK, 1);
138 ldp->set_context_sound(psg, SIG_AY_3_891X_PORT_A, 0x80);
140 joystick->set_context_psg(psg);
141 // keyboard->set_context_cpu(cpu);
142 keyboard->set_context_pio(pio);
143 memory->set_context_slot(0, slot0);
144 memory->set_context_slot(1, slot1);
145 memory->set_context_slot(2, slot2);
146 memory->set_context_slot(3, slot3);
148 rtcif->set_context_rtc(rtc);
151 slot2->set_context_cpu(cpu);
152 slot2->set_context_ldp(ldp);
153 slot2->set_context_vdp(vdp);
157 cpu->set_context_mem(memory);
158 cpu->set_context_io(io);
159 cpu->set_context_intr(dummy);
161 cpu->set_context_bios(memory);
164 cpu->set_context_debugger(new DEBUGGER(this, emu));
169 io->set_iomap_range_rw(0xb4, 0xb5, rtcif);
170 io->set_iomap_range_rw(0x98, 0x9b, vdp);
172 io->set_iomap_range_rw(0x98, 0x99, vdp);
174 io->set_iomap_range_rw(0xa8, 0xab, pio);
175 io->set_iomap_alias_w(0xa0, psg, 0); // PSG ch
176 io->set_iomap_alias_w(0xa1, psg, 1); // PSG data
177 io->set_iomap_alias_r(0xa2, psg, 1); // PSG data
178 io->set_iomap_range_rw(0xfc, 0xff, memory);
180 // initialize all devices
181 #if defined(__GIT_REPO_VERSION)
182 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
184 for(DEVICE* device = first_device; device; device = device->next_device) {
185 device->initialize();
191 // delete all devices
192 for(DEVICE* device = first_device; device;) {
193 DEVICE *next_device = device->next_device;
196 device = next_device;
200 DEVICE* VM::get_device(int id)
202 for(DEVICE* device = first_device; device; device = device->next_device) {
203 if(device->this_device_id == id) {
210 // ----------------------------------------------------------------------------
211 // drive virtual machine
212 // ----------------------------------------------------------------------------
217 for(DEVICE* device = first_device; device; device = device->next_device) {
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
232 DEVICE *VM::get_cpu(int index)
241 // ----------------------------------------------------------------------------
243 // ----------------------------------------------------------------------------
245 void VM::draw_screen()
250 // ----------------------------------------------------------------------------
252 // ----------------------------------------------------------------------------
254 void VM::initialize_sound(int rate, int samples)
256 // init sound manager
257 event->initialize_sound(rate, samples);
260 psg->initialize_sound(rate, 3579545, samples, 0, 0);
261 pcm->initialize_sound(rate, 8000);
263 ldp->initialize_sound(rate, samples);
267 uint16_t* VM::create_sound(int* extra_frames)
269 return event->create_sound(extra_frames);
272 int VM::get_sound_buffer_ptr()
274 return event->get_sound_buffer_ptr();
278 void VM::movie_sound_callback(uint8_t *buffer, long size)
280 ldp->movie_sound_callback(buffer, size);
284 #ifdef USE_SOUND_VOLUME
285 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
288 psg->set_volume(1, decibel_l, decibel_r);
289 } else if(ch-- == 0) {
290 pcm->set_volume(0, decibel_l, decibel_r);
291 } else if(ch-- == 0) {
292 drec->set_volume(0, decibel_l, decibel_r);
294 } else if(ch-- == 0) {
295 ldp->set_volume(0, decibel_l, decibel_r);
297 } else if(ch-- == 0) {
298 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
299 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
300 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 void VM::open_cart(int drv, const _TCHAR* file_path)
312 slot1->open_cart(file_path);
314 slot3->open_cart(file_path);
319 void VM::close_cart(int drv)
329 bool VM::is_cart_inserted(int drv)
332 return slot1->is_cart_inserted();
334 return slot3->is_cart_inserted();
338 void VM::play_tape(int drv, const _TCHAR* file_path)
340 drec->play_tape(file_path);
341 // drec->set_remote(true);
344 void VM::rec_tape(int drv, const _TCHAR* file_path)
346 drec->rec_tape(file_path);
347 // drec->set_remote(true);
350 void VM::close_tape(int drv)
355 // drec->set_remote(false);
358 bool VM::is_tape_inserted(int drv)
360 return drec->is_tape_inserted();
363 bool VM::is_tape_playing(int drv)
365 return drec->is_tape_playing();
368 bool VM::is_tape_recording(int drv)
370 return drec->is_tape_recording();
373 int VM::get_tape_position(int drv)
375 return drec->get_tape_position();
378 const _TCHAR* VM::get_tape_message(int drv)
380 return drec->get_message();
383 void VM::push_play(int drv)
386 drec->set_remote(true);
389 void VM::push_stop(int drv)
391 drec->set_remote(false);
394 void VM::push_fast_forward(int drv)
397 drec->set_remote(true);
400 void VM::push_fast_rewind(int drv)
402 drec->set_ff_rew(-1);
403 drec->set_remote(true);
406 void VM::load_binary(int drv, const _TCHAR* file_path)
409 pac2->open_rampac2(file_path);
414 void VM::open_laser_disc(int drv, const _TCHAR* file_path)
416 ldp->open_disc(file_path);
419 void VM::close_laser_disc(int drv)
424 bool VM::is_laser_disc_inserted(int drv)
426 return ldp->is_disc_inserted();
429 uint32_t VM::is_laser_disc_accessed()
431 return lpd->read_signal(0);
434 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
436 memory->open_disk(drv, file_path, bank);
439 void VM::close_floppy_disk(int drv)
441 memory->close_disk(drv);
444 bool VM::is_floppy_disk_inserted(int drv)
446 return memory->is_disk_inserted(drv);
449 void VM::is_floppy_disk_protected(int drv, bool value)
451 memory->is_disk_protected(drv, value);
454 bool VM::is_floppy_disk_protected(int drv)
456 return memory->is_disk_protected(drv);
459 uint32_t VM::is_floppy_disk_accessed()
461 return memory->read_signal(0);
465 bool VM::is_frame_skippable()
467 return event->is_frame_skippable();
470 void VM::update_config()
472 for(DEVICE* device = first_device; device; device = device->next_device) {
473 device->update_config();
477 #define STATE_VERSION 4
479 void VM::save_state(FILEIO* state_fio)
481 state_fio->FputUint32(STATE_VERSION);
483 for(DEVICE* device = first_device; device; device = device->next_device) {
484 device->save_state(state_fio);
488 bool VM::load_state(FILEIO* state_fio)
490 if(state_fio->FgetUint32() != STATE_VERSION) {
493 for(DEVICE* device = first_device; device; device = device->next_device) {
494 if(!device->load_state(state_fio)) {
501 bool VM::process_state(FILEIO* state_fio, bool loading)
503 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
506 for(DEVICE* device = first_device; device; device = device->next_device) {
507 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
508 // const char *name = typeid(*device).name();
509 // But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
510 const char *name = device->get_device_name();
511 int len = strlen(name);
513 if(!state_fio->StateCheckInt32(len)) {
515 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
519 if(!state_fio->StateCheckBuffer(name, len, 1)) {
521 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
525 if(!device->process_state(state_fio, loading)) {
527 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
532 // Machine specified.