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 "../mz1p17.h"
24 #include "../pcm1bit.h"
25 //#include "../pcpr201.h"
26 #include "../prnfile.h"
27 #include "../rp5c01.h"
28 //#include "../sasi.h"
29 #include "../upd71071.h"
30 #include "../ym2203.h"
31 #include "../z80pio.h"
32 #include "../z80sio.h"
35 #include "../debugger.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
56 first_device = last_device = NULL;
57 dummy = new DEVICE(this, emu); // must be 1st device
58 event = new EVENT(this, emu); // must be 2nd device
59 dummy->set_device_name(_T("1st Dummy"));
61 cpu = new I286(this, emu);
62 pit = new I8253(this, emu);
63 pio0 = new I8255(this, emu);
64 pic = new I8259(this, emu);
65 io = new IO(this, emu);
66 fdc = new MB8877(this, emu);
67 fdc->set_context_noise_seek(new NOISE(this, emu));
68 fdc->set_context_noise_head_down(new NOISE(this, emu));
69 fdc->set_context_noise_head_up(new NOISE(this, emu));
70 not_busy = new NOT(this, emu);
71 not_busy->set_device_name(_T("NOT GATE(PRINTER BUSY)"));
73 pcm = new PCM1BIT(this, emu);
74 rtc = new RP5C01(this, emu); // RP-5C15
75 // sasi = new SASI(this, emu);
76 dma = new UPD71071(this, emu);
77 opn = new YM2203(this, emu);
78 pio1 = new Z80PIO(this, emu);
79 sio = new Z80SIO(this, emu);
81 crtc = new CRTC(this, emu);
82 floppy = new FLOPPY(this, emu);
83 joystick = new JOYSTICK(this, emu);
84 keyboard = new KEYBOARD(this, emu);
85 memory = new MEMORY(this, emu);
86 mouse = new MOUSE(this, emu);
87 printer = new PRINTER(this, emu);
88 rst = new RESET(this, emu);
89 serial = new SERIAL(this, emu);
90 sysport = new SYSPORT(this, emu);
93 event->set_context_cpu(cpu);
94 event->set_context_sound(opn);
95 event->set_context_sound(pcm);
96 event->set_context_sound(fdc->get_context_noise_seek());
97 event->set_context_sound(fdc->get_context_noise_head_down());
98 event->set_context_sound(fdc->get_context_noise_head_up());
100 pit->set_constant_clock(0, 31250);
101 pit->set_constant_clock(2, 31250);
102 pit->set_context_ch0(pit, SIG_I8253_CLOCK_1, 1);
103 pit->set_context_ch0(pic, SIG_I8259_CHIP0 | SIG_I8259_IR0, 1);
104 pit->set_context_ch1(pic, SIG_I8259_CHIP0 | SIG_I8259_IR6, 1);
105 pit->set_context_ch2(pic, SIG_I8259_CHIP1 | SIG_I8259_IR0, 1);
106 pio0->set_context_port_c(rst, SIG_RESET_CONTROL, 0xff, 0);
107 pio0->set_context_port_c(crtc, SIG_CRTC_MASK, 0x01, 0);
108 pio0->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x04, 0);
109 // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
110 //pcm->set_realtime_render(true);
113 pic->set_context_cpu(cpu);
114 fdc->set_context_drq(dma, SIG_UPD71071_CH1, 1);
115 fdc->set_context_irq(pic, SIG_I8259_CHIP0 | SIG_I8259_IR5, 1);
116 not_busy->set_context_out(pic, SIG_I8259_CHIP1 | SIG_I8259_IR1, 1);
117 rtc->set_context_alarm(pic, SIG_I8259_CHIP1 | SIG_I8259_IR2, 1);
118 rtc->set_context_pulse(opn, SIG_YM2203_PORT_B, 8);
119 // sasi->set_context_drq(dma, SIG_UPD71071_CH0, 1);
120 // sasi->set_context_irq(pic, SIG_I8259_CHIP0 | SIG_I8259_IR4, 1);
121 dma->set_context_memory(memory);
122 // dma->set_context_ch0(sasi);
123 dma->set_context_ch1(fdc);
124 dma->set_context_tc(pic, SIG_I8259_CHIP0 | SIG_I8259_IR3, 1);
125 opn->set_context_irq(pic, SIG_I8259_CHIP1 | SIG_I8259_IR7, 1);
126 opn->set_context_port_a(crtc, SIG_CRTC_PALLETE, 0x04, 0);
127 opn->set_context_port_a(mouse, SIG_MOUSE_SEL, 0x08, 0);
128 pio1->set_context_port_a(crtc, SIG_CRTC_COLUMN_SIZE, 0x20, 0);
129 pio1->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0xff, 0);
130 sio->set_context_intr(pic, SIG_I8259_CHIP0 | SIG_I8259_IR2);
131 sio->set_context_dtr(1, mouse, SIG_MOUSE_DTR, 1);
133 crtc->set_context_pic(pic);
134 crtc->set_context_pio(pio0);
135 crtc->set_vram_ptr(memory->get_vram());
136 crtc->set_tvram_ptr(memory->get_tvram());
137 crtc->set_kanji_ptr(memory->get_kanji());
138 crtc->set_pcg_ptr(memory->get_pcg());
139 floppy->set_context_fdc(fdc);
140 keyboard->set_context_pio0(pio0);
141 keyboard->set_context_pio1(pio1);
142 memory->set_context_crtc(crtc);
143 mouse->set_context_sio(sio);
144 if(config.printer_type == 0) {
145 PRNFILE *prnfile = new PRNFILE(this, emu);
146 prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
147 printer->set_context_prn(prnfile);
148 } else if(config.printer_type == 1) {
149 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
150 mz1p17->mode = MZ1P17_MODE_MZ1;
151 mz1p17->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
152 printer->set_context_prn(mz1p17);
153 // } else if(config.printer_type == 2) {
154 // PCPR201 *pcpr201 = new PCPR201(this, emu);
155 // pcpr201->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
156 // printer->set_context_prn(pcpr201);
158 printer->set_context_prn(dummy);
160 serial->set_context_sio(sio);
161 sysport->set_context_pit(pit);
162 sysport->set_context_sio(sio);
165 cpu->set_context_mem(memory);
166 cpu->set_context_io(io);
167 cpu->set_context_intr(pic);
168 #ifdef SINGLE_MODE_DMA
169 cpu->set_context_dma(dma);
172 cpu->set_context_debugger(new DEBUGGER(this, emu));
176 io->set_iomap_range_rw(0x70, 0x7f, dma);
177 io->set_iomap_alias_rw(0x80, pic, I8259_ADDR_CHIP0 | 0);
178 io->set_iomap_alias_rw(0x81, pic, I8259_ADDR_CHIP0 | 1);
179 io->set_iomap_alias_rw(0x82, pic, I8259_ADDR_CHIP1 | 0);
180 io->set_iomap_alias_rw(0x83, pic, I8259_ADDR_CHIP1 | 1);
181 io->set_iomap_range_rw(0x8c, 0x8d, memory);
182 io->set_iovalue_single_r(0x8e, 0xff); // dipswitch
183 io->set_flipflop_single_rw(0x8f, 0x00); // shut
184 io->set_iomap_range_rw(0xa0, 0xa3, serial);
185 for(uint32_t p = 0xae; p <= 0x1fae; p += 0x100) {
186 io->set_iomap_single_w(p, crtc);
188 // io->set_iomap_single_rw(0xaf, sasi);
189 io->set_iomap_range_rw(0xb0, 0xb3, serial);
190 io->set_iomap_single_r(0xbe, sysport);
191 io->set_iomap_range_rw(0xc8, 0xc9, opn);
192 io->set_iovalue_single_r(0xca, 0x7f); // voice communication ???
193 for(uint32_t p = 0xcc; p <= 0xfcc; p += 0x100) {
194 io->set_iomap_alias_rw(p, rtc, p >> 8);
196 io->set_iomap_single_w(0xcd, serial);
197 io->set_iomap_single_rw(0xce, memory);
198 io->set_iomap_range_rw(0xd8, 0xdb, fdc);
199 io->set_iomap_range_w(0xdc, 0xdf, floppy);
200 io->set_iomap_range_rw(0xe0, 0xe3, pio0);
201 io->set_iomap_range_rw(0xe4, 0xe7, pit);
202 io->set_iomap_range_rw(0xe8, 0xeb, pio1);
203 io->set_iomap_single_rw(0xef, joystick);
204 // io->set_iomap_range_w(0xf0, 0xf3, sysport);
205 io->set_iomap_single_w(0x170, crtc);
206 io->set_iomap_single_w(0x172, crtc);
207 io->set_iomap_single_w(0x174, crtc);
208 io->set_iomap_single_w(0x176, crtc);
209 io->set_iomap_range_w(0x178, 0x17b, crtc);
210 io->set_iomap_range_rw(0x1fe, 0x1ff, printer);
211 io->set_iomap_single_w(0x270, crtc);
212 io->set_iomap_single_w(0x272, crtc);
213 io->set_iomap_single_rw(0x274, memory);
215 // initialize all devices
216 for(DEVICE* device = first_device; device; device = device->next_device) {
217 device->initialize();
224 // delete all devices
225 for(DEVICE* device = first_device; device;) {
226 DEVICE *next_device = device->next_device;
229 device = next_device;
233 DEVICE* VM::get_device(int id)
235 for(DEVICE* device = first_device; device; device = device->next_device) {
236 if(device->this_device_id == id) {
243 // ----------------------------------------------------------------------------
244 // drive virtual machine
245 // ----------------------------------------------------------------------------
250 for(DEVICE* device = first_device; device; device = device->next_device) {
254 for(DEVICE* device = first_device; device; device = device->next_device) {
258 // set initial port status
259 pio0->write_signal(SIG_I8255_PORT_B, 0x7c, 0xff);
260 opn->write_signal(SIG_YM2203_PORT_B, 0x37, 0xff);
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
278 DEVICE *VM::get_cpu(int index)
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
291 void VM::draw_screen()
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
300 void VM::initialize_sound(int rate, int samples)
302 // init sound manager
303 event->initialize_sound(rate, samples);
306 opn->initialize_sound(rate, 2000000, samples, 0, -8);
307 pcm->initialize_sound(rate, 4096);
310 uint16_t* VM::create_sound(int* extra_frames)
312 return event->create_sound(extra_frames);
315 int VM::get_sound_buffer_ptr()
317 return event->get_sound_buffer_ptr();
320 #ifdef USE_SOUND_VOLUME
321 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
324 opn->set_volume(0, decibel_l, decibel_r);
326 opn->set_volume(1, decibel_l, decibel_r);
328 pcm->set_volume(0, decibel_l, decibel_r);
330 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
331 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
332 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
337 // ----------------------------------------------------------------------------
339 // ----------------------------------------------------------------------------
341 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
343 fdc->open_disk(drv, file_path, bank);
346 void VM::close_floppy_disk(int drv)
348 fdc->close_disk(drv);
351 bool VM::is_floppy_disk_inserted(int drv)
353 return fdc->is_disk_inserted(drv);
356 void VM::is_floppy_disk_protected(int drv, bool value)
358 fdc->is_disk_protected(drv, value);
361 bool VM::is_floppy_disk_protected(int drv)
363 return fdc->is_disk_protected(drv);
366 uint32_t VM::is_floppy_disk_accessed()
368 return fdc->read_signal(0);
371 bool VM::is_frame_skippable()
373 return event->is_frame_skippable();
376 void VM::update_config()
378 for(DEVICE* device = first_device; device; device = device->next_device) {
379 device->update_config();
383 #define STATE_VERSION 4
385 #include "../../statesub.h"
386 #include "../../qt/gui/csp_logger.h"
387 extern CSP_Logger DLL_PREFIX_I *csp_logger;
389 void VM::decl_state(void)
391 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MZ_2800_HEAD")), csp_logger);
392 for(DEVICE* device = first_device; device; device = device->next_device) {
393 device->decl_state();
397 void VM::save_state(FILEIO* state_fio)
399 //state_fio->FputUint32(STATE_VERSION);
401 if(state_entry != NULL) {
402 state_entry->save_state(state_fio);
404 for(DEVICE* device = first_device; device; device = device->next_device) {
405 device->save_state(state_fio);
409 bool VM::load_state(FILEIO* state_fio)
411 //if(state_fio->FgetUint32() != STATE_VERSION) {
415 if(state_entry != NULL) {
416 mb = state_entry->load_state(state_fio);
419 emu->out_debug_log("INFO: HEADER DATA ERROR");
422 for(DEVICE* device = first_device; device; device = device->next_device) {
423 if(!device->load_state(state_fio)) {