OSDN Git Service

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