OSDN Git Service

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