2 SHARP MZ-3500 Emulator 'EmuZ-3500'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
21 #include "../mz1p17.h"
24 #include "../pcm1bit.h"
25 #include "../prnfile.h"
26 #include "../upd1990a.h"
27 #include "../upd7220.h"
28 #include "../upd765a.h"
32 #include "../debugger.h"
41 using MZ3500::KEYBOARD;
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
50 first_device = last_device = NULL;
51 dummy = new DEVICE(this, emu); // must be 1st device
52 event = new EVENT(this, emu); // must be 2nd device
53 dummy->set_device_name(_T("1st Dummy"));
56 mainio = new IO(this, emu);
57 fdc = new UPD765A(this, emu);
58 fdc->set_context_noise_seek(new NOISE(this, emu));
59 fdc->set_context_noise_head_down(new NOISE(this, emu));
60 fdc->set_context_noise_head_up(new NOISE(this, emu));
61 maincpu = new Z80(this, emu);
62 mainbus= new MAIN(this, emu);
63 mainio->set_device_name(_T("I/O Bus (Main)"));
64 maincpu->set_device_name(_T("Z80 CPU (Main)"));
65 mainbus->set_device_name(_T("MAIN BUS"));
67 if(config.printer_type == 0) {
68 printer = new PRNFILE(this, emu);
69 } else if(config.printer_type == 1) {
70 printer = new MZ1P17(this, emu);
74 sio = new I8251(this, emu);
75 pit = new I8253(this, emu);
76 pio = new I8255(this, emu);
77 subio = new IO(this, emu);
78 subio->set_device_name(_T("I/O Bus (Sub)"));
79 ls244 = new LS244(this, emu);
80 not_data0 = new NOT(this, emu);
81 not_data0->set_device_name(_T("NOT Gate (Printer Bit0)"));
82 not_data1 = new NOT(this, emu);
83 not_data1->set_device_name(_T("NOT Gate (Printer Bit1)"));
84 not_data2 = new NOT(this, emu);
85 not_data2->set_device_name(_T("NOT Gate (Printer Bit2)"));
86 not_data3 = new NOT(this, emu);
87 not_data3->set_device_name(_T("NOT Gate (Printer Bit3)"));
88 not_data4 = new NOT(this, emu);
89 not_data4->set_device_name(_T("NOT Gate (Printer Bit4)"));
90 not_data5 = new NOT(this, emu);
91 not_data5->set_device_name(_T("NOT Gate (Printer Bit5)"));
92 not_data6 = new NOT(this, emu);
93 not_data6->set_device_name(_T("NOT Gate (Printer Bit6)"));
94 not_data7 = new NOT(this, emu);
95 not_data7->set_device_name(_T("NOT Gate (Printer Bit7)"));
96 not_busy = new NOT(this, emu);
97 not_busy->set_device_name(_T("NOT Gate (Printer Busy)"));
98 pcm = new PCM1BIT(this, emu);
99 rtc = new UPD1990A(this, emu);
100 gdc_chr = new UPD7220(this, emu);
101 gdc_chr->set_device_name(_T("uPD7220 GDC (Character)"));
102 gdc_gfx = new UPD7220(this, emu);
103 gdc_gfx->set_device_name(_T("uPD7220 GDC (Graphics)"));
104 subcpu = new Z80(this, emu);
105 subcpu->set_device_name(_T("Z80 CPU (Sub)"));
106 subbus = new SUB(this, emu);
107 kbd = new KEYBOARD(this, emu);
110 event->set_context_cpu(maincpu, CPU_CLOCKS);
111 event->set_context_cpu(subcpu, CPU_CLOCKS);
112 event->set_context_sound(pcm);
113 event->set_context_sound(fdc->get_context_noise_seek());
114 event->set_context_sound(fdc->get_context_noise_head_down());
115 event->set_context_sound(fdc->get_context_noise_head_up());
118 fdc->set_context_irq(mainbus, SIG_MAIN_INTFD, 1);
119 fdc->set_context_drq(mainbus, SIG_MAIN_DRQ, 1);
120 fdc->set_context_index(mainbus, SIG_MAIN_INDEX, 1);
123 if(config.printer_type == 0) {
124 PRNFILE *prnfile = (PRNFILE *)printer;
125 prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
126 prnfile->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
127 } else if(config.printer_type == 1) {
128 MZ1P17 *mz1p17 = (MZ1P17 *)printer;
129 mz1p17->mode = MZ1P17_MODE_MZ1;
130 mz1p17->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
131 mz1p17->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
133 // sw1=on, sw2=off, select MZ-1P02 (mz3500sm p.85)
134 config.dipswitch &= ~0x03;
135 config.dipswitch |= 0x01;
139 sio->set_context_rxrdy(subcpu, SIG_CPU_NMI, 1);
142 // i8253 ch.0 -> i8251 txc,rxc
143 pit->set_context_ch1(pcm, SIG_PCM1BIT_SIGNAL, 1);
144 pit->set_context_ch2(pit, SIG_I8253_GATE_1, 1);
145 pit->set_constant_clock(0, 2450760);
146 pit->set_constant_clock(1, 2450760);
147 pit->set_constant_clock(2, 2450760);
149 // mz3500sm p.78,80,81
150 pio->set_context_port_a(not_data0, SIG_NOT_INPUT, 0x01, 0);
151 pio->set_context_port_a(not_data1, SIG_NOT_INPUT, 0x02, 0);
152 pio->set_context_port_a(not_data2, SIG_NOT_INPUT, 0x04, 0);
153 pio->set_context_port_a(not_data3, SIG_NOT_INPUT, 0x08, 0);
154 pio->set_context_port_a(not_data4, SIG_NOT_INPUT, 0x10, 0);
155 pio->set_context_port_a(not_data5, SIG_NOT_INPUT, 0x20, 0);
156 pio->set_context_port_a(not_data6, SIG_NOT_INPUT, 0x40, 0);
157 pio->set_context_port_a(not_data7, SIG_NOT_INPUT, 0x80, 0);
158 pio->set_context_port_b(rtc, SIG_UPD1990A_STB, 0x01, 0);
159 pio->set_context_port_b(rtc, SIG_UPD1990A_C0, 0x02, 0);
160 pio->set_context_port_b(rtc, SIG_UPD1990A_C1, 0x04, 0);
161 pio->set_context_port_b(rtc, SIG_UPD1990A_C2, 0x08, 0);
162 pio->set_context_port_b(rtc, SIG_UPD1990A_DIN, 0x10, 0);
163 pio->set_context_port_b(rtc, SIG_UPD1990A_CLK, 0x20, 0);
164 pio->set_context_port_b(mainbus, SIG_MAIN_SRDY, 0x40, 0);
165 // pio->set_context_port_b(subbus, SIG_SUB_PIO_PM, 0x80, 0); // P/M: CG Selection
166 pio->set_context_port_c(kbd, SIG_KEYBOARD_DC, 0x01, 0);
167 pio->set_context_port_c(kbd, SIG_KEYBOARD_STC, 0x02, 0);
168 pio->set_context_port_c(kbd, SIG_KEYBOARD_ACKC, 0x04, 0);
169 // i8255 pc3 <- intr (not use ???)
170 pio->set_context_port_c(pcm, SIG_PCM1BIT_MUTE, 0x10, 0);
171 pio->set_context_port_c(printer, SIG_PRINTER_STROBE, 0x20, 0);
172 // i8255 pc6 <- printer ack
173 pio->set_context_port_c(ls244, SIG_LS244_INPUT, 0x80, -6);
176 not_data0->set_context_out(printer, SIG_PRINTER_DATA, 0x01);
177 not_data1->set_context_out(printer, SIG_PRINTER_DATA, 0x02);
178 not_data2->set_context_out(printer, SIG_PRINTER_DATA, 0x04);
179 not_data3->set_context_out(printer, SIG_PRINTER_DATA, 0x08);
180 not_data4->set_context_out(printer, SIG_PRINTER_DATA, 0x10);
181 not_data5->set_context_out(printer, SIG_PRINTER_DATA, 0x20);
182 not_data6->set_context_out(printer, SIG_PRINTER_DATA, 0x40);
183 not_data7->set_context_out(printer, SIG_PRINTER_DATA, 0x80);
184 not_busy->set_context_out(ls244, SIG_LS244_INPUT, 0x04);
187 rtc->set_context_dout(ls244, SIG_LS244_INPUT, 0x01);
189 gdc_chr->set_vram_ptr(subbus->get_vram_chr(), 0x2000, 0xfff);
190 subbus->set_sync_ptr_chr(gdc_chr->get_sync());
191 subbus->set_ra_ptr_chr(gdc_chr->get_ra());
192 subbus->set_cs_ptr_chr(gdc_chr->get_cs());
193 subbus->set_ead_ptr_chr(gdc_chr->get_ead());
195 gdc_gfx->set_vram_ptr(subbus->get_vram_gfx(), 0x18000);
196 subbus->set_sync_ptr_gfx(gdc_gfx->get_sync());
197 subbus->set_ra_ptr_gfx(gdc_gfx->get_ra());
198 subbus->set_cs_ptr_gfx(gdc_gfx->get_cs());
199 subbus->set_ead_ptr_gfx(gdc_gfx->get_ead());
201 kbd->set_context_subcpu(subcpu);
202 kbd->set_context_ls244(ls244);
205 subcpu->set_context_busack(mainbus, SIG_MAIN_SACK, 1);
207 mainbus->set_context_maincpu(maincpu);
208 mainbus->set_context_subcpu(subcpu);
209 mainbus->set_context_fdc(fdc);
211 subbus->set_context_main(mainbus);
212 subbus->set_ipl(mainbus->get_ipl());
213 subbus->set_common(mainbus->get_common());
216 mainio->set_iomap_range_rw(0xec, 0xef, mainbus); // reset int0
217 mainio->set_iomap_range_rw(0xf4, 0xf7, fdc); // fdc: f4h,f6h = status, f5h,f7h = data
218 mainio->set_iomap_range_rw(0xf8, 0xfb, mainbus); // mfd interface
219 mainio->set_iomap_range_rw(0xfc, 0xff, mainbus); // memory mpaper
222 subio->set_iomap_range_w(0x00, 0x0f, subbus); // int0 to main (set flipflop)
223 subio->set_iomap_range_rw(0x10, 0x1f, sio);
224 subio->set_iomap_range_rw(0x20, 0x2f, pit);
225 subio->set_iomap_range_rw(0x30, 0x3f, pio);
226 subio->set_iomap_range_r(0x40, 0x4f, ls244); // input port
227 subio->set_iomap_range_rw(0x50, 0x5f, subbus); // crt control i/o
228 subio->set_iomap_range_rw(0x60, 0x6f, gdc_gfx);
229 subio->set_iomap_range_rw(0x70, 0x7f, gdc_chr);
231 subio->cpu_index = 1;
235 maincpu->set_context_mem(mainbus);
236 maincpu->set_context_io(mainio);
237 maincpu->set_context_intr(mainbus);
239 maincpu->set_context_debugger(new DEBUGGER(this, emu));
242 subcpu->set_context_mem(subbus);
243 subcpu->set_context_io(subio);
244 subcpu->set_context_intr(subbus);
246 subcpu->set_context_debugger(new DEBUGGER(this, emu));
249 // initialize all devices
250 #if defined(__GIT_REPO_VERSION)
251 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
253 for(DEVICE* device = first_device; device; device = device->next_device) {
254 device->initialize();
256 for(int i = 0; i < 4; i++) {
257 fdc->set_drive_type(i, DRIVE_TYPE_2D);
259 // GDC clock (mz3500sm p.33,34)
260 if(config.monitor_type == 0 || config.monitor_type == 1) {
261 gdc_chr->set_horiz_freq(20920);
262 gdc_gfx->set_horiz_freq(20920);
264 gdc_chr->set_horiz_freq(15870);
265 gdc_gfx->set_horiz_freq(15870);
271 // delete all devices
272 for(DEVICE* device = first_device; device;) {
273 DEVICE *next_device = device->next_device;
276 device = next_device;
280 DEVICE* VM::get_device(int id)
282 for(DEVICE* device = first_device; device; device = device->next_device) {
283 if(device->this_device_id == id) {
290 // ----------------------------------------------------------------------------
291 // drive virtual machine
292 // ----------------------------------------------------------------------------
297 for(DEVICE* device = first_device; device; device = device->next_device) {
301 // set busreq of sub cpu
302 subcpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
304 // halt key is not pressed (mz3500sm p.80)
306 ls244->write_signal(SIG_LS244_INPUT, 0x80, 0xff);
308 // set printer signal (mz3500sm p.78)
309 not_busy->write_signal(SIG_NOT_INPUT, 0, 0); // busy = low
310 ls244->write_signal(SIG_LS244_INPUT, 0x1c, 0x1c); // busy = ~(low), pe = ~(low), pdtr = high
311 pio->write_signal(SIG_I8255_PORT_C, 0x40, 0x40); // ack = high
314 void VM::special_reset()
316 // halt key is pressed (mz3500sm p.80)
318 ls244->write_signal(SIG_LS244_INPUT, 0x00, 0x80);
323 // halt key is released (mz3500sm p.80)
324 if(halt != 0 && --halt == 0) {
325 ls244->write_signal(SIG_LS244_INPUT, 0x80, 0x80);
330 double VM::get_frame_rate()
332 return event->get_frame_rate();
335 // ----------------------------------------------------------------------------
337 // ----------------------------------------------------------------------------
340 DEVICE *VM::get_cpu(int index)
344 } else if(index == 1) {
351 // ----------------------------------------------------------------------------
353 // ----------------------------------------------------------------------------
355 void VM::draw_screen()
357 subbus->draw_screen();
360 // ----------------------------------------------------------------------------
362 // ----------------------------------------------------------------------------
364 void VM::initialize_sound(int rate, int samples)
366 // init sound manager
367 event->initialize_sound(rate, samples);
370 pcm->initialize_sound(rate, 8000);
373 uint16_t* VM::create_sound(int* extra_frames)
375 return event->create_sound(extra_frames);
378 int VM::get_sound_buffer_ptr()
380 return event->get_sound_buffer_ptr();
383 #ifdef USE_SOUND_VOLUME
384 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
387 pcm->set_volume(0, decibel_l, decibel_r);
389 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
390 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
391 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
396 // ----------------------------------------------------------------------------
398 // ----------------------------------------------------------------------------
400 void VM::key_down(int code, bool repeat)
405 void VM::key_up(int code)
410 bool VM::get_caps_locked()
412 return kbd->get_caps_locked();
415 bool VM::get_kana_locked()
417 return kbd->get_kana_locked();
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
424 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
426 fdc->open_disk(drv, file_path, bank);
429 void VM::close_floppy_disk(int drv)
431 fdc->close_disk(drv);
434 bool VM::is_floppy_disk_inserted(int drv)
436 return fdc->is_disk_inserted(drv);
439 void VM::is_floppy_disk_protected(int drv, bool value)
441 fdc->is_disk_protected(drv, value);
444 bool VM::is_floppy_disk_protected(int drv)
446 return fdc->is_disk_protected(drv);
449 uint32_t VM::is_floppy_disk_accessed()
451 return fdc->read_signal(0);
454 bool VM::is_frame_skippable()
456 return event->is_frame_skippable();
459 void VM::update_config()
461 for(DEVICE* device = first_device; device; device = device->next_device) {
462 device->update_config();
466 #define STATE_VERSION 5
468 bool VM::process_state(FILEIO* state_fio, bool loading)
470 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
473 for(DEVICE* device = first_device; device; device = device->next_device) {
474 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
475 // const char *name = typeid(*device).name();
476 // But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
477 const char *name = device->get_device_name();
478 int len = strlen(name);
480 if(!state_fio->StateCheckInt32(len)) {
482 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
486 if(!state_fio->StateCheckBuffer(name, len, 1)) {
488 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
492 if(!device->process_state(state_fio, loading)) {
494 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
499 // Machine specified.
500 state_fio->StateUint8(halt);