2 SHARP MZ-2800 Emulator 'EmuZ-2800'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
20 #include "../mb8877.h"
21 #include "../pcm1bit.h"
22 #include "../rp5c01.h"
23 //#include "../sasi.h"
24 #include "../upd71071.h"
25 #include "../ym2203.h"
26 #include "../z80pio.h"
27 #include "../z80sio.h"
30 #include "../debugger.h"
43 #include "../../fileio.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 cpu = new I286(this, emu);
57 pit = new I8253(this, emu);
58 pio0 = new I8255(this, emu);
59 pic = new I8259(this, emu);
60 io = new IO(this, emu);
61 fdc = new MB8877(this, emu);
62 pcm = new PCM1BIT(this, emu);
63 rtc = new RP5C01(this, emu); // RP-5C15
64 // sasi = new SASI(this, emu);
65 dma = new UPD71071(this, emu);
66 opn = new YM2203(this, emu);
67 pio1 = new Z80PIO(this, emu);
68 sio = new Z80SIO(this, emu);
70 crtc = new CRTC(this, emu);
71 floppy = new FLOPPY(this, emu);
72 joystick = new JOYSTICK(this, emu);
73 keyboard = new KEYBOARD(this, emu);
74 memory = new MEMORY(this, emu);
75 mouse = new MOUSE(this, emu);
76 rst = new RESET(this, emu);
77 serial = new SERIAL(this, emu);
78 sysport = new SYSPORT(this, emu);
81 event->set_context_cpu(cpu);
82 event->set_context_sound(opn);
83 event->set_context_sound(pcm);
85 pit->set_constant_clock(0, 31250);
86 pit->set_constant_clock(2, 31250);
87 pit->set_context_ch0(pit, SIG_I8253_CLOCK_1, 1);
88 pit->set_context_ch0(pic, SIG_I8259_CHIP0 | SIG_I8259_IR0, 1);
89 pit->set_context_ch1(pic, SIG_I8259_CHIP0 | SIG_I8259_IR6, 1);
90 pit->set_context_ch2(pic, SIG_I8259_CHIP1 | SIG_I8259_IR0, 1);
91 pio0->set_context_port_c(rst, SIG_RESET_CONTROL, 0xff, 0);
92 pio0->set_context_port_c(crtc, SIG_CRTC_MASK, 0x01, 0);
93 pio0->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x04, 0);
94 pic->set_context_cpu(cpu);
95 fdc->set_context_drq(dma, SIG_UPD71071_CH1, 1);
96 fdc->set_context_irq(pic, SIG_I8259_CHIP0 | SIG_I8259_IR5, 1);
97 rtc->set_context_alarm(pic, SIG_I8259_CHIP1 | SIG_I8259_IR2, 1);
98 rtc->set_context_pulse(opn, SIG_YM2203_PORT_B, 8);
99 // sasi->set_context_drq(dma, SIG_UPD71071_CH0, 1);
100 // sasi->set_context_irq(pic, SIG_I8259_CHIP0 | SIG_I8259_IR4, 1);
101 dma->set_context_memory(memory);
102 // dma->set_context_ch0(sasi);
103 dma->set_context_ch1(fdc);
104 dma->set_context_tc(pic, SIG_I8259_CHIP0 | SIG_I8259_IR3, 1);
105 opn->set_context_irq(pic, SIG_I8259_CHIP1 | SIG_I8259_IR7, 1);
106 opn->set_context_port_a(crtc, SIG_CRTC_PALLETE, 0x04, 0);
107 opn->set_context_port_a(mouse, SIG_MOUSE_SEL, 0x08, 0);
108 pio1->set_context_port_a(crtc, SIG_CRTC_COLUMN_SIZE, 0x20, 0);
109 pio1->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0xff, 0);
110 sio->set_context_intr(pic, SIG_I8259_CHIP0 | SIG_I8259_IR2);
111 sio->set_context_dtr1(mouse, SIG_MOUSE_DTR, 1);
113 crtc->set_context_pic(pic);
114 crtc->set_context_pio(pio0);
115 crtc->set_vram_ptr(memory->get_vram());
116 crtc->set_tvram_ptr(memory->get_tvram());
117 crtc->set_kanji_ptr(memory->get_kanji());
118 crtc->set_pcg_ptr(memory->get_pcg());
119 floppy->set_context_fdc(fdc);
120 keyboard->set_context_pio0(pio0);
121 keyboard->set_context_pio1(pio1);
122 memory->set_context_crtc(crtc);
123 mouse->set_context_sio(sio);
124 serial->set_context_sio(sio);
125 sysport->set_context_pit(pit);
126 sysport->set_context_sio(sio);
129 cpu->set_context_mem(memory);
130 cpu->set_context_io(io);
131 cpu->set_context_intr(pic);
132 #ifdef SINGLE_MODE_DMA
133 cpu->set_context_dma(dma);
136 cpu->set_context_debugger(new DEBUGGER(this, emu));
140 io->set_iomap_range_rw(0x70, 0x7f, dma);
141 io->set_iomap_alias_rw(0x80, pic, I8259_ADDR_CHIP0 | 0);
142 io->set_iomap_alias_rw(0x81, pic, I8259_ADDR_CHIP0 | 1);
143 io->set_iomap_alias_rw(0x82, pic, I8259_ADDR_CHIP1 | 0);
144 io->set_iomap_alias_rw(0x83, pic, I8259_ADDR_CHIP1 | 1);
145 io->set_iomap_range_rw(0x8c, 0x8d, memory);
146 io->set_iovalue_single_r(0x8e, 0xff); // dipswitch
147 io->set_flipflop_single_rw(0x8f, 0x00); // shut
148 io->set_iomap_range_rw(0xa0, 0xa3, serial);
149 for(uint32 p = 0xae; p <= 0x1fae; p += 0x100) {
150 io->set_iomap_single_w(p, crtc);
152 // io->set_iomap_single_rw(0xaf, sasi);
153 io->set_iomap_range_rw(0xb0, 0xb3, serial);
154 io->set_iomap_single_r(0xbe, sysport);
155 io->set_iomap_range_rw(0xc8, 0xc9, opn);
156 io->set_iovalue_single_r(0xca, 0x7f); // voice communication ???
157 for(uint32 p = 0xcc; p <= 0xfcc; p += 0x100) {
158 io->set_iomap_alias_rw(p, rtc, p >> 8);
160 io->set_iomap_single_w(0xcd, serial);
161 io->set_iomap_single_rw(0xce, memory);
162 io->set_iomap_range_rw(0xd8, 0xdb, fdc);
163 io->set_iomap_range_w(0xdc, 0xdf, floppy);
164 io->set_iomap_range_rw(0xe0, 0xe3, pio0);
165 io->set_iomap_range_rw(0xe4, 0xe7, pit);
166 io->set_iomap_range_rw(0xe8, 0xeb, pio1);
167 io->set_iomap_single_rw(0xef, joystick);
168 // io->set_iomap_range_w(0xf0, 0xf3, sysport);
169 io->set_iomap_single_w(0x170, crtc);
170 io->set_iomap_single_w(0x172, crtc);
171 io->set_iomap_single_w(0x174, crtc);
172 io->set_iomap_single_w(0x176, crtc);
173 io->set_iomap_range_w(0x178, 0x17b, crtc);
174 io->set_iomap_single_w(0x270, crtc);
175 io->set_iomap_single_w(0x272, crtc);
176 io->set_iomap_single_rw(0x274, memory);
178 // initialize all devices
179 for(DEVICE* device = first_device; device; device = device->next_device) {
180 device->initialize();
186 // delete all devices
187 for(DEVICE* device = first_device; device;) {
188 DEVICE *next_device = device->next_device;
191 device = next_device;
195 DEVICE* VM::get_device(int id)
197 for(DEVICE* device = first_device; device; device = device->next_device) {
198 if(device->this_device_id == id) {
205 // ----------------------------------------------------------------------------
206 // drive virtual machine
207 // ----------------------------------------------------------------------------
212 for(DEVICE* device = first_device; device; device = device->next_device) {
216 for(DEVICE* device = first_device; device; device = device->next_device) {
220 // set initial port status
221 pio0->write_signal(SIG_I8255_PORT_B, 0x7c, 0xff);
222 opn->write_signal(SIG_YM2203_PORT_B, 0x37, 0xff);
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
240 DEVICE *VM::get_cpu(int index)
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 void VM::draw_screen()
258 int VM::access_lamp()
260 uint32 status = fdc->read_signal(0);
261 return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
264 // ----------------------------------------------------------------------------
266 // ----------------------------------------------------------------------------
268 void VM::initialize_sound(int rate, int samples)
270 // init sound manager
271 event->initialize_sound(rate, samples);
274 opn->init(rate, 2000000, samples, 0, -8);
275 pcm->init(rate, 4096);
278 uint16* VM::create_sound(int* extra_frames)
280 return event->create_sound(extra_frames);
283 int VM::sound_buffer_ptr()
285 return event->sound_buffer_ptr();
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
292 void VM::open_disk(int drv, _TCHAR* file_path, int offset)
294 fdc->open_disk(drv, file_path, offset);
297 void VM::close_disk(int drv)
299 fdc->close_disk(drv);
302 bool VM::disk_inserted(int drv)
304 return fdc->disk_inserted(drv);
309 return event->now_skip();
312 void VM::update_config()
314 for(DEVICE* device = first_device; device; device = device->next_device) {
315 device->update_config();
319 #define STATE_VERSION 2
321 void VM::save_state(FILEIO* state_fio)
323 state_fio->FputUint32(STATE_VERSION);
325 for(DEVICE* device = first_device; device; device = device->next_device) {
326 device->save_state(state_fio);
330 bool VM::load_state(FILEIO* state_fio)
332 if(state_fio->FgetUint32() != STATE_VERSION) {
335 for(DEVICE* device = first_device; device; device = device->next_device) {
336 if(!device->load_state(state_fio)) {