2 SHARP MZ-2800 Emulator 'EmuZ-2800'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
16 #include "../harddisk.h"
22 #include "../mb8877.h"
23 #include "../mz1p17.h"
26 #include "../pcm1bit.h"
27 //#include "../pcpr201.h"
28 #include "../prnfile.h"
29 #include "../rp5c01.h"
30 #include "../scsi_hdd.h"
31 #include "../scsi_host.h"
32 #include "../upd71071.h"
33 #include "../ym2203.h"
34 #include "../z80pio.h"
35 #include "../z80sio.h"
38 #include "../debugger.h"
55 using MZ2800::JOYSTICK;
56 using MZ2800::KEYBOARD;
59 using MZ2800::PRINTER;
63 using MZ2800::SYSPORT;
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
72 first_device = last_device = NULL;
73 dummy = new DEVICE(this, emu); // must be 1st device
74 event = new EVENT(this, emu); // must be 2nd device
75 dummy->set_device_name(_T("1st Dummy"));
77 cpu = new I286(this, emu);
78 pit = new I8253(this, emu);
79 pio0 = new I8255(this, emu);
80 pic = new I8259(this, emu);
81 io = new IO(this, emu);
82 fdc = new MB8877(this, emu);
83 fdc->set_context_noise_seek(new NOISE(this, emu));
84 fdc->set_context_noise_head_down(new NOISE(this, emu));
85 fdc->set_context_noise_head_up(new NOISE(this, emu));
86 not_busy = new NOT(this, emu);
87 not_busy->set_device_name(_T("NOT GATE(PRINTER BUSY)"));
89 pcm = new PCM1BIT(this, emu);
90 rtc = new RP5C01(this, emu); // RP-5C15
91 sasi_host = new SCSI_HOST(this, emu);
92 for(int i = 0; i < USE_HARD_DISK; i++) {
93 sasi_hdd[i] = new SASI_HDD(this, emu);
94 sasi_hdd[i]->set_device_name(_T("SASI Hard Disk Drive #%d"), i + 1);
95 sasi_hdd[i]->scsi_id = i;
96 sasi_hdd[i]->set_disk_handler(0, new HARDDISK(emu));
97 sasi_hdd[i]->set_context_interface(sasi_host);
98 sasi_hdd[i]->bytes_per_sec = 32 * 1024; // 32KB/s
99 sasi_host->set_context_target(sasi_hdd[i]);
101 dma = new UPD71071(this, emu);
102 opn = new YM2203(this, emu);
103 pio1 = new Z80PIO(this, emu);
104 sio = new Z80SIO(this, emu);
106 crtc = new CRTC(this, emu);
107 floppy = new FLOPPY(this, emu);
108 joystick = new JOYSTICK(this, emu);
109 keyboard = new KEYBOARD(this, emu);
110 memory = new MEMORY(this, emu);
111 mouse = new MOUSE(this, emu);
112 printer = new PRINTER(this, emu);
113 rst = new RESET(this, emu);
114 sasi = new SASI(this, emu);
115 serial = new SERIAL(this, emu);
116 sysport = new SYSPORT(this, emu);
119 event->set_context_cpu(cpu);
120 event->set_context_sound(opn);
121 event->set_context_sound(pcm);
122 event->set_context_sound(fdc->get_context_noise_seek());
123 event->set_context_sound(fdc->get_context_noise_head_down());
124 event->set_context_sound(fdc->get_context_noise_head_up());
126 pit->set_constant_clock(0, 31250);
127 pit->set_constant_clock(2, 31250);
128 pit->set_context_ch0(pit, SIG_I8253_CLOCK_1, 1);
129 pit->set_context_ch0(pic, SIG_I8259_CHIP0 | SIG_I8259_IR0, 1);
130 pit->set_context_ch1(pic, SIG_I8259_CHIP0 | SIG_I8259_IR6, 1);
131 pit->set_context_ch2(pic, SIG_I8259_CHIP1 | SIG_I8259_IR0, 1);
132 pio0->set_context_port_c(rst, SIG_RESET_CONTROL, 0xff, 0);
133 pio0->set_context_port_c(crtc, SIG_CRTC_MASK, 0x01, 0);
134 pio0->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x04, 0);
135 // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
136 //pcm->set_realtime_render(true);
139 pic->set_context_cpu(cpu);
140 fdc->set_context_drq(dma, SIG_UPD71071_CH1, 1);
141 fdc->set_context_irq(pic, SIG_I8259_CHIP0 | SIG_I8259_IR5, 1);
142 not_busy->set_context_out(pic, SIG_I8259_CHIP1 | SIG_I8259_IR1, 1);
143 rtc->set_context_alarm(pic, SIG_I8259_CHIP1 | SIG_I8259_IR2, 1);
144 rtc->set_context_pulse(opn, SIG_YM2203_PORT_B, 8);
145 sasi_host->set_context_irq(sasi, SIG_SASI_IRQ, 1);
146 sasi_host->set_context_drq(sasi, SIG_SASI_DRQ, 1);
147 dma->set_context_memory(memory);
148 dma->set_context_ch0(sasi);
149 dma->set_context_ch1(fdc);
150 dma->set_context_tc(pic, SIG_I8259_CHIP0 | SIG_I8259_IR3, 1);
151 opn->set_context_irq(pic, SIG_I8259_CHIP1 | SIG_I8259_IR7, 1);
152 opn->set_context_port_a(crtc, SIG_CRTC_PALLETE, 0x04, 0);
153 opn->set_context_port_a(mouse, SIG_MOUSE_SEL, 0x08, 0);
154 pio1->set_context_port_a(crtc, SIG_CRTC_COLUMN_SIZE, 0x20, 0);
155 pio1->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0xff, 0);
156 sio->set_context_intr(pic, SIG_I8259_CHIP0 | SIG_I8259_IR2);
157 sio->set_context_dtr(1, mouse, SIG_MOUSE_DTR, 1);
159 crtc->set_context_pic(pic);
160 crtc->set_context_pio(pio0);
161 crtc->set_vram_ptr(memory->get_vram());
162 crtc->set_tvram_ptr(memory->get_tvram());
163 crtc->set_kanji_ptr(memory->get_kanji());
164 crtc->set_pcg_ptr(memory->get_pcg());
165 floppy->set_context_fdc(fdc);
166 keyboard->set_context_pio0(pio0);
167 keyboard->set_context_pio1(pio1);
168 memory->set_context_crtc(crtc);
169 mouse->set_context_sio(sio);
170 if(config.printer_type == 0) {
171 PRNFILE *prnfile = new PRNFILE(this, emu);
172 prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
173 printer->set_context_prn(prnfile);
174 } else if(config.printer_type == 1) {
175 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
176 mz1p17->mode = MZ1P17_MODE_MZ1;
177 mz1p17->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
178 printer->set_context_prn(mz1p17);
179 // } else if(config.printer_type == 2) {
180 // PCPR201 *pcpr201 = new PCPR201(this, emu);
181 // pcpr201->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
182 // printer->set_context_prn(pcpr201);
184 printer->set_context_prn(dummy);
186 sasi->set_context_host(sasi_host);
187 sasi->set_context_dma(dma);
188 sasi->set_context_pic(pic);
189 serial->set_context_sio(sio);
190 sysport->set_context_pit(pit);
191 sysport->set_context_sio(sio);
194 cpu->set_context_mem(memory);
195 cpu->set_context_io(io);
196 cpu->set_context_intr(pic);
197 #ifdef SINGLE_MODE_DMA
198 cpu->set_context_dma(dma);
201 cpu->set_context_debugger(new DEBUGGER(this, emu));
205 io->set_iomap_range_rw(0x70, 0x7f, dma);
206 io->set_iomap_alias_rw(0x80, pic, I8259_ADDR_CHIP0 | 0);
207 io->set_iomap_alias_rw(0x81, pic, I8259_ADDR_CHIP0 | 1);
208 io->set_iomap_alias_rw(0x82, pic, I8259_ADDR_CHIP1 | 0);
209 io->set_iomap_alias_rw(0x83, pic, I8259_ADDR_CHIP1 | 1);
210 io->set_iomap_range_rw(0x8c, 0x8d, memory);
211 io->set_iovalue_single_r(0x8e, 0xff); // dipswitch
212 io->set_flipflop_single_rw(0x8f, 0x00); // shut
213 io->set_iomap_range_rw(0xa0, 0xa3, serial);
214 for(uint32_t p = 0xae; p <= 0x1fae; p += 0x100) {
215 io->set_iomap_single_w(p, crtc);
217 io->set_iomap_range_rw(0xa4, 0xa5, sasi);
218 io->set_iomap_range_rw(0xb0, 0xb3, serial);
219 io->set_iomap_single_r(0xbe, sysport);
220 io->set_iomap_range_rw(0xc8, 0xc9, opn);
221 io->set_iovalue_single_r(0xca, 0x7f); // voice communication ???
222 for(uint32_t p = 0xcc; p <= 0xfcc; p += 0x100) {
223 io->set_iomap_alias_rw(p, rtc, p >> 8);
225 io->set_iomap_single_w(0xcd, serial);
226 io->set_iomap_single_rw(0xce, memory);
227 io->set_iomap_range_rw(0xd8, 0xdb, fdc);
228 io->set_iomap_range_w(0xdc, 0xdf, floppy);
229 io->set_iomap_range_rw(0xe0, 0xe3, pio0);
230 io->set_iomap_range_rw(0xe4, 0xe7, pit);
231 io->set_iomap_range_rw(0xe8, 0xeb, pio1);
232 io->set_iomap_single_rw(0xef, joystick);
233 // io->set_iomap_range_w(0xf0, 0xf3, sysport);
234 io->set_iomap_single_w(0x170, crtc);
235 io->set_iomap_single_w(0x172, crtc);
236 io->set_iomap_single_w(0x174, crtc);
237 io->set_iomap_single_w(0x176, crtc);
238 io->set_iomap_range_w(0x178, 0x17b, crtc);
239 io->set_iomap_range_rw(0x1fe, 0x1ff, printer);
240 io->set_iomap_single_w(0x270, crtc);
241 io->set_iomap_single_w(0x272, crtc);
242 io->set_iomap_single_rw(0x274, memory);
244 // initialize all devices
245 #if defined(__GIT_REPO_VERSION)
246 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
248 for(DEVICE* device = first_device; device; device = device->next_device) {
249 device->initialize();
255 // delete all devices
256 for(DEVICE* device = first_device; device;) {
257 DEVICE *next_device = device->next_device;
260 device = next_device;
262 for(int drv = 0; drv < USE_HARD_DISK; drv++) {
263 if(!(config.last_hard_disk_path[drv][0] != _T('\0') && FILEIO::IsFileExisting(config.last_hard_disk_path[drv]))) {
264 create_local_path(config.last_hard_disk_path[drv], _MAX_PATH, _T("SASI%d.DAT"), drv);
269 DEVICE* VM::get_device(int id)
271 for(DEVICE* device = first_device; device; device = device->next_device) {
272 if(device->this_device_id == id) {
279 // ----------------------------------------------------------------------------
280 // drive virtual machine
281 // ----------------------------------------------------------------------------
286 for(DEVICE* device = first_device; device; device = device->next_device) {
290 for(DEVICE* device = first_device; device; device = device->next_device) {
294 // set initial port status
295 pio0->write_signal(SIG_I8255_PORT_B, 0x7c, 0xff);
296 opn->write_signal(SIG_YM2203_PORT_B, 0x37, 0xff);
309 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
314 DEVICE *VM::get_cpu(int index)
323 // ----------------------------------------------------------------------------
325 // ----------------------------------------------------------------------------
327 void VM::draw_screen()
332 // ----------------------------------------------------------------------------
334 // ----------------------------------------------------------------------------
336 void VM::initialize_sound(int rate, int samples)
338 // init sound manager
339 event->initialize_sound(rate, samples);
342 opn->initialize_sound(rate, 2000000, samples, 0, -8);
343 pcm->initialize_sound(rate, 4096);
346 uint16_t* VM::create_sound(int* extra_frames)
348 return event->create_sound(extra_frames);
351 int VM::get_sound_buffer_ptr()
353 return event->get_sound_buffer_ptr();
356 #ifdef USE_SOUND_VOLUME
357 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
360 opn->set_volume(0, decibel_l, decibel_r);
362 opn->set_volume(1, decibel_l, decibel_r);
364 pcm->set_volume(0, decibel_l, decibel_r);
366 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
367 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
368 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
373 // ----------------------------------------------------------------------------
375 // ----------------------------------------------------------------------------
377 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
379 fdc->open_disk(drv, file_path, bank);
381 if(fdc->get_media_type(drv) == MEDIA_TYPE_2DD) {
382 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2D) {
383 fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
385 } else if(fdc->get_media_type(drv) == MEDIA_TYPE_2D) {
386 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2DD) {
387 fdc->set_drive_type(drv, DRIVE_TYPE_2D);
392 void VM::close_floppy_disk(int drv)
394 fdc->close_disk(drv);
397 bool VM::is_floppy_disk_inserted(int drv)
399 return fdc->is_disk_inserted(drv);
402 void VM::is_floppy_disk_protected(int drv, bool value)
404 fdc->is_disk_protected(drv, value);
407 bool VM::is_floppy_disk_protected(int drv)
409 return fdc->is_disk_protected(drv);
412 uint32_t VM::is_floppy_disk_accessed()
414 return fdc->read_signal(0);
417 void VM::open_hard_disk(int drv, const _TCHAR* file_path)
419 if(drv < USE_HARD_DISK) {
420 sasi_hdd[drv]->open(0, file_path, 1024);
424 void VM::close_hard_disk(int drv)
426 if(drv < USE_HARD_DISK) {
427 sasi_hdd[drv]->close(0);
431 bool VM::is_hard_disk_inserted(int drv)
433 if(drv < USE_HARD_DISK) {
434 return sasi_hdd[drv]->mounted(0);
439 uint32_t VM::is_hard_disk_accessed()
443 for(int drv = 0; drv < USE_HARD_DISK; drv++) {
444 if(sasi_hdd[drv]->accessed(0)) {
451 bool VM::is_frame_skippable()
453 return event->is_frame_skippable();
456 void VM::update_config()
458 for(DEVICE* device = first_device; device; device = device->next_device) {
459 device->update_config();
463 #define STATE_VERSION 5
465 bool VM::process_state(FILEIO* state_fio, bool loading)
467 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
470 for(DEVICE* device = first_device; device; device = device->next_device) {
471 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
472 // const char *name = typeid(*device).name();
473 // But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
474 const char *name = device->get_device_name();
475 int len = strlen(name);
477 if(!state_fio->StateCheckInt32(len)) {
479 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
483 if(!state_fio->StateCheckBuffer(name, len, 1)) {
485 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
489 if(!device->process_state(state_fio, loading)) {
491 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
496 // Machine specified.