2 NEC PC-6001 Emulator 'yaPC-6001'
3 NEC PC-6001mkII Emulator 'yaPC-6201'
4 NEC PC-6001mkIISR Emulator 'yaPC-6401'
5 NEC PC-6601 Emulator 'yaPC-6601'
6 NEC PC-6601SR Emulator 'yaPC-6801'
15 #include "../../emu.h"
16 #include "../device.h"
22 #include "../mc6847.h"
25 #include "../upd7752.h"
27 #include "../pc6031.h"
28 #include "../pc80s31k.h"
29 #include "../upd765a.h"
30 #include "../ym2203.h"
33 #include "../datarec.h"
37 #include "../debugger.h"
40 #if defined(_PC6601) || defined(_PC6601SR)
50 #include "../../fileio.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 VM::VM(EMU* parent_emu) : emu(parent_emu)
58 support_pc80s31k = FILEIO::IsFileExists(emu->bios_path(_T("DISK.ROM")));
60 support_sub_cpu = false;
62 support_sub_cpu = FILEIO::IsFileExists(emu->bios_path(_T(SUB_CPU_ROM_FILE_NAME)));
66 first_device = last_device = NULL;
67 dummy = new DEVICE(this, emu); // must be 1st device
68 event = new EVENT(this, emu); // must be 2nd device
70 pio_sub = new I8255(this, emu);
71 io = new IO(this, emu);
72 psg = new YM2203(this, emu);
73 cpu = new Z80(this, emu);
75 #if defined(_PC6601) || defined(_PC6601SR)
76 floppy = new FLOPPY(this, emu);
78 joystick = new JOYSTICK(this, emu);
79 memory = new MEMORY(this, emu);
80 // printer = new PRINTER(this, emu);
81 timer = new TIMER(this, emu);
84 event->set_context_cpu(cpu);
85 event->set_context_sound(psg);
87 // pio_sub->set_context_port_b(printer, SIG_PRINTER_OUT, 0xff, 0);
88 // pio_sub->set_context_port_c(printer, SIG_PRINTER_STB, 0x01, 0);
89 pio_sub->set_context_port_c(memory, SIG_MEMORY_PIO_PORT_C, 0x06, 0); // CRTKILL,CGSWN
92 display = new DISPLAY(this, emu);
93 vdp = new MC6847(this, emu);
94 display->set_context_vdp(vdp);
95 display->set_vram_ptr(memory->get_vram());
96 display->set_context_timer(timer);
97 vdp->load_font_image(emu->bios_path(_T("CGROM60.60")));
98 vdp->set_context_cpu(cpu);
99 pio_sub->set_context_port_c(vdp, SIG_MC6847_ENABLE, 0x02, 0); // CRTKILL
101 voice = new UPD7752(this, emu);
102 event->set_context_sound(voice);
103 memory->set_context_timer(timer);
105 memory->set_context_cpu(cpu);
106 joystick->set_context_psg(psg);
108 timer->set_context_cpu(cpu);
110 timer->set_context_memory(memory);
112 if(support_sub_cpu) {
113 cpu_sub = new MCS48(this, emu);
114 sub = new SUB(this, emu);
115 drec = new DATAREC(this, emu);
116 event->set_context_cpu(cpu_sub, 8000000);
117 cpu_sub->set_context_mem(new MCS48MEM(this, emu));
118 cpu_sub->set_context_io(sub);
120 cpu_sub->set_context_debugger(new DEBUGGER(this, emu));
122 sub->set_context_pio(pio_sub);
123 sub->set_context_drec(drec);
124 sub->set_context_timer(timer);
125 pio_sub->set_context_port_c(cpu_sub, SIG_CPU_IRQ, 0x80, 0);
126 drec->set_context_out(sub, SIG_SUB_DATAREC, 1);
127 timer->set_context_sub(sub);
129 psub = new PSUB(this, emu);
130 psub->set_context_pio(pio_sub);
131 psub->set_context_timer(timer);
132 timer->set_context_sub(psub);
135 if(support_pc80s31k) {
136 pio_fdd = new I8255(this, emu);
137 pio_pc80s31k = new I8255(this, emu);
138 pc80s31k = new PC80S31K(this, emu);
139 fdc_pc80s31k = new UPD765A(this, emu);
140 cpu_pc80s31k = new Z80(this, emu);
142 event->set_context_cpu(cpu_pc80s31k, 4000000);
143 pc80s31k->set_context_cpu(cpu_pc80s31k);
144 pc80s31k->set_context_fdc(fdc_pc80s31k);
145 pc80s31k->set_context_pio(pio_pc80s31k);
146 pio_fdd->set_context_port_a(pio_pc80s31k, SIG_I8255_PORT_A, 0xff, 0);
147 pio_fdd->set_context_port_b(pio_pc80s31k, SIG_I8255_PORT_A, 0xff, 0);
148 pio_fdd->set_context_port_c(pio_pc80s31k, SIG_I8255_PORT_C, 0x0f, 4);
149 pio_fdd->set_context_port_c(pio_pc80s31k, SIG_I8255_PORT_C, 0xf0, -4);
150 pio_fdd->clear_ports_by_cmdreg = true;
151 pio_pc80s31k->set_context_port_a(pio_fdd, SIG_I8255_PORT_B, 0xff, 0);
152 pio_pc80s31k->set_context_port_b(pio_fdd, SIG_I8255_PORT_A, 0xff, 0);
153 pio_pc80s31k->set_context_port_c(pio_fdd, SIG_I8255_PORT_C, 0x0f, 4);
154 pio_pc80s31k->set_context_port_c(pio_fdd, SIG_I8255_PORT_C, 0xf0, -4);
155 pio_pc80s31k->clear_ports_by_cmdreg = true;
156 fdc_pc80s31k->set_context_irq(cpu_pc80s31k, SIG_CPU_IRQ, 1);
157 cpu_pc80s31k->set_context_mem(pc80s31k);
158 cpu_pc80s31k->set_context_io(pc80s31k);
159 cpu_pc80s31k->set_context_intr(pc80s31k);
161 cpu_pc80s31k->set_context_debugger(new DEBUGGER(this, emu));
163 #if defined(_PC6601) || defined(_PC6601SR)
164 floppy->set_context_ext(pio_fdd);
167 pc6031 = new PC6031(this, emu);
168 #if defined(_PC6601) || defined(_PC6601SR)
169 floppy->set_context_ext(pc6031);
175 cpu->set_context_mem(memory);
176 cpu->set_context_io(io);
177 cpu->set_context_intr(timer);
179 cpu->set_context_debugger(new DEBUGGER(this, emu));
183 if(support_sub_cpu) {
184 io->set_iomap_range_rw(0x90, 0x93, sub);
186 io->set_iomap_range_rw(0x90, 0x93, psub);
188 io->set_iomap_alias_w(0xa0, psg, 0); // PSG ch
189 io->set_iomap_alias_w(0xa1, psg, 1); // PSG data
190 io->set_iomap_alias_r(0xa2, psg, 1); // PSG data
191 #if defined(_PC6601SR) || defined(_PC6001MK2SR)
192 io->set_iomap_alias_r(0xa3, psg, 0); // FM status
193 io->set_iomap_range_rw(0x40, 0x6f, memory); // VRAM addr
196 io->set_iomap_single_w(0xb0, display); // VRAM addr
197 io->set_iomap_single_w(0x00, memory); // MEMORY MAP
199 io->set_iomap_single_w(0xb0, memory); // VRAM addr
200 io->set_iomap_range_rw(0xc0, 0xcf, memory); // VRAM addr
201 io->set_iomap_range_rw(0xe0, 0xe3, voice); // VOICE
202 #if defined(_PC6601) || defined(_PC6601SR)
203 io->set_iomap_range_rw(0xb8, 0xbf, timer); // IRQ
204 io->set_iomap_range_rw(0xfa, 0xfb, timer); // IRQ
206 io->set_iomap_range_rw(0xf3, 0xf7, timer); // IRQ/Timer
209 #if defined(_PC6601) || defined(_PC6601SR)
210 io->set_iomap_range_rw(0xb1, 0xb3, floppy); // DISK DRIVE
211 io->set_iomap_range_rw(0xb5, 0xb7, floppy); // DISK DRIVE (Mirror)
212 io->set_iomap_range_rw(0xd0, 0xde, floppy); // DISK DRIVE
214 io->set_iovalue_single_r(0xb1, 0x01);
215 #if defined(_PC6601SR)
216 io->set_iovalue_single_r(0xb2, 0x02);
217 #elif defined(_PC6001MK2SR)
218 io->set_iovalue_single_r(0xb2, 0x00);
221 if(support_pc80s31k) {
222 io->set_iomap_range_rw(0xd0, 0xd2, pio_fdd);
223 io->set_iomap_single_w(0xd3, pio_fdd);
225 io->set_iomap_range_rw(0xd0, 0xd3, pc6031);
228 io->set_iomap_range_rw(0xf0, 0xf2, memory); // MEMORY MAP
230 // initialize all devices
231 for(DEVICE* device = first_device; device; device = device->next_device) {
232 device->initialize();
234 if(support_sub_cpu) {
235 // load rom images after cpustate is allocated
238 cpu_sub->load_rom_image(emu->bios_path(SUB_CPU_ROM_FILE_NAME));
245 // delete all devices
246 for(DEVICE* device = first_device; device;) {
247 DEVICE *next_device = device->next_device;
250 device = next_device;
254 DEVICE* VM::get_device(int id)
256 for(DEVICE* device = first_device; device; device = device->next_device) {
257 if(device->this_device_id == id) {
264 // ----------------------------------------------------------------------------
265 // drive virtual machine
266 // ----------------------------------------------------------------------------
270 for(DEVICE* device = first_device; device; device = device->next_device) {
273 if(support_pc80s31k) {
274 pio_fdd->write_signal(SIG_I8255_PORT_C, 0, 0xff);
275 pio_pc80s31k->write_signal(SIG_I8255_PORT_C, 0, 0xff);
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
289 DEVICE *VM::get_cpu(int index)
293 } else if(index == 1) {
295 } else if(index == 2) {
302 // ----------------------------------------------------------------------------
304 // ----------------------------------------------------------------------------
306 void VM::draw_screen()
309 display->draw_screen();
311 memory->draw_screen();
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
318 void VM::initialize_sound(int rate, int samples)
320 // init sound manager
321 event->initialize_sound(rate, samples);
324 psg->init(rate, 4000000, samples, 0, 0);
330 uint16* VM::create_sound(int* extra_frames)
332 return event->create_sound(extra_frames);
335 int VM::sound_buffer_ptr()
337 return event->sound_buffer_ptr();
340 // ----------------------------------------------------------------------------
342 // ----------------------------------------------------------------------------
344 void VM::open_cart(int drv, _TCHAR* file_path)
347 memory->open_cart(file_path);
352 void VM::close_cart(int drv)
355 memory->close_cart();
360 bool VM::cart_inserted(int drv)
363 return memory->cart_inserted();
369 int VM::access_lamp()
371 uint32 status = 0; /// fdc->read_signal(0);
372 #if defined(_PC6601) || defined(_PC6601SR)
373 status = floppy->read_signal(0);
375 if(support_pc80s31k) {
376 status |= fdc_pc80s31k->read_signal(0);
378 status |= pc6031->read_signal(0);
383 void VM::open_disk(int drv, _TCHAR* file_path, int bank)
385 #if defined(_PC6601) || defined(_PC6601SR)
387 floppy->open_disk(drv, file_path, bank);
393 if(support_pc80s31k) {
394 fdc_pc80s31k->open_disk(drv, file_path, bank);
396 pc6031->open_disk(drv, file_path, bank);
400 void VM::close_disk(int drv)
402 #if defined(_PC6601) || defined(_PC6601SR)
404 floppy->close_disk(drv);
410 if(support_pc80s31k) {
411 fdc_pc80s31k->close_disk(drv);
413 pc6031->close_disk(drv);
417 bool VM::disk_inserted(int drv)
419 #if defined(_PC6601) || defined(_PC6601SR)
421 return floppy->disk_inserted(drv);
426 if(support_pc80s31k) {
427 return fdc_pc80s31k->disk_inserted(drv);
429 return pc6031->disk_inserted(drv);
433 void VM::write_protect_fd(int drv, bool flag)
435 #if defined(_PC6601) || defined(_PC6601SR)
437 return floppy->write_protect_fd(drv, flag);
442 if(support_pc80s31k) {
443 return fdc_pc80s31k->write_protect_fd(drv, flag);
445 return pc6031->write_protect_fd(drv, flag);
449 bool VM::is_write_protect_fd(int drv)
451 #if defined(_PC6601) || defined(_PC6601SR)
453 return floppy->is_write_protect_fd(drv);
458 if(support_pc80s31k) {
459 return fdc_pc80s31k->is_write_protect_fd(drv);
461 return pc6031->is_write_protect_fd(drv);
467 void VM::play_tape(_TCHAR* file_path)
469 if(support_sub_cpu) {
470 // support both p6/p6t and wav
471 drec->play_tape(file_path);
473 // support only p6/p6t
474 psub->play_tape(file_path);
478 void VM::rec_tape(_TCHAR* file_path)
480 if(support_sub_cpu) {
481 // support both p6/p6t and wav
482 sub->rec_tape(file_path); // temporary
483 // drec->rec_tape(file_path);
485 // support both p6/p6t and wav
486 psub->rec_tape(file_path);
490 void VM::close_tape()
492 if(support_sub_cpu) {
493 if(sub->tape_inserted()) {
494 sub->close_tape(); // temporary
503 bool VM::tape_inserted()
505 if(support_sub_cpu) {
506 return drec->tape_inserted() || sub->tape_inserted();
508 return psub->tape_inserted();
512 int VM::get_tape_ptr()
514 if(support_sub_cpu) {
515 return drec->get_tape_ptr();
517 return psub->get_tape_ptr();
525 return event->now_skip();
528 void VM::update_config()
530 for(DEVICE* device = first_device; device; device = device->next_device) {
531 device->update_config();
535 #define STATE_VERSION 1
537 void VM::save_state(FILEIO* state_fio)
539 state_fio->FputUint32(STATE_VERSION);
541 for(DEVICE* device = first_device; device; device = device->next_device) {
542 device->save_state(state_fio);
544 state_fio->FputInt32(sr_mode);
547 bool VM::load_state(FILEIO* state_fio)
549 if(state_fio->FgetUint32() != STATE_VERSION) {
552 for(DEVICE* device = first_device; device; device = device->next_device) {
553 if(!device->load_state(state_fio)) {
557 sr_mode = state_fio->FgetInt32();