OSDN Git Service

[General] Merge Upstream 2017-03-07.
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz3500 / mz3500.cpp
1 /*
2         SHARP MZ-3500 Emulator 'EmuZ-3500'
3
4         Author : Takeda.Toshiya
5         Date   : 2010.08.31-
6
7         [ virtual machine ]
8 */
9
10 #include "mz3500.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../disk.h"
16 #include "../i8251.h"
17 #include "../i8253.h"
18 #include "../i8255.h"
19 #include "../io.h"
20 #include "../ls244.h"
21 #include "../mz1p17.h"
22 #include "../not.h"
23 #include "../pcm1bit.h"
24 #include "../prnfile.h"
25 #include "../upd1990a.h"
26 #include "../upd7220.h"
27 #include "../upd765a.h"
28 #include "../z80.h"
29
30 #ifdef USE_DEBUGGER
31 #include "../debugger.h"
32 #endif
33
34 #include "./main.h"
35 #include "./sub.h"
36 #include "keyboard.h"
37
38 // ----------------------------------------------------------------------------
39 // initialize
40 // ----------------------------------------------------------------------------
41
42 VM::VM(EMU* parent_emu) : emu(parent_emu)
43 {
44         // create devices
45         first_device = last_device = NULL;
46         dummy = new DEVICE(this, emu);  // must be 1st device
47         event = new EVENT(this, emu);   // must be 2nd device
48         dummy->set_device_name(_T("1st Dummy"));
49         event->set_device_name(_T("EVENT"));
50         // for main cpu
51         mainio = new IO(this, emu);
52         fdc = new UPD765A(this, emu);
53         maincpu = new Z80(this, emu);
54         mainbus= new MAIN(this, emu);
55         mainio->set_device_name(_T("I/O Bus (Main)"));
56         maincpu->set_device_name(_T("Z80 CPU (Main)"));
57         mainbus->set_device_name(_T("MAIN BUS"));
58         // for sub cpu
59         if(config.printer_device_type == 0) {
60                 printer = new PRNFILE(this, emu);
61         } else if(config.printer_device_type == 1) {
62                 printer = new MZ1P17(this, emu);
63         } else {
64                 printer = dummy;
65         }
66         sio = new I8251(this, emu);
67         pit = new I8253(this, emu);
68         pio = new I8255(this, emu);
69         subio = new IO(this, emu);
70         subio->set_device_name(_T("I/O Bus (Sub)"));
71         ls244 = new LS244(this, emu);
72         not_data0 = new NOT(this, emu);
73         not_data0->set_device_name(_T("NOT Gate (Printer Bit0)"));
74         not_data1 = new NOT(this, emu);
75         not_data1->set_device_name(_T("NOT Gate (Printer Bit1)"));
76         not_data2 = new NOT(this, emu);
77         not_data2->set_device_name(_T("NOT Gate (Printer Bit2)"));
78         not_data3 = new NOT(this, emu);
79         not_data3->set_device_name(_T("NOT Gate (Printer Bit3)"));
80         not_data4 = new NOT(this, emu);
81         not_data4->set_device_name(_T("NOT Gate (Printer Bit4)"));
82         not_data5 = new NOT(this, emu);
83         not_data5->set_device_name(_T("NOT Gate (Printer Bit5)"));
84         not_data6 = new NOT(this, emu);
85         not_data6->set_device_name(_T("NOT Gate (Printer Bit6)"));
86         not_data7 = new NOT(this, emu);
87         not_data7->set_device_name(_T("NOT Gate (Printer Bit7)"));
88         not_busy = new NOT(this, emu);
89         not_busy->set_device_name(_T("NOT Gate (Printer Busy)"));
90         pcm = new PCM1BIT(this, emu);
91         rtc = new UPD1990A(this, emu);
92         gdc_chr = new UPD7220(this, emu);
93         gdc_chr->set_device_name(_T("uPD7220 GDC (Character)"));
94         gdc_gfx = new UPD7220(this, emu);
95         gdc_gfx->set_device_name(_T("uPD7220 GDC (Graphics)"));
96         subcpu = new Z80(this, emu);
97         subcpu->set_device_name(_T("Z80 CPU (Sub)"));
98         subbus = new SUB(this, emu);
99         kbd = new KEYBOARD(this, emu);
100 #if defined(_USE_QT)
101         subio->set_device_name(_T("SUB I/O"));
102         subcpu->set_device_name(_T("SUB CPU(Z80)"));
103         subbus->set_device_name(_T("SUB BUS"));
104         ls244->set_device_name(_T("74LS244(I/O)"));
105         not_data1->set_device_name(_T("NOT GATE #1"));
106         not_data2->set_device_name(_T("NOT GATE #2"));
107         not_data3->set_device_name(_T("NOT GATE #3"));
108         not_data4->set_device_name(_T("NOT GATE #4"));
109         not_data5->set_device_name(_T("NOT GATE #5"));
110         not_data6->set_device_name(_T("NOT GATE #6"));
111         not_data7->set_device_name(_T("NOT GATE #7"));
112         not_data8->set_device_name(_T("NOT GATE #8"));
113         gdc_chr->set_device_name(_T("uPD7220 GDC(CHARACTER)"));
114         gdc_gfx->set_device_name(_T("uPD7220 GDC(GRAPHICS)"));
115         kbd->set_device_name(_T("KEYBOARD I/F"));
116 #endif  
117         
118         // set contexts
119         event->set_context_cpu(maincpu, CPU_CLOCKS);
120         event->set_context_cpu(subcpu, CPU_CLOCKS);
121         event->set_context_sound(pcm);
122 #if defined(USE_SOUND_FILES)
123         if(fdc->load_sound_data(UPD765A_SND_TYPE_SEEK, _T("FDDSEEK.WAV"))) {
124                 event->set_context_sound(fdc);
125         }
126 #endif  
127         
128         // mz3500sm p.59
129         fdc->set_context_irq(mainbus, SIG_MAIN_INTFD, 1);
130         fdc->set_context_drq(mainbus, SIG_MAIN_DRQ, 1);
131         fdc->set_context_index(mainbus, SIG_MAIN_INDEX, 1);
132         
133         // mz3500sm p.78
134         if(config.printer_device_type == 0) {
135                 PRNFILE *prnfile = (PRNFILE *)printer;
136                 prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
137                 prnfile->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
138         } else if(config.printer_device_type == 1) {
139                 MZ1P17 *mz1p17 = (MZ1P17 *)printer;
140                 mz1p17->mode = MZ1P17_MODE_MZ1;
141                 mz1p17->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
142                 mz1p17->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
143                 
144                 // sw1=on, sw2=off, select MZ-1P02 (mz3500sm p.85)
145                 config.dipswitch &= ~0x03;
146                 config.dipswitch |=  0x01;
147         }
148         
149         // mz3500sm p.72,77
150         sio->set_context_rxrdy(subcpu, SIG_CPU_NMI, 1);
151         
152         // mz3500sm p.77,83
153         // i8253 ch.0 -> i8251 txc,rxc
154         pit->set_context_ch1(pcm, SIG_PCM1BIT_SIGNAL, 1);
155         pit->set_context_ch2(pit, SIG_I8253_GATE_1, 1);
156         pit->set_constant_clock(0, 2450760);
157         pit->set_constant_clock(1, 2450760);
158         pit->set_constant_clock(2, 2450760);
159         
160         // mz3500sm p.78,80,81
161         pio->set_context_port_a(not_data0, SIG_NOT_INPUT, 0x01, 0);
162         pio->set_context_port_a(not_data1, SIG_NOT_INPUT, 0x02, 0);
163         pio->set_context_port_a(not_data2, SIG_NOT_INPUT, 0x04, 0);
164         pio->set_context_port_a(not_data3, SIG_NOT_INPUT, 0x08, 0);
165         pio->set_context_port_a(not_data4, SIG_NOT_INPUT, 0x10, 0);
166         pio->set_context_port_a(not_data5, SIG_NOT_INPUT, 0x20, 0);
167         pio->set_context_port_a(not_data6, SIG_NOT_INPUT, 0x40, 0);
168         pio->set_context_port_a(not_data7, SIG_NOT_INPUT, 0x80, 0);
169         pio->set_context_port_b(rtc, SIG_UPD1990A_STB, 0x01, 0);
170         pio->set_context_port_b(rtc, SIG_UPD1990A_C0,  0x02, 0);
171         pio->set_context_port_b(rtc, SIG_UPD1990A_C1,  0x04, 0);
172         pio->set_context_port_b(rtc, SIG_UPD1990A_C2,  0x08, 0);
173         pio->set_context_port_b(rtc, SIG_UPD1990A_DIN, 0x10, 0);
174         pio->set_context_port_b(rtc, SIG_UPD1990A_CLK, 0x20, 0);
175         pio->set_context_port_b(mainbus, SIG_MAIN_SRDY, 0x40, 0);
176 //      pio->set_context_port_b(sub, SIG_SUB_PIO_PM, 0x80, 0);  // P/M: CG Selection
177         pio->set_context_port_c(kbd, SIG_KEYBOARD_DC, 0x01, 0);
178         pio->set_context_port_c(kbd, SIG_KEYBOARD_STC, 0x02, 0);
179         pio->set_context_port_c(kbd, SIG_KEYBOARD_ACKC, 0x04, 0);
180         // i8255 pc3 <- intr (not use ???)
181         pio->set_context_port_c(pcm, SIG_PCM1BIT_MUTE, 0x10, 0);
182         pio->set_context_port_c(printer, SIG_PRINTER_STROBE, 0x20, 0);
183         // i8255 pc6 <- printer ack
184         pio->set_context_port_c(ls244, SIG_LS244_INPUT, 0x80, -6);
185         
186         // mz3500sm p.78
187         not_data0->set_context_out(printer, SIG_PRINTER_DATA, 0x01);
188         not_data1->set_context_out(printer, SIG_PRINTER_DATA, 0x02);
189         not_data2->set_context_out(printer, SIG_PRINTER_DATA, 0x04);
190         not_data3->set_context_out(printer, SIG_PRINTER_DATA, 0x08);
191         not_data4->set_context_out(printer, SIG_PRINTER_DATA, 0x10);
192         not_data5->set_context_out(printer, SIG_PRINTER_DATA, 0x20);
193         not_data6->set_context_out(printer, SIG_PRINTER_DATA, 0x40);
194         not_data7->set_context_out(printer, SIG_PRINTER_DATA, 0x80);
195         not_busy->set_context_out(ls244, SIG_LS244_INPUT, 0x04);
196         
197         // mz3500sm p.80,81
198         rtc->set_context_dout(ls244, SIG_LS244_INPUT, 0x01);
199         
200         gdc_chr->set_vram_ptr(subbus->get_vram_chr(), 0x2000, 0xfff);
201         subbus->set_sync_ptr_chr(gdc_chr->get_sync());
202         subbus->set_ra_ptr_chr(gdc_chr->get_ra());
203         subbus->set_cs_ptr_chr(gdc_chr->get_cs());
204         subbus->set_ead_ptr_chr(gdc_chr->get_ead());
205         
206         gdc_gfx->set_vram_ptr(subbus->get_vram_gfx(), 0x18000);
207         subbus->set_sync_ptr_gfx(gdc_gfx->get_sync());
208         subbus->set_ra_ptr_gfx(gdc_gfx->get_ra());
209         subbus->set_cs_ptr_gfx(gdc_gfx->get_cs());
210         subbus->set_ead_ptr_gfx(gdc_gfx->get_ead());
211         
212         kbd->set_context_subcpu(subcpu);
213         kbd->set_context_ls244(ls244);
214         
215         // mz3500sm p.23
216         subcpu->set_context_busack(mainbus, SIG_MAIN_SACK, 1);
217         
218         mainbus->set_context_maincpu(maincpu);
219         mainbus->set_context_subcpu(subcpu);
220         mainbus->set_context_fdc(fdc);
221         
222         subbus->set_context_main(mainbus);
223         subbus->set_ipl(mainbus->get_ipl());
224         subbus->set_common(mainbus->get_common());
225         
226         // mz3500sm p.17
227         mainio->set_iomap_range_rw(0xec, 0xef, mainbus);        // reset int0
228         mainio->set_iomap_range_rw(0xf4, 0xf7, fdc);    // fdc: f4h,f6h = status, f5h,f7h = data
229         mainio->set_iomap_range_rw(0xf8, 0xfb, mainbus);        // mfd interface
230         mainio->set_iomap_range_rw(0xfc, 0xff, mainbus);        // memory mpaper
231         
232         // mz3500sm p.18
233         subio->set_iomap_range_w(0x00, 0x0f, subbus);   // int0 to main (set flipflop)
234         subio->set_iomap_range_rw(0x10, 0x1f, sio);
235         subio->set_iomap_range_rw(0x20, 0x2f, pit);
236         subio->set_iomap_range_rw(0x30, 0x3f, pio);
237         subio->set_iomap_range_r(0x40, 0x4f, ls244);    // input port
238         subio->set_iomap_range_rw(0x50, 0x5f, subbus);  // crt control i/o
239         subio->set_iomap_range_rw(0x60, 0x6f, gdc_gfx);
240         subio->set_iomap_range_rw(0x70, 0x7f, gdc_chr);
241 #ifdef _IO_DEBUG_LOG
242         subio->cpu_index = 1;
243 #endif
244         
245         // cpu bus
246         maincpu->set_context_mem(mainbus);
247         maincpu->set_context_io(mainio);
248         maincpu->set_context_intr(mainbus);
249 #ifdef USE_DEBUGGER
250         maincpu->set_context_debugger(new DEBUGGER(this, emu));
251 #endif
252         
253         subcpu->set_context_mem(subbus);
254         subcpu->set_context_io(subio);
255         subcpu->set_context_intr(subbus);
256 #ifdef USE_DEBUGGER
257         subcpu->set_context_debugger(new DEBUGGER(this, emu));
258 #endif
259         
260         // initialize all devices
261         for(DEVICE* device = first_device; device; device = device->next_device) {
262                 device->initialize();
263         }
264         for(int i = 0; i < 4; i++) {
265                 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
266         }
267         // GDC clock (mz3500sm p.33,34)
268         if(config.monitor_type == 0 || config.monitor_type == 1) {
269                 gdc_chr->set_horiz_freq(20920);
270                 gdc_gfx->set_horiz_freq(20920);
271         } else {
272                 gdc_chr->set_horiz_freq(15870);
273                 gdc_gfx->set_horiz_freq(15870);
274         }
275 }
276
277 VM::~VM()
278 {
279         // delete all devices
280         for(DEVICE* device = first_device; device;) {
281                 DEVICE *next_device = device->next_device;
282                 device->release();
283                 delete device;
284                 device = next_device;
285         }
286 }
287
288 DEVICE* VM::get_device(int id)
289 {
290         for(DEVICE* device = first_device; device; device = device->next_device) {
291                 if(device->this_device_id == id) {
292                         return device;
293                 }
294         }
295         return NULL;
296 }
297
298 // ----------------------------------------------------------------------------
299 // drive virtual machine
300 // ----------------------------------------------------------------------------
301
302 void VM::reset()
303 {
304         // reset all devices
305         for(DEVICE* device = first_device; device; device = device->next_device) {
306                 device->reset();
307         }
308         
309         // set busreq of sub cpu
310         subcpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
311         
312         // halt key is not pressed (mz3500sm p.80)
313         halt = 0;
314         ls244->write_signal(SIG_LS244_INPUT, 0x80, 0xff);
315         
316         // set printer signal (mz3500sm p.78)
317         not_busy->write_signal(SIG_NOT_INPUT, 0, 0);            // busy = low
318         ls244->write_signal(SIG_LS244_INPUT, 0x1c, 0x1c);       // busy = ~(low), pe = ~(low), pdtr = high
319         pio->write_signal(SIG_I8255_PORT_C, 0x40, 0x40);        // ack = high
320 }
321
322 void VM::special_reset()
323 {
324         // halt key is pressed (mz3500sm p.80)
325         halt = 8;
326         ls244->write_signal(SIG_LS244_INPUT, 0x00, 0x80);
327 }
328
329 void VM::run()
330 {
331         // halt key is released (mz3500sm p.80)
332         if(halt != 0 && --halt == 0) {
333                 ls244->write_signal(SIG_LS244_INPUT, 0x80, 0x80);
334         }
335         event->drive();
336 }
337
338 double VM::get_frame_rate()
339 {
340         return event->get_frame_rate();
341 }
342
343 // ----------------------------------------------------------------------------
344 // debugger
345 // ----------------------------------------------------------------------------
346
347 #ifdef USE_DEBUGGER
348 DEVICE *VM::get_cpu(int index)
349 {
350         if(index == 0) {
351                 return maincpu;
352         } else if(index == 1) {
353                 return subcpu;
354         }
355         return NULL;
356 }
357 #endif
358
359 // ----------------------------------------------------------------------------
360 // draw screen
361 // ----------------------------------------------------------------------------
362
363 void VM::draw_screen()
364 {
365         subbus->draw_screen();
366 }
367
368 uint32_t VM::get_access_lamp_status()
369 {
370         uint32_t status = fdc->read_signal(0);
371         return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
372 }
373
374 // ----------------------------------------------------------------------------
375 // soud manager
376 // ----------------------------------------------------------------------------
377
378 void VM::initialize_sound(int rate, int samples)
379 {
380         // init sound manager
381         event->initialize_sound(rate, samples);
382         
383         // init sound gen
384         pcm->initialize_sound(rate, 8000);
385 }
386
387 uint16_t* VM::create_sound(int* extra_frames)
388 {
389         return event->create_sound(extra_frames);
390 }
391
392 int VM::get_sound_buffer_ptr()
393 {
394         return event->get_sound_buffer_ptr();
395 }
396
397 #ifdef USE_SOUND_VOLUME
398 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
399 {
400         if(ch == 0) {
401                 pcm->set_volume(0, decibel_l, decibel_r);
402         }
403 #if defined(USE_SOUND_FILES)
404         else if(ch == 1) {
405                 fdc->set_volume(UPD765A_SND_TYPE_SEEK, decibel_l, decibel_r);
406         }
407 #endif
408 }
409 #endif
410
411 // ----------------------------------------------------------------------------
412 // notify key
413 // ----------------------------------------------------------------------------
414
415 void VM::key_down(int code, bool repeat)
416 {
417         kbd->key_down(code);
418 }
419
420 void VM::key_up(int code)
421 {
422         kbd->key_up(code);
423 }
424
425 // ----------------------------------------------------------------------------
426 // user interface
427 // ----------------------------------------------------------------------------
428
429 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
430 {
431         fdc->open_disk(drv, file_path, bank);
432 }
433
434 void VM::close_floppy_disk(int drv)
435 {
436         fdc->close_disk(drv);
437 }
438
439 bool VM::is_floppy_disk_inserted(int drv)
440 {
441         return fdc->is_disk_inserted(drv);
442 }
443
444 void VM::is_floppy_disk_protected(int drv, bool value)
445 {
446         fdc->is_disk_protected(drv, value);
447 }
448
449 bool VM::is_floppy_disk_protected(int drv)
450 {
451         return fdc->is_disk_protected(drv);
452 }
453
454 bool VM::is_frame_skippable()
455 {
456         return event->is_frame_skippable();
457 }
458
459 void VM::update_config()
460 {
461         for(DEVICE* device = first_device; device; device = device->next_device) {
462                 device->update_config();
463         }
464 }
465
466 #define STATE_VERSION   3
467
468 void VM::save_state(FILEIO* state_fio)
469 {
470         state_fio->FputUint32(STATE_VERSION);
471         
472         for(DEVICE* device = first_device; device; device = device->next_device) {
473                 device->save_state(state_fio);
474         }
475         state_fio->FputUint8(halt);
476 }
477
478 bool VM::load_state(FILEIO* state_fio)
479 {
480         if(state_fio->FgetUint32() != STATE_VERSION) {
481                 return false;
482         }
483         for(DEVICE* device = first_device; device; device = device->next_device) {
484                 if(!device->load_state(state_fio)) {
485                         return false;
486                 }
487         }
488         halt = state_fio->FgetUint8();
489         return true;
490 }
491