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 "../ay_3_891x.h"
34 #include "../z80ctc.h"
35 #include "../z80sio.h"
38 #include "../debugger.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 VM::VM(EMU* parent_emu) : emu(parent_emu)
53 first_device = last_device = NULL;
54 dummy = new DEVICE(this, emu); // must be 1st device
55 event = new EVENT(this, emu); // must be 2nd device
57 dummy->set_device_name(_T("1st Dummy"));
58 event->set_device_name(_T("EVENT"));
61 if(config.printer_device_type == 0) {
62 printer = new PRNFILE(this, emu);
63 } else if(config.printer_device_type == 1) {
64 printer = new MZ1P17(this, emu);
68 dma = new I8237(this, emu);
69 pio = new I8255(this, emu);
70 pic = new I8259(this, emu);
71 #if defined(HAS_I286) || defined(HAS_I186)
72 cpu = new I286(this, emu);
74 cpu = new I86(this, emu);
76 io = new IO(this, emu);
77 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)"));
85 not_data0 = new NOT(this, emu);
86 not_data0->set_device_name(_T("NOT Gate (Printer Bit0)"));
87 not_data1 = new NOT(this, emu);
88 not_data1->set_device_name(_T("NOT Gate (Printer Bit1)"));
89 not_data2 = new NOT(this, emu);
90 not_data2->set_device_name(_T("NOT Gate (Printer Bit2)"));
91 not_data3 = new NOT(this, emu);
92 not_data3->set_device_name(_T("NOT Gate (Printer Bit3)"));
93 not_data4 = new NOT(this, emu);
94 not_data4->set_device_name(_T("NOT Gate (Printer Bit4)"));
95 not_data5 = new NOT(this, emu);
96 not_data5->set_device_name(_T("NOT Gate (Printer Bit5)"));
97 not_data6 = new NOT(this, emu);
98 not_data6->set_device_name(_T("NOT Gate (Printer Bit6)"));
99 not_data7 = new NOT(this, emu);
100 not_data7->set_device_name(_T("NOT Gate (Printer Bit7)"));
101 not_busy = new NOT(this, emu);
102 not_busy->set_device_name(_T("NOT Gate (Printer Busy)"));
103 rtc = new RP5C01(this, emu);
104 gdc = new UPD7220(this, emu);
105 fdc = new UPD765A(this, emu);
106 // psg = new YM2203(this, emu);
107 psg = new AY_3_891X(this, emu); // AY-3-8912
108 ctc0 = new Z80CTC(this, emu);
109 #if defined(_MZ6500) || defined(_MZ6550)
110 ctc1 = new Z80CTC(this, emu);
112 sio = new Z80SIO(this, emu);
114 display = new DISPLAY(this, emu);
115 keyboard = new KEYBOARD(this, emu);
116 memory = new MEMORY(this, emu);
117 sysport = new SYSPORT(this, emu);
119 display->set_device_name(_T("DISPLAY I/F"));
120 keyboard->set_device_name(_T("KEYBOARD I/F"));
121 memory->set_device_name(_T("MEMORY"));
122 sysport->set_device_name(_T("SYSTEM PORT"));
125 event->set_context_cpu(cpu);
126 event->set_context_sound(psg);
127 #if defined(USE_SOUND_FILES)
128 if(fdc->load_sound_data(UPD765A_SND_TYPE_SEEK, _T("FDDSEEK.WAV"))) {
129 event->set_context_sound(fdc);
133 if(config.printer_device_type == 0) {
134 PRNFILE *prnfile = (PRNFILE *)printer;
135 prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
136 prnfile->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
137 } else if(config.printer_device_type == 1) {
138 MZ1P17 *mz1p17 = (MZ1P17 *)printer;
139 mz1p17->mode = MZ1P17_MODE_MZ1;
140 mz1p17->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
141 mz1p17->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
143 dma->set_context_memory(memory);
144 dma->set_context_ch1(fdc);
145 pio->set_context_port_a(not_data0, SIG_NOT_INPUT, 0x01, 0);
146 pio->set_context_port_a(not_data1, SIG_NOT_INPUT, 0x02, 0);
147 pio->set_context_port_a(not_data2, SIG_NOT_INPUT, 0x04, 0);
148 pio->set_context_port_a(not_data3, SIG_NOT_INPUT, 0x08, 0);
149 pio->set_context_port_a(not_data4, SIG_NOT_INPUT, 0x10, 0);
150 pio->set_context_port_a(not_data5, SIG_NOT_INPUT, 0x20, 0);
151 pio->set_context_port_a(not_data6, SIG_NOT_INPUT, 0x40, 0);
152 pio->set_context_port_a(not_data7, SIG_NOT_INPUT, 0x80, 0);
153 pio->set_context_port_c(keyboard, SIG_KEYBOARD_INPUT, 0x03, 0);
154 pio->set_context_port_c(pic, SIG_I8259_IR2 | SIG_I8259_CHIP0, 0x08, 0);
155 pio->set_context_port_c(printer, SIG_PRINTER_STROBE, 0x20, 0);
156 pic->set_context_cpu(cpu);
157 div->set_context_2qb(ctc0, SIG_Z80CTC_TRIG_3, 1);
158 #if defined(_MZ6500) || defined(_MZ6550)
159 div->set_context_1qb(ctc1, SIG_Z80CTC_TRIG_0, 1);
160 div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_1, 1);
161 div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_2, 1);
162 div->set_context_2qd(ctc1, SIG_Z80CTC_TRIG_3, 1);
164 rtc->set_context_alarm(pic, SIG_I8259_IR0 | SIG_I8259_CHIP1, 1);
165 gdc->set_vram_ptr(memory->get_vram(), 0x80000);
166 gdc->set_context_vsync(pic, SIG_I8259_IR0 | SIG_I8259_CHIP0, 1);
167 fdc->set_context_irq(pic, SIG_I8259_IR1 | SIG_I8259_CHIP1, 1);
168 fdc->set_context_drq(dma, SIG_I8237_CH1, 1);
169 psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP0, 0x20, 0);
170 psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP1, 0x40, 0);
171 psg->set_context_port_a(memory, SIG_MEMORY_BANK, 0xe0, 0);
172 ctc0->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
173 ctc0->set_context_zc0(div, SIG_LS393_CLK, 1);
174 ctc0->set_context_zc1(sio, SIG_Z80SIO_TX_CLK_CH0, 1);
175 ctc0->set_context_zc1(sio, SIG_Z80SIO_RX_CLK_CH0, 1);
176 ctc0->set_context_zc2(sio, SIG_Z80SIO_TX_CLK_CH1, 1);
177 ctc0->set_context_zc2(sio, SIG_Z80SIO_RX_CLK_CH1, 1);
178 #if defined(_MZ6500) || defined(_MZ6550)
179 ctc0->set_context_child(ctc1);
180 ctc1->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
182 sio->set_context_intr(pic, SIG_I8259_IR1 | SIG_I8259_CHIP0);
184 not_data0->set_context_out(printer, SIG_PRINTER_DATA, 0x01);
185 not_data1->set_context_out(printer, SIG_PRINTER_DATA, 0x02);
186 not_data2->set_context_out(printer, SIG_PRINTER_DATA, 0x04);
187 not_data3->set_context_out(printer, SIG_PRINTER_DATA, 0x08);
188 not_data4->set_context_out(printer, SIG_PRINTER_DATA, 0x10);
189 not_data5->set_context_out(printer, SIG_PRINTER_DATA, 0x20);
190 not_data6->set_context_out(printer, SIG_PRINTER_DATA, 0x40);
191 not_data7->set_context_out(printer, SIG_PRINTER_DATA, 0x80);
192 not_busy->set_context_out(pio, SIG_I8255_PORT_B, 0x01);
193 display->set_vram_ptr(memory->get_vram());
194 display->set_sync_ptr(gdc->get_sync());
195 display->set_ra_ptr(gdc->get_ra());
196 display->set_cs_ptr(gdc->get_cs());
197 display->set_ead_ptr(gdc->get_ead());
198 keyboard->set_context_pio(pio);
199 keyboard->set_context_pic(pic);
200 memory->set_context_cpu(cpu);
201 sysport->set_context_fdc(fdc);
202 sysport->set_context_ctc(ctc0);
203 sysport->set_context_sio(sio);
206 cpu->set_context_mem(memory);
207 cpu->set_context_io(io);
208 cpu->set_context_intr(pic);
209 #ifdef SINGLE_MODE_DMA
210 cpu->set_context_dma(dma);
213 cpu->set_context_debugger(new DEBUGGER(this, emu));
217 io->set_iomap_range_rw(0x00, 0x0f, dma);
218 io->set_iomap_range_rw(0x10, 0x1f, pio);
219 io->set_iomap_range_rw(0x20, 0x2f, fdc);
220 for(int i = 0x30; i < 0x40; i += 2) {
221 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP0 | ((i >> 1) & 1));
223 for(int i = 0x40; i < 0x50; i += 2) {
224 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP1 | ((i >> 1) & 1));
226 io->set_iomap_range_w(0x50, 0x5f, memory);
227 io->set_iomap_range_r(0x60, 0x6f, sysport);
228 io->set_iomap_range_w(0x70, 0x7f, sysport);
229 #if defined(_MZ6500) || defined(_MZ6550)
230 io->set_iomap_single_rw(0xcd, memory);
232 for(int i = 0x100; i < 0x110; i += 2) {
233 io->set_iomap_alias_rw(i, gdc, (i >> 1) & 1);
235 io->set_iomap_range_rw(0x110, 0x17f, display);
236 io->set_iomap_range_rw(0x200, 0x20f, sio);
237 io->set_iomap_range_rw(0x210, 0x21f, ctc0);
238 io->set_iomap_range_rw(0x220, 0x22f, rtc);
239 for(int i = 0x230; i < 0x240; i++) {
240 io->set_iomap_alias_rw(i, psg, ~i & 1);
242 io->set_iomap_range_r(0x240, 0x25f, sysport);
243 io->set_iomap_range_w(0x260, 0x26f, sysport);
244 io->set_iomap_range_r(0x270, 0x27f, sysport);
246 // initialize all devices
247 for(DEVICE* device = first_device; device; device = device->next_device) {
248 device->initialize();
250 for(int i = 0; i < 4; i++) {
251 #if defined(_MZ6500) || defined(_MZ6550)
252 fdc->set_drive_type(i, DRIVE_TYPE_2HD);
254 fdc->set_drive_type(i, DRIVE_TYPE_2D);
261 // delete all devices
262 for(DEVICE* device = first_device; device;) {
263 DEVICE *next_device = device->next_device;
266 device = next_device;
270 DEVICE* VM::get_device(int id)
272 for(DEVICE* device = first_device; device; device = device->next_device) {
273 if(device->this_device_id == id) {
280 // ----------------------------------------------------------------------------
281 // drive virtual machine
282 // ----------------------------------------------------------------------------
287 for(DEVICE* device = first_device; device; device = device->next_device) {
290 not_busy->write_signal(SIG_NOT_INPUT, 0, 0); // busy = low
291 pio->write_signal(SIG_I8255_PORT_B, 0x03, 0x07); // busy = ~(low), pe = ~(low), pdtr = ~(high)
292 pio->write_signal(SIG_I8255_PORT_C, 0x40, 0x40); // ack = high
295 void VM::special_reset()
298 cpu->write_signal(SIG_CPU_NMI, 1, 1);
299 sysport->nmi_reset();
307 double VM::get_frame_rate()
309 return event->get_frame_rate();
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
317 DEVICE *VM::get_cpu(int index)
326 // ----------------------------------------------------------------------------
328 // ----------------------------------------------------------------------------
330 void VM::draw_screen()
332 display->draw_screen();
335 uint32_t VM::get_access_lamp_status()
337 uint32_t status = fdc->read_signal(0);
338 return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
341 // ----------------------------------------------------------------------------
343 // ----------------------------------------------------------------------------
345 void VM::initialize_sound(int rate, int samples)
347 // init sound manager
348 event->initialize_sound(rate, samples);
351 psg->initialize_sound(rate, 4000000, samples, 0, 0);
354 uint16_t* VM::create_sound(int* extra_frames)
356 return event->create_sound(extra_frames);
359 int VM::get_sound_buffer_ptr()
361 return event->get_sound_buffer_ptr();
364 #ifdef USE_SOUND_VOLUME
365 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
368 psg->set_volume(1, decibel_l, decibel_r);
370 #if defined(USE_SOUND_FILES)
372 fdc->set_volume(UPD765A_SND_TYPE_SEEK, decibel_l, decibel_r);
378 // ----------------------------------------------------------------------------
380 // ----------------------------------------------------------------------------
382 void VM::key_down(int code, bool repeat)
384 keyboard->key_down(code);
387 void VM::key_up(int code)
389 // keyboard->key_up(code);
392 // ----------------------------------------------------------------------------
394 // ----------------------------------------------------------------------------
396 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
398 fdc->open_disk(drv, file_path, bank);
401 void VM::close_floppy_disk(int drv)
403 fdc->close_disk(drv);
406 bool VM::is_floppy_disk_inserted(int drv)
408 return fdc->is_disk_inserted(drv);
411 void VM::is_floppy_disk_protected(int drv, bool value)
413 fdc->is_disk_protected(drv, value);
416 bool VM::is_floppy_disk_protected(int drv)
418 return fdc->is_disk_protected(drv);
421 bool VM::is_frame_skippable()
423 return event->is_frame_skippable();
426 void VM::update_config()
428 for(DEVICE* device = first_device; device; device = device->next_device) {
429 device->update_config();
433 #define STATE_VERSION 3
435 void VM::save_state(FILEIO* state_fio)
437 state_fio->FputUint32(STATE_VERSION);
439 for(DEVICE* device = first_device; device; device = device->next_device) {
440 device->save_state(state_fio);
444 bool VM::load_state(FILEIO* state_fio)
446 if(state_fio->FgetUint32() != STATE_VERSION) {
449 for(DEVICE* device = first_device; device; device = device->next_device) {
450 if(!device->load_state(state_fio)) {