OSDN Git Service

[VM][STATE] Use namespace {VMNAME} to separate per VMs.
[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 using PASOPIA7::DISPLAY;
43 using PASOPIA7::FLOPPY;
44 using PASOPIA7::IOBUS;
45 using PASOPIA7::IOTRAP;
46 using PASOPIA7::KEYBOARD;
47 using PASOPIA7::MEMORY;
48 using PASOPIA7::PAC2;
49
50 // ----------------------------------------------------------------------------
51 // initialize
52 // ----------------------------------------------------------------------------
53
54 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
55 {
56         // create devices
57         first_device = last_device = NULL;
58         dummy = new DEVICE(this, emu);  // must be 1st device
59         event = new EVENT(this, emu);   // must be 2nd device
60         
61         drec = new DATAREC(this, emu);
62         drec->set_context_noise_play(new NOISE(this, emu));
63         drec->set_context_noise_stop(new NOISE(this, emu));
64         drec->set_context_noise_fast(new NOISE(this, emu));
65         crtc = new HD46505(this, emu);
66         pio0 = new I8255(this, emu);
67         pio0->set_device_name(_T("8255 PIO (Display)"));
68         pio1 = new I8255(this, emu);
69         pio1->set_device_name(_T("8255 PIO (Display/Memory)"));
70         pio2 = new I8255(this, emu);
71         pio2->set_device_name(_T("8255 PIO (Sound/Data Recorder)"));
72         io = new IO(this, emu);
73         flipflop = new LS393(this, emu); // LS74
74         not_remote = new NOT(this, emu);
75         pcm = new PCM1BIT(this, emu);
76         psg0 = new SN76489AN(this, emu);
77         psg0->set_device_name(_T("SN76489AN PSG #1"));
78         psg1 = new SN76489AN(this, emu);
79         psg1->set_device_name(_T("SN76489AN PSG #2"));
80         fdc = new UPD765A(this, emu);
81         fdc->set_context_noise_seek(new NOISE(this, emu));
82         fdc->set_context_noise_head_down(new NOISE(this, emu));
83         fdc->set_context_noise_head_up(new NOISE(this, emu));
84         cpu = new Z80(this, emu);
85         ctc = new Z80CTC(this, emu);
86         pio = new Z80PIO(this, emu);
87         
88         floppy = new FLOPPY(this, emu);
89         display = new DISPLAY(this, emu);
90         iobus = new IOBUS(this, emu);
91         iotrap = new IOTRAP(this, emu);
92         key = new KEYBOARD(this, emu);
93         memory = new MEMORY(this, emu);
94         pac2 = new PAC2(this, emu);
95         
96         // set contexts
97         event->set_context_cpu(cpu);
98         event->set_context_sound(pcm);
99         event->set_context_sound(psg0);
100         event->set_context_sound(psg1);
101         event->set_context_sound(drec);
102         event->set_context_sound(fdc->get_context_noise_seek());
103         event->set_context_sound(fdc->get_context_noise_head_down());
104         event->set_context_sound(fdc->get_context_noise_head_up());
105         event->set_context_sound(drec->get_context_noise_play());
106         event->set_context_sound(drec->get_context_noise_stop());
107         event->set_context_sound(drec->get_context_noise_fast());
108         
109         drec->set_context_ear(pio2, SIG_I8255_PORT_B, 0x20);
110         crtc->set_context_disp(pio0, SIG_I8255_PORT_B, 8);
111         crtc->set_context_vsync(pio0, SIG_I8255_PORT_B, 0x20);
112         pio0->set_context_port_a(display, SIG_DISPLAY_I8255_0_A, 0xff, 0);
113         pio1->set_context_port_a(memory, SIG_MEMORY_I8255_1_A, 0xff, 0);
114         pio1->set_context_port_b(display, SIG_DISPLAY_I8255_1_B, 0xff, 0);
115         pio1->set_context_port_b(memory, SIG_MEMORY_I8255_1_B, 0xff, 0);
116         pio1->set_context_port_c(display, SIG_DISPLAY_I8255_1_C, 0xff, 0);
117         pio1->set_context_port_c(memory, SIG_MEMORY_I8255_1_C, 0xff, 0);
118         pio2->set_context_port_a(pcm, SIG_PCM1BIT_MUTE, 0x02, 0);
119         pio2->set_context_port_a(psg0, SIG_SN76489AN_MUTE, 0x02, 0);
120         pio2->set_context_port_a(psg1, SIG_SN76489AN_MUTE, 0x02, 0);
121         pio2->set_context_port_a(drec, SIG_DATAREC_MIC, 0x10, 0);
122         pio2->set_context_port_a(not_remote, SIG_NOT_INPUT, 0x20, 0);
123         pio2->set_context_port_a(iotrap, SIG_IOTRAP_I8255_2_A, 0xff, 0);
124         pio2->set_context_port_c(iotrap, SIG_IOTRAP_I8255_2_C, 0xff, 0);
125         flipflop->set_context_1qa(pcm, SIG_PCM1BIT_SIGNAL, 1);
126         not_remote->set_context_out(drec, SIG_DATAREC_REMOTE, 1);
127         fdc->set_context_irq(floppy, SIG_FLOPPY_INTR, 1);
128         ctc->set_context_zc0(ctc, SIG_Z80CTC_TRIG_1, 1);
129         ctc->set_context_zc1(flipflop, SIG_LS393_CLK, 1);
130         ctc->set_context_zc2(ctc, SIG_Z80CTC_TRIG_3, 1);
131         ctc->set_constant_clock(0, CPU_CLOCKS);
132         ctc->set_constant_clock(2, CPU_CLOCKS);
133         pio->set_context_port_a(pcm, SIG_PCM1BIT_ON, 0x80, 0);
134         pio->set_context_port_a(key, SIG_KEYBOARD_Z80PIO_A, 0xff, 0);
135         
136         display->set_vram_ptr(memory->get_vram());
137         display->set_pal_ptr(memory->get_pal());
138         display->set_regs_ptr(crtc->get_regs());
139         floppy->set_context_fdc(fdc);
140         iobus->set_context_io(io);
141         iobus->set_ram_ptr(memory->get_ram());
142         iotrap->set_context_cpu(cpu);
143         iotrap->set_context_pio2(pio2);
144         key->set_context_pio(pio);
145         memory->set_context_iobus(iobus);
146         memory->set_context_pio0(pio0);
147         memory->set_context_pio2(pio2);
148         
149         // cpu bus
150         cpu->set_context_mem(memory);
151         cpu->set_context_io(iobus);
152         cpu->set_context_intr(ctc);
153 #ifdef USE_DEBUGGER
154         cpu->set_context_debugger(new DEBUGGER(this, emu));
155 #endif
156         
157         // z80 family daisy chain
158         ctc->set_context_intr(cpu, 0);
159         ctc->set_context_child(pio);
160         pio->set_context_intr(cpu, 1);
161         
162         // i/o bus
163         io->set_iomap_range_rw(0x08, 0x0b, pio0);
164         io->set_iomap_range_rw(0x0c, 0x0f, pio1);
165         io->set_iomap_range_rw(0x10, 0x11, crtc);
166         io->set_iomap_range_rw(0x18, 0x1b, pac2);
167         io->set_iomap_range_rw(0x20, 0x23, pio2);
168         io->set_iomap_range_rw(0x28, 0x2b, ctc);
169         io->set_iomap_alias_rw(0x30, pio, 0);
170         io->set_iomap_alias_rw(0x31, pio, 2);
171         io->set_iomap_alias_w(0x32, pio, 1);
172         io->set_iomap_alias_w(0x33, pio, 3);
173         io->set_iomap_single_w(0x3a, psg0);
174         io->set_iomap_single_w(0x3b, psg1);
175         io->set_iomap_single_w(0x3c, memory);
176         io->set_iomap_single_w(0xe0, floppy);
177         io->set_iomap_single_w(0xe2, floppy);
178         io->set_iomap_range_rw(0xe4, 0xe5, fdc);
179         io->set_iomap_single_rw(0xe6, floppy);
180         
181         // initialize and reset all devices except the event manager
182 #if defined(__GIT_REPO_VERSION)
183         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
184 #endif
185         for(DEVICE* device = first_device; device; device = device->next_device) {
186                 device->initialize();
187         }
188         for(DEVICE* device = first_device; device; device = device->next_device) {
189 //              if(device->this_device_id != event->this_device_id) {
190                         device->reset();
191 //              }
192         }
193         for(int i = 0; i < 4; i++) {
194                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
195         }
196 }
197
198 VM::~VM()
199 {
200         // delete all devices
201         for(DEVICE* device = first_device; device;) {
202                 DEVICE *next_device = device->next_device;
203                 device->release();
204                 delete device;
205                 device = next_device;
206         }
207 }
208
209 DEVICE* VM::get_device(int id)
210 {
211         for(DEVICE* device = first_device; device; device = device->next_device) {
212                 if(device->this_device_id == id) {
213                         return device;
214                 }
215         }
216         return NULL;
217 }
218
219 // ----------------------------------------------------------------------------
220 // drive virtual machine
221 // ----------------------------------------------------------------------------
222
223 void VM::reset()
224 {
225         // reset all devices
226 //      for(DEVICE* device = first_device; device; device = device->next_device) {
227 //              device->reset();
228 //      }
229         event->reset();
230         memory->reset();
231         iotrap->do_reset();
232         
233         // set initial port status
234 #ifdef _LCD
235         pio0->write_signal(SIG_I8255_PORT_B, 0, (0x10 | 0x40));
236 #else
237         pio0->write_signal(SIG_I8255_PORT_B, 0x10, (0x10 | 0x40));
238 #endif
239         pcm->write_signal(SIG_PCM1BIT_ON, 0, 1);
240 }
241
242 void VM::run()
243 {
244         event->drive();
245 }
246
247 double VM::get_frame_rate()
248 {
249         return event->get_frame_rate();
250 }
251
252 // ----------------------------------------------------------------------------
253 // debugger
254 // ----------------------------------------------------------------------------
255
256 #ifdef USE_DEBUGGER
257 DEVICE *VM::get_cpu(int index)
258 {
259         if(index == 0) {
260                 return cpu;
261         }
262         return NULL;
263 }
264 #endif
265
266 // ----------------------------------------------------------------------------
267 // draw screen
268 // ----------------------------------------------------------------------------
269
270 void VM::draw_screen()
271 {
272         display->draw_screen();
273 }
274
275 // ----------------------------------------------------------------------------
276 // soud manager
277 // ----------------------------------------------------------------------------
278
279 void VM::initialize_sound(int rate, int samples)
280 {
281         // init sound manager
282         event->initialize_sound(rate, samples);
283         
284         // init sound gen
285         pcm->initialize_sound(rate, 3600);
286         psg0->initialize_sound(rate, 1996800, 3600);
287         psg1->initialize_sound(rate, 1996800, 3600);
288 }
289
290 uint16_t* VM::create_sound(int* extra_frames)
291 {
292         return event->create_sound(extra_frames);
293 }
294
295 int VM::get_sound_buffer_ptr()
296 {
297         return event->get_sound_buffer_ptr();
298 }
299
300 #ifdef USE_SOUND_VOLUME
301 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
302 {
303         if(ch == 0) {
304                 psg0->set_volume(0, decibel_l, decibel_r);
305         } else if(ch == 1) {
306                 psg1->set_volume(0, decibel_l, decibel_r);
307         } else if(ch == 2) {
308                 pcm->set_volume(0, decibel_l, decibel_r);
309         } else if(ch == 3) {
310                 drec->set_volume(0, decibel_l, decibel_r);
311         } else if(ch == 4) {
312                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
313                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
314                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
315         } else if(ch == 5) {
316                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
317                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
318                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
319         }
320 }
321 #endif
322
323 // ----------------------------------------------------------------------------
324 // user interface
325 // ----------------------------------------------------------------------------
326
327 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
328 {
329         fdc->open_disk(drv, file_path, bank);
330 }
331
332 void VM::close_floppy_disk(int drv)
333 {
334         fdc->close_disk(drv);
335 }
336
337 bool VM::is_floppy_disk_inserted(int drv)
338 {
339         return fdc->is_disk_inserted(drv);
340 }
341
342 void VM::is_floppy_disk_protected(int drv, bool value)
343 {
344         fdc->is_disk_protected(drv, value);
345 }
346
347 bool VM::is_floppy_disk_protected(int drv)
348 {
349         return fdc->is_disk_protected(drv);
350 }
351
352 uint32_t VM::is_floppy_disk_accessed()
353 {
354         return fdc->read_signal(0);
355 }
356
357 void VM::play_tape(int drv, const _TCHAR* file_path)
358 {
359         drec->play_tape(file_path);
360 //      drec->set_remote(true);
361 }
362
363 void VM::rec_tape(int drv, const _TCHAR* file_path)
364 {
365         drec->rec_tape(file_path);
366 //      drec->set_remote(true);
367 }
368
369 void VM::close_tape(int drv)
370 {
371         emu->lock_vm();
372         drec->close_tape();
373         emu->unlock_vm();
374 //      drec->set_remote(false);
375 }
376
377 bool VM::is_tape_inserted(int drv)
378 {
379         return drec->is_tape_inserted();
380 }
381
382 bool VM::is_tape_playing(int drv)
383 {
384         return drec->is_tape_playing();
385 }
386
387 bool VM::is_tape_recording(int drv)
388 {
389         return drec->is_tape_recording();
390 }
391
392 int VM::get_tape_position(int drv)
393 {
394         return drec->get_tape_position();
395 }
396
397 const _TCHAR* VM::get_tape_message(int drv)
398 {
399         return drec->get_message();
400 }
401
402 void VM::push_play(int drv)
403 {
404         drec->set_ff_rew(0);
405         drec->set_remote(true);
406 }
407
408 void VM::push_stop(int drv)
409 {
410         drec->set_remote(false);
411 }
412
413 void VM::push_fast_forward(int drv)
414 {
415         drec->set_ff_rew(1);
416         drec->set_remote(true);
417 }
418
419 void VM::push_fast_rewind(int drv)
420 {
421         drec->set_ff_rew(-1);
422         drec->set_remote(true);
423 }
424
425 void VM::load_binary(int drv, const _TCHAR* file_path)
426 {
427         pac2->open_rampac2(drv, file_path);
428 }
429
430 bool VM::is_frame_skippable()
431 {
432         return event->is_frame_skippable();
433 }
434
435 void VM::update_config()
436 {
437         for(DEVICE* device = first_device; device; device = device->next_device) {
438                 device->update_config();
439         }
440 }
441
442 #define STATE_VERSION   3
443
444 bool VM::process_state(FILEIO* state_fio, bool loading)
445 {
446         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
447                 return false;
448         }
449         for(DEVICE* device = first_device; device; device = device->next_device) {
450                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
451                 // const char *name = typeid(*device).name();
452                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
453                 const char *name = device->get_device_name();
454                 int len = strlen(name);
455                 
456                 if(!state_fio->StateCheckInt32(len)) {
457                         if(loading) {
458                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
459                         }
460                         return false;
461                 }
462                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
463                         if(loading) {
464                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
465                         }
466                         return false;
467                 }
468                 if(!device->process_state(state_fio, loading)) {
469                         if(loading) {
470                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
471                         }
472                         return false;
473                 }
474         }
475         return true;
476 }