OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[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 "../noise.h"
28 #include "../not.h"
29 #include "../prnfile.h"
30 #include "../rp5c01.h"
31 #include "../upd7220.h"
32 #include "../upd765a.h"
33 //#include "../ym2203.h"
34 #include "../ay_3_891x.h"
35 #include "../z80ctc.h"
36 #include "../z80sio.h"
37
38 #ifdef USE_DEBUGGER
39 #include "../debugger.h"
40 #endif
41
42 #include "display.h"
43 #include "keyboard.h"
44 #include "memory.h"
45 #include "sysport.h"
46
47 // ----------------------------------------------------------------------------
48 // initialize
49 // ----------------------------------------------------------------------------
50
51 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
52 {
53         // create devices
54         first_device = last_device = NULL;
55         dummy = new DEVICE(this, emu);  // must be 1st device
56         event = new EVENT(this, emu);   // must be 2nd device
57         dummy->set_device_name(_T("1st Dummy"));
58         
59         if(config.printer_type == 0) {
60                 printer = new PRNFILE(this, emu);
61         } else if(config.printer_type == 1) {
62                 printer = new MZ1P17(this, emu);
63         } else {
64                 printer = dummy;
65         }
66         dma = new I8237(this, emu);
67         pio = new I8255(this, emu);
68         pic = new I8259(this, emu);
69 //#if defined(HAS_I286) || defined(HAS_I186)
70         cpu = new I286(this, emu);
71 //#else
72 //      cpu = new I86(this, emu);
73 //#endif
74         io = new IO(this, emu);
75         div = new LS393(this, emu);
76         not_data0 = new NOT(this, emu);
77         not_data0->set_device_name(_T("NOT Gate (Printer Bit0)"));
78         not_data1 = new NOT(this, emu);
79         not_data1->set_device_name(_T("NOT Gate (Printer Bit1)"));
80         not_data2 = new NOT(this, emu);
81         not_data2->set_device_name(_T("NOT Gate (Printer Bit2)"));
82         not_data3 = new NOT(this, emu);
83         not_data3->set_device_name(_T("NOT Gate (Printer Bit3)"));
84         not_data4 = new NOT(this, emu);
85         not_data4->set_device_name(_T("NOT Gate (Printer Bit4)"));
86         not_data5 = new NOT(this, emu);
87         not_data5->set_device_name(_T("NOT Gate (Printer Bit5)"));
88         not_data6 = new NOT(this, emu);
89         not_data6->set_device_name(_T("NOT Gate (Printer Bit6)"));
90         not_data7 = new NOT(this, emu);
91         not_data7->set_device_name(_T("NOT Gate (Printer Bit7)"));
92         not_busy = new NOT(this, emu);
93         not_busy->set_device_name(_T("NOT Gate (Printer Busy)"));
94         rtc = new RP5C01(this, emu);
95         gdc = new UPD7220(this, emu);
96         fdc = new UPD765A(this, emu);
97         fdc->set_context_noise_seek(new NOISE(this, emu));
98         fdc->set_context_noise_head_down(new NOISE(this, emu));
99         fdc->set_context_noise_head_up(new NOISE(this, emu));
100 //      psg = new YM2203(this, emu);
101         psg = new AY_3_891X(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
113         // set contexts
114         event->set_context_cpu(cpu);
115         event->set_context_sound(psg);
116         event->set_context_sound(fdc->get_context_noise_seek());
117         event->set_context_sound(fdc->get_context_noise_head_down());
118         event->set_context_sound(fdc->get_context_noise_head_up());
119         
120         if(config.printer_type == 0) {
121                 PRNFILE *prnfile = (PRNFILE *)printer;
122                 prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
123                 prnfile->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
124         } else if(config.printer_type == 1) {
125                 MZ1P17 *mz1p17 = (MZ1P17 *)printer;
126                 mz1p17->mode = MZ1P17_MODE_MZ1;
127                 mz1p17->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
128                 mz1p17->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
129         }
130         dma->set_context_memory(memory);
131         dma->set_context_ch1(fdc);
132         pio->set_context_port_a(not_data0, SIG_NOT_INPUT, 0x01, 0);
133         pio->set_context_port_a(not_data1, SIG_NOT_INPUT, 0x02, 0);
134         pio->set_context_port_a(not_data2, SIG_NOT_INPUT, 0x04, 0);
135         pio->set_context_port_a(not_data3, SIG_NOT_INPUT, 0x08, 0);
136         pio->set_context_port_a(not_data4, SIG_NOT_INPUT, 0x10, 0);
137         pio->set_context_port_a(not_data5, SIG_NOT_INPUT, 0x20, 0);
138         pio->set_context_port_a(not_data6, SIG_NOT_INPUT, 0x40, 0);
139         pio->set_context_port_a(not_data7, SIG_NOT_INPUT, 0x80, 0);
140         pio->set_context_port_c(keyboard, SIG_KEYBOARD_INPUT, 0x03, 0);
141         pio->set_context_port_c(pic, SIG_I8259_IR2 | SIG_I8259_CHIP0, 0x08, 0);
142         pio->set_context_port_c(printer, SIG_PRINTER_STROBE, 0x20, 0);
143         pic->set_context_cpu(cpu);
144         div->set_context_2qb(ctc0, SIG_Z80CTC_TRIG_3, 1);
145 #if defined(_MZ6500) || defined(_MZ6550)
146         div->set_context_1qb(ctc1, SIG_Z80CTC_TRIG_0, 1);
147         div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_1, 1);
148         div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_2, 1);
149         div->set_context_2qd(ctc1, SIG_Z80CTC_TRIG_3, 1);
150 #endif
151         rtc->set_context_alarm(pic, SIG_I8259_IR0 | SIG_I8259_CHIP1, 1);
152         gdc->set_vram_ptr(memory->get_vram(), 0x80000);
153         gdc->set_context_vsync(pic, SIG_I8259_IR0 | SIG_I8259_CHIP0, 1);
154         fdc->set_context_irq(pic, SIG_I8259_IR1 | SIG_I8259_CHIP1, 1);
155         fdc->set_context_drq(dma, SIG_I8237_CH1, 1);
156         psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP0, 0x20, 0);
157         psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP1, 0x40, 0);
158         psg->set_context_port_a(memory, SIG_MEMORY_BANK, 0xe0, 0);
159         ctc0->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
160         ctc0->set_context_zc0(div, SIG_LS393_CLK, 1);
161         ctc0->set_context_zc1(sio, SIG_Z80SIO_TX_CLK_CH0, 1);
162         ctc0->set_context_zc1(sio, SIG_Z80SIO_RX_CLK_CH0, 1);
163         ctc0->set_context_zc2(sio, SIG_Z80SIO_TX_CLK_CH1, 1);
164         ctc0->set_context_zc2(sio, SIG_Z80SIO_RX_CLK_CH1, 1);
165 #if defined(_MZ6500) || defined(_MZ6550)
166         ctc0->set_context_child(ctc1);
167         ctc1->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
168 #endif
169         sio->set_context_intr(pic, SIG_I8259_IR1 | SIG_I8259_CHIP0);
170         
171         not_data0->set_context_out(printer, SIG_PRINTER_DATA, 0x01);
172         not_data1->set_context_out(printer, SIG_PRINTER_DATA, 0x02);
173         not_data2->set_context_out(printer, SIG_PRINTER_DATA, 0x04);
174         not_data3->set_context_out(printer, SIG_PRINTER_DATA, 0x08);
175         not_data4->set_context_out(printer, SIG_PRINTER_DATA, 0x10);
176         not_data5->set_context_out(printer, SIG_PRINTER_DATA, 0x20);
177         not_data6->set_context_out(printer, SIG_PRINTER_DATA, 0x40);
178         not_data7->set_context_out(printer, SIG_PRINTER_DATA, 0x80);
179         not_busy->set_context_out(pio, SIG_I8255_PORT_B, 0x01);
180         display->set_vram_ptr(memory->get_vram());
181         display->set_sync_ptr(gdc->get_sync());
182         display->set_ra_ptr(gdc->get_ra());
183         display->set_cs_ptr(gdc->get_cs());
184         display->set_ead_ptr(gdc->get_ead());
185         keyboard->set_context_pio(pio);
186         keyboard->set_context_pic(pic);
187         memory->set_context_cpu(cpu);
188         sysport->set_context_fdc(fdc);
189         sysport->set_context_ctc(ctc0);
190         sysport->set_context_sio(sio);
191         
192         // cpu bus
193         cpu->set_context_mem(memory);
194         cpu->set_context_io(io);
195         cpu->set_context_intr(pic);
196 #ifdef SINGLE_MODE_DMA
197         cpu->set_context_dma(dma);
198 #endif
199 #ifdef USE_DEBUGGER
200         cpu->set_context_debugger(new DEBUGGER(this, emu));
201 #endif
202         
203         // i/o bus
204         io->set_iomap_range_rw(0x00, 0x0f, dma);
205         io->set_iomap_range_rw(0x10, 0x1f, pio);
206         io->set_iomap_range_rw(0x20, 0x2f, fdc);
207         for(int i = 0x30; i < 0x40; i += 2) {
208                 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP0 | ((i >> 1) & 1));
209         }
210         for(int i = 0x40; i < 0x50; i += 2) {
211                 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP1 | ((i >> 1) & 1));
212         }
213         io->set_iomap_range_w(0x50, 0x5f, memory);
214         io->set_iomap_range_r(0x60, 0x6f, sysport);
215         io->set_iomap_range_w(0x70, 0x7f, sysport);
216 #if defined(_MZ6500) || defined(_MZ6550)
217         io->set_iomap_single_rw(0xcd, memory);
218 #endif
219         for(int i = 0x100; i < 0x110; i += 2) {
220                 io->set_iomap_alias_rw(i, gdc, (i >> 1) & 1);
221         }
222         io->set_iomap_range_rw(0x110, 0x17f, display);
223         io->set_iomap_range_rw(0x200, 0x20f, sio);
224         io->set_iomap_range_rw(0x210, 0x21f, ctc0);
225         io->set_iomap_range_rw(0x220, 0x22f, rtc);
226         for(int i = 0x230; i < 0x240; i++) {
227                 io->set_iomap_alias_rw(i, psg, ~i & 1);
228         }
229         io->set_iomap_range_r(0x240, 0x25f, sysport);
230         io->set_iomap_range_w(0x260, 0x26f, sysport);
231         io->set_iomap_range_r(0x270, 0x27f, sysport);
232         
233         // initialize all devices
234 #if defined(__GIT_REPO_VERSION)
235         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
236 #endif
237         for(DEVICE* device = first_device; device; device = device->next_device) {
238                 device->initialize();
239         }
240         decl_state();
241         for(int i = 0; i < 4; i++) {
242 #if defined(_MZ6500) || defined(_MZ6550)
243                 fdc->set_drive_type(i, DRIVE_TYPE_2HD);
244 #else
245                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
246 #endif
247         }
248 }
249
250 VM::~VM()
251 {
252         // delete all devices
253         for(DEVICE* device = first_device; device;) {
254                 DEVICE *next_device = device->next_device;
255                 device->release();
256                 delete device;
257                 device = next_device;
258         }
259 }
260
261 DEVICE* VM::get_device(int id)
262 {
263         for(DEVICE* device = first_device; device; device = device->next_device) {
264                 if(device->this_device_id == id) {
265                         return device;
266                 }
267         }
268         return NULL;
269 }
270
271 // ----------------------------------------------------------------------------
272 // drive virtual machine
273 // ----------------------------------------------------------------------------
274
275 void VM::reset()
276 {
277         // reset all devices
278         for(DEVICE* device = first_device; device; device = device->next_device) {
279                 device->reset();
280         }
281         not_busy->write_signal(SIG_NOT_INPUT, 0, 0);            // busy = low
282         pio->write_signal(SIG_I8255_PORT_B, 0x03, 0x07);        // busy = ~(low), pe = ~(low), pdtr = ~(high)
283         pio->write_signal(SIG_I8255_PORT_C, 0x40, 0x40);        // ack = high
284 }
285
286 void VM::special_reset()
287 {
288         // nmi
289         cpu->write_signal(SIG_CPU_NMI, 1, 1);
290         sysport->nmi_reset();
291 }
292
293 void VM::run()
294 {
295         event->drive();
296 }
297
298 double VM::get_frame_rate()
299 {
300         return event->get_frame_rate();
301 }
302
303 // ----------------------------------------------------------------------------
304 // debugger
305 // ----------------------------------------------------------------------------
306
307 #ifdef USE_DEBUGGER
308 DEVICE *VM::get_cpu(int index)
309 {
310         if(index == 0) {
311                 return cpu;
312         }
313         return NULL;
314 }
315 #endif
316
317 // ----------------------------------------------------------------------------
318 // draw screen
319 // ----------------------------------------------------------------------------
320
321 void VM::draw_screen()
322 {
323         display->draw_screen();
324 }
325
326 // ----------------------------------------------------------------------------
327 // soud manager
328 // ----------------------------------------------------------------------------
329
330 void VM::initialize_sound(int rate, int samples)
331 {
332         // init sound manager
333         event->initialize_sound(rate, samples);
334         
335         // init sound gen
336         psg->initialize_sound(rate, 4000000, samples, 0, 0);
337 }
338
339 uint16_t* VM::create_sound(int* extra_frames)
340 {
341         return event->create_sound(extra_frames);
342 }
343
344 int VM::get_sound_buffer_ptr()
345 {
346         return event->get_sound_buffer_ptr();
347 }
348
349 #ifdef USE_SOUND_VOLUME
350 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
351 {
352         if(ch == 0) {
353                 psg->set_volume(1, decibel_l, decibel_r);
354         } else if(ch == 1) {
355                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
356                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
357                 fdc->get_context_noise_head_up()->set_volume(0, 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 bool VM::get_caps_locked()
377 {
378         return keyboard->get_caps_locked();
379 }
380
381 bool VM::get_kana_locked()
382 {
383         return keyboard->get_kana_locked();
384 }
385
386 // ----------------------------------------------------------------------------
387 // user interface
388 // ----------------------------------------------------------------------------
389
390 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
391 {
392         fdc->open_disk(drv, file_path, bank);
393 }
394
395 void VM::close_floppy_disk(int drv)
396 {
397         fdc->close_disk(drv);
398 }
399
400 bool VM::is_floppy_disk_inserted(int drv)
401 {
402         return fdc->is_disk_inserted(drv);
403 }
404
405 void VM::is_floppy_disk_protected(int drv, bool value)
406 {
407         fdc->is_disk_protected(drv, value);
408 }
409
410 bool VM::is_floppy_disk_protected(int drv)
411 {
412         return fdc->is_disk_protected(drv);
413 }
414
415 uint32_t VM::is_floppy_disk_accessed()
416 {
417         return fdc->read_signal(0);
418 }
419
420 bool VM::is_frame_skippable()
421 {
422         return event->is_frame_skippable();
423 }
424
425 void VM::update_config()
426 {
427         for(DEVICE* device = first_device; device; device = device->next_device) {
428                 device->update_config();
429         }
430 }
431
432 #define STATE_VERSION   5
433
434 #include "../../statesub.h"
435 #include "../../qt/gui/csp_logger.h"
436 extern CSP_Logger DLL_PREFIX_I *csp_logger;
437
438 void VM::decl_state(void)
439 {
440 #if defined(_MZ5500)
441         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MZ_5500_HEAD")), csp_logger);
442 #elif defined(_MZ6500)
443         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MZ_6500_HEAD")), csp_logger);
444 #elif defined(_MZ6550)
445         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MZ_6550_HEAD")), csp_logger);
446 #else
447         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MZ_5500_SERIES_HEAD")), csp_logger);
448 #endif
449         
450         for(DEVICE* device = first_device; device; device = device->next_device) {
451                 device->decl_state();
452         }
453 }
454
455 void VM::save_state(FILEIO* state_fio)
456 {
457         //state_fio->FputUint32(STATE_VERSION);
458         
459         if(state_entry != NULL) {
460                 state_entry->save_state(state_fio);
461         }
462         for(DEVICE* device = first_device; device; device = device->next_device) {
463                 device->save_state(state_fio);
464         }
465 }
466
467 bool VM::load_state(FILEIO* state_fio)
468 {
469         //if(state_fio->FgetUint32() != STATE_VERSION) {
470         //      return false;
471         //}
472         bool mb = false;
473         if(state_entry != NULL) {
474                 mb = state_entry->load_state(state_fio);
475         }
476         if(!mb) {
477                 emu->out_debug_log("INFO: HEADER DATA ERROR");
478                 return false;
479         }
480         for(DEVICE* device = first_device; device; device = device->next_device) {
481                 if(!device->load_state(state_fio)) {
482                         return false;
483                 }
484         }
485         return true;
486 }
487