OSDN Git Service

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