OSDN Git Service

[VM][General] Merge Upstream 2017-12-15.
[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
34 #if defined(_FM77AV_VARIANTS)
35 #include "mb61vh010.h"
36 #include "../beep.h"
37 #endif
38 #if defined(HAS_DMA)
39 #include "hd6844.h"
40 #endif
41 #if defined(_FM8)
42 #include "./bubblecasette.h"
43 #endif
44
45 #include "./fm7_mainio.h"
46 #include "./fm7_mainmem.h"
47 #include "./fm7_display.h"
48 #include "./fm7_keyboard.h"
49 #include "./joystick.h"
50
51 #include "./kanjirom.h"
52
53 VM::VM(EMU* parent_emu): emu(parent_emu)
54 {
55         
56         first_device = last_device = NULL;
57 #if defined(_FM8)
58         psg = NULL;
59 #else   
60 # if defined(_FM77AV_VARIANTS)
61         opn[0] = opn[1] = opn[2] = NULL;
62 # else   
63         opn[0] = opn[1] = opn[2] = NULL;
64         psg = NULL; 
65 # endif
66 #endif
67         dummy = new DEVICE(this, emu);  // must be 1st device
68         event = new EVENT(this, emu);   // must be 2nd device
69         
70         dummycpu = new DEVICE(this, emu);
71         maincpu = new MC6809(this, emu);
72         subcpu = new MC6809(this, emu);
73         g_substat_display = new AND(this, emu);
74         g_substat_mainhalt = new AND(this, emu);
75         
76 #ifdef WITH_Z80
77         z80cpu = new Z80(this, emu);
78         g_mainstat = new AND(this, emu);
79         g_intr = new OR(this, emu);
80
81         g_intr_irq = new AND(this, emu);
82         g_intr_firq = new AND(this, emu);
83         g_nmi = new AND(this, emu);
84 #endif
85         // basic devices
86         // I/Os
87 #if defined(HAS_DMA)
88         dmac = new HD6844(this, emu);
89 #endif   
90 #if defined(_FM8)
91 #  if defined(USE_AY_3_8910_AS_PSG)
92         psg = new AY_3_891X(this, emu);
93 #  else
94         psg = new YM2203(this, emu);
95 #  endif
96 #else   
97         opn[0] = new YM2203(this, emu); // OPN
98         opn[1] = new YM2203(this, emu); // WHG
99         opn[2] = new YM2203(this, emu); // THG
100 # if !defined(_FM77AV_VARIANTS)
101 #  if defined(USE_AY_3_8910_AS_PSG)
102         psg = new AY_3_891X(this, emu);
103 #  else
104         psg = new YM2203(this, emu);
105 #  endif
106 # endif 
107 #endif
108 #if defined(_FM8)
109         for(int i = 0; i < 2; i++) bubble_casette[i] = new BUBBLECASETTE(this, emu);
110 #endif
111         drec = NULL;
112         drec = new DATAREC(this, emu);
113         drec->set_context_noise_play(new NOISE(this, emu));
114         drec->set_context_noise_stop(new NOISE(this, emu));
115         drec->set_context_noise_fast(new NOISE(this, emu));
116         pcm1bit = new PCM1BIT(this, emu);
117
118         connect_320kfdc = connect_1Mfdc = false;
119         fdc = NULL;
120 #if defined(_FM8) || defined(_FM7) || defined(_FMNEW7)
121         if(((config.dipswitch & FM7_DIPSW_CONNECT_320KFDC) != 0) ||
122            ((config.dipswitch & FM7_DIPSW_CONNECT_1MFDC) != 0)) {
123 #endif          
124                 fdc = new MB8877(this, emu);
125                 fdc->set_context_noise_seek(new NOISE(this, emu));
126                 fdc->set_context_noise_head_down(new NOISE(this, emu));
127                 fdc->set_context_noise_head_up(new NOISE(this, emu));
128 #if defined(_FM8) || defined(_FM7) || defined(_FMNEW7)
129                 if((config.dipswitch & FM7_DIPSW_CONNECT_320KFDC) != 0) {
130                         connect_320kfdc = true;
131                 }
132                 if((config.dipswitch & FM7_DIPSW_CONNECT_1MFDC) != 0) {
133                         connect_1Mfdc = true;
134                 }
135 #elif defined(_FM77_VARIANTS)
136                 connect_320kfdc = true;
137                 if((config.dipswitch & FM7_DIPSW_CONNECT_1MFDC) != 0) {
138                         connect_1Mfdc = true;
139                 }
140 #else   // AV or later.
141                 connect_320kfdc = true;
142                 // 1MFDD??
143 #endif          
144 #if defined(_FM8) || defined(_FM7) || defined(_FMNEW7)
145         }
146 #endif  
147         joystick  = new JOYSTICK(this, emu);
148         printer = new PRNFILE(this, emu);
149 #if defined(_FM77AV_VARIANTS)
150         alu = new MB61VH010(this, emu);
151         keyboard_beep = new BEEP(this, emu);
152 #endif  
153         keyboard = new KEYBOARD(this, emu);
154         display = new DISPLAY(this, emu);       
155
156         mainio  = new FM7_MAINIO(this, emu);
157         mainmem = new FM7_MAINMEM(this, emu);
158         
159 #if defined(_FM8) || defined(_FM7) || defined(_FMNEW7)
160         if((config.dipswitch & FM7_DIPSW_CONNECT_KANJIROM) != 0) {
161                 kanjiclass1 = new KANJIROM(this, emu, false);
162         } else {
163                 kanjiclass1 = NULL;
164         }
165 #else
166         kanjiclass1 = new KANJIROM(this, emu, false);
167 #endif  
168 #ifdef CAPABLE_KANJI_CLASS2
169         kanjiclass2 = new KANJIROM(this, emu, true);
170 #endif
171 #ifdef WITH_Z80
172         g_mainstat->set_mask(SIG_AND_BIT_0);
173         g_mainstat->set_mask(SIG_AND_BIT_1);
174         maincpu->set_context_bus_ba(g_mainstat, SIG_AND_BIT_0, 0xffffffff);
175         maincpu->set_context_bus_bs(g_mainstat, SIG_AND_BIT_1, 0xffffffff);
176         g_mainstat->set_context_out(mainio, FM7_MAINIO_RUN_Z80, 0xffffffff);
177         
178         z80cpu->set_context_busack(mainio, FM7_MAINIO_RUN_6809, 0xffffffff);
179         mainio->set_context_z80cpu(z80cpu);
180
181 #endif
182 #if defined(_USE_QT)
183         event->set_device_name(_T("EVENT"));
184         dummy->set_device_name(_T("1st Dummy"));
185         
186         maincpu->set_device_name(_T("MAINCPU(MC6809)"));
187         subcpu->set_device_name(_T("SUBCPU(MC6809)"));
188         dummycpu->set_device_name(_T("DUMMY CPU"));
189 # ifdef WITH_Z80
190         z80cpu->set_device_name(_T("Z80 CPU"));
191 # endif
192         if(fdc != NULL) fdc->set_device_name(_T("MB8877 FDC(320KB)"));
193                                                 
194         // basic devices
195         // I/Os
196 # if defined(_FM8)
197         psg->set_device_name(_T("AY-3-8910 PSG"));
198 # else  
199         opn[0]->set_device_name(_T("YM2203 OPN"));
200         opn[1]->set_device_name(_T("YM2203 WHG"));
201         opn[2]->set_device_name(_T("YM2203 THG"));
202 #  if !defined(_FM77AV_VARIANTS)
203         psg->set_device_name(_T("AY-3-8910 PSG"));
204 #  endif
205 # endif
206         pcm1bit->set_device_name(_T("BEEP"));
207         printer->set_device_name(_T("PRINTER I/F"));
208 # if defined(_FM77AV_VARIANTS)
209         keyboard_beep->set_device_name(_T("BEEP(KEYBOARD)"));
210 # endif 
211         if(kanjiclass1 != NULL) kanjiclass1->set_device_name(_T("KANJI ROM CLASS1"));
212 # ifdef CAPABLE_KANJI_CLASS2
213         if(kanjiclass2 != NULL) kanjiclass2->set_device_name(_T("KANJI ROM CLASS2"));
214 # endif
215 # if defined(_FM8)
216         bubble_casette[0]->set_device_name(_T("BUBBLE CASETTE #0"));
217         bubble_casette[1]->set_device_name(_T("BUBBLE CASETTE #1"));
218 # endif 
219 #endif
220         this->connect_bus();
221         
222 }
223
224 VM::~VM()
225 {
226         // delete all devices
227         for(DEVICE* device = first_device; device;) {
228                 DEVICE *next_device = device->next_device;
229                 device->release();
230                 delete device;
231                 device = next_device;
232         }
233 }
234
235 DEVICE* VM::get_device(int id)
236 {
237         for(DEVICE* device = first_device; device; device = device->next_device) {
238                 if(device->this_device_id == id) {
239                         return device;
240                 }
241         }
242         return NULL;
243 }
244
245 void VM::connect_bus(void)
246 {
247         uint32_t mainclock;
248         uint32_t subclock;
249
250         /*
251          * CLASS CONSTRUCTION
252          *
253          * VM 
254          *  |-> MAINCPU -> MAINMEM -> MAINIO -> MAIN DEVICES
255          *  |             |        |      
256          *  | -> SUBCPU  -> SUBMEM  -> SUBIO -> SUB DEVICES
257          *  | -> DISPLAY
258          *  | -> KEYBOARD
259          *
260          *  MAINMEM can access SUBMEM/IO, when SUBCPU is halted.
261          *  MAINMEM and SUBMEM can access DISPLAY and KEYBOARD with exclusive.
262          *  MAINCPU can access MAINMEM.
263          *  SUBCPU  can access SUBMEM.
264          *  DISPLAY : R/W from MAINCPU and SUBCPU.
265          *  KEYBOARD : R/W
266          *
267          */
268         event->set_frames_per_sec(FRAMES_PER_SEC);
269         event->set_lines_per_frame(LINES_PER_FRAME);
270         //event->set_context_cpu(dummycpu, (CPU_CLOCKS * 3) / 8); // MAYBE FIX With eFM77AV40/20.
271         // With slow clock (for dummycpu), some softwares happen troubles,
272         // Use faster clock for dummycpu. 20160319 K.Ohta
273         event->set_context_cpu(dummycpu, SUBCLOCK_NORMAL);
274
275 #if defined(_FM8)
276         mainclock = MAINCLOCK_SLOW;
277         subclock = SUBCLOCK_SLOW;
278 #else
279         if(config.cpu_type == 0) {
280                 // 2MHz
281                 subclock = SUBCLOCK_NORMAL;
282                 mainclock = MAINCLOCK_NORMAL;
283         } else {
284                 // 1.2MHz
285                 mainclock = MAINCLOCK_SLOW;
286                 subclock = SUBCLOCK_SLOW;
287         }
288         //if((config.dipswitch & FM7_DIPSW_CYCLESTEAL) != 0) subclock = subclock / 3;
289 #endif
290         event->set_context_cpu(maincpu, mainclock);
291         event->set_context_cpu(subcpu,  subclock);
292    
293 #ifdef WITH_Z80
294         event->set_context_cpu(z80cpu,  4000000);
295         z80cpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
296         maincpu->write_signal(SIG_CPU_HALTREQ, 0, 1);
297
298         g_intr_irq->set_mask(SIG_AND_BIT_0);
299         g_intr_irq->set_mask(SIG_AND_BIT_1);
300         
301         g_intr_firq->set_mask(SIG_AND_BIT_0);
302         g_intr_firq->set_mask(SIG_AND_BIT_1);
303         
304         g_nmi->set_mask(SIG_AND_BIT_0);
305         g_nmi->set_mask(SIG_AND_BIT_1);
306
307         mainio->set_context_irq(g_intr_irq, SIG_AND_BIT_1, 0xffffffff);
308         g_intr_irq->set_context_out(g_intr, SIG_OR_BIT_0, 0xffffffff);
309         
310         mainio->set_context_firq(g_intr_firq, SIG_AND_BIT_1, 0xffffffff);
311         g_intr_firq->set_context_out(g_intr, SIG_OR_BIT_0, 0xffffffff);
312         
313         g_intr->set_context_out(z80cpu, SIG_CPU_IRQ, 0xffffffff);
314
315         mainio->set_context_nmi(g_nmi, SIG_AND_BIT_1, 0xffffffff);
316         g_nmi->set_context_out(z80cpu, SIG_CPU_NMI, 0xffffffff);
317 #endif
318
319         event->set_context_sound(pcm1bit);
320 #if defined(_FM8)
321         event->set_context_sound(psg);
322         if(drec != NULL) event->set_context_sound(drec);
323 #else
324         event->set_context_sound(opn[0]);
325         event->set_context_sound(opn[1]);
326         event->set_context_sound(opn[2]);
327 # if !defined(_FM77AV_VARIANTS)
328         event->set_context_sound(psg);
329 # endif
330         event->set_context_sound(drec);
331         if(fdc != NULL) {
332                 event->set_context_sound(fdc->get_context_noise_seek());
333                 event->set_context_sound(fdc->get_context_noise_head_down());
334                 event->set_context_sound(fdc->get_context_noise_head_up());
335         }
336         if(drec != NULL) {
337                 event->set_context_sound(drec->get_context_noise_play());
338                 event->set_context_sound(drec->get_context_noise_stop());
339                 event->set_context_sound(drec->get_context_noise_fast());
340         }
341 # if defined(_FM77AV_VARIANTS)
342         event->set_context_sound(keyboard_beep);
343 # endif
344 #endif   
345 #if !defined(_FM77AV_VARIANTS) && !defined(_FM77L4)
346         event->register_vline_event(display);
347         event->register_frame_event(display);
348 #endif  
349         mainio->set_context_maincpu(maincpu);
350         mainio->set_context_subcpu(subcpu);
351         
352         mainio->set_context_display(display);
353         mainio->set_context_irq(maincpu, SIG_CPU_IRQ, 0xffffffff);
354         mainio->set_context_firq(maincpu, SIG_CPU_FIRQ, 0xffffffff);
355         mainio->set_context_nmi(maincpu, SIG_CPU_NMI, 0xffffffff);
356 #if defined(_FM8) || defined(_FM7) || defined(_FMNEW7)
357         if((config.dipswitch & FM7_DIPSW_CONNECT_KANJIROM) != 0) {
358                 mainio->set_context_kanjirom_class1(kanjiclass1);
359         }
360 #else
361         mainio->set_context_kanjirom_class1(kanjiclass1);
362 #endif  
363         mainio->set_context_mainmem(mainmem);
364         mainio->set_context_keyboard(keyboard);
365         mainio->set_context_printer(printer);
366         mainio->set_context_printer_reset(printer, SIG_PRINTER_RESET, 0xffffffff);
367         mainio->set_context_printer_strobe(printer, SIG_PRINTER_STROBE, 0xffffffff);
368         mainio->set_context_printer_select(printer, SIG_PRINTER_SELECT, 0xffffffff);
369 #if defined(CAPABLE_KANJI_CLASS2)
370         mainio->set_context_kanjirom_class2(kanjiclass2);
371 #endif
372 #if defined(_FM8)
373         for(int i = 0; i < 2; i++) mainio->set_context_bubble(bubble_casette[i], i);
374 #endif  
375         keyboard->set_context_break_line(mainio, FM7_MAINIO_PUSH_BREAK, 0xffffffff);
376         keyboard->set_context_int_line(mainio, FM7_MAINIO_KEYBOARDIRQ, 0xffffffff);
377         keyboard->set_context_int_line(display, SIG_FM7_SUB_KEY_FIRQ, 0xffffffff);
378 #if defined(_FM77AV_VARIANTS)
379         keyboard->set_context_beep(keyboard_beep);
380 #endif  
381         keyboard->set_context_rxrdy(display, SIG_FM7KEY_RXRDY, 0x01);
382         keyboard->set_context_key_ack(display, SIG_FM7KEY_ACK, 0x01);
383    
384         if(drec != NULL) {
385                 drec->set_context_ear(mainio, FM7_MAINIO_CMT_RECV, 0xffffffff);
386                 //drec->set_context_remote(mainio, FM7_MAINIO_CMT_REMOTE, 0xffffffff);
387                 mainio->set_context_datarec(drec);
388         }
389         mainmem->set_context_mainio(mainio);
390         mainmem->set_context_display(display);
391         mainmem->set_context_maincpu(maincpu);
392 #if defined(CAPABLE_DICTROM)
393         mainmem->set_context_kanjirom_class1(kanjiclass1);
394 #endif  
395         display->set_context_mainio(mainio);
396         display->set_context_subcpu(subcpu);
397         display->set_context_keyboard(keyboard);
398
399         mainio->set_context_clock_status(mainmem, FM7_MAINIO_CLOCKMODE, 0xffffffff);
400         mainio->set_context_clock_status(display, SIG_DISPLAY_CLOCK, 0xffffffff);
401
402         g_substat_display->set_mask(SIG_AND_BIT_0);
403         g_substat_display->set_mask(SIG_AND_BIT_1);
404         subcpu->set_context_bus_ba(g_substat_display, SIG_AND_BIT_0, 0xffffffff);
405         subcpu->set_context_bus_bs(g_substat_display, SIG_AND_BIT_1, 0xffffffff);
406         g_substat_display->set_context_out(display, SIG_FM7_SUB_HALT, 0xffffffff);
407
408         g_substat_mainhalt->set_mask(SIG_AND_BIT_0);
409         g_substat_mainhalt->set_mask(SIG_AND_BIT_1);
410         subcpu->set_context_bus_ba(g_substat_mainhalt, SIG_AND_BIT_0, 0xffffffff);
411         subcpu->set_context_bus_bs(g_substat_mainhalt, SIG_AND_BIT_1, 0xffffffff);
412         g_substat_mainhalt->set_context_out(mainmem, SIG_FM7_SUB_HALT, 0xffffffff);
413
414 #if defined(_FM77_VARIANTS) || defined(_FM77AV_VARIANTS)
415         display->set_context_kanjiclass1(kanjiclass1);
416 #endif  
417 #if defined(CAPABLE_KANJI_CLASS2)
418         display->set_context_kanjiclass2(kanjiclass2);
419 #endif   
420 #if defined(_FM77AV_VARIANTS)
421         display->set_context_alu(alu);
422         alu->set_context_memory(display);
423         alu->set_direct_access_offset(DISPLAY_VRAM_DIRECT_ACCESS);
424 #endif  
425         // Palette, VSYNC, HSYNC, Multi-page, display mode. 
426         mainio->set_context_display(display);
427 #if defined(_FM8) || (_FM7) || (_FMNEW7)
428         if(connect_320kfdc || connect_1Mfdc) {
429 #endif          
430                 //FDC
431                 fdc->set_context_irq(mainio, FM7_MAINIO_FDC_IRQ, 0x1);
432                 fdc->set_context_drq(mainio, FM7_MAINIO_FDC_DRQ, 0x1);
433                 mainio->set_context_fdc(fdc);
434 #if defined(_FM8) || (_FM7) || (_FMNEW7)
435         }
436 #endif  
437         // SOUND
438         mainio->set_context_beep(pcm1bit);
439 #if defined(_FM8)       
440         mainio->set_context_psg(psg);
441 #else
442 # if !defined(_FM77AV_VARIANTS)
443         mainio->set_context_psg(psg);
444 # endif
445         opn[0]->set_context_irq(mainio, FM7_MAINIO_OPN_IRQ, 0xffffffff);
446         mainio->set_context_opn(opn[0], 0);
447         joystick->set_context_opn(opn[0]);
448         mainio->set_context_joystick(joystick);
449         opn[0]->set_context_port_b(joystick, FM7_JOYSTICK_MOUSE_STROBE, 0xff, 0);
450         
451         opn[1]->set_context_irq(mainio, FM7_MAINIO_WHG_IRQ, 0xffffffff);
452         mainio->set_context_opn(opn[1], 1);
453         opn[2]->set_context_irq(mainio, FM7_MAINIO_THG_IRQ, 0xffffffff);
454         mainio->set_context_opn(opn[2], 2);
455 #endif   
456         subcpu->set_context_bus_clr(display, SIG_FM7_SUB_USE_CLR, 0x0000000f);
457    
458         event->register_frame_event(joystick);
459 #if defined(HAS_DMA)
460         dmac->set_context_src(fdc, 0);
461         dmac->set_context_dst(mainmem, 0);
462         dmac->set_context_int_line(mainio, 0, FM7_MAINIO_DMA_INT, 0xffffffff);
463         dmac->set_context_drq_line(maincpu, 1, SIG_CPU_BUSREQ, 0xffffffff);
464         mainio->set_context_dmac(dmac);
465 #endif
466         
467         // MEMORIES must set before initialize().
468         maincpu->set_context_mem(mainmem);
469         subcpu->set_context_mem(display);
470 #ifdef WITH_Z80
471         z80cpu->set_context_mem(mainmem);
472 #endif
473 #ifdef USE_DEBUGGER
474         maincpu->set_context_debugger(new DEBUGGER(this, emu));
475         subcpu->set_context_debugger(new DEBUGGER(this, emu));
476 # ifdef WITH_Z80
477         z80cpu->set_context_debugger(new DEBUGGER(this, emu));
478 # endif
479 #endif
480         for(DEVICE* device = first_device; device; device = device->next_device) {
481                 device->initialize();
482         }
483
484 #if defined(WITH_Z80)
485         g_intr_irq->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_IRQ_ON) != 0) ? 1 : 0, 1);
486         g_intr_firq->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_FIRQ_ON) != 0) ? 1 : 0, 1);
487         g_nmi->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_NMI_ON) != 0) ? 1 : 0, 1);
488 #endif
489         // Disks
490 #if defined(_FM8) || (_FM7) || (_FMNEW7)
491         if(connect_320kfdc) {
492 #endif          
493                 for(int i = 0; i < 4; i++) {
494 #if defined(_FM77AV20) || defined(_FM77AV20EX) || \
495         defined(_FM77AV40SX) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
496                         fdc->set_drive_type(i, DRIVE_TYPE_2DD);
497 #else
498                         fdc->set_drive_type(i, DRIVE_TYPE_2D);
499 #endif
500                         fdc->set_drive_rpm(i, 360);
501                         fdc->set_drive_mfm(i, true);
502                 }
503 #if defined(_FM8) || (_FM7) || (_FMNEW7)
504         }
505 #endif  
506         
507 #if defined(_FM8) || (_FM7) || (_FMNEW7)
508         if(connect_1Mfdc) {
509 #endif
510 // 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.
511 //#if defined(_FM77) || defined(_FM77L4)
512 //              for(int i = 0; i < 4; i++) {
513 //                      fdc->set_drive_type(i, DRIVE_TYPE_2HD);
514 //                      fdc->set_drive_rpm(i, 360);
515 //                      fdc->set_drive_mfm(i, true);
516 //              }
517 //#endif
518 #if defined(_FM8) || (_FM7) || (_FMNEW7)
519         }
520 #endif  
521 }  
522
523 void VM::update_config()
524 {
525         for(DEVICE* device = first_device; device; device = device->next_device) {
526                 device->update_config();
527         }
528         update_dipswitch();
529 }
530
531 void VM::reset()
532 {
533         // reset all devices
534         for(DEVICE* device = first_device; device; device = device->next_device) {
535                 device->reset();
536         }
537 #if !defined(_FM77AV_VARIANTS) || defined(_FM8)
538 # if defined(USE_AY_3_8910_AS_PSG)
539         psg->set_reg(0x2e, 0);  // set prescaler
540         psg->write_signal(SIG_AY_3_891X_MUTE, 0x00, 0x01); // Okay?
541 # else  
542         psg->set_reg(0x27, 0); // stop timer
543         psg->set_reg(0x2e, 0);  // set prescaler
544         psg->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
545 #endif
546 #endif
547 #if !defined(_FM8)
548         for(int i = 0; i < 3; i++) {
549                 opn[i]->set_reg(0x27, 0); // stop timer
550                 opn[i]->set_reg(0x2e, 0);       // set prescaler
551                 opn[i]->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
552         }
553 #endif
554 }
555
556 void VM::special_reset()
557 {
558         // BREAK + RESET
559         mainio->write_signal(FM7_MAINIO_PUSH_BREAK, 1, 1);
560         mainio->reset();
561         mainmem->reset();
562         
563 #if defined(FM77AV_VARIANTS)    
564         mainio->write_signal(FM7_MAINIO_HOT_RESET, 1, 1);
565 #endif  
566         display->reset();
567         subcpu->reset();
568         mainio->write_signal(FM7_MAINIO_PUSH_BREAK, 1, 1);
569         maincpu->reset();
570         event->register_event(mainio, EVENT_UP_BREAK, 10000.0 * 1000.0, false, NULL);
571 }
572
573 void VM::run()
574 {
575         event->drive();
576 }
577
578 double VM::get_frame_rate()
579 {
580         return event->get_frame_rate();
581 }
582
583
584
585 // ----------------------------------------------------------------------------
586 // debugger
587 // ----------------------------------------------------------------------------
588
589 #ifdef USE_DEBUGGER
590 DEVICE *VM::get_cpu(int index)
591 {
592         if(index == 0) {
593                 return maincpu;
594         } else if(index == 1) {
595                 return subcpu;
596         }
597 #if defined(WITH_Z80)
598         else if(index == 2) {
599                 return z80cpu;
600         }
601 #endif
602         return NULL;
603 }
604 #endif
605
606 // ----------------------------------------------------------------------------
607 // draw screen
608 // ----------------------------------------------------------------------------
609
610 void VM::draw_screen()
611 {
612         display->draw_screen();
613 }
614
615 void VM::initialize_sound(int rate, int samples)
616 {
617         // init sound manager
618         event->initialize_sound(rate, samples);
619         // init sound gen
620 #if defined(_FM8)
621         psg->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
622 #else   
623         opn[0]->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
624         opn[1]->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
625         opn[2]->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
626 # if !defined(_FM77AV_VARIANTS)   
627         psg->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
628 # endif
629 # if defined(_FM77AV_VARIANTS)
630         keyboard_beep->initialize_sound(rate, 2400.0, 512);
631 # endif
632 #endif  
633         pcm1bit->initialize_sound(rate, 2000);
634         //drec->initialize_sound(rate, 0);
635 }
636
637 uint16_t* VM::create_sound(int* extra_frames)
638 {
639         uint16_t* p = event->create_sound(extra_frames);
640         return p;
641 }
642
643 int VM::get_sound_buffer_ptr()
644 {
645         int pos = event->get_sound_buffer_ptr();
646         return pos; 
647 }
648
649 #ifdef USE_SOUND_VOLUME
650 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
651 {
652 #if !defined(_FM77AV_VARIANTS)
653         if(ch-- == 0) {
654                 psg->set_volume(0, decibel_l, decibel_r);
655                 psg->set_volume(1, decibel_l, decibel_r);
656         } else
657 #endif
658 #if !defined(_FM8)              
659         if(ch-- == 0) {
660                 opn[0]->set_volume(0, decibel_l, decibel_r);
661         } else if(ch-- == 0) {
662                 opn[0]->set_volume(1, decibel_l, decibel_r);
663         } else if(ch-- == 0) {
664                 opn[1]->set_volume(0, decibel_l, decibel_r);
665         } else if(ch-- == 0) {
666                 opn[1]->set_volume(1, decibel_l, decibel_r);
667         } else if(ch-- == 0) {
668                 opn[2]->set_volume(0, decibel_l, decibel_r);
669         } else if(ch-- == 0) {
670                 opn[2]->set_volume(1, decibel_l, decibel_r);
671         } else
672 #endif  
673         if(ch-- == 0) {
674                 pcm1bit->set_volume(0, decibel_l, decibel_r);
675         } else if(ch-- == 0) {
676                 if(drec != NULL) drec->set_volume(0, decibel_l, decibel_r);
677         }
678 #if defined(_FM77AV_VARIANTS)
679         else if(ch-- == 0) {
680                 keyboard_beep->set_volume(0, decibel_l, decibel_r);
681         }
682 #endif
683         else if(ch-- == 0) {
684                 if(fdc != NULL) {
685                         fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
686                         fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
687                         fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
688                 }
689         } else if(ch-- == 0) {
690                 if(drec != NULL) {
691                         drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
692                         drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
693                         drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
694                 }
695         }
696 }
697 #endif
698
699 // ----------------------------------------------------------------------------
700 // notify key
701 // ----------------------------------------------------------------------------
702
703 void VM::key_down(int code, bool repeat)
704 {
705         if(!repeat) {
706                 keyboard->key_down(code);
707         }
708 }
709
710 void VM::key_up(int code)
711 {
712         keyboard->key_up(code);
713 }
714
715 bool VM::get_caps_locked()
716 {
717         return keyboard->get_caps_locked();
718 }
719
720 bool VM::get_kana_locked()
721 {
722         return keyboard->get_kana_locked();
723 }
724
725 // Get INS status.Important with FM-7 series (^_^;
726 uint32_t VM::get_extra_leds()
727 {
728         return keyboard->read_signal(SIG_FM7KEY_LED_STATUS);
729 }
730
731
732 // ----------------------------------------------------------------------------
733 // user interface
734 // ----------------------------------------------------------------------------
735
736 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
737 {
738         if(fdc != NULL) {
739                 fdc->open_disk(drv, file_path, bank);
740         }
741 }
742
743 void VM::close_floppy_disk(int drv)
744 {
745         if(fdc != NULL) {
746                 fdc->close_disk(drv);
747         }
748 }
749
750 bool VM::is_floppy_disk_inserted(int drv)
751 {
752         if(fdc != NULL) {
753                 return fdc->is_disk_inserted(drv);
754         } else {
755                 return false;
756         }
757 }
758
759 void VM::is_floppy_disk_protected(int drv, bool value)
760 {
761         if(fdc != NULL) {
762                 fdc->is_disk_protected(drv, value);
763         }
764 }
765
766 bool VM::is_floppy_disk_protected(int drv)
767 {
768         if(fdc != NULL) {
769                 return fdc->is_disk_protected(drv);
770         } else {
771                 return false;
772         }
773 }
774
775 uint32_t VM::is_floppy_disk_accessed()
776 {
777         // WILLFIX : Multiple FDC for 1M FD.
778 #if defined(_FM8) || (_FM7) || (_FMNEW7)
779         if(connect_320kfdc || connect_1Mfdc) {
780 #endif          
781                 return fdc->read_signal(0);
782 #if defined(_FM8) || (_FM7) || (_FMNEW7)
783         } else {
784                 return 0x00000000;
785         }
786 #endif  
787 }
788
789 void VM::play_tape(int drv, const _TCHAR* file_path)
790 {
791         if(drec != NULL) drec->play_tape(file_path);
792 }
793
794 void VM::rec_tape(int drv, const _TCHAR* file_path)
795 {
796         if(drec != NULL) drec->rec_tape(file_path);
797 }
798
799 void VM::close_tape(int drv)
800 {
801         emu->lock_vm();
802         if(drec != NULL) drec->close_tape();
803         emu->unlock_vm();
804 }
805
806 bool VM::is_tape_inserted(int drv)
807 {
808         if(drec != NULL) {
809                 return drec->is_tape_inserted();
810         }
811         return false;
812 }
813
814 bool VM::is_tape_playing(int drv)
815 {
816         if(drec != NULL) {
817                 return drec->is_tape_playing();
818         }
819         return false;
820 }
821
822 bool VM::is_tape_recording(int drv)
823 {
824         if(drec != NULL) {
825                 return drec->is_tape_recording();
826         }
827         return false;
828 }
829
830 int VM::get_tape_position(int drv)
831 {
832         if(drec != NULL) {
833                 return drec->get_tape_position();
834         }
835         return 0;
836 }
837
838 const _TCHAR* VM::get_tape_message(int drv)
839 {
840         if(drec != NULL) {
841                 return drec->get_message();
842         }
843         return NULL;
844 }
845
846 void VM::push_play(int drv)
847 {
848         if(drec != NULL) {
849                 drec->set_ff_rew(0);
850                 drec->set_remote(true);
851         }
852 }
853
854
855 void VM::push_stop(int drv)
856 {
857         if(drec != NULL) {
858                 drec->set_remote(false);
859         }
860 }
861
862 void VM::push_fast_forward(int drv)
863 {
864         if(drec != NULL) {
865                 drec->set_ff_rew(1);
866                 drec->set_remote(true);
867         }
868 }
869
870 void VM::push_fast_rewind(int drv)
871 {
872         if(drec != NULL) {
873                 drec->set_ff_rew(-1);
874                 drec->set_remote(true);
875         }
876 }
877
878 void VM::push_apss_forward(int drv)
879 {
880         if(drec != NULL) {
881                 drec->do_apss(1);
882         }
883 }
884
885 void VM::push_apss_rewind(int drv)
886 {
887         if(drec != NULL) {
888                 drec->do_apss(-1);
889         }
890 }
891
892 bool VM::is_frame_skippable()
893 {
894         return event->is_frame_skippable();
895 }
896
897 void VM::update_dipswitch()
898 {
899 #if defined(WITH_Z80)
900         g_intr_irq->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_IRQ_ON) != 0) ? 1 : 0, 1);
901         g_intr_firq->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_FIRQ_ON) != 0) ? 1 : 0, 1);
902         g_nmi->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_NMI_ON) != 0) ? 1 : 0, 1);
903 #endif
904 }
905
906 void VM::set_cpu_clock(DEVICE *cpu, uint32_t clocks) {
907         event->set_secondary_cpu_clock(cpu, clocks);
908 }
909
910 #if defined(USE_BUBBLE1)
911 void VM::open_bubble_casette(int drv, const _TCHAR *path, int bank)
912 {
913         if((drv >= 2) || (drv < 0)) return;
914         if(bubble_casette[drv] == NULL) return;
915         bubble_casette[drv]->open((_TCHAR *)path, bank);
916 }
917
918 void VM::close_bubble_casette(int drv)
919 {
920         if((drv >= 2) || (drv < 0)) return;
921         if(bubble_casette[drv] == NULL) return;
922         bubble_casette[drv]->close();
923 }
924
925 bool VM::is_bubble_casette_inserted(int drv)
926 {
927         if((drv >= 2) || (drv < 0)) return false;
928         if(bubble_casette[drv] == NULL) return false;
929         return bubble_casette[drv]->is_bubble_inserted();
930 }
931
932 bool VM::is_bubble_casette_protected(int drv)
933 {
934         if((drv >= 2) || (drv < 0)) return false;
935         if(bubble_casette[drv] == NULL) return false;
936         return bubble_casette[drv]->is_bubble_protected();
937 }
938
939 void VM::is_bubble_casette_protected(int drv, bool flag)
940 {
941         if((drv >= 2) || (drv < 0)) return;
942         if(bubble_casette[drv] == NULL) return;
943         bubble_casette[drv]->set_bubble_protect(flag);
944 }
945 #endif
946
947
948 #define STATE_VERSION   8
949 void VM::save_state(FILEIO* state_fio)
950 {
951         state_fio->FputUint32_BE(STATE_VERSION);
952         state_fio->FputBool(connect_320kfdc);
953         state_fio->FputBool(connect_1Mfdc);
954         for(DEVICE* device = first_device; device; device = device->next_device) {
955                 const char *name = typeid(*device).name() + 6; // skip "class "
956                 
957                 state_fio->FputInt32(strlen(name));
958                 state_fio->Fwrite(name, strlen(name), 1);
959                 device->save_state(state_fio);
960         }
961 }
962
963 bool VM::load_state(FILEIO* state_fio)
964 {
965         uint32_t version = state_fio->FgetUint32_BE();
966         if(version != STATE_VERSION) {
967                 return false;
968         }
969         connect_320kfdc = state_fio->FgetBool();
970         connect_1Mfdc = state_fio->FgetBool();
971         for(DEVICE* device = first_device; device; device = device->next_device) {
972                 const char *name = typeid(*device).name() + 6; // skip "class "
973                 
974                 if(!(state_fio->FgetInt32() == strlen(name) && state_fio->Fcompare(name, strlen(name)))) {
975                         printf("Load Error: DEVID=%d\n", device->this_device_id);
976                         return false;
977                 }
978                 if(!device->load_state(state_fio)) {
979                         printf("Load Error: DEVID=%d\n", device->this_device_id);
980                         return false;
981                 }
982         }
983         return true;
984 }
985
986 #ifdef USE_DIG_RESOLUTION
987 void VM::get_screen_resolution(int *w, int *h)
988 {
989         switch(display->get_screen_mode()) {
990         case DISPLAY_MODE_8_200L:
991         case DISPLAY_MODE_8_200L_TEXT:
992                 *w = 640;
993                 *h = 200;
994                 break;
995         case DISPLAY_MODE_8_400L:
996         case DISPLAY_MODE_8_400L_TEXT:
997                 *w = 640;
998                 *h = 400;
999                 break;
1000         case DISPLAY_MODE_4096:
1001         case DISPLAY_MODE_256k:
1002                 *w = 320;
1003                 *h = 200;
1004                 break;
1005         default:
1006                 *w = 640;
1007                 *h = 200;
1008                 break;
1009         }
1010 }
1011 #endif
1012
1013 bool VM::is_screen_changed()
1014 {
1015         bool f = true;
1016 #if defined(USE_MINIMUM_RENDERING)
1017         f = display->screen_update();
1018         display->reset_screen_update();
1019 #endif  
1020         return f;
1021 }