OSDN Git Service

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