OSDN Git Service

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