OSDN Git Service

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