OSDN Git Service

[General] Completely merge upstream 2019-01-11.
[csp-qt/common_source_project-fm7.git] / source / src / vm / pc9801 / pc9801.cpp
1 /*
2         NEC PC-9801 Emulator 'ePC-9801'
3         NEC PC-9801E/F/M Emulator 'ePC-9801E'
4         NEC PC-9801U Emulator 'ePC-9801U'
5         NEC PC-9801VF Emulator 'ePC-9801VF'
6         NEC PC-9801VM Emulator 'ePC-9801VM'
7         NEC PC-9801VX Emulator 'ePC-9801VX'
8         NEC PC-9801RA Emulator 'ePC-9801RA'
9         NEC PC-98XA Emulator 'ePC-98XA'
10         NEC PC-98XL Emulator 'ePC-98XL'
11         NEC PC-98RL Emulator 'ePC-98RL'
12         NEC PC-98DO Emulator 'ePC-98DO'
13
14         Author : Takeda.Toshiya
15         Date   : 2010.09.15-
16
17         [ virtual machine ]
18 */
19
20 #include "pc9801.h"
21 #include "../../emu.h"
22 #include "../device.h"
23 #include "../event.h"
24
25 #if defined(SUPPORT_OLD_BUZZER)
26 #include "../beep.h"
27 #endif
28 #include "../disk.h"
29 #if defined(USE_HARD_DISK)
30 #include "../harddisk.h"
31 #endif
32 #include "../i8237.h"
33 #include "../i8251.h"
34 #include "../i8253.h"
35 #include "../i8255.h"
36 #include "../i8259.h"
37 #if defined(HAS_I386) || defined(HAS_I486)
38 #include "../i386.h"
39 #elif defined(HAS_I86) || defined(HAS_V30)
40 #include "../i286.h"
41 #else
42 #include "../i286.h"
43 #endif
44 #include "../io.h"
45 #include "../ls244.h"
46 #include "../memory.h"
47 #include "../noise.h"
48 #include "../not.h"
49 #if !defined(SUPPORT_OLD_BUZZER)
50 #include "../pcm1bit.h"
51 #endif
52 //#include "../pcpr201.h"
53 #include "../prnfile.h"
54 #if defined(SUPPORT_SASI_IF) || defined(SUPPORT_SCSI_IF)
55 #include "../scsi_hdd.h"
56 #include "../scsi_host.h"
57 #endif
58 #include "../tms3631.h"
59 #include "../upd1990a.h"
60 #include "../upd7220.h"
61 #include "../upd765a.h"
62 #include "../ym2203.h"
63
64 #ifdef USE_DEBUGGER
65 #include "../debugger.h"
66 #endif
67
68 #if defined(SUPPORT_24BIT_ADDRESS) || defined(SUPPORT_32BIT_ADDRESS)
69 #include "cpureg.h"
70 #endif
71 #include "display.h"
72 #include "dmareg.h"
73 #include "floppy.h"
74 #include "fmsound.h"
75 #include "joystick.h"
76 #include "keyboard.h"
77 #include "membus.h"
78 #include "mouse.h"
79 #if defined(SUPPORT_SASI_IF)
80 #include "sasi.h"
81 #include "sasi_bios.h"
82 #endif
83 #if defined(SUPPORT_SCSI_IF)
84 #include "scsi.h"
85 #endif
86 #if defined(SUPPORT_IDE_IF)
87 #include "ide.h"
88 #endif
89
90 #if defined(SUPPORT_CMT_IF)
91 using PC9801::CMT;
92 #endif
93 #if defined(SUPPORT_24BIT_ADDRESS) || defined(SUPPORT_32BIT_ADDRESS)
94 using PC9801::CPUREG;
95 #endif
96 using PC9801::DISPLAY;
97 using PC9801::DMAREG;
98 using PC9801::FLOPPY;
99 using PC9801::FMSOUND;
100 using PC9801::JOYSTICK;
101 using PC9801::KEYBOARD;
102 using PC9801::MEMBUS;
103 using PC9801::MOUSE;
104 #if defined(SUPPORT_SASI_IF)
105 using PC9801::SASI;
106 using PC9801::BIOS;
107 #endif
108 #if defined(SUPPORT_SCSI_IF)
109 using PC9801::SCSI;
110 #endif
111 #if defined(SUPPORT_IDE_IF)
112 using PC9801::IDE;
113 #endif
114
115 #if defined(SUPPORT_320KB_FDD_IF)
116 #include "../pc80s31k.h"
117 #include "../z80.h"
118 #endif
119 #if defined(SUPPORT_CMT_IF)
120 #include "cmt.h"
121 #endif
122
123 #if defined(_PC98DO) || defined(_PC98DOPLUS)
124 #include "../pc80s31k.h"
125 #include "../z80.h"
126 #include "../pc8801/pc88.h"
127 #endif
128
129 #if defined(_PC98DO) || defined(_PC98DOPLUS)
130 using PC88DEV::PC88;
131 #endif
132 // ----------------------------------------------------------------------------
133 // initialize
134 // ----------------------------------------------------------------------------
135
136 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
137 {
138         // check configs
139 #if defined(_PC98DO) || defined(_PC98DOPLUS)
140         boot_mode = config.boot_mode;
141 #endif
142         int cpu_clocks = CPU_CLOCKS;
143 #if defined(PIT_CLOCK_8MHZ)
144         pit_clock_8mhz = true;
145 #else
146         pit_clock_8mhz = false;
147 #endif
148 #if defined(_PC9801E)
149         if(config.cpu_type != 0) {
150                 // 8MHz -> 5MHz
151                 cpu_clocks = 4992030;
152                 pit_clock_8mhz = false;
153         }
154 #elif defined(_PC9801VM) || defined(_PC98DO) || defined(_PC98DOPLUS) || defined(_PC9801VX) || defined(_PC98XL)
155         if(config.cpu_type != 0) {
156                 // 10MHz/16MHz -> 8MHz
157                 cpu_clocks = 7987248;
158                 pit_clock_8mhz = true;
159         }
160 #elif defined(_PC9801RA) || defined(_PC98RL)
161         if(config.cpu_type != 0) {
162                 // 20MHz -> 16MHz
163                 cpu_clocks = 15974496;
164                 pit_clock_8mhz = true;
165         }
166 #endif
167         int pit_clocks = pit_clock_8mhz ? 1996812 : 2457600;
168         
169         sound_type = config.sound_type;
170         
171         // create devices
172         first_device = last_device = NULL;
173         dummy = new DEVICE(this, emu);  // must be 1st device
174         event = new EVENT(this, emu);   // must be 2nd device
175         
176 #if defined(SUPPORT_OLD_BUZZER)
177         beep = new BEEP(this, emu);
178 #else
179         beep = new PCM1BIT(this, emu);
180 #endif
181         dma = new I8237(this, emu);
182 #if defined(SUPPORT_CMT_IF)
183         sio_cmt = new I8251(this, emu);         // for cmt
184         sio_cmt->set_device_name(_T("8251 SIO (CMT)"));
185 #endif
186         sio_rs = new I8251(this, emu);          // for rs232c
187         sio_rs->set_device_name(_T("8251 SIO (RS-232C)"));
188         sio_kbd = new I8251(this, emu);         // for keyboard
189         sio_kbd->set_device_name(_T("8251 SIO (Keyboard)"));
190         pit = new I8253(this, emu);
191 #if defined(SUPPORT_320KB_FDD_IF)
192         pio_fdd = new I8255(this, emu);         // for 320kb fdd i/f
193         pio_fdd->set_device_name(_T("8255 PIO (320KB I/F)"));
194 #endif
195         pio_mouse = new I8255(this, emu);       // for mouse
196         pio_mouse->set_device_name(_T("8255 PIO (Mouse)"));
197         pio_sys = new I8255(this, emu);         // for system port
198         pio_sys->set_device_name(_T("8255 PIO (System)"));
199         pio_prn = new I8255(this, emu);         // for printer
200         pio_prn->set_device_name(_T("8255 PIO (Printer)"));
201         pic = new I8259(this, emu);
202 #if defined(HAS_I386) || defined(HAS_I486)
203         cpu = new I386(this, emu); // 80386, 80486
204 #elif defined(HAS_I86) || defined(HAS_V30)
205         cpu = new I286(this, emu);// 8086, V30, 80286
206 #else
207         cpu = new I286(this, emu);
208 #endif  
209 #if defined(HAS_I86)
210         cpu->set_device_name(_T("CPU(i8086)"));
211 #elif defined(HAS_I386)
212         cpu->set_device_name(_T("CPU(i386)"));
213 #elif defined(HAS_I486)
214         cpu->set_device_name(_T("CPU(i486)"));
215 #elif defined(HAS_PENTIUM)
216         cpu->set_device_name(_T("CPU(Pentium)"));
217 #elif defined(HAS_V33A)
218         cpu->set_device_name(_T("CPU(V33A)"));
219 #elif defined(HAS_V30)
220         cpu->set_device_name(_T("CPU(V30)"));
221 #else
222         cpu->set_device_name(_T("CPU(i286)"));
223 #endif
224         io = new IO(this, emu);
225         rtcreg = new LS244(this, emu);
226         rtcreg->set_device_name(_T("74LS244 (RTC)"));
227         //memory = new MEMORY(this, emu);
228         memory = new MEMBUS(this, emu);
229         not_busy = new NOT(this, emu);
230         not_busy->set_device_name(_T("NOT Gate (Printer Busy)"));
231 #if defined(HAS_I86) || defined(HAS_V30)
232         not_prn = new NOT(this, emu);
233         not_prn->set_device_name(_T("NOT Gate (Printer IRQ)"));
234 #endif
235         rtc = new UPD1990A(this, emu);
236 #if defined(SUPPORT_2HD_FDD_IF)
237         fdc_2hd = new UPD765A(this, emu);
238         fdc_2hd->set_device_name(_T("uPD765A FDC (2HD I/F)"));
239 #endif
240 #if defined(SUPPORT_2DD_FDD_IF)
241         fdc_2dd = new UPD765A(this, emu);
242         fdc_2dd->set_device_name(_T("uPD765A FDC (2DD I/F)"));
243 #endif
244 #if defined(SUPPORT_2HD_2DD_FDD_IF)
245         fdc = new UPD765A(this, emu);
246         fdc->set_device_name(_T("uPD765A FDC (2HD/2DD I/F)"));
247 #endif
248         noise_seek = new NOISE(this, emu);
249         noise_head_down = new NOISE(this, emu);
250         noise_head_up = new NOISE(this, emu);
251 #if defined(SUPPORT_SASI_IF)
252         sasi_host = new SCSI_HOST(this, emu);
253         sasi_hdd = new SASI_HDD(this, emu);
254         sasi_hdd->set_device_name(_T("SASI Hard Disk Drive"));
255         sasi_hdd->scsi_id = 0;
256         sasi_hdd->bytes_per_sec = 625 * 1024; // 625KB/s
257         for(int i = 0; i < USE_HARD_DISK; i++) {
258                 sasi_hdd->set_disk_handler(i, new HARDDISK(emu));
259         }
260         sasi_hdd->set_context_interface(sasi_host);
261         sasi_host->set_context_target(sasi_hdd);
262 #endif
263 #if defined(SUPPORT_SCSI_IF)
264         scsi_host = new SCSI_HOST(this, emu);
265         for(int i = 0; i < USE_HARD_DISK; i++) {
266                 scsi_hdd[i] = new SCSI_HDD(this, emu);
267                 scsi_hdd[i]->set_device_name(_T("SCSI Hard Disk Drive #%d"), i + 1);
268                 scsi_hdd[i]->scsi_id = i;
269                 scsi_hdd[i]->set_disk_handler(0, new HARDDISK(emu));
270                 scsi_hdd[i]->set_context_interface(scsi_host);
271                 scsi_host->set_context_target(scsi_hdd[i]);
272         }
273 #endif
274         gdc_chr = new UPD7220(this, emu);
275         gdc_chr->set_device_name(_T("uPD7220 GDC (Character)"));
276         gdc_gfx = new UPD7220(this, emu);
277         gdc_gfx->set_device_name(_T("uPD7220 GDC (Graphics)"));
278         
279         if(sound_type == 0 || sound_type == 1) {
280                 opn = new YM2203(this, emu);
281 #ifdef SUPPORT_PC98_OPNA
282                 opn->set_device_name(_T("YM2608 OPNA (PC-9801-86)"));
283                 opn->is_ym2608 = true;
284 #else
285                 opn->set_device_name(_T("YM2203 OPN (PC-9801-26)"));
286                 opn->is_ym2608 = false;
287 #endif
288                 fmsound = new FMSOUND(this, emu);
289                 joystick = new JOYSTICK(this, emu);
290         } else if(sound_type == 2 || sound_type == 3) {
291                 tms3631 = new TMS3631(this, emu);
292                 tms3631->set_device_name(_T("TMS3631 SSG (PC-9801-14)"));
293                 pit_14 = new I8253(this, emu);
294                 pit_14->set_device_name(_T("8253 PIT (PC-9801-14)"));
295                 pio_14 = new I8255(this, emu);
296                 pio_14->set_device_name(_T("8255 PIO (PC-9801-14)"));
297                 maskreg_14 = new LS244(this, emu);
298                 maskreg_14->set_device_name(_T("74LS244 (PC-9801-14)"));
299         }
300         if(config.printer_type == 0) {
301                 printer = new PRNFILE(this, emu);
302 //      } else if(config.printer_type == 1) {
303 //              printer = new PCPR201(this, emu);
304         } else {
305                 printer = dummy;
306         }
307         
308 #if defined(SUPPORT_CMT_IF)
309         cmt = new CMT(this, emu);
310 #endif
311 #if defined(SUPPORT_24BIT_ADDRESS) || defined(SUPPORT_32BIT_ADDRESS)
312         cpureg = new CPUREG(this, emu);
313 #endif
314         display = new DISPLAY(this, emu);
315         dmareg = new DMAREG(this, emu);
316         floppy = new FLOPPY(this, emu);
317         keyboard = new KEYBOARD(this, emu);
318         mouse = new MOUSE(this, emu);
319 #if defined(SUPPORT_SASI_IF)
320         sasi = new SASI(this, emu);
321         sasi_bios = new BIOS(this, emu);
322 #endif
323 #if defined(SUPPORT_SCSI_IF)
324         scsi = new SCSI(this, emu);
325 #endif
326 #if defined(SUPPORT_IDE_IF)
327         ide = new IDE(this, emu);
328 #endif
329         
330 #if defined(SUPPORT_320KB_FDD_IF)
331         // 320kb fdd drives
332         pio_sub = new I8255(this, emu);
333         pio_sub->set_device_name(_T("8255 PIO (320KB FDD)"));
334         pc80s31k = new PC80S31K(this, emu);
335         pc80s31k->set_device_name(_T("PC-80S31K (320KB FDD)"));
336         fdc_sub = new UPD765A(this, emu);
337         fdc_sub->set_device_name(_T("uPD765A FDC (320KB FDD)"));
338         cpu_sub = new Z80(this, emu);
339         cpu_sub->set_device_name(_T("Z80 CPU (320KB FDD)"));
340 #endif
341         
342         /* IRQ  0  PIT
343                 1  KEYBOARD
344                 2  CRTV
345                 3  (INT0)
346                 4  RS-232C
347                 5  (INT1)
348                 6  (INT2)
349                 7  SLAVE PIC
350                 8  PRINTER
351                 9  (INT3) PC-9801-27 (SASI), PC-9801-55 (SCSI), or IDE
352                 10 (INT41) FDC (640KB I/F)
353                 11 (INT42) FDC (1MB I/F)
354                 12 (INT5) PC-9801-26(K) or PC-9801-14
355                 13 (INT6) MOUSE
356                 14 
357                 15 (RESERVED)
358         */
359         
360         // set contexts
361         event->set_context_cpu(cpu, cpu_clocks);
362 #if defined(SUPPORT_320KB_FDD_IF)
363         event->set_context_cpu(cpu_sub, 4000000);
364 #endif
365         event->set_context_sound(beep);
366         if(sound_type == 0 || sound_type == 1) {
367                 event->set_context_sound(opn);
368         } else if(sound_type == 2 || sound_type == 3) {
369                 event->set_context_sound(tms3631);
370         }
371         event->set_context_sound(noise_seek);
372         event->set_context_sound(noise_head_down);
373         event->set_context_sound(noise_head_up);
374         
375         dma->set_context_memory(memory);
376         // dma ch.0: sasi
377         // dma ch.1: memory refresh
378 #if defined(SUPPORT_2HD_FDD_IF)
379         dma->set_context_ch2(fdc_2hd);
380         dma->set_context_tc2(fdc_2hd, SIG_UPD765A_TC, 1);
381 #endif
382 #if defined(SUPPORT_2DD_FDD_IF)
383         dma->set_context_ch3(fdc_2dd);
384         dma->set_context_tc3(fdc_2dd, SIG_UPD765A_TC, 1);
385 #endif
386 #if defined(SUPPORT_2HD_2DD_FDD_IF)
387 #if !defined(SUPPORT_HIRESO)
388         dma->set_context_ch2(fdc);
389         dma->set_context_ch3(fdc);
390         dma->set_context_tc2(fdc, SIG_UPD765A_TC, 1);
391         dma->set_context_tc3(fdc, SIG_UPD765A_TC, 1);
392 #else
393         dma->set_context_ch1(fdc);
394         dma->set_context_tc1(fdc, SIG_UPD765A_TC, 1);
395 #endif
396 #endif
397 //      sio_rs->set_context_rxrdy(pic, SIG_I8259_CHIP0 | SIG_I8259_IR4, 1);
398         sio_kbd->set_context_rxrdy(pic, SIG_I8259_CHIP0 | SIG_I8259_IR1, 1);
399         pit->set_context_ch0(pic, SIG_I8259_CHIP0 | SIG_I8259_IR0, 1);
400 #if defined(SUPPORT_OLD_BUZZER)
401         // pit ch.1: memory refresh
402 #else
403         // pit ch.1: buzzer
404         pit->set_context_ch1(beep, SIG_PCM1BIT_SIGNAL, 1);
405 #endif
406         // pit ch.2: rs-232c
407         pit->set_constant_clock(0, pit_clocks);
408         pit->set_constant_clock(1, pit_clocks);
409         pit->set_constant_clock(2, pit_clocks);
410         pio_mouse->set_context_port_c(mouse, SIG_MOUSE_PORT_C, 0xf0, 0);
411 #if defined(SUPPORT_HIRESO)
412         // sysport port.c bit7,5: sysport port.b bit4,3
413         pio_sys->set_context_port_c(pio_sys, SIG_I8255_PORT_B, 0x80, -3); // SHUT0
414         pio_sys->set_context_port_c(pio_sys, SIG_I8255_PORT_B, 0x20, -2); // SHUT1
415 #endif
416         // sysport port.c bit6: printer strobe
417 #if defined(SUPPORT_OLD_BUZZER)
418         pio_sys->set_context_port_c(beep, SIG_BEEP_MUTE, 0x08, 0);
419 #else
420         pio_sys->set_context_port_c(beep, SIG_PCM1BIT_MUTE, 0x08, 0);
421 #endif
422         // sysport port.c bit2: enable txrdy interrupt
423         // sysport port.c bit1: enable txempty interrupt
424         // sysport port.c bit0: enable rxrdy interrupt
425         pio_prn->set_context_port_a(printer, SIG_PRINTER_DATA, 0xff, 0);
426         pio_prn->set_context_port_c(printer, SIG_PRINTER_STROBE, 0x80, 0);
427         if(config.printer_type == 0) {
428                 PRNFILE *prnfile = (PRNFILE *)printer;
429                 prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
430 //      } else if(config.printer_type == 1) {
431 //              PRNFILE *pcpr201 = (PCPR201 *)printer;
432 //              pcpr201->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
433         }
434         not_busy->set_context_out(pio_prn, SIG_I8255_PORT_B, 4);
435 #if defined(HAS_I86) || defined(HAS_V30)
436         pio_prn->set_context_port_c(not_prn, SIG_NOT_INPUT, 8, 0);
437         not_prn->set_context_out(pic, SIG_I8259_CHIP1 | SIG_I8259_IR0, 1);
438 #endif
439         rtcreg->set_context_output(rtc, SIG_UPD1990A_CMD, 0x07, 0);
440         rtcreg->set_context_output(rtc, SIG_UPD1990A_DIN, 0x20, 0);
441         rtcreg->set_context_output(rtc, SIG_UPD1990A_STB, 0x08, 0);
442         rtcreg->set_context_output(rtc, SIG_UPD1990A_CLK, 0x10, 0);
443         pic->set_context_cpu(cpu);
444         rtc->set_context_dout(pio_sys, SIG_I8255_PORT_B, 1);
445         
446         if(sound_type == 0 || sound_type == 1) {
447                 opn->set_context_irq(pic, SIG_I8259_CHIP1 | SIG_I8259_IR4, 1);
448                 opn->set_context_port_b(joystick, SIG_JOYSTICK_SELECT, 0xc0, 0);
449                 fmsound->set_context_opn(opn);
450                 joystick->set_context_opn(opn);
451         } else if(sound_type == 2 || sound_type == 3) {
452                 pio_14->set_context_port_a(tms3631, SIG_TMS3631_ENVELOP1, 0xff, 0);
453                 pio_14->set_context_port_b(tms3631, SIG_TMS3631_ENVELOP2, 0xff, 0);
454                 pio_14->set_context_port_c(tms3631, SIG_TMS3631_DATAREG, 0xff, 0);
455                 maskreg_14->set_context_output(tms3631, SIG_TMS3631_MASKREG, 0xff, 0);
456                 pit_14->set_constant_clock(2, 1996800 / 8);
457                 pit_14->set_context_ch2(pic, SIG_I8259_CHIP1 | SIG_I8259_IR4, 1);
458         }
459         
460 #if defined(SUPPORT_24BIT_ADDRESS) || defined(SUPPORT_32BIT_ADDRESS)
461         cpureg->set_context_cpu(cpu);
462 #endif
463         display->set_context_pic(pic);
464         display->set_context_gdc_chr(gdc_chr, gdc_chr->get_ra());
465         display->set_context_gdc_gfx(gdc_gfx, gdc_gfx->get_ra(), gdc_gfx->get_cs());
466         dmareg->set_context_dma(dma);
467         keyboard->set_context_sio(sio_kbd);
468         memory->set_context_display(display);
469         mouse->set_context_pic(pic);
470         mouse->set_context_pio(pio_mouse);
471         
472 #if defined(SUPPORT_2HD_FDD_IF)
473         fdc_2hd->set_context_irq(floppy, SIG_FLOPPY_2HD_IRQ, 1);
474         fdc_2hd->set_context_drq(floppy, SIG_FLOPPY_2HD_DRQ, 1);
475         fdc_2hd->set_context_noise_seek(noise_seek);
476         fdc_2hd->set_context_noise_head_down(noise_head_down);
477         fdc_2hd->set_context_noise_head_up(noise_head_up);
478         fdc_2hd->raise_irq_when_media_changed = true;
479         floppy->set_context_fdc_2hd(fdc_2hd);
480 #endif
481 #if defined(SUPPORT_2DD_FDD_IF)
482         fdc_2dd->set_context_irq(floppy, SIG_FLOPPY_2DD_IRQ, 1);
483         fdc_2dd->set_context_drq(floppy, SIG_FLOPPY_2DD_DRQ, 1);
484         fdc_2dd->set_context_noise_seek(noise_seek);
485         fdc_2dd->set_context_noise_head_down(noise_head_down);
486         fdc_2dd->set_context_noise_head_up(noise_head_up);
487         fdc_2dd->raise_irq_when_media_changed = true;
488         floppy->set_context_fdc_2dd(fdc_2dd);
489 #endif
490 #if defined(SUPPORT_2HD_2DD_FDD_IF)
491         fdc->set_context_irq(floppy, SIG_FLOPPY_IRQ, 1);
492         fdc->set_context_drq(floppy, SIG_FLOPPY_DRQ, 1);
493         fdc->set_context_noise_seek(noise_seek);
494         fdc->set_context_noise_head_down(noise_head_down);
495         fdc->set_context_noise_head_up(noise_head_up);
496         fdc->raise_irq_when_media_changed = true;
497         floppy->set_context_fdc(fdc);
498 #endif
499         floppy->set_context_dma(dma);
500         floppy->set_context_pic(pic);
501         
502 #if defined(SUPPORT_SASI_IF)
503 #if !defined(SUPPORT_HIRESO)
504         sasi_host->set_context_irq(pio_sys, SIG_I8255_PORT_B, 0x10);
505 #endif
506         sasi_host->set_context_irq(sasi, SIG_SASI_IRQ, 1);
507         sasi_host->set_context_drq(sasi, SIG_SASI_DRQ, 1);
508 #ifdef _PC98XA
509         dma->set_context_ch3(sasi_host);
510         dma->set_context_tc3(sasi, SIG_SASI_TC, 1);
511 #else
512         dma->set_context_ch0(sasi_host);
513         dma->set_context_tc0(sasi, SIG_SASI_TC, 1);
514 #endif
515         sasi->set_context_host(sasi_host);
516         sasi->set_context_hdd(sasi_hdd);
517         sasi->set_context_dma(dma);
518         sasi->set_context_pic(pic);
519
520         sasi_bios->set_context_sasi(sasi);
521         sasi_bios->set_context_memory(memory);
522         sasi_bios->set_context_cpu(cpu);
523         sasi_bios->set_context_pic(pic);
524         cpu->set_context_bios(sasi_bios);
525    
526 #endif
527 #if defined(SUPPORT_SCSI_IF)
528         dma->set_context_ch0(scsi);
529         scsi->set_context_dma(dma);
530         scsi->set_context_pic(pic);
531 #endif
532 #if defined(SUPPORT_IDE_IF)
533         dma->set_context_ch0(ide);
534         ide->set_context_dma(dma);
535         ide->set_context_pic(pic);
536 #endif
537         
538 #if defined(SUPPORT_CMT_IF)
539         sio_cmt->set_context_out(cmt, SIG_CMT_OUT);
540 //      sio_cmt->set_context_txrdy(cmt, SIG_CMT_TXRDY, 1);
541 //      sio_cmt->set_context_rxrdy(cmt, SIG_CMT_RXRDY, 1);
542 //      sio_cmt->set_context_txe(cmt, SIG_CMT_TXEMP, 1);
543         cmt->set_context_sio(sio_cmt);
544 #endif
545         
546         // cpu bus
547         cpu->set_context_mem(memory);
548         cpu->set_context_io(io);
549         cpu->set_context_intr(pic);
550 #ifdef SINGLE_MODE_DMA
551         cpu->set_context_dma(dma);
552 #endif
553 #ifdef USE_DEBUGGER
554         cpu->set_context_debugger(new DEBUGGER(this, emu));
555 #endif
556         
557 #if defined(SUPPORT_320KB_FDD_IF)
558         // 320kb fdd drives
559         pc80s31k->set_context_cpu(cpu_sub);
560         pc80s31k->set_context_fdc(fdc_sub);
561         pc80s31k->set_context_pio(pio_sub);
562         pio_fdd->set_context_port_a(pio_sub, SIG_I8255_PORT_A, 0xff, 0);
563         pio_fdd->set_context_port_b(pio_sub, SIG_I8255_PORT_A, 0xff, 0);
564         pio_fdd->set_context_port_c(pio_sub, SIG_I8255_PORT_C, 0x0f, 4);
565         pio_fdd->set_context_port_c(pio_sub, SIG_I8255_PORT_C, 0xf0, -4);
566         pio_fdd->clear_ports_by_cmdreg = true;
567         pio_sub->set_context_port_a(pio_fdd, SIG_I8255_PORT_B, 0xff, 0);
568         pio_sub->set_context_port_b(pio_fdd, SIG_I8255_PORT_A, 0xff, 0);
569         pio_sub->set_context_port_c(pio_fdd, SIG_I8255_PORT_C, 0x0f, 4);
570         pio_sub->set_context_port_c(pio_fdd, SIG_I8255_PORT_C, 0xf0, -4);
571         pio_sub->clear_ports_by_cmdreg = true;
572         fdc_sub->set_context_irq(cpu_sub, SIG_CPU_IRQ, 1);
573         fdc_sub->set_context_noise_seek(noise_seek);
574         fdc_sub->set_context_noise_head_down(noise_head_down);
575         fdc_sub->set_context_noise_head_up(noise_head_up);
576         cpu_sub->set_context_mem(pc80s31k);
577         cpu_sub->set_context_io(pc80s31k);
578         cpu_sub->set_context_intr(pc80s31k);
579 #ifdef USE_DEBUGGER
580         cpu_sub->set_context_debugger(new DEBUGGER(this, emu));
581 #endif
582 #endif
583         
584         // i/o bus
585         io->set_iomap_alias_rw(0x0000, pic, 0);
586         io->set_iomap_alias_rw(0x0002, pic, 1);
587         io->set_iomap_alias_rw(0x0008, pic, 2);
588         io->set_iomap_alias_rw(0x000a, pic, 3);
589         
590         io->set_iomap_alias_rw(0x0001, dma, 0x00);
591         io->set_iomap_alias_rw(0x0003, dma, 0x01);
592         io->set_iomap_alias_rw(0x0005, dma, 0x02);
593         io->set_iomap_alias_rw(0x0007, dma, 0x03);
594         io->set_iomap_alias_rw(0x0009, dma, 0x04);
595         io->set_iomap_alias_rw(0x000b, dma, 0x05);
596         io->set_iomap_alias_rw(0x000d, dma, 0x06);
597         io->set_iomap_alias_rw(0x000f, dma, 0x07);
598         io->set_iomap_alias_rw(0x0011, dma, 0x08);
599         io->set_iomap_alias_w (0x0013, dma, 0x09);
600         io->set_iomap_alias_w (0x0015, dma, 0x0a);
601         io->set_iomap_alias_w (0x0017, dma, 0x0b);
602         io->set_iomap_alias_w (0x0019, dma, 0x0c);
603         io->set_iomap_alias_rw(0x001b, dma, 0x0d);
604         io->set_iomap_alias_w (0x001d, dma, 0x0e);
605         io->set_iomap_alias_w (0x001f, dma, 0x0f);
606         io->set_iomap_single_w(0x0021, dmareg);
607         io->set_iomap_single_w(0x0023, dmareg);
608         io->set_iomap_single_w(0x0025, dmareg);
609         io->set_iomap_single_w(0x0027, dmareg);
610 #if defined(SUPPORT_24BIT_ADDRESS) || defined(SUPPORT_32BIT_ADDRESS)
611         io->set_iomap_single_w(0x0029, dmareg);
612 #endif
613 #if defined(SUPPORT_32BIT_ADDRESS)
614         io->set_iomap_single_w(0x0e05, dmareg);
615         io->set_iomap_single_w(0x0e07, dmareg);
616         io->set_iomap_single_w(0x0e09, dmareg);
617         io->set_iomap_single_w(0x0e0b, dmareg);
618 #endif
619         
620         io->set_iomap_single_w(0x0020, rtcreg);
621         
622         io->set_iomap_alias_rw(0x0030, sio_rs, 0);
623         io->set_iomap_alias_rw(0x0032, sio_rs, 1);
624         
625         io->set_iomap_alias_rw(0x0031, pio_sys, 0);
626         io->set_iomap_alias_rw(0x0033, pio_sys, 1);
627         io->set_iomap_alias_rw(0x0035, pio_sys, 2);
628         io->set_iomap_alias_w (0x0037, pio_sys, 3);
629         
630         io->set_iomap_alias_rw(0x0040, pio_prn, 0);
631         io->set_iomap_alias_rw(0x0042, pio_prn, 1);
632         io->set_iomap_alias_rw(0x0044, pio_prn, 2);
633         io->set_iomap_alias_w (0x0046, pio_prn, 3);
634         
635         io->set_iomap_alias_rw(0x0041, sio_kbd, 0);
636         io->set_iomap_alias_rw(0x0043, sio_kbd, 1);
637         
638 #if defined(SUPPORT_24BIT_ADDRESS) || defined(SUPPORT_32BIT_ADDRESS)
639         io->set_iomap_single_w(0x0050, cpureg);
640         io->set_iomap_single_w(0x0052, cpureg);
641 #endif
642         
643 #if defined(SUPPORT_320KB_FDD_IF)
644         io->set_iomap_alias_rw(0x0051, pio_fdd, 0);
645         io->set_iomap_alias_rw(0x0053, pio_fdd, 1);
646         io->set_iomap_alias_rw(0x0055, pio_fdd, 2);
647         io->set_iomap_alias_w (0x0057, pio_fdd, 3);
648 #endif
649         
650         io->set_iomap_alias_rw(0x0060, gdc_chr, 0);
651         io->set_iomap_alias_rw(0x0062, gdc_chr, 1);
652         
653         io->set_iomap_single_w(0x64, display);
654         io->set_iomap_single_w(0x68, display);
655 #if defined(SUPPORT_16_COLORS)
656         io->set_iomap_single_w(0x6a, display);
657 #endif
658         io->set_iomap_single_w(0x6c, display);
659         io->set_iomap_single_w(0x6e, display);
660         
661         io->set_iomap_single_w(0x70, display);
662         io->set_iomap_single_w(0x72, display);
663         io->set_iomap_single_w(0x74, display);
664         io->set_iomap_single_w(0x76, display);
665         io->set_iomap_single_w(0x78, display);
666         io->set_iomap_single_w(0x7a, display);
667 #if defined(SUPPORT_GRCG)
668 #if !defined(SUPPORT_HIRESO)
669         io->set_iomap_single_w(0x007c, display);
670         io->set_iomap_single_w(0x007e, display);
671 #else
672         io->set_iomap_single_w(0x00a4, display);
673         io->set_iomap_single_w(0x00a6, display);
674 #endif
675 #endif
676         
677 #if defined(SUPPORT_SASI_IF)
678         io->set_iomap_single_rw(0x0080, sasi);
679         io->set_iomap_single_rw(0x0082, sasi);
680 #endif
681 #if defined(SUPPORT_SCSI_IF)
682         io->set_iomap_single_rw(0x0cc0, scsi);
683         io->set_iomap_single_rw(0x0cc2, scsi);
684         io->set_iomap_single_rw(0x0cc4, scsi);
685 #endif
686 #if defined(SUPPORT_IDE_IF)
687         io->set_iomap_single_rw(0x0640, ide);
688         io->set_iomap_single_rw(0x0642, ide);
689         io->set_iomap_single_rw(0x0644, ide);
690         io->set_iomap_single_rw(0x0646, ide);
691         io->set_iomap_single_rw(0x0648, ide);
692         io->set_iomap_single_rw(0x064a, ide);
693         io->set_iomap_single_rw(0x064c, ide);
694         io->set_iomap_single_rw(0x064e, ide);
695         io->set_iomap_single_rw(0x074c, ide);
696         io->set_iomap_single_rw(0x074e, ide);
697 #endif
698         
699         io->set_iomap_alias_rw(0x00a0, gdc_gfx, 0);
700         io->set_iomap_alias_rw(0x00a2, gdc_gfx, 1);
701         
702 #if defined(SUPPORT_2ND_VRAM) && !defined(SUPPORT_HIRESO)
703         io->set_iomap_single_rw(0x00a4, display);
704         io->set_iomap_single_rw(0x00a6, display);
705 #endif
706         io->set_iomap_single_rw(0xa8, display);
707         io->set_iomap_single_rw(0xaa, display);
708         io->set_iomap_single_rw(0xac, display);
709         io->set_iomap_single_rw(0xae, display);
710         
711 //      io->set_iomap_single_w(0xa1, display);
712 //      io->set_iomap_single_w(0xa3, display);
713 //      io->set_iomap_single_w(0xa5, display);
714         io->set_iomap_single_rw(0xa1, display);
715         io->set_iomap_single_rw(0xa3, display);
716         io->set_iomap_single_rw(0xa5, display);
717         io->set_iomap_single_rw(0xa9, display);
718 #if defined(SUPPORT_EGC)
719         io->set_iomap_range_rw(0x04a0, 0x04af, display);
720 #endif
721         
722         io->set_iomap_alias_rw(0x0071, pit, 0);
723         io->set_iomap_alias_rw(0x0073, pit, 1);
724         io->set_iomap_alias_rw(0x0075, pit, 2);
725         io->set_iomap_alias_w (0x0077, pit, 3);
726         
727         io->set_iomap_single_rw(0x0090, floppy);
728         io->set_iomap_single_rw(0x0092, floppy);
729         io->set_iomap_single_rw(0x0094, floppy);
730         io->set_iomap_single_rw(0x0096, floppy);
731 #if defined(SUPPORT_2HD_2DD_FDD_IF)
732 #if !defined(SUPPORT_HIRESO)
733         io->set_iomap_single_rw(0x00be, floppy);
734 #else
735 //#if !defined(_PC98XA) && !defined(_PC98XL)
736         io->set_iomap_single_w (0x00be, floppy);
737 //#endif
738 #endif
739 #endif
740 #if !defined(SUPPORT_HIRESO)
741         io->set_iomap_single_rw(0x00c8, floppy);
742         io->set_iomap_single_rw(0x00ca, floppy);
743         io->set_iomap_single_rw(0x00cc, floppy);
744         io->set_iomap_single_rw(0x00ce, floppy);
745 #endif
746         
747 #if defined(SUPPORT_CMT_IF)
748         io->set_iomap_alias_rw(0x0091, sio_cmt, 0);
749         io->set_iomap_alias_rw(0x0093, sio_cmt, 1);
750         io->set_iomap_single_w(0x0095, cmt);
751         io->set_iomap_single_w(0x0097, cmt);
752 #endif
753         
754 #if defined(SUPPORT_24BIT_ADDRESS) || defined(SUPPORT_32BIT_ADDRESS)
755         io->set_iomap_single_rw(0x00f0, cpureg);
756         io->set_iomap_single_rw(0x00f2, cpureg);
757 #endif
758 #if defined(SUPPORT_32BIT_ADDRESS)
759         io->set_iomap_single_rw(0x00f6, cpureg);
760 #endif
761         
762         if(sound_type == 0 || sound_type == 1) {
763                 io->set_iomap_single_rw(0x0188, fmsound);
764                 io->set_iomap_single_rw(0x018a, fmsound);
765 #ifdef SUPPORT_PC98_OPNA
766                 io->set_iomap_single_rw(0x018c, fmsound);
767                 io->set_iomap_single_rw(0x018e, fmsound);
768                 io->set_iomap_single_rw(0xa460, fmsound);
769 #endif
770         } else if(sound_type == 2 || sound_type == 3) {
771                 io->set_iomap_alias_rw(0x0088, pio_14, 0);
772                 io->set_iomap_alias_rw(0x008a, pio_14, 1);
773                 io->set_iomap_alias_rw(0x008c, pio_14, 2);
774                 io->set_iomap_alias_w(0x008e, pio_14, 3);
775                 io->set_iovalue_single_r(0x008e, 0x08);
776                 io->set_iomap_single_w(0x0188, maskreg_14);
777                 io->set_iomap_single_w(0x018a, maskreg_14);
778                 io->set_iomap_alias_rw(0x018c, pit_14, 2);
779                 io->set_iomap_alias_w(0x018e, pit_14, 3);
780 //              io->set_iovalue_single_r(0x018e, 0x00); // INT0
781 //              io->set_iovalue_single_r(0x018e, 0x40); // INT41
782                 io->set_iovalue_single_r(0x018e, 0x80); // INT5
783 //              io->set_iovalue_single_r(0x018e, 0xc0); // INT6
784         }
785         
786 //#if !defined(SUPPORT_HIRESO)
787 //      io->set_iovalue_single_r(0x0431, 0x04); // bit2: 1 = Normal mode, 0 = Hireso mode
788 //#else
789 //      io->set_iovalue_single_r(0x0431, 0x00);
790 //#endif
791 #if defined(SUPPORT_ITF_ROM)
792         io->set_iomap_single_w(0x043d, memory);
793 #endif
794 #if !defined(SUPPORT_HIRESO)
795         io->set_iomap_single_w(0x043f, memory);
796 #endif
797 #if defined(SUPPORT_24BIT_ADDRESS) || defined(SUPPORT_32BIT_ADDRESS)
798 #if !defined(_PC98XA)
799         io->set_iomap_single_rw(0x0439, memory);
800 #endif
801 #if !defined(SUPPORT_HIRESO)
802         io->set_iomap_single_rw(0x0461, memory);
803         io->set_iomap_single_rw(0x0463, memory);
804 #else
805         io->set_iomap_single_rw(0x0091, memory);
806         io->set_iomap_single_rw(0x0093, memory);
807 #endif
808         io->set_iomap_single_r(0x0567, memory);
809 #endif
810 #if defined(SUPPORT_32BIT_ADDRESS)
811         io->set_iomap_single_w(0x053d, memory);
812 #endif
813         
814 #if !(defined(_PC9801) || defined(_PC9801E) || defined(SUPPORT_HIRESO))
815         io->set_iomap_alias_rw(0x3fd9, pit, 0);
816         io->set_iomap_alias_rw(0x3fdb, pit, 1);
817         io->set_iomap_alias_rw(0x3fdd, pit, 2);
818         io->set_iomap_alias_w (0x3fdf, pit, 3);
819 #endif
820         
821 #if !defined(SUPPORT_HIRESO)
822         io->set_iomap_alias_rw(0x7fd9, pio_mouse, 0);
823         io->set_iomap_alias_rw(0x7fdb, pio_mouse, 1);
824         io->set_iomap_alias_rw(0x7fdd, pio_mouse, 2);
825         io->set_iomap_alias_w (0x7fdf, pio_mouse, 3);
826         io->set_iomap_single_rw(0xbfdb, mouse);
827 #else
828         io->set_iomap_alias_rw(0x0061, pio_mouse, 0);
829         io->set_iomap_alias_rw(0x0063, pio_mouse, 1);
830         io->set_iomap_alias_rw(0x0065, pio_mouse, 2);
831         io->set_iomap_alias_rw(0x0067, pio_mouse, 3);
832 #endif
833         
834 #if defined(_PC98DO) || defined(_PC98DOPLUS)
835         pc88event = new EVENT(this, emu);
836         pc88event->set_device_name(_T("Event Manager (PC-8801)"));
837         pc88event->set_frames_per_sec(60);
838         pc88event->set_lines_per_frame(260);
839         
840         pc88 = new PC88(this, emu);
841         pc88->set_context_event_manager(pc88event);
842         pc88sio = new I8251(this, emu);
843         pc88sio->set_device_name(_T("8251 SIO (PC-8801)"));
844         pc88sio->set_context_event_manager(pc88event);
845         pc88pio = new I8255(this, emu);
846         pc88pio->set_device_name(_T("8255 PIO (PC-8801)"));
847         pc88pio->set_context_event_manager(pc88event);
848         pc88pcm = new PCM1BIT(this, emu);
849         pc88pcm->set_device_name(_T("1-Bit PCM Sound (PC-8801)"));
850         pc88pcm->set_context_event_manager(pc88event);
851         pc88rtc = new UPD1990A(this, emu);
852         pc88rtc->set_device_name(_T("uPD1990A RTC (PC-8801)"));
853         pc88rtc->set_context_event_manager(pc88event);
854         pc88opn1 = new YM2203(this, emu);
855 #ifdef SUPPORT_PC88_OPNA
856         pc88opn1->set_device_name(_T("YM2608 OPNA (PC-8801)"));
857         pc88opn1->is_ym2608 = true;
858 #else
859         pc88opn1->set_device_name(_T("YM2203 OPN (PC-8801)"));
860         pc88opn1->is_ym2608 = false;
861 #endif
862         pc88opn1->set_context_event_manager(pc88event);
863         pc88cpu = new Z80(this, emu);
864         pc88cpu->set_device_name(_T("Z80 CPU (PC-8801)"));
865         pc88cpu->set_context_event_manager(pc88event);
866         
867         if(config.printer_type == 0) {
868                 pc88prn = new PRNFILE(this, emu);
869                 pc88prn->set_context_event_manager(pc88event);
870 //      } else if(config.printer_type == 1) {
871 //              pc88prn = new PCPR201(this, emu);
872 //              pc88prn->set_context_event_manager(pc88event);
873         } else {
874                 pc88prn = dummy;
875         }
876         
877         pc88sub = new PC80S31K(this, emu);
878         pc88sub->set_device_name(_T("PC-80S31K (PC-8801 Sub)"));
879         pc88sub->set_context_event_manager(pc88event);
880         pc88pio_sub = new I8255(this, emu);
881         pc88pio_sub->set_device_name(_T("8255 PIO (PC-8801 Sub)"));
882         pc88pio_sub->set_context_event_manager(pc88event);
883         pc88fdc_sub = new UPD765A(this, emu);
884         pc88fdc_sub->set_device_name(_T("uPD765A FDC (PC-8801 Sub)"));
885         pc88fdc_sub->set_context_event_manager(pc88event);
886         pc88noise_seek = new NOISE(this, emu);
887         pc88noise_seek->set_context_event_manager(pc88event);
888         pc88noise_head_down = new NOISE(this, emu);
889         pc88noise_head_down->set_context_event_manager(pc88event);
890         pc88noise_head_up = new NOISE(this, emu);
891         pc88noise_head_up->set_context_event_manager(pc88event);
892         pc88cpu_sub = new Z80(this, emu);
893         pc88cpu_sub->set_device_name(_T("Z80 CPU (PC-8801 Sub)"));
894         pc88cpu_sub->set_context_event_manager(pc88event);
895         
896         pc88event->set_context_cpu(pc88cpu, (config.cpu_type == 1) ? 3993624 : 7987248);
897         pc88event->set_context_cpu(pc88cpu_sub, 3993624);
898         pc88event->set_context_sound(pc88opn1);
899         pc88event->set_context_sound(pc88pcm);
900         pc88event->set_context_sound(pc88noise_seek);
901         pc88event->set_context_sound(pc88noise_head_down);
902         pc88event->set_context_sound(pc88noise_head_up);
903         
904         pc88->set_context_cpu(pc88cpu);
905         pc88->set_context_opn1(pc88opn1);
906         pc88->set_context_pcm(pc88pcm);
907         pc88->set_context_pio(pc88pio);
908         pc88->set_context_prn(pc88prn);
909         pc88->set_context_rtc(pc88rtc);
910         pc88->set_context_sio(pc88sio);
911         pc88cpu->set_context_mem(pc88);
912         pc88cpu->set_context_io(pc88);
913         pc88cpu->set_context_intr(pc88);
914 #ifdef USE_DEBUGGER
915         pc88cpu->set_context_debugger(new DEBUGGER(this, emu));
916 #endif
917         pc88opn1->set_context_irq(pc88, SIG_PC88_OPN1_IRQ, 1);
918         pc88sio->set_context_rxrdy(pc88, SIG_PC88_USART_IRQ, 1);
919         pc88sio->set_context_out(pc88, SIG_PC88_USART_OUT);
920         
921         pc88sub->set_context_cpu(pc88cpu_sub);
922         pc88sub->set_context_fdc(pc88fdc_sub);
923         pc88sub->set_context_pio(pc88pio_sub);
924         pc88pio->set_context_port_a(pc88pio_sub, SIG_I8255_PORT_B, 0xff, 0);
925         pc88pio->set_context_port_b(pc88pio_sub, SIG_I8255_PORT_A, 0xff, 0);
926         pc88pio->set_context_port_c(pc88pio_sub, SIG_I8255_PORT_C, 0x0f, 4);
927         pc88pio->set_context_port_c(pc88pio_sub, SIG_I8255_PORT_C, 0xf0, -4);
928         pc88pio->clear_ports_by_cmdreg = true;
929         pc88pio_sub->set_context_port_a(pc88pio, SIG_I8255_PORT_B, 0xff, 0);
930         pc88pio_sub->set_context_port_b(pc88pio, SIG_I8255_PORT_A, 0xff, 0);
931         pc88pio_sub->set_context_port_c(pc88pio, SIG_I8255_PORT_C, 0x0f, 4);
932         pc88pio_sub->set_context_port_c(pc88pio, SIG_I8255_PORT_C, 0xf0, -4);
933         pc88pio_sub->clear_ports_by_cmdreg = true;
934         pc88fdc_sub->set_context_irq(pc88cpu_sub, SIG_CPU_IRQ, 1);
935         pc88fdc_sub->set_context_noise_seek(pc88noise_seek);
936         pc88fdc_sub->set_context_noise_head_down(pc88noise_head_down);
937         pc88fdc_sub->set_context_noise_head_up(pc88noise_head_up);
938         pc88cpu_sub->set_context_mem(pc88sub);
939         pc88cpu_sub->set_context_io(pc88sub);
940         pc88cpu_sub->set_context_intr(pc88sub);
941 #ifdef USE_DEBUGGER
942         pc88cpu_sub->set_context_debugger(new DEBUGGER(this, emu));
943 #endif
944 #endif
945
946         // initialize all devices
947 #if defined(__GIT_REPO_VERSION)
948         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
949 #endif
950         for(DEVICE* device = first_device; device; device = device->next_device) {
951                 device->initialize();
952         }
953         
954 #if defined(_PC9801) || defined(_PC9801E)
955         fdc_2hd->get_disk_handler(0)->drive_num = 0;
956         fdc_2hd->get_disk_handler(1)->drive_num = 1;
957         fdc_2dd->get_disk_handler(0)->drive_num = 2;
958         fdc_2dd->get_disk_handler(1)->drive_num = 3;
959         fdc_sub->get_disk_handler(0)->drive_num = 4;
960         fdc_sub->get_disk_handler(1)->drive_num = 5;
961 #elif defined(_PC9801VF) || defined(_PC9801U)
962         fdc_2dd->get_disk_handler(0)->drive_num = 0;
963         fdc_2dd->get_disk_handler(1)->drive_num = 1;
964 #elif defined(_PC98DO) || defined(_PC98DOPLUS)
965         fdc->get_disk_handler(0)->drive_num = 0;
966         fdc->get_disk_handler(1)->drive_num = 1;
967         pc88fdc_sub->get_disk_handler(0)->drive_num = 2;
968         pc88fdc_sub->get_disk_handler(1)->drive_num = 3;
969 #else
970         fdc->get_disk_handler(0)->drive_num = 0;
971         fdc->get_disk_handler(1)->drive_num = 1;
972 #endif
973 #if defined(USE_HARD_DISK)
974         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
975                 if(!(config.last_hard_disk_path[drv][0] != _T('\0') && FILEIO::IsFileExisting(config.last_hard_disk_path[drv]))) {
976 #if defined(SUPPORT_SASI_IF)
977                         create_local_path(config.last_hard_disk_path[drv], _MAX_PATH, _T("SASI%d.DAT"), drv);
978 #endif
979 #if defined(SUPPORT_SCSI_IF)
980                         create_local_path(config.last_hard_disk_path[drv], _MAX_PATH, _T("SCSI%d.DAT"), drv);
981 #endif
982 #if defined(SUPPORT_IDE_IF)
983                         create_local_path(config.last_hard_disk_path[drv], _MAX_PATH, _T("IDE%d.DAT"), drv);
984 #endif
985                 }
986         }
987 #endif
988 }
989
990 VM::~VM()
991 {
992         // delete all devices
993         for(DEVICE* device = first_device; device;) {
994                 DEVICE *next_device = device->next_device;
995                 device->release();
996                 delete device;
997                 device = next_device;
998         }
999 }
1000
1001 DEVICE* VM::get_device(int id)
1002 {
1003         for(DEVICE* device = first_device; device; device = device->next_device) {
1004                 if(device->this_device_id == id) {
1005                         return device;
1006                 }
1007         }
1008         return NULL;
1009 }
1010
1011 // ----------------------------------------------------------------------------
1012 // drive virtual machine
1013 // ----------------------------------------------------------------------------
1014
1015 void VM::reset()
1016 {
1017         // reset all devices
1018         for(DEVICE* device = first_device; device; device = device->next_device) {
1019                 device->reset();
1020         }
1021 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1022         for(DEVICE* device = first_device; device; device = device->next_device) {
1023                 device->reset();
1024         }
1025 #endif
1026         
1027         // initial device settings
1028         uint8_t port_a, port_b, port_c;
1029         
1030         port_a  = 0xf0; // Clear mouse buttons and counters
1031         port_b  = 0x00;
1032 #if defined(_PC98XA)
1033         port_b |= (uint8_t)((RAM_SIZE - 0x100000) / 0x40000);
1034 #else
1035 #if defined(SUPPORT_HIRESO)
1036         port_b |= 0x80; // DIP SW 1-4, 1 = External FDD #3/#4, 0 = #1/#2
1037 #endif
1038         port_b |= 0x40; // DIP SW 3-6, 1 = Enable internal RAM 80000h-9FFFFh
1039 #if defined(_PC98RL)
1040         port_b |= 0x10; // DIP SW 3-3, 1 = DMA ch.0 for SASI-HDD
1041 #endif
1042 #if defined(USE_CPU_TYPE)
1043         if(config.cpu_type != 0) {
1044 #if !defined(SUPPORT_HIRESO)
1045                 port_b |= 0x02; // SPDSW, 1 = 10MHz, 0 = 12MHz
1046 #else
1047                 port_b |= 0x01; // SPDSW, 1 = 8/16MHz, 0 = 10/20MHz
1048 #endif
1049         }
1050 #endif
1051 #endif
1052         port_c  = 0x00;
1053 #if defined(SUPPORT_HIRESO)
1054 //      port_c |= 0x08; // MODSW, 1 = Normal Mode, 0 = Hirezo Mode
1055 #endif
1056 #if defined(HAS_V30) || defined(HAS_V33)
1057         port_c |= 0x04; // DIP SW 3-8, 1 = V30, 0 = 80x86
1058 #endif
1059         pio_mouse->write_signal(SIG_I8255_PORT_A, port_a, 0xff);
1060         pio_mouse->write_signal(SIG_I8255_PORT_B, port_b, 0xff);
1061         pio_mouse->write_signal(SIG_I8255_PORT_C, port_c, 0xff);
1062         
1063         port_a  = 0x00;
1064         port_a |= 0x80; // DIP SW 2-8, 1 = GDC 2.5MHz, 0 = GDC 5MHz
1065         port_a |= 0x40; // DIP SW 2-7, 1 = Do not control FD motor
1066         port_a |= 0x20; // DIP SW 2-6, 1 = Enable internal HD
1067 //      port_a |= 0x10; // DIP SW 2-5, 1 = Initialize emory switch
1068 //      port_a |= 0x08; // DIP SW 2-4, 1 = 20 lines, 0 = 25 lines
1069 //      port_a |= 0x04; // DIP SW 2-3, 1 = 40 columns, 0 = 80 columns
1070         port_a |= 0x02; // DIP SW 2-2, 1 = BASIC mode, 0 = Terminal mode
1071         port_a |= 0x01; // DIP SW 2-1, 1 = Normal mode, 0 = LT mode
1072         port_b  = 0x00;
1073         port_b |= 0x80; // RS-232C CI#, 1 = OFF
1074         port_b |= 0x40; // RS-232C CS#, 1 = OFF
1075         port_b |= 0x20; // RS-232C CD#, 1 = OFF
1076 #if !defined(SUPPORT_HIRESO)
1077 //      port_b |= 0x10; // INT3, 1 = Active, 0 = Inactive
1078         port_b |= 0x08; // DIP SW 1-1, 1 = Hiresolution CRT, 0 = Standard CRT
1079 #else
1080         port_b |= 0x10; // SHUT0
1081         port_b |= 0x08; // SHUT1
1082 #endif
1083 //      port_b |= 0x04; // IMCK, 1 = Parity error occurs in internal RAM
1084 //      port_b |= 0x02; // EMCK, 1 = Parity error occurs in external RAM
1085 //      port_b |= 0x01; // CDAT
1086         port_c  = 0x00;
1087         port_c |= 0x80; // SHUT0
1088         port_c |= 0x40; // PSTBM, 1 = Mask printer's PSTB#
1089         port_c |= 0x20; // SHUT1
1090         port_c |= 0x10; // MCHKEN, 1 = Enable parity check for RAM
1091         port_c |= 0x08; // BUZ, 1 = Stop buzzer
1092         port_c |= 0x04; // TXRE, 1 = Enable IRQ from RS-232C 8251A TXRDY
1093         port_c |= 0x02; // TXEE, 1 = Enable IRQ from RS-232C 8251A TXEMPTY
1094         port_c |= 0x01; // RXRE, 1 = Enable IRQ from RS-232C 8251A RXRDY
1095         pio_sys->write_signal(SIG_I8255_PORT_A, port_a, 0xff);
1096         pio_sys->write_signal(SIG_I8255_PORT_B, port_b, 0xff);
1097         pio_sys->write_signal(SIG_I8255_PORT_C, port_c, 0xff);
1098         
1099         port_b  = 0x00;
1100 #if defined(_PC9801)
1101 //      port_b |= 0x80; // TYP1, 0 = PC-9801 (first)
1102 //      port_b |= 0x40; // TYP0, 0
1103 #elif defined(_PC9801U)
1104         port_b |= 0x80; // TYP1, 1 = PC-9801U
1105         port_b |= 0x40; // TYP0, 1
1106 #else
1107         port_b |= 0x80; // TYP1, 1 = Other PC-9801 series
1108 //      port_b |= 0x40; // TYP0, 0
1109 #endif
1110         if(pit_clock_8mhz) {
1111                 port_b |= 0x20; // MOD, 1 = System clock 8MHz, 0 = 5/10MHz
1112         }
1113         port_b |= 0x10; // DIP SW 1-3, 1 = Don't use LCD
1114 #if !defined(SUPPORT_16_COLORS)
1115         port_b |= 0x08; // DIP SW 1-8, 1 = Standard graphic mode, 0 = Enhanced graphic mode
1116 #endif
1117         port_b |= 0x04; // Printer BUSY#, 1 = Inactive, 0 = Active (BUSY)
1118 #if defined(HAS_V30) || defined(HAS_V33)
1119         port_b |= 0x02; // CPUT, 1 = V30/V33, 0 = 80x86
1120 #endif
1121 #if defined(_PC9801VF) || defined(_PC9801U)
1122         port_b |= 0x01; // VF, 1 = PC-9801VF/U
1123 #endif
1124         pio_prn->write_signal(SIG_I8255_PORT_B, port_b, 0xff);
1125         
1126 #if defined(SUPPORT_320KB_FDD_IF)
1127         pio_fdd->write_signal(SIG_I8255_PORT_A, 0xff, 0xff);
1128         pio_fdd->write_signal(SIG_I8255_PORT_B, 0xff, 0xff);
1129         pio_fdd->write_signal(SIG_I8255_PORT_C, 0xff, 0xff);
1130 #endif
1131 #if defined(SUPPORT_2DD_FDD_IF)
1132         fdc_2dd->write_signal(SIG_UPD765A_FREADY, 1, 1);        // 2DD FDC RDY is pulluped
1133 #endif
1134         
1135         if(sound_type == 0 || sound_type == 1) {
1136 //              opn->write_signal(SIG_YM2203_PORT_A, 0x3f, 0xff);       // PC-9801-26(K) INT0
1137 //              opn->write_signal(SIG_YM2203_PORT_A, 0xbf, 0xff);       // PC-9801-26(K) INT41
1138                 opn->write_signal(SIG_YM2203_PORT_A, 0xff, 0xff);       // PC-9801-26(K) INT5
1139 //              opn->write_signal(SIG_YM2203_PORT_A, 0x7f, 0xff);       // PC-9801-26(K) INT6
1140         }
1141         
1142 #if defined(SUPPORT_OLD_BUZZER)
1143         beep->write_signal(SIG_BEEP_ON, 1, 1);
1144         beep->write_signal(SIG_BEEP_MUTE, 1, 1);
1145 #else
1146         beep->write_signal(SIG_PCM1BIT_ON, 1, 1);
1147         beep->write_signal(SIG_PCM1BIT_MUTE, 1, 1);
1148 #endif
1149         
1150 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1151         pc88opn1->set_reg(0x29, 3); // for Misty Blue
1152         pc88pio->write_signal(SIG_I8255_PORT_C, 0, 0xff);
1153         pc88pio_sub->write_signal(SIG_I8255_PORT_C, 0, 0xff);
1154 #endif
1155 }
1156
1157 void VM::run()
1158 {
1159 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1160         if(boot_mode != 0) {
1161                 pc88event->drive();
1162         } else
1163 #endif
1164         event->drive();
1165 }
1166
1167 double VM::get_frame_rate()
1168 {
1169 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1170         if(config.boot_mode != 0) {
1171                 return pc88event->get_frame_rate();
1172         } else
1173 #endif
1174         return event->get_frame_rate();
1175 }
1176
1177 // ----------------------------------------------------------------------------
1178 // debugger
1179 // ----------------------------------------------------------------------------
1180
1181 #ifdef USE_DEBUGGER
1182 DEVICE *VM::get_cpu(int index)
1183 {
1184 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1185         if(index == 0 && boot_mode == 0) {
1186                 return cpu;
1187         } else if(index == 1 && boot_mode != 0) {
1188                 return pc88cpu;
1189         } else if(index == 2 && boot_mode != 0) {
1190                 return pc88cpu_sub;
1191         }
1192 #else
1193         if(index == 0) {
1194                 return cpu;
1195 #if defined(SUPPORT_320KB_FDD_IF)
1196         } else if(index == 1) {
1197                 return cpu_sub;
1198 #endif
1199         }
1200 #endif
1201         return NULL;
1202 }
1203 #endif
1204
1205 // ----------------------------------------------------------------------------
1206 // draw screen
1207 // ----------------------------------------------------------------------------
1208
1209 void VM::draw_screen()
1210 {
1211 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1212         if(boot_mode != 0) {
1213                 pc88->draw_screen();
1214         } else
1215 #endif
1216         display->draw_screen();
1217 }
1218
1219 // ----------------------------------------------------------------------------
1220 // soud manager
1221 // ----------------------------------------------------------------------------
1222
1223 void VM::initialize_sound(int rate, int samples)
1224 {
1225         // init sound manager
1226         event->initialize_sound(rate, samples);
1227         
1228         // init sound gen
1229 #if defined(SUPPORT_OLD_BUZZER)
1230         beep->initialize_sound(rate, 2400, 8000);
1231 #else
1232         beep->initialize_sound(rate, 8000);
1233 #endif
1234         if(sound_type == 0 || sound_type == 1) {
1235                 if(opn->is_ym2608) {
1236                         opn->initialize_sound(rate, 7987248, samples, 0, 0);
1237                 } else {
1238                         opn->initialize_sound(rate, 3993624, samples, 0, 0);
1239                 }
1240         } else if(sound_type == 2 || sound_type == 3) {
1241                 tms3631->initialize_sound(rate, 8000);
1242         }
1243         
1244 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1245         // init sound manager
1246         pc88event->initialize_sound(rate, samples);
1247         
1248         // init sound gen
1249         if(pc88opn1->is_ym2608) {
1250                 pc88opn1->initialize_sound(rate, 7987248, samples, 0, 0);
1251         } else {
1252                 pc88opn1->initialize_sound(rate, 3993624, samples, 0, 0);
1253         }
1254         pc88pcm->initialize_sound(rate, 8000);
1255 #endif
1256 }
1257
1258 uint16_t* VM::create_sound(int* extra_frames)
1259 {
1260 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1261         if(boot_mode != 0) {
1262                 return pc88event->create_sound(extra_frames);
1263         } else
1264 #endif
1265         return event->create_sound(extra_frames);
1266 }
1267
1268 int VM::get_sound_buffer_ptr()
1269 {
1270 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1271         if(boot_mode != 0) {
1272                 return pc88event->get_sound_buffer_ptr();
1273         } else
1274 #endif
1275         return event->get_sound_buffer_ptr();
1276 }
1277
1278 #ifdef USE_SOUND_VOLUME
1279 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
1280 {
1281         if(ch-- == 0) {
1282                 if(sound_type == 0 || sound_type == 1) {
1283                         opn->set_volume(0, decibel_l, decibel_r);
1284                 }
1285         } else if(ch-- == 0) {
1286                 if(sound_type == 0 || sound_type == 1) {
1287                         opn->set_volume(1, decibel_l, decibel_r);
1288                 }
1289 #if defined(SUPPORT_PC98_OPNA)
1290         } else if(ch-- == 0) {
1291                 if(sound_type == 0 || sound_type == 1) {
1292                         opn->set_volume(2, decibel_l, decibel_r);
1293                 }
1294         } else if(ch-- == 0) {
1295                 if(sound_type == 0 || sound_type == 1) {
1296                         opn->set_volume(3, decibel_l, decibel_r);
1297                 }
1298 #endif
1299         } else if(ch-- == 0) {
1300                 if(sound_type == 2 || sound_type == 3) {
1301                         tms3631->set_volume(0, decibel_l, decibel_r);
1302                 }
1303         } else if(ch-- == 0) {
1304                 beep->set_volume(0, decibel_l, decibel_r);
1305 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1306         } else if(ch-- == 0) {
1307                 pc88opn1->set_volume(0, decibel_l, decibel_r);
1308         } else if(ch-- == 0) {
1309                 pc88opn1->set_volume(1, decibel_l, decibel_r);
1310 #if defined(SUPPORT_PC88_OPNA)
1311         } else if(ch-- == 0) {
1312                 pc88opn1->set_volume(2, decibel_l, decibel_r);
1313         } else if(ch-- == 0) {
1314                 pc88opn1->set_volume(3, decibel_l, decibel_r);
1315 #endif
1316         } else if(ch-- == 0) {
1317                 pc88pcm->set_volume(0, decibel_l, decibel_r);
1318 #endif
1319         } else if(ch-- == 0) {
1320                 noise_seek->set_volume(0, decibel_l, decibel_r);
1321                 noise_head_down->set_volume(0, decibel_l, decibel_r);
1322                 noise_head_up->set_volume(0, decibel_l, decibel_r);
1323 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1324                 pc88noise_seek->set_volume(0, decibel_l, decibel_r);
1325                 pc88noise_head_down->set_volume(0, decibel_l, decibel_r);
1326                 pc88noise_head_up->set_volume(0, decibel_l, decibel_r);
1327 #endif
1328         }
1329 }
1330 #endif
1331
1332 // ----------------------------------------------------------------------------
1333 // notify key
1334 // ----------------------------------------------------------------------------
1335
1336 void VM::key_down(int code, bool repeat)
1337 {
1338 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1339         if(boot_mode != 0) {
1340                 pc88->key_down(code, repeat);
1341         } else
1342 #endif
1343         keyboard->key_down(code, repeat);
1344 }
1345
1346 void VM::key_up(int code)
1347 {
1348 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1349         if(boot_mode != 0) {
1350 //              pc88->key_up(code);
1351         } else
1352 #endif
1353         keyboard->key_up(code);
1354 }
1355
1356 bool VM::get_caps_locked()
1357 {
1358 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1359         if(boot_mode != 0) {
1360                 return pc88->get_caps_locked();
1361         } else
1362 #endif
1363         return keyboard->get_caps_locked();
1364 }
1365
1366 bool VM::get_kana_locked()
1367 {
1368 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1369         if(boot_mode != 0) {
1370                 return pc88->get_kana_locked();
1371         } else
1372 #endif
1373         return keyboard->get_kana_locked();
1374 }
1375
1376 // ----------------------------------------------------------------------------
1377 // user interface
1378 // ----------------------------------------------------------------------------
1379
1380 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
1381 {
1382         UPD765A *controller = get_floppy_disk_controller(drv);
1383         
1384         if(controller != NULL) {
1385                 controller->open_disk(drv & 1, file_path, bank);
1386         }
1387 }
1388
1389 void VM::close_floppy_disk(int drv)
1390 {
1391         UPD765A *controller = get_floppy_disk_controller(drv);
1392         
1393         if(controller != NULL) {
1394                 controller->close_disk(drv & 1);
1395         }
1396 }
1397
1398 bool VM::is_floppy_disk_inserted(int drv)
1399 {
1400         DISK *handler = get_floppy_disk_handler(drv);
1401         
1402         if(handler != NULL) {
1403                 return handler->inserted;
1404         }
1405         return false;
1406 }
1407
1408 void VM::is_floppy_disk_protected(int drv, bool value)
1409 {
1410         DISK *handler = get_floppy_disk_handler(drv);
1411         
1412         if(handler != NULL) {
1413                 handler->write_protected = value;
1414         }
1415 }
1416
1417 bool VM::is_floppy_disk_protected(int drv)
1418 {
1419         DISK *handler = get_floppy_disk_handler(drv);
1420         
1421         if(handler != NULL) {
1422                 return handler->write_protected;
1423         }
1424         return false;
1425 }
1426
1427 uint32_t VM::is_floppy_disk_accessed()
1428 {
1429         uint32_t status = 0;
1430         
1431 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1432         if(boot_mode != 0) {
1433                 status = pc88fdc_sub->read_signal(0) & 3;
1434         } else {
1435                 status = fdc->read_signal(0) & 3;
1436         }
1437 #else
1438         for(int drv = 0; drv < USE_FLOPPY_DISK; drv += 2) {
1439                 UPD765A *controller = get_floppy_disk_controller(drv);
1440                 
1441                 if(controller != NULL) {
1442                         status |= (controller->read_signal(0) & 3) << drv;
1443                 }
1444         }
1445 #endif
1446         return status;
1447 }
1448
1449 UPD765A *VM::get_floppy_disk_controller(int drv)
1450 {
1451 #if defined(_PC9801) || defined(_PC9801E)
1452         if(drv == 0 || drv == 1) {
1453                 return fdc_2hd;
1454         } else if(drv == 2 || drv == 3) {
1455                 return fdc_2dd;
1456         } else if(drv == 4 || drv == 5) {
1457                 return fdc_sub;
1458         }
1459 #elif defined(_PC9801VF) || defined(_PC9801U)
1460         if(drv == 0 || drv == 1) {
1461                 return fdc_2dd;
1462         }
1463 #elif defined(_PC98DO) || defined(_PC98DOPLUS)
1464         if(drv == 0 || drv == 1) {
1465                 return fdc;
1466         } else if(drv == 2 || drv == 3) {
1467                 return pc88fdc_sub;
1468         }
1469 #else
1470         if(drv == 0 || drv == 1) {
1471                 return fdc;
1472         }
1473 #endif
1474         return NULL;
1475 }
1476
1477 DISK *VM::get_floppy_disk_handler(int drv)
1478 {
1479         UPD765A *controller = get_floppy_disk_controller(drv);
1480         
1481         if(controller != NULL) {
1482                 return controller->get_disk_handler(drv & 1);
1483         }
1484         return NULL;
1485 }
1486
1487 #if defined(USE_HARD_DISK)
1488 void VM::open_hard_disk(int drv, const _TCHAR* file_path)
1489 {
1490         if(drv < USE_HARD_DISK) {
1491 #if defined(SUPPORT_SASI_IF)
1492                 sasi_hdd->open(drv, file_path, 256);
1493 #endif
1494 #if defined(SUPPORT_SCSI_IF)
1495                 scsi_hdd[drv]->open(0, file_path, 512);
1496 #endif
1497 #if defined(SUPPORT_IDE_IF)
1498                 ide_hdd[drv]->open(0, file_path, 512);
1499 #endif
1500         }
1501 }
1502
1503 void VM::close_hard_disk(int drv)
1504 {
1505         if(drv < USE_HARD_DISK) {
1506 #if defined(SUPPORT_SASI_IF)
1507                 sasi_hdd->close(drv);
1508 #endif
1509 #if defined(SUPPORT_SCSI_IF)
1510                 scsi_hdd[drv]->close(0);
1511 #endif
1512 #if defined(SUPPORT_IDE_IF)
1513                 ide_hdd[drv]->close(0);
1514 #endif
1515         }
1516 }
1517
1518 bool VM::is_hard_disk_inserted(int drv)
1519 {
1520         if(drv < USE_HARD_DISK) {
1521 #if defined(SUPPORT_SASI_IF)
1522                 return sasi_hdd->mounted(drv);
1523 #endif
1524 #if defined(SUPPORT_SCSI_IF)
1525                 return scsi_hdd[drv]->mounted(0);
1526 #endif
1527 #if defined(SUPPORT_IDE_IF)
1528                 return ide_hdd[drv]->mounted(0);
1529 #endif
1530         }
1531         return false;
1532 }
1533
1534 uint32_t VM::is_hard_disk_accessed()
1535 {
1536         uint32_t status = 0;
1537         
1538         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
1539 #if defined(SUPPORT_SASI_IF)
1540                 if(sasi_hdd->accessed(drv)) {
1541                         status |= 1 << drv;
1542                 }
1543 #endif
1544 #if defined(SUPPORT_SCSI_IF)
1545                 if(scsi_hdd[drv]->accessed(0)) {
1546                         status |= 1 << drv;
1547                 }
1548 #endif
1549 #if defined(SUPPORT_IDE_IF)
1550                 if(ide_hdd[drv]->accessed(0)) {
1551                         status |= 1 << drv;
1552                 }
1553 #endif
1554         }
1555         return status;
1556 }
1557 #endif
1558
1559 #if defined(USE_TAPE)
1560 void VM::play_tape(int drv, const _TCHAR* file_path)
1561 {
1562 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1563         pc88->play_tape(file_path);
1564 #else
1565         cmt->play_tape(file_path);
1566 #endif
1567 }
1568
1569 void VM::rec_tape(int drv, const _TCHAR* file_path)
1570 {
1571 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1572         pc88->rec_tape(file_path);
1573 #else
1574         cmt->rec_tape(file_path);
1575 #endif
1576 }
1577
1578 void VM::close_tape(int drv)
1579 {
1580 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1581         pc88->close_tape();
1582 #else
1583         cmt->close_tape();
1584 #endif
1585 }
1586
1587 bool VM::is_tape_inserted(int drv)
1588 {
1589 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1590         return pc88->is_tape_inserted();
1591 #else
1592         return cmt->is_tape_inserted();
1593 #endif
1594 }
1595 #endif
1596
1597 bool VM::is_frame_skippable()
1598 {
1599 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1600         if(boot_mode != 0) {
1601 //              return pc88event->is_frame_skippable();
1602                 return pc88->is_frame_skippable();
1603         } else
1604 #endif
1605         return event->is_frame_skippable();
1606 }
1607
1608 void VM::update_config()
1609 {
1610 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1611         if(boot_mode != config.boot_mode) {
1612                 // boot mode is changed !!!
1613                 boot_mode = config.boot_mode;
1614                 reset();
1615         } else
1616 #endif
1617         for(DEVICE* device = first_device; device; device = device->next_device) {
1618                 device->update_config();
1619         }
1620 }
1621
1622 #define STATE_VERSION   14
1623
1624 bool VM::process_state(FILEIO* state_fio, bool loading)
1625 {
1626         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
1627                 return false;
1628         }
1629         for(DEVICE* device = first_device; device; device = device->next_device) {
1630                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
1631                 // const char *name = typeid(*device).name();
1632                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
1633                 const char *name = device->get_device_name();
1634                 int len = strlen(name);
1635                 if(!state_fio->StateCheckInt32(len)) {
1636                         if(loading) {
1637                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
1638                         }
1639                         return false;
1640                 }
1641                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
1642                         if(loading) {
1643                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
1644                         }
1645                         return false;
1646                 }
1647                 if(!device->process_state(state_fio, loading)) {
1648                         if(loading) {
1649                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
1650                         }
1651                         return false;
1652                 }
1653         }
1654         state_fio->StateValue(pit_clock_8mhz);
1655 #if defined(_PC98DO) || defined(_PC98DOPLUS)
1656         state_fio->StateValue(boot_mode);
1657 #endif
1658         state_fio->StateValue(sound_type);
1659         return true;
1660 }