OSDN Git Service

a2c988dae6543fb33302977ad6b54aa5ca2176bc
[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 "../noise.h"
22 #include "../not.h"
23 #include "../pcm1bit.h"
24 #include "../sn76489an.h"
25 #include "../upd765a.h"
26 #include "../z80.h"
27 #include "../z80ctc.h"
28 #include "../z80pio.h"
29
30 #ifdef USE_DEBUGGER
31 #include "../debugger.h"
32 #endif
33
34 #include "floppy.h"
35 #include "display.h"
36 #include "iobus.h"
37 #include "iotrap.h"
38 #include "keyboard.h"
39 #include "./memory.h"
40 #include "pac2.h"
41
42 // ----------------------------------------------------------------------------
43 // initialize
44 // ----------------------------------------------------------------------------
45
46 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
47 {
48         // create devices
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
52         
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);
79         
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);
87         
88         // set contexts
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());
100         
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);
127         
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);
140         
141         // cpu bus
142         cpu->set_context_mem(memory);
143         cpu->set_context_io(iobus);
144         cpu->set_context_intr(ctc);
145 #ifdef USE_DEBUGGER
146         cpu->set_context_debugger(new DEBUGGER(this, emu));
147 #endif
148         
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);
153         
154         // i/o bus
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);
172         
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);
176 #endif
177         for(DEVICE* device = first_device; device; device = device->next_device) {
178                 device->initialize();
179         }
180         for(DEVICE* device = first_device; device; device = device->next_device) {
181 //              if(device->this_device_id != event->this_device_id) {
182                         device->reset();
183 //              }
184         }
185         for(int i = 0; i < 4; i++) {
186                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
187         }
188 }
189
190 VM::~VM()
191 {
192         // delete all devices
193         for(DEVICE* device = first_device; device;) {
194                 DEVICE *next_device = device->next_device;
195                 device->release();
196                 delete device;
197                 device = next_device;
198         }
199 }
200
201 DEVICE* VM::get_device(int id)
202 {
203         for(DEVICE* device = first_device; device; device = device->next_device) {
204                 if(device->this_device_id == id) {
205                         return device;
206                 }
207         }
208         return NULL;
209 }
210
211 // ----------------------------------------------------------------------------
212 // drive virtual machine
213 // ----------------------------------------------------------------------------
214
215 void VM::reset()
216 {
217         // reset all devices
218 //      for(DEVICE* device = first_device; device; device = device->next_device) {
219 //              device->reset();
220 //      }
221         event->reset();
222         memory->reset();
223         iotrap->do_reset();
224         
225         // set initial port status
226 #ifdef _LCD
227         pio0->write_signal(SIG_I8255_PORT_B, 0, (0x10 | 0x40));
228 #else
229         pio0->write_signal(SIG_I8255_PORT_B, 0x10, (0x10 | 0x40));
230 #endif
231         pcm->write_signal(SIG_PCM1BIT_ON, 0, 1);
232 }
233
234 void VM::run()
235 {
236         event->drive();
237 }
238
239 double VM::get_frame_rate()
240 {
241         return event->get_frame_rate();
242 }
243
244 // ----------------------------------------------------------------------------
245 // debugger
246 // ----------------------------------------------------------------------------
247
248 #ifdef USE_DEBUGGER
249 DEVICE *VM::get_cpu(int index)
250 {
251         if(index == 0) {
252                 return cpu;
253         }
254         return NULL;
255 }
256 #endif
257
258 // ----------------------------------------------------------------------------
259 // draw screen
260 // ----------------------------------------------------------------------------
261
262 void VM::draw_screen()
263 {
264         display->draw_screen();
265 }
266
267 // ----------------------------------------------------------------------------
268 // soud manager
269 // ----------------------------------------------------------------------------
270
271 void VM::initialize_sound(int rate, int samples)
272 {
273         // init sound manager
274         event->initialize_sound(rate, samples);
275         
276         // init sound gen
277         pcm->initialize_sound(rate, 3600);
278         psg0->initialize_sound(rate, 1996800, 3600);
279         psg1->initialize_sound(rate, 1996800, 3600);
280 }
281
282 uint16_t* VM::create_sound(int* extra_frames)
283 {
284         return event->create_sound(extra_frames);
285 }
286
287 int VM::get_sound_buffer_ptr()
288 {
289         return event->get_sound_buffer_ptr();
290 }
291
292 #ifdef USE_SOUND_VOLUME
293 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
294 {
295         if(ch == 0) {
296                 psg0->set_volume(0, decibel_l, decibel_r);
297         } else if(ch == 1) {
298                 psg1->set_volume(0, decibel_l, decibel_r);
299         } else if(ch == 2) {
300                 pcm->set_volume(0, decibel_l, decibel_r);
301         } else if(ch == 3) {
302                 drec->set_volume(0, decibel_l, decibel_r);
303         } else if(ch == 4) {
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);
307         } else if(ch == 5) {
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);
311         }
312 }
313 #endif
314
315 // ----------------------------------------------------------------------------
316 // user interface
317 // ----------------------------------------------------------------------------
318
319 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
320 {
321         fdc->open_disk(drv, file_path, bank);
322 }
323
324 void VM::close_floppy_disk(int drv)
325 {
326         fdc->close_disk(drv);
327 }
328
329 bool VM::is_floppy_disk_inserted(int drv)
330 {
331         return fdc->is_disk_inserted(drv);
332 }
333
334 void VM::is_floppy_disk_protected(int drv, bool value)
335 {
336         fdc->is_disk_protected(drv, value);
337 }
338
339 bool VM::is_floppy_disk_protected(int drv)
340 {
341         return fdc->is_disk_protected(drv);
342 }
343
344 uint32_t VM::is_floppy_disk_accessed()
345 {
346         return fdc->read_signal(0);
347 }
348
349 void VM::play_tape(int drv, const _TCHAR* file_path)
350 {
351         drec->play_tape(file_path);
352 //      drec->set_remote(true);
353 }
354
355 void VM::rec_tape(int drv, const _TCHAR* file_path)
356 {
357         drec->rec_tape(file_path);
358 //      drec->set_remote(true);
359 }
360
361 void VM::close_tape(int drv)
362 {
363         emu->lock_vm();
364         drec->close_tape();
365         emu->unlock_vm();
366 //      drec->set_remote(false);
367 }
368
369 bool VM::is_tape_inserted(int drv)
370 {
371         return drec->is_tape_inserted();
372 }
373
374 bool VM::is_tape_playing(int drv)
375 {
376         return drec->is_tape_playing();
377 }
378
379 bool VM::is_tape_recording(int drv)
380 {
381         return drec->is_tape_recording();
382 }
383
384 int VM::get_tape_position(int drv)
385 {
386         return drec->get_tape_position();
387 }
388
389 const _TCHAR* VM::get_tape_message(int drv)
390 {
391         return drec->get_message();
392 }
393
394 void VM::push_play(int drv)
395 {
396         drec->set_ff_rew(0);
397         drec->set_remote(true);
398 }
399
400 void VM::push_stop(int drv)
401 {
402         drec->set_remote(false);
403 }
404
405 void VM::push_fast_forward(int drv)
406 {
407         drec->set_ff_rew(1);
408         drec->set_remote(true);
409 }
410
411 void VM::push_fast_rewind(int drv)
412 {
413         drec->set_ff_rew(-1);
414         drec->set_remote(true);
415 }
416
417 void VM::load_binary(int drv, const _TCHAR* file_path)
418 {
419         pac2->open_rampac2(drv, file_path);
420 }
421
422 bool VM::is_frame_skippable()
423 {
424         return event->is_frame_skippable();
425 }
426
427 void VM::update_config()
428 {
429         for(DEVICE* device = first_device; device; device = device->next_device) {
430                 device->update_config();
431         }
432 }
433
434 #define STATE_VERSION   3
435
436 bool VM::process_state(FILEIO* state_fio, bool loading)
437 {
438         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
439                 return false;
440         }
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);
447                 
448                 if(!state_fio->StateCheckInt32(len)) {
449                         if(loading) {
450                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
451                         }
452                         return false;
453                 }
454                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
455                         if(loading) {
456                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
457                         }
458                         return false;
459                 }
460                 if(!device->process_state(state_fio, loading)) {
461                         if(loading) {
462                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
463                         }
464                         return false;
465                 }
466         }
467         return true;
468 }