OSDN Git Service

[VM][I286] Save cpustate without StateBuffer().
[csp-qt/common_source_project-fm7.git] / source / src / vm / fp1100 / fp1100.cpp
1 /*
2         CASIO FP-1100 Emulator 'eFP-1100'
3
4         Author : Takeda.Toshiya
5         Date   : 2010.06.18-
6
7         [ virtual machine ]
8 */
9
10 #include "fp1100.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../beep.h"
16 #include "../datarec.h"
17 #include "../disk.h"
18 #include "../hd46505.h"
19 #include "../noise.h"
20 #include "../upd765a.h"
21 #include "../upd7801.h"
22 #include "../z80.h"
23
24 #ifdef USE_DEBUGGER
25 #include "../debugger.h"
26 #endif
27
28 #include "./main.h"
29 #include "./sub.h"
30 #include "fdcpack.h"
31 #include "rampack.h"
32 #include "rompack.h"
33
34 // ----------------------------------------------------------------------------
35 // initialize
36 // ----------------------------------------------------------------------------
37
38 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
39 {
40         // create devices
41         first_device = last_device = NULL;
42         dummy = new DEVICE(this, emu);  // must be 1st device
43         event = new EVENT(this, emu);   // must be 2nd device
44         dummy->set_device_name(_T("1st Dummy"));
45    
46         beep = new BEEP(this, emu);
47         drec = new DATAREC(this, emu);
48         drec->set_context_noise_play(new NOISE(this, emu));
49         drec->set_context_noise_stop(new NOISE(this, emu));
50         drec->set_context_noise_fast(new NOISE(this, emu));
51         crtc = new HD46505(this, emu);
52         fdc = new UPD765A(this, emu);
53         fdc->set_context_noise_seek(new NOISE(this, emu));
54         fdc->set_context_noise_head_down(new NOISE(this, emu));
55         fdc->set_context_noise_head_up(new NOISE(this, emu));
56         subcpu = new UPD7801(this, emu);
57         maincpu = new Z80(this, emu);
58         subcpu->set_device_name(_T("SUB CPU (uPD7801)"));
59         maincpu->set_device_name(_T("MAIN CPU (Z80)"));
60         
61         mainbus = new MAIN(this, emu);
62         subbus = new SUB(this, emu);
63         fdcpack = new FDCPACK(this, emu);
64         rampack1 = new RAMPACK(this, emu);
65         rampack1->index = 1;
66         rampack1->set_device_name(_T("RAM Pack #1"));
67         rampack2 = new RAMPACK(this, emu);
68         rampack2->index = 2;
69         rampack2->set_device_name(_T("RAM Pack #2"));
70         rampack3 = new RAMPACK(this, emu);
71         rampack3->index = 3;
72         rampack3->set_device_name(_T("RAM Pack #3"));
73         rampack4 = new RAMPACK(this, emu);
74         rampack4->index = 4;
75         rampack4->set_device_name(_T("RAM Pack #4"));
76         rampack5 = new RAMPACK(this, emu);
77         rampack5->index = 5;
78         rampack5->set_device_name(_T("RAM Pack #5"));
79         rampack6 = new RAMPACK(this, emu);
80         rampack6->index = 6;
81         rampack6->set_device_name(_T("RAM Pack #6"));
82         rompack = new ROMPACK(this, emu);
83         // set contexts
84         event->set_context_cpu(maincpu);
85         event->set_context_cpu(subcpu, SUB_CPU_CLOCKS);
86         event->set_context_sound(beep);
87         event->set_context_sound(drec);
88         event->set_context_sound(fdc->get_context_noise_seek());
89         event->set_context_sound(fdc->get_context_noise_head_down());
90         event->set_context_sound(fdc->get_context_noise_head_up());
91         event->set_context_sound(drec->get_context_noise_play());
92         event->set_context_sound(drec->get_context_noise_stop());
93         event->set_context_sound(drec->get_context_noise_fast());
94         drec->set_context_ear(subbus, SIG_SUB_EAR, 1);
95         crtc->set_context_hsync(subbus, SIG_SUB_HSYNC, 1);
96         fdc->set_context_drq(mainbus, SIG_MAIN_INTA, 1);
97         fdc->set_context_irq(mainbus, SIG_MAIN_INTB, 1);
98         subcpu->set_context_so(subbus, SIG_SUB_SO, 1);
99         
100         mainbus->set_context_cpu(maincpu);
101         mainbus->set_context_sub(subbus);
102         mainbus->set_context_slot(0, fdcpack);
103         mainbus->set_context_slot(1, rampack1);
104         mainbus->set_context_slot(2, rampack2);
105         mainbus->set_context_slot(3, rampack3);
106         mainbus->set_context_slot(4, rampack4);
107         mainbus->set_context_slot(5, rampack5);
108         mainbus->set_context_slot(6, rampack6);
109         mainbus->set_context_slot(7, rompack);
110         
111         subbus->set_context_cpu(subcpu);
112         subbus->set_context_main(mainbus);
113         subbus->set_context_beep(beep);
114         subbus->set_context_drec(drec);
115         subbus->set_context_crtc(crtc, crtc->get_regs());
116         
117         fdcpack->set_context_fdc(fdc);
118         
119         // cpu bus
120         maincpu->set_context_mem(mainbus);
121         maincpu->set_context_io(mainbus);
122         maincpu->set_context_intr(mainbus);
123 #ifdef USE_DEBUGGER
124         maincpu->set_context_debugger(new DEBUGGER(this, emu));
125 #endif
126         subcpu->set_context_mem(subbus);
127         subcpu->set_context_io(subbus);
128 #ifdef USE_DEBUGGER
129         subcpu->set_context_debugger(new DEBUGGER(this, emu));
130 #endif
131         
132         // initialize all devices
133 #if defined(__GIT_REPO_VERSION)
134         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
135 #endif
136         for(DEVICE* device = first_device; device; device = device->next_device) {
137                 device->initialize();
138         }
139         for(int i = 0; i < 4; i++) {
140                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
141         }
142 }
143
144 VM::~VM()
145 {
146         // delete all devices
147         for(DEVICE* device = first_device; device;) {
148                 DEVICE *next_device = device->next_device;
149                 device->release();
150                 delete device;
151                 device = next_device;
152         }
153 }
154
155 DEVICE* VM::get_device(int id)
156 {
157         for(DEVICE* device = first_device; device; device = device->next_device) {
158                 if(device->this_device_id == id) {
159                         return device;
160                 }
161         }
162         return NULL;
163 }
164
165 // ----------------------------------------------------------------------------
166 // drive virtual machine
167 // ----------------------------------------------------------------------------
168
169 void VM::reset()
170 {
171         // reset all devices
172         for(DEVICE* device = first_device; device; device = device->next_device) {
173                 device->reset();
174         }
175 }
176
177 void VM::run()
178 {
179         event->drive();
180 }
181
182 double VM::get_frame_rate()
183 {
184         return event->get_frame_rate();
185 }
186
187 // ----------------------------------------------------------------------------
188 // debugger
189 // ----------------------------------------------------------------------------
190
191 #ifdef USE_DEBUGGER
192 DEVICE *VM::get_cpu(int index)
193 {
194         if(index == 0) {
195                 return maincpu;
196         } else if(index == 1) {
197                 return subcpu;
198         }
199         return NULL;
200 }
201 #endif
202
203 // ----------------------------------------------------------------------------
204 // draw screen
205 // ----------------------------------------------------------------------------
206
207 void VM::draw_screen()
208 {
209         subbus->draw_screen();
210 }
211
212 // ----------------------------------------------------------------------------
213 // soud manager
214 // ----------------------------------------------------------------------------
215
216 void VM::initialize_sound(int rate, int samples)
217 {
218         // init sound manager
219         event->initialize_sound(rate, samples);
220         
221         // init sound gen
222         beep->initialize_sound(rate, 2400, 8000);
223 }
224
225 uint16_t* VM::create_sound(int* extra_frames)
226 {
227         return event->create_sound(extra_frames);
228 }
229
230 int VM::get_sound_buffer_ptr()
231 {
232         return event->get_sound_buffer_ptr();
233 }
234
235 #ifdef USE_SOUND_VOLUME
236 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
237 {
238         if(ch == 0) {
239                 beep->set_volume(0, decibel_l, decibel_r);
240         } else if(ch == 1) {
241                 drec->set_volume(0, decibel_l, decibel_r);
242         } else if(ch == 2) {
243                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
244                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
245                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
246         } else if(ch == 3) {
247                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
248                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
249                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
250         }
251 }
252 #endif
253
254 // ----------------------------------------------------------------------------
255 // notify key
256 // ----------------------------------------------------------------------------
257
258 void VM::key_down(int code, bool repeat)
259 {
260         subbus->key_down(code);
261 }
262
263 void VM::key_up(int code)
264 {
265         subbus->key_up(code);
266 }
267
268 // ----------------------------------------------------------------------------
269 // user interface
270 // ----------------------------------------------------------------------------
271
272 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
273 {
274         fdc->open_disk(drv, file_path, bank);
275 }
276
277 void VM::close_floppy_disk(int drv)
278 {
279         fdc->close_disk(drv);
280 }
281
282 bool VM::is_floppy_disk_inserted(int drv)
283 {
284         return fdc->is_disk_inserted(drv);
285 }
286
287 void VM::is_floppy_disk_protected(int drv, bool value)
288 {
289         fdc->is_disk_protected(drv, value);
290 }
291
292 bool VM::is_floppy_disk_protected(int drv)
293 {
294         return fdc->is_disk_protected(drv);
295 }
296
297 uint32_t VM::is_floppy_disk_accessed()
298 {
299         return fdc->read_signal(0);
300 }
301
302 void VM::play_tape(int drv, const _TCHAR* file_path)
303 {
304         drec->play_tape(file_path);
305 //      drec->set_remote(true);
306 }
307
308 void VM::rec_tape(int drv, const _TCHAR* file_path)
309 {
310         drec->rec_tape(file_path);
311 //      drec->set_remote(true);
312 }
313
314 void VM::close_tape(int drv)
315 {
316         emu->lock_vm();
317         drec->close_tape();
318         emu->unlock_vm();
319 //      drec->set_remote(false);
320 }
321
322 bool VM::is_tape_inserted(int drv)
323 {
324         return drec->is_tape_inserted();
325 }
326
327 bool VM::is_tape_playing(int drv)
328 {
329         return drec->is_tape_playing();
330 }
331
332 bool VM::is_tape_recording(int drv)
333 {
334         return drec->is_tape_recording();
335 }
336
337 int VM::get_tape_position(int drv)
338 {
339         return drec->get_tape_position();
340 }
341
342 const _TCHAR* VM::get_tape_message(int drv)
343 {
344         return drec->get_message();
345 }
346
347 void VM::push_play(int drv)
348 {
349         drec->set_ff_rew(0);
350         drec->set_remote(true);
351 }
352
353 void VM::push_stop(int drv)
354 {
355         drec->set_remote(false);
356 }
357
358 void VM::push_fast_forward(int drv)
359 {
360         drec->set_ff_rew(1);
361         drec->set_remote(true);
362 }
363
364 void VM::push_fast_rewind(int drv)
365 {
366         drec->set_ff_rew(-1);
367         drec->set_remote(true);
368 }
369
370 bool VM::is_frame_skippable()
371 {
372         return event->is_frame_skippable();
373 }
374
375 void VM::update_config()
376 {
377         for(DEVICE* device = first_device; device; device = device->next_device) {
378                 device->update_config();
379         }
380 }
381
382 #define STATE_VERSION   4
383
384 bool VM::process_state(FILEIO* state_fio, bool loading)
385 {
386         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
387                 return false;
388         }
389         for(DEVICE* device = first_device; device; device = device->next_device) {
390                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
391                 // const char *name = typeid(*device).name();
392                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
393                 const char *name = device->get_device_name();
394                 int len = strlen(name);
395                 
396                 if(!state_fio->StateCheckInt32(len)) {
397                         if(loading) {
398                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
399                         }
400                         return false;
401                 }
402                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
403                         if(loading) {
404                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
405                         }
406                         return false;
407                 }
408                 if(!device->process_state(state_fio, loading)) {
409                         if(loading) {
410                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
411                         }
412                         return false;
413                 }
414         }
415         return true;
416 }