OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[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 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         decl_state();
181         for(DEVICE* device = first_device; device; device = device->next_device) {
182 //              if(device->this_device_id != event->this_device_id) {
183                         device->reset();
184 //              }
185         }
186         for(int i = 0; i < 4; i++) {
187                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
188         }
189 }
190
191 VM::~VM()
192 {
193         // delete all devices
194         for(DEVICE* device = first_device; device;) {
195                 DEVICE *next_device = device->next_device;
196                 device->release();
197                 delete device;
198                 device = next_device;
199         }
200 }
201
202 DEVICE* VM::get_device(int id)
203 {
204         for(DEVICE* device = first_device; device; device = device->next_device) {
205                 if(device->this_device_id == id) {
206                         return device;
207                 }
208         }
209         return NULL;
210 }
211
212 // ----------------------------------------------------------------------------
213 // drive virtual machine
214 // ----------------------------------------------------------------------------
215
216 void VM::reset()
217 {
218         // reset all devices
219 //      for(DEVICE* device = first_device; device; device = device->next_device) {
220 //              device->reset();
221 //      }
222         event->reset();
223         memory->reset();
224         iotrap->do_reset();
225         
226         // set initial port status
227 #ifdef _LCD
228         pio0->write_signal(SIG_I8255_PORT_B, 0, (0x10 | 0x40));
229 #else
230         pio0->write_signal(SIG_I8255_PORT_B, 0x10, (0x10 | 0x40));
231 #endif
232         pcm->write_signal(SIG_PCM1BIT_ON, 0, 1);
233 }
234
235 void VM::run()
236 {
237         event->drive();
238 }
239
240 double VM::get_frame_rate()
241 {
242         return event->get_frame_rate();
243 }
244
245 // ----------------------------------------------------------------------------
246 // debugger
247 // ----------------------------------------------------------------------------
248
249 #ifdef USE_DEBUGGER
250 DEVICE *VM::get_cpu(int index)
251 {
252         if(index == 0) {
253                 return cpu;
254         }
255         return NULL;
256 }
257 #endif
258
259 // ----------------------------------------------------------------------------
260 // draw screen
261 // ----------------------------------------------------------------------------
262
263 void VM::draw_screen()
264 {
265         display->draw_screen();
266 }
267
268 // ----------------------------------------------------------------------------
269 // soud manager
270 // ----------------------------------------------------------------------------
271
272 void VM::initialize_sound(int rate, int samples)
273 {
274         // init sound manager
275         event->initialize_sound(rate, samples);
276         
277         // init sound gen
278         pcm->initialize_sound(rate, 3600);
279         psg0->initialize_sound(rate, 1996800, 3600);
280         psg1->initialize_sound(rate, 1996800, 3600);
281 }
282
283 uint16_t* VM::create_sound(int* extra_frames)
284 {
285         return event->create_sound(extra_frames);
286 }
287
288 int VM::get_sound_buffer_ptr()
289 {
290         return event->get_sound_buffer_ptr();
291 }
292
293 #ifdef USE_SOUND_VOLUME
294 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
295 {
296         if(ch == 0) {
297                 psg0->set_volume(0, decibel_l, decibel_r);
298         } else if(ch == 1) {
299                 psg1->set_volume(0, decibel_l, decibel_r);
300         } else if(ch == 2) {
301                 pcm->set_volume(0, decibel_l, decibel_r);
302         } else if(ch == 3) {
303                 drec->set_volume(0, decibel_l, decibel_r);
304         } else if(ch == 4) {
305                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
306                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
307                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
308         } else if(ch == 5) {
309                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
310                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
311                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
312         }
313 }
314 #endif
315
316 // ----------------------------------------------------------------------------
317 // user interface
318 // ----------------------------------------------------------------------------
319
320 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
321 {
322         fdc->open_disk(drv, file_path, bank);
323 }
324
325 void VM::close_floppy_disk(int drv)
326 {
327         fdc->close_disk(drv);
328 }
329
330 bool VM::is_floppy_disk_inserted(int drv)
331 {
332         return fdc->is_disk_inserted(drv);
333 }
334
335 void VM::is_floppy_disk_protected(int drv, bool value)
336 {
337         fdc->is_disk_protected(drv, value);
338 }
339
340 bool VM::is_floppy_disk_protected(int drv)
341 {
342         return fdc->is_disk_protected(drv);
343 }
344
345 uint32_t VM::is_floppy_disk_accessed()
346 {
347         return fdc->read_signal(0);
348 }
349
350 void VM::play_tape(int drv, const _TCHAR* file_path)
351 {
352         drec->play_tape(file_path);
353 //      drec->set_remote(true);
354 }
355
356 void VM::rec_tape(int drv, const _TCHAR* file_path)
357 {
358         drec->rec_tape(file_path);
359 //      drec->set_remote(true);
360 }
361
362 void VM::close_tape(int drv)
363 {
364         emu->lock_vm();
365         drec->close_tape();
366         emu->unlock_vm();
367 //      drec->set_remote(false);
368 }
369
370 bool VM::is_tape_inserted(int drv)
371 {
372         return drec->is_tape_inserted();
373 }
374
375 bool VM::is_tape_playing(int drv)
376 {
377         return drec->is_tape_playing();
378 }
379
380 bool VM::is_tape_recording(int drv)
381 {
382         return drec->is_tape_recording();
383 }
384
385 int VM::get_tape_position(int drv)
386 {
387         return drec->get_tape_position();
388 }
389
390 const _TCHAR* VM::get_tape_message(int drv)
391 {
392         return drec->get_message();
393 }
394
395 void VM::push_play(int drv)
396 {
397         drec->set_ff_rew(0);
398         drec->set_remote(true);
399 }
400
401 void VM::push_stop(int drv)
402 {
403         drec->set_remote(false);
404 }
405
406 void VM::push_fast_forward(int drv)
407 {
408         drec->set_ff_rew(1);
409         drec->set_remote(true);
410 }
411
412 void VM::push_fast_rewind(int drv)
413 {
414         drec->set_ff_rew(-1);
415         drec->set_remote(true);
416 }
417
418 void VM::load_binary(int drv, const _TCHAR* file_path)
419 {
420         pac2->open_rampac2(drv, file_path);
421 }
422
423 bool VM::is_frame_skippable()
424 {
425         return event->is_frame_skippable();
426 }
427
428 void VM::update_config()
429 {
430         for(DEVICE* device = first_device; device; device = device->next_device) {
431                 device->update_config();
432         }
433 }
434
435 #define STATE_VERSION   3
436
437 #include "../../statesub.h"
438 #include "../../qt/gui/csp_logger.h"
439 extern CSP_Logger DLL_PREFIX_I *csp_logger;
440
441 void VM::decl_state(void)
442 {
443 #if defined(_LCD)
444         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PASOPIA7_WITH_LCD_HEAD")), csp_logger);
445 #else
446         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PASOPIA7_HEAD")), csp_logger);
447 #endif  
448         for(DEVICE* device = first_device; device; device = device->next_device) {
449                 device->decl_state();
450         }
451 }
452 void VM::save_state(FILEIO* state_fio)
453 {
454         //state_fio->FputUint32(STATE_VERSION);
455         
456         if(state_entry != NULL) {
457                 state_entry->save_state(state_fio);
458         }
459         for(DEVICE* device = first_device; device; device = device->next_device) {
460                 device->save_state(state_fio);
461         }
462 }
463
464 bool VM::load_state(FILEIO* state_fio)
465 {
466         //if(state_fio->FgetUint32() != STATE_VERSION) {
467         //      return false;
468         //}
469         bool mb = false;
470         if(state_entry != NULL) {
471                 mb = state_entry->load_state(state_fio);
472         }
473         if(!mb) {
474                 emu->out_debug_log("INFO: HEADER DATA ERROR");
475                 return false;
476         }
477         for(DEVICE* device = first_device; device; device = device->next_device) {
478                 if(!device->load_state(state_fio)) {
479                         return false;
480                 }
481         }
482         return true;
483 }
484