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"
22 #include "../pcm1bit.h"
23 #include "../sn76489an.h"
24 #include "../upd765a.h"
26 #include "../z80ctc.h"
27 #include "../z80pio.h"
30 #include "../debugger.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 VM::VM(EMU* parent_emu) : emu(parent_emu)
48 first_device = last_device = NULL;
49 dummy = new DEVICE(this, emu); // must be 1st device
50 event = new EVENT(this, emu); // must be 2nd device
52 drec = new DATAREC(this, emu);
53 crtc = new HD46505(this, emu);
54 pio0 = new I8255(this, emu);
55 pio1 = new I8255(this, emu);
56 pio2 = new I8255(this, emu);
57 io = new IO(this, emu);
58 flipflop = new LS393(this, emu); // LS74
59 not_remote = new NOT(this, emu);
60 pcm = new PCM1BIT(this, emu);
61 psg0 = new SN76489AN(this, emu);
62 psg1 = new SN76489AN(this, emu);
63 fdc = new UPD765A(this, emu);
64 cpu = new Z80(this, emu);
65 ctc = new Z80CTC(this, emu);
66 pio = new Z80PIO(this, emu);
68 floppy = new FLOPPY(this, emu);
69 display = new DISPLAY(this, emu);
70 iobus = new IOBUS(this, emu);
71 iotrap = new IOTRAP(this, emu);
72 key = new KEYBOARD(this, emu);
73 memory = new MEMORY(this, emu);
74 pac2 = new PAC2(this, emu);
77 event->set_context_cpu(cpu);
78 event->set_context_sound(pcm);
79 event->set_context_sound(psg0);
80 event->set_context_sound(psg1);
81 event->set_context_sound(drec);
83 drec->set_context_ear(pio2, SIG_I8255_PORT_B, 0x20);
84 crtc->set_context_disp(pio0, SIG_I8255_PORT_B, 8);
85 crtc->set_context_vsync(pio0, SIG_I8255_PORT_B, 0x20);
86 pio0->set_context_port_a(display, SIG_DISPLAY_I8255_0_A, 0xff, 0);
87 pio1->set_context_port_a(memory, SIG_MEMORY_I8255_1_A, 0xff, 0);
88 pio1->set_context_port_b(display, SIG_DISPLAY_I8255_1_B, 0xff, 0);
89 pio1->set_context_port_b(memory, SIG_MEMORY_I8255_1_B, 0xff, 0);
90 pio1->set_context_port_c(display, SIG_DISPLAY_I8255_1_C, 0xff, 0);
91 pio1->set_context_port_c(memory, SIG_MEMORY_I8255_1_C, 0xff, 0);
92 pio2->set_context_port_a(pcm, SIG_PCM1BIT_MUTE, 0x02, 0);
93 pio2->set_context_port_a(psg0, SIG_SN76489AN_MUTE, 0x02, 0);
94 pio2->set_context_port_a(psg1, SIG_SN76489AN_MUTE, 0x02, 0);
95 pio2->set_context_port_a(drec, SIG_DATAREC_MIC, 0x10, 0);
96 pio2->set_context_port_a(not_remote, SIG_NOT_INPUT, 0x20, 0);
97 pio2->set_context_port_a(iotrap, SIG_IOTRAP_I8255_2_A, 0xff, 0);
98 pio2->set_context_port_c(iotrap, SIG_IOTRAP_I8255_2_C, 0xff, 0);
99 flipflop->set_context_1qa(pcm, SIG_PCM1BIT_SIGNAL, 1);
100 not_remote->set_context_out(drec, SIG_DATAREC_REMOTE, 1);
101 fdc->set_context_irq(floppy, SIG_FLOPPY_INTR, 1);
102 ctc->set_context_zc0(ctc, SIG_Z80CTC_TRIG_1, 1);
103 ctc->set_context_zc1(flipflop, SIG_LS393_CLK, 1);
104 ctc->set_context_zc2(ctc, SIG_Z80CTC_TRIG_3, 1);
105 ctc->set_constant_clock(0, CPU_CLOCKS);
106 ctc->set_constant_clock(2, CPU_CLOCKS);
107 pio->set_context_port_a(pcm, SIG_PCM1BIT_ON, 0x80, 0);
108 pio->set_context_port_a(key, SIG_KEYBOARD_Z80PIO_A, 0xff, 0);
110 display->set_vram_ptr(memory->get_vram());
111 display->set_pal_ptr(memory->get_pal());
112 display->set_regs_ptr(crtc->get_regs());
113 floppy->set_context_fdc(fdc);
114 iobus->set_context_io(io);
115 iobus->set_ram_ptr(memory->get_ram());
116 iotrap->set_context_cpu(cpu);
117 iotrap->set_context_pio2(pio2);
118 key->set_context_pio(pio);
119 memory->set_context_iobus(iobus);
120 memory->set_context_pio0(pio0);
121 memory->set_context_pio2(pio2);
124 cpu->set_context_mem(memory);
125 cpu->set_context_io(iobus);
126 cpu->set_context_intr(ctc);
128 cpu->set_context_debugger(new DEBUGGER(this, emu));
131 // z80 family daisy chain
132 ctc->set_context_intr(cpu, 0);
133 ctc->set_context_child(pio);
134 pio->set_context_intr(cpu, 1);
137 io->set_iomap_range_rw(0x08, 0x0b, pio0);
138 io->set_iomap_range_rw(0x0c, 0x0f, pio1);
139 io->set_iomap_range_rw(0x10, 0x11, crtc);
140 io->set_iomap_range_rw(0x18, 0x1b, pac2);
141 io->set_iomap_range_rw(0x20, 0x23, pio2);
142 io->set_iomap_range_rw(0x28, 0x2b, ctc);
143 io->set_iomap_alias_rw(0x30, pio, 0);
144 io->set_iomap_alias_rw(0x31, pio, 2);
145 io->set_iomap_alias_w(0x32, pio, 1);
146 io->set_iomap_alias_w(0x33, pio, 3);
147 io->set_iomap_single_w(0x3a, psg0);
148 io->set_iomap_single_w(0x3b, psg1);
149 io->set_iomap_single_w(0x3c, memory);
150 io->set_iomap_single_w(0xe0, floppy);
151 io->set_iomap_single_w(0xe2, floppy);
152 io->set_iomap_range_rw(0xe4, 0xe5, fdc);
153 io->set_iomap_single_rw(0xe6, floppy);
155 // initialize and reset all devices except the event manager
156 for(DEVICE* device = first_device; device; device = device->next_device) {
157 device->initialize();
159 for(DEVICE* device = first_device; device; device = device->next_device) {
160 // if(device->this_device_id != event->this_device_id) {
164 for(int i = 0; i < 4; i++) {
165 fdc->set_drive_type(i, DRIVE_TYPE_2D);
171 // delete all devices
172 for(DEVICE* device = first_device; device;) {
173 DEVICE *next_device = device->next_device;
176 device = next_device;
180 DEVICE* VM::get_device(int id)
182 for(DEVICE* device = first_device; device; device = device->next_device) {
183 if(device->this_device_id == id) {
190 // ----------------------------------------------------------------------------
191 // drive virtual machine
192 // ----------------------------------------------------------------------------
197 // for(DEVICE* device = first_device; device; device = device->next_device) {
204 // set initial port status
206 pio0->write_signal(SIG_I8255_PORT_B, 0, (0x10 | 0x40));
208 pio0->write_signal(SIG_I8255_PORT_B, 0x10, (0x10 | 0x40));
210 pcm->write_signal(SIG_PCM1BIT_ON, 0, 1);
218 double VM::frame_rate()
220 return event->frame_rate();
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
228 DEVICE *VM::get_cpu(int index)
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 void VM::draw_screen()
243 display->draw_screen();
246 int VM::access_lamp()
248 uint32 status = fdc->read_signal(0);
249 return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 void VM::initialize_sound(int rate, int samples)
258 // init sound manager
259 event->initialize_sound(rate, samples);
262 pcm->init(rate, 3600);
263 psg0->init(rate, 1996800, 3600);
264 psg1->init(rate, 1996800, 3600);
267 uint16* VM::create_sound(int* extra_frames)
269 return event->create_sound(extra_frames);
272 int VM::sound_buffer_ptr()
274 return event->sound_buffer_ptr();
277 #ifdef USE_SOUND_VOLUME
278 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
281 psg0->set_volume(0, decibel_l, decibel_r);
283 psg1->set_volume(0, decibel_l, decibel_r);
285 pcm->set_volume(0, decibel_l, decibel_r);
287 drec->set_volume(0, decibel_l, decibel_r);
292 // ----------------------------------------------------------------------------
294 // ----------------------------------------------------------------------------
296 void VM::open_disk(int drv, const _TCHAR* file_path, int bank)
298 fdc->open_disk(drv, file_path, bank);
301 void VM::close_disk(int drv)
303 fdc->close_disk(drv);
306 bool VM::disk_inserted(int drv)
308 return fdc->disk_inserted(drv);
311 void VM::set_disk_protected(int drv, bool value)
313 fdc->set_disk_protected(drv, value);
316 bool VM::get_disk_protected(int drv)
318 return fdc->get_disk_protected(drv);
321 void VM::play_tape(const _TCHAR* file_path)
323 drec->play_tape(file_path);
326 void VM::rec_tape(const _TCHAR* file_path)
328 drec->rec_tape(file_path);
331 void VM::close_tape()
336 bool VM::tape_inserted()
338 return drec->tape_inserted();
341 bool VM::tape_playing()
343 return drec->tape_playing();
346 bool VM::tape_recording()
348 return drec->tape_recording();
351 int VM::tape_position()
353 return drec->tape_position();
356 void VM::load_binary(int drv, const _TCHAR* file_path)
358 pac2->open_rampac2(drv, file_path);
363 return event->now_skip();
366 void VM::update_config()
368 for(DEVICE* device = first_device; device; device = device->next_device) {
369 device->update_config();
373 #define STATE_VERSION 1
375 void VM::save_state(FILEIO* state_fio)
377 state_fio->FputUint32(STATE_VERSION);
379 for(DEVICE* device = first_device; device; device = device->next_device) {
380 device->save_state(state_fio);
384 bool VM::load_state(FILEIO* state_fio)
386 if(state_fio->FgetUint32() != STATE_VERSION) {
389 for(DEVICE* device = first_device; device; device = device->next_device) {
390 if(!device->load_state(state_fio)) {