OSDN Git Service

eab892d2187b0ee4b38b0ba502b1676f0af7be61
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz3500 / mz3500.cpp
1 /*
2         SHARP MZ-3500 Emulator 'EmuZ-3500'
3
4         Author : Takeda.Toshiya
5         Date   : 2010.08.31-
6
7         [ virtual machine ]
8 */
9
10 #include "mz3500.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../disk.h"
16 #include "../i8251.h"
17 #include "../i8253.h"
18 #include "../i8255.h"
19 #include "../io.h"
20 #include "../ls244.h"
21 #include "../mz1p17.h"
22 #include "../noise.h"
23 #include "../not.h"
24 #include "../pcm1bit.h"
25 #include "../prnfile.h"
26 #include "../upd1990a.h"
27 #include "../upd7220.h"
28 #include "../upd765a.h"
29 #include "../z80.h"
30
31 #ifdef USE_DEBUGGER
32 #include "../debugger.h"
33 #endif
34
35 #include "./main.h"
36 #include "./sub.h"
37 #include "keyboard.h"
38
39 // ----------------------------------------------------------------------------
40 // initialize
41 // ----------------------------------------------------------------------------
42
43 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
44 {
45         // create devices
46         first_device = last_device = NULL;
47         dummy = new DEVICE(this, emu);  // must be 1st device
48         event = new EVENT(this, emu);   // must be 2nd device
49         dummy->set_device_name(_T("1st Dummy"));
50
51         // for main cpu
52         mainio = new IO(this, emu);
53         fdc = new UPD765A(this, emu);
54         fdc->set_context_noise_seek(new NOISE(this, emu));
55         fdc->set_context_noise_head_down(new NOISE(this, emu));
56         fdc->set_context_noise_head_up(new NOISE(this, emu));
57         maincpu = new Z80(this, emu);
58         mainbus= new MAIN(this, emu);
59         mainio->set_device_name(_T("I/O Bus (Main)"));
60         maincpu->set_device_name(_T("Z80 CPU (Main)"));
61         mainbus->set_device_name(_T("MAIN BUS"));
62         // for sub cpu
63         if(config.printer_type == 0) {
64                 printer = new PRNFILE(this, emu);
65         } else if(config.printer_type == 1) {
66                 printer = new MZ1P17(this, emu);
67         } else {
68                 printer = dummy;
69         }
70         sio = new I8251(this, emu);
71         pit = new I8253(this, emu);
72         pio = new I8255(this, emu);
73         subio = new IO(this, emu);
74         subio->set_device_name(_T("I/O Bus (Sub)"));
75         ls244 = new LS244(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         pcm = new PCM1BIT(this, emu);
95         rtc = new UPD1990A(this, emu);
96         gdc_chr = new UPD7220(this, emu);
97         gdc_chr->set_device_name(_T("uPD7220 GDC (Character)"));
98         gdc_gfx = new UPD7220(this, emu);
99         gdc_gfx->set_device_name(_T("uPD7220 GDC (Graphics)"));
100         subcpu = new Z80(this, emu);
101         subcpu->set_device_name(_T("Z80 CPU (Sub)"));
102         subbus = new SUB(this, emu);
103         kbd = new KEYBOARD(this, emu);
104         
105         // set contexts
106         event->set_context_cpu(maincpu, CPU_CLOCKS);
107         event->set_context_cpu(subcpu, CPU_CLOCKS);
108         event->set_context_sound(pcm);
109         event->set_context_sound(fdc->get_context_noise_seek());
110         event->set_context_sound(fdc->get_context_noise_head_down());
111         event->set_context_sound(fdc->get_context_noise_head_up());
112         
113         // mz3500sm p.59
114         fdc->set_context_irq(mainbus, SIG_MAIN_INTFD, 1);
115         fdc->set_context_drq(mainbus, SIG_MAIN_DRQ, 1);
116         fdc->set_context_index(mainbus, SIG_MAIN_INDEX, 1);
117         
118         // mz3500sm p.78
119         if(config.printer_type == 0) {
120                 PRNFILE *prnfile = (PRNFILE *)printer;
121                 prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
122                 prnfile->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
123         } else if(config.printer_type == 1) {
124                 MZ1P17 *mz1p17 = (MZ1P17 *)printer;
125                 mz1p17->mode = MZ1P17_MODE_MZ1;
126                 mz1p17->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
127                 mz1p17->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
128                 
129                 // sw1=on, sw2=off, select MZ-1P02 (mz3500sm p.85)
130                 config.dipswitch &= ~0x03;
131                 config.dipswitch |=  0x01;
132         }
133         
134         // mz3500sm p.72,77
135         sio->set_context_rxrdy(subcpu, SIG_CPU_NMI, 1);
136         
137         // mz3500sm p.77,83
138         // i8253 ch.0 -> i8251 txc,rxc
139         pit->set_context_ch1(pcm, SIG_PCM1BIT_SIGNAL, 1);
140         pit->set_context_ch2(pit, SIG_I8253_GATE_1, 1);
141         pit->set_constant_clock(0, 2450760);
142         pit->set_constant_clock(1, 2450760);
143         pit->set_constant_clock(2, 2450760);
144         
145         // mz3500sm p.78,80,81
146         pio->set_context_port_a(not_data0, SIG_NOT_INPUT, 0x01, 0);
147         pio->set_context_port_a(not_data1, SIG_NOT_INPUT, 0x02, 0);
148         pio->set_context_port_a(not_data2, SIG_NOT_INPUT, 0x04, 0);
149         pio->set_context_port_a(not_data3, SIG_NOT_INPUT, 0x08, 0);
150         pio->set_context_port_a(not_data4, SIG_NOT_INPUT, 0x10, 0);
151         pio->set_context_port_a(not_data5, SIG_NOT_INPUT, 0x20, 0);
152         pio->set_context_port_a(not_data6, SIG_NOT_INPUT, 0x40, 0);
153         pio->set_context_port_a(not_data7, SIG_NOT_INPUT, 0x80, 0);
154         pio->set_context_port_b(rtc, SIG_UPD1990A_STB, 0x01, 0);
155         pio->set_context_port_b(rtc, SIG_UPD1990A_C0,  0x02, 0);
156         pio->set_context_port_b(rtc, SIG_UPD1990A_C1,  0x04, 0);
157         pio->set_context_port_b(rtc, SIG_UPD1990A_C2,  0x08, 0);
158         pio->set_context_port_b(rtc, SIG_UPD1990A_DIN, 0x10, 0);
159         pio->set_context_port_b(rtc, SIG_UPD1990A_CLK, 0x20, 0);
160         pio->set_context_port_b(mainbus, SIG_MAIN_SRDY, 0x40, 0);
161 //      pio->set_context_port_b(subbus, SIG_SUB_PIO_PM, 0x80, 0);       // P/M: CG Selection
162         pio->set_context_port_c(kbd, SIG_KEYBOARD_DC, 0x01, 0);
163         pio->set_context_port_c(kbd, SIG_KEYBOARD_STC, 0x02, 0);
164         pio->set_context_port_c(kbd, SIG_KEYBOARD_ACKC, 0x04, 0);
165         // i8255 pc3 <- intr (not use ???)
166         pio->set_context_port_c(pcm, SIG_PCM1BIT_MUTE, 0x10, 0);
167         pio->set_context_port_c(printer, SIG_PRINTER_STROBE, 0x20, 0);
168         // i8255 pc6 <- printer ack
169         pio->set_context_port_c(ls244, SIG_LS244_INPUT, 0x80, -6);
170         
171         // mz3500sm p.78
172         not_data0->set_context_out(printer, SIG_PRINTER_DATA, 0x01);
173         not_data1->set_context_out(printer, SIG_PRINTER_DATA, 0x02);
174         not_data2->set_context_out(printer, SIG_PRINTER_DATA, 0x04);
175         not_data3->set_context_out(printer, SIG_PRINTER_DATA, 0x08);
176         not_data4->set_context_out(printer, SIG_PRINTER_DATA, 0x10);
177         not_data5->set_context_out(printer, SIG_PRINTER_DATA, 0x20);
178         not_data6->set_context_out(printer, SIG_PRINTER_DATA, 0x40);
179         not_data7->set_context_out(printer, SIG_PRINTER_DATA, 0x80);
180         not_busy->set_context_out(ls244, SIG_LS244_INPUT, 0x04);
181         
182         // mz3500sm p.80,81
183         rtc->set_context_dout(ls244, SIG_LS244_INPUT, 0x01);
184         
185         gdc_chr->set_vram_ptr(subbus->get_vram_chr(), 0x2000, 0xfff);
186         subbus->set_sync_ptr_chr(gdc_chr->get_sync());
187         subbus->set_ra_ptr_chr(gdc_chr->get_ra());
188         subbus->set_cs_ptr_chr(gdc_chr->get_cs());
189         subbus->set_ead_ptr_chr(gdc_chr->get_ead());
190         
191         gdc_gfx->set_vram_ptr(subbus->get_vram_gfx(), 0x18000);
192         subbus->set_sync_ptr_gfx(gdc_gfx->get_sync());
193         subbus->set_ra_ptr_gfx(gdc_gfx->get_ra());
194         subbus->set_cs_ptr_gfx(gdc_gfx->get_cs());
195         subbus->set_ead_ptr_gfx(gdc_gfx->get_ead());
196         
197         kbd->set_context_subcpu(subcpu);
198         kbd->set_context_ls244(ls244);
199         
200         // mz3500sm p.23
201         subcpu->set_context_busack(mainbus, SIG_MAIN_SACK, 1);
202         
203         mainbus->set_context_maincpu(maincpu);
204         mainbus->set_context_subcpu(subcpu);
205         mainbus->set_context_fdc(fdc);
206         
207         subbus->set_context_main(mainbus);
208         subbus->set_ipl(mainbus->get_ipl());
209         subbus->set_common(mainbus->get_common());
210         
211         // mz3500sm p.17
212         mainio->set_iomap_range_rw(0xec, 0xef, mainbus);        // reset int0
213         mainio->set_iomap_range_rw(0xf4, 0xf7, fdc);            // fdc: f4h,f6h = status, f5h,f7h = data
214         mainio->set_iomap_range_rw(0xf8, 0xfb, mainbus);        // mfd interface
215         mainio->set_iomap_range_rw(0xfc, 0xff, mainbus);        // memory mpaper
216         
217         // mz3500sm p.18
218         subio->set_iomap_range_w(0x00, 0x0f, subbus);           // int0 to main (set flipflop)
219         subio->set_iomap_range_rw(0x10, 0x1f, sio);
220         subio->set_iomap_range_rw(0x20, 0x2f, pit);
221         subio->set_iomap_range_rw(0x30, 0x3f, pio);
222         subio->set_iomap_range_r(0x40, 0x4f, ls244);            // input port
223         subio->set_iomap_range_rw(0x50, 0x5f, subbus);          // crt control i/o
224         subio->set_iomap_range_rw(0x60, 0x6f, gdc_gfx);
225         subio->set_iomap_range_rw(0x70, 0x7f, gdc_chr);
226 #ifdef _IO_DEBUG_LOG
227         subio->cpu_index = 1;
228 #endif
229         
230         // cpu bus
231         maincpu->set_context_mem(mainbus);
232         maincpu->set_context_io(mainio);
233         maincpu->set_context_intr(mainbus);
234 #ifdef USE_DEBUGGER
235         maincpu->set_context_debugger(new DEBUGGER(this, emu));
236 #endif
237         
238         subcpu->set_context_mem(subbus);
239         subcpu->set_context_io(subio);
240         subcpu->set_context_intr(subbus);
241 #ifdef USE_DEBUGGER
242         subcpu->set_context_debugger(new DEBUGGER(this, emu));
243 #endif
244         
245         // initialize all devices
246 #if defined(__GIT_REPO_VERSION)
247         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
248 #endif
249         for(DEVICE* device = first_device; device; device = device->next_device) {
250                 device->initialize();
251         }
252         decl_state();
253         for(int i = 0; i < 4; i++) {
254                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
255         }
256         // GDC clock (mz3500sm p.33,34)
257         if(config.monitor_type == 0 || config.monitor_type == 1) {
258                 gdc_chr->set_horiz_freq(20920);
259                 gdc_gfx->set_horiz_freq(20920);
260         } else {
261                 gdc_chr->set_horiz_freq(15870);
262                 gdc_gfx->set_horiz_freq(15870);
263         }
264 }
265
266 VM::~VM()
267 {
268         // delete all devices
269         for(DEVICE* device = first_device; device;) {
270                 DEVICE *next_device = device->next_device;
271                 device->release();
272                 delete device;
273                 device = next_device;
274         }
275 }
276
277 DEVICE* VM::get_device(int id)
278 {
279         for(DEVICE* device = first_device; device; device = device->next_device) {
280                 if(device->this_device_id == id) {
281                         return device;
282                 }
283         }
284         return NULL;
285 }
286
287 // ----------------------------------------------------------------------------
288 // drive virtual machine
289 // ----------------------------------------------------------------------------
290
291 void VM::reset()
292 {
293         // reset all devices
294         for(DEVICE* device = first_device; device; device = device->next_device) {
295                 device->reset();
296         }
297         
298         // set busreq of sub cpu
299         subcpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
300         
301         // halt key is not pressed (mz3500sm p.80)
302         halt = 0;
303         ls244->write_signal(SIG_LS244_INPUT, 0x80, 0xff);
304         
305         // set printer signal (mz3500sm p.78)
306         not_busy->write_signal(SIG_NOT_INPUT, 0, 0);            // busy = low
307         ls244->write_signal(SIG_LS244_INPUT, 0x1c, 0x1c);       // busy = ~(low), pe = ~(low), pdtr = high
308         pio->write_signal(SIG_I8255_PORT_C, 0x40, 0x40);        // ack = high
309 }
310
311 void VM::special_reset()
312 {
313         // halt key is pressed (mz3500sm p.80)
314         halt = 8;
315         ls244->write_signal(SIG_LS244_INPUT, 0x00, 0x80);
316 }
317
318 void VM::run()
319 {
320         // halt key is released (mz3500sm p.80)
321         if(halt != 0 && --halt == 0) {
322                 ls244->write_signal(SIG_LS244_INPUT, 0x80, 0x80);
323         }
324         event->drive();
325 }
326
327 double VM::get_frame_rate()
328 {
329         return event->get_frame_rate();
330 }
331
332 // ----------------------------------------------------------------------------
333 // debugger
334 // ----------------------------------------------------------------------------
335
336 #ifdef USE_DEBUGGER
337 DEVICE *VM::get_cpu(int index)
338 {
339         if(index == 0) {
340                 return maincpu;
341         } else if(index == 1) {
342                 return subcpu;
343         }
344         return NULL;
345 }
346 #endif
347
348 // ----------------------------------------------------------------------------
349 // draw screen
350 // ----------------------------------------------------------------------------
351
352 void VM::draw_screen()
353 {
354         subbus->draw_screen();
355 }
356
357 // ----------------------------------------------------------------------------
358 // soud manager
359 // ----------------------------------------------------------------------------
360
361 void VM::initialize_sound(int rate, int samples)
362 {
363         // init sound manager
364         event->initialize_sound(rate, samples);
365         
366         // init sound gen
367         pcm->initialize_sound(rate, 8000);
368 }
369
370 uint16_t* VM::create_sound(int* extra_frames)
371 {
372         return event->create_sound(extra_frames);
373 }
374
375 int VM::get_sound_buffer_ptr()
376 {
377         return event->get_sound_buffer_ptr();
378 }
379
380 #ifdef USE_SOUND_VOLUME
381 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
382 {
383         if(ch == 0) {
384                 pcm->set_volume(0, decibel_l, decibel_r);
385         } else if(ch == 1) {
386                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
387                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
388                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
389         }
390 }
391 #endif
392
393 // ----------------------------------------------------------------------------
394 // notify key
395 // ----------------------------------------------------------------------------
396
397 void VM::key_down(int code, bool repeat)
398 {
399         kbd->key_down(code);
400 }
401
402 void VM::key_up(int code)
403 {
404         kbd->key_up(code);
405 }
406
407 bool VM::get_caps_locked()
408 {
409         return kbd->get_caps_locked();
410 }
411
412 bool VM::get_kana_locked()
413 {
414         return kbd->get_kana_locked();
415 }
416
417 // ----------------------------------------------------------------------------
418 // user interface
419 // ----------------------------------------------------------------------------
420
421 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
422 {
423         fdc->open_disk(drv, file_path, bank);
424 }
425
426 void VM::close_floppy_disk(int drv)
427 {
428         fdc->close_disk(drv);
429 }
430
431 bool VM::is_floppy_disk_inserted(int drv)
432 {
433         return fdc->is_disk_inserted(drv);
434 }
435
436 void VM::is_floppy_disk_protected(int drv, bool value)
437 {
438         fdc->is_disk_protected(drv, value);
439 }
440
441 bool VM::is_floppy_disk_protected(int drv)
442 {
443         return fdc->is_disk_protected(drv);
444 }
445
446 uint32_t VM::is_floppy_disk_accessed()
447 {
448         return fdc->read_signal(0);
449 }
450
451 bool VM::is_frame_skippable()
452 {
453         return event->is_frame_skippable();
454 }
455
456 void VM::update_config()
457 {
458         for(DEVICE* device = first_device; device; device = device->next_device) {
459                 device->update_config();
460         }
461 }
462
463 #define STATE_VERSION   5
464
465 #include "../../statesub.h"
466 #include "../../qt/gui/csp_logger.h"
467 extern CSP_Logger DLL_PREFIX_I *csp_logger;
468
469 void VM::decl_state(void)
470 {
471         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MZ_3500_HEAD")), csp_logger);
472         DECL_STATE_ENTRY_UINT8(halt);
473         
474         for(DEVICE* device = first_device; device; device = device->next_device) {
475                 device->decl_state();
476         }
477 }
478
479 void VM::save_state(FILEIO* state_fio)
480 {
481         //state_fio->FputUint32(STATE_VERSION);
482         
483         if(state_entry != NULL) {
484                 state_entry->save_state(state_fio);
485         }
486         for(DEVICE* device = first_device; device; device = device->next_device) {
487                 device->save_state(state_fio);
488         }
489         //state_fio->FputUint8(halt);
490 }
491
492 bool VM::load_state(FILEIO* state_fio)
493 {
494         //if(state_fio->FgetUint32() != STATE_VERSION) {
495         //      return false;
496         //}
497         bool mb = false;
498         if(state_entry != NULL) {
499                 mb = state_entry->load_state(state_fio);
500         }
501         if(!mb) {
502                 emu->out_debug_log("INFO: HEADER DATA ERROR");
503                 return false;
504         }
505         for(DEVICE* device = first_device; device; device = device->next_device) {
506                 if(!device->load_state(state_fio)) {
507                         return false;
508                 }
509         }
510         //halt = state_fio->FgetUint8();
511         return true;
512 }
513
514 bool VM::process_state(FILEIO* state_fio, bool loading)
515 {
516         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
517                 return false;
518         }
519         for(DEVICE* device = first_device; device; device = device->next_device) {
520                 const char *name = typeid(*device).name() + 6; // skip "class "
521                 int len = strlen(name);
522                 
523                 if(!state_fio->StateCheckInt32(len)) {
524                         return false;
525                 }
526                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
527                         return false;
528                 }
529                 if(!device->process_state(state_fio, loading)) {
530                         return false;
531                 }
532         }
533         state_fio->StateUint8(halt);
534         return true;
535 }