OSDN Git Service

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