OSDN Git Service

[VM][General] Merge upstream 2016-03-01. (Pahse 1).
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz2500 / mz2500.cpp
1 /*
2         SHARP MZ-2500 Emulator 'EmuZ-2500'
3
4         Author : Takeda.Toshiya
5         Date   : 2006.11.24 -
6
7         [ virtual machine ]
8 */
9
10 #include "mz2500.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../disk.h"
17 #include "../i8253.h"
18 #include "../i8255.h"
19 #include "../io.h"
20 #include "../mb8877.h"
21 #include "../mz1p17.h"
22 #include "../pcm1bit.h"
23 //#include "../pcpr201.h"
24 #include "../prnfile.h"
25 #include "../rp5c01.h"
26 #include "../w3100a.h"
27 #include "../ym2203.h"
28 #include "../z80.h"
29 #include "../z80pio.h"
30 #include "../z80sio.h"
31
32 #ifdef USE_DEBUGGER
33 #include "../debugger.h"
34 #endif
35
36 #include "calendar.h"
37 #include "cmt.h"
38 #include "crtc.h"
39 #include "floppy.h"
40 #include "interrupt.h"
41 #include "joystick.h"
42 #include "keyboard.h"
43 #include "memory.h"
44 #include "mouse.h"
45 #include "mz1e26.h"
46 #include "mz1e30.h"
47 #include "mz1r13.h"
48 #include "mz1r37.h"
49 #include "printer.h"
50 #include "serial.h"
51 #include "timer.h"
52
53 // ----------------------------------------------------------------------------
54 // initialize
55 // ----------------------------------------------------------------------------
56
57 VM::VM(EMU* parent_emu) : emu(parent_emu)
58 {
59         // create devices
60         first_device = last_device = NULL;
61         dummy = new DEVICE(this, emu);  // must be 1st device
62         event = new EVENT(this, emu);   // must be 2nd device
63         
64         drec = new DATAREC(this, emu);
65         pit = new I8253(this, emu);
66         pio_i = new I8255(this, emu);
67         io = new IO(this, emu);
68         fdc = new MB8877(this, emu);
69         pcm = new PCM1BIT(this, emu);
70         rtc = new RP5C01(this, emu);    // RP-5C15
71         w3100a = new W3100A(this, emu);
72         opn = new YM2203(this, emu);
73         cpu = new Z80(this, emu);
74         pio = new Z80PIO(this, emu);
75         sio = new Z80SIO(this, emu);
76         
77         calendar = new CALENDAR(this, emu);
78         cmt = new CMT(this, emu);
79         crtc = new CRTC(this, emu);
80         floppy = new FLOPPY(this, emu);
81         interrupt = new INTERRUPT(this, emu);
82         joystick = new JOYSTICK(this, emu);
83         keyboard = new KEYBOARD(this, emu);
84         memory = new MEMORY(this, emu);
85         mouse = new MOUSE(this, emu);
86         mz1e26 = new MZ1E26(this, emu);
87         mz1e30 = new MZ1E30(this, emu);
88         mz1r13 = new MZ1R13(this, emu);
89         mz1r37 = new MZ1R37(this, emu);
90         printer = new PRINTER(this, emu);
91         serial = new SERIAL(this, emu);
92         timer = new TIMER(this, emu);
93         
94         // set contexts
95         event->set_context_cpu(cpu);
96         event->set_context_sound(opn);
97         event->set_context_sound(pcm);
98         event->set_context_sound(drec);
99         
100         drec->set_context_ear(cmt, SIG_CMT_OUT, 1);
101         drec->set_context_remote(cmt, SIG_CMT_REMOTE, 1);
102         drec->set_context_end(cmt, SIG_CMT_END, 1);
103         drec->set_context_top(cmt, SIG_CMT_TOP, 1);
104         
105         pit->set_context_ch0(interrupt, SIG_INTERRUPT_I8253, 1);
106         pit->set_context_ch0(pit, SIG_I8253_CLOCK_1, 1);
107         pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);
108         pit->set_constant_clock(0, 31250);
109         pio_i->set_context_port_a(cmt, SIG_CMT_PIO_PA, 0xff, 0);
110         pio_i->set_context_port_c(cmt, SIG_CMT_PIO_PC, 0xff, 0);
111         pio_i->set_context_port_c(crtc, SIG_CRTC_MASK, 0x01, 0);
112         pio_i->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x04, 0);
113         rtc->set_context_alarm(interrupt, SIG_INTERRUPT_RP5C15, 1);
114         rtc->set_context_pulse(opn, SIG_YM2203_PORT_B, 8);
115         opn->set_context_port_a(floppy, SIG_FLOPPY_REVERSE, 0x02, 0);
116         opn->set_context_port_a(crtc, SIG_CRTC_PALLETE, 0x04, 0);
117         opn->set_context_port_a(mouse, SIG_MOUSE_SEL, 0x08, 0);
118         pio->set_context_port_a(crtc, SIG_CRTC_COLUMN_SIZE, 0x20, 0);
119         pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x1f, 0);
120         sio->set_context_dtr(1, mouse, SIG_MOUSE_DTR, 1);
121         
122         calendar->set_context_rtc(rtc);
123         cmt->set_context_pio(pio_i);
124         cmt->set_context_drec(drec);
125         crtc->set_context_mem(memory);
126         crtc->set_context_int(interrupt);
127         crtc->set_context_pio(pio_i);
128         crtc->set_vram_ptr(memory->get_vram());
129         crtc->set_tvram_ptr(memory->get_tvram());
130         crtc->set_kanji_ptr(memory->get_kanji());
131         crtc->set_pcg_ptr(memory->get_pcg());
132         floppy->set_context_fdc(fdc);
133         keyboard->set_context_pio_i(pio_i);
134         keyboard->set_context_pio(pio);
135         memory->set_context_cpu(cpu);
136         memory->set_context_crtc(crtc);
137         mouse->set_context_sio(sio);
138         if(config.printer_device_type == 0) {  
139                 printer->set_context_prn(new PRNFILE(this, emu));
140         } else if(config.printer_device_type == 1) {
141                 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
142                 mz1p17->mode = MZ1P17_MODE_MZ1;
143                 printer->set_context_prn(mz1p17);
144 //      } else if(config.printer_device_type == 2) {
145 //              printer->set_context_prn(new PCPR201(this, emu));
146         } else {
147                 printer->set_context_prn(dummy);
148         }
149         serial->set_context_sio(sio);
150         timer->set_context_pit(pit);
151         
152         // cpu bus
153         cpu->set_context_mem(memory);
154         cpu->set_context_io(io);
155         cpu->set_context_intr(pio);
156 #ifdef USE_DEBUGGER
157         cpu->set_context_debugger(new DEBUGGER(this, emu));
158 #endif
159         
160         // z80 family daisy chain
161         pio->set_context_intr(cpu, 0);
162         pio->set_context_child(sio);
163         sio->set_context_intr(cpu, 1);
164         sio->set_context_child(interrupt);
165         interrupt->set_context_intr(cpu, 2);
166         
167         // i/o bus
168         io->set_iomap_range_rw(0x60, 0x63, w3100a);
169         io->set_iomap_range_rw(0xa0, 0xa3, serial);
170         io->set_iomap_range_rw(0xa4, 0xa5, mz1e30);
171         io->set_iomap_range_rw(0xa8, 0xa9, mz1e30);
172         io->set_iomap_range_rw(0xac, 0xad, mz1r37);
173         io->set_iomap_single_w(0xae, crtc);
174         io->set_iomap_range_rw(0xb0, 0xb3, serial);
175         io->set_iomap_range_rw(0xb4, 0xb5, memory);
176         io->set_iomap_range_rw(0xb8, 0xb9, mz1r13);
177         io->set_iomap_range_rw(0xbc, 0xbf, crtc);
178         io->set_iomap_range_w(0xc6, 0xc7, interrupt);
179         io->set_iomap_range_rw(0xc8, 0xc9, opn);
180         io->set_iomap_single_rw(0xca, mz1e26);
181         io->set_iomap_single_rw(0xcc, calendar);
182         io->set_iomap_single_w(0xcd, serial);
183         io->set_iomap_range_w(0xce, 0xcf, memory);
184         io->set_iomap_range_rw(0xd8, 0xdb, fdc);
185         io->set_iomap_range_w(0xdc, 0xdd, floppy);
186         io->set_iomap_range_rw(0xe0, 0xe3, pio_i);
187         io->set_iomap_range_rw(0xe4, 0xe7, pit);
188         io->set_iomap_range_rw(0xe8, 0xeb, pio);
189         io->set_iomap_single_rw(0xef, joystick);
190         io->set_iomap_range_w(0xf0, 0xf3, timer);
191         io->set_iomap_range_rw(0xf4, 0xf7, crtc);
192         io->set_iomap_range_rw(0xfe, 0xff, printer);
193         
194         io->set_iowait_range_rw(0xc8, 0xc9, 1);
195         io->set_iowait_single_rw(0xcc, 3);
196         io->set_iowait_range_rw(0xd8, 0xdf, 1);
197         io->set_iowait_range_rw(0xe8, 0xeb, 1);
198         
199         // initialize all devices
200         for(DEVICE* device = first_device; device; device = device->next_device) {
201                 device->initialize();
202         }
203         for(int i = 0; i < MAX_DRIVE; i++) {
204                 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
205         }
206         monitor_type = config.monitor_type;
207 }
208
209 VM::~VM()
210 {
211         // delete all devices
212         for(DEVICE* device = first_device; device;) {
213                 DEVICE *next_device = device->next_device;
214                 device->release();
215                 delete device;
216                 device = next_device;
217         }
218 }
219
220 DEVICE* VM::get_device(int id)
221 {
222         for(DEVICE* device = first_device; device; device = device->next_device) {
223                 if(device->this_device_id == id) {
224                         return device;
225                 }
226         }
227         return NULL;
228 }
229
230 // ----------------------------------------------------------------------------
231 // drive virtual machine
232 // ----------------------------------------------------------------------------
233
234 void VM::reset()
235 {
236         // reset all devices
237         for(DEVICE* device = first_device; device; device = device->next_device) {
238                 device->reset();
239         }
240         
241         // set initial port status
242         opn->write_signal(SIG_YM2203_PORT_B, (monitor_type & 2) ? 0x77 : 0x37, 0xff);
243 }
244
245 void VM::special_reset()
246 {
247         // reset all devices
248 //      for(DEVICE* device = first_device; device; device = device->next_device) {
249 //              device->special_reset();
250 //      }
251         memory->special_reset();
252         cpu->reset();
253 }
254
255 void VM::run()
256 {
257         event->drive();
258 }
259
260 double VM::get_frame_rate()
261 {
262         return event->get_frame_rate();
263 }
264
265 // ----------------------------------------------------------------------------
266 // debugger
267 // ----------------------------------------------------------------------------
268
269 #ifdef USE_DEBUGGER
270 DEVICE *VM::get_cpu(int index)
271 {
272         if(index == 0) {
273                 return cpu;
274         }
275         return NULL;
276 }
277 #endif
278
279 // ----------------------------------------------------------------------------
280 // draw screen
281 // ----------------------------------------------------------------------------
282
283 void VM::draw_screen()
284 {
285         crtc->draw_screen();
286 }
287
288 uint32_t VM::get_access_lamp_status()
289 {
290         uint32_t status = fdc->read_signal(0) | mz1e30->read_signal(0);
291         return (status & 0x30) ? 4 : (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
292 }
293
294 // ----------------------------------------------------------------------------
295 // soud manager
296 // ----------------------------------------------------------------------------
297
298 void VM::initialize_sound(int rate, int samples)
299 {
300         // init sound manager
301         event->initialize_sound(rate, samples);
302         
303         // init sound gen
304         opn->initialize_sound(rate, 2000000, samples, 0, -8);
305         pcm->initialize_sound(rate, 4096);
306 }
307
308 uint16_t* VM::create_sound(int* extra_frames)
309 {
310         return event->create_sound(extra_frames);
311 }
312
313 int VM::get_sound_buffer_ptr()
314 {
315         return event->get_sound_buffer_ptr();
316 }
317
318 #ifdef USE_SOUND_VOLUME
319 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
320 {
321         if(ch == 0) {
322                 opn->set_volume(0, decibel_l, decibel_r);
323         } else if(ch == 1) {
324                 opn->set_volume(1, decibel_l, decibel_r);
325         } else if(ch == 2) {
326                 pcm->set_volume(0, decibel_l, decibel_r);
327         } else if(ch == 3) {
328                 drec->set_volume(0, decibel_l, decibel_r);
329         } else if(ch == 4) {
330                 drec->set_volume(1, decibel_l, decibel_r);
331         }
332 }
333 #endif
334
335 // ----------------------------------------------------------------------------
336 // socket
337 // ----------------------------------------------------------------------------
338
339 void VM::notify_socket_connected(int ch)
340 {
341         w3100a->notify_connected(ch);
342 }
343
344 void VM::notify_socket_disconnected(int ch)
345 {
346         w3100a->notify_disconnected(ch);
347 }
348
349 uint8_t* VM::get_socket_send_buffer(int ch, int* size)
350 {
351         return w3100a->get_send_buffer(ch, size);
352 }
353
354 void VM::inc_socket_send_buffer_ptr(int ch, int size)
355 {
356         w3100a->inc_send_buffer_ptr(ch, size);
357 }
358
359 uint8_t* VM::get_socket_recv_buffer0(int ch, int* size0, int* size1)
360 {
361         return w3100a->get_recv_buffer0(ch, size0, size1);
362 }
363
364 uint8_t* VM::get_socket_recv_buffer1(int ch)
365 {
366         return w3100a->get_recv_buffer1(ch);
367 }
368
369 void VM::inc_socket_recv_buffer_ptr(int ch, int size)
370 {
371         w3100a->inc_recv_buffer_ptr(ch, size);
372 }
373
374 // ----------------------------------------------------------------------------
375 // user interface
376 // ----------------------------------------------------------------------------
377
378 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
379 {
380         fdc->open_disk(drv, file_path, bank);
381 }
382
383 void VM::close_floppy_disk(int drv)
384 {
385         fdc->close_disk(drv);
386 }
387
388 bool VM::is_floppy_disk_inserted(int drv)
389 {
390         return fdc->is_disk_inserted(drv);
391 }
392
393 void VM::is_floppy_disk_protected(int drv, bool value)
394 {
395         fdc->is_disk_protected(drv, value);
396 }
397
398 bool VM::is_floppy_disk_protected(int drv)
399 {
400         return fdc->is_disk_protected(drv);
401 }
402
403 void VM::play_tape(const _TCHAR* file_path)
404 {
405         bool value = drec->play_tape(file_path);
406         cmt->close_tape();
407         cmt->play_tape(value);
408 }
409
410 void VM::rec_tape(const _TCHAR* file_path)
411 {
412         bool value = drec->rec_tape(file_path);
413         cmt->close_tape();
414         cmt->rec_tape(value);
415 }
416
417 void VM::close_tape()
418 {
419         drec->close_tape();
420         cmt->close_tape();
421 }
422
423 bool VM::is_tape_inserted()
424 {
425         return drec->is_tape_inserted();
426 }
427
428 bool VM::is_tape_playing()
429 {
430         return drec->is_tape_playing();
431 }
432
433 bool VM::is_tape_recording()
434 {
435         return drec->is_tape_recording();
436 }
437
438 int VM::get_tape_position()
439 {
440         return drec->get_tape_position();
441 }
442
443 void VM::push_play()
444 {
445         drec->set_ff_rew(0);
446         drec->set_remote(true);
447 }
448
449 void VM::push_stop()
450 {
451         drec->set_remote(false);
452 }
453
454 void VM::push_fast_forward()
455 {
456         drec->set_ff_rew(1);
457         drec->set_remote(true);
458 }
459
460 void VM::push_fast_rewind()
461 {
462         drec->set_ff_rew(-1);
463         drec->set_remote(true);
464 }
465
466
467 bool VM::is_frame_skippable()
468 {
469         return event->is_frame_skippable();
470 }
471
472 void VM::update_config()
473 {
474         for(DEVICE* device = first_device; device; device = device->next_device) {
475                 device->update_config();
476         }
477 }
478
479 #define STATE_VERSION   4
480
481 void VM::save_state(FILEIO* state_fio)
482 {
483         state_fio->FputUint32(STATE_VERSION);
484         
485         for(DEVICE* device = first_device; device; device = device->next_device) {
486                 device->save_state(state_fio);
487         }
488         state_fio->FputInt32(monitor_type);
489 }
490
491 bool VM::load_state(FILEIO* state_fio)
492 {
493         if(state_fio->FgetUint32() != STATE_VERSION) {
494                 return false;
495         }
496         for(DEVICE* device = first_device; device; device = device->next_device) {
497                 if(!device->load_state(state_fio)) {
498                         return false;
499                 }
500         }
501         monitor_type = state_fio->FgetInt32();
502         return true;
503 }
504