OSDN Git Service

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