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 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);
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);
95 event->set_context_cpu(cpu);
96 event->set_context_sound(opn);
97 event->set_context_sound(pcm);
98 event->set_context_sound(drec);
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);
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);
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));
147 printer->set_context_prn(dummy);
149 serial->set_context_sio(sio);
150 timer->set_context_pit(pit);
153 cpu->set_context_mem(memory);
154 cpu->set_context_io(io);
155 cpu->set_context_intr(pio);
157 cpu->set_context_debugger(new DEBUGGER(this, emu));
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);
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);
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);
199 // initialize all devices
200 for(DEVICE* device = first_device; device; device = device->next_device) {
201 device->initialize();
203 for(int i = 0; i < MAX_DRIVE; i++) {
204 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
206 monitor_type = config.monitor_type;
211 // delete all devices
212 for(DEVICE* device = first_device; device;) {
213 DEVICE *next_device = device->next_device;
216 device = next_device;
220 DEVICE* VM::get_device(int id)
222 for(DEVICE* device = first_device; device; device = device->next_device) {
223 if(device->this_device_id == id) {
230 // ----------------------------------------------------------------------------
231 // drive virtual machine
232 // ----------------------------------------------------------------------------
237 for(DEVICE* device = first_device; device; device = device->next_device) {
241 // set initial port status
242 opn->write_signal(SIG_YM2203_PORT_B, (monitor_type & 2) ? 0x77 : 0x37, 0xff);
245 void VM::special_reset()
248 // for(DEVICE* device = first_device; device; device = device->next_device) {
249 // device->special_reset();
251 memory->special_reset();
260 double VM::get_frame_rate()
262 return event->get_frame_rate();
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
270 DEVICE *VM::get_cpu(int index)
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 void VM::draw_screen()
288 uint32_t VM::get_access_lamp_status()
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;
294 // ----------------------------------------------------------------------------
296 // ----------------------------------------------------------------------------
298 void VM::initialize_sound(int rate, int samples)
300 // init sound manager
301 event->initialize_sound(rate, samples);
304 opn->initialize_sound(rate, 2000000, samples, 0, -8);
305 pcm->initialize_sound(rate, 4096);
308 uint16_t* VM::create_sound(int* extra_frames)
310 return event->create_sound(extra_frames);
313 int VM::get_sound_buffer_ptr()
315 return event->get_sound_buffer_ptr();
318 #ifdef USE_SOUND_VOLUME
319 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
322 opn->set_volume(0, decibel_l, decibel_r);
324 opn->set_volume(1, decibel_l, decibel_r);
326 pcm->set_volume(0, decibel_l, decibel_r);
328 drec->set_volume(0, decibel_l, decibel_r);
330 drec->set_volume(1, decibel_l, decibel_r);
335 // ----------------------------------------------------------------------------
337 // ----------------------------------------------------------------------------
339 void VM::notify_socket_connected(int ch)
341 w3100a->notify_connected(ch);
344 void VM::notify_socket_disconnected(int ch)
346 w3100a->notify_disconnected(ch);
349 uint8_t* VM::get_socket_send_buffer(int ch, int* size)
351 return w3100a->get_send_buffer(ch, size);
354 void VM::inc_socket_send_buffer_ptr(int ch, int size)
356 w3100a->inc_send_buffer_ptr(ch, size);
359 uint8_t* VM::get_socket_recv_buffer0(int ch, int* size0, int* size1)
361 return w3100a->get_recv_buffer0(ch, size0, size1);
364 uint8_t* VM::get_socket_recv_buffer1(int ch)
366 return w3100a->get_recv_buffer1(ch);
369 void VM::inc_socket_recv_buffer_ptr(int ch, int size)
371 w3100a->inc_recv_buffer_ptr(ch, size);
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
380 fdc->open_disk(drv, file_path, bank);
383 void VM::close_floppy_disk(int drv)
385 fdc->close_disk(drv);
388 bool VM::is_floppy_disk_inserted(int drv)
390 return fdc->is_disk_inserted(drv);
393 void VM::is_floppy_disk_protected(int drv, bool value)
395 fdc->is_disk_protected(drv, value);
398 bool VM::is_floppy_disk_protected(int drv)
400 return fdc->is_disk_protected(drv);
403 void VM::play_tape(const _TCHAR* file_path)
405 bool value = drec->play_tape(file_path);
407 cmt->play_tape(value);
410 void VM::rec_tape(const _TCHAR* file_path)
412 bool value = drec->rec_tape(file_path);
414 cmt->rec_tape(value);
417 void VM::close_tape()
423 bool VM::is_tape_inserted()
425 return drec->is_tape_inserted();
428 bool VM::is_tape_playing()
430 return drec->is_tape_playing();
433 bool VM::is_tape_recording()
435 return drec->is_tape_recording();
438 int VM::get_tape_position()
440 return drec->get_tape_position();
446 drec->set_remote(true);
451 drec->set_remote(false);
454 void VM::push_fast_forward()
457 drec->set_remote(true);
460 void VM::push_fast_rewind()
462 drec->set_ff_rew(-1);
463 drec->set_remote(true);
467 bool VM::is_frame_skippable()
469 return event->is_frame_skippable();
472 void VM::update_config()
474 for(DEVICE* device = first_device; device; device = device->next_device) {
475 device->update_config();
479 #define STATE_VERSION 4
481 void VM::save_state(FILEIO* state_fio)
483 state_fio->FputUint32(STATE_VERSION);
485 for(DEVICE* device = first_device; device; device = device->next_device) {
486 device->save_state(state_fio);
488 state_fio->FputInt32(monitor_type);
491 bool VM::load_state(FILEIO* state_fio)
493 if(state_fio->FgetUint32() != STATE_VERSION) {
496 for(DEVICE* device = first_device; device; device = device->next_device) {
497 if(!device->load_state(state_fio)) {
501 monitor_type = state_fio->FgetInt32();