OSDN Git Service

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