OSDN Git Service

b7fee0eff37dfc9acd660189e64cb31d1834d03a
[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 "../upd1990a.h"
20 #include "../ym2203.h"
21 #include "../z80.h"
22
23 #include "../disk.h"
24 #include "../pc80s31k.h"
25 #include "../upd765a.h"
26
27 #ifdef USE_DEBUGGER
28 #include "../debugger.h"
29 #endif
30
31 #ifdef SUPPORT_PC88_PCG8100
32 #include "../i8253.h"
33 #endif
34
35 #include "pc88.h"
36
37 // ----------------------------------------------------------------------------
38 // initialize
39 // ----------------------------------------------------------------------------
40
41 VM::VM(EMU* parent_emu) : emu(parent_emu)
42 {
43         // check configs
44         boot_mode = config.boot_mode;
45         
46         // create devices
47         first_device = last_device = NULL;
48         dummy = new DEVICE(this, emu);  // must be 1st device
49         
50         pc88event = new EVENT(this, emu);
51 //      pc88event->set_frames_per_sec(60);
52 //      pc88event->set_lines_per_frame(260);
53         
54         pc88 = new PC88(this, emu);
55 //      pc88->set_context_event_manager(pc88event);
56         pc88sio = new I8251(this, emu);
57 //      pc88sio->set_context_event_manager(pc88event);
58         pc88pio = new I8255(this, emu);
59 //      pc88pio->set_context_event_manager(pc88event);
60         pc88pcm = new PCM1BIT(this, emu);
61 //      pc88pcm->set_context_event_manager(pc88event);
62         pc88rtc = new UPD1990A(this, emu);
63 //      pc88rtc->set_context_event_manager(pc88event);
64         // config.sound_device_type
65         //      0: 44h:OPNA A4h:None            PC-8801FH/MH or later
66         //      1: 44h:OPN  A4h:None            PC-8801mkIISR/TR/MR/FR
67         //      2: 44h:OPN  A4h:OPNA            PC-8801mkIISR/TR/MR/FR + PC-8801-23
68         pc88opn = new YM2203(this, emu);
69 #ifdef SUPPORT_PC88_OPNA
70         pc88opn->is_ym2608 = (config.sound_device_type == 0);
71 #endif
72 //      pc88opn->set_context_event_manager(pc88event);
73 #ifdef SUPPORT_PC88_SB2
74         if(config.sound_device_type == 2) {
75                 pc88sb2 = new YM2203(this, emu);
76 #ifdef SUPPORT_PC88_OPNA
77                 pc88sb2->is_ym2608 = true;
78 #endif
79 //              pc88sb2->set_context_event_manager(pc88event);
80         } else {
81                 pc88sb2 = NULL;
82         }
83 #endif
84         pc88cpu = new Z80(this, emu);
85 //      pc88cpu->set_context_event_manager(pc88event);
86         dummycpu = new DEVICE(this, emu);
87         
88         pc88sub = new PC80S31K(this, emu);
89 //      pc88sub->set_context_event_manager(pc88event);
90         pc88pio_sub = new I8255(this, emu);
91 //      pc88pio_sub->set_context_event_manager(pc88event);
92         pc88fdc_sub = new UPD765A(this, emu);
93 //      pc88fdc_sub->set_context_event_manager(pc88event);
94         pc88cpu_sub = new Z80(this, emu);
95 //      pc88cpu_sub->set_context_event_manager(pc88event);
96         
97 #ifdef SUPPORT_PC88_PCG8100
98         pc88pit = new I8253(this, emu);
99 //      pc88pit->set_context_event_manager(pc88event);
100         pc88pcm0 = new PCM1BIT(this, emu);
101 //      pc88pcm->set_context_event_manager(pc88event);
102         pc88pcm1 = new PCM1BIT(this, emu);
103 //      pc88pcm->set_context_event_manager(pc88event);
104         pc88pcm2 = new PCM1BIT(this, emu);
105 //      pc88pcm->set_context_event_manager(pc88event);
106 #endif
107         
108 #ifdef SUPPORT_PC88_HIGH_CLOCK
109         pc88event->set_context_cpu(dummycpu, 7987248 / 8);
110         pc88event->set_context_cpu(pc88cpu, (config.cpu_type != 0) ? 3993624 : 7987248);
111 #else
112         pc88event->set_context_cpu(dummycpu, 3993624 / 4);
113         pc88event->set_context_cpu(pc88cpu, 3993624);
114 #endif
115         pc88event->set_context_cpu(pc88cpu_sub, 3993624);
116         pc88event->set_context_sound(pc88opn);
117 #ifdef SUPPORT_PC88_SB2
118         if(pc88sb2 != NULL) {
119                 pc88event->set_context_sound(pc88sb2);
120         }
121 #endif
122         pc88event->set_context_sound(pc88pcm);
123 #ifdef SUPPORT_PC88_PCG8100
124         pc88event->set_context_sound(pc88pcm0);
125         pc88event->set_context_sound(pc88pcm1);
126         pc88event->set_context_sound(pc88pcm2);
127 #endif
128         pc88->set_context_cpu(pc88cpu);
129         pc88->set_context_opn(pc88opn);
130 #ifdef SUPPORT_PC88_SB2
131         pc88->set_context_sb2(pc88sb2);
132 #endif
133         pc88->set_context_pcm(pc88pcm);
134         pc88->set_context_pio(pc88pio);
135         pc88->set_context_rtc(pc88rtc);
136         pc88->set_context_sio(pc88sio);
137 #ifdef SUPPORT_PC88_PCG8100
138         pc88->set_context_pcg_pit(pc88pit);
139         pc88->set_context_pcg_pcm0(pc88pcm0);
140         pc88->set_context_pcg_pcm1(pc88pcm1);
141         pc88->set_context_pcg_pcm2(pc88pcm2);
142 #endif
143         pc88cpu->set_context_mem(pc88);
144         pc88cpu->set_context_io(pc88);
145         pc88cpu->set_context_intr(pc88);
146 #ifdef USE_DEBUGGER
147         pc88cpu->set_context_debugger(new DEBUGGER(this, emu));
148 #endif
149         pc88opn->set_context_irq(pc88, SIG_PC88_SOUND_IRQ, 1);
150 #ifdef SUPPORT_PC88_SB2
151         if(pc88sb2 != NULL) {
152                 pc88sb2->set_context_irq(pc88, SIG_PC88_SB2_IRQ, 1);
153         }
154 #endif
155         pc88sio->set_context_rxrdy(pc88, SIG_PC88_USART_IRQ, 1);
156         pc88sio->set_context_out(pc88, SIG_PC88_USART_OUT);
157         
158         pc88sub->set_context_cpu(pc88cpu_sub);
159         pc88sub->set_context_fdc(pc88fdc_sub);
160         pc88sub->set_context_pio(pc88pio_sub);
161         pc88pio->set_context_port_a(pc88pio_sub, SIG_I8255_PORT_B, 0xff, 0);
162         pc88pio->set_context_port_b(pc88pio_sub, SIG_I8255_PORT_A, 0xff, 0);
163         pc88pio->set_context_port_c(pc88pio_sub, SIG_I8255_PORT_C, 0x0f, 4);
164         pc88pio->set_context_port_c(pc88pio_sub, SIG_I8255_PORT_C, 0xf0, -4);
165         pc88pio->clear_ports_by_cmdreg = true;
166         pc88pio_sub->set_context_port_a(pc88pio, SIG_I8255_PORT_B, 0xff, 0);
167         pc88pio_sub->set_context_port_b(pc88pio, SIG_I8255_PORT_A, 0xff, 0);
168         pc88pio_sub->set_context_port_c(pc88pio, SIG_I8255_PORT_C, 0x0f, 4);
169         pc88pio_sub->set_context_port_c(pc88pio, SIG_I8255_PORT_C, 0xf0, -4);
170         pc88pio_sub->clear_ports_by_cmdreg = true;
171         pc88fdc_sub->set_context_irq(pc88cpu_sub, SIG_CPU_IRQ, 1);
172         pc88cpu_sub->set_context_mem(pc88sub);
173         pc88cpu_sub->set_context_io(pc88sub);
174         pc88cpu_sub->set_context_intr(pc88sub);
175 #ifdef USE_DEBUGGER
176         pc88cpu_sub->set_context_debugger(new DEBUGGER(this, emu));
177 #endif
178         
179 #ifdef SUPPORT_PC88_PCG8100
180         pc88pit->set_context_ch0(pc88pcm0, SIG_PCM1BIT_SIGNAL, 1);
181         pc88pit->set_context_ch1(pc88pcm1, SIG_PCM1BIT_SIGNAL, 1);
182         pc88pit->set_context_ch2(pc88pcm2, SIG_PCM1BIT_SIGNAL, 1);
183         pc88pit->set_constant_clock(0, 3993624);
184         pc88pit->set_constant_clock(1, 3993624);
185         pc88pit->set_constant_clock(2, 3993624);
186 #endif
187         
188         // initialize all devices
189         for(DEVICE* device = first_device; device; device = device->next_device) {
190                 device->initialize();
191         }
192 }
193
194 VM::~VM()
195 {
196         // delete all devices
197         for(DEVICE* device = first_device; device;) {
198                 DEVICE *next_device = device->next_device;
199                 device->release();
200                 delete device;
201                 device = next_device;
202         }
203 }
204
205 DEVICE* VM::get_device(int id)
206 {
207         for(DEVICE* device = first_device; device; device = device->next_device) {
208                 if(device->this_device_id == id) {
209                         return device;
210                 }
211         }
212         return NULL;
213 }
214
215 // ----------------------------------------------------------------------------
216 // drive virtual machine
217 // ----------------------------------------------------------------------------
218
219 void VM::reset()
220 {
221         // reset all devices
222         for(DEVICE* device = first_device; device; device = device->next_device) {
223                 device->reset();
224         }
225         for(DEVICE* device = first_device; device; device = device->next_device) {
226                 device->reset();
227         }
228         
229         // initial device settings
230         pc88opn->SetReg(0x29, 3); // for Misty Blue
231         pc88pio->write_signal(SIG_I8255_PORT_C, 0, 0xff);
232         pc88pio_sub->write_signal(SIG_I8255_PORT_C, 0, 0xff);
233 }
234
235 void VM::run()
236 {
237         pc88event->drive();
238 }
239
240 double VM::frame_rate()
241 {
242         return pc88event->frame_rate();
243 }
244
245 // ----------------------------------------------------------------------------
246 // debugger
247 // ----------------------------------------------------------------------------
248
249 #ifdef USE_DEBUGGER
250 DEVICE *VM::get_cpu(int index)
251 {
252         if(index == 0) {
253                 return pc88cpu;
254         } else if(index == 1) {
255                 return pc88cpu_sub;
256         }
257         return NULL;
258 }
259 #endif
260
261 // ----------------------------------------------------------------------------
262 // draw screen
263 // ----------------------------------------------------------------------------
264
265 void VM::draw_screen()
266 {
267         pc88->draw_screen();
268 }
269
270 int VM::access_lamp()
271 {
272         return pc88fdc_sub->read_signal(0);
273 }
274
275 // ----------------------------------------------------------------------------
276 // soud manager
277 // ----------------------------------------------------------------------------
278
279 void VM::initialize_sound(int rate, int samples)
280 {
281         // init sound manager
282         pc88event->initialize_sound(rate, samples);
283         
284         // init sound gen
285 #ifdef SUPPORT_PC88_OPNA
286         if(pc88opn->is_ym2608) {
287                 pc88opn->init(rate, 7987248, samples, 0, 0);
288         } else
289 #endif
290         pc88opn->init(rate, 3993624, samples, 0, 0);
291 #ifdef SUPPORT_PC88_SB2
292         if(pc88sb2 != NULL) {
293 #ifdef SUPPORT_PC88_OPNA
294                 if(pc88sb2->is_ym2608) {
295                         pc88sb2->init(rate, 7987248, samples, 0, 0);
296                 } else
297 #endif
298                 pc88sb2->init(rate, 3993624, samples, 0, 0);
299         }
300 #endif
301         pc88pcm->init(rate, 8000);
302 #ifdef SUPPORT_PC88_PCG8100
303         pc88pcm0->init(rate, 8000);
304         pc88pcm1->init(rate, 8000);
305         pc88pcm2->init(rate, 8000);
306 #endif
307 }
308
309 uint16* VM::create_sound(int* extra_frames)
310 {
311         return pc88event->create_sound(extra_frames);
312 }
313
314 int VM::sound_buffer_ptr()
315 {
316         return pc88event->sound_buffer_ptr();
317 }
318
319 // ----------------------------------------------------------------------------
320 // notify key
321 // ----------------------------------------------------------------------------
322
323 void VM::key_down(int code, bool repeat)
324 {
325         pc88->key_down(code, repeat);
326 }
327
328 void VM::key_up(int code)
329 {
330 }
331
332 // ----------------------------------------------------------------------------
333 // user interface
334 // ----------------------------------------------------------------------------
335
336 void VM::open_disk(int drv, const _TCHAR* file_path, int bank)
337 {
338         pc88fdc_sub->open_disk(drv, file_path, bank);
339 }
340
341 void VM::close_disk(int drv)
342 {
343         pc88fdc_sub->close_disk(drv);
344 }
345
346 bool VM::disk_inserted(int drv)
347 {
348         return pc88fdc_sub->disk_inserted(drv);
349 }
350
351 void VM::set_disk_protected(int drv, bool value)
352 {
353         pc88fdc_sub->set_disk_protected(drv, value);
354 }
355
356 bool VM::get_disk_protected(int drv)
357 {
358         return pc88fdc_sub->get_disk_protected(drv);
359 }
360
361 void VM::play_tape(const _TCHAR* file_path)
362 {
363         pc88->play_tape(file_path);
364 }
365
366 void VM::rec_tape(const _TCHAR* file_path)
367 {
368         pc88->rec_tape(file_path);
369 }
370
371 void VM::close_tape()
372 {
373         pc88->close_tape();
374 }
375
376 bool VM::tape_inserted()
377 {
378         return pc88->tape_inserted();
379 }
380
381 bool VM::now_skip()
382 {
383 //      return event->now_skip();
384         return pc88->now_skip();
385 }
386
387 void VM::update_config()
388 {
389         int ii;
390         uint32 vol1, vol2;
391 #ifdef USE_MULTIPLE_SOUNDCARDS
392         vol1 = vol2 = (config.sound_device_level[1] + 32768) >> 8; // Card1
393         pc88opn->write_signal(SIG_YM2203_LVOLUME, vol1, 0xffffffff); // OPN: LEFT
394         pc88opn->write_signal(SIG_YM2203_RVOLUME, vol2, 0xffffffff); // OPN: RIGHT
395 # ifdef SUPPORT_PC88_SB2
396         if(pc88sb2 != NULL) {
397                 vol1 = vol2 = (config.sound_device_level[2] + 32768) >> 8; // Card2
398                 pc88sb2->write_signal(SIG_YM2203_LVOLUME, vol1, 0xffffffff); // OPN: LEFT
399                 pc88sb2->write_signal(SIG_YM2203_RVOLUME, vol2, 0xffffffff); // OPN: RIGHT
400         }
401 # endif
402 #endif
403         if(boot_mode != config.boot_mode) {
404                 // boot mode is changed !!!
405                 boot_mode = config.boot_mode;
406                 reset();
407         } else {
408                 for(DEVICE* device = first_device; device; device = device->next_device) {
409                         device->update_config();
410                 }
411         }
412 }
413
414 #define STATE_VERSION   4
415
416 void VM::save_state(FILEIO* state_fio)
417 {
418         state_fio->FputUint32(STATE_VERSION);
419         
420         for(DEVICE* device = first_device; device; device = device->next_device) {
421                 device->save_state(state_fio);
422         }
423         state_fio->FputInt32(boot_mode);
424 }
425
426 bool VM::load_state(FILEIO* state_fio)
427 {
428         if(state_fio->FgetUint32() != STATE_VERSION) {
429                 return false;
430         }
431         for(DEVICE* device = first_device; device; device = device->next_device) {
432                 if(!device->load_state(state_fio)) {
433                         return false;
434                 }
435         }
436         boot_mode = state_fio->FgetInt32();
437         return true;
438 }
439