OSDN Git Service

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