OSDN Git Service

[VM][FM7] Sync via sub and main to 500uS, reduce usage of HOST CPU.
[csp-qt/common_source_project-fm7.git] / source / src / vm / fm7 / fm7.cpp
1
2 /*
3  * FM7 -> VM
4  * (C) 2015 K.Ohta <whatisthis.sowhat _at_ gmail.com>
5  * History:
6  *   Feb 27, 2015 : Initial
7  */
8
9 #include "fm7.h"
10 #include "../../emu.h"
11 #include "../../config.h"
12 #include "../device.h"
13 #include "../event.h"
14 #include "../memory.h"
15 #ifdef USE_DEBUGGER
16 #include "../debugger.h"
17 #endif
18
19
20 #include "../datarec.h"
21 #include "../disk.h"
22
23 #include "../mc6809.h"
24 #include "../z80.h"
25 #include "../ym2203.h"
26 #include "../mb8877.h"
27 //#include "../beep.h"
28 #include "../pcm1bit.h"
29 #include "../ym2203.h"
30 #if defined(_FM77AV_VARIANTS)
31 #include "./mb61vh010.h"
32 #endif
33
34 #include "./fm7_mainio.h"
35 #include "./fm7_mainmem.h"
36 #include "./fm7_display.h"
37 #include "./fm7_keyboard.h"
38
39 #include "./kanjirom.h"
40
41 VM::VM(EMU* parent_emu): emu(parent_emu)
42 {
43         
44         first_device = last_device = NULL;
45         connect_opn = false;
46         connect_whg = false;
47         connect_thg = false;
48         opn[0] = opn[1] = opn[2] = psg = NULL; 
49    
50         dummy = new DEVICE(this, emu);  // must be 1st device
51         event = new EVENT(this, emu);   // must be 2nd device
52         
53         dummycpu = new DEVICE(this, emu);
54         // basic devices
55         mainmem = new FM7_MAINMEM(this, emu);
56         mainio  = new FM7_MAINIO(this, emu);
57         
58         display = new DISPLAY(this, emu);
59         keyboard = new KEYBOARD(this, emu);
60 #if defined(_FM77AV_VARIANTS)
61         alu = new MB61VH010(this, emu);
62 #endif  
63
64         // I/Os
65         drec = new DATAREC(this, emu);
66         pcm1bit = new PCM1BIT(this, emu);
67 //      beep = new BEEP(this, emu);
68         fdc  = new MB8877(this, emu);
69         
70         opn[0] = new YM2203(this, emu); // OPN
71         opn[1] = new YM2203(this, emu); // WHG
72         opn[2] = new YM2203(this, emu); // THG
73 #if !defined(_FM77AV_VARIANTS)
74         psg = new YM2203(this, emu);
75 #endif
76         kanjiclass1 = new KANJIROM(this, emu, false);
77 #ifdef CAPABLE_KANJI_CLASS2
78         kanjiclass2 = new KANJIROM(this, emu, true);
79 #endif
80         maincpu = new MC6809(this, emu);
81         subcpu = new MC6809(this, emu);
82 #ifdef WITH_Z80
83         z80cpu = new Z80(this, emu);
84 #endif
85         connect_bus();
86         initialize();
87 }
88
89 VM::~VM()
90 {
91         // delete all devices
92         for(DEVICE* device = first_device; device;) {
93                 DEVICE *next_device = device->next_device;
94                 device->release();
95                 delete device;
96                 device = next_device;
97         }
98 }
99
100 DEVICE* VM::get_device(int id)
101 {
102         for(DEVICE* device = first_device; device; device = device->next_device) {
103                 if(device->this_device_id == id) {
104                         return device;
105                 }
106         }
107         return NULL;
108 }
109
110
111 void VM::initialize(void)
112 {
113 #if defined(_FM8) || defined(_FM7)
114         cycle_steal = false;
115 #else
116         cycle_steal = true;
117 #endif
118         clock_low = false;
119 }
120
121
122 void VM::connect_bus(void)
123 {
124         int i;
125         uint32 subclock;
126         uint32 mainclock;
127         
128         /*
129          * CLASS CONSTRUCTION
130          *
131          * VM 
132          *  |-> MAINCPU -> MAINMEM -> MAINIO -> MAIN DEVICES
133          *  |             |        |      
134          *  | -> SUBCPU  -> SUBMEM  -> SUBIO -> SUB DEVICES
135          *  | -> DISPLAY
136          *  | -> KEYBOARD
137          *
138          *  MAINMEM can access SUBMEM/IO, when SUBCPU is halted.
139          *  MAINMEM and SUBMEM can access DISPLAY and KEYBOARD with exclusive.
140          *  MAINCPU can access MAINMEM.
141          *  SUBCPU  can access SUBMEM.
142          *  DISPLAY : R/W from MAINCPU and SUBCPU.
143          *  KEYBOARD : R/W
144          *
145          */
146         event->set_frames_per_sec(60.00);
147         event->set_lines_per_frame(400);
148 #if defined(_FM77AV40) || defined(_FM77AV20)
149         event->set_context_cpu(dummycpu, MAINCLOCK_FAST_MMR * 2);
150 #else
151         event->set_context_cpu(dummycpu, SUBCLOCK_NORMAL * 2);
152 #endif
153         
154 #if defined(_FM8)
155         mainclock = MAINCLOCK_SLOW;
156         subclock = SUBCLOCK_SLOW;
157 #else
158         if(config.cpu_type == 0) {
159                 // 2MHz
160                 subclock = SUBCLOCK_NORMAL;
161                 mainclock = MAINCLOCK_NORMAL;
162         } else {
163                 // 1.2MHz
164                 mainclock = MAINCLOCK_SLOW;
165                 subclock = SUBCLOCK_SLOW;
166         }
167 #endif
168 #if defined(_FM77AV40) || defined(_FM77AV20)
169         event->set_context_cpu(maincpu, MAINCLOCK_FAST_MMR);
170 #else
171         event->set_context_cpu(maincpu, MAINCLOCK_NORMAL);
172 #endif  
173         event->set_context_cpu(subcpu,  SUBCLOCK_NORMAL);
174    
175 #ifdef WITH_Z80
176         event->set_context_cpu(z80cpu,  4000000);
177         z80cpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
178 #endif
179
180 //      event->set_context_sound(beep);
181         event->set_context_sound(pcm1bit);
182 #if !defined(_FM77AV_VARIANTS)
183         mainio->set_context_psg(psg);
184         event->set_context_sound(psg);
185 #endif
186         event->set_context_sound(opn[0]);
187         event->set_context_sound(opn[1]);
188         event->set_context_sound(opn[2]);
189         event->set_context_sound(drec);
190         //event->register_vline_event(display);
191         //event->register_vline_event(mainio);
192    
193         mainio->set_context_maincpu(maincpu);
194         mainio->set_context_subcpu(subcpu);
195         
196         mainio->set_context_display(display);
197         mainio->set_context_kanjirom_class1(kanjiclass1);
198         mainio->set_context_mainmem(mainmem);
199         mainio->set_context_keyboard(keyboard);
200    
201 #if defined(CAPABLE_KANJI_CLASS2)
202         mainio->set_context_kanjirom_class2(kanjiclass2);
203 #endif
204
205         keyboard->set_context_break_line(mainio, FM7_MAINIO_PUSH_BREAK, 0xffffffff);
206         keyboard->set_context_mainio(mainio);
207         keyboard->set_context_display(display);
208         
209         keyboard->set_context_rxrdy(keyboard, SIG_FM7KEY_RXRDY, 0x01);
210         keyboard->set_context_rxrdy(display, SIG_FM7KEY_RXRDY, 0x01);
211         
212         keyboard->set_context_key_ack(keyboard, SIG_FM7KEY_ACK, 0x01);
213         keyboard->set_context_key_ack(display, SIG_FM7KEY_ACK, 0x01);
214    
215         drec->set_context_out(mainio, FM7_MAINIO_CMT_RECV, 0xffffffff);
216         //drec->set_context_remote(mainio, FM7_MAINIO_CMT_REMOTE, 0xffffffff);
217         mainio->set_context_datarec(drec);
218         mainmem->set_context_mainio(mainio);
219         mainmem->set_context_display(display);
220         mainmem->set_context_maincpu(maincpu);
221   
222         display->set_context_mainio(mainio);
223         display->set_context_subcpu(subcpu);
224         display->set_context_keyboard(keyboard);
225         subcpu->set_context_bus_halt(display, SIG_FM7_SUB_HALT, 0xffffffff);
226
227         display->set_context_kanjiclass1(kanjiclass1);
228 #if defined(CAPABLE_KANJI_CLASS2)
229         display->set_context_kanjiclass2(kanjiclass2);
230 #endif   
231 #if defined(_FM77AV_VARIANTS)
232         display->set_context_alu(alu);
233         alu->set_context_memory(display);
234 #endif  
235         // Palette, VSYNC, HSYNC, Multi-page, display mode. 
236         mainio->set_context_display(display);
237         
238         //FDC
239         mainio->set_context_fdc(fdc);
240         fdc->set_context_irq(mainio, FM7_MAINIO_FDC_IRQ, 0x1);
241         fdc->set_context_drq(mainio, FM7_MAINIO_FDC_DRQ, 0x1);
242         // SOUND
243         mainio->set_context_beep(pcm1bit);
244         //mainio->set_context_beep(beep);
245         
246         opn[0]->set_context_irq(mainio, FM7_MAINIO_OPN_IRQ, 0xffffffff);
247         //opn[0]->set_context_port_a(mainio, FM7_MAINIO_OPNPORTA_CHANGED, 0xff, 0);
248         //opn[0]->set_context_port_b(mainio, FM7_MAINIO_OPNPORTB_CHANGED, 0xff, 0);
249         mainio->set_context_opn(opn[0], 0);
250         opn[1]->set_context_irq(mainio, FM7_MAINIO_WHG_IRQ, 0xffffffff);
251         mainio->set_context_opn(opn[1], 1);
252         opn[2]->set_context_irq(mainio, FM7_MAINIO_THG_IRQ, 0xffffffff);
253         mainio->set_context_opn(opn[2], 2);
254    
255         subcpu->set_context_bus_halt(mainmem, SIG_FM7_SUB_HALT, 0xffffffff);
256         subcpu->set_context_bus_clr(display, SIG_FM7_SUB_USE_CLR, 0x0000000f);
257    
258         maincpu->set_context_mem(mainmem);
259         subcpu->set_context_mem(display);
260 #ifdef USE_DEBUGGER
261         maincpu->set_context_debugger(new DEBUGGER(this, emu));
262         subcpu->set_context_debugger(new DEBUGGER(this, emu));
263 #endif
264
265         for(DEVICE* device = first_device; device; device = device->next_device) {
266                 device->initialize();
267         }
268         maincpu->write_signal(SIG_CPU_BUSREQ, 0, 1);
269         subcpu->write_signal(SIG_CPU_BUSREQ, 0, 1);
270    
271         for(int i = 0; i < 2; i++) {
272 #if defined(_FM77AV20) || defined(_FM77AV40SX) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
273                 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
274 #else
275                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
276 #endif
277                 fdc->set_drive_rpm(i, 300);
278                 fdc->set_drive_mfm(i, true);
279         }
280 #if defined(_FM77) || defined(_FM77L4)
281         for(int i = 2; i < 4; i++) {
282                 fdc->set_drive_type(i, DRIVE_TYPE_2HD);
283 //              fdc->set_drive_rpm(i, 300);
284 //              fdc->set_drive_mfm(i, true);
285         }
286 #endif
287         
288 }  
289
290 void VM::update_config()
291 {
292    uint32 subclock;
293
294 #if !defined(_FM8)
295         switch(config.cpu_type){
296                 case 0:
297                         event->set_secondary_cpu_clock(maincpu, MAINCLOCK_NORMAL);
298                         break;
299                 case 1:
300                         event->set_secondary_cpu_clock(maincpu, MAINCLOCK_SLOW);
301                         break;
302         }
303 #endif
304
305         for(DEVICE* device = first_device; device; device = device->next_device) {
306                 device->update_config();
307         }
308         //update_dipswitch();
309 }
310
311 void VM::reset()
312 {
313         int i, j;
314         uint8 data;
315         // reset all devices
316         for(DEVICE* device = first_device; device; device = device->next_device) {
317                 device->reset();
318         }
319         opn[0]->SetReg(0x2e, 0);        // set prescaler
320         opn[1]->SetReg(0x2e, 0);        // set prescaler
321         opn[2]->SetReg(0x2e, 0);        // set prescaler
322
323         // Init OPN/PSG.
324         // Parameters from XM7.
325         opn[0]->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
326         opn[1]->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
327         opn[2]->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
328
329 #if !defined(_FM77AV_VARIANTS)
330         psg->SetReg(0x27, 0); // stop timer
331         psg->SetReg(0x2e, 0);   // set prescaler
332         psg->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
333 #endif  
334 }
335
336 void VM::special_reset()
337 {
338         // BREAK + RESET
339         mainio->write_signal(FM7_MAINIO_PUSH_BREAK, 1, 1);
340         mainio->reset();
341         display->reset();
342         subcpu->reset();   
343         maincpu->reset();
344         mainio->write_signal(FM7_MAINIO_PUSH_BREAK, 1, 1);
345         event->register_event(mainio, EVENT_UP_BREAK, 10000.0 * 1000.0, false, NULL);
346 }
347
348 void VM::run()
349 {
350         event->drive();
351 }
352
353 double VM::frame_rate()
354 {
355         return event->frame_rate();
356 }
357
358 // ----------------------------------------------------------------------------
359 // debugger
360 // ----------------------------------------------------------------------------
361
362 #ifdef USE_DEBUGGER
363 DEVICE *VM::get_cpu(int index)
364 {
365         if(index == 0) {
366                 return maincpu;
367         } else if(index == 1) {
368                 return subcpu;
369         }
370 #if defined(_WITH_Z80)
371         else if(index == 2) {
372                 return z80cpu;
373         }
374 #endif
375         return NULL;
376 }
377 #endif
378
379 // ----------------------------------------------------------------------------
380 // draw screen
381 // ----------------------------------------------------------------------------
382
383 void VM::draw_screen()
384 {
385         display->draw_screen();
386 }
387
388 int VM::access_lamp()
389 {
390         uint32 status = fdc->read_signal(0);
391         return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
392 }
393
394 void VM::initialize_sound(int rate, int samples)
395 {
396         // init sound manager
397         event->initialize_sound(rate, samples);
398         // init sound gen
399         opn[0]->init(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
400         opn[1]->init(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
401         opn[2]->init(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
402 #if !defined(_FM77AV_VARIANTS)   
403         psg->init(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
404 #endif   
405         pcm1bit->init(rate, 2000);
406         //beep->init(rate, 1200, 2000);
407         //drec->init_pcm(rate, 0);
408 }
409
410 uint16* VM::create_sound(int* extra_frames)
411 {
412         uint16* p = event->create_sound(extra_frames);
413         return p;
414 }
415
416 int VM::sound_buffer_ptr()
417 {
418         int pos = event->sound_buffer_ptr();
419         return pos; 
420 }
421
422 // ----------------------------------------------------------------------------
423 // notify key
424 // ----------------------------------------------------------------------------
425
426 void VM::key_down(int code, bool repeat)
427 {
428         if(!repeat) {
429                 keyboard->key_down(code);
430         }
431 }
432
433 void VM::key_up(int code)
434 {
435         keyboard->key_up(code);
436 }
437
438 // ----------------------------------------------------------------------------
439 // user interface
440 // ----------------------------------------------------------------------------
441
442 void VM::open_disk(int drv, _TCHAR* file_path, int bank)
443 {
444         fdc->open_disk(drv, file_path, bank);
445 }
446
447 void VM::close_disk(int drv)
448 {
449         fdc->close_disk(drv);
450 }
451
452 bool VM::disk_inserted(int drv)
453 {
454         return fdc->disk_inserted(drv);
455 }
456
457 #if defined(USE_DISK_WRITE_PROTECT)
458 void VM::write_protect_fd(int drv, bool flag)
459 {
460         fdc->write_protect_fd(drv, flag);
461 }
462
463 bool VM::is_write_protect_fd(int drv)
464 {
465         return fdc->is_write_protect_fd(drv);
466 }
467 #endif
468
469 void VM::play_tape(_TCHAR* file_path)
470 {
471         bool value = drec->play_tape(file_path);
472 }
473
474 void VM::rec_tape(_TCHAR* file_path)
475 {
476         bool value = drec->rec_tape(file_path);
477 }
478
479 void VM::close_tape()
480 {
481         drec->close_tape();
482 }
483
484 bool VM::tape_inserted()
485 {
486         return drec->tape_inserted();
487 }
488
489 #if defined(USE_TAPE_PTR)
490 int VM::get_tape_ptr(void)
491 {
492         return drec->get_tape_ptr();
493 }
494 #endif
495
496 bool VM::now_skip()
497 {
498         return event->now_skip();
499 }
500
501 void VM::update_dipswitch()
502 {
503         // bit0         0=High 1=Standard
504         // bit2         0=5"2D 1=5"2HD
505   //    io->set_iovalue_single_r(0x1ff0, (config.monitor_type & 1) | ((config.drive_type & 1) << 2));
506 }
507
508 void VM::set_cpu_clock(DEVICE *cpu, uint32 clocks) {
509         event->set_secondary_cpu_clock(cpu, clocks);
510 }
511
512 #define STATE_VERSION   1
513