OSDN Git Service

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