OSDN Git Service

[VM][General] Merge upstream 2016-02-13. Still don't implement OSD/Gui part of joysti...
[csp-qt/common_source_project-fm7.git] / source / src / vm / pasopia7 / pasopia7.cpp
1 /*
2         TOSHIBA PASOPIA 7 Emulator 'EmuPIA7'
3
4         Author : Takeda.Toshiya
5         Date   : 2006.09.20 -
6
7         [ virtual machine ]
8 */
9
10 #include "pasopia7.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../disk.h"
17 #include "../hd46505.h"
18 #include "../i8255.h"
19 #include "../io.h"
20 #include "../ls393.h"
21 #include "../not.h"
22 #include "../pcm1bit.h"
23 #include "../sn76489an.h"
24 #include "../upd765a.h"
25 #include "../z80.h"
26 #include "../z80ctc.h"
27 #include "../z80pio.h"
28
29 #ifdef USE_DEBUGGER
30 #include "../debugger.h"
31 #endif
32
33 #include "floppy.h"
34 #include "display.h"
35 #include "iobus.h"
36 #include "iotrap.h"
37 #include "keyboard.h"
38 #include "memory.h"
39 #include "pac2.h"
40
41 // ----------------------------------------------------------------------------
42 // initialize
43 // ----------------------------------------------------------------------------
44
45 VM::VM(EMU* parent_emu) : emu(parent_emu)
46 {
47         // create devices
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
51         
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);
67         
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);
75         
76         // set contexts
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);
82         
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);
109         
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);
122         
123         // cpu bus
124         cpu->set_context_mem(memory);
125         cpu->set_context_io(iobus);
126         cpu->set_context_intr(ctc);
127 #ifdef USE_DEBUGGER
128         cpu->set_context_debugger(new DEBUGGER(this, emu));
129 #endif
130         
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);
135         
136         // i/o bus
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);
154         
155         // initialize and reset all devices except the event manager
156         for(DEVICE* device = first_device; device; device = device->next_device) {
157                 device->initialize();
158         }
159         for(DEVICE* device = first_device; device; device = device->next_device) {
160 //              if(device->this_device_id != event->this_device_id) {
161                         device->reset();
162 //              }
163         }
164         for(int i = 0; i < 4; i++) {
165                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
166         }
167 }
168
169 VM::~VM()
170 {
171         // delete all devices
172         for(DEVICE* device = first_device; device;) {
173                 DEVICE *next_device = device->next_device;
174                 device->release();
175                 delete device;
176                 device = next_device;
177         }
178 }
179
180 DEVICE* VM::get_device(int id)
181 {
182         for(DEVICE* device = first_device; device; device = device->next_device) {
183                 if(device->this_device_id == id) {
184                         return device;
185                 }
186         }
187         return NULL;
188 }
189
190 // ----------------------------------------------------------------------------
191 // drive virtual machine
192 // ----------------------------------------------------------------------------
193
194 void VM::reset()
195 {
196         // reset all devices
197 //      for(DEVICE* device = first_device; device; device = device->next_device) {
198 //              device->reset();
199 //      }
200         event->reset();
201         memory->reset();
202         iotrap->do_reset();
203         
204         // set initial port status
205 #ifdef _LCD
206         pio0->write_signal(SIG_I8255_PORT_B, 0, (0x10 | 0x40));
207 #else
208         pio0->write_signal(SIG_I8255_PORT_B, 0x10, (0x10 | 0x40));
209 #endif
210         pcm->write_signal(SIG_PCM1BIT_ON, 0, 1);
211 }
212
213 void VM::run()
214 {
215         event->drive();
216 }
217
218 double VM::frame_rate()
219 {
220         return event->frame_rate();
221 }
222
223 // ----------------------------------------------------------------------------
224 // debugger
225 // ----------------------------------------------------------------------------
226
227 #ifdef USE_DEBUGGER
228 DEVICE *VM::get_cpu(int index)
229 {
230         if(index == 0) {
231                 return cpu;
232         }
233         return NULL;
234 }
235 #endif
236
237 // ----------------------------------------------------------------------------
238 // draw screen
239 // ----------------------------------------------------------------------------
240
241 void VM::draw_screen()
242 {
243         display->draw_screen();
244 }
245
246 int VM::access_lamp()
247 {
248         uint32 status = fdc->read_signal(0);
249         return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
250 }
251
252 // ----------------------------------------------------------------------------
253 // soud manager
254 // ----------------------------------------------------------------------------
255
256 void VM::initialize_sound(int rate, int samples)
257 {
258         // init sound manager
259         event->initialize_sound(rate, samples);
260         
261         // init sound gen
262         pcm->init(rate, 3600);
263         psg0->init(rate, 1996800, 3600);
264         psg1->init(rate, 1996800, 3600);
265 }
266
267 uint16* VM::create_sound(int* extra_frames)
268 {
269         return event->create_sound(extra_frames);
270 }
271
272 int VM::sound_buffer_ptr()
273 {
274         return event->sound_buffer_ptr();
275 }
276
277 #ifdef USE_SOUND_VOLUME
278 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
279 {
280         if(ch == 0) {
281                 psg0->set_volume(0, decibel_l, decibel_r);
282         } else if(ch == 1) {
283                 psg1->set_volume(0, decibel_l, decibel_r);
284         } else if(ch == 2) {
285                 pcm->set_volume(0, decibel_l, decibel_r);
286         } else if(ch == 3) {
287                 drec->set_volume(0, decibel_l, decibel_r);
288         }
289 }
290 #endif
291
292 // ----------------------------------------------------------------------------
293 // user interface
294 // ----------------------------------------------------------------------------
295
296 void VM::open_disk(int drv, const _TCHAR* file_path, int bank)
297 {
298         fdc->open_disk(drv, file_path, bank);
299 }
300
301 void VM::close_disk(int drv)
302 {
303         fdc->close_disk(drv);
304 }
305
306 bool VM::disk_inserted(int drv)
307 {
308         return fdc->disk_inserted(drv);
309 }
310
311 void VM::set_disk_protected(int drv, bool value)
312 {
313         fdc->set_disk_protected(drv, value);
314 }
315
316 bool VM::get_disk_protected(int drv)
317 {
318         return fdc->get_disk_protected(drv);
319 }
320
321 void VM::play_tape(const _TCHAR* file_path)
322 {
323         drec->play_tape(file_path);
324 }
325
326 void VM::rec_tape(const _TCHAR* file_path)
327 {
328         drec->rec_tape(file_path);
329 }
330
331 void VM::close_tape()
332 {
333         drec->close_tape();
334 }
335
336 bool VM::tape_inserted()
337 {
338         return drec->tape_inserted();
339 }
340
341 bool VM::tape_playing()
342 {
343         return drec->tape_playing();
344 }
345
346 bool VM::tape_recording()
347 {
348         return drec->tape_recording();
349 }
350
351 int VM::tape_position()
352 {
353         return drec->tape_position();
354 }
355
356 void VM::load_binary(int drv, const _TCHAR* file_path)
357 {
358         pac2->open_rampac2(drv, file_path);
359 }
360
361 bool VM::now_skip()
362 {
363         return event->now_skip();
364 }
365
366 void VM::update_config()
367 {
368         for(DEVICE* device = first_device; device; device = device->next_device) {
369                 device->update_config();
370         }
371 }
372
373 #define STATE_VERSION   1
374
375 void VM::save_state(FILEIO* state_fio)
376 {
377         state_fio->FputUint32(STATE_VERSION);
378         
379         for(DEVICE* device = first_device; device; device = device->next_device) {
380                 device->save_state(state_fio);
381         }
382 }
383
384 bool VM::load_state(FILEIO* state_fio)
385 {
386         if(state_fio->FgetUint32() != STATE_VERSION) {
387                 return false;
388         }
389         for(DEVICE* device = first_device; device; device = device->next_device) {
390                 if(!device->load_state(state_fio)) {
391                         return false;
392                 }
393         }
394         return true;
395 }
396