OSDN Git Service

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