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 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
49 first_device = last_device = NULL;
50 dummy = new DEVICE(this, emu); // must be 1st device
51 event = new EVENT(this, emu); // must be 2nd device
53 drec = new DATAREC(this, emu);
54 drec->set_context_noise_play(new NOISE(this, emu));
55 drec->set_context_noise_stop(new NOISE(this, emu));
56 drec->set_context_noise_fast(new NOISE(this, emu));
57 crtc = new HD46505(this, emu);
58 pio0 = new I8255(this, emu);
59 pio0->set_device_name(_T("8255 PIO (Display)"));
60 pio1 = new I8255(this, emu);
61 pio1->set_device_name(_T("8255 PIO (Display/Memory)"));
62 pio2 = new I8255(this, emu);
63 pio2->set_device_name(_T("8255 PIO (Sound/Data Recorder)"));
64 io = new IO(this, emu);
65 flipflop = new LS393(this, emu); // LS74
66 not_remote = new NOT(this, emu);
67 pcm = new PCM1BIT(this, emu);
68 psg0 = new SN76489AN(this, emu);
69 psg0->set_device_name(_T("SN76489AN PSG #1"));
70 psg1 = new SN76489AN(this, emu);
71 psg1->set_device_name(_T("SN76489AN PSG #2"));
72 fdc = new UPD765A(this, emu);
73 fdc->set_context_noise_seek(new NOISE(this, emu));
74 fdc->set_context_noise_head_down(new NOISE(this, emu));
75 fdc->set_context_noise_head_up(new NOISE(this, emu));
76 cpu = new Z80(this, emu);
77 ctc = new Z80CTC(this, emu);
78 pio = new Z80PIO(this, emu);
80 floppy = new FLOPPY(this, emu);
81 display = new DISPLAY(this, emu);
82 iobus = new IOBUS(this, emu);
83 iotrap = new IOTRAP(this, emu);
84 key = new KEYBOARD(this, emu);
85 memory = new PASOPIA7_MEMORY(this, emu);
86 pac2 = new PAC2(this, emu);
89 event->set_context_cpu(cpu);
90 event->set_context_sound(pcm);
91 event->set_context_sound(psg0);
92 event->set_context_sound(psg1);
93 event->set_context_sound(drec);
94 event->set_context_sound(fdc->get_context_noise_seek());
95 event->set_context_sound(fdc->get_context_noise_head_down());
96 event->set_context_sound(fdc->get_context_noise_head_up());
97 event->set_context_sound(drec->get_context_noise_play());
98 event->set_context_sound(drec->get_context_noise_stop());
99 event->set_context_sound(drec->get_context_noise_fast());
101 drec->set_context_ear(pio2, SIG_I8255_PORT_B, 0x20);
102 crtc->set_context_disp(pio0, SIG_I8255_PORT_B, 8);
103 crtc->set_context_vsync(pio0, SIG_I8255_PORT_B, 0x20);
104 pio0->set_context_port_a(display, SIG_DISPLAY_I8255_0_A, 0xff, 0);
105 pio1->set_context_port_a(memory, SIG_MEMORY_I8255_1_A, 0xff, 0);
106 pio1->set_context_port_b(display, SIG_DISPLAY_I8255_1_B, 0xff, 0);
107 pio1->set_context_port_b(memory, SIG_MEMORY_I8255_1_B, 0xff, 0);
108 pio1->set_context_port_c(display, SIG_DISPLAY_I8255_1_C, 0xff, 0);
109 pio1->set_context_port_c(memory, SIG_MEMORY_I8255_1_C, 0xff, 0);
110 pio2->set_context_port_a(pcm, SIG_PCM1BIT_MUTE, 0x02, 0);
111 pio2->set_context_port_a(psg0, SIG_SN76489AN_MUTE, 0x02, 0);
112 pio2->set_context_port_a(psg1, SIG_SN76489AN_MUTE, 0x02, 0);
113 pio2->set_context_port_a(drec, SIG_DATAREC_MIC, 0x10, 0);
114 pio2->set_context_port_a(not_remote, SIG_NOT_INPUT, 0x20, 0);
115 pio2->set_context_port_a(iotrap, SIG_IOTRAP_I8255_2_A, 0xff, 0);
116 pio2->set_context_port_c(iotrap, SIG_IOTRAP_I8255_2_C, 0xff, 0);
117 flipflop->set_context_1qa(pcm, SIG_PCM1BIT_SIGNAL, 1);
118 not_remote->set_context_out(drec, SIG_DATAREC_REMOTE, 1);
119 fdc->set_context_irq(floppy, SIG_FLOPPY_INTR, 1);
120 ctc->set_context_zc0(ctc, SIG_Z80CTC_TRIG_1, 1);
121 ctc->set_context_zc1(flipflop, SIG_LS393_CLK, 1);
122 ctc->set_context_zc2(ctc, SIG_Z80CTC_TRIG_3, 1);
123 ctc->set_constant_clock(0, CPU_CLOCKS);
124 ctc->set_constant_clock(2, CPU_CLOCKS);
125 pio->set_context_port_a(pcm, SIG_PCM1BIT_ON, 0x80, 0);
126 pio->set_context_port_a(key, SIG_KEYBOARD_Z80PIO_A, 0xff, 0);
128 display->set_vram_ptr(memory->get_vram());
129 display->set_pal_ptr(memory->get_pal());
130 display->set_regs_ptr(crtc->get_regs());
131 floppy->set_context_fdc(fdc);
132 iobus->set_context_io(io);
133 iobus->set_ram_ptr(memory->get_ram());
134 iotrap->set_context_cpu(cpu);
135 iotrap->set_context_pio2(pio2);
136 key->set_context_pio(pio);
137 memory->set_context_iobus(iobus);
138 memory->set_context_pio0(pio0);
139 memory->set_context_pio2(pio2);
142 cpu->set_context_mem(memory);
143 cpu->set_context_io(iobus);
144 cpu->set_context_intr(ctc);
146 cpu->set_context_debugger(new DEBUGGER(this, emu));
149 // z80 family daisy chain
150 ctc->set_context_intr(cpu, 0);
151 ctc->set_context_child(pio);
152 pio->set_context_intr(cpu, 1);
155 io->set_iomap_range_rw(0x08, 0x0b, pio0);
156 io->set_iomap_range_rw(0x0c, 0x0f, pio1);
157 io->set_iomap_range_rw(0x10, 0x11, crtc);
158 io->set_iomap_range_rw(0x18, 0x1b, pac2);
159 io->set_iomap_range_rw(0x20, 0x23, pio2);
160 io->set_iomap_range_rw(0x28, 0x2b, ctc);
161 io->set_iomap_alias_rw(0x30, pio, 0);
162 io->set_iomap_alias_rw(0x31, pio, 2);
163 io->set_iomap_alias_w(0x32, pio, 1);
164 io->set_iomap_alias_w(0x33, pio, 3);
165 io->set_iomap_single_w(0x3a, psg0);
166 io->set_iomap_single_w(0x3b, psg1);
167 io->set_iomap_single_w(0x3c, memory);
168 io->set_iomap_single_w(0xe0, floppy);
169 io->set_iomap_single_w(0xe2, floppy);
170 io->set_iomap_range_rw(0xe4, 0xe5, fdc);
171 io->set_iomap_single_rw(0xe6, floppy);
173 // initialize and reset all devices except the event manager
174 #if defined(__GIT_REPO_VERSION)
175 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
177 for(DEVICE* device = first_device; device; device = device->next_device) {
178 device->initialize();
180 for(DEVICE* device = first_device; device; device = device->next_device) {
181 // if(device->this_device_id != event->this_device_id) {
185 for(int i = 0; i < 4; i++) {
186 fdc->set_drive_type(i, DRIVE_TYPE_2D);
192 // delete all devices
193 for(DEVICE* device = first_device; device;) {
194 DEVICE *next_device = device->next_device;
197 device = next_device;
201 DEVICE* VM::get_device(int id)
203 for(DEVICE* device = first_device; device; device = device->next_device) {
204 if(device->this_device_id == id) {
211 // ----------------------------------------------------------------------------
212 // drive virtual machine
213 // ----------------------------------------------------------------------------
218 // for(DEVICE* device = first_device; device; device = device->next_device) {
225 // set initial port status
227 pio0->write_signal(SIG_I8255_PORT_B, 0, (0x10 | 0x40));
229 pio0->write_signal(SIG_I8255_PORT_B, 0x10, (0x10 | 0x40));
231 pcm->write_signal(SIG_PCM1BIT_ON, 0, 1);
239 double VM::get_frame_rate()
241 return event->get_frame_rate();
244 // ----------------------------------------------------------------------------
246 // ----------------------------------------------------------------------------
249 DEVICE *VM::get_cpu(int index)
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 void VM::draw_screen()
264 display->draw_screen();
267 // ----------------------------------------------------------------------------
269 // ----------------------------------------------------------------------------
271 void VM::initialize_sound(int rate, int samples)
273 // init sound manager
274 event->initialize_sound(rate, samples);
277 pcm->initialize_sound(rate, 3600);
278 psg0->initialize_sound(rate, 1996800, 3600);
279 psg1->initialize_sound(rate, 1996800, 3600);
282 uint16_t* VM::create_sound(int* extra_frames)
284 return event->create_sound(extra_frames);
287 int VM::get_sound_buffer_ptr()
289 return event->get_sound_buffer_ptr();
292 #ifdef USE_SOUND_VOLUME
293 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
296 psg0->set_volume(0, decibel_l, decibel_r);
298 psg1->set_volume(0, decibel_l, decibel_r);
300 pcm->set_volume(0, decibel_l, decibel_r);
302 drec->set_volume(0, decibel_l, decibel_r);
304 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
305 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
306 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
308 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
309 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
310 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
321 fdc->open_disk(drv, file_path, bank);
324 void VM::close_floppy_disk(int drv)
326 fdc->close_disk(drv);
329 bool VM::is_floppy_disk_inserted(int drv)
331 return fdc->is_disk_inserted(drv);
334 void VM::is_floppy_disk_protected(int drv, bool value)
336 fdc->is_disk_protected(drv, value);
339 bool VM::is_floppy_disk_protected(int drv)
341 return fdc->is_disk_protected(drv);
344 uint32_t VM::is_floppy_disk_accessed()
346 return fdc->read_signal(0);
349 void VM::play_tape(int drv, const _TCHAR* file_path)
351 drec->play_tape(file_path);
352 // drec->set_remote(true);
355 void VM::rec_tape(int drv, const _TCHAR* file_path)
357 drec->rec_tape(file_path);
358 // drec->set_remote(true);
361 void VM::close_tape(int drv)
366 // drec->set_remote(false);
369 bool VM::is_tape_inserted(int drv)
371 return drec->is_tape_inserted();
374 bool VM::is_tape_playing(int drv)
376 return drec->is_tape_playing();
379 bool VM::is_tape_recording(int drv)
381 return drec->is_tape_recording();
384 int VM::get_tape_position(int drv)
386 return drec->get_tape_position();
389 const _TCHAR* VM::get_tape_message(int drv)
391 return drec->get_message();
394 void VM::push_play(int drv)
397 drec->set_remote(true);
400 void VM::push_stop(int drv)
402 drec->set_remote(false);
405 void VM::push_fast_forward(int drv)
408 drec->set_remote(true);
411 void VM::push_fast_rewind(int drv)
413 drec->set_ff_rew(-1);
414 drec->set_remote(true);
417 void VM::load_binary(int drv, const _TCHAR* file_path)
419 pac2->open_rampac2(drv, file_path);
422 bool VM::is_frame_skippable()
424 return event->is_frame_skippable();
427 void VM::update_config()
429 for(DEVICE* device = first_device; device; device = device->next_device) {
430 device->update_config();
434 #define STATE_VERSION 3
436 bool VM::process_state(FILEIO* state_fio, bool loading)
438 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
441 for(DEVICE* device = first_device; device; device = device->next_device) {
442 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
443 // const char *name = typeid(*device).name();
444 // But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
445 const char *name = device->get_device_name();
446 int len = strlen(name);
448 if(!state_fio->StateCheckInt32(len)) {
450 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
454 if(!state_fio->StateCheckBuffer(name, len, 1)) {
456 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
460 if(!device->process_state(state_fio, loading)) {
462 printf("Data loading Error: DEVID=%d\n", device->this_device_id);