OSDN Git Service

[VM] Enable to build with upstream 2018-10-14 @some VMs.
[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 using FP200::IO;
31
32 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
33 {
34         // create devices
35         first_device = last_device = NULL;
36         dummy = new DEVICE(this, emu);  // must be 1st device
37         event = new EVENT(this, emu);   // must be 2nd device
38         dummy->set_device_name(_T("1st Dummy"));
39
40         drec = new DATAREC(this, emu);
41         drec->set_context_noise_play(new NOISE(this, emu));
42         drec->set_context_noise_stop(new NOISE(this, emu));
43         drec->set_context_noise_fast(new NOISE(this, emu));
44         cpu = new I8080(this, emu);     // i8085
45         memory = new MEMORY(this, emu);
46         rtc = new RP5C01(this, emu);
47         
48         io = new IO(this, emu);
49         // set contexts
50         event->set_context_cpu(cpu);
51         event->set_context_sound(drec);
52         event->set_context_sound(drec->get_context_noise_play());
53         event->set_context_sound(drec->get_context_noise_stop());
54         event->set_context_sound(drec->get_context_noise_fast());
55         
56         drec->set_context_ear(io, SIG_IO_CMT, 1);
57         cpu->set_context_sod(io, SIG_IO_SOD, 1);
58         
59         io->set_context_cpu(cpu);
60         io->set_context_drec(drec);
61         io->set_context_rtc(rtc);
62         
63         // cpu bus
64         cpu->set_context_mem(memory);
65         cpu->set_context_io(io);
66         cpu->set_context_intr(io);
67 #ifdef USE_DEBUGGER
68         cpu->set_context_debugger(new DEBUGGER(this, emu));
69 #endif
70         
71         // memory bus
72         memset(rom, 0xff, sizeof(rom));
73         memset(ram, 0, sizeof(ram));
74         
75         memory->read_bios(_T("BIOS.ROM"), rom, sizeof(rom));
76         
77         FILEIO* fio = new FILEIO();
78         if(fio->Fopen(create_local_path(_T("RAM.BIN")), FILEIO_READ_BINARY)) {
79                 fio->Fread(ram, sizeof(ram), 1);
80                 fio->Fclose();
81         }
82         delete fio;
83         
84         memory->set_memory_r(0x0000, 0x7fff, rom);
85         memory->set_memory_rw(0x8000, 0xffff, ram);
86         memory->set_wait_rw(0x0000, 0xffff, 1);
87         
88         // initialize all devices
89 #if defined(__GIT_REPO_VERSION)
90         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
91 #endif
92         for(DEVICE* device = first_device; device; device = device->next_device) {
93                 device->initialize();
94         }
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 bool VM::process_state(FILEIO* state_fio, bool loading)
330 {
331         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
332                 return false;
333         }
334         for(DEVICE* device = first_device; device; device = device->next_device) {
335                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
336                 // const char *name = typeid(*device).name();
337                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
338                 const char *name = device->get_device_name();
339                 int len = strlen(name);
340                 
341                 if(!state_fio->StateCheckInt32(len)) {
342                         if(loading) {
343                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
344                         }
345                         return false;
346                 }
347                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
348                         if(loading) {
349                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
350                         }
351                         return false;
352                 }
353                 if(!device->process_state(state_fio, loading)) {
354                         if(loading) {
355                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
356                         }
357                         return false;
358                 }
359         }
360         state_fio->StateArray(ram, sizeof(ram), 1);
361         return true;
362 }