OSDN Git Service

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