OSDN Git Service

[VM][General] Merge upstream 2016-03-01. (Pahse 1).
[csp-qt/common_source_project-fm7.git] / source / src / vm / pc8801 / pc8801.cpp
1 /*
2         NEC PC-8801MA Emulator 'ePC-8801MA'
3         NEC PC-8001mkIISR Emulator 'ePC-8001mkIISR'
4
5         Author : Takeda.Toshiya
6         Date   : 2012.02.16-
7
8         [ virtual machine ]
9 */
10
11 #include "pc8801.h"
12 #include "../../emu.h"
13 #include "../device.h"
14 #include "../event.h"
15
16 #include "../i8251.h"
17 #include "../i8255.h"
18 #include "../pcm1bit.h"
19 //#include "../pcpr201.h"
20 #include "../prnfile.h"
21 #include "../upd1990a.h"
22 #include "../ym2203.h"
23 #include "../z80.h"
24
25 #include "../disk.h"
26 #include "../pc80s31k.h"
27 #include "../upd765a.h"
28
29 #ifdef USE_DEBUGGER
30 #include "../debugger.h"
31 #endif
32
33 #ifdef SUPPORT_PC88_PCG8100
34 #include "../i8253.h"
35 #endif
36
37 #include "pc88.h"
38
39 // ----------------------------------------------------------------------------
40 // initialize
41 // ----------------------------------------------------------------------------
42
43 VM::VM(EMU* parent_emu) : emu(parent_emu)
44 {
45         // check configs
46         boot_mode = config.boot_mode;
47         
48         // create devices
49         first_device = last_device = NULL;
50         dummy = new DEVICE(this, emu);  // must be 1st device
51         
52         pc88event = new EVENT(this, emu);
53 //      pc88event->set_frames_per_sec(60);
54 //      pc88event->set_lines_per_frame(260);
55         
56         pc88 = new PC88(this, emu);
57 //      pc88->set_context_event_manager(pc88event);
58         pc88sio = new I8251(this, emu);
59 //      pc88sio->set_context_event_manager(pc88event);
60         pc88pio = new I8255(this, emu);
61 //      pc88pio->set_context_event_manager(pc88event);
62         pc88pcm = new PCM1BIT(this, emu);
63 //      pc88pcm->set_context_event_manager(pc88event);
64         pc88rtc = new UPD1990A(this, emu);
65 //      pc88rtc->set_context_event_manager(pc88event);
66         // config.sound_device_type
67         //      0: 44h:OPNA A4h:None            PC-8801FH/MH or later
68         //      1: 44h:OPN  A4h:None            PC-8801mkIISR/TR/MR/FR
69         //      2: 44h:OPN  A4h:OPNA            PC-8801mkIISR/TR/MR/FR + PC-8801-23
70         pc88opn = new YM2203(this, emu);
71 //      pc88opn->set_context_event_manager(pc88event);
72 #ifdef USE_SOUND_DEVICE_TYPE
73 #ifdef SUPPORT_PC88_OPNA
74         pc88opn->is_ym2608 = (config.sound_device_type == 0);
75 #endif
76 #ifdef SUPPORT_PC88_SB2
77         if(config.sound_device_type == 2) {
78                 pc88sb2 = new YM2203(this, emu);
79 #ifdef SUPPORT_PC88_OPNA
80                 pc88sb2->is_ym2608 = true;
81 #endif
82 //              pc88sb2->set_context_event_manager(pc88event);
83         } else {
84                 pc88sb2 = NULL;
85         }
86 #endif
87 #endif
88         if(config.printer_device_type == 0) {
89                 pc88prn = new PRNFILE(this, emu);
90 //              pc88prn->set_context_event_manager(pc88event);
91 //      } else if(config.printer_device_type == 1) {
92 //              pc88prn = new PCPR201(this, emu);
93 //              pc88prn->set_context_event_manager(pc88event);
94         } else {
95                 pc88prn = dummy;
96         }
97         pc88cpu = new Z80(this, emu);
98 //      pc88cpu->set_context_event_manager(pc88event);
99         dummycpu = new DEVICE(this, emu);
100         
101         pc88sub = new PC80S31K(this, emu);
102 //      pc88sub->set_context_event_manager(pc88event);
103         pc88pio_sub = new I8255(this, emu);
104 //      pc88pio_sub->set_context_event_manager(pc88event);
105         pc88fdc_sub = new UPD765A(this, emu);
106 //      pc88fdc_sub->set_context_event_manager(pc88event);
107         pc88cpu_sub = new Z80(this, emu);
108 //      pc88cpu_sub->set_context_event_manager(pc88event);
109         
110 #ifdef SUPPORT_PC88_PCG8100
111         pc88pit = new I8253(this, emu);
112 //      pc88pit->set_context_event_manager(pc88event);
113         pc88pcm0 = new PCM1BIT(this, emu);
114 //      pc88pcm->set_context_event_manager(pc88event);
115         pc88pcm1 = new PCM1BIT(this, emu);
116 //      pc88pcm->set_context_event_manager(pc88event);
117         pc88pcm2 = new PCM1BIT(this, emu);
118 //      pc88pcm->set_context_event_manager(pc88event);
119 #endif
120         
121 #ifdef SUPPORT_PC88_HIGH_CLOCK
122         pc88event->set_context_cpu(dummycpu, 7987248 / 8);
123         pc88event->set_context_cpu(pc88cpu, (config.cpu_type != 0) ? 3993624 : 7987248);
124 #else
125         pc88event->set_context_cpu(dummycpu, 3993624 / 4);
126         pc88event->set_context_cpu(pc88cpu, 3993624);
127 #endif
128         pc88event->set_context_cpu(pc88cpu_sub, 3993624);
129         pc88event->set_context_sound(pc88opn);
130 #ifdef SUPPORT_PC88_SB2
131         if(pc88sb2 != NULL) {
132                 pc88event->set_context_sound(pc88sb2);
133         }
134 #endif
135         pc88event->set_context_sound(pc88pcm);
136 #ifdef SUPPORT_PC88_PCG8100
137         pc88event->set_context_sound(pc88pcm0);
138         pc88event->set_context_sound(pc88pcm1);
139         pc88event->set_context_sound(pc88pcm2);
140 #endif
141         pc88->set_context_cpu(pc88cpu);
142         pc88->set_context_opn(pc88opn);
143 #ifdef SUPPORT_PC88_SB2
144         pc88->set_context_sb2(pc88sb2);
145 #endif
146         pc88->set_context_pcm(pc88pcm);
147         pc88->set_context_pio(pc88pio);
148         pc88->set_context_prn(pc88prn);
149         pc88->set_context_rtc(pc88rtc);
150         pc88->set_context_sio(pc88sio);
151 #ifdef SUPPORT_PC88_PCG8100
152         pc88->set_context_pcg_pit(pc88pit);
153         pc88->set_context_pcg_pcm0(pc88pcm0);
154         pc88->set_context_pcg_pcm1(pc88pcm1);
155         pc88->set_context_pcg_pcm2(pc88pcm2);
156 #endif
157         pc88cpu->set_context_mem(pc88);
158         pc88cpu->set_context_io(pc88);
159         pc88cpu->set_context_intr(pc88);
160 #ifdef USE_DEBUGGER
161         pc88cpu->set_context_debugger(new DEBUGGER(this, emu));
162 #endif
163         pc88opn->set_context_irq(pc88, SIG_PC88_SOUND_IRQ, 1);
164 #ifdef SUPPORT_PC88_SB2
165         if(pc88sb2 != NULL) {
166                 pc88sb2->set_context_irq(pc88, SIG_PC88_SB2_IRQ, 1);
167         }
168 #endif
169         pc88sio->set_context_rxrdy(pc88, SIG_PC88_USART_IRQ, 1);
170         pc88sio->set_context_out(pc88, SIG_PC88_USART_OUT);
171         
172         pc88sub->set_context_cpu(pc88cpu_sub);
173         pc88sub->set_context_fdc(pc88fdc_sub);
174         pc88sub->set_context_pio(pc88pio_sub);
175         pc88pio->set_context_port_a(pc88pio_sub, SIG_I8255_PORT_B, 0xff, 0);
176         pc88pio->set_context_port_b(pc88pio_sub, SIG_I8255_PORT_A, 0xff, 0);
177         pc88pio->set_context_port_c(pc88pio_sub, SIG_I8255_PORT_C, 0x0f, 4);
178         pc88pio->set_context_port_c(pc88pio_sub, SIG_I8255_PORT_C, 0xf0, -4);
179         pc88pio->clear_ports_by_cmdreg = true;
180         pc88pio_sub->set_context_port_a(pc88pio, SIG_I8255_PORT_B, 0xff, 0);
181         pc88pio_sub->set_context_port_b(pc88pio, SIG_I8255_PORT_A, 0xff, 0);
182         pc88pio_sub->set_context_port_c(pc88pio, SIG_I8255_PORT_C, 0x0f, 4);
183         pc88pio_sub->set_context_port_c(pc88pio, SIG_I8255_PORT_C, 0xf0, -4);
184         pc88pio_sub->clear_ports_by_cmdreg = true;
185         pc88fdc_sub->set_context_irq(pc88cpu_sub, SIG_CPU_IRQ, 1);
186         pc88cpu_sub->set_context_mem(pc88sub);
187         pc88cpu_sub->set_context_io(pc88sub);
188         pc88cpu_sub->set_context_intr(pc88sub);
189 #ifdef USE_DEBUGGER
190         pc88cpu_sub->set_context_debugger(new DEBUGGER(this, emu));
191 #endif
192         
193 #ifdef SUPPORT_PC88_PCG8100
194         pc88pit->set_context_ch0(pc88pcm0, SIG_PCM1BIT_SIGNAL, 1);
195         pc88pit->set_context_ch1(pc88pcm1, SIG_PCM1BIT_SIGNAL, 1);
196         pc88pit->set_context_ch2(pc88pcm2, SIG_PCM1BIT_SIGNAL, 1);
197         pc88pit->set_constant_clock(0, 3993624);
198         pc88pit->set_constant_clock(1, 3993624);
199         pc88pit->set_constant_clock(2, 3993624);
200 #endif
201         
202         // initialize all devices
203         for(DEVICE* device = first_device; device; device = device->next_device) {
204                 device->initialize();
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 // ----------------------------------------------------------------------------
230 // drive virtual machine
231 // ----------------------------------------------------------------------------
232
233 void VM::reset()
234 {
235         // reset all devices
236         for(DEVICE* device = first_device; device; device = device->next_device) {
237                 device->reset();
238         }
239         for(DEVICE* device = first_device; device; device = device->next_device) {
240                 device->reset();
241         }
242         
243         // initial device settings
244         pc88opn->set_reg(0x29, 3); // for Misty Blue
245         pc88pio->write_signal(SIG_I8255_PORT_C, 0, 0xff);
246         pc88pio_sub->write_signal(SIG_I8255_PORT_C, 0, 0xff);
247 }
248
249 void VM::run()
250 {
251         pc88event->drive();
252 }
253
254 double VM::get_frame_rate()
255 {
256         return pc88event->get_frame_rate();
257 }
258
259 // ----------------------------------------------------------------------------
260 // debugger
261 // ----------------------------------------------------------------------------
262
263 #ifdef USE_DEBUGGER
264 DEVICE *VM::get_cpu(int index)
265 {
266         if(index == 0) {
267                 return pc88cpu;
268         } else if(index == 1) {
269                 return pc88cpu_sub;
270         }
271         return NULL;
272 }
273 #endif
274
275 // ----------------------------------------------------------------------------
276 // draw screen
277 // ----------------------------------------------------------------------------
278
279 void VM::draw_screen()
280 {
281         pc88->draw_screen();
282 }
283
284 uint32_t VM::get_access_lamp_status()
285 {
286         return pc88fdc_sub->read_signal(0);
287 }
288
289 // ----------------------------------------------------------------------------
290 // soud manager
291 // ----------------------------------------------------------------------------
292
293 void VM::initialize_sound(int rate, int samples)
294 {
295         // init sound manager
296         pc88event->initialize_sound(rate, samples);
297         
298         // init sound gen
299 #ifdef SUPPORT_PC88_OPNA
300         if(pc88opn->is_ym2608) {
301                 pc88opn->initialize_sound(rate, 7987248, samples, 0, 0);
302         } else
303 #endif
304         pc88opn->initialize_sound(rate, 3993624, samples, 0, 0);
305 #ifdef SUPPORT_PC88_SB2
306         if(pc88sb2 != NULL) {
307 #ifdef SUPPORT_PC88_OPNA
308                 if(pc88sb2->is_ym2608) {
309                         pc88sb2->initialize_sound(rate, 7987248, samples, 0, 0);
310                 } else
311 #endif
312                 pc88sb2->initialize_sound(rate, 3993624, samples, 0, 0);
313         }
314 #endif
315         pc88pcm->initialize_sound(rate, 8000);
316 #ifdef SUPPORT_PC88_PCG8100
317         pc88pcm0->initialize_sound(rate, 8000);
318         pc88pcm1->initialize_sound(rate, 8000);
319         pc88pcm2->initialize_sound(rate, 8000);
320 #endif
321 }
322
323 uint16_t* VM::create_sound(int* extra_frames)
324 {
325         return pc88event->create_sound(extra_frames);
326 }
327
328 int VM::get_sound_buffer_ptr()
329 {
330         return pc88event->get_sound_buffer_ptr();
331 }
332
333 #ifdef USE_SOUND_VOLUME
334 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
335 {
336         if(ch-- == 0) {
337                 pc88opn->set_volume(0, decibel_l, decibel_r);
338         } else if(ch-- == 0) {
339                 pc88opn->set_volume(1, decibel_l, decibel_r);
340 #ifdef SUPPORT_PC88_OPNA
341         } else if(ch-- == 0) {
342                 pc88opn->set_volume(2, decibel_l, decibel_r);
343         } else if(ch-- == 0) {
344                 pc88opn->set_volume(3, decibel_l, decibel_r);
345 #endif
346 #ifdef SUPPORT_PC88_SB2
347         } else if(ch-- == 0) {
348                 if(pc88sb2 != NULL) {
349                         pc88sb2->set_volume(0, decibel_l, decibel_r);
350                 }
351         } else if(ch-- == 0) {
352                 if(pc88sb2 != NULL) {
353                         pc88sb2->set_volume(1, decibel_l, decibel_r);
354                 }
355         } else if(ch-- == 0) {
356                 if(pc88sb2 != NULL) {
357                         pc88sb2->set_volume(2, decibel_l, decibel_r);
358                 }
359         } else if(ch-- == 0) {
360                 if(pc88sb2 != NULL) {
361                         pc88sb2->set_volume(3, decibel_l, decibel_r);
362                 }
363 #endif
364 #ifdef SUPPORT_PC88_PCG8100
365         } else if(ch-- == 0) {
366                 pc88pcm0->set_volume(0, decibel_l, decibel_r);
367                 pc88pcm1->set_volume(0, decibel_l, decibel_r);
368                 pc88pcm2->set_volume(0, decibel_l, decibel_r);
369 #endif
370         } else if(ch-- == 0) {
371                 pc88pcm->set_volume(0, decibel_l, decibel_r);
372         }
373 }
374 #endif
375
376 // ----------------------------------------------------------------------------
377 // notify key
378 // ----------------------------------------------------------------------------
379
380 void VM::key_down(int code, bool repeat)
381 {
382         pc88->key_down(code, repeat);
383 }
384
385 void VM::key_up(int code)
386 {
387 }
388
389 // ----------------------------------------------------------------------------
390 // user interface
391 // ----------------------------------------------------------------------------
392
393 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
394 {
395         pc88fdc_sub->open_disk(drv, file_path, bank);
396 }
397
398 void VM::close_floppy_disk(int drv)
399 {
400         pc88fdc_sub->close_disk(drv);
401 }
402
403 bool VM::is_floppy_disk_inserted(int drv)
404 {
405         return pc88fdc_sub->is_disk_inserted(drv);
406 }
407
408 void VM::is_floppy_disk_protected(int drv, bool value)
409 {
410         pc88fdc_sub->is_disk_protected(drv, value);
411 }
412
413 bool VM::is_floppy_disk_protected(int drv)
414 {
415         return pc88fdc_sub->is_disk_protected(drv);
416 }
417
418 void VM::play_tape(const _TCHAR* file_path)
419 {
420         pc88->play_tape(file_path);
421 }
422
423 void VM::rec_tape(const _TCHAR* file_path)
424 {
425         pc88->rec_tape(file_path);
426 }
427
428 void VM::close_tape()
429 {
430         pc88->close_tape();
431 }
432
433 bool VM::is_tape_inserted()
434 {
435         return pc88->is_tape_inserted();
436 }
437
438 bool VM::is_frame_skippable()
439 {
440 //      return event->is_frame_skippable();
441         return pc88->is_frame_skippable();
442 }
443
444 void VM::update_config()
445 {
446         int ii;
447         uint32 vol1, vol2;
448         if(boot_mode != config.boot_mode) {
449                 // boot mode is changed !!!
450                 boot_mode = config.boot_mode;
451                 reset();
452         } else {
453                 for(DEVICE* device = first_device; device; device = device->next_device) {
454                         device->update_config();
455                 }
456         }
457 }
458
459 #define STATE_VERSION   6
460
461 void VM::save_state(FILEIO* state_fio)
462 {
463         state_fio->FputUint32(STATE_VERSION);
464         
465         for(DEVICE* device = first_device; device; device = device->next_device) {
466                 device->save_state(state_fio);
467         }
468         state_fio->FputInt32(boot_mode);
469 }
470
471 bool VM::load_state(FILEIO* state_fio)
472 {
473         if(state_fio->FgetUint32() != STATE_VERSION) {
474                 return false;
475         }
476         for(DEVICE* device = first_device; device; device = device->next_device) {
477                 if(!device->load_state(state_fio)) {
478                         return false;
479                 }
480         }
481         boot_mode = state_fio->FgetInt32();
482         return true;
483 }
484