OSDN Git Service

[General][Qt] Merge upstream, datarecorder with sound.
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz700 / mz700.cpp
1 /*
2         SHARP MZ-700 Emulator 'EmuZ-700'
3         SHARP MZ-800 Emulator 'EmuZ-800'
4         SHARP MZ-1500 Emulator 'EmuZ-1500'
5
6         Author : Takeda.Toshiya
7         Date   : 2008.06.05 -
8
9         [ virtual machine ]
10 */
11
12 #include "mz700.h"
13 #include "../../emu.h"
14 #include "../device.h"
15 #include "../event.h"
16
17 #include "../and.h"
18 #include "../datarec.h"
19 #include "../i8253.h"
20 #include "../i8255.h"
21 #include "../io.h"
22 #include "../pcm1bit.h"
23 #include "../z80.h"
24
25 #ifdef USE_DEBUGGER
26 #include "../debugger.h"
27 #endif
28
29 //#include "cmos.h"
30 #include "emm.h"
31 #include "kanji.h"
32 #include "keyboard.h"
33 #include "memory.h"
34 #include "ramfile.h"
35
36 #if defined(_MZ800) || defined(_MZ1500)
37 #include "../disk.h"
38 #include "../mb8877.h"
39 #if defined(_MZ800)
40 #include "../not.h"
41 #endif
42 #include "../sn76489an.h"
43 #include "../z80pio.h"
44 #include "../z80sio.h"
45 #include "floppy.h"
46 #if defined(_MZ1500)
47 #include "psg.h"
48 #endif
49 #include "quickdisk.h"
50 #endif
51
52 #include "../../fileio.h"
53
54 // ----------------------------------------------------------------------------
55 // initialize
56 // ----------------------------------------------------------------------------
57
58 VM::VM(EMU* parent_emu) : emu(parent_emu)
59 {
60 #if defined(_MZ800)
61         boot_mode = config.boot_mode;
62 #endif
63         
64         // create devices
65         first_device = last_device = NULL;
66         dummy = new DEVICE(this, emu);  // must be 1st device
67         event = new EVENT(this, emu);   // must be 2nd device
68         
69         and_int = new AND(this, emu);
70         drec = new DATAREC(this, emu);
71         pit = new I8253(this, emu);
72         pio = new I8255(this, emu);
73         io = new IO(this, emu);
74         pcm = new PCM1BIT(this, emu);
75         cpu = new Z80(this, emu);
76         
77 //      cmos = new CMOS(this, emu);
78         emm = new EMM(this, emu);
79         kanji = new KANJI(this, emu);
80         keyboard = new KEYBOARD(this, emu);
81         memory = new MEMORY(this, emu);
82         ramfile = new RAMFILE(this, emu);
83         
84 #if defined(_MZ800) || defined(_MZ1500)
85         and_snd = new AND(this, emu);
86         fdc = new MB8877(this, emu);    // mb8876
87 #if defined(_MZ800)
88         not_pit = new NOT(this, emu);
89         psg = new SN76489AN(this, emu);
90 #elif defined(_MZ1500)
91         psg_l = new SN76489AN(this, emu);
92         psg_r = new SN76489AN(this, emu);
93 #endif
94         pio_int = new Z80PIO(this, emu);
95         sio_rs = new Z80SIO(this, emu);
96         sio_qd = new Z80SIO(this, emu);
97         
98         floppy = new FLOPPY(this, emu);
99 #if defined(_MZ1500)
100         psg = new PSG(this, emu);
101 #endif
102         qd = new QUICKDISK(this, emu);
103 #endif
104         event->set_context_sound(drec);
105         
106         // set contexts
107         event->set_context_cpu(cpu);
108         event->set_context_sound(pcm);
109 #if defined(_MZ800)
110         event->set_context_sound(psg);
111 #elif defined(_MZ1500)
112         event->set_context_sound(psg_l);
113         event->set_context_sound(psg_r);
114 #endif
115 #if defined(DATAREC_SOUND)
116         event->set_context_sound(drec);
117 #endif
118         // VRAM/PCG wait
119         memory->set_context_cpu(cpu);
120         
121         // memory mapped I/O
122         memory->set_context_pio(pio);
123         memory->set_context_pit(pit);
124         
125 #if defined(_MZ1500)
126         // psg mixer
127         psg->set_context_psg_l(psg_l);
128         psg->set_context_psg_r(psg_r);
129 #endif
130         
131 #if defined(_MZ800)
132         // 8253:CLK#0 <- 1.10MHz
133         pit->set_constant_clock(0, 1100000);
134 #else
135         // 8253:CLK#0 <- 895KHz
136         pit->set_constant_clock(0, CPU_CLOCKS / 4);
137 #endif
138         
139 #if defined(_MZ800) || defined(_MZ1500)
140         // 8253:OUT#0 AND 8255:PC0 -> SPEAKER
141         pit->set_context_ch0(and_snd, SIG_AND_BIT_0, 1);
142         pio->set_context_port_c(and_snd, SIG_AND_BIT_1, 1, 0);
143         and_snd->set_context_out(pcm, SIG_PCM1BIT_SIGNAL, 1);
144         and_snd->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
145 #else
146         // 8253:OUT#0 -> SPEAKER
147         pit->set_context_ch0(pcm, SIG_PCM1BIT_SIGNAL, 1);
148 #endif
149 #if defined(_MZ800)
150         // 8253:OUT#0 -> NOT -> Z80PIO:PA4
151         pit->set_context_ch0(not_pit, SIG_NOT_INPUT, 1);
152         not_pit->set_context_out(pio_int, SIG_Z80PIO_PORT_A, 0x10);
153 #elif defined(_MZ1500)
154         // 8253:OUT#0 -> Z80PIO:PA4
155         pit->set_context_ch0(pio_int, SIG_Z80PIO_PORT_A, 0x10);
156 #endif
157         
158         // 8253:CLK#1 <- 15.7KHz
159         pit->set_constant_clock(1, CPU_CLOCKS / 228);
160         
161         // 8253:OUT#1 -> 8253:CLK#2
162         pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);
163         
164         // 8253:OUT#2 (N)AND 8255:PC2 -> Z80:INT
165         pit->set_context_ch2(and_int, SIG_AND_BIT_0, 1);
166         pio->set_context_port_c(and_int, SIG_AND_BIT_1, 4, 0);
167         and_int->set_context_out(cpu, SIG_CPU_IRQ, 1);
168         and_int->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
169         
170 #if defined(_MZ1500)
171         // 8253:OUT#2 -> Z80PIO:PA5
172         pit->set_context_ch2(pio_int, SIG_Z80PIO_PORT_A, 0x20);
173 #endif
174         
175         // 8255:PA0-3 -> KEYBOARD:STROBE
176         pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);
177 #if defined(_MZ800)
178         // 8255:PA4 -> JOYSTICK #1
179         // 8255:PA5 -> JOYSTICK #2
180 #endif
181         // 8255:PA7 -> 556 RESET
182         
183         // 8255:PB0-7 <- KEYBOARD:DATA
184         keyboard->set_context_pio(pio);
185         
186         // 8255:PC0 -> AND -> SPEAKER
187         // 8255:PC1 -> DATA RECORDER:WRITE DATA
188         pio->set_context_port_c(drec, SIG_DATAREC_OUT, 0x02, 0);
189         // 8255:PC2 -> (N)AND -> Z80:INT
190         // 8255:PC3 -> DATA RECORDER:MOTOR ON/OFF
191         pio->set_context_port_c(drec, SIG_DATAREC_TRIG, 0x08, 0);
192         // 8255:PC4 <- DATA RECORDER:MOTOR REMOTE
193         drec->set_context_remote(pio, SIG_I8255_PORT_C, 0x10);
194         // 8255:PC5 <- DATA RECORDER:READ DATA
195         drec->set_context_out(pio, SIG_I8255_PORT_C, 0x20);
196         // 8255:PC6 <- MEMORY:556 OUT (1.5KHz)
197         // 8255:PC7 <- MEMORY:VBLANK
198         
199 #if defined(_MZ800) || defined(_MZ1500)
200         // Z80PIO:PA0 <- PRINTER:RDA
201         // Z80PIO:PA1 <- PRINTER:STA
202         // Z80PIO:PA2 <- GND
203         // Z80PIO:PA3 <- GND
204 #if defined(_MZ800)
205         // Z80PIO:PA4 <- NOT <- 8253:OUT#0
206         // Z80PIO:PA5 <- HBLANK
207         memory->set_context_pio_int(pio_int);
208 #elif defined(_MZ1500)
209         // Z80PIO:PA4 <- 8253:OUT#0
210         // Z80PIO:PA5 <- 8253:OUT#2
211 #endif
212         // Z80PIO:PA6 -> PRINTER:IRT
213         // Z80PIO:PA7 -> PRINTER:RDP
214 #endif
215         
216 #if defined(_MZ800) || defined(_MZ1500)
217         // Z80SIO:RTSA -> QD:WRGA
218         sio_qd->set_context_rts(0, qd, QUICKDISK_SIO_RTSA, 1);
219         // Z80SIO:DTRB -> QD:MTON
220         sio_qd->set_context_dtr(1, qd, QUICKDISK_SIO_DTRB, 1);
221         // Z80SIO:SENDA -> QD:RECV
222         sio_qd->set_context_sync(0, qd, QUICKDISK_SIO_SYNC, 1);
223         sio_qd->set_context_rxdone(0, qd, QUICKDISK_SIO_RXDONE, 1);
224         sio_qd->set_context_send(0, qd, QUICKDISK_SIO_DATA);
225         sio_qd->set_context_break(0, qd, QUICKDISK_SIO_BREAK, 1);
226         // Z80SIO:CTSA <- QD:PROTECT
227         // Z80SIO:DCDA <- QD:INSERT
228         // Z80SIO:DCDB <- QD:HOE
229         qd->set_context_sio(sio_qd);
230         
231         sio_rs->set_tx_clock(0, 1200 * 16);     // 1200 baud
232         sio_rs->set_rx_clock(0, 1200 * 16);     // baud-rate can be changed by jumper pin
233         sio_rs->set_tx_clock(1, 1200 * 16);
234         sio_rs->set_rx_clock(1, 1200 * 16);
235         
236         sio_qd->set_tx_clock(0, 101562.5);
237         sio_qd->set_rx_clock(0, 101562.5);
238         sio_qd->set_tx_clock(1, 101562.5);
239         sio_qd->set_rx_clock(1, 101562.5);
240         
241         // floppy drives
242         floppy->set_context_cpu(cpu);
243         floppy->set_context_fdc(fdc);
244         fdc->set_context_drq(floppy, SIG_FLOPPY_DRQ, 1);
245 #endif
246         
247         // cpu bus
248         cpu->set_context_mem(memory);
249         cpu->set_context_io(io);
250 #if defined(_MZ800) || defined(_MZ1500)
251         cpu->set_context_intr(pio_int);
252         // z80 family daisy chain
253         // 0=8253:OUT2
254         pio_int->set_context_intr(cpu, 1);
255         pio_int->set_context_child(sio_rs);
256         sio_rs->set_context_intr(cpu, 2);
257         sio_rs->set_context_child(sio_qd);
258         sio_qd->set_context_intr(cpu, 3);
259 #else
260         cpu->set_context_intr(dummy);
261 #endif
262 #ifdef USE_DEBUGGER
263         cpu->set_context_debugger(new DEBUGGER(this, emu));
264 #endif
265         
266         // emm
267         io->set_iomap_range_rw(0x00, 0x03, emm);
268         // kanji
269         io->set_iomap_range_rw(0xb8, 0xb9, kanji);
270         // ramfile
271         io->set_iomap_range_rw(0xea, 0xeb, ramfile);
272         // cmos
273 //      io->set_iomap_range_rw(0xf8, 0xfa, cmos);
274         
275 #if defined(_MZ800)
276         // 8255/8253
277         io->set_iomap_range_rw(0xd0, 0xd3, pio);
278         io->set_iomap_range_rw(0xd4, 0xd7, pit);
279 #endif
280         
281 #if defined(_MZ800) || defined(_MZ1500)
282         // floppy drives
283         io->set_iomap_range_rw(0xd8, 0xdb, fdc);
284         io->set_iomap_range_w(0xdc, 0xdf, floppy);
285 #endif
286         
287         // memory mapper
288 #if defined(_MZ800)
289         io->set_iomap_range_r(0xe0, 0xe1, memory);
290         io->set_iomap_range_w(0xe0, 0xe6, memory);
291 #elif defined(_MZ1500)
292         io->set_iomap_range_w(0xe0, 0xe6, memory);
293 #else
294         io->set_iomap_range_w(0xe0, 0xe4, memory);
295 #endif
296         
297 #if defined(_MZ800)
298         // crtc
299         io->set_iomap_range_w(0xcc, 0xcf, memory);
300         io->set_iomap_single_r(0xce, memory);
301         // palette
302         io->set_iomap_single_w(0xf0, memory);
303 #elif defined(_MZ1500)
304         // palette
305         io->set_iomap_range_w(0xf0, 0xf1, memory);
306 #endif
307         
308 #if defined(_MZ800)
309         // joystick
310 //      io->set_iomap_range_r(0xf0, 0xf1, joystick);
311 #endif
312         
313         // psg
314 #if defined(_MZ800)
315         io->set_iomap_single_w(0xf2, psg);
316 #elif defined(_MZ1500)
317         io->set_iomap_single_w(0xe9, psg);
318         io->set_iomap_single_w(0xf2, psg_l);
319         io->set_iomap_single_w(0xf3, psg_r);
320 #endif
321         
322 #if defined(_MZ800) || defined(_MZ1500)
323         // z80pio/sio
324         // z80pio and z80sio*2
325         static const int z80_sio_addr[4] = {0, 2, 1, 3};
326         static const int z80_pio_addr[4] = {1, 3, 0, 2};
327         for(int i = 0; i < 4; i++) {
328                 io->set_iomap_alias_rw(0xb0 + i, sio_rs, z80_sio_addr[i]);
329                 io->set_iomap_alias_rw(0xf4 + i, sio_qd, z80_sio_addr[i]);
330                 io->set_iomap_alias_rw(0xfc + i, pio_int, z80_pio_addr[i]);
331         }
332 #else
333         // printer
334         io->set_iovalue_single_r(0xfe, 0xc0);
335 #endif
336
337         // initialize all devices
338         for(DEVICE* device = first_device; device; device = device->next_device) {
339                 device->initialize();
340         }
341 #if defined(_MZ800) || defined(_MZ1500)
342         for(int i = 0; i < MAX_DRIVE; i++) {
343                 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
344         }
345 #endif
346 }
347
348 VM::~VM()
349 {
350         // delete all devices
351         for(DEVICE* device = first_device; device;) {
352                 DEVICE *next_device = device->next_device;
353                 device->release();
354                 delete device;
355                 device = next_device;
356         }
357 }
358
359 DEVICE* VM::get_device(int id)
360 {
361         for(DEVICE* device = first_device; device; device = device->next_device) {
362                 if(device->this_device_id == id) {
363                         return device;
364                 }
365         }
366         return NULL;
367 }
368
369 // ----------------------------------------------------------------------------
370 // drive virtual machine
371 // ----------------------------------------------------------------------------
372
373 void VM::reset()
374 {
375         // reset all devices
376         for(DEVICE* device = first_device; device; device = device->next_device) {
377                 device->reset();
378         }
379         and_int->write_signal(SIG_AND_BIT_0, 0, 1);     // CLOCK = L
380         and_int->write_signal(SIG_AND_BIT_1, 1, 1);     // INTMASK = H
381 #if defined(_MZ800) || defined(_MZ1500)
382         and_snd->write_signal(SIG_AND_BIT_1, 1, 1);     // SNDMASK = H
383 #endif
384 }
385
386 void VM::run()
387 {
388         event->drive();
389 }
390
391 // ----------------------------------------------------------------------------
392 // debugger
393 // ----------------------------------------------------------------------------
394
395 #ifdef USE_DEBUGGER
396 DEVICE *VM::get_cpu(int index)
397 {
398         if(index == 0) {
399                 return cpu;
400         }
401         return NULL;
402 }
403 #endif
404
405 // ----------------------------------------------------------------------------
406 // draw screen
407 // ----------------------------------------------------------------------------
408
409 void VM::draw_screen()
410 {
411         memory->draw_screen();
412 }
413
414 #if defined(_MZ800) || defined(_MZ1500)
415 int VM::access_lamp()
416 {
417   uint32 status = fdc->read_signal(0) | qd->read_signal(0); // 4 + 1: Nagative
418   return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
419 }
420 #endif
421
422 // ----------------------------------------------------------------------------
423 // soud manager
424 // ----------------------------------------------------------------------------
425
426 void VM::initialize_sound(int rate, int samples)
427 {
428         // init sound manager
429         event->initialize_sound(rate, samples);
430         
431         // init sound gen
432         pcm->init(rate, 8000);
433 #if defined(_MZ800)
434         psg->init(rate, 3579545, 8000);
435 #elif defined(_MZ1500)
436         psg_l->init(rate, 3579545, 8000);
437         psg_r->init(rate, 3579545, 8000);
438 #endif
439 }
440
441 uint16* VM::create_sound(int* extra_frames)
442 {
443         return event->create_sound(extra_frames);
444 }
445
446 int VM::sound_buffer_ptr()
447 {
448         return event->sound_buffer_ptr();
449 }
450
451 // ----------------------------------------------------------------------------
452 // user interface
453 // ----------------------------------------------------------------------------
454
455 void VM::play_tape(_TCHAR* file_path)
456 {
457         drec->play_tape(file_path);
458         drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
459 }
460
461 void VM::rec_tape(_TCHAR* file_path)
462 {
463         drec->rec_tape(file_path);
464         drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
465 }
466
467 void VM::close_tape()
468 {
469         drec->close_tape();
470         drec->write_signal(SIG_DATAREC_REMOTE, 0, 0);
471 }
472
473 bool VM::tape_inserted()
474 {
475         return drec->tape_inserted();
476 }
477
478 int VM::get_tape_ptr()
479 {
480         return drec->get_tape_ptr();
481 }
482
483 void VM::push_play()
484 {
485         drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
486 }
487
488 void VM::push_stop()
489 {
490         drec->write_signal(SIG_DATAREC_REMOTE, 0, 0);
491 }
492
493 bool VM::get_tape_play(void)
494 {
495         return drec->get_tape_play();
496 }
497
498 #if defined(_MZ800) || defined(_MZ1500)
499 void VM::open_quickdisk(int drv, _TCHAR* file_path)
500 {
501         if(drv == 0) {
502                 qd->open_disk(file_path);
503         }
504 }
505
506 void VM::close_quickdisk(int drv)
507 {
508         if(drv == 0) {
509                 qd->close_disk();
510         }
511 }
512
513 bool VM::quickdisk_inserted(int drv)
514 {
515         if(drv == 0) {
516                 return qd->disk_inserted();
517         } else {
518                 return false;
519         }
520 }
521
522 void VM::write_protect_fd(int drv, bool flag)
523 {
524         fdc->write_protect_fd(drv, flag);
525 }
526
527 bool VM::is_write_protect_fd(int drv)
528 {
529         return fdc->is_write_protect_fd(drv);
530 }
531
532 void VM::open_disk(int drv, _TCHAR* file_path, int bank)
533 {
534         fdc->open_disk(drv, file_path, bank);
535 }
536
537 void VM::close_disk(int drv)
538 {
539         fdc->close_disk(drv);
540 }
541
542 bool VM::disk_inserted(int drv)
543 {
544         return fdc->disk_inserted(drv);
545 }
546
547 #endif
548
549 bool VM::now_skip()
550 {
551         return event->now_skip();
552 }
553
554 void VM::update_config()
555 {
556 #if defined(_MZ800)
557         if(boot_mode != config.boot_mode) {
558                 // boot mode is changed !!!
559                 boot_mode = config.boot_mode;
560                 reset();
561         } else {
562 #endif
563                 for(DEVICE* device = first_device; device; device = device->next_device) {
564                         device->update_config();
565                 }
566 #if defined(_MZ800)
567         }
568 #endif
569 }
570
571 #define STATE_VERSION   1
572
573 void VM::save_state(FILEIO* state_fio)
574 {
575         state_fio->FputUint32(STATE_VERSION);
576         
577         for(DEVICE* device = first_device; device; device = device->next_device) {
578                 device->save_state(state_fio);
579         }
580 #if defined(_MZ800)
581         state_fio->FputInt32(boot_mode);
582 #endif
583 }
584
585 bool VM::load_state(FILEIO* state_fio)
586 {
587         if(state_fio->FgetUint32() != STATE_VERSION) {
588                 return false;
589         }
590         for(DEVICE* device = first_device; device; device = device->next_device) {
591                 if(!device->load_state(state_fio)) {
592                         return false;
593                 }
594         }
595 #if defined(_MZ800)
596         boot_mode = state_fio->FgetInt32();
597 #endif
598         return true;
599 }
600