OSDN Git Service

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