OSDN Git Service

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