OSDN Git Service

b5269c6b21bf648f866dca3754b21616ace43fb2
[csp-qt/common_source_project-fm7.git] / source / src / vm / fmtowns / fmtowns.cpp
1 /*
2         FUJITSU FM-Towns Emulator 'eFMTowns'
3
4         Author : Kyuma Ohta <whatisthis.sowhat _at_ gmail.com>
5         Date   : 2016.12.28 -
6
7         [ virtual machine ]
8         History:
9                 2016-12-28 Copy from eFMR-50.
10 */
11
12 #include "fmtowns.h"
13 #include "../../emu.h"
14 #include "../device.h"
15 #include "../event.h"
16
17 //#include "../hd46505.h"
18 #include "../i8251.h"
19 #include "../i8253.h"
20 #include "../i8259.h"
21
22 #include "../i386_np21.h"
23 //#include "../i386.h"
24
25 #include "../io.h"
26 #include "../mb8877.h"
27 #include "../msm58321.h"
28 #include "../noise.h"
29 #include "../pcm1bit.h"
30 #include "../harddisk.h"
31 #include "../scsi_hdd.h"
32 #include "./towns_scsi_host.h"
33 #include "../upd71071.h"
34
35 #include "./cdrom.h"
36 #include "./crtc.h"
37 #include "./dictionary.h"
38 #include "./dmac.h"
39 #include "./towns_memory.h"
40 #include "./sprite.h"
41 #include "./sysrom.h"
42 #include "./vram.h"
43
44 // Electric Volume
45 #include "mb87078.h"
46 //YM-2612 "OPN2"
47 //#include "../ym2612.h"
48 //RF5C68 PCM
49 #include "rf5c68.h"
50 //AD7820 ADC
51 #include "ad7820kr.h"
52 #include "ym2612.h"
53 // 80387?
54
55 #ifdef USE_DEBUGGER
56 #include "../debugger.h"
57 #endif
58
59 #include "./adpcm.h"
60 //#include "./cdc.h"
61 #include "./floppy.h"
62 #include "./fontroms.h"
63 #include "./joystick.h"
64 #include "./joypad_2btn.h"
65 #include "./joypad_6btn.h"
66 #include "./keyboard.h"
67 #include "./mouse.h"
68 #include "./msdosrom.h"
69 #include "./scsi.h"
70 #include "./serialrom.h"
71 #include "./timer.h"
72 #include "./iccard.h"
73
74 #include "./planevram.h"
75
76 // ----------------------------------------------------------------------------
77 // initialize
78 // ----------------------------------------------------------------------------
79 using FMTOWNS::ADPCM;
80 //using FMTOWNS::CDC;
81 using FMTOWNS::DICTIONARY;
82 using FMTOWNS::FLOPPY;
83 using FMTOWNS::FONT_ROMS;
84 using FMTOWNS::JOYSTICK;
85 using FMTOWNS::JOYPAD_2BTN;
86 using FMTOWNS::JOYPAD_6BTN;
87
88 using FMTOWNS::KEYBOARD;
89 using FMTOWNS::MOUSE;
90 using FMTOWNS::MSDOSROM;
91 using FMTOWNS::SCSI;
92 using FMTOWNS::SERIAL_ROM;
93 using FMTOWNS::SYSROM;
94 using FMTOWNS::TIMER;
95 using FMTOWNS::TOWNS_ICCARD;
96
97 using FMTOWNS::TOWNS_CDROM;
98 using FMTOWNS::TOWNS_CRTC;
99 using FMTOWNS::TOWNS_DMAC;
100 using FMTOWNS::TOWNS_MEMORY;
101 using FMTOWNS::TOWNS_SCSI_HOST;
102 using FMTOWNS::TOWNS_SPRITE;
103 using FMTOWNS::TOWNS_VRAM;
104 using FMTOWNS::PLANEVRAM;
105
106
107 VM::VM(EMU_TEMPLATE* parent_emu) : VM_TEMPLATE(parent_emu)
108 {
109 /*
110         Machine ID & CPU ID
111
112         FMR-50FD/HD/LT  0xF8
113         FMR-50FX/HX     0xE0
114         FMR-50SFX/SHX   0xE8
115         FMR-50LT        0xF8
116         FMR-50NBX       0x28
117         FMR-50NB        0x60
118         FMR-50NE/T      0x08
119         FMR-CARD        0x70
120
121         80286           0x00
122         80386           0x01
123         80386SX         0x03
124         80486           0x02
125 */
126
127         // create devices
128         //first_device = last_device = nullptr;
129         dummy = new DEVICE(this, emu);  // must be 1st device
130         event = new EVENT(this, emu);   // must be 2nd device
131 #if defined(_USE_QT)
132         dummy->set_device_name(_T("1st Dummy"));
133         event->set_device_name(_T("EVENT"));
134 #endif
135
136         cpu = new I386(this, emu);
137 #if defined(_USE_QT)
138   #if defined(HAS_I386)
139         cpu->set_device_name(_T("CPU(i386)"));
140   #elif defined(HAS_I486)
141         cpu->set_device_name(_T("CPU(i486)"));
142   #elif defined(HAS_PENTIUM)
143         cpu->set_device_name(_T("CPU(Pentium)"));
144   #endif
145 #endif
146
147         io = new IO(this, emu);
148         io->space = _IO_SPACE;
149         io->bus_width = _IO_BUS_WIDTH;
150
151         crtc = new TOWNS_CRTC(this, emu);
152         cdrom = new TOWNS_CDROM(this, emu);
153
154         memory = new TOWNS_MEMORY(this, emu);
155         memory->space = _MEMORY_SPACE;
156         memory->bank_size = _MEMORY_BANK_SIZE;
157         memory->bus_width = _MEMORY_BUS_WIDTH;
158
159         vram = new TOWNS_VRAM(this, emu);
160         sprite = new TOWNS_SPRITE(this, emu);
161         sysrom = new SYSROM(this, emu);
162         msdosrom = new MSDOSROM(this, emu);
163         fontrom = new FONT_ROMS(this, emu);
164         dictionary = new DICTIONARY(this, emu);
165 #if defined(HAS_20PIX_FONTS)
166         fontrom_20pix = new FONT_ROM_20PIX(this, emu);
167 #endif
168         serialrom = new SERIAL_ROM(this, emu);
169
170         adpcm = new ADPCM(this, emu);
171 //      mixer = new MIXER(this, emu); // Pseudo mixer.
172
173         planevram = new PLANEVRAM(this, emu);
174
175         adc = new AD7820KR(this, emu);
176         rf5c68 = new RF5C68(this, emu);
177         e_volumes[0] = new MB87078(this, emu);
178         e_volumes[1] = new MB87078(this, emu);
179
180         sio = new I8251(this, emu);
181         pit0 = new I8253(this, emu);
182         pit0->device_model = INTEL_8253;
183
184         pit1 = new I8253(this, emu);
185         pit1->device_model = INTEL_8253;
186
187         pic = new I8259(this, emu);
188         pic->num_chips = 2;
189         fdc = new MB8877(this, emu);
190         rtc = new MSM58321(this, emu);
191         beep = new PCM1BIT(this, emu);
192         opn2 = new YM2612(this, emu);
193
194         seek_sound = new NOISE(this, emu);
195         head_up_sound = new NOISE(this, emu);
196         head_down_sound = new NOISE(this, emu);
197
198 //      scsi_host = new TOWNS_SCSI_HOST(this, emu);
199         scsi_host = new SCSI_HOST(this, emu);
200
201         for(int i = 0; i < 7; i++) {
202                 scsi_hdd[i] = nullptr;
203         }
204 #if defined(USE_HARD_DISK)
205         for(int i = 0; i < USE_HARD_DISK; i++) {
206                 scsi_hdd[i] = new SCSI_HDD(this, emu);
207                 scsi_hdd[i]->set_device_name(_T("SCSI Hard Disk Drive #%d"), i + 1);
208                 scsi_hdd[i]->scsi_id = i ;
209                 scsi_hdd[i]->set_disk_handler(0, new HARDDISK(emu));
210                 scsi_hdd[i]->set_context_interface(scsi_host);
211                 my_sprintf_s(scsi_hdd[i]->vendor_id, 9, "FUJITSU");
212                 my_sprintf_s(scsi_hdd[i]->product_id, 17, "SCSI-HDD");
213                 scsi_host->set_context_target(scsi_hdd[i]);
214         }
215 #endif
216         dma = new TOWNS_DMAC(this, emu);
217         extra_dma = new TOWNS_DMAC(this, emu);
218
219         floppy = new FLOPPY(this, emu);
220         keyboard = new KEYBOARD(this, emu);
221         joystick = new JOYSTICK(this, emu);
222         scsi = new SCSI(this, emu);
223         timer = new TIMER(this, emu);
224
225         iccard1 = new TOWNS_ICCARD(this, emu);
226 #if 0
227         iccard2 = new TOWNS_ICCARD(this, emu);
228 #else
229         iccard2 = nullptr;
230 #endif
231         for(int i = 0; i < 2; i++) {
232                 joypad_2btn[i] = new JOYPAD_2BTN(this, emu);
233                 joypad_6btn[i] = new JOYPAD_6BTN(this, emu);
234                 mouse[i] = new MOUSE(this, emu);
235         }
236
237         uint16_t machine_id = 0x0100; // FM-Towns1
238         uint16_t cpu_id = 0x0001;     // i386DX
239         uint32_t cpu_clock = 16000 * 1000; // 16MHz
240 #if defined(_FMTOWNS1_2ND_GEN)
241         machine_id = 0x0200;   // 1F/2F/1H/2H
242 #elif defined(_FMTOWNS_UX_VARIANTS)
243         machine_id = 0x0300;   // UX10/20/40
244         cpu_id = 0x0003;       // i386SX
245 #elif defined(_FMTOWNS1_3RD_GEN)
246         machine_id = 0x0400;  // 10F/20F.40H/80H
247 #elif defined(_FMTOWNS2_CX_VARIANTS)
248         machine_id = 0x0500;  // CX10/20/30/40
249 #elif defined(_FMTOWNS_UG_VARIANTS)
250         machine_id = 0x0600;  // UG10/20/40/80
251         cpu_id = 0x0003;      // i386SX
252         cpu_clock = 20000 * 1000; // 20MHz
253 #elif defined(_FMTOWNS_HR_VARIANTS)
254         machine_id = 0x0700;
255         cpu_id = 0x0002;      // i486SX
256         cpu_clock = 20000 * 1000; // 20MHz
257 #elif defined(_FMTOWNS_HG_VARIANTS)
258         machine_id = 0x0800;
259         cpu_clock = 20000 * 1000; // 20MHz
260 #elif defined(_FMTOWNS_SG_VARIANTS)
261         machine_id = 0x0800; // OK?
262 #elif defined(_FMTOWNS_SR_VARIANTS)
263         machine_id = 0x0700; // OK?
264         cpu_id = 0x0002;      // i486SX
265         cpu_clock = 20000 * 1000; // 20MHz
266 #elif defined(_FMTOWNS_UR_VARIANTS)
267         machine_id = 0x0900;  // UR10/20/40/80
268         cpu_id = 0x0002;      // i486DX
269         cpu_clock = 20000 * 1000; // ToDo: Correct frequency.
270 #elif defined(_FMTOWNS_MA_VARIANTS)
271         machine_id = 0x0b00; // OK?
272         cpu_id = 0x0002;      // i486SX
273         cpu_clock = 33000 * 1000; // 33MHz
274 #elif defined(_FMTOWNS_ME_VARIANTS)
275         machine_id = 0x0d00; // OK?
276         cpu_id = 0x0002;      // i486SX
277         cpu_clock = 25000 * 1000; // 25MHz
278 #elif defined(_FMTOWNS_MF_VARIANTS)
279         machine_id = 0x0f00; // OK?
280         cpu_id = 0x0002;      // i486SX
281         cpu_clock = 33000 * 1000; // 33MHz
282 #elif defined(_FMTOWNS_MX_VARIANTS)
283         machine_id = 0x0c00; // OK?
284         cpu_id = 0x0002;      // i486DX (With FPU?)
285         cpu_clock = 66000 * 1000; // 66MHz
286 #elif defined(_FMTOWNS_HC_VARIANTS)
287         // 20210227 K.O
288         // From FMTowns::MachineID()  of TSUGARU,
289         // git 83d4ec2309ac9fcbb8c01f26061ff0d49c5321e4.
290         machine_id = 0x1100; // OK?
291         cpu_id = 0x0002;      // Pentium (With FPU?)
292         cpu_clock = 50000 * 1000; // ToDo: Correctness frequency.
293 #else
294         // ToDo: Pentium Model (After HB).
295
296 #endif
297
298         event->set_frames_per_sec(FRAMES_PER_SEC);
299         event->set_lines_per_frame(LINES_PER_FRAME);
300
301         set_machine_type(machine_id, cpu_id);
302         // set contexts
303         event->set_context_cpu(cpu, cpu_clock);
304
305         adc_in_ch = -1;
306         line_in_ch = -1;
307         modem_in_ch = -1;
308         mic_in_ch = -1;
309
310         // Use pseudo mixer instead of event.Due to using ADC.
311         // Temporally not use mixer.
312         event->set_context_sound(beep);
313         event->set_context_sound(opn2);
314         event->set_context_sound(rf5c68);
315         event->set_context_sound(cdrom);
316
317         fdc->set_context_noise_seek(seek_sound);
318         fdc->set_context_noise_head_down(head_down_sound);
319         fdc->set_context_noise_head_up(head_up_sound);
320         event->set_context_sound(seek_sound);
321         event->set_context_sound(head_down_sound);
322         event->set_context_sound(head_up_sound);
323
324 #ifdef USE_DEBUGGER
325         pit0->set_context_debugger(new DEBUGGER(this, emu));
326         pit1->set_context_debugger(new DEBUGGER(this, emu));
327 #endif
328         pit0->set_context_ch0(timer, SIG_TIMER_CH0, 1);
329         pit0->set_context_ch1(timer, SIG_TIMER_CH1, 1);
330         pit0->set_context_ch2(beep,  SIG_PCM1BIT_SIGNAL, 1);
331         pit0->set_constant_clock(0, 307200);
332         pit0->set_constant_clock(1, 307200);
333         pit0->set_constant_clock(2, 307200);
334         pit1->set_constant_clock(0, 1229900);
335         pit1->set_constant_clock(1, 1229900);
336         pit1->set_constant_clock(2, 1229900);
337         pic->set_context_cpu(cpu);
338         //pic->set_context_cpu(memory);
339         fdc->set_context_irq(floppy, SIG_FLOPPY_IRQ, 1);
340         rtc->set_context_data(timer, SIG_TIMER_RTC, 0x0f, 0);
341         rtc->set_context_busy(timer, SIG_TIMER_RTC_BUSY, 0x80);
342         scsi_host->set_context_irq(scsi, SIG_SCSI_IRQ, 1);
343         scsi_host->set_context_drq(scsi, SIG_SCSI_DRQ, 1);
344         scsi_host->set_context_drq(keyboard, SIG_KEYBOARD_BOOTSEQ_END, 1);
345
346 #ifdef USE_DEBUGGER
347         dma->set_context_debugger(new DEBUGGER(this, emu));
348         extra_dma->set_context_debugger(new DEBUGGER(this, emu));
349 #endif
350         //dma->set_context_cpu(cpu);
351         dma->set_context_memory(memory);
352         dma->set_context_ch0(fdc);
353         dma->set_context_ch1(scsi_host);
354         //dma->set_context_ch2(printer);
355         dma->set_context_ch3(cdrom);
356         dma->set_context_tc1(scsi, SIG_SCSI_EOT, 0xffffffff);
357         dma->set_context_tc3(cdrom, SIG_TOWNS_CDROM_DMAINT, 0xffffffff);
358
359         //dma->set_context_ack1(scsi_host, SIG_SCSI_ACK, 0xffffffff);
360         dma->set_context_ack3(cdrom, SIG_TOWNS_CDROM_DMAACK, 0xffffffff);
361         dma->set_context_ube1(scsi_host, SIG_SCSI_16BIT_BUS, 0x02);
362         dma->set_context_child_dma(extra_dma);
363
364         floppy->set_context_fdc(fdc);
365
366         sprite->set_context_vram(vram);
367 //      sprite->set_context_font(fontrom);
368 //      sprite->set_context_crtc(crtc);
369 #ifdef USE_DEBUGGER
370         sprite->set_context_debugger(new DEBUGGER(this, emu));
371 #endif
372
373         planevram->set_context_vram(vram);
374         planevram->set_context_sprite(sprite);
375         planevram->set_context_crtc(crtc);
376
377         crtc->set_context_sprite(sprite);
378         crtc->set_context_vram(vram);
379         crtc->set_context_font(fontrom);
380
381 //      e_volumes[0]->set_context_device(0, line_in, 0,
382 //                                                                       MB87078_TYPE_MASK_LEFT);
383 //      e_volumes[0]->set_context_device(1, line_in, 0,
384 //                                                                       MB87078_TYPE_MASK_RIGHT);
385         e_volumes[1]->set_context_device(0, cdrom, 0,
386                                                                          MB87078_TYPE_SET_LEFT, SIG_TOWNS_CDROM_MUTE_L,
387                                                                          0xffffffff,
388                                                                          0xffffffff,
389                                                                          false
390                 );
391         e_volumes[1]->set_context_device(1, cdrom, 0,
392                                                                          MB87078_TYPE_SET_RIGHT, SIG_TOWNS_CDROM_MUTE_R,
393                                                                          0xffffffff,
394                                                                          0xffffffff,
395                                                                          false
396                 );
397
398         memory->set_context_cpu(cpu);
399         memory->set_context_dmac(dma);
400         memory->set_context_vram(vram);
401         memory->set_context_planevram(planevram);
402         memory->set_context_crtc(crtc);
403         memory->set_context_system_rom(sysrom);
404         memory->set_context_msdos(msdosrom);
405         memory->set_context_dictionary(dictionary);
406         memory->set_context_font_rom(fontrom);
407         memory->set_context_timer(timer);
408         memory->set_context_serial_rom(serialrom);
409         memory->set_context_sprite(sprite);
410         memory->set_context_pcm(rf5c68);
411         memory->set_context_iccard(iccard1, 0);
412         memory->set_context_iccard(iccard2, 1);
413
414         adpcm->set_context_opn2(opn2);
415         adpcm->set_context_rf5c68(rf5c68);
416         adpcm->set_context_adc(adc);
417
418         rf5c68->set_context_interrupt_boundary(adpcm, SIG_ADPCM_WRITE_INTERRUPT, 0xffffffff);
419 #ifdef USE_DEBUGGER
420         rf5c68->set_context_debugger(new DEBUGGER(this, emu));
421 #endif
422         opn2->set_context_irq(adpcm, SIG_ADPCM_OPX_INTR, 0xffffffff);
423
424         adc->set_sample_rate(19200);
425         adc->set_sound_bank(-1);
426         adc->set_context_interrupt(adpcm, SIG_ADPCM_ADC_INTR, 0xffffffff);
427
428         scsi->set_context_dma(dma);
429         scsi->set_context_host(scsi_host);
430         scsi->set_context_pic(pic);
431         timer->set_context_pcm(beep);
432         timer->set_context_rtc(rtc);
433         //timer->set_context_halt_line(cpu, SIG_CPU_HALTREQ, 0xffffffff);
434         timer->set_context_halt_line(cpu, SIG_CPU_BUSREQ, 0xffffffff);
435
436         for(int i = 0; i < 2; i++) {
437                 // type =
438                 // 0: Towns PAD 2buttons
439                 // 1: Towns PAD 6buttons
440                 // 2: Towns MOUSE
441                 // 3: Analog Pad (reserved)
442                 // 4: Libble Rabble stick (reserved)
443                 joystick->set_context_joystick(i, joypad_2btn[i]);
444                 joystick->set_context_joystick(i, joypad_6btn[i]);
445                 joystick->set_context_joystick(i, mouse[i]);
446         }
447 #ifdef USE_DEBUGGER
448         joystick->set_context_debugger(new DEBUGGER(this, emu));
449 #endif
450         // ToDo: Selective by config.
451         for(int i = 0; i < 2; i++) {
452                 joypad_2btn[i]->set_context_pad_num(i);
453                 joypad_2btn[i]->set_context_parent_port(i, joystick, 0, 0xff);
454                 joypad_2btn[i]->set_negative_logic(true);
455                 joypad_2btn[i]->set_enable(true);
456         }
457         for(int i = 0; i < 2; i++) {
458                 joypad_6btn[i]->set_context_pad_num(i);
459                 joypad_6btn[i]->set_context_parent_port(i, joystick, 0, 0xff);
460                 joypad_6btn[i]->set_negative_logic(true);
461                 joypad_6btn[i]->set_enable(false);
462         }
463         for(int i = 0; i < 2; i++) {
464                 mouse[i]->set_context_pad_num(i);
465                 mouse[i]->set_context_parent_port(i, joystick, 0, 0xff);
466                 mouse[i]->set_negative_logic(true);
467                 mouse[i]->set_enable(false);
468         }
469         joystick->set_using_pad(0, -1);
470         joystick->set_using_pad(1, -1);
471
472         // cpu bus
473         cpu->set_context_mem(memory);
474         cpu->set_context_io(io);
475         cpu->set_context_intr(pic);
476         cpu->set_context_dma(dma);
477         cpu->set_context_bios(nullptr);
478         cpu->set_context_extreset(memory, SIG_FMTOWNS_NOTIFY_RESET, 0xffffffff);
479 #ifdef USE_DEBUGGER
480         cpu->set_context_debugger(new DEBUGGER(this, emu));
481 #endif
482         // Interrupts
483         // IRQ0  : TIMER
484         // IRQ1  : KEYBOARD
485         // IRQ2  : USART (ToDo)
486         // IRQ3  : EXTRA USART (ToDo)
487         // IRQ4  : EXTRA I/O (Maybe not implement)
488         // IRQ5  : EXTRA I/O (Maybe not implement)
489         // IRQ6  : FDC
490         // IRQ7  : Deisy chain (to IRQ8 - 15)
491         timer->set_context_intr_line(pic, SIG_I8259_CHIP0 | SIG_I8259_IR0, 0xffffffff);
492         keyboard->set_context_intr_line(pic, SIG_I8259_CHIP0 | SIG_I8259_IR1, 0xffffffff);
493         floppy->set_context_intr_line(pic, SIG_I8259_CHIP0 | SIG_I8259_IR6, 0xffffffff);
494
495         // IRQ8  : SCSI (-> scsi.cpp)
496         // IRQ9  : CDC/CDROM
497         // IRQ10 : EXTRA I/O (Maybe not implement)
498         // IRQ11 : VSYNC
499         // IRQ12 : PRINTER (ToDo)
500         // IRQ13 : ADPCM AND OPN2 (Route to adpcm.cpp)
501         // IRQ14 : EXTRA I/O (Maybe not implement)
502         // IRQ15 : RESERVED.
503         cdrom->set_context_mpuint_line(pic, SIG_I8259_CHIP1 | SIG_I8259_IR1, 0xffffffff);
504         crtc->set_context_vsync(pic, SIG_I8259_CHIP1 | SIG_I8259_IR3, 0xffffffff);
505         adpcm->set_context_intr_line(pic, SIG_I8259_CHIP1 | SIG_I8259_IR5, 0xffffffff);
506
507         // DMA0  : FDC/DRQ
508         // DMA1  : SCSI (-> scsi.cpp)
509         // DMA2  : PRINTER (ToDo)
510         // DMA3  : CDC/CDROM
511         // EXTRA DMA0 : EXTRA SLOT (Maybe not implement)
512         // EXTRA DMA1 : Reserved
513         // EXTRA DMA2 : Reserved
514         // EXTRA DMA3 : Reserved
515         fdc->set_context_drq(dma, SIG_UPD71071_CH0, 1);
516         fdc->set_context_drq(keyboard, SIG_KEYBOARD_BOOTSEQ_END, 1);
517         cdrom->set_context_drq_line(dma, SIG_UPD71071_CH3, 0xff);
518         cdrom->set_context_drq_line(keyboard, SIG_KEYBOARD_BOOTSEQ_END, 1);
519
520         // NMI0 : KEYBOARD (RAS)
521         // NMI1 : Extra SLOT (Maybe not implement)
522         keyboard->set_context_nmi_line(memory, SIG_CPU_NMI, 0xffffffff);
523
524         cdrom->set_context_dmac(dma);
525         // For Debugging, will remove 20200822 K.O
526         cdrom->set_context_cpu(cpu);
527
528         // i/o bus
529         io->set_iowait_range_rw(0x0000, 0xffff, 6); // ToDo: May variable wait.
530
531         io->set_iomap_alias_rw (0x0000, pic, I8259_ADDR_CHIP0 | 0);
532         io->set_iomap_alias_rw (0x0002, pic, I8259_ADDR_CHIP0 | 1);
533         io->set_iomap_alias_rw (0x0010, pic, I8259_ADDR_CHIP1 | 0);
534         io->set_iomap_alias_rw (0x0012, pic, I8259_ADDR_CHIP1 | 1);
535
536         io->set_iomap_range_rw (0x0020, 0x0025, memory);
537         io->set_iomap_range_rw (0x0026, 0x0027, timer);  // Freerun counter
538         io->set_iomap_single_rw(0x0028, memory);
539
540         io->set_iomap_range_r  (0x0030, 0x0031, memory);        // cpu id / machine id
541         io->set_iomap_single_rw(0x0032, memory);        // serial rom (routed from memory)
542         io->set_iomap_single_r (0x0034, scsi);  // ENABLE/ UNABLE to WORD DMA for SCSI
543
544         io->set_iomap_alias_rw(0x0040, pit0, 0);
545         io->set_iomap_alias_rw(0x0042, pit0, 1);
546         io->set_iomap_alias_rw(0x0044, pit0, 2);
547         io->set_iomap_alias_rw(0x0046, pit0, 3);
548         io->set_iomap_alias_rw(0x0050, pit1, 0);
549         io->set_iomap_alias_rw(0x0052, pit1, 1);
550         io->set_iomap_alias_rw(0x0054, pit1, 2);
551         io->set_iomap_alias_rw(0x0056, pit1, 3);
552
553         io->set_iomap_single_rw(0x0060, timer); // Beep and interrupts register
554         io->set_iomap_single_rw(0x0068, timer); // Interval timer register2 (after Towns 10F).
555         io->set_iomap_single_rw(0x006a, timer); // Interval timer register2 (after Towns 10F).
556         io->set_iomap_single_rw(0x006b, timer); // Interval timer register2 (after Towns 10F).
557         io->set_iomap_single_rw(0x006c, timer); // 1uS wait register (after Towns 10F).
558
559         io->set_iomap_single_rw(0x0070, timer); // RTC DATA
560         io->set_iomap_single_w (0x0080, timer); // RTC COMMAND
561
562         io->set_iomap_range_rw (0x00a0, 0x00af, dma);
563         io->set_iomap_range_rw (0x00b0, 0x00bf, extra_dma);
564
565         io->set_iomap_single_rw(0x00c0, memory);   // CACHE CONTROLLER
566         io->set_iomap_single_rw(0x00c2, memory);   // CACHE CONTROLLER
567
568         io->set_iomap_alias_rw (0x0200, fdc, 0);  // STATUS/COMMAND
569         io->set_iomap_alias_rw (0x0202, fdc, 1);  // TRACK
570         io->set_iomap_alias_rw (0x0204, fdc, 2);  // SECTOR
571         io->set_iomap_alias_rw (0x0206, fdc, 3);  // DATA
572         io->set_iomap_single_rw(0x0208, floppy);  // DRIVE STATUS / DRIVE CONTROL
573         io->set_iomap_single_rw(0x020c, floppy);  // DRIVE SELECT
574         io->set_iomap_single_r (0x020d, floppy);  // FDDVEXT (after HG/HR).
575         io->set_iomap_single_rw(0x020e, floppy);  // Towns drive SW
576
577         io->set_iomap_range_rw (0x0400, 0x0404, memory); // System Status
578 //      io->set_iomap_range_rw (0x0406, 0x043f, memory); // Reserved
579
580         io->set_iomap_range_rw(0x0440, 0x0443, crtc); // CRTC
581         io->set_iomap_range_rw(0x0448, 0x044f, crtc); // VIDEO OUT (CRTC)
582
583         io->set_iomap_range_rw(0x0450, 0x0452, sprite); // SPRITE
584
585         io->set_iomap_single_rw(0x0458, vram);         // VRAM ACCESS CONTROLLER (ADDRESS)
586         io->set_iomap_range_rw (0x045a, 0x045b, vram); // VRAM ACCESS CONTROLLER (DATA)
587
588         io->set_iomap_single_rw(0x0480, memory); //  MEMORY REGISTER
589         io->set_iomap_single_rw(0x0484, dictionary); // Dictionary
590
591         io->set_iomap_alias_r(0x48a, iccard1, 0); //
592         //io->set_iomap_alias_rw(0x490, memory_card); // After Towns2
593         //io->set_iomap_alias_rw(0x491, memory_card); // After Towns2
594
595         io->set_iomap_range_rw(0x04c0, 0x04c6, cdrom); // CDROM
596         io->set_iomap_range_r (0x04cc, 0x04cd, cdrom); // CDROM
597         // PAD, Sound
598
599         io->set_iomap_single_r(0x04d0, joystick); // Pad1
600         io->set_iomap_single_r(0x04d2, joystick); // Pad 2
601         io->set_iomap_single_w(0x04d6, joystick); // Pad out
602
603         io->set_iomap_single_rw(0x04d5, adpcm); // mute
604         // OPN2(YM2612)
605         io->set_iomap_alias_rw(0x04d8, opn2, 0); // STATUS(R)/Addrreg 0(W)
606         io->set_iomap_alias_w (0x04da, opn2, 1);  // Datareg 0(W)
607         io->set_iomap_alias_w (0x04dc, opn2, 2);  // Addrreg 1(W)
608         io->set_iomap_alias_w (0x04de, opn2, 3);  // Datareg 1(W)
609         // Electrical volume
610         io->set_iomap_alias_rw(0x04e0, e_volumes[0], 0);
611         io->set_iomap_alias_rw(0x04e1, e_volumes[0], 1);
612         io->set_iomap_alias_rw(0x04e2, e_volumes[1], 0);
613         io->set_iomap_alias_rw(0x04e3, e_volumes[1], 1);
614
615         // ADPCM
616         io->set_iomap_range_rw(0x04e7, 0x04ec, adpcm); // A/D SAMPLING DATA REG
617         io->set_iomap_range_rw(0x04f0, 0x04f8, rf5c68); // A/D SAMPLING DATA REG
618
619         io->set_iomap_single_rw(0x05c0, memory); // NMI MASK
620         io->set_iomap_single_r (0x05c2, memory);  // NMI STATUS
621         io->set_iomap_single_r (0x05c8, sprite); // TVRAM EMULATION
622         io->set_iomap_single_w (0x05ca, crtc); // VSYNC INTERRUPT
623
624         io->set_iomap_single_rw(0x05e0, memory); // Hidden MEMORY WAIT REGISTER from AB.COM (Towns 1/2)
625         io->set_iomap_single_rw(0x05e2, memory); // Hidden MEMORY WAIT REGISTER from AB.COM (After Towns 1H/1F/2H/2F )
626         io->set_iomap_single_rw(0x05e6, memory); // Hidden VRAM WAIT REGISTER from TSUGARU (Maybe after Towns 10H/10F/20H/20F )
627         io->set_iomap_single_r (0x05e8, memory); // RAM capacity register.(later Towns1H/2H/1F/2F).
628         io->set_iomap_single_rw(0x05ec, memory); // RAM Wait register , ofcially after Towns2, but exists after Towns1H.
629         io->set_iomap_single_r (0x05ed, memory); // Maximum clock register (After HR/HG).
630         io->set_iomap_single_rw(0x05ee, vram);   // VRAM CACHE CONTROLLER
631
632         io->set_iomap_single_rw(0x0600, keyboard);
633         io->set_iomap_single_rw(0x0602, keyboard);
634         io->set_iomap_single_rw(0x0604, keyboard);
635
636         //io->set_iomap_single_rw(0x0800, printer);
637         //io->set_iomap_single_rw(0x0802, printer);
638         //io->set_iomap_single_rw(0x0804, printer);
639
640         io->set_iomap_alias_rw (0x0a00, sio, 0);
641         io->set_iomap_alias_rw (0x0a02, sio, 1);
642 //      io->set_iomap_single_r (0x0a04, serial);
643 //      io->set_iomap_single_r (0x0a06, serial);
644 //      io->set_iomap_single_w (0x0a08, serial);
645 //      io->set_iomap_single_rw(0x0a0a, modem);
646
647         io->set_iomap_single_rw(0x0c30, scsi);
648         io->set_iomap_single_rw(0x0c32, scsi);
649         io->set_iomap_single_r (0x0c34, scsi);
650
651         io->set_iomap_range_rw (0x3000, 0x3fff, dictionary); // CMOS
652
653         io->set_iomap_range_rw (0xfd90, 0xfda0, crtc);  // Palette and CRTC
654         io->set_iomap_single_r (0xfda2, crtc);  // CRTC
655         io->set_iomap_single_rw(0xfda4, memory);        // memory
656
657         io->set_iomap_range_rw (0xff80, 0xff83, planevram);     // MMIO
658         io->set_iomap_single_r (0xff84, planevram);     // MMIO
659         io->set_iomap_single_rw(0xff86, planevram);     // MMIO
660         io->set_iomap_single_rw(0xff88, memory);        // MMIO
661         //io->set_iomap_range_rw (0xff94, 0xff97, fontrom);     // MMIO
662         //io->set_iomap_range_rw (0xff98, 0xff99, memory);      // MMIO
663         //io->set_iomap_range_rw (0xff9c, 0xff9f, memory);      // MMIO
664
665         io->set_iomap_range_rw (0xff94, 0xff99, memory);        // MMIO
666         io->set_iomap_range_r  (0xff9c, 0xff9d, memory);        // MMIO
667         io->set_iomap_single_rw(0xff9e, memory);        // MMIO
668         io->set_iomap_single_rw(0xffa0, planevram);     // MMIO
669
670
671
672         // Vram allocation may be before initialize().
673         // initialize all devices
674 #if defined(__GIT_REPO_VERSION)
675         set_git_repo_version(__GIT_REPO_VERSION);
676 #endif
677         // ToDo : Use config framework
678         int exram_size = config.current_ram_size;
679         if(exram_size < 1) {
680                 if(machine_id < 0x0200) { // Model1 - 2H
681                         exram_size = 6;
682                 } else if(machine_id == 0x0500) { // CX
683                         exram_size = 15;
684                 } else if(machine_id < 0x0700) {  // 10F,20H
685                         exram_size = 8;
686                 } else if(machine_id == 0x0800) { // HG
687                         exram_size = 15;
688                 } else {
689                         exram_size = 31;
690                 }
691         }
692         if(exram_size < MIN_RAM_SIZE) {
693                 exram_size = MIN_RAM_SIZE;
694         }
695
696         memory->set_extra_ram_size(exram_size);
697
698 #if defined(WITH_I386SX)
699         cpu->device_model = INTEL_80386;
700 #elif defined(WITH_I486SX)
701         cpu->device_model = INTEL_I486SX;
702 #elif defined(WITH_I486DX)
703         cpu->device_model = INTEL_I486DX;
704 #elif defined(WITH_PENTIUM)
705         cpu->device_model = INTEL_PENTIUM;
706 #else
707         // I386
708         cpu->device_model = INTEL_80386;
709 #endif
710
711         initialize_devices();
712 //      cpu->set_address_mask(0xffffffff);
713 }
714
715 VM::~VM()
716 {
717         // delete all devices
718         release_devices();
719 }
720
721 void VM::set_machine_type(uint16_t machine_id, uint16_t cpu_id)
722 {
723         if(memory != nullptr) {
724                 memory->set_cpu_id(cpu_id);
725                 memory->set_machine_id(machine_id);
726         }
727         if(crtc != nullptr) {
728                 crtc->set_cpu_id(cpu_id);
729                 crtc->set_machine_id(machine_id);
730         }
731         if(timer != nullptr) {
732                 timer->set_cpu_id(cpu_id);
733                 timer->set_machine_id(machine_id);
734         }
735         if(cdrom != nullptr) {
736                 cdrom->set_cpu_id(cpu_id);
737                 cdrom->set_machine_id(machine_id);
738         }
739         if(scsi != nullptr) {
740                 scsi->set_cpu_id(cpu_id);
741                 scsi->set_machine_id(machine_id);
742         }
743         if(serialrom != nullptr) {
744                 serialrom->set_cpu_id(cpu_id);
745                 serialrom->set_machine_id(machine_id);
746         }
747         if(floppy != nullptr) {
748                 floppy->set_cpu_id(cpu_id);
749                 floppy->set_machine_id(machine_id);
750         }
751 #if defined(HAS_20PIX_FONTS)
752         if(fontrom_20pix != nullptr) {
753                 fontrom_20pix->set_cpu_id(cpu_id);
754                 fontrom_20pix->set_machine_id(machine_id);
755         }
756 #endif
757         if(vram != nullptr) {
758                 vram->set_cpu_id(cpu_id);
759                 vram->set_machine_id(machine_id);
760         }
761
762 }
763
764
765 // ----------------------------------------------------------------------------
766 // drive virtual machine
767 // ----------------------------------------------------------------------------
768
769 void VM::reset()
770 {
771         // reset all devices
772         boot_seq = false;
773         VM_TEMPLATE::reset();
774 //      cpu->set_address_mask(0xffffffff);
775 }
776
777 void VM::special_reset(int num)
778 {
779         boot_seq = true;
780
781         // reset all devices
782         VM_TEMPLATE::reset();
783         //      cpu->set_address_mask(0xffffffff);
784         __LIKELY_IF(keyboard != nullptr) {
785                 keyboard->special_reset(num);
786         }
787 }
788
789 void VM::run()
790 {
791         __LIKELY_IF(event != nullptr) {
792                 event->drive();
793         }
794 }
795 void VM::process_boot_sequence(uint32_t val)
796 {
797         if(boot_seq) {
798                 if(val != 0) {
799                         if(keyboard != nullptr) {
800                                 keyboard->write_signal(SIG_KEYBOARD_BOOTSEQ_END, 0xffffffff, 0xffffffff);
801                         }
802                         boot_seq = false;
803                 }
804         }
805 }
806
807 // ----------------------------------------------------------------------------
808 // debugger
809 // ----------------------------------------------------------------------------
810
811 #ifdef USE_DEBUGGER
812 DEVICE *VM::get_cpu(int index)
813 {
814         if(index == 0) {
815                 return cpu;
816         }
817         return nullptr;
818 }
819 #endif
820
821 // ----------------------------------------------------------------------------
822 // draw screen
823 // ----------------------------------------------------------------------------
824
825 void VM::draw_screen()
826 {
827         __LIKELY_IF(crtc != nullptr) {
828                 crtc->draw_screen();
829         }
830 }
831
832
833 // ----------------------------------------------------------------------------
834 // soud manager
835 // ----------------------------------------------------------------------------
836
837 void VM::initialize_sound(int rate, int samples)
838 {
839 //      emu->lock_vm();
840         // init sound manager
841         event->initialize_sound(rate, samples);
842
843         // init sound gen
844         beep->initialize_sound(rate, 8000);
845
846         // init OPN2
847         #if 0
848         // MASTER CLOCK MAYBE 600KHz * 12 = 7200KHz .
849         // From FM-Towns Technical Databook (Rev.2), Page 201
850         // opn2->initialize_sound(rate, (int)(600.0e3 * 12.0) , samples, 0.0, 0.0);
851         #else
852         // But...20230330 K.O:
853         // From src/ym2612/ym2612.h of Tsugaru below quote.
854         // Thanks to YS-11 San.
855         //
856         // What exactly is the master clock given to YM2612 of TOWNS?
857         // FM Towns Technical Databook tells internal clock is 600KHz on page 201.
858         // However, after calibrating the tone to match real Towns, it must be 690KHz.  Is it correct?
859         // Master clock must be the internal clock times pre scaler.  But, we don't know the default pre scalar value.
860         // Let's say it is 3.  Then, 690KHz times 3=2070KHz.  Sounds reasonable.
861         // But, now FM77AV's YM2203C uses master clock frequency of 1228.8KHz.
862         // And after calibration we know the ratio between the two.
863         // From there, 1228.8*1698/1038=1999.46.  2MHz.  Makes more sense.
864         opn2->initialize_sound(rate, (int)(((1228.8e3 * 1698.0) / 1038.0) * 4.0) , samples, 0.0, 0.0);
865         #endif
866         // init PCM
867         rf5c68->initialize_sound(rate, samples);
868
869         // add_sound_in_source() must add after per initialize_sound().
870         adc_in_ch = event->add_sound_in_source(rate, samples, 2);
871         adc->set_sample_rate(19200);
872         adc->set_sound_bank(adc_in_ch);
873 #if 0
874         mixer->set_context_out_line(adc_in_ch);
875         mixer->set_context_sample_out(adc_in_ch, rate, samples); // Must be 2ch.
876         // ToDo: Check recording sample rate & channels.
877         mic_in_ch = event->add_sound_in_source(rate, samples, 2);
878         mixer->set_context_mic_in(mic_in_ch, rate, samples);
879
880         line_in_ch = event->add_sound_in_source(rate, samples, 2);
881         mixer->set_context_line_in(line_in_ch, rate, samples);
882 #endif
883 //      emu->unlock_vm();
884 }
885
886 uint16_t* VM::create_sound(int* extra_frames)
887 {
888         __LIKELY_IF(event != nullptr) {
889                 return event->create_sound(extra_frames);
890         }
891         return VM_TEMPLATE::create_sound(extra_frames);
892 }
893
894 int VM::get_sound_buffer_ptr()
895 {
896         __LIKELY_IF(event != nullptr) {
897                 return event->get_sound_buffer_ptr();
898         }
899         return VM_TEMPLATE::get_sound_buffer_ptr();
900 }
901
902 void VM::clear_sound_in()
903 {
904         __UNLIKELY_IF(event == nullptr) return;
905
906         event->clear_sound_in_source(adc_in_ch);
907         event->clear_sound_in_source(mic_in_ch);
908         event->clear_sound_in_source(line_in_ch);
909         return;
910 }
911
912 int VM::get_sound_in_data(int ch, int32_t* dst, int expect_samples, int expect_rate, int expect_channels)
913 {
914         if(dst == nullptr) return 0;
915         if(expect_samples <= 0) return 0;
916         int n_ch = -1;
917         switch(ch) {
918         case 0x00:
919                 n_ch = line_in_ch;
920                 break;
921         case 0x01:
922                 n_ch = mic_in_ch;
923                 break;
924         case 0x100:
925                 n_ch = adc_in_ch;
926                 break;
927         }
928         if(n_ch < 0) return 0;
929         int samples = 0;
930         if(event != nullptr) {
931                 samples = event->get_sound_in_data(n_ch, dst, expect_samples, expect_rate, expect_channels);
932         }
933         return samples;
934 }
935
936 // Write to event's buffer
937 int VM::sound_in(int ch, int32_t* src, int samples)
938 {
939         if(ch < 0) return 0;
940         if(ch >= 2) return 0;
941         int n_ch = -1;
942         switch(ch) {
943         case 0x100:  // ADC in from MIXER, not connected.
944                 break;
945         case 0x00: // LINE
946                 n_ch = line_in_ch;
947                 break;
948         case 0x01: // MIC
949                 n_ch = mic_in_ch;
950                 break;
951         }
952         if(n_ch < 0) return 0;
953
954         int ss = 0;
955         if(event != nullptr) {
956                 emu->lock_vm();
957                 ss =  event->write_sound_in_buffer(n_ch, src, samples);
958                 emu->unlock_vm();
959         }
960         return ss;
961 }
962
963 #if defined(USE_HARD_DISK)
964 void VM::open_hard_disk(int drv, const _TCHAR* file_path)
965 {
966         if((drv < USE_HARD_DISK) && (drv < 8) && (drv >= 0)) {
967                 if(scsi_hdd[drv] != nullptr) {
968                         scsi_hdd[drv]->open(0, file_path, 512);
969                 }
970         }
971 }
972
973 void VM::close_hard_disk(int drv)
974 {
975         if((drv < USE_HARD_DISK) && (drv < 8) && (drv >= 0)) {
976                 if(scsi_hdd[drv] != nullptr) {
977                         scsi_hdd[drv]->close(0);
978                 }
979         }
980 }
981
982 bool VM::is_hard_disk_inserted(int drv)
983 {
984         if((drv < USE_HARD_DISK) && (drv < 8) && (drv >= 0)) {
985                 if(scsi_hdd[drv] != nullptr) {
986                         return scsi_hdd[drv]->mounted(0);
987                 }
988         }
989         return false;
990 }
991
992 uint32_t VM::is_hard_disk_accessed()
993 {
994         uint32_t status = 0;
995
996         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
997                 if(scsi_hdd[drv] != nullptr) {
998                         if(scsi_hdd[drv]->accessed(0)) {
999                                 status |= 1 << drv;
1000                         }
1001                 }
1002         }
1003         process_boot_sequence(status);
1004         return status;
1005 }
1006 #endif // USE_HARD_DISK
1007
1008 void VM::open_compact_disc(int drv, const _TCHAR* file_path)
1009 {
1010         if(cdrom != nullptr) {
1011                 cdrom->open(file_path);
1012         }
1013 }
1014
1015 void VM::close_compact_disc(int drv)
1016 {
1017         if(cdrom != nullptr) {
1018                 cdrom->close();
1019         }
1020 }
1021
1022 bool VM::is_compact_disc_inserted(int drv)
1023 {
1024         if(cdrom == nullptr) return false;
1025         return cdrom->mounted();
1026 }
1027
1028 uint32_t VM::is_compact_disc_accessed()
1029 {
1030         uint32_t status = cdrom->accessed();
1031         process_boot_sequence(status);
1032         return status;
1033 }
1034
1035 #ifdef USE_SOUND_VOLUME
1036 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
1037 {
1038 #ifndef HAS_LINEIN_SOUND
1039 //      if(ch >= 7) ch++;
1040 #endif
1041 #ifndef HAS_MIC_SOUND
1042 //      if(ch >= 8) ch++;
1043 #endif
1044 #ifndef HAS_MODEM_SOUND
1045 //      if(ch >= 9) ch++;
1046 #endif
1047 #ifndef HAS_2ND_ADPCM
1048 //      if(ch >= 10) ch++;
1049 #endif
1050         if(ch == 0) { // BEEP
1051                 if(beep != nullptr) {
1052                         beep->set_volume(0, decibel_l, decibel_r);
1053                 }
1054         }
1055         else if(ch == 1) { // CD-ROM
1056                 if(e_volumes[1] != nullptr) {
1057                         e_volumes[1]->set_volumes(0, decibel_l, 1, decibel_r);
1058                 } else if(cdrom != nullptr) {
1059                         cdrom->set_volume(0, decibel_l, decibel_r);
1060                 }
1061         }
1062         else if(ch == 2) { // OPN2
1063                 if(opn2 != nullptr) {
1064                         opn2->set_volume(0, decibel_l, decibel_r);
1065                 }
1066         }
1067         else if(ch == 3) { // ADPCM
1068                 if(rf5c68 != nullptr) {
1069                         rf5c68->set_volume(0, decibel_l, decibel_r);
1070                 }
1071         }
1072         else if(ch == 4) { // SEEK, HEAD UP / DOWN
1073                 if(seek_sound != nullptr) {
1074                         seek_sound->set_volume(0, decibel_l, decibel_r);
1075                 }
1076                 if(head_up_sound != nullptr) {
1077                         head_up_sound->set_volume(0, decibel_l, decibel_r);
1078                 }
1079                 if(head_down_sound != nullptr) {
1080                         head_down_sound->set_volume(0, decibel_l, decibel_r);
1081                 }
1082         }
1083 }
1084 #endif
1085
1086 // ----------------------------------------------------------------------------
1087 // notify key
1088 // ----------------------------------------------------------------------------
1089
1090 void VM::key_down(int code, bool repeat)
1091 {
1092         __LIKELY_IF(keyboard != nullptr) {
1093                 keyboard->key_down(code);
1094         }
1095 }
1096
1097 void VM::key_up(int code)
1098 {
1099         __LIKELY_IF(keyboard != nullptr) {
1100                 keyboard->key_up(code);
1101         }
1102 }
1103
1104 // ----------------------------------------------------------------------------
1105 // user interface
1106 // ----------------------------------------------------------------------------
1107 void VM::open_cart(int drv, const _TCHAR* file_path)
1108 {
1109         switch(drv) {
1110         case 0:
1111                 if(iccard1 != nullptr) {
1112                         iccard1->open_cart(file_path);
1113                 }
1114                 break;
1115         case 1:
1116                 if(iccard2 != nullptr) {
1117                         iccard2->open_cart(file_path);
1118                 }
1119                 break;
1120         }
1121 }
1122
1123 void VM::close_cart(int drv)
1124 {
1125         switch(drv) {
1126         case 0:
1127                 if(iccard1 != nullptr) {
1128                         iccard1->close_cart();
1129                 }
1130                 break;
1131         case 1:
1132                 if(iccard2 != nullptr) {
1133                         iccard2->close_cart();
1134                 }
1135                 break;
1136         }
1137 }
1138
1139 bool VM::is_cart_inserted(int drv)
1140 {
1141         switch(drv) {
1142         case 0:
1143                 if(iccard1 != nullptr) {
1144                         return iccard1->is_cart_inserted();
1145                 }
1146                 break;
1147         case 1:
1148                 if(iccard2 != nullptr) {
1149                         return iccard2->is_cart_inserted();
1150                 }
1151                 break;
1152         }
1153         return false;
1154 }
1155
1156 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
1157 {
1158
1159         if(fdc != nullptr) {
1160                 fdc->open_disk(drv, file_path, bank);
1161                 floppy->change_disk(drv);
1162         }
1163 }
1164
1165 void VM::close_floppy_disk(int drv)
1166 {
1167         if(fdc != nullptr) {
1168                 fdc->close_disk(drv);
1169         //      floppy->change_disk(drv);
1170         }
1171 }
1172 uint32_t VM::is_floppy_disk_accessed()
1173 {
1174         uint32_t val = fdc->read_signal(0);
1175         process_boot_sequence(val);
1176         return val;
1177 }
1178
1179 bool VM::is_floppy_disk_inserted(int drv)
1180 {
1181         if(fdc != nullptr) {
1182                 return fdc->is_disk_inserted(drv);
1183         }
1184         return VM_TEMPLATE::is_floppy_disk_inserted(drv);
1185 }
1186
1187 void VM::is_floppy_disk_protected(int drv, bool value)
1188 {
1189         __LIKELY_IF(fdc != nullptr) {
1190                 fdc->is_disk_protected(drv, value);
1191         }
1192 }
1193
1194 bool VM::is_floppy_disk_protected(int drv)
1195 {
1196         __LIKELY_IF(fdc != nullptr) {
1197                 return fdc->is_disk_protected(drv);
1198         }
1199         return VM_TEMPLATE::is_floppy_disk_protected(drv);
1200 }
1201
1202 bool VM::is_frame_skippable()
1203 {
1204         __LIKELY_IF(event != nullptr) {
1205                 return event->is_frame_skippable();
1206         }
1207         return VM_TEMPLATE::is_frame_skippable();
1208 }
1209
1210
1211 double VM::get_current_usec()
1212 {
1213         __LIKELY_IF(event != nullptr) {
1214                 return event->get_current_usec();
1215         }
1216         return VM_TEMPLATE::get_current_usec();
1217 }
1218
1219 uint64_t VM::get_current_clock_uint64()
1220 {
1221         __LIKELY_IF(event != nullptr) {
1222                 return event->get_current_clock_uint64();
1223         }
1224         return VM_TEMPLATE::get_current_clock_uint64();
1225 }
1226
1227 #define STATE_VERSION   4
1228
1229 bool VM::process_state(FILEIO* state_fio, bool loading)
1230 {
1231         if(!(VM_TEMPLATE::process_state_core(state_fio, loading, STATE_VERSION))) {
1232                 return false;
1233         }
1234
1235         // Machine specified.
1236         state_fio->StateValue(boot_seq);
1237
1238         if(loading) {
1239                 update_config();
1240         }
1241         return true;
1242 }