OSDN Git Service

[VM][FMTOWNS][MEMORY] Fix setup around memory banks by I/O 0404h and 0480h.
[csp-qt/common_source_project-fm7.git] / source / src / vm / fm7 / fm7.cpp
1 /*
2  * FM7 -> VM
3  * (C) 2015 K.Ohta <whatisthis.sowhat _at_ gmail.com>
4  * History:
5  *   Feb 27, 2015 : Initial
6  */
7
8 #include "fm7.h"
9 #include "../../emu.h"
10 #include "../../config.h"
11 #include "../device.h"
12
13 #include "../memory.h"
14 #include "../prnfile.h"
15
16 #ifdef USE_DEBUGGER
17 #include "../debugger.h"
18 #endif
19
20 #include "../datarec.h"
21 #include "../disk.h"
22
23 #include "../mc6809.h"
24 #include "../z80.h"
25 #include "../mb8877.h"
26 #include "../noise.h"
27
28 #include "../pcm1bit.h"
29 #include "../ym2203.h"
30 #include "../ay_3_891x.h"
31 #include "../and.h"
32 #include "../or.h"
33 #include "../i8251.h"
34
35 #if defined(_FM77AV_VARIANTS)
36 #include "mb61vh010.h"
37 #include "../beep.h"
38 #endif
39 #if defined(HAS_DMA)
40 #include "hd6844.h"
41 #endif
42 #if defined(_FM77L4)
43 #include "../hd46505.h"
44 #endif
45
46 #if defined(_FM8)
47 #include "./bubblecasette.h"
48 #endif
49
50 #if defined(_FM8)
51 #include "./fm8_mainio.h"
52 #else
53 #include "./fm7_mainio.h"
54 #endif
55 #include "./fm7_mainmem.h"
56 #include "./fm7_display.h"
57 #include "./fm7_keyboard.h"
58 #include "./joystick.h"
59
60 #include "./kanjirom.h"
61 #if defined(CAPABLE_JCOMMCARD)
62 #include "./jcommcard.h"
63 #endif
64 #include "../event.h"
65
66 #if defined(USE_BUBBLE)
67 using FM7::BUBBLECASETTE;
68 #endif
69 using FM7::DISPLAY;
70 #if defined(CAPABLE_JCOMMCARD)
71 using FM7::FM7_JCOMMCARD;
72 #endif
73
74 #if defined(_FM8)
75 using FM7::FM8_MAINIO;
76 #else
77 using FM7::FM7_MAINIO;
78 #endif
79 using FM7::FM7_MAINMEM;
80 using FM7::JOYSTICK;
81 using FM7::KEYBOARD;
82 using FM7::KANJIROM;
83
84 VM::VM(EMU_TEMPLATE* parent_emu): VM_TEMPLATE(parent_emu)
85 {
86         
87         first_device = last_device = nullptr;
88 #if defined(_FM8)
89         psg = nullptr;
90 #else   
91 # if defined(_FM77AV_VARIANTS)
92         opn[0] = opn[1] = opn[2] = nullptr;
93 # else   
94         opn[0] = opn[1] = opn[2] = nullptr;
95         psg = nullptr; 
96 # endif
97 #endif
98         for(int i = 0; i < 3; i++) uart[i] = nullptr;
99         
100         dummy = new DEVICE(this, emu);  // must be 1st device
101         event = new EVENT(this, emu);   // must be 2nd device
102
103         //dummycpu = new DEVICE(this, emu);
104         maincpu = new MC6809(this, emu);
105         subcpu = new MC6809(this, emu);
106         g_substat_display = new AND(this, emu);
107         g_substat_mainhalt = new AND(this, emu);
108
109         
110 #ifdef WITH_Z80
111         if((config.dipswitch & FM7_DIPSW_Z80CARD_ON) != 0) {
112                 z80cpu = new Z80(this, emu);
113                 g_mainstat = new AND(this, emu);
114                 g_intr = new OR(this, emu);
115                 
116                 g_intr_irq = new AND(this, emu);
117                 g_intr_firq = new AND(this, emu);
118                 g_nmi = new AND(this, emu);
119         } else {
120                 z80cpu = nullptr;
121                 g_mainstat = nullptr;
122                 g_intr = nullptr;
123
124                 g_intr_irq = nullptr;
125                 g_intr_firq = nullptr;
126                 g_nmi = nullptr;
127         }
128 #endif
129 #if defined(CAPABLE_JCOMMCARD)
130         if((config.dipswitch & FM7_DIPSW_JSUBCARD_ON) != 0) {
131                 jsubcpu = new MC6809(this, emu);
132                 jcommcard = new FM7_JCOMMCARD(this, emu);
133                 g_jsubhalt = new AND(this, emu);
134         } else {
135                 jsubcpu = nullptr;
136                 jcommcard = nullptr;
137                 g_jsubhalt = nullptr;
138         }
139 #endif
140 # if defined(_FM77AV40) || defined(_FM77AV40SX) || defined(_FM77AV40EX) || defined(_FM77AV20) \
141         || defined(_FM77AV20EX) || defined(_FM8)
142         uart[0] = new I8251(this, emu);
143 # else
144 #  if defined(CAPABLE_JCOMMCARD)
145         if((config.dipswitch & FM7_DIPSW_JSUBCARD_ON) != 0) uart[0] = new I8251(this, emu);
146 #  endif
147         if(((config.dipswitch & FM7_DIPSW_RS232C_ON) != 0) && (uart[0] == nullptr)) uart[0] = new I8251(this, emu);
148 # endif
149         if((config.dipswitch & FM7_DIPSW_MODEM_ON) != 0) uart[1] = new I8251(this, emu);
150         if((config.dipswitch & FM7_DIPSW_MIDI_ON) != 0) uart[2] = new I8251(this, emu);
151
152                 
153
154 #if defined(_FM77AV_VARIANTS)
155         alu = new MB61VH010(this, emu);
156         keyboard_beep = new BEEP(this, emu);
157 #ifdef USE_DEBUGGER
158 //      keyboard_beep->set_context_debugger(new DEBUGGER(this, emu));
159 #endif
160 #endif  
161
162         // basic devices
163         // I/Os
164 #if defined(_FM8)
165 #  if defined(USE_AY_3_8910_AS_PSG)
166         psg = new AY_3_891X(this, emu);
167 #  else
168         psg = new YM2203(this, emu);
169 #  endif
170 #  ifdef USE_DEBUGGER
171         psg->set_context_debugger(new DEBUGGER(this, emu));
172 #  endif
173 #else   
174         opn[0] = new YM2203(this, emu); // OPN
175         opn[1] = new YM2203(this, emu); // WHG
176         opn[2] = new YM2203(this, emu); // THG
177 #  ifdef USE_DEBUGGER
178         opn[0]->set_context_debugger(new DEBUGGER(this, emu));
179         opn[1]->set_context_debugger(new DEBUGGER(this, emu));
180         opn[2]->set_context_debugger(new DEBUGGER(this, emu));
181 #  endif
182 # if !defined(_FM77AV_VARIANTS)
183 #  if defined(USE_AY_3_8910_AS_PSG)
184         psg = new AY_3_891X(this, emu);
185 #  else
186         psg = new YM2203(this, emu);
187 #  endif
188 #  ifdef USE_DEBUGGER
189         psg->set_context_debugger(new DEBUGGER(this, emu));
190 #  endif
191 # endif 
192 #endif
193
194 #if defined(HAS_DMA)
195         dmac = new HD6844(this, emu);
196 #endif   
197 #if defined(_FM8)
198         for(int i = 0; i < 2; i++) bubble_casette[i] = new BUBBLECASETTE(this, emu);
199 #endif
200         drec = nullptr;
201         drec = new DATAREC(this, emu);
202         drec->set_context_noise_play(new NOISE(this, emu));
203         drec->set_context_noise_stop(new NOISE(this, emu));
204         drec->set_context_noise_fast(new NOISE(this, emu));
205         pcm1bit = new PCM1BIT(this, emu);
206 #ifdef USE_DEBUGGER
207 //      pcm1bit->set_context_debugger(new DEBUGGER(this, emu));
208 #endif
209
210         connect_320kfdc = connect_1Mfdc = false;
211         fdc = nullptr;
212 #if defined(HAS_2HD)
213         fdc_2HD = nullptr;
214 #endif
215
216 #if defined(_FM8) || defined(_FM7) || defined(_FMNEW7)
217         if((config.dipswitch & FM7_DIPSW_CONNECT_320KFDC) != 0) {
218                 fdc = new MB8877(this, emu);
219                 fdc->set_context_noise_seek(new NOISE(this, emu));
220                 fdc->set_context_noise_head_down(new NOISE(this, emu));
221                 fdc->set_context_noise_head_up(new NOISE(this, emu));
222                 connect_320kfdc = true;
223         }
224 #else
225         {
226                 fdc = new MB8877(this, emu);
227                 fdc->set_context_noise_seek(new NOISE(this, emu));
228                 fdc->set_context_noise_head_down(new NOISE(this, emu));
229                 fdc->set_context_noise_head_up(new NOISE(this, emu));
230                 connect_320kfdc = true;
231         }
232 #endif          
233 #if defined(HAS_2HD)
234         if((config.dipswitch & FM7_DIPSW_CONNECT_1MFDC) != 0) {
235                 fdc_2HD = new MB8877(this, emu);
236                 fdc_2HD->set_context_noise_seek(new NOISE(this, emu));
237                 fdc_2HD->set_context_noise_head_down(new NOISE(this, emu));
238                 fdc_2HD->set_context_noise_head_up(new NOISE(this, emu));
239                 connect_1Mfdc = true;
240         }
241 #endif
242         
243         joystick  = new JOYSTICK(this, emu);
244         printer = new PRNFILE(this, emu);
245 #if defined(_FM77L4)
246         l4crtc = new HD46505(this, emu);;
247 #endif
248
249 #if defined(_FM8) || defined(_FM7) || defined(_FMNEW7)
250         if((config.dipswitch & FM7_DIPSW_CONNECT_KANJIROM) != 0) {
251                 kanjiclass1 = new KANJIROM(this, emu, false);
252         } else {
253                 kanjiclass1 = nullptr;
254         }
255 #else
256         kanjiclass1 = new KANJIROM(this, emu, false);
257 #endif  
258 #ifdef CAPABLE_KANJI_CLASS2
259         kanjiclass2 = new KANJIROM(this, emu, true);
260 #endif
261
262
263         keyboard = new KEYBOARD(this, emu);
264 //      display = new DISPLAY(this, emu);
265 #if defined(_FM8)
266         mainio  = new FM8_MAINIO(this, emu);
267 #else
268         mainio  = new FM7_MAINIO(this, emu);
269 #endif
270         mainmem = new FM7_MAINMEM(this, emu);
271         display = new DISPLAY(this, emu);
272
273 # if defined(_FM77AV20) || defined(_FM77AV40) || defined(_FM77AV20EX) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
274         g_rs232c_dtr = new AND(this, emu);
275         g_rs232c_dtr->set_mask(SIG_AND_BIT_0);
276         g_rs232c_dtr->set_mask(SIG_AND_BIT_1);
277
278         // DCD
279 #endif
280 #ifdef WITH_Z80
281         if(g_mainstat != nullptr) {
282                 g_mainstat->set_mask(SIG_AND_BIT_0);
283                 g_mainstat->set_mask(SIG_AND_BIT_1);
284                 maincpu->set_context_bus_ba(g_mainstat, SIG_AND_BIT_0, 0xffffffff);
285                 maincpu->set_context_bus_bs(g_mainstat, SIG_AND_BIT_1, 0xffffffff);
286                 g_mainstat->set_context_out(mainio, FM7_MAINIO_RUN_Z80, 0xffffffff);
287         }
288         if(z80cpu != nullptr) {
289                 z80cpu->set_context_busack(mainio, FM7_MAINIO_RUN_6809, 0xffffffff);
290                 mainio->set_context_z80cpu(z80cpu);
291         }
292 #endif
293 #if defined(_USE_QT)
294         event->set_device_name(_T("EVENT"));
295         dummy->set_device_name(_T("1st Dummy"));
296         
297         maincpu->set_device_name(_T("MAINCPU(MC6809B)"));
298         subcpu->set_device_name(_T("SUBCPU(MC6809B)"));
299 #if defined(CAPABLE_JCOMMCARD)
300         if(jsubcpu != nullptr) {
301                 jsubcpu->set_device_name(_T("J.COMM BOARD CPU(MC6809)"));
302         }
303         if(jcommcard != nullptr) {
304                 jcommcard->set_device_name(_T("Japanese COMM BOARD"));
305         }
306         if(g_jsubhalt != nullptr) {
307                 g_jsubhalt->set_device_name(_T("J.COMM BOARD HALT(MC6809)"));
308         }
309 # endif
310 # ifdef WITH_Z80
311         if(z80cpu != nullptr) z80cpu->set_device_name(_T("Z80 CPU BOARD"));
312 # endif
313         if(fdc != nullptr) fdc->set_device_name(_T("MB8877 FDC(320KB)"));
314 #if defined(HAS_2HD)
315         if(fdc_2HD != nullptr) fdc_2HD->set_device_name(_T("MB8877 FDC(1MB/2HD)"));
316 #endif  
317         if(uart[0] != nullptr) {
318                 uart[0]->set_device_name(_T("RS-232C BOARD(I8251 SIO)"));
319         }
320 #if defined(CAPABLE_JCOMMCARD)
321         if((config.dipswitch & FM7_DIPSW_JSUBCARD_ON) != 0) {
322                 if(uart[0] != nullptr) uart[0]->set_device_name(_T("J.COMM BOARD RS-232C(I8251 SIO)"));
323         }
324 #elif defined(_FM77AV20) || defined(_FM77AV40) || defined(_FM77AV20EX) || \
325         defined(_FM77AV40EX) || defined(_FM77AV40SX) || defined(_FM8)
326         if(uart[0] != nullptr) uart[0]->set_device_name(_T("RS-232C(I8251 SIO)"));
327 #endif
328                 
329         if(uart[1] != nullptr) {
330                 uart[1]->set_device_name(_T("MODEM BOARD(I8251 SIO)"));
331         }
332         if(uart[2] != nullptr) {
333                 uart[2]->set_device_name(_T("MIDI BOARD(I8251 SIO)"));
334         }
335                                                 
336         // basic devices
337         // I/Os
338 #if defined(_FM8)
339         psg->set_device_name(_T("AY-3-8910 PSG"));
340 #else
341         opn[0]->set_device_name(_T("YM2203 OPN"));
342         opn[1]->set_device_name(_T("YM2203 WHG"));
343         opn[2]->set_device_name(_T("YM2203 THG"));
344         #if !defined(_FM77AV_VARIANTS)
345         psg->set_device_name(_T("AY-3-8910 PSG"));
346         #endif
347         pcm1bit->set_device_name(_T("BEEP"));
348         printer->set_device_name(_T("PRINTER I/F"));
349         #if defined(_FM77AV_VARIANTS)
350         keyboard_beep->set_device_name(_T("BEEP(KEYBOARD)"));
351         #endif  
352         if(kanjiclass1 != nullptr) kanjiclass1->set_device_name(_T("KANJI ROM CLASS1"));
353         #ifdef CAPABLE_KANJI_CLASS2
354         if(kanjiclass2 != nullptr) kanjiclass2->set_device_name(_T("KANJI ROM CLASS2"));
355         #endif
356         #if defined(_FM8)
357         bubble_casette[0]->set_device_name(_T("BUBBLE CASETTE #0"));
358         bubble_casette[1]->set_device_name(_T("BUBBLE CASETTE #1"));
359         #endif  
360 #endif
361 # if defined(_FM77AV20) || defined(_FM77AV40) || defined(_FM77AV20EX) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
362         g_rs232c_dtr->set_device_name(_T("RS232C DTR(AND)"));
363 #endif
364 #if defined(_FM77L4)
365         l4crtc->set_device_name(_T("CRTC OF 400LINES BOARD"));
366 #endif
367 #ifdef WITH_Z80
368         if(g_intr != nullptr) g_intr->set_device_name(_T("Z80 INTR(OR)"));
369         if(g_mainstat != nullptr) g_mainstat->set_device_name(_T("Z80 HALT/RUN(AND)"));
370         if(g_intr_irq != nullptr) g_intr_irq->set_device_name(_T("Z80 IRQ(AND)"));
371         if(g_intr_firq != nullptr) g_intr_firq->set_device_name(_T("Z80 FIRQ(AND)"));
372         if(g_nmi != nullptr) g_nmi->set_device_name(_T("Z80 NMI(AND)"));
373 #endif
374         g_substat_display->set_device_name(_T("DISPLAY STATUS(AND)"));
375         g_substat_mainhalt->set_device_name(_T("SUBSYSTEM HALT STATUS(AND)"));
376 #endif  
377         this->connect_bus();
378 }
379
380 VM::~VM()
381 {
382         // delete all devices
383         release_devices();
384 }
385
386 void VM::connect_bus(void)
387 {
388         uint32_t mainclock;
389         uint32_t subclock;
390
391         /*
392          * CLASS CONSTRUCTION
393          *
394          * VM 
395          *  |-> MAINCPU -> MAINMEM -> MAINIO -> MAIN DEVICES
396          *  |             |        |      
397          *  | -> SUBCPU  -> SUBMEM  -> SUBIO -> SUB DEVICES
398          *  | -> DISPLAY
399          *  | -> KEYBOARD
400          *
401          *  MAINMEM can access SUBMEM/IO, when SUBCPU is halted.
402          *  MAINMEM and SUBMEM can access DISPLAY and KEYBOARD with exclusive.
403          *  MAINCPU can access MAINMEM.
404          *  SUBCPU  can access SUBMEM.
405          *  DISPLAY : R/W from MAINCPU and SUBCPU.
406          *  KEYBOARD : R/W
407          *
408          */
409         event->set_frames_per_sec(FRAMES_PER_SEC);
410         event->set_lines_per_frame(LINES_PER_FRAME);
411         mainclock = CPU_CLOCKS; // Hack
412         subclock = SUBCLOCK_NORMAL;
413
414         event->set_context_cpu(maincpu, mainclock);
415         event->set_context_cpu(subcpu,  subclock);      
416    
417 #ifdef WITH_Z80
418         if(z80cpu != nullptr) {
419                 event->set_context_cpu(z80cpu,  4000000);
420                 z80cpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
421
422                 g_intr_irq->set_mask(SIG_AND_BIT_0);
423                 g_intr_irq->set_mask(SIG_AND_BIT_1);
424                 
425                 g_intr_firq->set_mask(SIG_AND_BIT_0);
426                 g_intr_firq->set_mask(SIG_AND_BIT_1);
427         
428                 g_nmi->set_mask(SIG_AND_BIT_0);
429                 g_nmi->set_mask(SIG_AND_BIT_1);
430
431                 mainio->set_context_irq(g_intr_irq, SIG_AND_BIT_1, 0xffffffff);
432                 g_intr_irq->set_context_out(g_intr, SIG_OR_BIT_0, 0xffffffff);
433         
434                 mainio->set_context_firq(g_intr_firq, SIG_AND_BIT_1, 0xffffffff);
435                 g_intr_firq->set_context_out(g_intr, SIG_OR_BIT_0, 0xffffffff);
436                 
437                 g_intr->set_context_out(z80cpu, SIG_CPU_IRQ, 0xffffffff);
438
439                 mainio->set_context_nmi(g_nmi, SIG_AND_BIT_1, 0xffffffff);
440                 g_nmi->set_context_out(z80cpu, SIG_CPU_NMI, 0xffffffff);
441         }
442         maincpu->write_signal(SIG_CPU_HALTREQ, 0, 1);
443 #endif
444 #if defined(CAPABLE_JCOMMCARD)
445         if((jsubcpu != nullptr) && (jcommcard != nullptr)) {
446                 event->set_context_cpu(jsubcpu,  JCOMMCARD_CLOCK);
447                 jcommcard->set_context_cpu(jsubcpu);
448                 if(g_jsubhalt != nullptr) {
449                         g_jsubhalt->set_mask(SIG_AND_BIT_0);
450                         g_jsubhalt->set_mask(SIG_AND_BIT_1);
451                 
452                         jsubcpu->set_context_bus_ba(g_jsubhalt, SIG_AND_BIT_0, 0xffffffff);
453                         jsubcpu->set_context_bus_bs(g_jsubhalt, SIG_AND_BIT_1, 0xffffffff);
454                         g_jsubhalt->set_context_out(jcommcard, FM7_JCOMMCARD_BUS_HALT, 0xffffffff);
455                         mainio->set_context_jcommcard(jcommcard);
456                 }
457         }
458 #endif
459         event->set_context_sound(pcm1bit);
460 #if defined(_FM8)
461         event->set_context_sound(psg);
462         if(drec != nullptr) event->set_context_sound(drec);
463 #else
464         event->set_context_sound(opn[0]);
465         event->set_context_sound(opn[1]);
466         event->set_context_sound(opn[2]);
467         #if !defined(_FM77AV_VARIANTS)
468         event->set_context_sound(psg);
469         #endif
470         event->set_context_sound(drec);
471         if(fdc != nullptr) {
472                 event->set_context_sound(fdc->get_context_noise_seek());
473                 event->set_context_sound(fdc->get_context_noise_head_down());
474                 event->set_context_sound(fdc->get_context_noise_head_up());
475         }
476 #if defined(HAS_2HD)
477         if(fdc_2HD != nullptr) {
478                 event->set_context_sound(fdc_2HD->get_context_noise_seek());
479                 event->set_context_sound(fdc_2HD->get_context_noise_head_down());
480                 event->set_context_sound(fdc_2HD->get_context_noise_head_up());
481         }
482 #endif
483         if(drec != nullptr) {
484                 event->set_context_sound(drec->get_context_noise_play());
485                 event->set_context_sound(drec->get_context_noise_stop());
486                 event->set_context_sound(drec->get_context_noise_fast());
487         }
488         #if defined(_FM77AV_VARIANTS)
489         event->set_context_sound(keyboard_beep);
490         #endif
491 #endif   
492 #if !defined(_FM77AV_VARIANTS) && !defined(_FM77L4)
493         //event->register_vline_event(display);
494         //event->register_frame_event(display);
495 #endif  
496         mainio->set_context_maincpu(maincpu);
497         mainio->set_context_subcpu(subcpu);
498         
499         mainio->set_context_display(display);
500         mainio->set_context_irq(maincpu, SIG_CPU_IRQ, 0xffffffff);
501         mainio->set_context_firq(maincpu, SIG_CPU_FIRQ, 0xffffffff);
502         mainio->set_context_nmi(maincpu, SIG_CPU_NMI, 0xffffffff);
503 #if defined(_FM77AV) || defined(_FM7) || defined(_FMNEW7) || defined(_FM77_VARIANTS)
504         if((config.dipswitch & FM7_DIPSW_RS232C_ON) != 0)       mainio->set_context_uart(0, uart[0]); /* $FD06- : RS232C */
505 #else
506         mainio->set_context_uart(0, uart[0]);
507 #endif
508         mainio->set_context_uart(1, uart[1]); /* $FD40- : MODEM */
509         mainio->set_context_uart(2, uart[2]); /* $FDEA- : MIDI */
510
511 #if defined(_FM77AV20) || defined(_FM77AV40) || defined(_FM77AV20EX) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
512         mainio->set_context_rs232c_dtr(g_rs232c_dtr);
513         if(uart[0] != nullptr) uart[0]->set_context_dtr(g_rs232c_dtr, SIG_AND_BIT_1, 0xffffffff);
514 #endif
515         if(uart[0] != nullptr) {
516                 uart[0]->set_context_rxrdy(mainio, FM7_MAINIO_UART0_RXRDY, 0xffffffff);
517                 uart[0]->set_context_txrdy(mainio, FM7_MAINIO_UART0_TXRDY, 0xffffffff);
518                 uart[0]->set_context_syndet(mainio, FM7_MAINIO_UART0_SYNDET, 0xffffffff);
519         }
520         if(uart[1] != nullptr) {
521                 uart[1]->set_context_rxrdy(mainio, FM7_MAINIO_MODEM_RXRDY, 0xffffffff);
522                 uart[1]->set_context_txrdy(mainio, FM7_MAINIO_MODEM_TXRDY, 0xffffffff);
523                 uart[1]->set_context_syndet(mainio, FM7_MAINIO_MODEM_SYNDET, 0xffffffff);
524         }
525         if(uart[2] != nullptr) {
526                 uart[2]->set_context_rxrdy(mainio, FM7_MAINIO_MIDI_RXRDY, 0xffffffff);
527                 uart[2]->set_context_txrdy(mainio, FM7_MAINIO_MIDI_TXRDY, 0xffffffff);
528                 uart[2]->set_context_syndet(mainio, FM7_MAINIO_MIDI_SYNDET, 0xffffffff);
529         }
530         
531 #if defined(_FM8) || defined(_FM7) || defined(_FMNEW7)
532         if((config.dipswitch & FM7_DIPSW_CONNECT_KANJIROM) != 0) {
533                 mainio->set_context_kanjirom_class1(kanjiclass1);
534         }
535 #else
536         mainio->set_context_kanjirom_class1(kanjiclass1);
537 #endif  
538         mainio->set_context_mainmem(mainmem);
539         mainio->set_context_keyboard(keyboard);
540         mainio->set_context_printer(printer);
541         mainio->set_context_printer_reset(printer, SIG_PRINTER_RESET, 0xffffffff);
542         mainio->set_context_printer_strobe(printer, SIG_PRINTER_STROBE, 0xffffffff);
543         mainio->set_context_printer_select(printer, SIG_PRINTER_SELECT, 0xffffffff);
544 #if defined(CAPABLE_KANJI_CLASS2)
545         mainio->set_context_kanjirom_class2(kanjiclass2);
546 #endif
547 #if defined(_FM8)
548         for(int i = 0; i < 2; i++) mainio->set_context_bubble(bubble_casette[i], i);
549 #endif  
550         keyboard->set_context_break_line(mainio, FM7_MAINIO_PUSH_BREAK, 0xffffffff);
551         keyboard->set_context_int_line(mainio, FM7_MAINIO_KEYBOARDIRQ, 0xffffffff);
552         keyboard->set_context_int_line(display, SIG_FM7_SUB_KEY_FIRQ, 0xffffffff);
553 #if defined(_FM77AV_VARIANTS)
554         keyboard->set_context_beep(keyboard_beep);
555 #endif  
556         keyboard->set_context_rxrdy(display, SIG_FM7KEY_RXRDY, 0x01);
557         keyboard->set_context_key_ack(display, SIG_FM7KEY_ACK, 0x01);
558    
559         if(drec != nullptr) {
560                 drec->set_context_ear(mainio, FM7_MAINIO_CMT_RECV, 0xffffffff);
561                 //drec->set_context_remote(mainio, FM7_MAINIO_CMT_REMOTE, 0xffffffff);
562                 mainio->set_context_datarec(drec);
563         }
564         mainmem->set_context_mainio(mainio);
565         mainmem->set_context_display(display);
566         mainmem->set_context_maincpu(maincpu);
567 #if defined(CAPABLE_DICTROM)
568         mainmem->set_context_kanjirom_class1(kanjiclass1);
569 #endif  
570         display->set_context_mainio(mainio);
571         display->set_context_subcpu(subcpu);
572         display->set_context_keyboard(keyboard);
573 #if defined(_FM77L4)
574         display->set_context_l4crtc(l4crtc);
575 #endif
576         
577         mainio->set_context_clock_status(mainmem, FM7_MAINIO_CLOCKMODE, 0xffffffff);
578         mainio->set_context_clock_status(display, SIG_DISPLAY_CLOCK, 0xffffffff);
579
580         g_substat_display->set_mask(SIG_AND_BIT_0);
581         g_substat_display->set_mask(SIG_AND_BIT_1);
582         subcpu->set_context_bus_ba(g_substat_display, SIG_AND_BIT_0, 0xffffffff);
583         subcpu->set_context_bus_bs(g_substat_display, SIG_AND_BIT_1, 0xffffffff);
584         g_substat_display->set_context_out(display, SIG_FM7_SUB_HALT, 0xffffffff);
585
586         g_substat_mainhalt->set_mask(SIG_AND_BIT_0);
587         g_substat_mainhalt->set_mask(SIG_AND_BIT_1);
588         subcpu->set_context_bus_ba(g_substat_mainhalt, SIG_AND_BIT_0, 0xffffffff);
589         subcpu->set_context_bus_bs(g_substat_mainhalt, SIG_AND_BIT_1, 0xffffffff);
590         g_substat_mainhalt->set_context_out(mainmem, SIG_FM7_SUB_HALT, 0xffffffff);
591
592 #if defined(_FM77_VARIANTS) || defined(_FM77AV_VARIANTS)
593         display->set_context_kanjiclass1(kanjiclass1);
594 #endif  
595 #if defined(CAPABLE_KANJI_CLASS2)
596         display->set_context_kanjiclass2(kanjiclass2);
597 #endif   
598 #if defined(_FM77AV_VARIANTS)
599         display->set_context_alu(alu);
600         alu->set_context_memory(display);
601         alu->set_direct_access_offset(DISPLAY_VRAM_DIRECT_ACCESS);
602 #endif  
603         // Palette, VSYNC, HSYNC, Multi-page, display mode. 
604         mainio->set_context_display(display);
605  
606 #if defined(_FM8) || (_FM7) || (_FMNEW7)
607         if(connect_320kfdc) {
608 #endif
609                 if(fdc != nullptr) {
610                         //FDC
611                         fdc->set_context_irq(mainio, FM7_MAINIO_FDC_IRQ, 0x1);
612                         fdc->set_context_drq(mainio, FM7_MAINIO_FDC_DRQ, 0x1);
613                         fdc->set_context_rdy(mainio, SIG_FM7_MAINIO_FDC_MOTOR, 0xffffffff); // OK?
614                         mainio->set_context_fdc(fdc);
615                 }
616 #if defined(_FM8) || (_FM7) || (_FMNEW7)
617         }
618 #endif  
619 #if defined(HAS_2HD)
620         if(connect_1Mfdc && (fdc_2HD != nullptr)) {
621                 //FDC
622                 fdc_2HD->set_context_irq(mainio, FM7_MAINIO_FDC_IRQ_2HD, 0x1);
623                 fdc_2HD->set_context_drq(mainio, FM7_MAINIO_FDC_DRQ_2HD, 0x1);
624                 fdc_2HD->set_context_rdy(mainio, SIG_FM7_MAINIO_FDC_MOTOR_2HD, 0xffffffff); 
625                 mainio->set_context_fdc_2HD(fdc_2HD);
626         }
627 #endif  
628         // SOUND
629         mainio->set_context_beep(pcm1bit);
630 #if defined(_FM8)       
631         mainio->set_context_psg(psg);
632 #else
633         #if !defined(_FM77AV_VARIANTS)
634         mainio->set_context_psg(psg);
635         #endif
636         opn[0]->set_context_irq(mainio, FM7_MAINIO_OPN_IRQ, 0xffffffff);
637         mainio->set_context_opn(opn[0], 0);
638         joystick->set_context_opn(opn[0]);
639         mainio->set_context_joystick(joystick);
640         opn[0]->set_context_port_b(joystick, FM7_JOYSTICK_MOUSE_STROBE, 0xff, 0);
641         
642         opn[1]->set_context_irq(mainio, FM7_MAINIO_WHG_IRQ, 0xffffffff);
643         mainio->set_context_opn(opn[1], 1);
644         opn[2]->set_context_irq(mainio, FM7_MAINIO_THG_IRQ, 0xffffffff);
645         mainio->set_context_opn(opn[2], 2);
646 #endif   
647         //subcpu->set_context_bus_clr(display, SIG_FM7_SUB_USE_CLR, 0x0000000f);
648    
649         event->register_frame_event(joystick);
650 #if defined(HAS_DMA)
651         dmac->set_context_src(fdc, 0);
652         dmac->set_context_dst(mainmem, 0);
653         dmac->set_context_int_line(mainio, FM7_MAINIO_DMA_INT, 0xffffffff);
654         dmac->set_context_busreq_line(maincpu, 1, SIG_CPU_BUSREQ, 0xffffffff);
655         mainio->set_context_dmac(dmac);
656 #endif
657         
658         // MEMORIES must set before initialize().
659         //dummycpu->set_context_mem(dummy);
660         maincpu->set_context_mem(mainmem);
661         subcpu->set_context_mem(display);
662         //dummycpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
663 #ifdef WITH_Z80
664         if(z80cpu != nullptr) z80cpu->set_context_mem(mainmem);
665 #endif
666 #if defined(CAPABLE_JCOMMCARD)
667         if((jsubcpu != nullptr) && (jcommcard != nullptr)) {
668                 jsubcpu->set_context_mem(jcommcard);
669         }
670 #endif
671 #ifdef USE_DEBUGGER
672         maincpu->set_context_debugger(new DEBUGGER(this, emu));
673         subcpu->set_context_debugger(new DEBUGGER(this, emu));
674         #ifdef WITH_Z80
675         if(z80cpu != nullptr) z80cpu->set_context_debugger(new DEBUGGER(this, emu));
676         #endif
677         #if defined(CAPABLE_JCOMMCARD)
678         if(jsubcpu != nullptr) {
679                 jsubcpu->set_context_debugger(new DEBUGGER(this, emu));
680         }
681         #endif
682 #endif
683 #if defined(__GIT_REPO_VERSION)
684         set_git_repo_version(__GIT_REPO_VERSION);
685 #endif
686         initialize_devices();
687         
688 #if defined(WITH_Z80)
689         if(g_intr_irq != nullptr) g_intr_irq->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_IRQ_ON) != 0) ? 1 : 0, 1);
690         if(g_intr_firq != nullptr) g_intr_firq->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_FIRQ_ON) != 0) ? 1 : 0, 1);
691         if(g_nmi != nullptr) g_nmi->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_NMI_ON) != 0) ? 1 : 0, 1);
692 #endif
693         // Disks
694 #if defined(_FM8) || (_FM7) || (_FMNEW7)
695         if(connect_320kfdc) {
696 #endif
697                 if(fdc != nullptr) {
698                         for(int i = 0; i < 4; i++) {
699 #if defined(_FM77AV20) || defined(_FM77AV20EX) || \
700         defined(_FM77AV40) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
701                                 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
702 #else
703                                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
704 #endif
705                                 fdc->set_drive_mfm(i, true);
706                                 fdc->set_drive_rpm(i, 300);
707                         }
708                 }
709 #if defined(_FM8) || (_FM7) || (_FMNEW7)
710         }
711 #endif  
712         
713 #if defined(HAS_2HD)
714         if(connect_1Mfdc && (fdc_2HD != nullptr)) {
715 // ToDo: Implement another FDC for 1MB (2HD or 8''), this is used by FM-8 to FM-77? Not FM77AV or later? I still know this.
716                 for(int i = 0; i < 2; i++) {
717                         fdc_2HD->set_drive_type(i, DRIVE_TYPE_2HD);
718                         fdc_2HD->set_drive_mfm(i, true);
719                         fdc_2HD->set_drive_rpm(i, 300);
720                 }
721         }
722 #endif  
723 }  
724
725 void VM::update_config()
726 {
727         VM_TEMPLATE::update_config();
728         update_dipswitch();
729 }
730
731 void VM::reset()
732 {
733         // reset all devices
734         VM_TEMPLATE::reset();
735         
736 #if !defined(_FM77AV_VARIANTS) || defined(_FM8)
737         #if defined(USE_AY_3_8910_AS_PSG)
738         psg->set_reg(0x27, 0);  // set prescaler
739         psg->set_reg(0x2e, 0);  // set prescaler
740         psg->write_signal(SIG_AY_3_891X_MUTE, 0x00, 0x01); // Okay?
741         #else   
742         psg->set_reg(0x27, 0); // stop timer
743         psg->set_reg(0x2e, 0);  // set prescaler
744         psg->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
745         #endif
746 #endif
747 #if !defined(_FM8)
748         for(int i = 0; i < 3; i++) {
749                 opn[i]->set_reg(0x27, 0); // stop timer
750                 opn[i]->set_reg(0x2e, 0);       // set prescaler
751                 opn[i]->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
752         }
753 #endif
754         //display->reset(); // 20180618 K.O for RELICS
755 }
756
757 void VM::special_reset(int num)
758 {
759         // BREAK + RESET
760         mainio->reset();
761         mainmem->reset();
762         //mainio->write_signal(FM7_MAINIO_PUSH_BREAK, 1, 1);
763         //keyboard->write_signal(SIG_FM7KEY_OVERRIDE_PRESS_BREAK, 0xffffffff, 0xffffffff);
764         
765 #if defined(FM77AV_VARIANTS)    
766         mainio->write_signal(FM7_MAINIO_HOT_RESET, 1, 1);
767 #endif  
768         display->reset();
769         subcpu->reset();
770         maincpu->reset();
771         mainio->write_signal(FM7_MAINIO_PUSH_BREAK, 1, 1);
772         keyboard->write_signal(SIG_FM7KEY_OVERRIDE_PRESS_BREAK, 0xffffffff, 0xffffffff);
773         event->register_event(mainio, EVENT_UP_BREAK, 1000.0 * 1000.0, false, nullptr);
774 }
775
776 void VM::run()
777 {
778         __LIKELY_IF(event != nullptr) {
779                 event->drive();
780         }
781 }
782
783 double VM::get_frame_rate()
784 {
785         __LIKELY_IF(event != nullptr) {
786                 return event->get_frame_rate();
787         }
788         return VM_TEMPLATE::get_frame_rate();
789 }
790
791 void VM::set_vm_frame_rate(double fps)
792 {
793         __LIKELY_IF(event != nullptr) {
794                 event->set_frames_per_sec(fps);
795         }
796 }
797
798 double VM::get_current_usec()
799 {
800         __LIKELY_IF(event != nullptr) {
801                 return event->get_current_usec();
802         }
803         return VM_TEMPLATE::get_current_usec();
804 }
805
806 uint64_t VM::get_current_clock_uint64()
807 {
808         __LIKELY_IF(event != nullptr) {
809                 return event->get_current_clock_uint64();
810         }
811         return VM_TEMPLATE::get_current_clock_uint64();
812 }
813
814
815
816 // ----------------------------------------------------------------------------
817 // debugger
818 // ----------------------------------------------------------------------------
819
820 #ifdef USE_DEBUGGER
821 DEVICE *VM::get_cpu(int index)
822 {
823         if(index == 0) {
824                 return maincpu;
825         } else if(index == 1) {
826                 return subcpu;
827         }
828         #if defined(WITH_Z80)
829         else if(index == 2) {
830                 #if defined(CAPABLE_JCOMMCARD)
831                 if(z80cpu == nullptr) {
832                         return jsubcpu;
833                 }
834                 #endif
835                 return z80cpu;
836         }
837                 #if defined(CAPABLE_JCOMMCARD)
838         else if(index == 3) {
839                 return jsubcpu;
840         }
841                 #endif
842         #else
843                 #if defined(CAPABLE_JCOMMCARD)
844         else if(index == 2) {
845                 return jsubcpu;
846         }
847                 #endif
848         #endif
849         return nullptr;
850 }
851 #endif
852
853 // ----------------------------------------------------------------------------
854 // draw screen
855 // ----------------------------------------------------------------------------
856
857 void VM::draw_screen()
858 {
859         __UNLIKELY_IF(display == nullptr) return;
860         display->draw_screen();
861 }
862
863 void VM::initialize_sound(int rate, int samples)
864 {
865         // init sound manager
866         if(event != nullptr) {
867                 event->initialize_sound(rate, samples);
868         }
869         // init sound gen
870 #if defined(_FM8)
871         if(psg != nullptr) {
872                 psg->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
873         }
874 #else
875         if(opn[0] != nullptr) {
876                 opn[0]->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
877         }
878         if(opn[1] != nullptr) {
879                 opn[1]->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
880         }
881         if(opn[2] != nullptr) {
882                 opn[2]->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
883         }
884         # if !defined(_FM77AV_VARIANTS)   
885         if(psg != nullptr) {
886                 psg->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
887                 #if defined(USE_AY_3_8910_AS_PSG)
888                 psg->set_low_pass_filter_freq(4500);
889                 psg->set_high_pass_filter_freq(50);
890                 #endif
891         }
892         #endif
893         #if defined(_FM77AV_VARIANTS)
894         if(keyboard_beep != nullptr) {
895                 keyboard_beep->initialize_sound(rate, 2400.0, 512);
896         }
897         # endif
898 #endif
899         if(pcm1bit != nullptr) {
900                 pcm1bit->initialize_sound(rate, 8000);
901                 pcm1bit->set_high_pass_filter_freq(10);
902                 pcm1bit->set_low_pass_filter_freq(1500);
903         }
904         //drec->initialize_sound(rate, 0);
905 }
906
907 uint16_t* VM::create_sound(int* extra_frames)
908 {
909         __LIKELY_IF(event != nullptr) {
910                 uint16_t* p = event->create_sound(extra_frames);
911                 return p;
912         }
913         return VM_TEMPLATE::create_sound(extra_frames);
914 }
915
916 int VM::get_sound_buffer_ptr()
917 {
918         __LIKELY_IF(event != nullptr) {
919                 int pos = event->get_sound_buffer_ptr();
920                 return pos;
921         }
922         return VM_TEMPLATE::get_sound_buffer_ptr();
923 }
924
925 #ifdef USE_SOUND_VOLUME
926 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
927 {
928 #if !defined(_FM77AV_VARIANTS)
929         if(ch-- == 0) {
930                 if(psg != nullptr) {
931                         psg->set_volume(0, decibel_l, decibel_r);
932                         psg->set_volume(1, decibel_l, decibel_r);
933                 }
934         } else
935 #endif
936 #if !defined(_FM8)              
937         if(ch-- == 0) {
938                 if(opn[0] != nullptr) {
939                         opn[0]->set_volume(0, decibel_l, decibel_r);
940                 }
941         } else if(ch-- == 0) {
942                 if(opn[0] != nullptr) {
943                         opn[0]->set_volume(1, decibel_l, decibel_r);
944                 }
945         } else if(ch-- == 0) {
946                 if(opn[1] != nullptr) {
947                         opn[1]->set_volume(0, decibel_l, decibel_r);
948                 }
949         } else if(ch-- == 0) {
950                 if(opn[1] != nullptr) {
951                         opn[1]->set_volume(1, decibel_l, decibel_r);
952                 }
953         } else if(ch-- == 0) {
954                 if(opn[2] != nullptr) {
955                         opn[2]->set_volume(0, decibel_l, decibel_r);
956                 }
957         } else if(ch-- == 0) {
958                 if(opn[2] != nullptr) {
959                         opn[2]->set_volume(1, decibel_l, decibel_r);
960                 }
961         } else
962 #endif  
963         if(ch-- == 0) {
964                 if(pcm1bit != nullptr) {
965                         pcm1bit->set_volume(0, decibel_l, decibel_r);
966                 }
967         } else if(ch-- == 0) {
968                 if(drec != nullptr) {
969                         drec->set_volume(0, decibel_l, decibel_r);
970                 }
971         }
972 #if defined(_FM77AV_VARIANTS)
973         else if(ch-- == 0) {
974                 if(keyboard_beep != nullptr) {
975                         keyboard_beep->set_volume(0, decibel_l, decibel_r);
976                 }
977         }
978 #endif
979         else if(ch-- == 0) {
980                 if(fdc != nullptr) {
981                         fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
982                         fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
983                         fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
984                 }
985         } 
986 #if defined(HAS_2HD)
987         else if(ch-- == 0) {
988                 if(fdc_2HD != nullptr) {
989                         fdc_2HD->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
990                         fdc_2HD->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
991                         fdc_2HD->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
992                 }
993         } 
994 #endif
995         else if(ch-- == 0) {
996                 if(drec != nullptr) {
997                         drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
998                         drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
999                         drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
1000                 }
1001         }
1002 }
1003 #endif
1004
1005 // ----------------------------------------------------------------------------
1006 // notify key
1007 // ----------------------------------------------------------------------------
1008
1009 void VM::key_down(int code, bool repeat)
1010 {
1011         __UNLIKELY_IF(keyboard == nullptr) return;
1012         if(!repeat) {
1013                 keyboard->key_down(code);
1014         }
1015 }
1016
1017 void VM::key_up(int code)
1018 {
1019         __UNLIKELY_IF(keyboard == nullptr) return;
1020         keyboard->key_up(code);
1021 }
1022
1023 bool VM::get_caps_locked()
1024 {
1025         __UNLIKELY_IF(keyboard == nullptr) return false;
1026         return keyboard->get_caps_locked();
1027 }
1028
1029 bool VM::get_kana_locked()
1030 {
1031         __UNLIKELY_IF(keyboard == nullptr) return false;
1032         return keyboard->get_kana_locked();
1033 }
1034
1035 // Get INS status.Important with FM-7 series (^_^;
1036 uint32_t VM::get_led_status()
1037 {
1038         __UNLIKELY_IF(keyboard == nullptr) return 0;
1039         return keyboard->read_signal(SIG_FM7KEY_LED_STATUS);
1040 }
1041
1042
1043 // ----------------------------------------------------------------------------
1044 // user interface
1045 // ----------------------------------------------------------------------------
1046
1047 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
1048 {
1049         if(drv < 0) return;
1050 #if defined(HAS_2HD)
1051         if(drv < 2) {
1052                 if(fdc != nullptr) {
1053                         fdc->open_disk(drv, file_path, bank);
1054                 }
1055         } else {
1056                 if(fdc_2HD != nullptr) {
1057                         fdc_2HD->open_disk(drv - 2, file_path, bank);
1058                 }
1059         }
1060 #else
1061         if(fdc != nullptr) {
1062                 fdc->open_disk(drv, file_path, bank);
1063         }
1064 #endif
1065         //display->reset(); // 20180618 K.O for RELICS
1066 }
1067
1068 void VM::close_floppy_disk(int drv)
1069 {
1070 #if defined(HAS_2HD)
1071         if(drv < 2) {
1072                 if(fdc != nullptr) fdc->close_disk(drv);
1073         } else {
1074                 if(fdc_2HD != nullptr) fdc_2HD->close_disk(drv - 2);
1075         }               
1076 #else
1077         if(fdc != nullptr) {
1078                 fdc->close_disk(drv);
1079         }
1080 #endif
1081 }
1082
1083 bool VM::is_floppy_disk_inserted(int drv)
1084 {
1085 #if defined(HAS_2HD)
1086         if((fdc != nullptr) && (drv < 2)) {
1087                 return fdc->is_disk_inserted(drv);
1088         } else if(fdc_2HD != nullptr) {
1089                 return fdc_2HD->is_disk_inserted(drv - 2);
1090         } else {
1091                 return false;
1092         }
1093 #else
1094         if(fdc != nullptr) {
1095                 return fdc->is_disk_inserted(drv);
1096         } else {
1097                 return false;
1098         }
1099 #endif
1100 }
1101
1102 void VM::is_floppy_disk_protected(int drv, bool value)
1103 {
1104 #if defined(HAS_2HD)
1105         if((fdc != nullptr) && (drv < 2)) {
1106                 fdc->is_disk_protected(drv, value);
1107         } else if(fdc_2HD != nullptr) {
1108                 fdc_2HD->is_disk_protected(drv - 2, value);
1109         }               
1110 #else
1111         if(fdc != nullptr) {
1112                 fdc->is_disk_protected(drv, value);
1113         }
1114 #endif
1115 }
1116
1117 bool VM::is_floppy_disk_protected(int drv)
1118 {
1119 #if defined(HAS_2HD)
1120         if((fdc != nullptr) && (drv < 2)) {
1121                 return fdc->is_disk_protected(drv);
1122         } else if(fdc_2HD != nullptr) {
1123                 return fdc_2HD->is_disk_protected(drv);
1124         } else {
1125                 return false;
1126         }
1127 #else
1128         if(fdc != nullptr) {
1129                 return fdc->is_disk_protected(drv);
1130         } else {
1131                 return false;
1132         }
1133 #endif
1134 }
1135
1136 uint32_t VM::is_floppy_disk_accessed()
1137 {
1138         uint32_t v = 0;
1139 #if defined(HAS_2HD)
1140         uint32_t v1, v2;
1141         v1 = v2 = 0;
1142 # if defined(_FM8) || (_FM7) || (_FMNEW7)
1143         if(connect_320kfdc) {
1144 # endif
1145                 if(fdc != nullptr) v1 = fdc->read_signal(0);
1146 # if defined(_FM8) || (_FM7) || (_FMNEW7)
1147         }
1148 # endif
1149         if(connect_1Mfdc) {
1150                 if(fdc_2HD != nullptr) v2 = fdc_2HD->read_signal(0);
1151         }
1152         v1 = v1 & 0x03;
1153         v2 = (v2 & 0x03) << 2;
1154         v = v1 | v2;
1155         return v;
1156 #else
1157 # if defined(_FM8) || (_FM7) || (_FMNEW7)
1158         if(connect_320kfdc) {
1159 # endif         
1160                 v = fdc->read_signal(0);
1161 # if defined(_FM8) || (_FM7) || (_FMNEW7)
1162         }
1163 # endif
1164         return v;
1165 #endif
1166 }
1167
1168 void VM::play_tape(int drv, const _TCHAR* file_path)
1169 {
1170         if(drec != nullptr) {
1171                 bool remote = drec->get_remote();
1172                 
1173                 if(drec->play_tape(file_path) && remote) {
1174                         // if machine already sets remote on, start playing now
1175                         push_play(drv);
1176                 }
1177         }
1178 }
1179
1180 void VM::rec_tape(int drv, const _TCHAR* file_path)
1181 {
1182         if(drec != nullptr) {
1183                 bool remote = drec->get_remote();
1184                 
1185                 if(drec->rec_tape(file_path) && remote) {
1186                         // if machine already sets remote on, start recording now
1187                         push_play(drv);
1188                 }
1189         }
1190 }
1191
1192 void VM::close_tape(int drv)
1193 {
1194         if(drec != nullptr) {
1195                 emu->lock_vm();
1196                 drec->close_tape();
1197                 emu->unlock_vm();
1198                 drec->set_remote(false);
1199         }
1200 }
1201
1202 bool VM::is_tape_inserted(int drv)
1203 {
1204         if(drec != nullptr) {
1205                 return drec->is_tape_inserted();
1206         }
1207         return false;
1208 }
1209
1210 bool VM::is_tape_playing(int drv)
1211 {
1212         if(drec != nullptr) {
1213                 return drec->is_tape_playing();
1214         }
1215         return false;
1216 }
1217
1218 bool VM::is_tape_recording(int drv)
1219 {
1220         if(drec != nullptr) {
1221                 return drec->is_tape_recording();
1222         }
1223         return false;
1224 }
1225
1226 int VM::get_tape_position(int drv)
1227 {
1228         __LIKELY_IF(drec != nullptr) {
1229                 return drec->get_tape_position();
1230         }
1231         return 0;
1232 }
1233
1234 const _TCHAR* VM::get_tape_message(int drv)
1235 {
1236         __LIKELY_IF(drec != nullptr) {
1237                 return drec->get_message();
1238         }
1239         return nullptr;
1240 }
1241
1242 void VM::push_play(int drv)
1243 {
1244         if(drec != nullptr) {
1245                 drec->set_remote(false);
1246                 drec->set_ff_rew(0);
1247                 drec->set_remote(true);
1248         }
1249 }
1250
1251
1252 void VM::push_stop(int drv)
1253 {
1254         if(drec != nullptr) {
1255                 drec->set_remote(false);
1256         }
1257 }
1258
1259 void VM::push_fast_forward(int drv)
1260 {
1261         if(drec != nullptr) {
1262                 drec->set_remote(false);
1263                 drec->set_ff_rew(1);
1264                 drec->set_remote(true);
1265         }
1266 }
1267
1268 void VM::push_fast_rewind(int drv)
1269 {
1270         if(drec != nullptr) {
1271                 drec->set_remote(false);
1272                 drec->set_ff_rew(-1);
1273                 drec->set_remote(true);
1274         }
1275 }
1276
1277 void VM::push_apss_forward(int drv)
1278 {
1279         if(drec != nullptr) {
1280                 drec->do_apss(1);
1281         }
1282 }
1283
1284 void VM::push_apss_rewind(int drv)
1285 {
1286         __UNLIKELY_IF(drec != nullptr) {
1287                 drec->do_apss(-1);
1288         }
1289 }
1290
1291 bool VM::is_frame_skippable()
1292 {
1293         __UNLIKELY_IF(event == nullptr) {
1294                 return false;
1295         }
1296         return event->is_frame_skippable();
1297 }
1298
1299 void VM::update_dipswitch()
1300 {
1301 #if defined(WITH_Z80)
1302         if(g_intr_irq != nullptr) g_intr_irq->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_IRQ_ON) != 0) ? 1 : 0, 1);
1303         if(g_intr_firq != nullptr) g_intr_firq->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_FIRQ_ON) != 0) ? 1 : 0, 1);
1304         if(g_nmi != nullptr) g_nmi->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_NMI_ON) != 0) ? 1 : 0, 1);
1305 #endif
1306 }
1307
1308 void VM::set_cpu_clock(DEVICE *cpu, uint32_t clocks)
1309 {
1310         __LIKELY_IF(event != nullptr) {
1311                 event->set_secondary_cpu_clock(cpu, clocks);
1312         }
1313 }
1314
1315 #if defined(USE_BUBBLE)
1316 void VM::open_bubble_casette(int drv, const _TCHAR *path, int bank)
1317 {
1318         __UNLIKELY_IF((drv >= 2) || (drv < 0)) return;
1319         __UNLIKELY_IF(bubble_casette[drv] == nullptr) return;
1320         bubble_casette[drv]->open((_TCHAR *)path, bank);
1321 }
1322
1323 void VM::close_bubble_casette(int drv)
1324 {
1325         __UNLIKELY_IF((drv >= 2) || (drv < 0)) return;
1326         __UNLIKELY_IF(bubble_casette[drv] == nullptr) return;
1327         bubble_casette[drv]->close();
1328 }
1329
1330 bool VM::is_bubble_casette_inserted(int drv)
1331 {
1332         if((drv >= 2) || (drv < 0)) return false;
1333         if(bubble_casette[drv] == nullptr) return false;
1334         return bubble_casette[drv]->is_bubble_inserted();
1335 }
1336
1337 bool VM::is_bubble_casette_protected(int drv)
1338 {
1339         if((drv >= 2) || (drv < 0)) return false;
1340         if(bubble_casette[drv] == nullptr) return false;
1341         return bubble_casette[drv]->is_bubble_protected();
1342 }
1343
1344 void VM::is_bubble_casette_protected(int drv, bool flag)
1345 {
1346         if((drv >= 2) || (drv < 0)) return;
1347         if(bubble_casette[drv] == nullptr) return;
1348         bubble_casette[drv]->set_bubble_protect(flag);
1349 }
1350 #endif
1351
1352 #define STATE_VERSION   12
1353
1354 bool VM::process_state(FILEIO* state_fio, bool loading)
1355 {
1356         if(!(VM_TEMPLATE::process_state_core(state_fio, loading, STATE_VERSION))) {
1357                 return false;
1358         }
1359         // Machine specified.
1360         state_fio->StateValue(connect_320kfdc);
1361         state_fio->StateValue(connect_1Mfdc);
1362         if(loading) {
1363                 update_config();
1364         }
1365         //mainio->restore_opn();
1366         return true;
1367 }
1368 #ifdef SUPPORT_QUERY_PHY_KEY_NAME
1369 int VM::get_key_name_table_size(void)
1370 {
1371         if(keyboard != nullptr) {
1372                 return keyboard->get_key_name_table_size();
1373         }
1374         return 0;
1375 }
1376 const _TCHAR *VM::get_phy_key_name_by_scancode(uint32_t scancode)
1377 {
1378         if(keyboard != nullptr) {
1379                 return keyboard->get_phy_key_name_by_scancode(scancode);
1380         }
1381         return (const _TCHAR *)nullptr;
1382 }
1383         
1384 const _TCHAR *VM::get_phy_key_name_by_vk(uint32_t vk)
1385 {
1386         if(keyboard != nullptr) {
1387                 return keyboard->get_phy_key_name_by_vk(vk);
1388         }
1389         return (const _TCHAR *)nullptr;
1390 }
1391
1392 uint32_t VM::get_scancode_by_vk(uint32_t vk)
1393 {
1394         if(keyboard != nullptr) {
1395                 return keyboard->get_scancode_by_vk(vk);
1396         }
1397         return 0xffffffff;
1398 }
1399
1400 uint32_t VM::get_vk_by_scancode(uint32_t scancode)
1401 {
1402         if(keyboard != nullptr) {
1403                 return keyboard->get_vk_by_scancode(scancode);
1404         }
1405         return 0xffffffff;
1406 }
1407 #endif
1408
1409 #ifdef USE_DIG_RESOLUTION
1410 void VM::get_screen_resolution(int *w, int *h)
1411 {
1412         switch(display->get_screen_mode()) {
1413         case DISPLAY_MODE_8_200L:
1414                 *w = 640;
1415                 *h = 200;
1416                 break;
1417         case DISPLAY_MODE_8_400L:
1418         case DISPLAY_MODE_1_400L:
1419                 *w = 640;
1420                 *h = 400;
1421                 break;
1422         case DISPLAY_MODE_4096:
1423         case DISPLAY_MODE_256k:
1424                 *w = 320;
1425                 *h = 200;
1426                 break;
1427         default:
1428                 *w = 640;
1429                 *h = 200;
1430                 break;
1431         }
1432 }
1433 #endif
1434
1435 bool VM::is_screen_changed()
1436 {
1437         bool f = true;
1438 #if defined(USE_MINIMUM_RENDERING)
1439         f = display->screen_update();
1440         display->reset_screen_update();
1441 #endif  
1442         return f;
1443 }