2 NEC PC-8801MA Emulator 'ePC-8801MA'
3 NEC PC-8001mkIISR Emulator 'ePC-8001mkIISR'
5 Author : Takeda.Toshiya
12 #include "../../emu.h"
13 #include "../device.h"
18 #include "../pcm1bit.h"
19 //#include "../pcpr201.h"
20 #include "../prnfile.h"
21 #include "../upd1990a.h"
22 #include "../ym2203.h"
27 #include "../pc80s31k.h"
28 #include "../upd765a.h"
31 #include "../debugger.h"
34 #ifdef SUPPORT_PC88_PCG8100
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
47 boot_mode = config.boot_mode;
50 first_device = last_device = NULL;
51 dummy = new DEVICE(this, emu); // must be 1st device
53 pc88event = new EVENT(this, emu);
54 pc88event->set_device_name(_T("Event Manager (PC-8801)"));
55 // pc88event->set_frames_per_sec(60);
56 // pc88event->set_lines_per_frame(260);
57 dummy->set_device_name(_T("1st Dummy"));
58 pc88 = new PC88(this, emu);
60 #if defined(_PC8001) || defined(_PC8001MK2) || defined(_PC8001SR)
61 pc88->set_device_name(_T("PC8001 MAIN"));
63 pc88->set_device_name(_T("PC8801 MAIN"));
65 // pc88->set_context_event_manager(pc88event);
66 pc88sio = new I8251(this, emu);
67 // pc88sio->set_device_name(_T("8251 SIO (PC-8801)"));
68 // pc88sio->set_context_event_manager(pc88event);
69 pc88pio = new I8255(this, emu);
70 // pc88pio->set_device_name(_T("8255 PIO (PC-8801)"));
71 // pc88pio->set_context_event_manager(pc88event);
72 pc88pcm = new PCM1BIT(this, emu);
73 // pc88pcm->set_device_name(_T("1-Bit PCM Sound (PC-8801)"));
74 // pc88pcm->set_context_event_manager(pc88event);
75 pc88rtc = new UPD1990A(this, emu);
76 // pc88rtc->set_device_name(_T("uPD1990A RTC (PC-8801)"));
77 // pc88rtc->set_context_event_manager(pc88event);
79 // 0: 44h:OPNA A4h:None PC-8801FH/MH or later
80 // 1: 44h:OPN A4h:None PC-8801mkIISR/TR/FR/MR
81 // 2: 44h:OPN A4h:OPNA PC-8801mkIISR/TR/FR/MR + PC-8801-23
82 // 3: 44h:OPN A4h:OPN PC-8801mkIISR/TR/FR/MR + PC-8801-11
83 // 4: 44h:OPNA A4h:OPNA PC-8801FH/MH or later + PC-8801-24
84 // 5: 44h:OPNA A4h:OPN PC-8801FH/MH or later + PC-8801-11
85 pc88opn = new YM2203(this, emu);
86 // pc88opn->set_context_event_manager(pc88event);
88 #ifdef SUPPORT_PC88_OPNA
89 if(config.sound_type == 0 || config.sound_type == 4 || config.sound_type == 5) {
90 pc88opn->is_ym2608 = true;
91 pc88opn->set_device_name(_T("YM2608 OPNA"));
93 pc88opn->is_ym2608 = false;
94 pc88opn->set_device_name(_T("YM2203 OPN"));
97 #ifdef SUPPORT_PC88_SB2
98 if(config.sound_type >= 2) {
99 pc88sb2 = new YM2203(this, emu);
100 // pc88sb2->set_context_event_manager(pc88event);
101 #ifdef SUPPORT_PC88_OPNA
102 if(config.sound_type == 2 || config.sound_type == 4) {
103 pc88sb2->is_ym2608 = true;
104 pc88sb2->set_device_name(_T("YM2608 OPNA (SB2)"));
106 pc88sb2->is_ym2608 = false;
108 pc88sb2->set_device_name(_T("YM2203 OPN (SB2)"));
109 #ifdef SUPPORT_PC88_OPNA
117 if(config.printer_type == 0) {
118 pc88prn = new PRNFILE(this, emu);
119 // pc88prn->set_context_event_manager(pc88event);
120 // } else if(config.printer_type == 1) {
121 // pc88prn = new PCPR201(this, emu);
122 // pc88prn->set_context_event_manager(pc88event);
127 dummycpu = new DEVICE(this, emu);
128 pc88cpu = new Z80(this, emu);
129 // pc88cpu->set_context_event_manager(pc88event);
130 dummycpu->set_device_name(_T("DUMMY CPU"));
131 pc88cpu->set_device_name(_T("MAIN CPU(Z80)"));
133 pc88sub = new PC80S31K(this, emu);
134 pc88sub->set_device_name(_T("PC-80S31K (Sub)"));
135 // pc88sub->set_context_event_manager(pc88event);
136 pc88pio_sub = new I8255(this, emu);
137 pc88pio_sub->set_device_name(_T("8255 PIO (Sub)"));
138 // pc88pio_sub->set_context_event_manager(pc88event);
139 pc88fdc_sub = new UPD765A(this, emu);
140 pc88fdc_sub->set_device_name(_T("uPD765A FDC (Sub)"));
141 // pc88fdc_sub->set_context_event_manager(pc88event);
142 pc88noise_seek = new NOISE(this, emu);
143 // pc88noise_seek->set_context_event_manager(pc88event);
144 pc88noise_head_down = new NOISE(this, emu);
145 // pc88noise_head_down->set_context_event_manager(pc88event);
146 pc88noise_head_up = new NOISE(this, emu);
147 // pc88noise_head_up->set_context_event_manager(pc88event);
148 pc88cpu_sub = new Z80(this, emu);
149 pc88cpu_sub->set_device_name(_T("Z80 CPU (Sub)"));
150 // pc88cpu_sub->set_context_event_manager(pc88event);
152 #ifdef SUPPORT_PC88_PCG8100
153 pc88pit = new I8253(this, emu);
154 // pc88pit->set_context_event_manager(pc88event);
155 pc88pcm0 = new PCM1BIT(this, emu);
156 // pc88pcm0->set_context_event_manager(pc88event);
157 pc88pcm1 = new PCM1BIT(this, emu);
158 // pc88pcm1->set_context_event_manager(pc88event);
159 pc88pcm2 = new PCM1BIT(this, emu);
160 // pc88pcm2->set_context_event_manager(pc88event);
161 pc88pit->set_device_name(_T("i8253 PIT (PCG8100)"));
162 pc88pcm0->set_device_name(_T("SOUND #1 (PCG8100)"));
163 pc88pcm1->set_device_name(_T("SOUND #2 (PCG8100)"));
164 pc88pcm2->set_device_name(_T("SOUND #3 (PCG8100)"));
167 pc88event->set_context_cpu(dummycpu, 3993624 / 4);
168 #ifdef SUPPORT_PC88_HIGH_CLOCK
169 pc88event->set_context_cpu(pc88cpu, (config.cpu_type == 1) ? 3993624 : 7987248);
171 pc88event->set_context_cpu(pc88cpu, 3993624);
173 pc88event->set_context_cpu(pc88cpu_sub, 3993624);
174 pc88event->set_context_sound(pc88opn);
175 #ifdef SUPPORT_PC88_SB2
176 if(pc88sb2 != NULL) {
177 pc88event->set_context_sound(pc88sb2);
180 pc88event->set_context_sound(pc88pcm);
181 #ifdef SUPPORT_PC88_PCG8100
182 pc88event->set_context_sound(pc88pcm0);
183 pc88event->set_context_sound(pc88pcm1);
184 pc88event->set_context_sound(pc88pcm2);
186 pc88event->set_context_sound(pc88noise_seek);
187 pc88event->set_context_sound(pc88noise_head_down);
188 pc88event->set_context_sound(pc88noise_head_up);
189 pc88->set_context_cpu(pc88cpu);
190 pc88->set_context_opn(pc88opn);
191 #ifdef SUPPORT_PC88_SB2
192 pc88->set_context_sb2(pc88sb2);
194 pc88->set_context_pcm(pc88pcm);
195 pc88->set_context_pio(pc88pio);
196 pc88->set_context_prn(pc88prn);
197 pc88->set_context_rtc(pc88rtc);
198 pc88->set_context_sio(pc88sio);
199 #ifdef SUPPORT_PC88_PCG8100
200 pc88->set_context_pcg_pit(pc88pit);
201 pc88->set_context_pcg_pcm0(pc88pcm0);
202 pc88->set_context_pcg_pcm1(pc88pcm1);
203 pc88->set_context_pcg_pcm2(pc88pcm2);
205 pc88cpu->set_context_mem(pc88);
206 pc88cpu->set_context_io(pc88);
207 pc88cpu->set_context_intr(pc88);
209 pc88cpu->set_context_debugger(new DEBUGGER(this, emu));
211 pc88opn->set_context_irq(pc88, SIG_PC88_SOUND_IRQ, 1);
212 #ifdef SUPPORT_PC88_SB2
213 if(pc88sb2 != NULL) {
214 pc88sb2->set_context_irq(pc88, SIG_PC88_SB2_IRQ, 1);
217 pc88sio->set_context_rxrdy(pc88, SIG_PC88_USART_IRQ, 1);
218 pc88sio->set_context_out(pc88, SIG_PC88_USART_OUT);
220 pc88sub->set_context_cpu(pc88cpu_sub);
221 pc88sub->set_context_fdc(pc88fdc_sub);
222 pc88sub->set_context_pio(pc88pio_sub);
223 pc88pio->set_context_port_a(pc88pio_sub, SIG_I8255_PORT_B, 0xff, 0);
224 pc88pio->set_context_port_b(pc88pio_sub, SIG_I8255_PORT_A, 0xff, 0);
225 pc88pio->set_context_port_c(pc88pio_sub, SIG_I8255_PORT_C, 0x0f, 4);
226 pc88pio->set_context_port_c(pc88pio_sub, SIG_I8255_PORT_C, 0xf0, -4);
227 pc88pio->clear_ports_by_cmdreg = true;
228 pc88pio_sub->set_context_port_a(pc88pio, SIG_I8255_PORT_B, 0xff, 0);
229 pc88pio_sub->set_context_port_b(pc88pio, SIG_I8255_PORT_A, 0xff, 0);
230 pc88pio_sub->set_context_port_c(pc88pio, SIG_I8255_PORT_C, 0x0f, 4);
231 pc88pio_sub->set_context_port_c(pc88pio, SIG_I8255_PORT_C, 0xf0, -4);
232 pc88pio_sub->clear_ports_by_cmdreg = true;
233 pc88fdc_sub->set_context_irq(pc88cpu_sub, SIG_CPU_IRQ, 1);
234 pc88fdc_sub->set_context_noise_seek(pc88noise_seek);
235 pc88fdc_sub->set_context_noise_head_down(pc88noise_head_down);
236 pc88fdc_sub->set_context_noise_head_up(pc88noise_head_up);
237 pc88cpu_sub->set_context_mem(pc88sub);
238 pc88cpu_sub->set_context_io(pc88sub);
239 pc88cpu_sub->set_context_intr(pc88sub);
241 pc88cpu_sub->set_context_debugger(new DEBUGGER(this, emu));
244 #ifdef SUPPORT_PC88_PCG8100
245 pc88pit->set_context_ch0(pc88pcm0, SIG_PCM1BIT_SIGNAL, 1);
246 pc88pit->set_context_ch1(pc88pcm1, SIG_PCM1BIT_SIGNAL, 1);
247 pc88pit->set_context_ch2(pc88pcm2, SIG_PCM1BIT_SIGNAL, 1);
248 pc88pit->set_constant_clock(0, 3993624);
249 pc88pit->set_constant_clock(1, 3993624);
250 pc88pit->set_constant_clock(2, 3993624);
252 // initialize all devices
253 #if defined(__GIT_REPO_VERSION)
254 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
256 for(DEVICE* device = first_device; device; device = device->next_device) {
257 device->initialize();
264 // delete all devices
265 for(DEVICE* device = first_device; device;) {
266 DEVICE *next_device = device->next_device;
269 device = next_device;
273 DEVICE* VM::get_device(int id)
275 for(DEVICE* device = first_device; device; device = device->next_device) {
276 if(device->this_device_id == id) {
283 // ----------------------------------------------------------------------------
284 // drive virtual machine
285 // ----------------------------------------------------------------------------
290 for(DEVICE* device = first_device; device; device = device->next_device) {
293 for(DEVICE* device = first_device; device; device = device->next_device) {
297 // initial device settings
298 pc88opn->set_reg(0x29, 3); // for Misty Blue
299 pc88pio->write_signal(SIG_I8255_PORT_C, 0, 0xff);
300 pc88pio_sub->write_signal(SIG_I8255_PORT_C, 0, 0xff);
308 double VM::get_frame_rate()
310 return pc88event->get_frame_rate();
313 // ----------------------------------------------------------------------------
315 // ----------------------------------------------------------------------------
318 DEVICE *VM::get_cpu(int index)
322 } else if(index == 1) {
329 // ----------------------------------------------------------------------------
331 // ----------------------------------------------------------------------------
333 void VM::draw_screen()
338 // ----------------------------------------------------------------------------
340 // ----------------------------------------------------------------------------
342 void VM::initialize_sound(int rate, int samples)
344 // init sound manager
345 pc88event->initialize_sound(rate, samples);
348 #ifdef SUPPORT_PC88_OPNA
349 if(pc88opn->is_ym2608) {
350 pc88opn->initialize_sound(rate, 7987248, samples, 0, 0);
353 pc88opn->initialize_sound(rate, 3993624, samples, 0, 0);
354 #ifdef SUPPORT_PC88_SB2
355 if(pc88sb2 != NULL) {
356 #ifdef SUPPORT_PC88_OPNA
357 if(pc88sb2->is_ym2608) {
358 pc88sb2->initialize_sound(rate, 7987248, samples, 0, 0);
361 pc88sb2->initialize_sound(rate, 3993624, samples, 0, 0);
364 pc88pcm->initialize_sound(rate, 8000);
365 #ifdef SUPPORT_PC88_PCG8100
366 pc88pcm0->initialize_sound(rate, 8000);
367 pc88pcm1->initialize_sound(rate, 8000);
368 pc88pcm2->initialize_sound(rate, 8000);
372 uint16_t* VM::create_sound(int* extra_frames)
374 return pc88event->create_sound(extra_frames);
377 int VM::get_sound_buffer_ptr()
379 return pc88event->get_sound_buffer_ptr();
382 #ifdef USE_SOUND_VOLUME
383 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
386 pc88opn->set_volume(0, decibel_l, decibel_r);
387 } else if(ch-- == 0) {
388 pc88opn->set_volume(1, decibel_l, decibel_r);
389 #ifdef SUPPORT_PC88_OPNA
390 } else if(ch-- == 0) {
391 pc88opn->set_volume(2, decibel_l, decibel_r);
392 } else if(ch-- == 0) {
393 pc88opn->set_volume(3, decibel_l, decibel_r);
395 #ifdef SUPPORT_PC88_SB2
396 } else if(ch-- == 0) {
397 if(pc88sb2 != NULL) {
398 pc88sb2->set_volume(0, decibel_l, decibel_r);
400 } else if(ch-- == 0) {
401 if(pc88sb2 != NULL) {
402 pc88sb2->set_volume(1, decibel_l, decibel_r);
404 #ifdef SUPPORT_PC88_OPNA
405 } else if(ch-- == 0) {
406 if(pc88sb2 != NULL) {
407 pc88sb2->set_volume(2, decibel_l, decibel_r);
409 } else if(ch-- == 0) {
410 if(pc88sb2 != NULL) {
411 pc88sb2->set_volume(3, decibel_l, decibel_r);
415 #ifdef SUPPORT_PC88_PCG8100
416 } else if(ch-- == 0) {
417 pc88pcm0->set_volume(0, decibel_l, decibel_r);
418 pc88pcm1->set_volume(0, decibel_l, decibel_r);
419 pc88pcm2->set_volume(0, decibel_l, decibel_r);
421 } else if(ch-- == 0) {
422 pc88pcm->set_volume(0, decibel_l, decibel_r);
423 } else if(ch-- == 0) {
424 pc88noise_seek->set_volume(0, decibel_l, decibel_r);
425 pc88noise_head_down->set_volume(0, decibel_l, decibel_r);
426 pc88noise_head_up->set_volume(0, decibel_l, decibel_r);
431 // ----------------------------------------------------------------------------
433 // ----------------------------------------------------------------------------
435 void VM::key_down(int code, bool repeat)
437 pc88->key_down(code, repeat);
440 void VM::key_up(int code)
444 bool VM::get_caps_locked()
446 return pc88->get_caps_locked();
449 bool VM::get_kana_locked()
451 return pc88->get_kana_locked();
454 // ----------------------------------------------------------------------------
456 // ----------------------------------------------------------------------------
458 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
460 pc88fdc_sub->open_disk(drv, file_path, bank);
463 void VM::close_floppy_disk(int drv)
465 pc88fdc_sub->close_disk(drv);
468 bool VM::is_floppy_disk_inserted(int drv)
470 return pc88fdc_sub->is_disk_inserted(drv);
473 void VM::is_floppy_disk_protected(int drv, bool value)
475 pc88fdc_sub->is_disk_protected(drv, value);
478 bool VM::is_floppy_disk_protected(int drv)
480 return pc88fdc_sub->is_disk_protected(drv);
483 uint32_t VM::is_floppy_disk_accessed()
485 return pc88fdc_sub->read_signal(0);
488 void VM::play_tape(int drv, const _TCHAR* file_path)
490 pc88->play_tape(file_path);
493 void VM::rec_tape(int drv, const _TCHAR* file_path)
495 pc88->rec_tape(file_path);
498 void VM::close_tape(int drv)
503 bool VM::is_tape_inserted(int drv)
505 return pc88->is_tape_inserted();
508 bool VM::is_frame_skippable()
510 // return event->is_frame_skippable();
511 return pc88->is_frame_skippable();
514 void VM::update_config()
516 if(boot_mode != config.boot_mode) {
517 // boot mode is changed !!!
518 boot_mode = config.boot_mode;
521 for(DEVICE* device = first_device; device; device = device->next_device) {
522 device->update_config();
527 #define STATE_VERSION 8
529 #include "../../statesub.h"
530 #include "../../qt/gui/csp_logger.h"
531 extern CSP_Logger DLL_PREFIX_I *csp_logger;
533 void VM::decl_state(void)
535 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PC88_SERIES_HEAD")), csp_logger);
536 DECL_STATE_ENTRY_BOOL(boot_mode);
537 for(DEVICE* device = first_device; device; device = device->next_device) {
538 device->decl_state();
542 void VM::save_state(FILEIO* state_fio)
544 if(state_entry != NULL) {
545 state_entry->save_state(state_fio);
547 for(DEVICE* device = first_device; device; device = device->next_device) {
548 device->save_state(state_fio);
552 bool VM::load_state(FILEIO* state_fio)
555 if(state_entry != NULL) {
556 mb = state_entry->load_state(state_fio);
559 emu->out_debug_log("INFO: HEADER DATA ERROR");
562 for(DEVICE* device = first_device; device; device = device->next_device) {
563 if(!device->load_state(state_fio)) {