2 TOSHIBA PASOPIA 7 Emulator 'EmuPIA7'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
15 #include "../datarec.h"
17 #include "../hd46505.h"
23 #include "../pcm1bit.h"
24 #include "../sn76489an.h"
25 #include "../upd765a.h"
27 #include "../z80ctc.h"
28 #include "../z80pio.h"
31 #include "../debugger.h"
42 using PASOPIA7::DISPLAY;
43 using PASOPIA7::FLOPPY;
44 using PASOPIA7::IOBUS;
45 using PASOPIA7::IOTRAP;
46 using PASOPIA7::KEYBOARD;
47 using PASOPIA7::MEMORY;
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
57 first_device = last_device = NULL;
58 dummy = new DEVICE(this, emu); // must be 1st device
59 event = new EVENT(this, emu); // must be 2nd device
61 drec = new DATAREC(this, emu);
62 drec->set_context_noise_play(new NOISE(this, emu));
63 drec->set_context_noise_stop(new NOISE(this, emu));
64 drec->set_context_noise_fast(new NOISE(this, emu));
65 crtc = new HD46505(this, emu);
66 pio0 = new I8255(this, emu);
67 pio0->set_device_name(_T("8255 PIO (Display)"));
68 pio1 = new I8255(this, emu);
69 pio1->set_device_name(_T("8255 PIO (Display/Memory)"));
70 pio2 = new I8255(this, emu);
71 pio2->set_device_name(_T("8255 PIO (Sound/Data Recorder)"));
72 io = new IO(this, emu);
73 flipflop = new LS393(this, emu); // LS74
74 not_remote = new NOT(this, emu);
75 pcm = new PCM1BIT(this, emu);
76 psg0 = new SN76489AN(this, emu);
77 psg0->set_device_name(_T("SN76489AN PSG #1"));
78 psg1 = new SN76489AN(this, emu);
79 psg1->set_device_name(_T("SN76489AN PSG #2"));
80 fdc = new UPD765A(this, emu);
81 fdc->set_context_noise_seek(new NOISE(this, emu));
82 fdc->set_context_noise_head_down(new NOISE(this, emu));
83 fdc->set_context_noise_head_up(new NOISE(this, emu));
84 cpu = new Z80(this, emu);
85 ctc = new Z80CTC(this, emu);
86 pio = new Z80PIO(this, emu);
88 floppy = new FLOPPY(this, emu);
89 display = new DISPLAY(this, emu);
90 iobus = new IOBUS(this, emu);
91 iotrap = new IOTRAP(this, emu);
92 key = new KEYBOARD(this, emu);
93 memory = new MEMORY(this, emu);
94 pac2 = new PAC2(this, emu);
97 event->set_context_cpu(cpu);
98 event->set_context_sound(pcm);
99 event->set_context_sound(psg0);
100 event->set_context_sound(psg1);
101 event->set_context_sound(drec);
102 event->set_context_sound(fdc->get_context_noise_seek());
103 event->set_context_sound(fdc->get_context_noise_head_down());
104 event->set_context_sound(fdc->get_context_noise_head_up());
105 event->set_context_sound(drec->get_context_noise_play());
106 event->set_context_sound(drec->get_context_noise_stop());
107 event->set_context_sound(drec->get_context_noise_fast());
109 drec->set_context_ear(pio2, SIG_I8255_PORT_B, 0x20);
110 crtc->set_context_disp(pio0, SIG_I8255_PORT_B, 8);
111 crtc->set_context_vsync(pio0, SIG_I8255_PORT_B, 0x20);
112 pio0->set_context_port_a(display, SIG_DISPLAY_I8255_0_A, 0xff, 0);
113 pio1->set_context_port_a(memory, SIG_MEMORY_I8255_1_A, 0xff, 0);
114 pio1->set_context_port_b(display, SIG_DISPLAY_I8255_1_B, 0xff, 0);
115 pio1->set_context_port_b(memory, SIG_MEMORY_I8255_1_B, 0xff, 0);
116 pio1->set_context_port_c(display, SIG_DISPLAY_I8255_1_C, 0xff, 0);
117 pio1->set_context_port_c(memory, SIG_MEMORY_I8255_1_C, 0xff, 0);
118 pio2->set_context_port_a(pcm, SIG_PCM1BIT_MUTE, 0x02, 0);
119 pio2->set_context_port_a(psg0, SIG_SN76489AN_MUTE, 0x02, 0);
120 pio2->set_context_port_a(psg1, SIG_SN76489AN_MUTE, 0x02, 0);
121 pio2->set_context_port_a(drec, SIG_DATAREC_MIC, 0x10, 0);
122 pio2->set_context_port_a(not_remote, SIG_NOT_INPUT, 0x20, 0);
123 pio2->set_context_port_a(iotrap, SIG_IOTRAP_I8255_2_A, 0xff, 0);
124 pio2->set_context_port_c(iotrap, SIG_IOTRAP_I8255_2_C, 0xff, 0);
125 flipflop->set_context_1qa(pcm, SIG_PCM1BIT_SIGNAL, 1);
126 not_remote->set_context_out(drec, SIG_DATAREC_REMOTE, 1);
127 fdc->set_context_irq(floppy, SIG_FLOPPY_INTR, 1);
128 ctc->set_context_zc0(ctc, SIG_Z80CTC_TRIG_1, 1);
129 ctc->set_context_zc1(flipflop, SIG_LS393_CLK, 1);
130 ctc->set_context_zc2(ctc, SIG_Z80CTC_TRIG_3, 1);
131 ctc->set_constant_clock(0, CPU_CLOCKS);
132 ctc->set_constant_clock(2, CPU_CLOCKS);
133 pio->set_context_port_a(pcm, SIG_PCM1BIT_ON, 0x80, 0);
134 pio->set_context_port_a(key, SIG_KEYBOARD_Z80PIO_A, 0xff, 0);
136 display->set_vram_ptr(memory->get_vram());
137 display->set_pal_ptr(memory->get_pal());
138 display->set_regs_ptr(crtc->get_regs());
139 floppy->set_context_fdc(fdc);
140 iobus->set_context_io(io);
141 iobus->set_ram_ptr(memory->get_ram());
142 iotrap->set_context_cpu(cpu);
143 iotrap->set_context_pio2(pio2);
144 key->set_context_pio(pio);
145 memory->set_context_iobus(iobus);
146 memory->set_context_pio0(pio0);
147 memory->set_context_pio2(pio2);
150 cpu->set_context_mem(memory);
151 cpu->set_context_io(iobus);
152 cpu->set_context_intr(ctc);
154 cpu->set_context_debugger(new DEBUGGER(this, emu));
157 // z80 family daisy chain
158 ctc->set_context_intr(cpu, 0);
159 ctc->set_context_child(pio);
160 pio->set_context_intr(cpu, 1);
163 io->set_iomap_range_rw(0x08, 0x0b, pio0);
164 io->set_iomap_range_rw(0x0c, 0x0f, pio1);
165 io->set_iomap_range_rw(0x10, 0x11, crtc);
166 io->set_iomap_range_rw(0x18, 0x1b, pac2);
167 io->set_iomap_range_rw(0x20, 0x23, pio2);
168 io->set_iomap_range_rw(0x28, 0x2b, ctc);
169 io->set_iomap_alias_rw(0x30, pio, 0);
170 io->set_iomap_alias_rw(0x31, pio, 2);
171 io->set_iomap_alias_w(0x32, pio, 1);
172 io->set_iomap_alias_w(0x33, pio, 3);
173 io->set_iomap_single_w(0x3a, psg0);
174 io->set_iomap_single_w(0x3b, psg1);
175 io->set_iomap_single_w(0x3c, memory);
176 io->set_iomap_single_w(0xe0, floppy);
177 io->set_iomap_single_w(0xe2, floppy);
178 io->set_iomap_range_rw(0xe4, 0xe5, fdc);
179 io->set_iomap_single_rw(0xe6, floppy);
181 // initialize and reset all devices except the event manager
182 #if defined(__GIT_REPO_VERSION)
183 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
185 for(DEVICE* device = first_device; device; device = device->next_device) {
186 device->initialize();
188 for(DEVICE* device = first_device; device; device = device->next_device) {
189 // if(device->this_device_id != event->this_device_id) {
193 for(int i = 0; i < 4; i++) {
194 fdc->set_drive_type(i, DRIVE_TYPE_2D);
200 // delete all devices
201 for(DEVICE* device = first_device; device;) {
202 DEVICE *next_device = device->next_device;
205 device = next_device;
209 DEVICE* VM::get_device(int id)
211 for(DEVICE* device = first_device; device; device = device->next_device) {
212 if(device->this_device_id == id) {
219 // ----------------------------------------------------------------------------
220 // drive virtual machine
221 // ----------------------------------------------------------------------------
226 // for(DEVICE* device = first_device; device; device = device->next_device) {
233 // set initial port status
235 pio0->write_signal(SIG_I8255_PORT_B, 0, (0x10 | 0x40));
237 pio0->write_signal(SIG_I8255_PORT_B, 0x10, (0x10 | 0x40));
239 pcm->write_signal(SIG_PCM1BIT_ON, 0, 1);
247 double VM::get_frame_rate()
249 return event->get_frame_rate();
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
257 DEVICE *VM::get_cpu(int index)
266 // ----------------------------------------------------------------------------
268 // ----------------------------------------------------------------------------
270 void VM::draw_screen()
272 display->draw_screen();
275 // ----------------------------------------------------------------------------
277 // ----------------------------------------------------------------------------
279 void VM::initialize_sound(int rate, int samples)
281 // init sound manager
282 event->initialize_sound(rate, samples);
285 pcm->initialize_sound(rate, 3600);
286 psg0->initialize_sound(rate, 1996800, 3600);
287 psg1->initialize_sound(rate, 1996800, 3600);
290 uint16_t* VM::create_sound(int* extra_frames)
292 return event->create_sound(extra_frames);
295 int VM::get_sound_buffer_ptr()
297 return event->get_sound_buffer_ptr();
300 #ifdef USE_SOUND_VOLUME
301 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
304 psg0->set_volume(0, decibel_l, decibel_r);
306 psg1->set_volume(0, decibel_l, decibel_r);
308 pcm->set_volume(0, decibel_l, decibel_r);
310 drec->set_volume(0, decibel_l, decibel_r);
312 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
313 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
314 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
316 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
317 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
318 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
323 // ----------------------------------------------------------------------------
325 // ----------------------------------------------------------------------------
327 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
329 fdc->open_disk(drv, file_path, bank);
332 void VM::close_floppy_disk(int drv)
334 fdc->close_disk(drv);
337 bool VM::is_floppy_disk_inserted(int drv)
339 return fdc->is_disk_inserted(drv);
342 void VM::is_floppy_disk_protected(int drv, bool value)
344 fdc->is_disk_protected(drv, value);
347 bool VM::is_floppy_disk_protected(int drv)
349 return fdc->is_disk_protected(drv);
352 uint32_t VM::is_floppy_disk_accessed()
354 return fdc->read_signal(0);
357 void VM::play_tape(int drv, const _TCHAR* file_path)
359 drec->play_tape(file_path);
360 // drec->set_remote(true);
363 void VM::rec_tape(int drv, const _TCHAR* file_path)
365 drec->rec_tape(file_path);
366 // drec->set_remote(true);
369 void VM::close_tape(int drv)
374 // drec->set_remote(false);
377 bool VM::is_tape_inserted(int drv)
379 return drec->is_tape_inserted();
382 bool VM::is_tape_playing(int drv)
384 return drec->is_tape_playing();
387 bool VM::is_tape_recording(int drv)
389 return drec->is_tape_recording();
392 int VM::get_tape_position(int drv)
394 return drec->get_tape_position();
397 const _TCHAR* VM::get_tape_message(int drv)
399 return drec->get_message();
402 void VM::push_play(int drv)
405 drec->set_remote(true);
408 void VM::push_stop(int drv)
410 drec->set_remote(false);
413 void VM::push_fast_forward(int drv)
416 drec->set_remote(true);
419 void VM::push_fast_rewind(int drv)
421 drec->set_ff_rew(-1);
422 drec->set_remote(true);
425 void VM::load_binary(int drv, const _TCHAR* file_path)
427 pac2->open_rampac2(drv, file_path);
430 bool VM::is_frame_skippable()
432 return event->is_frame_skippable();
435 void VM::update_config()
437 for(DEVICE* device = first_device; device; device = device->next_device) {
438 device->update_config();
442 #define STATE_VERSION 3
444 bool VM::process_state(FILEIO* state_fio, bool loading)
446 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
449 for(DEVICE* device = first_device; device; device = device->next_device) {
450 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
451 // const char *name = typeid(*device).name();
452 // But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
453 const char *name = device->get_device_name();
454 int len = strlen(name);
456 if(!state_fio->StateCheckInt32(len)) {
458 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
462 if(!state_fio->StateCheckBuffer(name, len, 1)) {
464 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
468 if(!device->process_state(state_fio, loading)) {
470 printf("Data loading Error: DEVID=%d\n", device->this_device_id);