2 SHARP MZ-5500 Emulator 'EmuZ-5500'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
19 #if defined(HAS_I286) || defined(HAS_I186)
26 #include "../mz1p17.h"
28 #include "../prnfile.h"
29 #include "../rp5c01.h"
30 #include "../upd7220.h"
31 #include "../upd765a.h"
32 #include "../ym2203.h"
33 #include "../z80ctc.h"
34 #include "../z80sio.h"
37 #include "../debugger.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 VM::VM(EMU* parent_emu) : emu(parent_emu)
52 first_device = last_device = NULL;
53 dummy = new DEVICE(this, emu); // must be 1st device
54 event = new EVENT(this, emu); // must be 2nd device
56 dummy->set_device_name(_T("1st Dummy"));
57 event->set_device_name(_T("EVENT"));
60 if(config.printer_device_type == 0) {
61 printer = new PRNFILE(this, emu);
62 } else if(config.printer_device_type == 1) {
63 printer = new MZ1P17(this, emu);
67 dma = new I8237(this, emu);
68 pio = new I8255(this, emu);
69 pic = new I8259(this, emu);
70 #if defined(HAS_I286) || defined(HAS_I186)
71 cpu = new I286(this, emu);
73 cpu = new I86(this, emu);
75 io = new IO(this, emu);
76 div = new LS393(this, emu);
78 io->set_device_name(_T("MAIN I/O"));
80 cpu->set_device_name(_T("MAIN CPU(i286)"));
82 cpu->set_device_name(_T("MAIN CPU(i8086)"));
84 div->set_device_name(_T("74LS393(DIVIDER)"));
86 not_data1 = new NOT(this, emu);
87 not_data2 = new NOT(this, emu);
88 not_data3 = new NOT(this, emu);
89 not_data4 = new NOT(this, emu);
90 not_data5 = new NOT(this, emu);
91 not_data6 = new NOT(this, emu);
92 not_data7 = new NOT(this, emu);
93 not_data8 = new NOT(this, emu);
94 not_busy = new NOT(this, emu);
96 not_data1->set_device_name(_T("NOT GATE #1"));
97 not_data2->set_device_name(_T("NOT GATE #2"));
98 not_data3->set_device_name(_T("NOT GATE #3"));
99 not_data4->set_device_name(_T("NOT GATE #4"));
100 not_data5->set_device_name(_T("NOT GATE #5"));
101 not_data6->set_device_name(_T("NOT GATE #6"));
102 not_data7->set_device_name(_T("NOT GATE #7"));
103 not_data8->set_device_name(_T("NOT GATE #8"));
104 not_busy->set_device_name(_T("NOT GATE(PRINTER BUSY)"));
106 rtc = new RP5C01(this, emu);
107 gdc = new UPD7220(this, emu);
108 fdc = new UPD765A(this, emu);
109 psg = new YM2203(this, emu); // AY-3-8912
110 ctc0 = new Z80CTC(this, emu);
111 #if defined(_MZ6500) || defined(_MZ6550)
112 ctc1 = new Z80CTC(this, emu);
114 sio = new Z80SIO(this, emu);
116 display = new DISPLAY(this, emu);
117 keyboard = new KEYBOARD(this, emu);
118 memory = new MEMORY(this, emu);
119 sysport = new SYSPORT(this, emu);
121 display->set_device_name(_T("DISPLAY I/F"));
122 keyboard->set_device_name(_T("KEYBOARD I/F"));
123 memory->set_device_name(_T("MEMORY"));
124 sysport->set_device_name(_T("SYSTEM PORT"));
127 event->set_context_cpu(cpu);
128 event->set_context_sound(psg);
129 #if defined(USE_SOUND_FILES)
130 if(fdc->load_sound_data(UPD765A_SND_TYPE_SEEK, _T("FDDSEEK.WAV"))) {
131 event->set_context_sound(fdc);
135 if(config.printer_device_type == 0) {
136 PRNFILE *prnfile = (PRNFILE *)printer;
137 prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
138 prnfile->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
139 } else if(config.printer_device_type == 1) {
140 MZ1P17 *mz1p17 = (MZ1P17 *)printer;
141 mz1p17->mode = MZ1P17_MODE_MZ1;
142 mz1p17->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
143 mz1p17->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
145 dma->set_context_memory(memory);
146 dma->set_context_ch1(fdc);
147 pio->set_context_port_a(not_data1, SIG_NOT_INPUT, 0x01, 0);
148 pio->set_context_port_a(not_data2, SIG_NOT_INPUT, 0x02, 0);
149 pio->set_context_port_a(not_data3, SIG_NOT_INPUT, 0x04, 0);
150 pio->set_context_port_a(not_data4, SIG_NOT_INPUT, 0x08, 0);
151 pio->set_context_port_a(not_data5, SIG_NOT_INPUT, 0x10, 0);
152 pio->set_context_port_a(not_data6, SIG_NOT_INPUT, 0x20, 0);
153 pio->set_context_port_a(not_data7, SIG_NOT_INPUT, 0x40, 0);
154 pio->set_context_port_a(not_data8, SIG_NOT_INPUT, 0x80, 0);
155 pio->set_context_port_c(keyboard, SIG_KEYBOARD_INPUT, 0x03, 0);
156 pio->set_context_port_c(pic, SIG_I8259_IR2 | SIG_I8259_CHIP0, 0x08, 0);
157 pio->set_context_port_c(printer, SIG_PRINTER_STROBE, 0x20, 0);
158 pic->set_context_cpu(cpu);
159 div->set_context_2qb(ctc0, SIG_Z80CTC_TRIG_3, 1);
160 #if defined(_MZ6500) || defined(_MZ6550)
161 div->set_context_1qb(ctc1, SIG_Z80CTC_TRIG_0, 1);
162 div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_1, 1);
163 div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_2, 1);
164 div->set_context_2qd(ctc1, SIG_Z80CTC_TRIG_3, 1);
166 rtc->set_context_alarm(pic, SIG_I8259_IR0 | SIG_I8259_CHIP1, 1);
167 gdc->set_vram_ptr(memory->get_vram(), 0x80000);
168 gdc->set_context_vsync(pic, SIG_I8259_IR0 | SIG_I8259_CHIP0, 1);
169 fdc->set_context_irq(pic, SIG_I8259_IR1 | SIG_I8259_CHIP1, 1);
170 fdc->set_context_drq(dma, SIG_I8237_CH1, 1);
171 psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP0, 0x20, 0);
172 psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP1, 0x40, 0);
173 psg->set_context_port_a(memory, SIG_MEMORY_BANK, 0xe0, 0);
174 ctc0->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
175 ctc0->set_context_zc0(div, SIG_LS393_CLK, 1);
176 ctc0->set_context_zc1(sio, SIG_Z80SIO_TX_CLK_CH0, 1);
177 ctc0->set_context_zc1(sio, SIG_Z80SIO_RX_CLK_CH0, 1);
178 ctc0->set_context_zc2(sio, SIG_Z80SIO_TX_CLK_CH1, 1);
179 ctc0->set_context_zc2(sio, SIG_Z80SIO_RX_CLK_CH1, 1);
180 #if defined(_MZ6500) || defined(_MZ6550)
181 ctc0->set_context_child(ctc1);
182 ctc1->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
184 sio->set_context_intr(pic, SIG_I8259_IR1 | SIG_I8259_CHIP0);
186 not_data1->set_context_out(printer, SIG_PRINTER_DATA, 0x01);
187 not_data2->set_context_out(printer, SIG_PRINTER_DATA, 0x02);
188 not_data3->set_context_out(printer, SIG_PRINTER_DATA, 0x04);
189 not_data4->set_context_out(printer, SIG_PRINTER_DATA, 0x08);
190 not_data5->set_context_out(printer, SIG_PRINTER_DATA, 0x10);
191 not_data6->set_context_out(printer, SIG_PRINTER_DATA, 0x20);
192 not_data7->set_context_out(printer, SIG_PRINTER_DATA, 0x40);
193 not_data8->set_context_out(printer, SIG_PRINTER_DATA, 0x80);
194 not_busy->set_context_out(pio, SIG_I8255_PORT_B, 0x01);
195 display->set_vram_ptr(memory->get_vram());
196 display->set_sync_ptr(gdc->get_sync());
197 display->set_ra_ptr(gdc->get_ra());
198 display->set_cs_ptr(gdc->get_cs());
199 display->set_ead_ptr(gdc->get_ead());
200 keyboard->set_context_pio(pio);
201 keyboard->set_context_pic(pic);
202 memory->set_context_cpu(cpu);
203 sysport->set_context_fdc(fdc);
204 sysport->set_context_ctc(ctc0);
205 sysport->set_context_sio(sio);
208 cpu->set_context_mem(memory);
209 cpu->set_context_io(io);
210 cpu->set_context_intr(pic);
211 #ifdef SINGLE_MODE_DMA
212 cpu->set_context_dma(dma);
215 cpu->set_context_debugger(new DEBUGGER(this, emu));
219 io->set_iomap_range_rw(0x00, 0x0f, dma);
220 io->set_iomap_range_rw(0x10, 0x1f, pio);
221 io->set_iomap_range_rw(0x20, 0x2f, fdc);
222 for(int i = 0x30; i < 0x40; i += 2) {
223 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP0 | ((i >> 1) & 1));
225 for(int i = 0x40; i < 0x50; i += 2) {
226 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP1 | ((i >> 1) & 1));
228 io->set_iomap_range_w(0x50, 0x5f, memory);
229 io->set_iomap_range_r(0x60, 0x6f, sysport);
230 io->set_iomap_range_w(0x70, 0x7f, sysport);
231 #if defined(_MZ6500) || defined(_MZ6550)
232 io->set_iomap_single_rw(0xcd, memory);
234 for(int i = 0x100; i < 0x110; i += 2) {
235 io->set_iomap_alias_rw(i, gdc, (i >> 1) & 1);
237 io->set_iomap_range_rw(0x110, 0x17f, display);
238 io->set_iomap_range_rw(0x200, 0x20f, sio);
239 io->set_iomap_range_rw(0x210, 0x21f, ctc0);
240 io->set_iomap_range_rw(0x220, 0x22f, rtc);
241 for(int i = 0x230; i < 0x240; i++) {
242 io->set_iomap_alias_rw(i, psg, ~i & 1);
244 io->set_iomap_range_r(0x240, 0x25f, sysport);
245 io->set_iomap_range_w(0x260, 0x26f, sysport);
246 io->set_iomap_range_r(0x270, 0x27f, sysport);
248 // initialize all devices
249 for(DEVICE* device = first_device; device; device = device->next_device) {
250 device->initialize();
252 for(int i = 0; i < 4; i++) {
253 #if defined(_MZ6500) || defined(_MZ6550)
254 fdc->set_drive_type(i, DRIVE_TYPE_2HD);
256 fdc->set_drive_type(i, DRIVE_TYPE_2D);
263 // delete all devices
264 for(DEVICE* device = first_device; device;) {
265 DEVICE *next_device = device->next_device;
268 device = next_device;
272 DEVICE* VM::get_device(int id)
274 for(DEVICE* device = first_device; device; device = device->next_device) {
275 if(device->this_device_id == id) {
282 // ----------------------------------------------------------------------------
283 // drive virtual machine
284 // ----------------------------------------------------------------------------
289 for(DEVICE* device = first_device; device; device = device->next_device) {
292 not_busy->write_signal(SIG_NOT_INPUT, 0, 0); // busy = low
293 pio->write_signal(SIG_I8255_PORT_B, 0x03, 0x07); // busy = ~(low), pe = ~(low), pdtr = ~(high)
294 pio->write_signal(SIG_I8255_PORT_C, 0x40, 0x40); // ack = high
297 void VM::special_reset()
300 cpu->write_signal(SIG_CPU_NMI, 1, 1);
301 sysport->nmi_reset();
309 double VM::get_frame_rate()
311 return event->get_frame_rate();
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
319 DEVICE *VM::get_cpu(int index)
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
332 void VM::draw_screen()
334 display->draw_screen();
337 uint32_t VM::get_access_lamp_status()
339 uint32_t status = fdc->read_signal(0);
340 return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
343 // ----------------------------------------------------------------------------
345 // ----------------------------------------------------------------------------
347 void VM::initialize_sound(int rate, int samples)
349 // init sound manager
350 event->initialize_sound(rate, samples);
353 psg->initialize_sound(rate, 4000000, samples, 0, 0);
356 uint16_t* VM::create_sound(int* extra_frames)
358 return event->create_sound(extra_frames);
361 int VM::get_sound_buffer_ptr()
363 return event->get_sound_buffer_ptr();
366 #ifdef USE_SOUND_VOLUME
367 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
370 psg->set_volume(1, decibel_l, decibel_r);
372 #if defined(USE_SOUND_FILES)
374 fdc->set_volume(UPD765A_SND_TYPE_SEEK, decibel_l, decibel_r);
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
384 void VM::key_down(int code, bool repeat)
386 keyboard->key_down(code);
389 void VM::key_up(int code)
391 // keyboard->key_up(code);
394 // ----------------------------------------------------------------------------
396 // ----------------------------------------------------------------------------
398 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
400 fdc->open_disk(drv, file_path, bank);
403 void VM::close_floppy_disk(int drv)
405 fdc->close_disk(drv);
408 bool VM::is_floppy_disk_inserted(int drv)
410 return fdc->is_disk_inserted(drv);
413 void VM::is_floppy_disk_protected(int drv, bool value)
415 fdc->is_disk_protected(drv, value);
418 bool VM::is_floppy_disk_protected(int drv)
420 return fdc->is_disk_protected(drv);
423 bool VM::is_frame_skippable()
425 return event->is_frame_skippable();
428 void VM::update_config()
430 for(DEVICE* device = first_device; device; device = device->next_device) {
431 device->update_config();
435 #define STATE_VERSION 2
437 void VM::save_state(FILEIO* state_fio)
439 state_fio->FputUint32(STATE_VERSION);
441 for(DEVICE* device = first_device; device; device = device->next_device) {
442 device->save_state(state_fio);
446 bool VM::load_state(FILEIO* state_fio)
448 if(state_fio->FgetUint32() != STATE_VERSION) {
451 for(DEVICE* device = first_device; device; device = device->next_device) {
452 if(!device->load_state(state_fio)) {