OSDN Git Service

[VM][Qt][General][CMAKE] Merge upstream 2016-03-01, phase 2. Expect to build and...
[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         
100         pc88sub = new PC80S31K(this, emu);
101 //      pc88sub->set_context_event_manager(pc88event);
102         pc88pio_sub = new I8255(this, emu);
103 //      pc88pio_sub->set_context_event_manager(pc88event);
104         pc88fdc_sub = new UPD765A(this, emu);
105 //      pc88fdc_sub->set_context_event_manager(pc88event);
106         pc88cpu_sub = new Z80(this, emu);
107 //      pc88cpu_sub->set_context_event_manager(pc88event);
108         
109 #ifdef SUPPORT_PC88_PCG8100
110         pc88pit = new I8253(this, emu);
111 //      pc88pit->set_context_event_manager(pc88event);
112         pc88pcm0 = new PCM1BIT(this, emu);
113 //      pc88pcm->set_context_event_manager(pc88event);
114         pc88pcm1 = new PCM1BIT(this, emu);
115 //      pc88pcm->set_context_event_manager(pc88event);
116         pc88pcm2 = new PCM1BIT(this, emu);
117 //      pc88pcm->set_context_event_manager(pc88event);
118 #endif
119         
120 #ifdef SUPPORT_PC88_HIGH_CLOCK
121         pc88event->set_context_cpu(pc88cpu, (config.cpu_type != 0) ? 3993624 : 7987248);
122 #else
123         pc88event->set_context_cpu(pc88cpu, 3993624);
124 #endif
125         pc88event->set_context_cpu(pc88cpu_sub, 3993624);
126         pc88event->set_context_sound(pc88opn);
127 #ifdef SUPPORT_PC88_SB2
128         if(pc88sb2 != NULL) {
129                 pc88event->set_context_sound(pc88sb2);
130         }
131 #endif
132         pc88event->set_context_sound(pc88pcm);
133 #ifdef SUPPORT_PC88_PCG8100
134         pc88event->set_context_sound(pc88pcm0);
135         pc88event->set_context_sound(pc88pcm1);
136         pc88event->set_context_sound(pc88pcm2);
137 #endif
138         
139         pc88->set_context_cpu(pc88cpu);
140         pc88->set_context_opn(pc88opn);
141 #ifdef SUPPORT_PC88_SB2
142         pc88->set_context_sb2(pc88sb2);
143 #endif
144         pc88->set_context_pcm(pc88pcm);
145         pc88->set_context_pio(pc88pio);
146         pc88->set_context_prn(pc88prn);
147         pc88->set_context_rtc(pc88rtc);
148         pc88->set_context_sio(pc88sio);
149 #ifdef SUPPORT_PC88_PCG8100
150         pc88->set_context_pcg_pit(pc88pit);
151         pc88->set_context_pcg_pcm0(pc88pcm0);
152         pc88->set_context_pcg_pcm1(pc88pcm1);
153         pc88->set_context_pcg_pcm2(pc88pcm2);
154 #endif
155         pc88cpu->set_context_mem(pc88);
156         pc88cpu->set_context_io(pc88);
157         pc88cpu->set_context_intr(pc88);
158 #ifdef USE_DEBUGGER
159         pc88cpu->set_context_debugger(new DEBUGGER(this, emu));
160 #endif
161         pc88opn->set_context_irq(pc88, SIG_PC88_SOUND_IRQ, 1);
162 #ifdef SUPPORT_PC88_SB2
163         if(pc88sb2 != NULL) {
164                 pc88sb2->set_context_irq(pc88, SIG_PC88_SB2_IRQ, 1);
165         }
166 #endif
167         pc88sio->set_context_rxrdy(pc88, SIG_PC88_USART_IRQ, 1);
168         pc88sio->set_context_out(pc88, SIG_PC88_USART_OUT);
169         
170         pc88sub->set_context_cpu(pc88cpu_sub);
171         pc88sub->set_context_fdc(pc88fdc_sub);
172         pc88sub->set_context_pio(pc88pio_sub);
173         pc88pio->set_context_port_a(pc88pio_sub, SIG_I8255_PORT_B, 0xff, 0);
174         pc88pio->set_context_port_b(pc88pio_sub, SIG_I8255_PORT_A, 0xff, 0);
175         pc88pio->set_context_port_c(pc88pio_sub, SIG_I8255_PORT_C, 0x0f, 4);
176         pc88pio->set_context_port_c(pc88pio_sub, SIG_I8255_PORT_C, 0xf0, -4);
177         pc88pio->clear_ports_by_cmdreg = true;
178         pc88pio_sub->set_context_port_a(pc88pio, SIG_I8255_PORT_B, 0xff, 0);
179         pc88pio_sub->set_context_port_b(pc88pio, SIG_I8255_PORT_A, 0xff, 0);
180         pc88pio_sub->set_context_port_c(pc88pio, SIG_I8255_PORT_C, 0x0f, 4);
181         pc88pio_sub->set_context_port_c(pc88pio, SIG_I8255_PORT_C, 0xf0, -4);
182         pc88pio_sub->clear_ports_by_cmdreg = true;
183         pc88fdc_sub->set_context_irq(pc88cpu_sub, SIG_CPU_IRQ, 1);
184         pc88cpu_sub->set_context_mem(pc88sub);
185         pc88cpu_sub->set_context_io(pc88sub);
186         pc88cpu_sub->set_context_intr(pc88sub);
187 #ifdef USE_DEBUGGER
188         pc88cpu_sub->set_context_debugger(new DEBUGGER(this, emu));
189 #endif
190         
191 #ifdef SUPPORT_PC88_PCG8100
192         pc88pit->set_context_ch0(pc88pcm0, SIG_PCM1BIT_SIGNAL, 1);
193         pc88pit->set_context_ch1(pc88pcm1, SIG_PCM1BIT_SIGNAL, 1);
194         pc88pit->set_context_ch2(pc88pcm2, SIG_PCM1BIT_SIGNAL, 1);
195         pc88pit->set_constant_clock(0, 3993624);
196         pc88pit->set_constant_clock(1, 3993624);
197         pc88pit->set_constant_clock(2, 3993624);
198 #endif
199         
200         // initialize all devices
201         for(DEVICE* device = first_device; device; device = device->next_device) {
202                 device->initialize();
203         }
204 }
205
206 VM::~VM()
207 {
208         // delete all devices
209         for(DEVICE* device = first_device; device;) {
210                 DEVICE *next_device = device->next_device;
211                 device->release();
212                 delete device;
213                 device = next_device;
214         }
215 }
216
217 DEVICE* VM::get_device(int id)
218 {
219         for(DEVICE* device = first_device; device; device = device->next_device) {
220                 if(device->this_device_id == id) {
221                         return device;
222                 }
223         }
224         return NULL;
225 }
226
227 // ----------------------------------------------------------------------------
228 // drive virtual machine
229 // ----------------------------------------------------------------------------
230
231 void VM::reset()
232 {
233         // reset all devices
234         for(DEVICE* device = first_device; device; device = device->next_device) {
235                 device->reset();
236         }
237         for(DEVICE* device = first_device; device; device = device->next_device) {
238                 device->reset();
239         }
240         
241         // initial device settings
242         pc88opn->set_reg(0x29, 3); // for Misty Blue
243         pc88pio->write_signal(SIG_I8255_PORT_C, 0, 0xff);
244         pc88pio_sub->write_signal(SIG_I8255_PORT_C, 0, 0xff);
245 }
246
247 void VM::run()
248 {
249         pc88event->drive();
250 }
251
252 double VM::get_frame_rate()
253 {
254         return pc88event->get_frame_rate();
255 }
256
257 // ----------------------------------------------------------------------------
258 // debugger
259 // ----------------------------------------------------------------------------
260
261 #ifdef USE_DEBUGGER
262 DEVICE *VM::get_cpu(int index)
263 {
264         if(index == 0) {
265                 return pc88cpu;
266         } else if(index == 1) {
267                 return pc88cpu_sub;
268         }
269         return NULL;
270 }
271 #endif
272
273 // ----------------------------------------------------------------------------
274 // draw screen
275 // ----------------------------------------------------------------------------
276
277 void VM::draw_screen()
278 {
279         pc88->draw_screen();
280 }
281
282 uint32_t VM::get_access_lamp_status()
283 {
284         return pc88fdc_sub->read_signal(0);
285 }
286
287 // ----------------------------------------------------------------------------
288 // soud manager
289 // ----------------------------------------------------------------------------
290
291 void VM::initialize_sound(int rate, int samples)
292 {
293         // init sound manager
294         pc88event->initialize_sound(rate, samples);
295         
296         // init sound gen
297 #ifdef SUPPORT_PC88_OPNA
298         if(pc88opn->is_ym2608) {
299                 pc88opn->initialize_sound(rate, 7987248, samples, 0, 0);
300         } else
301 #endif
302         pc88opn->initialize_sound(rate, 3993624, samples, 0, 0);
303 #ifdef SUPPORT_PC88_SB2
304         if(pc88sb2 != NULL) {
305 #ifdef SUPPORT_PC88_OPNA
306                 if(pc88sb2->is_ym2608) {
307                         pc88sb2->initialize_sound(rate, 7987248, samples, 0, 0);
308                 } else
309 #endif
310                 pc88sb2->initialize_sound(rate, 3993624, samples, 0, 0);
311         }
312 #endif
313         pc88pcm->initialize_sound(rate, 8000);
314 #ifdef SUPPORT_PC88_PCG8100
315         pc88pcm0->initialize_sound(rate, 8000);
316         pc88pcm1->initialize_sound(rate, 8000);
317         pc88pcm2->initialize_sound(rate, 8000);
318 #endif
319 }
320
321 uint16_t* VM::create_sound(int* extra_frames)
322 {
323         return pc88event->create_sound(extra_frames);
324 }
325
326 int VM::get_sound_buffer_ptr()
327 {
328         return pc88event->get_sound_buffer_ptr();
329 }
330
331 #ifdef USE_SOUND_VOLUME
332 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
333 {
334         if(ch-- == 0) {
335                 pc88opn->set_volume(0, decibel_l, decibel_r);
336         } else if(ch-- == 0) {
337                 pc88opn->set_volume(1, decibel_l, decibel_r);
338 #ifdef SUPPORT_PC88_OPNA
339         } else if(ch-- == 0) {
340                 pc88opn->set_volume(2, decibel_l, decibel_r);
341         } else if(ch-- == 0) {
342                 pc88opn->set_volume(3, decibel_l, decibel_r);
343 #endif
344 #ifdef SUPPORT_PC88_SB2
345         } else if(ch-- == 0) {
346                 if(pc88sb2 != NULL) {
347                         pc88sb2->set_volume(0, decibel_l, decibel_r);
348                 }
349         } else if(ch-- == 0) {
350                 if(pc88sb2 != NULL) {
351                         pc88sb2->set_volume(1, decibel_l, decibel_r);
352                 }
353         } else if(ch-- == 0) {
354                 if(pc88sb2 != NULL) {
355                         pc88sb2->set_volume(2, decibel_l, decibel_r);
356                 }
357         } else if(ch-- == 0) {
358                 if(pc88sb2 != NULL) {
359                         pc88sb2->set_volume(3, decibel_l, decibel_r);
360                 }
361 #endif
362 #ifdef SUPPORT_PC88_PCG8100
363         } else if(ch-- == 0) {
364                 pc88pcm0->set_volume(0, decibel_l, decibel_r);
365                 pc88pcm1->set_volume(0, decibel_l, decibel_r);
366                 pc88pcm2->set_volume(0, decibel_l, decibel_r);
367 #endif
368         } else if(ch-- == 0) {
369                 pc88pcm->set_volume(0, decibel_l, decibel_r);
370         }
371 }
372 #endif
373
374 // ----------------------------------------------------------------------------
375 // notify key
376 // ----------------------------------------------------------------------------
377
378 void VM::key_down(int code, bool repeat)
379 {
380         pc88->key_down(code, repeat);
381 }
382
383 void VM::key_up(int code)
384 {
385 }
386
387 // ----------------------------------------------------------------------------
388 // user interface
389 // ----------------------------------------------------------------------------
390
391 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
392 {
393         pc88fdc_sub->open_disk(drv, file_path, bank);
394 }
395
396 void VM::close_floppy_disk(int drv)
397 {
398         pc88fdc_sub->close_disk(drv);
399 }
400
401 bool VM::is_floppy_disk_inserted(int drv)
402 {
403         return pc88fdc_sub->is_disk_inserted(drv);
404 }
405
406 void VM::is_floppy_disk_protected(int drv, bool value)
407 {
408         pc88fdc_sub->is_disk_protected(drv, value);
409 }
410
411 bool VM::is_floppy_disk_protected(int drv)
412 {
413         return pc88fdc_sub->is_disk_protected(drv);
414 }
415
416 void VM::play_tape(const _TCHAR* file_path)
417 {
418         pc88->play_tape(file_path);
419 }
420
421 void VM::rec_tape(const _TCHAR* file_path)
422 {
423         pc88->rec_tape(file_path);
424 }
425
426 void VM::close_tape()
427 {
428         pc88->close_tape();
429 }
430
431 bool VM::is_tape_inserted()
432 {
433         return pc88->is_tape_inserted();
434 }
435
436 bool VM::is_frame_skippable()
437 {
438 //      return event->is_frame_skippable();
439         return pc88->is_frame_skippable();
440 }
441
442 void VM::update_config()
443 {
444         if(boot_mode != config.boot_mode) {
445                 // boot mode is changed !!!
446                 boot_mode = config.boot_mode;
447                 reset();
448         } else {
449                 for(DEVICE* device = first_device; device; device = device->next_device) {
450                         device->update_config();
451                 }
452         }
453 }
454
455 #define STATE_VERSION   6
456
457 void VM::save_state(FILEIO* state_fio)
458 {
459         state_fio->FputUint32(STATE_VERSION);
460         
461         for(DEVICE* device = first_device; device; device = device->next_device) {
462                 device->save_state(state_fio);
463         }
464         state_fio->FputInt32(boot_mode);
465 }
466
467 bool VM::load_state(FILEIO* state_fio)
468 {
469         if(state_fio->FgetUint32() != STATE_VERSION) {
470                 return false;
471         }
472         for(DEVICE* device = first_device; device; device = device->next_device) {
473                 if(!device->load_state(state_fio)) {
474                         return false;
475                 }
476         }
477         boot_mode = state_fio->FgetInt32();
478         return true;
479 }
480