OSDN Git Service

461c80ea0920c6a5ec2d239706419a11a7cc2719
[csp-qt/common_source_project-fm7.git] / source / src / vm / fp200 / fp200.cpp
1 /*
2         CASIO FP-200 Emulator 'eFP-200'
3
4         Author : Takeda.Toshiya
5         Date   : 2013.03.21-
6
7         [ virtual machine ]
8 */
9
10 #include "fp200.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../i8080.h"
17 #include "../memory.h"
18 #include "../noise.h"
19 #include "../rp5c01.h"
20
21 #ifdef USE_DEBUGGER
22 #include "../debugger.h"
23 #endif
24
25 #include "io.h"
26
27 // ----------------------------------------------------------------------------
28 // initialize
29 // ----------------------------------------------------------------------------
30
31 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
32 {
33         // create devices
34         first_device = last_device = NULL;
35         dummy = new DEVICE(this, emu);  // must be 1st device
36         event = new EVENT(this, emu);   // must be 2nd device
37         dummy->set_device_name(_T("1st Dummy"));
38
39         drec = new DATAREC(this, emu);
40         drec->set_context_noise_play(new NOISE(this, emu));
41         drec->set_context_noise_stop(new NOISE(this, emu));
42         drec->set_context_noise_fast(new NOISE(this, emu));
43         cpu = new I8080(this, emu);     // i8085
44         memory = new MEMORY(this, emu);
45         rtc = new RP5C01(this, emu);
46         
47         io = new IO(this, emu);
48         // set contexts
49         event->set_context_cpu(cpu);
50         event->set_context_sound(drec);
51         event->set_context_sound(drec->get_context_noise_play());
52         event->set_context_sound(drec->get_context_noise_stop());
53         event->set_context_sound(drec->get_context_noise_fast());
54         
55         drec->set_context_ear(io, SIG_IO_CMT, 1);
56         cpu->set_context_sod(io, SIG_IO_SOD, 1);
57         
58         io->set_context_cpu(cpu);
59         io->set_context_drec(drec);
60         io->set_context_rtc(rtc);
61         
62         // cpu bus
63         cpu->set_context_mem(memory);
64         cpu->set_context_io(io);
65         cpu->set_context_intr(io);
66 #ifdef USE_DEBUGGER
67         cpu->set_context_debugger(new DEBUGGER(this, emu));
68 #endif
69         
70         // memory bus
71         memset(rom, 0xff, sizeof(rom));
72         memset(ram, 0, sizeof(ram));
73         
74         memory->read_bios(_T("BIOS.ROM"), rom, sizeof(rom));
75         
76         FILEIO* fio = new FILEIO();
77         if(fio->Fopen(create_local_path(_T("RAM.BIN")), FILEIO_READ_BINARY)) {
78                 fio->Fread(ram, sizeof(ram), 1);
79                 fio->Fclose();
80         }
81         delete fio;
82         
83         memory->set_memory_r(0x0000, 0x7fff, rom);
84         memory->set_memory_rw(0x8000, 0xffff, ram);
85         memory->set_wait_rw(0x0000, 0xffff, 1);
86         
87         // initialize all devices
88         for(DEVICE* device = first_device; device; device = device->next_device) {
89                 device->initialize();
90         }
91         decl_state();
92 }
93
94 VM::~VM()
95 {
96         FILEIO* fio = new FILEIO();
97         if(fio->Fopen(create_local_path(_T("RAM.BIN")), FILEIO_WRITE_BINARY)) {
98                 fio->Fwrite(ram, sizeof(ram), 1);
99                 fio->Fclose();
100         }
101         delete fio;
102         
103         // delete all devices
104         for(DEVICE* device = first_device; device;) {
105                 DEVICE *next_device = device->next_device;
106                 device->release();
107                 delete device;
108                 device = next_device;
109         }
110 }
111
112 DEVICE* VM::get_device(int id)
113 {
114         for(DEVICE* device = first_device; device; device = device->next_device) {
115                 if(device->this_device_id == id) {
116                         return device;
117                 }
118         }
119         return NULL;
120 }
121
122 // ----------------------------------------------------------------------------
123 // drive virtual machine
124 // ----------------------------------------------------------------------------
125
126 void VM::reset()
127 {
128         // reset all devices
129         for(DEVICE* device = first_device; device; device = device->next_device) {
130                 device->reset();
131         }
132 }
133
134 void VM::run()
135 {
136         event->drive();
137 }
138
139 // ----------------------------------------------------------------------------
140 // debugger
141 // ----------------------------------------------------------------------------
142
143 #ifdef USE_DEBUGGER
144 DEVICE *VM::get_cpu(int index)
145 {
146         if(index == 0) {
147                 return cpu;
148         }
149         return NULL;
150 }
151 #endif
152
153 // ----------------------------------------------------------------------------
154 // draw screen
155 // ----------------------------------------------------------------------------
156
157 void VM::draw_screen()
158 {
159         io->draw_screen();
160 }
161
162 // ----------------------------------------------------------------------------
163 // soud manager
164 // ----------------------------------------------------------------------------
165
166 void VM::initialize_sound(int rate, int samples)
167 {
168         // init sound manager
169         event->initialize_sound(rate, samples);
170 }
171
172 uint16_t* VM::create_sound(int* extra_frames)
173 {
174         return event->create_sound(extra_frames);
175 }
176
177 int VM::get_sound_buffer_ptr()
178 {
179         return event->get_sound_buffer_ptr();
180 }
181
182 #ifdef USE_SOUND_VOLUME
183 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
184 {
185         if(ch == 0) {
186                 drec->set_volume(0, decibel_l, decibel_r);
187         } else if(ch == 1) {
188                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
189                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
190                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
191         }
192 }
193 #endif
194
195 // ----------------------------------------------------------------------------
196 // notify key
197 // ----------------------------------------------------------------------------
198
199 void VM::key_down(int code, bool repeat)
200 {
201         if(!repeat) {
202                 io->key_down(code);
203         }
204 }
205
206 void VM::key_up(int code)
207 {
208         io->key_up();
209 }
210
211 // ----------------------------------------------------------------------------
212 // user interface
213 // ----------------------------------------------------------------------------
214
215 void VM::play_tape(int drv, const _TCHAR* file_path)
216 {
217         io->close_tape();
218         drec->play_tape(file_path);
219 //      drec->set_remote(true);
220 }
221
222 void VM::rec_tape(int drv, const _TCHAR* file_path)
223 {
224         emu->lock_vm();
225         drec->close_tape();
226         emu->unlock_vm();
227 //      drec->set_remote(false);
228         io->rec_tape(file_path);
229 }
230
231 void VM::close_tape(int drv)
232 {
233         emu->lock_vm();
234         drec->close_tape();
235         emu->unlock_vm();
236 //      drec->set_remote(false);
237         io->close_tape();
238 }
239
240 bool VM::is_tape_inserted(int drv)
241 {
242         return drec->is_tape_inserted() || io->is_tape_inserted();
243 }
244
245 bool VM::is_tape_playing(int drv)
246 {
247         if(drec->is_tape_inserted()) {
248                 return drec->is_tape_playing();
249         } else {
250                 return io->is_tape_playing();
251         }
252 }
253
254 bool VM::is_tape_recording(int drv)
255 {
256         if(drec->is_tape_inserted()) {
257                 return drec->is_tape_recording();
258         } else {
259                 return io->is_tape_recording();
260         }
261 }
262
263 int VM::get_tape_position(int drv)
264 {
265         if(drec->is_tape_inserted()) {
266                 return drec->get_tape_position();
267         } else {
268                 return io->get_tape_position();
269         }
270 }
271
272 const _TCHAR* VM::get_tape_message(int drv)
273 {
274         if(drec->is_tape_inserted()) {
275                 return drec->get_message();
276         } else {
277                 return NULL;
278         }
279 }
280
281 void VM::push_play(int drv)
282 {
283         if(drec->is_tape_inserted()) {
284                 drec->set_ff_rew(0);
285                 drec->set_remote(true);
286         }
287 }
288
289 void VM::push_stop(int drv)
290 {
291         if(drec->is_tape_inserted()) {
292                 drec->set_remote(false);
293         }
294 }
295
296 void VM::push_fast_forward(int drv)
297 {
298         if(drec->is_tape_inserted()) {
299                 drec->set_ff_rew(1);
300                 drec->set_remote(true);
301         }
302 }
303
304 void VM::push_fast_rewind(int drv)
305 {
306         if(drec->is_tape_inserted()) {
307                 drec->set_ff_rew(-1);
308                 drec->set_remote(true);
309         }
310 }
311
312 bool VM::is_frame_skippable()
313 {
314         return event->is_frame_skippable();
315 }
316
317 void VM::update_config()
318 {
319         for(DEVICE* device = first_device; device; device = device->next_device) {
320                 device->update_config();
321         }
322 }
323
324 #define STATE_VERSION   3
325
326 #include "../../statesub.h"
327 #include "../../qt/gui/csp_logger.h"
328 extern CSP_Logger DLL_PREFIX_I *csp_logger;
329
330 void VM::decl_state(void)
331 {
332         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FP_200_HEAD")), csp_logger);
333         DECL_STATE_ENTRY_MULTI(void, ram, sizeof(ram));
334         for(DEVICE* device = first_device; device; device = device->next_device) {
335                 device->decl_state();
336         }
337 }
338 void VM::save_state(FILEIO* state_fio)
339 {
340         //state_fio->FputUint32(STATE_VERSION);
341         
342         if(state_entry != NULL) {
343                 state_entry->save_state(state_fio);
344         }
345         for(DEVICE* device = first_device; device; device = device->next_device) {
346                 device->save_state(state_fio);
347         }
348         //state_fio->Fwrite(ram, sizeof(ram), 1);
349 }
350
351 bool VM::load_state(FILEIO* state_fio)
352 {
353         //if(state_fio->FgetUint32() != STATE_VERSION) {
354         //      return false;
355         //}
356         bool mb = false;
357         if(state_entry != NULL) {
358                 mb = state_entry->load_state(state_fio);
359         }
360         if(!mb) {
361                 emu->out_debug_log("INFO: HEADER DATA ERROR");
362                 return false;
363         }
364         for(DEVICE* device = first_device; device; device = device->next_device) {
365                 if(!device->load_state(state_fio)) {
366                         return false;
367                 }
368         }
369         //state_fio->Fread(ram, sizeof(ram), 1);
370         return true;
371 }
372