2 SHARP MZ-2500 Emulator 'EmuZ-2500'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
15 #include "../datarec.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"
29 #include "../z80pio.h"
30 #include "../z80sio.h"
33 #include "../debugger.h"
40 #include "interrupt.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 VM::VM(EMU* parent_emu) : emu(parent_emu)
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
64 dummy->set_device_name(_T("1st Dummy"));
65 event->set_device_name(_T("EVENT"));
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);
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)"));
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);
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"));
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);
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"));
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);
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);
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));
179 printer->set_context_prn(dummy);
181 serial->set_context_sio(sio);
182 timer->set_context_pit(pit);
185 cpu->set_context_mem(memory);
186 cpu->set_context_io(io);
187 cpu->set_context_intr(pio);
189 cpu->set_context_debugger(new DEBUGGER(this, emu));
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);
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);
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);
231 // initialize all devices
232 for(DEVICE* device = first_device; device; device = device->next_device) {
233 device->initialize();
235 for(int i = 0; i < MAX_DRIVE; i++) {
236 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
238 monitor_type = config.monitor_type;
243 // delete all devices
244 for(DEVICE* device = first_device; device;) {
245 DEVICE *next_device = device->next_device;
248 device = next_device;
252 DEVICE* VM::get_device(int id)
254 for(DEVICE* device = first_device; device; device = device->next_device) {
255 if(device->this_device_id == id) {
262 // ----------------------------------------------------------------------------
263 // drive virtual machine
264 // ----------------------------------------------------------------------------
269 for(DEVICE* device = first_device; device; device = device->next_device) {
273 // set initial port status
274 opn->write_signal(SIG_YM2203_PORT_B, (monitor_type & 2) ? 0x77 : 0x37, 0xff);
277 void VM::special_reset()
280 // for(DEVICE* device = first_device; device; device = device->next_device) {
281 // device->special_reset();
283 memory->special_reset();
292 double VM::get_frame_rate()
294 return event->get_frame_rate();
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
302 DEVICE *VM::get_cpu(int index)
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 void VM::draw_screen()
320 uint32_t VM::get_access_lamp_status()
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;
326 // ----------------------------------------------------------------------------
328 // ----------------------------------------------------------------------------
330 void VM::initialize_sound(int rate, int samples)
332 // init sound manager
333 event->initialize_sound(rate, samples);
336 opn->initialize_sound(rate, 2000000, samples, 0, -8);
337 pcm->initialize_sound(rate, 4096);
340 uint16_t* VM::create_sound(int* extra_frames)
342 return event->create_sound(extra_frames);
345 int VM::get_sound_buffer_ptr()
347 return event->get_sound_buffer_ptr();
350 #ifdef USE_SOUND_VOLUME
351 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
354 opn->set_volume(0, decibel_l, decibel_r);
356 opn->set_volume(1, decibel_l, decibel_r);
358 pcm->set_volume(0, decibel_l, decibel_r);
360 drec->set_volume(0, decibel_l, decibel_r);
362 drec->set_volume(1, decibel_l, decibel_r);
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
371 void VM::notify_socket_connected(int ch)
373 w3100a->notify_connected(ch);
376 void VM::notify_socket_disconnected(int ch)
378 w3100a->notify_disconnected(ch);
381 uint8_t* VM::get_socket_send_buffer(int ch, int* size)
383 return w3100a->get_send_buffer(ch, size);
386 void VM::inc_socket_send_buffer_ptr(int ch, int size)
388 w3100a->inc_send_buffer_ptr(ch, size);
391 uint8_t* VM::get_socket_recv_buffer0(int ch, int* size0, int* size1)
393 return w3100a->get_recv_buffer0(ch, size0, size1);
396 uint8_t* VM::get_socket_recv_buffer1(int ch)
398 return w3100a->get_recv_buffer1(ch);
401 void VM::inc_socket_recv_buffer_ptr(int ch, int size)
403 w3100a->inc_recv_buffer_ptr(ch, size);
406 // ----------------------------------------------------------------------------
408 // ----------------------------------------------------------------------------
410 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
412 fdc->open_disk(drv, file_path, bank);
415 void VM::close_floppy_disk(int drv)
417 fdc->close_disk(drv);
420 bool VM::is_floppy_disk_inserted(int drv)
422 return fdc->is_disk_inserted(drv);
425 void VM::is_floppy_disk_protected(int drv, bool value)
427 fdc->is_disk_protected(drv, value);
430 bool VM::is_floppy_disk_protected(int drv)
432 return fdc->is_disk_protected(drv);
435 void VM::play_tape(const _TCHAR* file_path)
437 bool value = drec->play_tape(file_path);
439 cmt->play_tape(value);
442 void VM::rec_tape(const _TCHAR* file_path)
444 bool value = drec->rec_tape(file_path);
446 cmt->rec_tape(value);
449 void VM::close_tape()
457 bool VM::is_tape_inserted()
459 return drec->is_tape_inserted();
462 bool VM::is_tape_playing()
464 return drec->is_tape_playing();
467 bool VM::is_tape_recording()
469 return drec->is_tape_recording();
472 int VM::get_tape_position()
474 return drec->get_tape_position();
480 drec->set_remote(true);
485 drec->set_remote(false);
488 void VM::push_fast_forward()
491 drec->set_remote(true);
494 void VM::push_fast_rewind()
496 drec->set_ff_rew(-1);
497 drec->set_remote(true);
500 bool VM::is_frame_skippable()
502 return event->is_frame_skippable();
505 void VM::update_config()
507 for(DEVICE* device = first_device; device; device = device->next_device) {
508 device->update_config();
512 #define STATE_VERSION 4
514 void VM::save_state(FILEIO* state_fio)
516 state_fio->FputUint32(STATE_VERSION);
518 for(DEVICE* device = first_device; device; device = device->next_device) {
519 device->save_state(state_fio);
521 state_fio->FputInt32(monitor_type);
524 bool VM::load_state(FILEIO* state_fio)
526 if(state_fio->FgetUint32() != STATE_VERSION) {
529 for(DEVICE* device = first_device; device; device = device->next_device) {
530 if(!device->load_state(state_fio)) {
534 monitor_type = state_fio->FgetInt32();