OSDN Git Service

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