OSDN Git Service

[VM][GENERAL] MErge upstream : 2015-06-28.
[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         pc88opn = new YM2203(this, emu);
65 #ifdef SUPPORT_PC88_OPNA
66         pc88opn->is_ym2608 = (config.sound_device_type == 0);
67 #endif
68 //      pc88opn->set_context_event_manager(pc88event);
69         pc88cpu = new Z80(this, emu);
70 //      pc88cpu->set_context_event_manager(pc88event);
71         dummycpu = new DEVICE(this, emu);
72         
73         pc88sub = new PC80S31K(this, emu);
74 //      pc88sub->set_context_event_manager(pc88event);
75         pc88pio_sub = new I8255(this, emu);
76 //      pc88pio_sub->set_context_event_manager(pc88event);
77         pc88fdc_sub = new UPD765A(this, emu);
78 //      pc88fdc_sub->set_context_event_manager(pc88event);
79         pc88cpu_sub = new Z80(this, emu);
80 //      pc88cpu_sub->set_context_event_manager(pc88event);
81         
82 #ifdef SUPPORT_PC88_PCG8100
83         pc88pit = new I8253(this, emu);
84 //      pc88pit->set_context_event_manager(pc88event);
85         pc88pcm0 = new PCM1BIT(this, emu);
86 //      pc88pcm->set_context_event_manager(pc88event);
87         pc88pcm1 = new PCM1BIT(this, emu);
88 //      pc88pcm->set_context_event_manager(pc88event);
89         pc88pcm2 = new PCM1BIT(this, emu);
90 //      pc88pcm->set_context_event_manager(pc88event);
91 #endif
92         
93 #ifdef SUPPORT_PC88_HIGH_CLOCK
94         pc88event->set_context_cpu(dummycpu, 7981350);
95         pc88event->set_context_cpu(pc88cpu, (config.cpu_type != 0) ? 3993624 : 7981350);        // XM8 version 1.00
96         //pc88event->set_context_cpu(pc88cpu, (config.cpu_type != 0) ? 3993624 : 7987248);
97 #else
98         pc88event->set_context_cpu(dummycpu, 3993624);
99         pc88event->set_context_cpu(pc88cpu, 3993624);
100 #endif
101         pc88event->set_context_cpu(pc88cpu_sub, 3993624);
102         pc88event->set_context_sound(pc88opn);
103         pc88event->set_context_sound(pc88pcm);
104 #ifdef SUPPORT_PC88_PCG8100
105         pc88event->set_context_sound(pc88pcm0);
106         pc88event->set_context_sound(pc88pcm1);
107         pc88event->set_context_sound(pc88pcm2);
108 #endif
109 #ifdef DATAREC_SOUND
110         pc88event->set_context_sound(pc88);
111 #endif  
112         pc88->set_context_cpu(pc88cpu);
113         pc88->set_context_opn(pc88opn);
114         pc88->set_context_pcm(pc88pcm);
115         pc88->set_context_pio(pc88pio);
116         pc88->set_context_rtc(pc88rtc);
117         pc88->set_context_sio(pc88sio);
118 #ifdef SUPPORT_PC88_PCG8100
119         pc88->set_context_pcg_pit(pc88pit);
120         pc88->set_context_pcg_pcm0(pc88pcm0);
121         pc88->set_context_pcg_pcm1(pc88pcm1);
122         pc88->set_context_pcg_pcm2(pc88pcm2);
123 #endif
124         pc88cpu->set_context_mem(pc88);
125         pc88cpu->set_context_io(pc88);
126         pc88cpu->set_context_intr(pc88);
127 #ifdef USE_DEBUGGER
128         pc88cpu->set_context_debugger(new DEBUGGER(this, emu));
129 #endif
130         pc88opn->set_context_irq(pc88, SIG_PC88_SOUND_IRQ, 1);
131         pc88sio->set_context_rxrdy(pc88, SIG_PC88_USART_IRQ, 1);
132         pc88sio->set_context_out(pc88, SIG_PC88_USART_OUT);
133         
134         pc88sub->set_context_cpu(pc88cpu_sub);
135         pc88sub->set_context_fdc(pc88fdc_sub);
136         pc88sub->set_context_pio(pc88pio_sub);
137         pc88pio->set_context_port_a(pc88pio_sub, SIG_I8255_PORT_B, 0xff, 0);
138         pc88pio->set_context_port_b(pc88pio_sub, SIG_I8255_PORT_A, 0xff, 0);
139         pc88pio->set_context_port_c(pc88pio_sub, SIG_I8255_PORT_C, 0x0f, 4);
140         pc88pio->set_context_port_c(pc88pio_sub, SIG_I8255_PORT_C, 0xf0, -4);
141         pc88pio->clear_ports_by_cmdreg = true;
142         pc88pio_sub->set_context_port_a(pc88pio, SIG_I8255_PORT_B, 0xff, 0);
143         pc88pio_sub->set_context_port_b(pc88pio, SIG_I8255_PORT_A, 0xff, 0);
144         pc88pio_sub->set_context_port_c(pc88pio, SIG_I8255_PORT_C, 0x0f, 4);
145         pc88pio_sub->set_context_port_c(pc88pio, SIG_I8255_PORT_C, 0xf0, -4);
146         pc88pio_sub->clear_ports_by_cmdreg = true;
147         pc88fdc_sub->set_context_irq(pc88cpu_sub, SIG_CPU_IRQ, 1);
148         pc88cpu_sub->set_context_mem(pc88sub);
149         pc88cpu_sub->set_context_io(pc88sub);
150         pc88cpu_sub->set_context_intr(pc88sub);
151 #ifdef USE_DEBUGGER
152         pc88cpu_sub->set_context_debugger(new DEBUGGER(this, emu));
153 #endif
154         
155 #ifdef SUPPORT_PC88_PCG8100
156         pc88pit->set_context_ch0(pc88pcm0, SIG_PCM1BIT_SIGNAL, 1);
157         pc88pit->set_context_ch1(pc88pcm1, SIG_PCM1BIT_SIGNAL, 1);
158         pc88pit->set_context_ch2(pc88pcm2, SIG_PCM1BIT_SIGNAL, 1);
159         pc88pit->set_constant_clock(0, 3993624);
160         pc88pit->set_constant_clock(1, 3993624);
161         pc88pit->set_constant_clock(2, 3993624);
162 #endif
163         
164         // initialize all devices
165         for(DEVICE* device = first_device; device; device = device->next_device) {
166                 device->initialize();
167         }
168 }
169
170 VM::~VM()
171 {
172         // delete all devices
173         for(DEVICE* device = first_device; device;) {
174                 DEVICE *next_device = device->next_device;
175                 device->release();
176                 delete device;
177                 device = next_device;
178         }
179 }
180
181 DEVICE* VM::get_device(int id)
182 {
183         for(DEVICE* device = first_device; device; device = device->next_device) {
184                 if(device->this_device_id == id) {
185                         return device;
186                 }
187         }
188         return NULL;
189 }
190
191 // ----------------------------------------------------------------------------
192 // drive virtual machine
193 // ----------------------------------------------------------------------------
194
195 void VM::reset()
196 {
197         // reset all devices
198         for(DEVICE* device = first_device; device; device = device->next_device) {
199                 device->reset();
200         }
201         for(DEVICE* device = first_device; device; device = device->next_device) {
202                 device->reset();
203         }
204         
205         // initial device settings
206         pc88opn->SetReg(0x29, 3); // for Misty Blue
207         pc88pio->write_signal(SIG_I8255_PORT_C, 0, 0xff);
208         pc88pio_sub->write_signal(SIG_I8255_PORT_C, 0, 0xff);
209 }
210
211 void VM::run()
212 {
213         pc88event->drive();
214 }
215
216 double VM::frame_rate()
217 {
218         return pc88event->frame_rate();
219 }
220
221 // ----------------------------------------------------------------------------
222 // debugger
223 // ----------------------------------------------------------------------------
224
225 #ifdef USE_DEBUGGER
226 DEVICE *VM::get_cpu(int index)
227 {
228         if(index == 0) {
229                 return pc88cpu;
230         } else if(index == 1) {
231                 return pc88cpu_sub;
232         }
233         return NULL;
234 }
235 #endif
236
237 // ----------------------------------------------------------------------------
238 // draw screen
239 // ----------------------------------------------------------------------------
240
241 void VM::draw_screen()
242 {
243         pc88->draw_screen();
244 }
245
246 int VM::access_lamp()
247 {
248         return pc88fdc_sub->read_signal(0);
249 }
250
251 // ----------------------------------------------------------------------------
252 // soud manager
253 // ----------------------------------------------------------------------------
254
255 void VM::initialize_sound(int rate, int samples)
256 {
257         // init sound manager
258         pc88event->initialize_sound(rate, samples);
259         
260         // init sound gen
261 #ifdef SUPPORT_PC88_OPNA
262         if(pc88opn->is_ym2608) {
263                 pc88opn->init(rate, 7987248, samples, 0, 0);
264         } else
265 #endif
266         pc88opn->init(rate, 3993624, samples, 0, 0);
267         pc88pcm->init(rate, 8000);
268 #ifdef SUPPORT_PC88_PCG8100
269         pc88pcm0->init(rate, 8000);
270         pc88pcm1->init(rate, 8000);
271         pc88pcm2->init(rate, 8000);
272 #endif
273 }
274
275 uint16* VM::create_sound(int* extra_frames)
276 {
277         return pc88event->create_sound(extra_frames);
278 }
279
280 int VM::sound_buffer_ptr()
281 {
282         return pc88event->sound_buffer_ptr();
283 }
284
285 // ----------------------------------------------------------------------------
286 // notify key
287 // ----------------------------------------------------------------------------
288
289 void VM::key_down(int code, bool repeat)
290 {
291         pc88->key_down(code, repeat);
292 }
293
294 void VM::key_up(int code)
295 {
296 }
297
298 // ----------------------------------------------------------------------------
299 // user interface
300 // ----------------------------------------------------------------------------
301
302 void VM::open_disk(int drv, _TCHAR* file_path, int bank)
303 {
304         pc88fdc_sub->open_disk(drv, file_path, bank);
305 }
306
307 void VM::close_disk(int drv)
308 {
309         pc88fdc_sub->close_disk(drv);
310 }
311
312 bool VM::disk_inserted(int drv)
313 {
314         return pc88fdc_sub->disk_inserted(drv);
315 }
316
317 #if defined(USE_DISK_WRITE_PROTECT)
318 void VM::write_protect_fd(int drv, bool flag)
319 {
320         pc88fdc_sub->write_protect_fd(drv, flag);
321 }
322
323 bool VM::is_write_protect_fd(int drv)
324 {
325         return pc88fdc_sub->is_write_protect_fd(drv);
326 }
327 #endif
328
329 void VM::play_tape(_TCHAR* file_path)
330 {
331         pc88->play_tape(file_path);
332 }
333
334 void VM::rec_tape(_TCHAR* file_path)
335 {
336         pc88->rec_tape(file_path);
337 }
338
339 void VM::close_tape()
340 {
341         pc88->close_tape();
342 }
343
344 bool VM::tape_inserted()
345 {
346         return pc88->tape_inserted();
347 }
348
349 #if defined(USE_TAPE_PTR)
350 int VM::get_tape_ptr()
351 {
352         return pc88->get_tape_ptr();
353 }
354 #endif
355
356 bool VM::now_skip()
357 {
358 //      return event->now_skip();
359         return pc88->now_skip();
360 }
361
362 void VM::update_config()
363 {
364         if(boot_mode != config.boot_mode) {
365                 // boot mode is changed !!!
366                 boot_mode = config.boot_mode;
367                 reset();
368         } else {
369                 for(DEVICE* device = first_device; device; device = device->next_device) {
370                         device->update_config();
371                 }
372         }
373 #ifdef SUPPORT_PC88_HIGH_CLOCK
374         pc88event->set_cpu_clock(pc88cpu, (config.cpu_type != 0) ? 3993624 : 7987248);
375 #endif
376    
377 }
378
379 #define STATE_VERSION   3
380
381 void VM::save_state(FILEIO* state_fio)
382 {
383         state_fio->FputUint32(STATE_VERSION);
384         
385         for(DEVICE* device = first_device; device; device = device->next_device) {
386                 device->save_state(state_fio);
387         }
388         state_fio->FputInt32(boot_mode);
389 }
390
391 bool VM::load_state(FILEIO* state_fio)
392 {
393         if(state_fio->FgetUint32() != STATE_VERSION) {
394                 return false;
395         }
396         for(DEVICE* device = first_device; device; device = device->next_device) {
397                 if(!device->load_state(state_fio)) {
398                         return false;
399                 }
400         }
401         boot_mode = state_fio->FgetInt32();
402         return true;
403 }
404