2 TOSHIBA PASOPIA Emulator 'EmuPIA'
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 "../upd765a.h"
26 #include "../z80ctc.h"
27 #include "../z80pio.h"
30 #include "../debugger.h"
39 using PASOPIA::DISPLAY;
40 using PASOPIA::FLOPPY;
41 using PASOPIA::KEYBOARD;
42 using PASOPIA::MEMORY;
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
52 boot_mode = config.boot_mode;
55 first_device = last_device = NULL;
56 dummy = new DEVICE(this, emu); // must be 1st device
57 event = new EVENT(this, emu); // must be 2nd device
58 dummy->set_device_name(_T("1st Dummy"));
60 drec = new DATAREC(this, emu);
61 drec->set_context_noise_play(new NOISE(this, emu));
62 drec->set_context_noise_stop(new NOISE(this, emu));
63 drec->set_context_noise_fast(new NOISE(this, emu));
64 crtc = new HD46505(this, emu);
65 pio0 = new I8255(this, emu);
66 pio0->set_device_name(_T("8255 PIO (Memory)"));
67 pio1 = new I8255(this, emu);
68 pio1->set_device_name(_T("8255 PIO (Memory/Display)"));
69 pio2 = new I8255(this, emu);
70 pio2->set_device_name(_T("8255 PIO (Sound/Data Recorder)"));
71 io = new IO(this, emu);
72 flipflop = new LS393(this, emu); // LS74
73 not_remote = new NOT(this, emu);
74 pcm = new PCM1BIT(this, emu);
75 fdc = new UPD765A(this, emu);
76 fdc->set_context_noise_seek(new NOISE(this, emu));
77 fdc->set_context_noise_head_down(new NOISE(this, emu));
78 fdc->set_context_noise_head_up(new NOISE(this, emu));
79 cpu = new Z80(this, emu);
80 ctc = new Z80CTC(this, emu);
81 pio = new Z80PIO(this, emu);
83 floppy = new FLOPPY(this, emu);
84 display = new DISPLAY(this, emu);
85 key = new KEYBOARD(this, emu);
86 memory = new MEMORY(this, emu);
87 pac2 = new PAC2(this, emu);
89 event->set_context_cpu(cpu);
90 event->set_context_sound(pcm);
91 event->set_context_sound(drec);
92 event->set_context_sound(fdc->get_context_noise_seek());
93 event->set_context_sound(fdc->get_context_noise_head_down());
94 event->set_context_sound(fdc->get_context_noise_head_up());
95 event->set_context_sound(drec->get_context_noise_play());
96 event->set_context_sound(drec->get_context_noise_stop());
97 event->set_context_sound(drec->get_context_noise_fast());
99 drec->set_context_ear(pio2, SIG_I8255_PORT_B, 0x20);
100 crtc->set_context_disp(pio1, SIG_I8255_PORT_B, 8);
101 crtc->set_context_vsync(pio1, SIG_I8255_PORT_B, 0x20);
102 crtc->set_context_hsync(pio1, SIG_I8255_PORT_B, 0x40);
103 pio0->set_context_port_a(memory, SIG_MEMORY_I8255_0_A, 0xff, 0);
104 pio0->set_context_port_b(memory, SIG_MEMORY_I8255_0_B, 0xff, 0);
105 pio1->set_context_port_a(display, SIG_DISPLAY_I8255_1_A, 0xff, 0);
106 pio1->set_context_port_c(memory, SIG_MEMORY_I8255_1_C, 0xff, 0);
107 pio2->set_context_port_a(pcm, SIG_PCM1BIT_MUTE, 0x02, 0);
108 pio2->set_context_port_a(drec, SIG_DATAREC_MIC, 0x10, 0);
109 pio2->set_context_port_a(not_remote, SIG_NOT_INPUT, 0x20, 0);
110 flipflop->set_context_1qa(pcm, SIG_PCM1BIT_SIGNAL, 1);
111 not_remote->set_context_out(drec, SIG_DATAREC_REMOTE, 1);
112 fdc->set_context_irq(floppy, SIG_FLOPPY_INTR, 1);
113 ctc->set_context_zc1(flipflop, SIG_LS393_CLK, 1);
114 ctc->set_context_zc2(ctc, SIG_Z80CTC_TRIG_3, 1);
115 ctc->set_constant_clock(0, CPU_CLOCKS);
116 ctc->set_constant_clock(1, CPU_CLOCKS);
117 ctc->set_constant_clock(2, CPU_CLOCKS);
118 pio->set_context_port_a(pcm, SIG_PCM1BIT_ON, 0x80, 0);
119 pio->set_context_port_a(key, SIG_KEYBOARD_Z80PIO_A, 0xff, 0);
121 display->set_context_crtc(crtc);
122 display->set_vram_ptr(memory->get_vram());
123 display->set_attr_ptr(memory->get_attr());
124 display->set_regs_ptr(crtc->get_regs());
125 floppy->set_context_fdc(fdc);
126 floppy->supported = (boot_mode != MODE_OABASIC_NO_DISK);
127 key->set_context_pio(pio);
128 memory->set_context_pio0(pio0);
129 memory->set_context_pio1(pio1);
130 memory->set_context_pio2(pio2);
133 cpu->set_context_mem(memory);
134 cpu->set_context_io(io);
135 cpu->set_context_intr(ctc);
137 cpu->set_context_debugger(new DEBUGGER(this, emu));
140 // z80 family daisy chain
141 ctc->set_context_intr(cpu, 0);
142 ctc->set_context_child(pio);
143 pio->set_context_intr(cpu, 1);
145 pio0 0 8255 vram laddr
149 pio1 8 8255 crtc mode
151 a 8255 vram addr, attrib & strobe
159 pio2 20 8255 out cmt, sound
161 22 8255 in memory map
172 3c memory out memory map
175 io->set_iomap_range_rw(0x00, 0x03, pio0);
176 io->set_iomap_range_rw(0x08, 0x0b, pio1);
177 io->set_iomap_range_r(0x10, 0x11, crtc);
178 io->set_iomap_range_w(0x10, 0x11, display);
179 io->set_iomap_range_rw(0x18, 0x1b, pac2);
180 io->set_iomap_range_rw(0x20, 0x23, pio2);
181 io->set_iomap_range_rw(0x28, 0x2b, ctc);
182 io->set_iomap_alias_rw(0x30, pio, 0);
183 io->set_iomap_alias_rw(0x31, pio, 2);
184 io->set_iomap_alias_w(0x32, pio, 1);
185 io->set_iomap_alias_w(0x33, pio, 3);
186 io->set_iomap_single_w(0x3c, memory);
187 io->set_iomap_single_w(0xe0, floppy);
188 io->set_iomap_single_w(0xe2, floppy);
189 io->set_iomap_range_rw(0xe4, 0xe5, floppy);
190 io->set_iomap_single_rw(0xe6, floppy);
192 // initialize all devices
193 #if defined(__GIT_REPO_VERSION)
194 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
196 for(DEVICE* device = first_device; device; device = device->next_device) {
197 device->initialize();
199 for(int i = 0; i < 4; i++) {
200 fdc->set_drive_type(i, DRIVE_TYPE_2D);
206 // delete all devices
207 for(DEVICE* device = first_device; device;) {
208 DEVICE *next_device = device->next_device;
211 device = next_device;
215 DEVICE* VM::get_device(int id)
217 for(DEVICE* device = first_device; device; device = device->next_device) {
218 if(device->this_device_id == id) {
225 // ----------------------------------------------------------------------------
226 // drive virtual machine
227 // ----------------------------------------------------------------------------
232 for(DEVICE* device = first_device; device; device = device->next_device) {
236 // set initial port status
238 pio1->write_signal(SIG_I8255_PORT_B, 0, 0x10);
240 pio1->write_signal(SIG_I8255_PORT_B, 0xffffffff, 0x10);
242 pcm->write_signal(SIG_PCM1BIT_ON, 0, 1);
250 double VM::get_frame_rate()
252 return event->get_frame_rate();
255 // ----------------------------------------------------------------------------
257 // ----------------------------------------------------------------------------
260 DEVICE *VM::get_cpu(int index)
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 void VM::draw_screen()
275 display->draw_screen();
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
282 void VM::initialize_sound(int rate, int samples)
284 // init sound manager
285 event->initialize_sound(rate, samples);
288 pcm->initialize_sound(rate, 8000);
291 uint16_t* VM::create_sound(int* extra_frames)
293 return event->create_sound(extra_frames);
296 int VM::get_sound_buffer_ptr()
298 return event->get_sound_buffer_ptr();
301 #ifdef USE_SOUND_VOLUME
302 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
305 pcm->set_volume(0, decibel_l, decibel_r);
307 drec->set_volume(0, decibel_l, decibel_r);
309 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
310 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
311 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
313 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
314 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
315 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
320 // ----------------------------------------------------------------------------
322 // ----------------------------------------------------------------------------
324 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
326 fdc->open_disk(drv, file_path, bank);
329 void VM::close_floppy_disk(int drv)
331 fdc->close_disk(drv);
334 bool VM::is_floppy_disk_inserted(int drv)
336 return fdc->is_disk_inserted(drv);
339 void VM::is_floppy_disk_protected(int drv, bool value)
341 fdc->is_disk_protected(drv, value);
344 bool VM::is_floppy_disk_protected(int drv)
346 return fdc->is_disk_protected(drv);
349 uint32_t VM::is_floppy_disk_accessed()
351 return fdc->read_signal(0);
354 void VM::play_tape(int drv, const _TCHAR* file_path)
356 drec->play_tape(file_path);
357 // drec->set_remote(true);
360 void VM::rec_tape(int drv, const _TCHAR* file_path)
362 drec->rec_tape(file_path);
363 // drec->set_remote(true);
366 void VM::close_tape(int drv)
371 // drec->set_remote(false);
374 bool VM::is_tape_inserted(int drv)
376 return drec->is_tape_inserted();
379 bool VM::is_tape_playing(int drv)
381 return drec->is_tape_playing();
384 bool VM::is_tape_recording(int drv)
386 return drec->is_tape_recording();
389 int VM::get_tape_position(int drv)
391 return drec->get_tape_position();
394 const _TCHAR* VM::get_tape_message(int drv)
396 return drec->get_message();
399 void VM::push_play(int drv)
402 drec->set_remote(true);
405 void VM::push_stop(int drv)
407 drec->set_remote(false);
410 void VM::push_fast_forward(int drv)
413 drec->set_remote(true);
416 void VM::push_fast_rewind(int drv)
418 drec->set_ff_rew(-1);
419 drec->set_remote(true);
422 void VM::load_binary(int drv, const _TCHAR* file_path)
425 pac2->open_rampac2(file_path);
429 bool VM::is_frame_skippable()
431 return event->is_frame_skippable();
434 void VM::update_config()
436 if(boot_mode != config.boot_mode) {
437 // boot mode is changed !!!
438 boot_mode = config.boot_mode;
440 floppy->supported = (boot_mode != MODE_OABASIC_NO_DISK);
441 // reset virtual machine
444 for(DEVICE* device = first_device; device; device = device->next_device) {
445 device->update_config();
450 #define STATE_VERSION 3
452 bool VM::process_state(FILEIO* state_fio, bool loading)
454 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
457 for(DEVICE* device = first_device; device; device = device->next_device) {
458 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
459 // const char *name = typeid(*device).name();
460 // But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
461 const char *name = device->get_device_name();
462 int len = strlen(name);
464 if(!state_fio->StateCheckInt32(len)) {
466 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
470 if(!state_fio->StateCheckBuffer(name, len, 1)) {
472 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
476 if(!device->process_state(state_fio, loading)) {
478 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
483 // Machine specified.
484 state_fio->StateInt32(boot_mode);