OSDN Git Service

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