OSDN Git Service

[VM][STATE] Use namespace {VMNAME} to separate per VMs.
[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 using MZ5500::DISPLAY;
48 using MZ5500::KEYBOARD;
49 using MZ5500::MEMORY;
50 using MZ5500::SYSPORT;
51
52 // ----------------------------------------------------------------------------
53 // initialize
54 // ----------------------------------------------------------------------------
55
56 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
57 {
58         // create devices
59         first_device = last_device = NULL;
60         dummy = new DEVICE(this, emu);  // must be 1st device
61         event = new EVENT(this, emu);   // must be 2nd device
62         dummy->set_device_name(_T("1st Dummy"));
63         
64         if(config.printer_type == 0) {
65                 printer = new PRNFILE(this, emu);
66         } else if(config.printer_type == 1) {
67                 printer = new MZ1P17(this, emu);
68         } else {
69                 printer = dummy;
70         }
71         dma = new I8237(this, emu);
72         pio = new I8255(this, emu);
73         pic = new I8259(this, emu);
74 //#if defined(HAS_I286) || defined(HAS_I186)
75         cpu = new I286(this, emu);
76 //#else
77 //      cpu = new I86(this, emu);
78 //#endif
79         io = new IO(this, emu);
80         div = new LS393(this, emu);
81         not_data0 = new NOT(this, emu);
82         not_data0->set_device_name(_T("NOT Gate (Printer Bit0)"));
83         not_data1 = new NOT(this, emu);
84         not_data1->set_device_name(_T("NOT Gate (Printer Bit1)"));
85         not_data2 = new NOT(this, emu);
86         not_data2->set_device_name(_T("NOT Gate (Printer Bit2)"));
87         not_data3 = new NOT(this, emu);
88         not_data3->set_device_name(_T("NOT Gate (Printer Bit3)"));
89         not_data4 = new NOT(this, emu);
90         not_data4->set_device_name(_T("NOT Gate (Printer Bit4)"));
91         not_data5 = new NOT(this, emu);
92         not_data5->set_device_name(_T("NOT Gate (Printer Bit5)"));
93         not_data6 = new NOT(this, emu);
94         not_data6->set_device_name(_T("NOT Gate (Printer Bit6)"));
95         not_data7 = new NOT(this, emu);
96         not_data7->set_device_name(_T("NOT Gate (Printer Bit7)"));
97         not_busy = new NOT(this, emu);
98         not_busy->set_device_name(_T("NOT Gate (Printer Busy)"));
99         rtc = new RP5C01(this, emu);
100         gdc = new UPD7220(this, emu);
101         fdc = new UPD765A(this, emu);
102         fdc->set_context_noise_seek(new NOISE(this, emu));
103         fdc->set_context_noise_head_down(new NOISE(this, emu));
104         fdc->set_context_noise_head_up(new NOISE(this, emu));
105 //      psg = new YM2203(this, emu);
106         psg = new AY_3_891X(this, emu); // AY-3-8912
107         ctc0 = new Z80CTC(this, emu);
108 #if defined(_MZ6500) || defined(_MZ6550)
109         ctc1 = new Z80CTC(this, emu);
110 #endif
111         sio = new Z80SIO(this, emu);
112         
113         display = new DISPLAY(this, emu);
114         keyboard = new KEYBOARD(this, emu);
115         memory = new MEMORY(this, emu);
116         sysport = new SYSPORT(this, emu);
117
118         // set contexts
119         event->set_context_cpu(cpu);
120         event->set_context_sound(psg);
121         event->set_context_sound(fdc->get_context_noise_seek());
122         event->set_context_sound(fdc->get_context_noise_head_down());
123         event->set_context_sound(fdc->get_context_noise_head_up());
124         
125         if(config.printer_type == 0) {
126                 PRNFILE *prnfile = (PRNFILE *)printer;
127                 prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
128                 prnfile->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
129         } else if(config.printer_type == 1) {
130                 MZ1P17 *mz1p17 = (MZ1P17 *)printer;
131                 mz1p17->mode = MZ1P17_MODE_MZ1;
132                 mz1p17->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
133                 mz1p17->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
134         }
135         dma->set_context_memory(memory);
136         dma->set_context_ch1(fdc);
137         pio->set_context_port_a(not_data0, SIG_NOT_INPUT, 0x01, 0);
138         pio->set_context_port_a(not_data1, SIG_NOT_INPUT, 0x02, 0);
139         pio->set_context_port_a(not_data2, SIG_NOT_INPUT, 0x04, 0);
140         pio->set_context_port_a(not_data3, SIG_NOT_INPUT, 0x08, 0);
141         pio->set_context_port_a(not_data4, SIG_NOT_INPUT, 0x10, 0);
142         pio->set_context_port_a(not_data5, SIG_NOT_INPUT, 0x20, 0);
143         pio->set_context_port_a(not_data6, SIG_NOT_INPUT, 0x40, 0);
144         pio->set_context_port_a(not_data7, SIG_NOT_INPUT, 0x80, 0);
145         pio->set_context_port_c(keyboard, SIG_KEYBOARD_INPUT, 0x03, 0);
146         pio->set_context_port_c(pic, SIG_I8259_IR2 | SIG_I8259_CHIP0, 0x08, 0);
147         pio->set_context_port_c(printer, SIG_PRINTER_STROBE, 0x20, 0);
148         pic->set_context_cpu(cpu);
149         div->set_context_2qb(ctc0, SIG_Z80CTC_TRIG_3, 1);
150 #if defined(_MZ6500) || defined(_MZ6550)
151         div->set_context_1qb(ctc1, SIG_Z80CTC_TRIG_0, 1);
152         div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_1, 1);
153         div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_2, 1);
154         div->set_context_2qd(ctc1, SIG_Z80CTC_TRIG_3, 1);
155 #endif
156         rtc->set_context_alarm(pic, SIG_I8259_IR0 | SIG_I8259_CHIP1, 1);
157         gdc->set_vram_ptr(memory->get_vram(), 0x80000);
158         gdc->set_context_vsync(pic, SIG_I8259_IR0 | SIG_I8259_CHIP0, 1);
159         fdc->set_context_irq(pic, SIG_I8259_IR1 | SIG_I8259_CHIP1, 1);
160         fdc->set_context_drq(dma, SIG_I8237_CH1, 1);
161         psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP0, 0x20, 0);
162         psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP1, 0x40, 0);
163         psg->set_context_port_a(memory, SIG_MEMORY_BANK, 0xe0, 0);
164         ctc0->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
165         ctc0->set_context_zc0(div, SIG_LS393_CLK, 1);
166         ctc0->set_context_zc1(sio, SIG_Z80SIO_TX_CLK_CH0, 1);
167         ctc0->set_context_zc1(sio, SIG_Z80SIO_RX_CLK_CH0, 1);
168         ctc0->set_context_zc2(sio, SIG_Z80SIO_TX_CLK_CH1, 1);
169         ctc0->set_context_zc2(sio, SIG_Z80SIO_RX_CLK_CH1, 1);
170 #if defined(_MZ6500) || defined(_MZ6550)
171         ctc0->set_context_child(ctc1);
172         ctc1->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
173 #endif
174         sio->set_context_intr(pic, SIG_I8259_IR1 | SIG_I8259_CHIP0);
175         
176         not_data0->set_context_out(printer, SIG_PRINTER_DATA, 0x01);
177         not_data1->set_context_out(printer, SIG_PRINTER_DATA, 0x02);
178         not_data2->set_context_out(printer, SIG_PRINTER_DATA, 0x04);
179         not_data3->set_context_out(printer, SIG_PRINTER_DATA, 0x08);
180         not_data4->set_context_out(printer, SIG_PRINTER_DATA, 0x10);
181         not_data5->set_context_out(printer, SIG_PRINTER_DATA, 0x20);
182         not_data6->set_context_out(printer, SIG_PRINTER_DATA, 0x40);
183         not_data7->set_context_out(printer, SIG_PRINTER_DATA, 0x80);
184         not_busy->set_context_out(pio, SIG_I8255_PORT_B, 0x01);
185         display->set_vram_ptr(memory->get_vram());
186         display->set_sync_ptr(gdc->get_sync());
187         display->set_ra_ptr(gdc->get_ra());
188         display->set_cs_ptr(gdc->get_cs());
189         display->set_ead_ptr(gdc->get_ead());
190         keyboard->set_context_pio(pio);
191         keyboard->set_context_pic(pic);
192         memory->set_context_cpu(cpu);
193         sysport->set_context_fdc(fdc);
194         sysport->set_context_ctc(ctc0);
195         sysport->set_context_sio(sio);
196         
197         // cpu bus
198         cpu->set_context_mem(memory);
199         cpu->set_context_io(io);
200         cpu->set_context_intr(pic);
201 #ifdef SINGLE_MODE_DMA
202         cpu->set_context_dma(dma);
203 #endif
204 #ifdef USE_DEBUGGER
205         cpu->set_context_debugger(new DEBUGGER(this, emu));
206 #endif
207         
208         // i/o bus
209         io->set_iomap_range_rw(0x00, 0x0f, dma);
210         io->set_iomap_range_rw(0x10, 0x1f, pio);
211         io->set_iomap_range_rw(0x20, 0x2f, fdc);
212         for(int i = 0x30; i < 0x40; i += 2) {
213                 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP0 | ((i >> 1) & 1));
214         }
215         for(int i = 0x40; i < 0x50; i += 2) {
216                 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP1 | ((i >> 1) & 1));
217         }
218         io->set_iomap_range_w(0x50, 0x5f, memory);
219         io->set_iomap_range_r(0x60, 0x6f, sysport);
220         io->set_iomap_range_w(0x70, 0x7f, sysport);
221 #if defined(_MZ6500) || defined(_MZ6550)
222         io->set_iomap_single_rw(0xcd, memory);
223 #endif
224         for(int i = 0x100; i < 0x110; i += 2) {
225                 io->set_iomap_alias_rw(i, gdc, (i >> 1) & 1);
226         }
227         io->set_iomap_range_rw(0x110, 0x17f, display);
228         io->set_iomap_range_rw(0x200, 0x20f, sio);
229         io->set_iomap_range_rw(0x210, 0x21f, ctc0);
230         io->set_iomap_range_rw(0x220, 0x22f, rtc);
231         for(int i = 0x230; i < 0x240; i++) {
232                 io->set_iomap_alias_rw(i, psg, ~i & 1);
233         }
234         io->set_iomap_range_r(0x240, 0x25f, sysport);
235         io->set_iomap_range_w(0x260, 0x26f, sysport);
236         io->set_iomap_range_r(0x270, 0x27f, sysport);
237         
238         // initialize all devices
239 #if defined(__GIT_REPO_VERSION)
240         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
241 #endif
242         for(DEVICE* device = first_device; device; device = device->next_device) {
243                 device->initialize();
244         }
245         for(int i = 0; i < 4; i++) {
246 #if defined(_MZ6500) || defined(_MZ6550)
247                 fdc->set_drive_type(i, DRIVE_TYPE_2HD);
248 #else
249                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
250 #endif
251         }
252 }
253
254 VM::~VM()
255 {
256         // delete all devices
257         for(DEVICE* device = first_device; device;) {
258                 DEVICE *next_device = device->next_device;
259                 device->release();
260                 delete device;
261                 device = next_device;
262         }
263 }
264
265 DEVICE* VM::get_device(int id)
266 {
267         for(DEVICE* device = first_device; device; device = device->next_device) {
268                 if(device->this_device_id == id) {
269                         return device;
270                 }
271         }
272         return NULL;
273 }
274
275 // ----------------------------------------------------------------------------
276 // drive virtual machine
277 // ----------------------------------------------------------------------------
278
279 void VM::reset()
280 {
281         // reset all devices
282         for(DEVICE* device = first_device; device; device = device->next_device) {
283                 device->reset();
284         }
285         not_busy->write_signal(SIG_NOT_INPUT, 0, 0);            // busy = low
286         pio->write_signal(SIG_I8255_PORT_B, 0x03, 0x07);        // busy = ~(low), pe = ~(low), pdtr = ~(high)
287         pio->write_signal(SIG_I8255_PORT_C, 0x40, 0x40);        // ack = high
288 }
289
290 void VM::special_reset()
291 {
292         // nmi
293         cpu->write_signal(SIG_CPU_NMI, 1, 1);
294         sysport->nmi_reset();
295 }
296
297 void VM::run()
298 {
299         event->drive();
300 }
301
302 double VM::get_frame_rate()
303 {
304         return event->get_frame_rate();
305 }
306
307 // ----------------------------------------------------------------------------
308 // debugger
309 // ----------------------------------------------------------------------------
310
311 #ifdef USE_DEBUGGER
312 DEVICE *VM::get_cpu(int index)
313 {
314         if(index == 0) {
315                 return cpu;
316         }
317         return NULL;
318 }
319 #endif
320
321 // ----------------------------------------------------------------------------
322 // draw screen
323 // ----------------------------------------------------------------------------
324
325 void VM::draw_screen()
326 {
327         display->draw_screen();
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         } else if(ch == 1) {
359                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
360                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
361                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
362         }
363 }
364 #endif
365
366 // ----------------------------------------------------------------------------
367 // notify key
368 // ----------------------------------------------------------------------------
369
370 void VM::key_down(int code, bool repeat)
371 {
372         keyboard->key_down(code);
373 }
374
375 void VM::key_up(int code)
376 {
377 //      keyboard->key_up(code);
378 }
379
380 bool VM::get_caps_locked()
381 {
382         return keyboard->get_caps_locked();
383 }
384
385 bool VM::get_kana_locked()
386 {
387         return keyboard->get_kana_locked();
388 }
389
390 // ----------------------------------------------------------------------------
391 // user interface
392 // ----------------------------------------------------------------------------
393
394 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
395 {
396         fdc->open_disk(drv, file_path, bank);
397 }
398
399 void VM::close_floppy_disk(int drv)
400 {
401         fdc->close_disk(drv);
402 }
403
404 bool VM::is_floppy_disk_inserted(int drv)
405 {
406         return fdc->is_disk_inserted(drv);
407 }
408
409 void VM::is_floppy_disk_protected(int drv, bool value)
410 {
411         fdc->is_disk_protected(drv, value);
412 }
413
414 bool VM::is_floppy_disk_protected(int drv)
415 {
416         return fdc->is_disk_protected(drv);
417 }
418
419 uint32_t VM::is_floppy_disk_accessed()
420 {
421         return fdc->read_signal(0);
422 }
423
424 bool VM::is_frame_skippable()
425 {
426         return event->is_frame_skippable();
427 }
428
429 void VM::update_config()
430 {
431         for(DEVICE* device = first_device; device; device = device->next_device) {
432                 device->update_config();
433         }
434 }
435
436 #define STATE_VERSION   5
437
438 bool VM::process_state(FILEIO* state_fio, bool loading)
439 {
440         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
441                 return false;
442         }
443         for(DEVICE* device = first_device; device; device = device->next_device) {
444                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
445                 // const char *name = typeid(*device).name();
446                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
447                 const char *name = device->get_device_name();
448                 int len = strlen(name);
449                 
450                 if(!state_fio->StateCheckInt32(len)) {
451                         if(loading) {
452                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
453                         }
454                         return false;
455                 }
456                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
457                         if(loading) {
458                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
459                         }
460                         return false;
461                 }
462                 if(!device->process_state(state_fio, loading)) {
463                         if(loading) {
464                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
465                         }
466                         return false;
467                 }
468         }
469         return true;
470 }