2 SHARP MZ-5500 Emulator 'EmuZ-5500'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
22 #include "../mz1p17.h"
24 #include "../prnfile.h"
25 #include "../rp5c01.h"
26 #include "../upd7220.h"
27 #include "../upd765a.h"
28 #include "../ym2203.h"
29 #include "../z80ctc.h"
30 #include "../z80sio.h"
33 #include "../debugger.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 VM::VM(EMU* parent_emu) : emu(parent_emu)
48 first_device = last_device = NULL;
49 dummy = new DEVICE(this, emu); // must be 1st device
50 event = new EVENT(this, emu); // must be 2nd device
52 dummy->set_device_name(_T("1st Dummy"));
53 event->set_device_name(_T("EVENT"));
56 if(config.printer_device_type == 0) {
57 printer = new PRNFILE(this, emu);
58 } else if(config.printer_device_type == 1) {
59 printer = new MZ1P17(this, emu);
63 dma = new I8237(this, emu);
64 pio = new I8255(this, emu);
65 pic = new I8259(this, emu);
66 cpu = new I286(this, emu);
67 io = new IO(this, emu);
68 div = new LS393(this, emu);
70 io->set_device_name(_T("MAIN I/O"));
72 cpu->set_device_name(_T("MAIN CPU(i286)"));
74 cpu->set_device_name(_T("MAIN CPU(i8086)"));
76 div->set_device_name(_T("74LS393(DIVIDER)"));
78 not_data1 = new NOT(this, emu);
79 not_data2 = new NOT(this, emu);
80 not_data3 = new NOT(this, emu);
81 not_data4 = new NOT(this, emu);
82 not_data5 = new NOT(this, emu);
83 not_data6 = new NOT(this, emu);
84 not_data7 = new NOT(this, emu);
85 not_data8 = new NOT(this, emu);
86 not_busy = new NOT(this, emu);
88 not_data1->set_device_name(_T("NOT GATE #1"));
89 not_data2->set_device_name(_T("NOT GATE #2"));
90 not_data3->set_device_name(_T("NOT GATE #3"));
91 not_data4->set_device_name(_T("NOT GATE #4"));
92 not_data5->set_device_name(_T("NOT GATE #5"));
93 not_data6->set_device_name(_T("NOT GATE #6"));
94 not_data7->set_device_name(_T("NOT GATE #7"));
95 not_data8->set_device_name(_T("NOT GATE #8"));
96 not_busy->set_device_name(_T("NOT GATE(PRINTER BUSY)"));
98 rtc = new RP5C01(this, emu);
99 gdc = new UPD7220(this, emu);
100 fdc = new UPD765A(this, emu);
101 psg = new YM2203(this, emu); // AY-3-8912
102 ctc0 = new Z80CTC(this, emu);
103 #if defined(_MZ6500) || defined(_MZ6550)
104 ctc1 = new Z80CTC(this, emu);
106 sio = new Z80SIO(this, emu);
108 display = new DISPLAY(this, emu);
109 keyboard = new KEYBOARD(this, emu);
110 memory = new MEMORY(this, emu);
111 sysport = new SYSPORT(this, emu);
113 display->set_device_name(_T("DISPLAY I/F"));
114 keyboard->set_device_name(_T("KEYBOARD I/F"));
115 memory->set_device_name(_T("MEMORY"));
116 sysport->set_device_name(_T("SYSTEM PORT"));
119 event->set_context_cpu(cpu);
120 event->set_context_sound(psg);
121 #if defined(USE_SOUND_FILES)
122 if(fdc->load_sound_data(UPD765A_SND_TYPE_SEEK, _T("FDDSEEK.WAV"))) {
123 event->set_context_sound(fdc);
127 if(config.printer_device_type == 0) {
128 PRNFILE *prnfile = (PRNFILE *)printer;
129 prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
130 prnfile->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
131 } else if(config.printer_device_type == 1) {
132 MZ1P17 *mz1p17 = (MZ1P17 *)printer;
133 mz1p17->mode = MZ1P17_MODE_MZ1;
134 mz1p17->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
135 mz1p17->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
137 dma->set_context_memory(memory);
138 dma->set_context_ch1(fdc);
139 pio->set_context_port_a(not_data1, SIG_NOT_INPUT, 0x01, 0);
140 pio->set_context_port_a(not_data2, SIG_NOT_INPUT, 0x02, 0);
141 pio->set_context_port_a(not_data3, SIG_NOT_INPUT, 0x04, 0);
142 pio->set_context_port_a(not_data4, SIG_NOT_INPUT, 0x08, 0);
143 pio->set_context_port_a(not_data5, SIG_NOT_INPUT, 0x10, 0);
144 pio->set_context_port_a(not_data6, SIG_NOT_INPUT, 0x20, 0);
145 pio->set_context_port_a(not_data7, SIG_NOT_INPUT, 0x40, 0);
146 pio->set_context_port_a(not_data8, SIG_NOT_INPUT, 0x80, 0);
147 pio->set_context_port_c(keyboard, SIG_KEYBOARD_INPUT, 0x03, 0);
148 pio->set_context_port_c(pic, SIG_I8259_IR2 | SIG_I8259_CHIP0, 0x08, 0);
149 pio->set_context_port_c(printer, SIG_PRINTER_STROBE, 0x20, 0);
150 pic->set_context_cpu(cpu);
151 div->set_context_2qb(ctc0, SIG_Z80CTC_TRIG_3, 1);
152 #if defined(_MZ6500) || defined(_MZ6550)
153 div->set_context_1qb(ctc1, SIG_Z80CTC_TRIG_0, 1);
154 div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_1, 1);
155 div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_2, 1);
156 div->set_context_2qd(ctc1, SIG_Z80CTC_TRIG_3, 1);
158 rtc->set_context_alarm(pic, SIG_I8259_IR0 | SIG_I8259_CHIP1, 1);
159 gdc->set_vram_ptr(memory->get_vram(), 0x80000);
160 gdc->set_context_vsync(pic, SIG_I8259_IR0 | SIG_I8259_CHIP0, 1);
161 fdc->set_context_irq(pic, SIG_I8259_IR1 | SIG_I8259_CHIP1, 1);
162 fdc->set_context_drq(dma, SIG_I8237_CH1, 1);
163 psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP0, 0x20, 0);
164 psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP1, 0x40, 0);
165 psg->set_context_port_a(memory, SIG_MEMORY_BANK, 0xe0, 0);
166 ctc0->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
167 ctc0->set_context_zc0(div, SIG_LS393_CLK, 1);
168 ctc0->set_context_zc1(sio, SIG_Z80SIO_TX_CLK_CH0, 1);
169 ctc0->set_context_zc1(sio, SIG_Z80SIO_RX_CLK_CH0, 1);
170 ctc0->set_context_zc2(sio, SIG_Z80SIO_TX_CLK_CH1, 1);
171 ctc0->set_context_zc2(sio, SIG_Z80SIO_RX_CLK_CH1, 1);
172 #if defined(_MZ6500) || defined(_MZ6550)
173 ctc0->set_context_child(ctc1);
174 ctc1->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
176 sio->set_context_intr(pic, SIG_I8259_IR1 | SIG_I8259_CHIP0);
178 not_data1->set_context_out(printer, SIG_PRINTER_DATA, 0x01);
179 not_data2->set_context_out(printer, SIG_PRINTER_DATA, 0x02);
180 not_data3->set_context_out(printer, SIG_PRINTER_DATA, 0x04);
181 not_data4->set_context_out(printer, SIG_PRINTER_DATA, 0x08);
182 not_data5->set_context_out(printer, SIG_PRINTER_DATA, 0x10);
183 not_data6->set_context_out(printer, SIG_PRINTER_DATA, 0x20);
184 not_data7->set_context_out(printer, SIG_PRINTER_DATA, 0x40);
185 not_data8->set_context_out(printer, SIG_PRINTER_DATA, 0x80);
186 not_busy->set_context_out(pio, SIG_I8255_PORT_B, 0x01);
187 display->set_vram_ptr(memory->get_vram());
188 display->set_sync_ptr(gdc->get_sync());
189 display->set_ra_ptr(gdc->get_ra());
190 display->set_cs_ptr(gdc->get_cs());
191 display->set_ead_ptr(gdc->get_ead());
192 keyboard->set_context_pio(pio);
193 keyboard->set_context_pic(pic);
194 memory->set_context_cpu(cpu);
195 sysport->set_context_fdc(fdc);
196 sysport->set_context_ctc(ctc0);
197 sysport->set_context_sio(sio);
200 cpu->set_context_mem(memory);
201 cpu->set_context_io(io);
202 cpu->set_context_intr(pic);
203 #ifdef SINGLE_MODE_DMA
204 cpu->set_context_dma(dma);
207 cpu->set_context_debugger(new DEBUGGER(this, emu));
211 io->set_iomap_range_rw(0x00, 0x0f, dma);
212 io->set_iomap_range_rw(0x10, 0x1f, pio);
213 io->set_iomap_range_rw(0x20, 0x2f, fdc);
214 for(int i = 0x30; i < 0x40; i += 2) {
215 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP0 | ((i >> 1) & 1));
217 for(int i = 0x40; i < 0x50; i += 2) {
218 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP1 | ((i >> 1) & 1));
220 io->set_iomap_range_w(0x50, 0x5f, memory);
221 io->set_iomap_range_r(0x60, 0x6f, sysport);
222 io->set_iomap_range_w(0x70, 0x7f, sysport);
223 #if defined(_MZ6500) || defined(_MZ6550)
224 io->set_iomap_single_rw(0xcd, memory);
226 for(int i = 0x100; i < 0x110; i += 2) {
227 io->set_iomap_alias_rw(i, gdc, (i >> 1) & 1);
229 io->set_iomap_range_rw(0x110, 0x17f, display);
230 io->set_iomap_range_rw(0x200, 0x20f, sio);
231 io->set_iomap_range_rw(0x210, 0x21f, ctc0);
232 io->set_iomap_range_rw(0x220, 0x22f, rtc);
233 for(int i = 0x230; i < 0x240; i++) {
234 io->set_iomap_alias_rw(i, psg, ~i & 1);
236 io->set_iomap_range_r(0x240, 0x25f, sysport);
237 io->set_iomap_range_w(0x260, 0x26f, sysport);
238 io->set_iomap_range_r(0x270, 0x27f, sysport);
240 // initialize all devices
241 for(DEVICE* device = first_device; device; device = device->next_device) {
242 device->initialize();
244 for(int i = 0; i < 4; i++) {
245 #if defined(_MZ6500) || defined(_MZ6550)
246 fdc->set_drive_type(i, DRIVE_TYPE_2HD);
248 fdc->set_drive_type(i, DRIVE_TYPE_2D);
255 // delete all devices
256 for(DEVICE* device = first_device; device;) {
257 DEVICE *next_device = device->next_device;
260 device = next_device;
264 DEVICE* VM::get_device(int id)
266 for(DEVICE* device = first_device; device; device = device->next_device) {
267 if(device->this_device_id == id) {
274 // ----------------------------------------------------------------------------
275 // drive virtual machine
276 // ----------------------------------------------------------------------------
281 for(DEVICE* device = first_device; device; device = device->next_device) {
284 not_busy->write_signal(SIG_NOT_INPUT, 0, 0); // busy = low
285 pio->write_signal(SIG_I8255_PORT_B, 0x03, 0x07); // busy = ~(low), pe = ~(low), pdtr = ~(high)
286 pio->write_signal(SIG_I8255_PORT_C, 0x40, 0x40); // ack = high
289 void VM::special_reset()
292 cpu->write_signal(SIG_CPU_NMI, 1, 1);
293 sysport->nmi_reset();
301 double VM::get_frame_rate()
303 return event->get_frame_rate();
306 // ----------------------------------------------------------------------------
308 // ----------------------------------------------------------------------------
311 DEVICE *VM::get_cpu(int index)
320 // ----------------------------------------------------------------------------
322 // ----------------------------------------------------------------------------
324 void VM::draw_screen()
326 display->draw_screen();
329 uint32_t VM::get_access_lamp_status()
331 uint32_t status = fdc->read_signal(0);
332 return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
335 // ----------------------------------------------------------------------------
337 // ----------------------------------------------------------------------------
339 void VM::initialize_sound(int rate, int samples)
341 // init sound manager
342 event->initialize_sound(rate, samples);
345 psg->initialize_sound(rate, 4000000, samples, 0, 0);
348 uint16_t* VM::create_sound(int* extra_frames)
350 return event->create_sound(extra_frames);
353 int VM::get_sound_buffer_ptr()
355 return event->get_sound_buffer_ptr();
358 #ifdef USE_SOUND_VOLUME
359 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
362 psg->set_volume(1, decibel_l, decibel_r);
364 #if defined(USE_SOUND_FILES)
366 fdc->set_volume(UPD765A_SND_TYPE_SEEK, decibel_l, decibel_r);
372 // ----------------------------------------------------------------------------
374 // ----------------------------------------------------------------------------
376 void VM::key_down(int code, bool repeat)
378 keyboard->key_down(code);
381 void VM::key_up(int code)
383 // keyboard->key_up(code);
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
390 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
392 fdc->open_disk(drv, file_path, bank);
395 void VM::close_floppy_disk(int drv)
397 fdc->close_disk(drv);
400 bool VM::is_floppy_disk_inserted(int drv)
402 return fdc->is_disk_inserted(drv);
405 void VM::is_floppy_disk_protected(int drv, bool value)
407 fdc->is_disk_protected(drv, value);
410 bool VM::is_floppy_disk_protected(int drv)
412 return fdc->is_disk_protected(drv);
415 bool VM::is_frame_skippable()
417 return event->is_frame_skippable();
420 void VM::update_config()
422 for(DEVICE* device = first_device; device; device = device->next_device) {
423 device->update_config();
427 #define STATE_VERSION 2
429 void VM::save_state(FILEIO* state_fio)
431 state_fio->FputUint32(STATE_VERSION);
433 for(DEVICE* device = first_device; device; device = device->next_device) {
434 device->save_state(state_fio);
438 bool VM::load_state(FILEIO* state_fio)
440 if(state_fio->FgetUint32() != STATE_VERSION) {
443 for(DEVICE* device = first_device; device; device = device->next_device) {
444 if(!device->load_state(state_fio)) {