OSDN Git Service

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