OSDN Git Service

7c62c22b122a309421de1c4722827a461d043eea
[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 #if defined(__GIT_REPO_VERSION)
89         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
90 #endif
91         for(DEVICE* device = first_device; device; device = device->next_device) {
92                 device->initialize();
93         }
94         decl_state();
95 }
96
97 VM::~VM()
98 {
99         FILEIO* fio = new FILEIO();
100         if(fio->Fopen(create_local_path(_T("RAM.BIN")), FILEIO_WRITE_BINARY)) {
101                 fio->Fwrite(ram, sizeof(ram), 1);
102                 fio->Fclose();
103         }
104         delete fio;
105         
106         // delete all devices
107         for(DEVICE* device = first_device; device;) {
108                 DEVICE *next_device = device->next_device;
109                 device->release();
110                 delete device;
111                 device = next_device;
112         }
113 }
114
115 DEVICE* VM::get_device(int id)
116 {
117         for(DEVICE* device = first_device; device; device = device->next_device) {
118                 if(device->this_device_id == id) {
119                         return device;
120                 }
121         }
122         return NULL;
123 }
124
125 // ----------------------------------------------------------------------------
126 // drive virtual machine
127 // ----------------------------------------------------------------------------
128
129 void VM::reset()
130 {
131         // reset all devices
132         for(DEVICE* device = first_device; device; device = device->next_device) {
133                 device->reset();
134         }
135 }
136
137 void VM::run()
138 {
139         event->drive();
140 }
141
142 // ----------------------------------------------------------------------------
143 // debugger
144 // ----------------------------------------------------------------------------
145
146 #ifdef USE_DEBUGGER
147 DEVICE *VM::get_cpu(int index)
148 {
149         if(index == 0) {
150                 return cpu;
151         }
152         return NULL;
153 }
154 #endif
155
156 // ----------------------------------------------------------------------------
157 // draw screen
158 // ----------------------------------------------------------------------------
159
160 void VM::draw_screen()
161 {
162         io->draw_screen();
163 }
164
165 // ----------------------------------------------------------------------------
166 // soud manager
167 // ----------------------------------------------------------------------------
168
169 void VM::initialize_sound(int rate, int samples)
170 {
171         // init sound manager
172         event->initialize_sound(rate, samples);
173 }
174
175 uint16_t* VM::create_sound(int* extra_frames)
176 {
177         return event->create_sound(extra_frames);
178 }
179
180 int VM::get_sound_buffer_ptr()
181 {
182         return event->get_sound_buffer_ptr();
183 }
184
185 #ifdef USE_SOUND_VOLUME
186 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
187 {
188         if(ch == 0) {
189                 drec->set_volume(0, decibel_l, decibel_r);
190         } else if(ch == 1) {
191                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
192                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
193                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
194         }
195 }
196 #endif
197
198 // ----------------------------------------------------------------------------
199 // notify key
200 // ----------------------------------------------------------------------------
201
202 void VM::key_down(int code, bool repeat)
203 {
204         if(!repeat) {
205                 io->key_down(code);
206         }
207 }
208
209 void VM::key_up(int code)
210 {
211         io->key_up();
212 }
213
214 // ----------------------------------------------------------------------------
215 // user interface
216 // ----------------------------------------------------------------------------
217
218 void VM::play_tape(int drv, const _TCHAR* file_path)
219 {
220         io->close_tape();
221         drec->play_tape(file_path);
222 //      drec->set_remote(true);
223 }
224
225 void VM::rec_tape(int drv, const _TCHAR* file_path)
226 {
227         emu->lock_vm();
228         drec->close_tape();
229         emu->unlock_vm();
230 //      drec->set_remote(false);
231         io->rec_tape(file_path);
232 }
233
234 void VM::close_tape(int drv)
235 {
236         emu->lock_vm();
237         drec->close_tape();
238         emu->unlock_vm();
239 //      drec->set_remote(false);
240         io->close_tape();
241 }
242
243 bool VM::is_tape_inserted(int drv)
244 {
245         return drec->is_tape_inserted() || io->is_tape_inserted();
246 }
247
248 bool VM::is_tape_playing(int drv)
249 {
250         if(drec->is_tape_inserted()) {
251                 return drec->is_tape_playing();
252         } else {
253                 return io->is_tape_playing();
254         }
255 }
256
257 bool VM::is_tape_recording(int drv)
258 {
259         if(drec->is_tape_inserted()) {
260                 return drec->is_tape_recording();
261         } else {
262                 return io->is_tape_recording();
263         }
264 }
265
266 int VM::get_tape_position(int drv)
267 {
268         if(drec->is_tape_inserted()) {
269                 return drec->get_tape_position();
270         } else {
271                 return io->get_tape_position();
272         }
273 }
274
275 const _TCHAR* VM::get_tape_message(int drv)
276 {
277         if(drec->is_tape_inserted()) {
278                 return drec->get_message();
279         } else {
280                 return NULL;
281         }
282 }
283
284 void VM::push_play(int drv)
285 {
286         if(drec->is_tape_inserted()) {
287                 drec->set_ff_rew(0);
288                 drec->set_remote(true);
289         }
290 }
291
292 void VM::push_stop(int drv)
293 {
294         if(drec->is_tape_inserted()) {
295                 drec->set_remote(false);
296         }
297 }
298
299 void VM::push_fast_forward(int drv)
300 {
301         if(drec->is_tape_inserted()) {
302                 drec->set_ff_rew(1);
303                 drec->set_remote(true);
304         }
305 }
306
307 void VM::push_fast_rewind(int drv)
308 {
309         if(drec->is_tape_inserted()) {
310                 drec->set_ff_rew(-1);
311                 drec->set_remote(true);
312         }
313 }
314
315 bool VM::is_frame_skippable()
316 {
317         return event->is_frame_skippable();
318 }
319
320 void VM::update_config()
321 {
322         for(DEVICE* device = first_device; device; device = device->next_device) {
323                 device->update_config();
324         }
325 }
326
327 #define STATE_VERSION   3
328
329 #include "../../statesub.h"
330 #include "../../qt/gui/csp_logger.h"
331 extern CSP_Logger DLL_PREFIX_I *csp_logger;
332
333 void VM::decl_state(void)
334 {
335         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FP_200_HEAD")), csp_logger);
336         DECL_STATE_ENTRY_MULTI(void, ram, sizeof(ram));
337         for(DEVICE* device = first_device; device; device = device->next_device) {
338                 device->decl_state();
339         }
340 }
341 void VM::save_state(FILEIO* state_fio)
342 {
343         //state_fio->FputUint32(STATE_VERSION);
344         
345         if(state_entry != NULL) {
346                 state_entry->save_state(state_fio);
347         }
348         for(DEVICE* device = first_device; device; device = device->next_device) {
349                 device->save_state(state_fio);
350         }
351         //state_fio->Fwrite(ram, sizeof(ram), 1);
352 }
353
354 bool VM::load_state(FILEIO* state_fio)
355 {
356         //if(state_fio->FgetUint32() != STATE_VERSION) {
357         //      return false;
358         //}
359         bool mb = false;
360         if(state_entry != NULL) {
361                 mb = state_entry->load_state(state_fio);
362         }
363         if(!mb) {
364                 emu->out_debug_log("INFO: HEADER DATA ERROR");
365                 return false;
366         }
367         for(DEVICE* device = first_device; device; device = device->next_device) {
368                 if(!device->load_state(state_fio)) {
369                         return false;
370                 }
371         }
372         //state_fio->Fread(ram, sizeof(ram), 1);
373         return true;
374 }
375