OSDN Git Service

6f0ff04d6b689943846da09976d3ce6b5a98a620
[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         for(DEVICE* device = first_device; device; device = device->next_device) {
661                 device->initialize();
662         }
663
664 #if defined(WITH_Z80)
665         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);
666         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);
667         if(g_nmi != NULL) g_nmi->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_NMI_ON) != 0) ? 1 : 0, 1);
668 #endif
669         // Disks
670 #if defined(_FM8) || (_FM7) || (_FMNEW7)
671         if(connect_320kfdc) {
672 #endif
673                 if(fdc != NULL) {
674                         for(int i = 0; i < 4; i++) {
675 #if defined(_FM77AV20) || defined(_FM77AV20EX) || \
676         defined(_FM77AV40SX) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
677                                 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
678 #else
679                                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
680 #endif
681                                 fdc->set_drive_mfm(i, true);
682                                 fdc->set_drive_rpm(i, 300);
683                         }
684                 }
685 #if defined(_FM8) || (_FM7) || (_FMNEW7)
686         }
687 #endif  
688         
689 #if defined(HAS_2HD)
690         if(connect_1Mfdc && (fdc_2HD != NULL)) {
691 // 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.
692                 for(int i = 0; i < 2; i++) {
693                         fdc_2HD->set_drive_type(i, DRIVE_TYPE_2HD);
694                         fdc_2HD->set_drive_mfm(i, true);
695                         fdc_2HD->set_drive_rpm(i, 300);
696                 }
697         }
698 #endif  
699 }  
700
701 void VM::update_config()
702 {
703         for(DEVICE* device = first_device; device; device = device->next_device) {
704                 device->update_config();
705         }
706         update_dipswitch();
707 }
708
709 void VM::reset()
710 {
711         // reset all devices
712         for(DEVICE* device = first_device; device; device = device->next_device) {
713                 device->reset();
714         }
715 #if !defined(_FM77AV_VARIANTS) || defined(_FM8)
716 # if defined(USE_AY_3_8910_AS_PSG)
717         psg->set_reg(0x2e, 0);  // set prescaler
718         psg->write_signal(SIG_AY_3_891X_MUTE, 0x00, 0x01); // Okay?
719 # else  
720         psg->set_reg(0x27, 0); // stop timer
721         psg->set_reg(0x2e, 0);  // set prescaler
722         psg->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
723 #endif
724 #endif
725 #if !defined(_FM8)
726         for(int i = 0; i < 3; i++) {
727                 opn[i]->set_reg(0x27, 0); // stop timer
728                 opn[i]->set_reg(0x2e, 0);       // set prescaler
729                 opn[i]->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
730         }
731 #endif
732         //display->reset(); // 20180618 K.O for RELICS
733 }
734
735 void VM::special_reset()
736 {
737         // BREAK + RESET
738         mainio->reset();
739         mainmem->reset();
740         //mainio->write_signal(FM7_MAINIO_PUSH_BREAK, 1, 1);
741         //keyboard->write_signal(SIG_FM7KEY_OVERRIDE_PRESS_BREAK, 0xffffffff, 0xffffffff);
742         
743 #if defined(FM77AV_VARIANTS)    
744         mainio->write_signal(FM7_MAINIO_HOT_RESET, 1, 1);
745 #endif  
746         display->reset();
747         subcpu->reset();
748         maincpu->reset();
749         mainio->write_signal(FM7_MAINIO_PUSH_BREAK, 1, 1);
750         keyboard->write_signal(SIG_FM7KEY_OVERRIDE_PRESS_BREAK, 0xffffffff, 0xffffffff);
751         event->register_event(mainio, EVENT_UP_BREAK, 1000.0 * 1000.0, false, NULL);
752 }
753
754 void VM::run()
755 {
756         event->drive();
757 }
758
759 double VM::get_frame_rate()
760 {
761         return event->get_frame_rate();
762 }
763
764
765
766 // ----------------------------------------------------------------------------
767 // debugger
768 // ----------------------------------------------------------------------------
769
770 #ifdef USE_DEBUGGER
771 DEVICE *VM::get_cpu(int index)
772 {
773         if(index == 0) {
774                 return maincpu;
775         } else if(index == 1) {
776                 return subcpu;
777         }
778 #if defined(WITH_Z80)
779         else if(index == 2) {
780 # if defined(CAPABLE_JCOMMCARD)
781                 if(z80cpu == NULL) {
782                         return jsubcpu;
783                 }
784 # endif
785                 return z80cpu;
786         }
787 # if defined(CAPABLE_JCOMMCARD)
788         else if(index == 3) {
789                 return jsubcpu;
790         }
791 # endif
792 #else
793 # if defined(CAPABLE_JCOMMCARD)
794         else if(index == 2) {
795                 return jsubcpu;
796         }
797 # endif
798 #endif
799         return NULL;
800 }
801 #endif
802
803 // ----------------------------------------------------------------------------
804 // draw screen
805 // ----------------------------------------------------------------------------
806
807 void VM::draw_screen()
808 {
809         display->draw_screen();
810 }
811
812 void VM::initialize_sound(int rate, int samples)
813 {
814         // init sound manager
815         event->initialize_sound(rate, samples);
816         // init sound gen
817 #if defined(_FM8)
818         psg->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
819 #else   
820         opn[0]->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
821         opn[1]->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
822         opn[2]->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
823 # if !defined(_FM77AV_VARIANTS)   
824         psg->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
825 # endif
826 # if defined(_FM77AV_VARIANTS)
827         keyboard_beep->initialize_sound(rate, 2400.0, 512);
828 # endif
829 #endif  
830         pcm1bit->initialize_sound(rate, 6000);
831
832         //drec->initialize_sound(rate, 0);
833 }
834
835 uint16_t* VM::create_sound(int* extra_frames)
836 {
837         uint16_t* p = event->create_sound(extra_frames);
838         return p;
839 }
840
841 int VM::get_sound_buffer_ptr()
842 {
843         int pos = event->get_sound_buffer_ptr();
844         return pos; 
845 }
846
847 #ifdef USE_SOUND_VOLUME
848 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
849 {
850 #if !defined(_FM77AV_VARIANTS)
851         if(ch-- == 0) {
852                 psg->set_volume(0, decibel_l, decibel_r);
853                 psg->set_volume(1, decibel_l, decibel_r);
854         } else
855 #endif
856 #if !defined(_FM8)              
857         if(ch-- == 0) {
858                 opn[0]->set_volume(0, decibel_l, decibel_r);
859         } else if(ch-- == 0) {
860                 opn[0]->set_volume(1, decibel_l, decibel_r);
861         } else if(ch-- == 0) {
862                 opn[1]->set_volume(0, decibel_l, decibel_r);
863         } else if(ch-- == 0) {
864                 opn[1]->set_volume(1, decibel_l, decibel_r);
865         } else if(ch-- == 0) {
866                 opn[2]->set_volume(0, decibel_l, decibel_r);
867         } else if(ch-- == 0) {
868                 opn[2]->set_volume(1, decibel_l, decibel_r);
869         } else
870 #endif  
871         if(ch-- == 0) {
872                 pcm1bit->set_volume(0, decibel_l, decibel_r);
873         } else if(ch-- == 0) {
874                 if(drec != NULL) drec->set_volume(0, decibel_l, decibel_r);
875         }
876 #if defined(_FM77AV_VARIANTS)
877         else if(ch-- == 0) {
878                 keyboard_beep->set_volume(0, decibel_l, decibel_r);
879         }
880 #endif
881         else if(ch-- == 0) {
882                 if(fdc != NULL) {
883                         fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
884                         fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
885                         fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
886                 }
887         } 
888 #if defined(HAS_2HD)
889         else if(ch-- == 0) {
890                 if(fdc_2HD != NULL) {
891                         fdc_2HD->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
892                         fdc_2HD->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
893                         fdc_2HD->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
894                 }
895         } 
896 #endif
897         else if(ch-- == 0) {
898                 if(drec != NULL) {
899                         drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
900                         drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
901                         drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
902                 }
903         }
904 }
905 #endif
906
907 // ----------------------------------------------------------------------------
908 // notify key
909 // ----------------------------------------------------------------------------
910
911 void VM::key_down(int code, bool repeat)
912 {
913         if(!repeat) {
914                 keyboard->key_down(code);
915         }
916 }
917
918 void VM::key_up(int code)
919 {
920         keyboard->key_up(code);
921 }
922
923 bool VM::get_caps_locked()
924 {
925         return keyboard->get_caps_locked();
926 }
927
928 bool VM::get_kana_locked()
929 {
930         return keyboard->get_kana_locked();
931 }
932
933 // Get INS status.Important with FM-7 series (^_^;
934 uint32_t VM::get_led_status()
935 {
936         return keyboard->read_signal(SIG_FM7KEY_LED_STATUS);
937 }
938
939
940 // ----------------------------------------------------------------------------
941 // user interface
942 // ----------------------------------------------------------------------------
943
944 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
945 {
946         if(drv < 0) return;
947 #if defined(HAS_2HD)
948         if(drv < 2) {
949                 if(fdc != NULL) {
950                         fdc->open_disk(drv, file_path, bank);
951                 }
952         } else {
953                 if(fdc_2HD != NULL) {
954                         fdc_2HD->open_disk(drv - 2, file_path, bank);
955                 }
956         }
957 #else
958         if(fdc != NULL) {
959                 fdc->open_disk(drv, file_path, bank);
960         }
961 #endif
962 }
963
964 void VM::close_floppy_disk(int drv)
965 {
966 #if defined(HAS_2HD)
967         if(drv < 2) {
968                 if(fdc != NULL) fdc->close_disk(drv);
969         } else {
970                 if(fdc_2HD != NULL) fdc_2HD->close_disk(drv - 2);
971         }               
972 #else
973         if(fdc != NULL) {
974                 fdc->close_disk(drv);
975         }
976 #endif
977 }
978
979 bool VM::is_floppy_disk_inserted(int drv)
980 {
981 #if defined(HAS_2HD)
982         if((fdc != NULL) && (drv < 2)) {
983                 return fdc->is_disk_inserted(drv);
984         } else if(fdc_2HD != NULL) {
985                 return fdc_2HD->is_disk_inserted(drv - 2);
986         } else {
987                 return false;
988         }
989 #else
990         if(fdc != NULL) {
991                 return fdc->is_disk_inserted(drv);
992         } else {
993                 return false;
994         }
995 #endif
996 }
997
998 void VM::is_floppy_disk_protected(int drv, bool value)
999 {
1000 #if defined(HAS_2HD)
1001         if((fdc != NULL) && (drv < 2)) {
1002                 fdc->is_disk_protected(drv, value);
1003         } else if(fdc_2HD != NULL) {
1004                 fdc_2HD->is_disk_protected(drv - 2, value);
1005         }               
1006 #else
1007         if(fdc != NULL) {
1008                 fdc->is_disk_protected(drv, value);
1009         }
1010 #endif
1011 }
1012
1013 bool VM::is_floppy_disk_protected(int drv)
1014 {
1015 #if defined(HAS_2HD)
1016         if((fdc != NULL) && (drv < 2)) {
1017                 return fdc->is_disk_protected(drv);
1018         } else if(fdc_2HD != NULL) {
1019                 return fdc_2HD->is_disk_protected(drv);
1020         } else {
1021                 return false;
1022         }
1023 #else
1024         if(fdc != NULL) {
1025                 return fdc->is_disk_protected(drv);
1026         } else {
1027                 return false;
1028         }
1029 #endif
1030 }
1031
1032 uint32_t VM::is_floppy_disk_accessed()
1033 {
1034         uint32_t v = 0;
1035 #if defined(HAS_2HD)
1036         uint32_t v1, v2;
1037         v1 = v2 = 0;
1038 # if defined(_FM8) || (_FM7) || (_FMNEW7)
1039         if(connect_320kfdc) {
1040 # endif
1041                 if(fdc != NULL) v1 = fdc->read_signal(0);
1042 # if defined(_FM8) || (_FM7) || (_FMNEW7)
1043         }
1044 # endif
1045         if(connect_1Mfdc) {
1046                 if(fdc_2HD != NULL) v2 = fdc_2HD->read_signal(0);
1047         }
1048         v1 = v1 & 0x03;
1049         v2 = (v2 & 0x03) << 2;
1050         v = v1 | v2;
1051         return v;
1052 #else
1053 # if defined(_FM8) || (_FM7) || (_FMNEW7)
1054         if(connect_320kfdc) {
1055 # endif         
1056                 v = fdc->read_signal(0);
1057 # if defined(_FM8) || (_FM7) || (_FMNEW7)
1058         }
1059 # endif
1060         return v;
1061 #endif
1062 }
1063
1064 void VM::play_tape(int drv, const _TCHAR* file_path)
1065 {
1066         if(drec != NULL) drec->play_tape(file_path);
1067 }
1068
1069 void VM::rec_tape(int drv, const _TCHAR* file_path)
1070 {
1071         if(drec != NULL) drec->rec_tape(file_path);
1072 }
1073
1074 void VM::close_tape(int drv)
1075 {
1076         if(drec != NULL) drec->close_tape();
1077 }
1078
1079 bool VM::is_tape_inserted(int drv)
1080 {
1081         if(drec != NULL) {
1082                 return drec->is_tape_inserted();
1083         }
1084         return false;
1085 }
1086
1087 bool VM::is_tape_playing(int drv)
1088 {
1089         if(drec != NULL) {
1090                 return drec->is_tape_playing();
1091         }
1092         return false;
1093 }
1094
1095 bool VM::is_tape_recording(int drv)
1096 {
1097         if(drec != NULL) {
1098                 return drec->is_tape_recording();
1099         }
1100         return false;
1101 }
1102
1103 int VM::get_tape_position(int drv)
1104 {
1105         if(drec != NULL) {
1106                 return drec->get_tape_position();
1107         }
1108         return 0;
1109 }
1110
1111 const _TCHAR* VM::get_tape_message(int drv)
1112 {
1113         if(drec != NULL) {
1114                 return drec->get_message();
1115         }
1116         return NULL;
1117 }
1118
1119 void VM::push_play(int drv)
1120 {
1121         if(drec != NULL) {
1122                 drec->set_ff_rew(0);
1123                 drec->set_remote(true);
1124         }
1125 }
1126
1127
1128 void VM::push_stop(int drv)
1129 {
1130         if(drec != NULL) {
1131                 drec->set_remote(false);
1132         }
1133 }
1134
1135 void VM::push_fast_forward(int drv)
1136 {
1137         if(drec != NULL) {
1138                 drec->set_ff_rew(1);
1139                 drec->set_remote(true);
1140         }
1141 }
1142
1143 void VM::push_fast_rewind(int drv)
1144 {
1145         if(drec != NULL) {
1146                 drec->set_ff_rew(-1);
1147                 drec->set_remote(true);
1148         }
1149 }
1150
1151 void VM::push_apss_forward(int drv)
1152 {
1153         if(drec != NULL) {
1154                 drec->do_apss(1);
1155         }
1156 }
1157
1158 void VM::push_apss_rewind(int drv)
1159 {
1160         if(drec != NULL) {
1161                 drec->do_apss(-1);
1162         }
1163 }
1164
1165 bool VM::is_frame_skippable()
1166 {
1167         return event->is_frame_skippable();
1168 }
1169
1170 void VM::update_dipswitch()
1171 {
1172 #if defined(WITH_Z80)
1173         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);
1174         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);
1175         if(g_nmi != NULL) g_nmi->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_NMI_ON) != 0) ? 1 : 0, 1);
1176 #endif
1177 }
1178
1179 void VM::set_cpu_clock(DEVICE *cpu, uint32_t clocks) {
1180         event->set_secondary_cpu_clock(cpu, clocks);
1181 }
1182
1183 #if defined(USE_BUBBLE)
1184 void VM::open_bubble_casette(int drv, const _TCHAR *path, int bank)
1185 {
1186         if((drv >= 2) || (drv < 0)) return;
1187         if(bubble_casette[drv] == NULL) return;
1188         bubble_casette[drv]->open((_TCHAR *)path, bank);
1189 }
1190
1191 void VM::close_bubble_casette(int drv)
1192 {
1193         if((drv >= 2) || (drv < 0)) return;
1194         if(bubble_casette[drv] == NULL) return;
1195         bubble_casette[drv]->close();
1196 }
1197
1198 bool VM::is_bubble_casette_inserted(int drv)
1199 {
1200         if((drv >= 2) || (drv < 0)) return false;
1201         if(bubble_casette[drv] == NULL) return false;
1202         return bubble_casette[drv]->is_bubble_inserted();
1203 }
1204
1205 bool VM::is_bubble_casette_protected(int drv)
1206 {
1207         if((drv >= 2) || (drv < 0)) return false;
1208         if(bubble_casette[drv] == NULL) return false;
1209         return bubble_casette[drv]->is_bubble_protected();
1210 }
1211
1212 void VM::is_bubble_casette_protected(int drv, bool flag)
1213 {
1214         if((drv >= 2) || (drv < 0)) return;
1215         if(bubble_casette[drv] == NULL) return;
1216         bubble_casette[drv]->set_bubble_protect(flag);
1217 }
1218 #endif
1219
1220 void VM::set_vm_frame_rate(double fps)
1221 {
1222    if(event != NULL) event->set_frames_per_sec(fps);
1223 }
1224
1225 #define STATE_VERSION   10
1226 #include "../../statesub.h"
1227 #include "../../qt/gui/csp_logger.h"
1228 extern CSP_Logger DLL_PREFIX_I *csp_logger;
1229
1230 void VM::decl_state(void)
1231 {
1232 #if defined(_FM8)
1233         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FM8_HEAD")), csp_logger);
1234 #elif defined(_FM7) || defined(_FMNEW7)
1235         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FM7_HEAD")), csp_logger);
1236 #elif defined(_FM77) || defined(_FM77L2)
1237         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FM77_HEAD")), csp_logger);
1238 #elif defined(_FM77L4)
1239         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FM77L4_HEAD")), csp_logger);
1240 #elif defined(_FM77AV)
1241         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FM77AV_HEAD")), csp_logger);
1242 #elif defined(_FM77AV20)
1243         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FM77AV20_HEAD")), csp_logger);
1244 #elif defined(_FM77AV40)
1245         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FM77AV40_HEAD")), csp_logger);
1246 #elif defined(_FM77AV20EX)
1247         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FM77AV20EX_HEAD")), csp_logger);
1248 #elif defined(_FM77AV40EX)
1249         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FM77AV40EX_HEAD")), csp_logger);
1250 #elif defined(_FM77AV40SX)
1251         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FM77AV40SX_HEAD")), csp_logger);
1252 #else
1253         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FM7_SERIES_HEAD")), csp_logger);
1254 #endif
1255         DECL_STATE_ENTRY_BOOL(connect_320kfdc);
1256         DECL_STATE_ENTRY_BOOL(connect_1Mfdc);
1257         for(DEVICE* device = first_device; device; device = device->next_device) {
1258                 device->decl_state();
1259         }
1260 }
1261
1262 void VM::save_state(FILEIO* state_fio)
1263 {
1264         if(state_entry != NULL) {
1265                 state_entry->save_state(state_fio);
1266         }
1267         for(DEVICE* device = first_device; device; device = device->next_device) {
1268                 emu->out_debug_log("SAVE State: DEVID=%d", device->this_device_id);
1269                 device->save_state(state_fio);
1270         }
1271 }
1272
1273 bool VM::load_state(FILEIO* state_fio)
1274 {
1275         bool mb = false;
1276         if(state_entry != NULL) {
1277                 mb = state_entry->load_state(state_fio);
1278         }
1279         if(!mb) {
1280                 emu->out_debug_log("INFO: HEADER DATA ERROR");
1281                 return false;
1282         }
1283         for(DEVICE* device = first_device; device; device = device->next_device) {
1284                 if(!device->load_state(state_fio)) {
1285                         printf("Load Error: DEVID=%d\n", device->this_device_id);
1286                         return false;
1287                 }
1288         }
1289         update_config();
1290         //mainio->restore_opn();
1291         
1292         return true;
1293 }
1294
1295 #ifdef USE_DIG_RESOLUTION
1296 void VM::get_screen_resolution(int *w, int *h)
1297 {
1298         switch(display->get_screen_mode()) {
1299         case DISPLAY_MODE_8_200L:
1300                 *w = 640;
1301                 *h = 200;
1302                 break;
1303         case DISPLAY_MODE_8_400L:
1304         case DISPLAY_MODE_1_400L:
1305                 *w = 640;
1306                 *h = 400;
1307                 break;
1308         case DISPLAY_MODE_4096:
1309         case DISPLAY_MODE_256k:
1310                 *w = 320;
1311                 *h = 200;
1312                 break;
1313         default:
1314                 *w = 640;
1315                 *h = 200;
1316                 break;
1317         }
1318 }
1319 #endif
1320
1321 bool VM::is_screen_changed()
1322 {
1323         bool f = true;
1324 #if defined(USE_MINIMUM_RENDERING)
1325         f = display->screen_update();
1326         display->reset_screen_update();
1327 #endif  
1328         return f;
1329 }