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 "../pcm1bit.h"
22 #include "../rp5c01.h"
23 #include "../w3100a.h"
24 #include "../ym2203.h"
26 #include "../z80pio.h"
27 #include "../z80sio.h"
30 #include "../debugger.h"
37 #include "interrupt.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 VM::VM(EMU* parent_emu) : emu(parent_emu)
56 first_device = last_device = NULL;
57 dummy = new DEVICE(this, emu); // must be 1st device
58 event = new EVENT(this, emu); // must be 2nd device
60 drec = new DATAREC(this, emu);
61 pit = new I8253(this, emu);
62 pio_i = new I8255(this, emu);
63 io = new IO(this, emu);
64 fdc = new MB8877(this, emu);
65 pcm = new PCM1BIT(this, emu);
66 rtc = new RP5C01(this, emu); // RP-5C15
67 w3100a = new W3100A(this, emu);
68 opn = new YM2203(this, emu);
69 cpu = new Z80(this, emu);
70 pio = new Z80PIO(this, emu);
71 sio = new Z80SIO(this, emu);
73 calendar = new CALENDAR(this, emu);
74 cmt = new CMT(this, emu);
75 crtc = new CRTC(this, emu);
76 floppy = new FLOPPY(this, emu);
77 interrupt = new INTERRUPT(this, emu);
78 joystick = new JOYSTICK(this, emu);
79 keyboard = new KEYBOARD(this, emu);
80 memory = new MEMORY(this, emu);
81 mouse = new MOUSE(this, emu);
82 mz1e26 = new MZ1E26(this, emu);
83 mz1e30 = new MZ1E30(this, emu);
84 mz1r13 = new MZ1R13(this, emu);
85 mz1r37 = new MZ1R37(this, emu);
86 serial = new SERIAL(this, emu);
87 timer = new TIMER(this, emu);
90 event->set_context_cpu(cpu);
91 event->set_context_sound(opn);
92 event->set_context_sound(pcm);
93 event->set_context_sound(drec);
95 drec->set_context_ear(cmt, SIG_CMT_OUT, 1);
96 drec->set_context_remote(cmt, SIG_CMT_REMOTE, 1);
97 drec->set_context_end(cmt, SIG_CMT_END, 1);
98 drec->set_context_top(cmt, SIG_CMT_TOP, 1);
100 pit->set_context_ch0(interrupt, SIG_INTERRUPT_I8253, 1);
101 pit->set_context_ch0(pit, SIG_I8253_CLOCK_1, 1);
102 pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);
103 pit->set_constant_clock(0, 31250);
104 pio_i->set_context_port_a(cmt, SIG_CMT_PIO_PA, 0xff, 0);
105 pio_i->set_context_port_c(cmt, SIG_CMT_PIO_PC, 0xff, 0);
106 pio_i->set_context_port_c(crtc, SIG_CRTC_MASK, 0x01, 0);
107 pio_i->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x04, 0);
108 rtc->set_context_alarm(interrupt, SIG_INTERRUPT_RP5C15, 1);
109 rtc->set_context_pulse(opn, SIG_YM2203_PORT_B, 8);
110 opn->set_context_port_a(floppy, SIG_FLOPPY_REVERSE, 0x02, 0);
111 opn->set_context_port_a(crtc, SIG_CRTC_PALLETE, 0x04, 0);
112 opn->set_context_port_a(mouse, SIG_MOUSE_SEL, 0x08, 0);
113 pio->set_context_port_a(crtc, SIG_CRTC_COLUMN_SIZE, 0x20, 0);
114 pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x1f, 0);
115 sio->set_context_dtr(1, mouse, SIG_MOUSE_DTR, 1);
117 calendar->set_context_rtc(rtc);
118 cmt->set_context_pio(pio_i);
119 cmt->set_context_drec(drec);
120 crtc->set_context_mem(memory);
121 crtc->set_context_int(interrupt);
122 crtc->set_context_pio(pio_i);
123 crtc->set_vram_ptr(memory->get_vram());
124 crtc->set_tvram_ptr(memory->get_tvram());
125 crtc->set_kanji_ptr(memory->get_kanji());
126 crtc->set_pcg_ptr(memory->get_pcg());
127 floppy->set_context_fdc(fdc);
128 keyboard->set_context_pio_i(pio_i);
129 keyboard->set_context_pio(pio);
130 memory->set_context_cpu(cpu);
131 memory->set_context_crtc(crtc);
132 mouse->set_context_sio(sio);
133 serial->set_context_sio(sio);
134 timer->set_context_pit(pit);
137 cpu->set_context_mem(memory);
138 cpu->set_context_io(io);
139 cpu->set_context_intr(pio);
141 cpu->set_context_debugger(new DEBUGGER(this, emu));
144 // z80 family daisy chain
145 pio->set_context_intr(cpu, 0);
146 pio->set_context_child(sio);
147 sio->set_context_intr(cpu, 1);
148 sio->set_context_child(interrupt);
149 interrupt->set_context_intr(cpu, 2);
152 io->set_iomap_range_rw(0x60, 0x63, w3100a);
153 io->set_iomap_range_rw(0xa0, 0xa3, serial);
154 io->set_iomap_range_rw(0xa4, 0xa5, mz1e30);
155 io->set_iomap_range_rw(0xa8, 0xa9, mz1e30);
156 io->set_iomap_range_rw(0xac, 0xad, mz1r37);
157 io->set_iomap_single_w(0xae, crtc);
158 io->set_iomap_range_rw(0xb0, 0xb3, serial);
159 io->set_iomap_range_rw(0xb4, 0xb5, memory);
160 io->set_iomap_range_rw(0xb8, 0xb9, mz1r13);
161 io->set_iomap_range_rw(0xbc, 0xbf, crtc);
162 io->set_iomap_range_w(0xc6, 0xc7, interrupt);
163 io->set_iomap_range_rw(0xc8, 0xc9, opn);
164 io->set_iomap_single_rw(0xca, mz1e26);
165 io->set_iomap_single_rw(0xcc, calendar);
166 io->set_iomap_single_w(0xcd, serial);
167 io->set_iomap_range_w(0xce, 0xcf, memory);
168 io->set_iomap_range_rw(0xd8, 0xdb, fdc);
169 io->set_iomap_range_w(0xdc, 0xdd, floppy);
170 io->set_iomap_range_rw(0xe0, 0xe3, pio_i);
171 io->set_iomap_range_rw(0xe4, 0xe7, pit);
172 io->set_iomap_range_rw(0xe8, 0xeb, pio);
173 io->set_iomap_single_rw(0xef, joystick);
174 io->set_iomap_range_w(0xf0, 0xf3, timer);
175 io->set_iomap_range_rw(0xf4, 0xf7, crtc);
177 io->set_iowait_range_rw(0xc8, 0xc9, 1);
178 io->set_iowait_single_rw(0xcc, 3);
179 io->set_iowait_range_rw(0xd8, 0xdf, 1);
180 io->set_iowait_range_rw(0xe8, 0xeb, 1);
182 // initialize all devices
183 for(DEVICE* device = first_device; device; device = device->next_device) {
184 device->initialize();
186 for(int i = 0; i < MAX_DRIVE; i++) {
187 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
189 monitor_type = config.monitor_type;
194 // delete all devices
195 for(DEVICE* device = first_device; device;) {
196 DEVICE *next_device = device->next_device;
199 device = next_device;
203 DEVICE* VM::get_device(int id)
205 for(DEVICE* device = first_device; device; device = device->next_device) {
206 if(device->this_device_id == id) {
213 // ----------------------------------------------------------------------------
214 // drive virtual machine
215 // ----------------------------------------------------------------------------
220 for(DEVICE* device = first_device; device; device = device->next_device) {
224 // set initial port status
225 opn->write_signal(SIG_YM2203_PORT_B, (monitor_type & 2) ? 0x77 : 0x37, 0xff);
228 void VM::special_reset()
231 // for(DEVICE* device = first_device; device; device = device->next_device) {
232 // device->special_reset();
234 memory->special_reset();
243 double VM::frame_rate()
245 return event->frame_rate();
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
253 DEVICE *VM::get_cpu(int index)
262 // ----------------------------------------------------------------------------
264 // ----------------------------------------------------------------------------
266 void VM::draw_screen()
271 int VM::access_lamp()
273 uint32 status = fdc->read_signal(0) | mz1e30->read_signal(0);
274 return (status & 0x30) ? 4 : (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
277 // ----------------------------------------------------------------------------
279 // ----------------------------------------------------------------------------
281 void VM::initialize_sound(int rate, int samples)
283 // init sound manager
284 event->initialize_sound(rate, samples);
287 opn->init(rate, 2000000, samples, 0, -8);
288 pcm->init(rate, 4096);
291 uint16* VM::create_sound(int* extra_frames)
293 return event->create_sound(extra_frames);
296 int VM::sound_buffer_ptr()
298 return event->sound_buffer_ptr();
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 void VM::network_connected(int ch)
307 w3100a->connected(ch);
310 void VM::network_disconnected(int ch)
312 w3100a->disconnected(ch);
315 uint8* VM::get_sendbuffer(int ch, int* size)
317 return w3100a->get_sendbuffer(ch, size);
320 void VM::inc_sendbuffer_ptr(int ch, int size)
322 w3100a->inc_sendbuffer_ptr(ch, size);
325 uint8* VM::get_recvbuffer0(int ch, int* size0, int* size1)
327 return w3100a->get_recvbuffer0(ch, size0, size1);
330 uint8* VM::get_recvbuffer1(int ch)
332 return w3100a->get_recvbuffer1(ch);
335 void VM::inc_recvbuffer_ptr(int ch, int size)
337 w3100a->inc_recvbuffer_ptr(ch, size);
340 // ----------------------------------------------------------------------------
342 // ----------------------------------------------------------------------------
344 void VM::open_disk(int drv, const _TCHAR* file_path, int bank)
346 fdc->open_disk(drv, file_path, bank);
349 void VM::close_disk(int drv)
351 fdc->close_disk(drv);
354 bool VM::disk_inserted(int drv)
356 return fdc->disk_inserted(drv);
359 void VM::set_disk_protected(int drv, bool value)
361 fdc->set_disk_protected(drv, value);
364 bool VM::get_disk_protected(int drv)
366 return fdc->get_disk_protected(drv);
369 void VM::play_tape(const _TCHAR* file_path)
371 bool value = drec->play_tape(file_path);
373 cmt->play_tape(value);
376 void VM::rec_tape(const _TCHAR* file_path)
378 bool value = drec->rec_tape(file_path);
380 cmt->rec_tape(value);
383 void VM::close_tape()
389 bool VM::tape_inserted()
391 return drec->tape_inserted();
394 bool VM::tape_playing()
396 return drec->tape_playing();
399 bool VM::tape_recording()
401 return drec->tape_recording();
404 int VM::tape_position()
406 return drec->tape_position();
412 drec->set_remote(true);
417 drec->set_remote(false);
420 void VM::push_fast_forward()
423 drec->set_remote(true);
426 void VM::push_fast_rewind()
428 drec->set_ff_rew(-1);
429 drec->set_remote(true);
435 return event->now_skip();
438 void VM::update_config()
440 for(DEVICE* device = first_device; device; device = device->next_device) {
441 device->update_config();
445 #define STATE_VERSION 2
447 void VM::save_state(FILEIO* state_fio)
449 state_fio->FputUint32(STATE_VERSION);
451 for(DEVICE* device = first_device; device; device = device->next_device) {
452 device->save_state(state_fio);
454 state_fio->FputInt32(monitor_type);
457 bool VM::load_state(FILEIO* state_fio)
459 if(state_fio->FgetUint32() != STATE_VERSION) {
462 for(DEVICE* device = first_device; device; device = device->next_device) {
463 if(!device->load_state(state_fio)) {
467 monitor_type = state_fio->FgetInt32();