2 SHARP MZ-5500 Emulator 'EmuZ-5500'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
22 #include "../rp5c01.h"
23 #include "../upd7220.h"
24 #include "../upd765a.h"
25 #include "../ym2203.h"
26 #include "../z80ctc.h"
27 #include "../z80sio.h"
30 #include "../debugger.h"
38 #include "../../fileio.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 VM::VM(EMU* parent_emu) : emu(parent_emu)
47 first_device = last_device = NULL;
48 dummy = new DEVICE(this, emu); // must be 1st device
49 event = new EVENT(this, emu); // must be 2nd device
51 dma = new I8237(this, emu);
52 pio = new I8255(this, emu);
53 pic = new I8259(this, emu);
54 cpu = new I286(this, emu);
55 io = new IO(this, emu);
56 div = new LS393(this, emu);
57 rtc = new RP5C01(this, emu);
58 gdc = new UPD7220(this, emu);
59 fdc = new UPD765A(this, emu);
60 psg = new YM2203(this, emu); // AY-3-8912
61 ctc0 = new Z80CTC(this, emu);
62 #if defined(_MZ6500) || defined(_MZ6550)
63 ctc1 = new Z80CTC(this, emu);
65 sio = new Z80SIO(this, emu);
67 display = new DISPLAY(this, emu);
68 keyboard = new KEYBOARD(this, emu);
69 memory = new MEMORY(this, emu);
70 sysport = new SYSPORT(this, emu);
73 event->set_context_cpu(cpu);
74 event->set_context_sound(psg);
76 dma->set_context_memory(memory);
77 dma->set_context_ch1(fdc);
78 pio->set_context_port_c(keyboard, SIG_KEYBOARD_INPUT, 3, 0);
79 pic->set_context_cpu(cpu);
80 div->set_context_2qb(ctc0, SIG_Z80CTC_TRIG_3, 1);
81 #if defined(_MZ6500) || defined(_MZ6550)
82 div->set_context_1qb(ctc1, SIG_Z80CTC_TRIG_0, 1);
83 div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_1, 1);
84 div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_2, 1);
85 div->set_context_2qd(ctc1, SIG_Z80CTC_TRIG_3, 1);
87 rtc->set_context_alarm(pic, SIG_I8259_IR0 | SIG_I8259_CHIP1, 1);
88 gdc->set_vram_ptr(memory->get_vram(), 0x80000);
89 gdc->set_context_vsync(pic, SIG_I8259_IR0 | SIG_I8259_CHIP0, 1);
90 fdc->set_context_irq(pic, SIG_I8259_IR1 | SIG_I8259_CHIP1, 1);
91 fdc->set_context_drq(dma, SIG_I8237_CH1, 1);
92 psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP0, 0x20, 0);
93 psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP1, 0x40, 0);
94 psg->set_context_port_a(memory, SIG_MEMORY_BANK, 0xe0, 0);
95 ctc0->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
96 ctc0->set_context_zc0(div, SIG_LS393_CLK, 1);
97 ctc0->set_context_zc1(sio, SIG_Z80SIO_TX_CLK_CH0, 1);
98 ctc0->set_context_zc1(sio, SIG_Z80SIO_RX_CLK_CH0, 1);
99 ctc0->set_context_zc2(sio, SIG_Z80SIO_TX_CLK_CH1, 1);
100 ctc0->set_context_zc2(sio, SIG_Z80SIO_RX_CLK_CH1, 1);
101 #if defined(_MZ6500) || defined(_MZ6550)
102 ctc0->set_context_child(ctc1);
103 ctc1->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
105 sio->set_context_intr(pic, SIG_I8259_IR1 | SIG_I8259_CHIP0);
107 display->set_vram_ptr(memory->get_vram());
108 display->set_sync_ptr(gdc->get_sync());
109 display->set_ra_ptr(gdc->get_ra());
110 display->set_cs_ptr(gdc->get_cs());
111 display->set_ead_ptr(gdc->get_ead());
112 keyboard->set_context_pio(pio);
113 keyboard->set_context_pic(pic);
114 memory->set_context_cpu(cpu);
115 sysport->set_context_fdc(fdc);
116 sysport->set_context_ctc(ctc0);
117 sysport->set_context_sio(sio);
120 cpu->set_context_mem(memory);
121 cpu->set_context_io(io);
122 cpu->set_context_intr(pic);
123 #ifdef SINGLE_MODE_DMA
124 cpu->set_context_dma(dma);
127 cpu->set_context_debugger(new DEBUGGER(this, emu));
131 io->set_iomap_range_rw(0x00, 0x0f, dma);
132 io->set_iomap_range_rw(0x10, 0x1f, pio);
133 io->set_iomap_range_rw(0x20, 0x2f, fdc);
134 for(int i = 0x30; i < 0x40; i += 2) {
135 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP0 | ((i >> 1) & 1));
137 for(int i = 0x40; i < 0x50; i += 2) {
138 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP1 | ((i >> 1) & 1));
140 io->set_iomap_range_w(0x50, 0x5f, memory);
141 io->set_iomap_range_r(0x60, 0x6f, sysport);
142 io->set_iomap_range_w(0x70, 0x7f, sysport);
143 #if defined(_MZ6500) || defined(_MZ6550)
144 io->set_iomap_single_rw(0xcd, memory);
146 for(int i = 0x100; i < 0x110; i += 2) {
147 io->set_iomap_alias_rw(i, gdc, (i >> 1) & 1);
149 io->set_iomap_range_rw(0x110, 0x17f, display);
150 io->set_iomap_range_rw(0x200, 0x20f, sio);
151 io->set_iomap_range_rw(0x210, 0x21f, ctc0);
152 io->set_iomap_range_rw(0x220, 0x22f, rtc);
153 for(int i = 0x230; i < 0x240; i++) {
154 io->set_iomap_alias_rw(i, psg, ~i & 1);
156 io->set_iomap_range_r(0x240, 0x25f, sysport);
157 io->set_iomap_range_w(0x260, 0x26f, sysport);
158 io->set_iomap_range_r(0x270, 0x27f, sysport);
160 // initialize all devices
161 for(DEVICE* device = first_device; device; device = device->next_device) {
162 device->initialize();
164 for(int i = 0; i < 4; i++) {
165 #if defined(_MZ6500) || defined(_MZ6550)
166 fdc->set_drive_type(i, DRIVE_TYPE_2HD);
168 fdc->set_drive_type(i, DRIVE_TYPE_2D);
175 // delete all devices
176 for(DEVICE* device = first_device; device;) {
177 DEVICE *next_device = device->next_device;
180 device = next_device;
184 DEVICE* VM::get_device(int id)
186 for(DEVICE* device = first_device; device; device = device->next_device) {
187 if(device->this_device_id == id) {
194 // ----------------------------------------------------------------------------
195 // drive virtual machine
196 // ----------------------------------------------------------------------------
201 for(DEVICE* device = first_device; device; device = device->next_device) {
206 void VM::special_reset()
209 cpu->write_signal(SIG_CPU_NMI, 1, 1);
210 sysport->nmi_reset();
218 double VM::frame_rate()
220 return event->frame_rate();
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
228 DEVICE *VM::get_cpu(int index)
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 void VM::draw_screen()
243 display->draw_screen();
246 int VM::access_lamp()
248 uint32 status = fdc->read_signal(0);
249 return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 void VM::initialize_sound(int rate, int samples)
258 // init sound manager
259 event->initialize_sound(rate, samples);
262 psg->init(rate, 4000000, samples, 0, 0);
265 uint16* VM::create_sound(int* extra_frames)
267 return event->create_sound(extra_frames);
270 int VM::sound_buffer_ptr()
272 return event->sound_buffer_ptr();
275 // ----------------------------------------------------------------------------
277 // ----------------------------------------------------------------------------
279 void VM::key_down(int code, bool repeat)
281 keyboard->key_down(code);
284 void VM::key_up(int code)
286 // keyboard->key_up(code);
289 // ----------------------------------------------------------------------------
291 // ----------------------------------------------------------------------------
293 void VM::open_disk(int drv, _TCHAR* file_path, int offset)
295 fdc->open_disk(drv, file_path, offset);
298 void VM::close_disk(int drv)
300 fdc->close_disk(drv);
303 bool VM::disk_inserted(int drv)
305 return fdc->disk_inserted(drv);
310 return event->now_skip();
313 void VM::update_config()
315 for(DEVICE* device = first_device; device; device = device->next_device) {
316 device->update_config();
320 #define STATE_VERSION 2
322 void VM::save_state(FILEIO* state_fio)
324 state_fio->FputUint32(STATE_VERSION);
326 for(DEVICE* device = first_device; device; device = device->next_device) {
327 device->save_state(state_fio);
331 bool VM::load_state(FILEIO* state_fio)
333 if(state_fio->FgetUint32() != STATE_VERSION) {
336 for(DEVICE* device = first_device; device; device = device->next_device) {
337 if(!device->load_state(state_fio)) {