OSDN Git Service

[General] Merge Upstream 2017-03-07.
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz5500 / mz5500.cpp
1 /*
2         SHARP MZ-5500 Emulator 'EmuZ-5500'
3
4         Author : Takeda.Toshiya
5         Date   : 2008.04.10 -
6
7         [ virtual machine ]
8 */
9
10 #include "mz5500.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../disk.h"
16 #include "../i8237.h"
17 #include "../i8255.h"
18 #include "../i8259.h"
19 #if defined(HAS_I286) || defined(HAS_I186)
20 #include "../i286.h"
21 #else
22 #include "../i86.h"
23 #endif
24 #include "../io.h"
25 #include "../ls393.h"
26 #include "../mz1p17.h"
27 #include "../not.h"
28 #include "../prnfile.h"
29 #include "../rp5c01.h"
30 #include "../upd7220.h"
31 #include "../upd765a.h"
32 //#include "../ym2203.h"
33 #include "../ay_3_891x.h"
34 #include "../z80ctc.h"
35 #include "../z80sio.h"
36
37 #ifdef USE_DEBUGGER
38 #include "../debugger.h"
39 #endif
40
41 #include "display.h"
42 #include "keyboard.h"
43 #include "memory.h"
44 #include "sysport.h"
45
46 // ----------------------------------------------------------------------------
47 // initialize
48 // ----------------------------------------------------------------------------
49
50 VM::VM(EMU* parent_emu) : emu(parent_emu)
51 {
52         // create devices
53         first_device = last_device = NULL;
54         dummy = new DEVICE(this, emu);  // must be 1st device
55         event = new EVENT(this, emu);   // must be 2nd device
56 #if defined(_USE_QT)
57         dummy->set_device_name(_T("1st Dummy"));
58         event->set_device_name(_T("EVENT"));
59 #endif  
60         
61         if(config.printer_device_type == 0) {
62                 printer = new PRNFILE(this, emu);
63         } else if(config.printer_device_type == 1) {
64                 printer = new MZ1P17(this, emu);
65         } else {
66                 printer = dummy;
67         }
68         dma = new I8237(this, emu);
69         pio = new I8255(this, emu);
70         pic = new I8259(this, emu);
71 #if defined(HAS_I286) || defined(HAS_I186)
72         cpu = new I286(this, emu);
73 #else
74         cpu = new I86(this, emu);
75 #endif
76         io = new IO(this, emu);
77         div = new LS393(this, emu);
78         io->set_device_name(_T("MAIN I/O"));
79   #if defined(_MZ6550)
80         cpu->set_device_name(_T("MAIN CPU(i286)"));
81   #else
82         cpu->set_device_name(_T("MAIN CPU(i8086)"));
83   #endif
84         div->set_device_name(_T("74LS393(DIVIDER)"));
85         not_data0 = new NOT(this, emu);
86         not_data0->set_device_name(_T("NOT Gate (Printer Bit0)"));
87         not_data1 = new NOT(this, emu);
88         not_data1->set_device_name(_T("NOT Gate (Printer Bit1)"));
89         not_data2 = new NOT(this, emu);
90         not_data2->set_device_name(_T("NOT Gate (Printer Bit2)"));
91         not_data3 = new NOT(this, emu);
92         not_data3->set_device_name(_T("NOT Gate (Printer Bit3)"));
93         not_data4 = new NOT(this, emu);
94         not_data4->set_device_name(_T("NOT Gate (Printer Bit4)"));
95         not_data5 = new NOT(this, emu);
96         not_data5->set_device_name(_T("NOT Gate (Printer Bit5)"));
97         not_data6 = new NOT(this, emu);
98         not_data6->set_device_name(_T("NOT Gate (Printer Bit6)"));
99         not_data7 = new NOT(this, emu);
100         not_data7->set_device_name(_T("NOT Gate (Printer Bit7)"));
101         not_busy = new NOT(this, emu);
102         not_busy->set_device_name(_T("NOT Gate (Printer Busy)"));
103         rtc = new RP5C01(this, emu);
104         gdc = new UPD7220(this, emu);
105         fdc = new UPD765A(this, emu);
106 //      psg = new YM2203(this, emu);
107         psg = new AY_3_891X(this, emu); // AY-3-8912
108         ctc0 = new Z80CTC(this, emu);
109 #if defined(_MZ6500) || defined(_MZ6550)
110         ctc1 = new Z80CTC(this, emu);
111 #endif
112         sio = new Z80SIO(this, emu);
113         
114         display = new DISPLAY(this, emu);
115         keyboard = new KEYBOARD(this, emu);
116         memory = new MEMORY(this, emu);
117         sysport = new SYSPORT(this, emu);
118 #if defined(_USE_QT)
119         display->set_device_name(_T("DISPLAY I/F"));
120         keyboard->set_device_name(_T("KEYBOARD I/F"));
121         memory->set_device_name(_T("MEMORY"));
122         sysport->set_device_name(_T("SYSTEM PORT"));
123 #endif  
124         // set contexts
125         event->set_context_cpu(cpu);
126         event->set_context_sound(psg);
127 #if defined(USE_SOUND_FILES)
128         if(fdc->load_sound_data(UPD765A_SND_TYPE_SEEK, _T("FDDSEEK.WAV"))) {
129                 event->set_context_sound(fdc);
130         }
131 #endif  
132         
133         if(config.printer_device_type == 0) {
134                 PRNFILE *prnfile = (PRNFILE *)printer;
135                 prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
136                 prnfile->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
137         } else if(config.printer_device_type == 1) {
138                 MZ1P17 *mz1p17 = (MZ1P17 *)printer;
139                 mz1p17->mode = MZ1P17_MODE_MZ1;
140                 mz1p17->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
141                 mz1p17->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
142         }
143         dma->set_context_memory(memory);
144         dma->set_context_ch1(fdc);
145         pio->set_context_port_a(not_data0, SIG_NOT_INPUT, 0x01, 0);
146         pio->set_context_port_a(not_data1, SIG_NOT_INPUT, 0x02, 0);
147         pio->set_context_port_a(not_data2, SIG_NOT_INPUT, 0x04, 0);
148         pio->set_context_port_a(not_data3, SIG_NOT_INPUT, 0x08, 0);
149         pio->set_context_port_a(not_data4, SIG_NOT_INPUT, 0x10, 0);
150         pio->set_context_port_a(not_data5, SIG_NOT_INPUT, 0x20, 0);
151         pio->set_context_port_a(not_data6, SIG_NOT_INPUT, 0x40, 0);
152         pio->set_context_port_a(not_data7, SIG_NOT_INPUT, 0x80, 0);
153         pio->set_context_port_c(keyboard, SIG_KEYBOARD_INPUT, 0x03, 0);
154         pio->set_context_port_c(pic, SIG_I8259_IR2 | SIG_I8259_CHIP0, 0x08, 0);
155         pio->set_context_port_c(printer, SIG_PRINTER_STROBE, 0x20, 0);
156         pic->set_context_cpu(cpu);
157         div->set_context_2qb(ctc0, SIG_Z80CTC_TRIG_3, 1);
158 #if defined(_MZ6500) || defined(_MZ6550)
159         div->set_context_1qb(ctc1, SIG_Z80CTC_TRIG_0, 1);
160         div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_1, 1);
161         div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_2, 1);
162         div->set_context_2qd(ctc1, SIG_Z80CTC_TRIG_3, 1);
163 #endif
164         rtc->set_context_alarm(pic, SIG_I8259_IR0 | SIG_I8259_CHIP1, 1);
165         gdc->set_vram_ptr(memory->get_vram(), 0x80000);
166         gdc->set_context_vsync(pic, SIG_I8259_IR0 | SIG_I8259_CHIP0, 1);
167         fdc->set_context_irq(pic, SIG_I8259_IR1 | SIG_I8259_CHIP1, 1);
168         fdc->set_context_drq(dma, SIG_I8237_CH1, 1);
169         psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP0, 0x20, 0);
170         psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP1, 0x40, 0);
171         psg->set_context_port_a(memory, SIG_MEMORY_BANK, 0xe0, 0);
172         ctc0->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
173         ctc0->set_context_zc0(div, SIG_LS393_CLK, 1);
174         ctc0->set_context_zc1(sio, SIG_Z80SIO_TX_CLK_CH0, 1);
175         ctc0->set_context_zc1(sio, SIG_Z80SIO_RX_CLK_CH0, 1);
176         ctc0->set_context_zc2(sio, SIG_Z80SIO_TX_CLK_CH1, 1);
177         ctc0->set_context_zc2(sio, SIG_Z80SIO_RX_CLK_CH1, 1);
178 #if defined(_MZ6500) || defined(_MZ6550)
179         ctc0->set_context_child(ctc1);
180         ctc1->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
181 #endif
182         sio->set_context_intr(pic, SIG_I8259_IR1 | SIG_I8259_CHIP0);
183         
184         not_data0->set_context_out(printer, SIG_PRINTER_DATA, 0x01);
185         not_data1->set_context_out(printer, SIG_PRINTER_DATA, 0x02);
186         not_data2->set_context_out(printer, SIG_PRINTER_DATA, 0x04);
187         not_data3->set_context_out(printer, SIG_PRINTER_DATA, 0x08);
188         not_data4->set_context_out(printer, SIG_PRINTER_DATA, 0x10);
189         not_data5->set_context_out(printer, SIG_PRINTER_DATA, 0x20);
190         not_data6->set_context_out(printer, SIG_PRINTER_DATA, 0x40);
191         not_data7->set_context_out(printer, SIG_PRINTER_DATA, 0x80);
192         not_busy->set_context_out(pio, SIG_I8255_PORT_B, 0x01);
193         display->set_vram_ptr(memory->get_vram());
194         display->set_sync_ptr(gdc->get_sync());
195         display->set_ra_ptr(gdc->get_ra());
196         display->set_cs_ptr(gdc->get_cs());
197         display->set_ead_ptr(gdc->get_ead());
198         keyboard->set_context_pio(pio);
199         keyboard->set_context_pic(pic);
200         memory->set_context_cpu(cpu);
201         sysport->set_context_fdc(fdc);
202         sysport->set_context_ctc(ctc0);
203         sysport->set_context_sio(sio);
204         
205         // cpu bus
206         cpu->set_context_mem(memory);
207         cpu->set_context_io(io);
208         cpu->set_context_intr(pic);
209 #ifdef SINGLE_MODE_DMA
210         cpu->set_context_dma(dma);
211 #endif
212 #ifdef USE_DEBUGGER
213         cpu->set_context_debugger(new DEBUGGER(this, emu));
214 #endif
215         
216         // i/o bus
217         io->set_iomap_range_rw(0x00, 0x0f, dma);
218         io->set_iomap_range_rw(0x10, 0x1f, pio);
219         io->set_iomap_range_rw(0x20, 0x2f, fdc);
220         for(int i = 0x30; i < 0x40; i += 2) {
221                 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP0 | ((i >> 1) & 1));
222         }
223         for(int i = 0x40; i < 0x50; i += 2) {
224                 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP1 | ((i >> 1) & 1));
225         }
226         io->set_iomap_range_w(0x50, 0x5f, memory);
227         io->set_iomap_range_r(0x60, 0x6f, sysport);
228         io->set_iomap_range_w(0x70, 0x7f, sysport);
229 #if defined(_MZ6500) || defined(_MZ6550)
230         io->set_iomap_single_rw(0xcd, memory);
231 #endif
232         for(int i = 0x100; i < 0x110; i += 2) {
233                 io->set_iomap_alias_rw(i, gdc, (i >> 1) & 1);
234         }
235         io->set_iomap_range_rw(0x110, 0x17f, display);
236         io->set_iomap_range_rw(0x200, 0x20f, sio);
237         io->set_iomap_range_rw(0x210, 0x21f, ctc0);
238         io->set_iomap_range_rw(0x220, 0x22f, rtc);
239         for(int i = 0x230; i < 0x240; i++) {
240                 io->set_iomap_alias_rw(i, psg, ~i & 1);
241         }
242         io->set_iomap_range_r(0x240, 0x25f, sysport);
243         io->set_iomap_range_w(0x260, 0x26f, sysport);
244         io->set_iomap_range_r(0x270, 0x27f, sysport);
245         
246         // initialize all devices
247         for(DEVICE* device = first_device; device; device = device->next_device) {
248                 device->initialize();
249         }
250         for(int i = 0; i < 4; i++) {
251 #if defined(_MZ6500) || defined(_MZ6550)
252                 fdc->set_drive_type(i, DRIVE_TYPE_2HD);
253 #else
254                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
255 #endif
256         }
257 }
258
259 VM::~VM()
260 {
261         // delete all devices
262         for(DEVICE* device = first_device; device;) {
263                 DEVICE *next_device = device->next_device;
264                 device->release();
265                 delete device;
266                 device = next_device;
267         }
268 }
269
270 DEVICE* VM::get_device(int id)
271 {
272         for(DEVICE* device = first_device; device; device = device->next_device) {
273                 if(device->this_device_id == id) {
274                         return device;
275                 }
276         }
277         return NULL;
278 }
279
280 // ----------------------------------------------------------------------------
281 // drive virtual machine
282 // ----------------------------------------------------------------------------
283
284 void VM::reset()
285 {
286         // reset all devices
287         for(DEVICE* device = first_device; device; device = device->next_device) {
288                 device->reset();
289         }
290         not_busy->write_signal(SIG_NOT_INPUT, 0, 0);            // busy = low
291         pio->write_signal(SIG_I8255_PORT_B, 0x03, 0x07);        // busy = ~(low), pe = ~(low), pdtr = ~(high)
292         pio->write_signal(SIG_I8255_PORT_C, 0x40, 0x40);        // ack = high
293 }
294
295 void VM::special_reset()
296 {
297         // nmi
298         cpu->write_signal(SIG_CPU_NMI, 1, 1);
299         sysport->nmi_reset();
300 }
301
302 void VM::run()
303 {
304         event->drive();
305 }
306
307 double VM::get_frame_rate()
308 {
309         return event->get_frame_rate();
310 }
311
312 // ----------------------------------------------------------------------------
313 // debugger
314 // ----------------------------------------------------------------------------
315
316 #ifdef USE_DEBUGGER
317 DEVICE *VM::get_cpu(int index)
318 {
319         if(index == 0) {
320                 return cpu;
321         }
322         return NULL;
323 }
324 #endif
325
326 // ----------------------------------------------------------------------------
327 // draw screen
328 // ----------------------------------------------------------------------------
329
330 void VM::draw_screen()
331 {
332         display->draw_screen();
333 }
334
335 uint32_t VM::get_access_lamp_status()
336 {
337         uint32_t status = fdc->read_signal(0);
338         return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
339 }
340
341 // ----------------------------------------------------------------------------
342 // soud manager
343 // ----------------------------------------------------------------------------
344
345 void VM::initialize_sound(int rate, int samples)
346 {
347         // init sound manager
348         event->initialize_sound(rate, samples);
349         
350         // init sound gen
351         psg->initialize_sound(rate, 4000000, samples, 0, 0);
352 }
353
354 uint16_t* VM::create_sound(int* extra_frames)
355 {
356         return event->create_sound(extra_frames);
357 }
358
359 int VM::get_sound_buffer_ptr()
360 {
361         return event->get_sound_buffer_ptr();
362 }
363
364 #ifdef USE_SOUND_VOLUME
365 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
366 {
367         if(ch == 0) {
368                 psg->set_volume(1, decibel_l, decibel_r);
369         }
370 #if defined(USE_SOUND_FILES)
371         else if(ch == 1) {
372                 fdc->set_volume(UPD765A_SND_TYPE_SEEK, decibel_l, decibel_r);
373         }
374 #endif
375 }
376 #endif
377
378 // ----------------------------------------------------------------------------
379 // notify key
380 // ----------------------------------------------------------------------------
381
382 void VM::key_down(int code, bool repeat)
383 {
384         keyboard->key_down(code);
385 }
386
387 void VM::key_up(int code)
388 {
389 //      keyboard->key_up(code);
390 }
391
392 // ----------------------------------------------------------------------------
393 // user interface
394 // ----------------------------------------------------------------------------
395
396 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
397 {
398         fdc->open_disk(drv, file_path, bank);
399 }
400
401 void VM::close_floppy_disk(int drv)
402 {
403         fdc->close_disk(drv);
404 }
405
406 bool VM::is_floppy_disk_inserted(int drv)
407 {
408         return fdc->is_disk_inserted(drv);
409 }
410
411 void VM::is_floppy_disk_protected(int drv, bool value)
412 {
413         fdc->is_disk_protected(drv, value);
414 }
415
416 bool VM::is_floppy_disk_protected(int drv)
417 {
418         return fdc->is_disk_protected(drv);
419 }
420
421 bool VM::is_frame_skippable()
422 {
423         return event->is_frame_skippable();
424 }
425
426 void VM::update_config()
427 {
428         for(DEVICE* device = first_device; device; device = device->next_device) {
429                 device->update_config();
430         }
431 }
432
433 #define STATE_VERSION   3
434
435 void VM::save_state(FILEIO* state_fio)
436 {
437         state_fio->FputUint32(STATE_VERSION);
438         
439         for(DEVICE* device = first_device; device; device = device->next_device) {
440                 device->save_state(state_fio);
441         }
442 }
443
444 bool VM::load_state(FILEIO* state_fio)
445 {
446         if(state_fio->FgetUint32() != STATE_VERSION) {
447                 return false;
448         }
449         for(DEVICE* device = first_device; device; device = device->next_device) {
450                 if(!device->load_state(state_fio)) {
451                         return false;
452                 }
453         }
454         return true;
455 }
456