OSDN Git Service

5a95c4e91f43b3c869238c241230e51d71e9b425
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz80k / mz80k.cpp
1 /*
2         SHARP MZ-80K/C Emulator 'EmuZ-80K'
3         SHARP MZ-1200 Emulator 'EmuZ-1200'
4
5         Author : Takeda.Toshiya
6         Date   : 2010.08.18-
7
8         SHARP MZ-80A Emulator 'EmuZ-80A'
9         Modify : Hideki Suga
10         Date   : 2014.12.10 -
11
12         [ virtual machine ]
13 */
14
15 #include "mz80k.h"
16 #include "../../emu.h"
17 #include "../device.h"
18 #include "../event.h"
19
20 #if defined(_MZ1200) || defined(_MZ80A)
21 #include "../and.h"
22 #endif
23 #include "../datarec.h"
24 #include "../i8253.h"
25 #include "../i8255.h"
26 #include "../ls393.h"
27 #include "../mz1p17.h"
28 #include "../noise.h"
29 #include "../pcm1bit.h"
30 #include "../prnfile.h"
31 #include "../z80.h"
32
33 #ifdef USE_DEBUGGER
34 #include "../debugger.h"
35 #endif
36
37 #include "keyboard.h"
38 #include "memory.h"
39 #include "printer.h"
40
41 #if defined(SUPPORT_MZ80AIF)
42 #include "../io.h"
43 #include "../disk.h"
44 #include "../mb8877.h"
45 #include "mz80aif.h"
46 #elif defined(SUPPORT_MZ80FIO)
47 #include "../io.h"
48 #include "../disk.h"
49 #include "../t3444a.h"
50 #include "mz80fio.h"
51 #endif
52
53 // ----------------------------------------------------------------------------
54 // initialize
55 // ----------------------------------------------------------------------------
56
57 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
58 {
59         // create devices
60         first_device = last_device = NULL;
61         dummy = new DEVICE(this, emu);  // must be 1st device
62         event = new EVENT(this, emu);   // must be 2nd device
63         dummy->set_device_name(_T("1st Dummy"));
64         
65 #if defined(_MZ1200) || defined(_MZ80A)
66         and_int = new AND(this, emu);
67         and_int->set_device_name(_T("AND Gate (Interrupts)"));
68 #endif
69         drec = new DATAREC(this, emu);
70         drec->set_context_noise_play(new NOISE(this, emu));
71         drec->set_context_noise_stop(new NOISE(this, emu));
72         drec->set_context_noise_fast(new NOISE(this, emu));
73         ctc = new I8253(this, emu);
74         pio = new I8255(this, emu);
75         counter = new LS393(this, emu);
76         pcm = new PCM1BIT(this, emu);
77         cpu = new Z80(this, emu);
78         counter->set_device_name(_T("74LS393 Counter (CTC/Sound)"));
79         
80         keyboard = new KEYBOARD(this, emu);
81         memory = new MEMORY(this, emu);
82         printer = new PRINTER(this, emu);
83 #if defined(SUPPORT_MZ80AIF)
84         io = new IO(this, emu);
85         fdc = new MB8877(this, emu);    // mb8866
86         fdc->set_context_noise_seek(new NOISE(this, emu));
87         fdc->set_context_noise_head_down(new NOISE(this, emu));
88         fdc->set_context_noise_head_up(new NOISE(this, emu));
89         mz80aif = new MZ80AIF(this, emu);
90 #elif defined(SUPPORT_MZ80FIO)
91         io = new IO(this, emu);
92         fdc = new T3444A(this, emu);    // t3444m
93         fdc->set_context_noise_seek(new NOISE(this, emu));
94         fdc->set_context_noise_head_down(new NOISE(this, emu));
95         fdc->set_context_noise_head_up(new NOISE(this, emu));
96         mz80fio = new MZ80FIO(this, emu);
97 #endif
98         
99         // set contexts
100         event->set_context_cpu(cpu);
101         event->set_context_sound(pcm);
102         event->set_context_sound(drec);
103 #if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
104         event->set_context_sound(fdc->get_context_noise_seek());
105         event->set_context_sound(fdc->get_context_noise_head_down());
106         event->set_context_sound(fdc->get_context_noise_head_up());
107 #endif
108         event->set_context_sound(drec->get_context_noise_play());
109         event->set_context_sound(drec->get_context_noise_stop());
110         event->set_context_sound(drec->get_context_noise_fast());
111         
112 #if defined(_MZ1200) || defined(_MZ80A)
113         and_int->set_context_out(cpu, SIG_CPU_IRQ, 1);
114         and_int->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
115 #endif
116         drec->set_context_ear(pio, SIG_I8255_PORT_C, 0x20);
117         drec->set_context_remote(pio, SIG_I8255_PORT_C, 0x10);
118         ctc->set_context_ch0(counter, SIG_LS393_CLK, 1);
119         ctc->set_context_ch1(ctc, SIG_I8253_CLOCK_2, 1);
120 #if defined(_MZ1200) || defined(_MZ80A)
121         ctc->set_context_ch2(and_int, SIG_AND_BIT_0, 1);
122 #else
123         ctc->set_context_ch2(cpu, SIG_CPU_IRQ, 1);
124 #endif
125         ctc->set_constant_clock(0, 2000000);
126         ctc->set_constant_clock(1, 31250);
127         pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);
128         pio->set_context_port_c(memory, SIG_MEMORY_VGATE, 1, 0);
129         pio->set_context_port_c(drec, SIG_DATAREC_MIC, 2, 0);
130 #if defined(_MZ1200) || defined(_MZ80A)
131         pio->set_context_port_c(and_int, SIG_AND_BIT_1, 4, 0);
132 #endif
133         pio->set_context_port_c(drec, SIG_DATAREC_TRIG, 8, 0);
134         counter->set_context_1qa(pcm, SIG_PCM1BIT_SIGNAL, 1);
135         // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
136         //pcm->set_realtime_render(true);
137         
138         keyboard->set_context_pio(pio);
139         memory->set_context_ctc(ctc);
140         memory->set_context_pio(pio);
141         
142 #if defined(SUPPORT_MZ80AIF)
143         fdc->set_context_irq(memory, SIG_MEMORY_FDC_IRQ, 1);
144         fdc->set_context_drq(memory, SIG_MEMORY_FDC_DRQ, 1);
145         mz80aif->set_context_fdc(fdc);
146 #elif defined(SUPPORT_MZ80FIO)
147         mz80fio->set_context_fdc(fdc);
148 #endif
149         
150         if(config.printer_type == 0) {  
151                 printer->set_context_prn(new PRNFILE(this, emu));
152         } else if(config.printer_type == 1) {
153                 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
154                 mz1p17->mode = MZ1P17_MODE_MZ80P4;
155                 printer->set_context_prn(mz1p17);
156         } else {
157                 printer->set_context_prn(dummy);
158         }
159         
160         // cpu bus
161         cpu->set_context_mem(memory);
162 #if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
163         cpu->set_context_io(io);
164 #else
165         cpu->set_context_io(dummy);
166 #endif
167         cpu->set_context_intr(dummy);
168 #ifdef USE_DEBUGGER
169         cpu->set_context_debugger(new DEBUGGER(this, emu));
170 #endif
171         
172         // i/o bus
173 #if defined(SUPPORT_MZ80AIF)
174         io->set_iomap_range_rw(0xd8, 0xdb, fdc);
175         io->set_iomap_range_w(0xdc, 0xdd, mz80aif);
176 #elif defined(SUPPORT_MZ80FIO)
177         io->set_iomap_range_rw(0xf8, 0xfb, mz80fio);
178 #endif
179         io->set_iomap_range_rw(0xfe, 0xff, printer);
180         
181         // initialize all devices
182 #if defined(__GIT_REPO_VERSION)
183         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
184 #endif
185         for(DEVICE* device = first_device; device; device = device->next_device) {
186                 device->initialize();
187         }
188         decl_state();
189 }
190
191 VM::~VM()
192 {
193         // delete all devices
194         for(DEVICE* device = first_device; device;) {
195                 DEVICE *next_device = device->next_device;
196                 device->release();
197                 delete device;
198                 device = next_device;
199         }
200 #if defined(SUPPORT_MZ80AIF)
201         for(int drv = 0; drv < MAX_DRIVE; drv++) {
202 //              if(config.drive_type) {
203                         fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
204 //              } else {
205 //                      fdc->set_drive_type(drv, DRIVE_TYPE_2D);
206 //              }
207         }
208 #elif defined(SUPPORT_MZ80FIO)
209         for(int drv = 0; drv < MAX_DRIVE; drv++) {
210                 fdc->set_drive_type(drv, DRIVE_TYPE_2D);
211 //              fdc->set_drive_mfm(drv, false);
212         }
213 #endif
214 }
215
216 DEVICE* VM::get_device(int id)
217 {
218         for(DEVICE* device = first_device; device; device = device->next_device) {
219                 if(device->this_device_id == id) {
220                         return device;
221                 }
222         }
223         return NULL;
224 }
225
226 // ----------------------------------------------------------------------------
227 // drive virtual machine
228 // ----------------------------------------------------------------------------
229
230 void VM::reset()
231 {
232         // reset all devices
233         for(DEVICE* device = first_device; device; device = device->next_device) {
234                 device->reset();
235         }
236 #if defined(_MZ1200) || defined(_MZ80A)
237         and_int->write_signal(SIG_AND_BIT_0, 0, 1);     // CLOCK = L
238         and_int->write_signal(SIG_AND_BIT_1, 1, 1);     // INTMASK = H
239 #endif
240 }
241
242 void VM::run()
243 {
244         event->drive();
245 }
246
247 // ----------------------------------------------------------------------------
248 // debugger
249 // ----------------------------------------------------------------------------
250
251 #ifdef USE_DEBUGGER
252 DEVICE *VM::get_cpu(int index)
253 {
254         if(index == 0) {
255                 return cpu;
256         }
257         return NULL;
258 }
259 #endif
260
261 // ----------------------------------------------------------------------------
262 // draw screen
263 // ----------------------------------------------------------------------------
264
265 void VM::draw_screen()
266 {
267         memory->draw_screen();
268 }
269
270 // ----------------------------------------------------------------------------
271 // soud manager
272 // ----------------------------------------------------------------------------
273
274 void VM::initialize_sound(int rate, int samples)
275 {
276         // init sound manager
277         event->initialize_sound(rate, samples);
278         
279         // init sound gen
280         pcm->initialize_sound(rate, 8000);
281 }
282
283 uint16_t* VM::create_sound(int* extra_frames)
284 {
285         return event->create_sound(extra_frames);
286 }
287
288 int VM::get_sound_buffer_ptr()
289 {
290         return event->get_sound_buffer_ptr();
291 }
292
293 #ifdef USE_SOUND_VOLUME
294 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
295 {
296         if(ch-- == 0) {
297                 pcm->set_volume(0, decibel_l, decibel_r);
298         } else if(ch-- == 0) {
299                 drec->set_volume(0, decibel_l, decibel_r);
300 #if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
301         } else if(ch-- == 0) {
302                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
303                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
304                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
305 #endif
306         } else if(ch-- == 0) {
307                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
308                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
309                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
310         }
311 }
312 #endif
313
314 // ----------------------------------------------------------------------------
315 // notify key
316 // ----------------------------------------------------------------------------
317
318 void VM::key_down(int code, bool repeat)
319 {
320         if(!repeat) {
321                 keyboard->key_down(code);
322         }
323 }
324
325 void VM::key_up(int code)
326 {
327 //      keyboard->key_up(code);
328 }
329
330 bool VM::get_caps_locked()
331 {
332         return keyboard->get_caps_locked();
333 }
334
335 bool VM::get_kana_locked()
336 {
337         return keyboard->get_kana_locked();
338 }
339
340 // ----------------------------------------------------------------------------
341 // user interface
342 // ----------------------------------------------------------------------------
343
344 #if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
345 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
346 {
347         fdc->open_disk(drv, file_path, bank);
348         
349 #if defined(SUPPORT_MZ80AIF)
350         if(fdc->get_media_type(drv) == MEDIA_TYPE_2DD) {
351                 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2D) {
352                         fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
353                 }
354         } else if(fdc->get_media_type(drv) == MEDIA_TYPE_2D) {
355                 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2DD) {
356                         fdc->set_drive_type(drv, DRIVE_TYPE_2D);
357                 }
358         }
359 #endif
360 }
361
362 void VM::close_floppy_disk(int drv)
363 {
364         fdc->close_disk(drv);
365 }
366
367 bool VM::is_floppy_disk_inserted(int drv)
368 {
369         return fdc->is_disk_inserted(drv);
370 }
371
372 void VM::is_floppy_disk_protected(int drv, bool value)
373 {
374         fdc->is_disk_protected(drv, value);
375 }
376
377 bool VM::is_floppy_disk_protected(int drv)
378 {
379         return fdc->is_disk_protected(drv);
380 }
381
382 uint32_t VM::is_floppy_disk_accessed()
383 {
384         return fdc->read_signal(0);
385 }
386 #endif
387
388
389 void VM::play_tape(int drv, const _TCHAR* file_path)
390 {
391         drec->play_tape(file_path);
392 //      drec->set_remote(true);
393 }
394
395 void VM::rec_tape(int drv, const _TCHAR* file_path)
396 {
397         drec->rec_tape(file_path);
398 //      drec->set_remote(true);
399 }
400
401 void VM::close_tape(int drv)
402 {
403         emu->lock_vm();
404         drec->close_tape();
405         emu->unlock_vm();
406 //      drec->set_remote(false);
407 }
408
409 bool VM::is_tape_inserted(int drv)
410 {
411         return drec->is_tape_inserted();
412 }
413
414 bool VM::is_tape_playing(int drv)
415 {
416         return drec->is_tape_playing();
417 }
418
419 bool VM::is_tape_recording(int drv)
420 {
421         return drec->is_tape_recording();
422 }
423
424 int VM::get_tape_position(int drv)
425 {
426         return drec->get_tape_position();
427 }
428
429 const _TCHAR* VM::get_tape_message(int drv)
430 {
431         return drec->get_message();
432 }
433
434 void VM::push_play(int drv)
435 {
436         drec->set_ff_rew(0);
437         drec->set_remote(true);
438 }
439
440 void VM::push_stop(int drv)
441 {
442         drec->set_remote(false);
443 }
444
445 void VM::push_fast_forward(int drv)
446 {
447         drec->set_ff_rew(1);
448         drec->set_remote(true);
449 }
450
451 void VM::push_fast_rewind(int drv)
452 {
453         drec->set_ff_rew(-1);
454         drec->set_remote(true);
455 }
456
457 bool VM::is_frame_skippable()
458 {
459         return event->is_frame_skippable();
460 }
461
462 void VM::update_config()
463 {
464         for(DEVICE* device = first_device; device; device = device->next_device) {
465                 device->update_config();
466         }
467 }
468
469 #define STATE_VERSION   7
470
471 #include "../../statesub.h"
472 #include "../../qt/gui/csp_logger.h"
473 extern CSP_Logger DLL_PREFIX_I *csp_logger;
474
475 void VM::decl_state(void)
476 {
477         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MZ80_SERIES_HEAD")), csp_logger);
478
479         for(DEVICE* device = first_device; device; device = device->next_device) {
480                 device->decl_state();
481         }
482 }
483
484 void VM::save_state(FILEIO* state_fio)
485 {
486         //state_fio->FputUint32(STATE_VERSION);
487         if(state_entry != NULL) {
488                 state_entry->save_state(state_fio);
489         }
490         for(DEVICE* device = first_device; device; device = device->next_device) {
491                 device->save_state(state_fio);
492         }
493 }
494
495 bool VM::load_state(FILEIO* state_fio)
496 {
497         bool mb = false;
498         if(state_entry != NULL) {
499                 mb = state_entry->load_state(state_fio);
500         }
501         if(!mb) return false;
502         //if(state_fio->FgetUint32() != STATE_VERSION) {
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         return true;
511 }
512
513 bool VM::process_state(FILEIO* state_fio, bool loading)
514 {
515         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
516                 return false;
517         }
518         for(DEVICE* device = first_device; device; device = device->next_device) {
519                 const char *name = typeid(*device).name() + 6; // skip "class "
520                 int len = strlen(name);
521                 
522                 if(!state_fio->StateCheckInt32(len)) {
523                         return false;
524                 }
525                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
526                         return false;
527                 }
528                 if(!device->process_state(state_fio, loading)) {
529                         return false;
530                 }
531         }
532         return true;
533 }