OSDN Git Service

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