2 CASIO FP-200 Emulator 'eFP-200'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
15 #include "../datarec.h"
17 #include "../memory.h"
19 #include "../rp5c01.h"
22 #include "../debugger.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
32 VM::VM(EMU_TEMPLATE* parent_emu) : VM_TEMPLATE(parent_emu)
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"));
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);
47 rtc = new RP5C01(this, emu);
49 io = new IO(this, emu);
51 event->set_context_cpu(cpu);
52 event->set_context_sound(drec);
53 event->set_context_sound(drec->get_context_noise_play());
54 event->set_context_sound(drec->get_context_noise_stop());
55 event->set_context_sound(drec->get_context_noise_fast());
57 drec->set_context_ear(io, SIG_IO_CMT, 1);
58 cpu->set_context_sod(io, SIG_IO_SOD, 1);
60 io->set_context_cpu(cpu);
61 io->set_context_drec(drec);
62 io->set_context_rtc(rtc);
65 cpu->set_context_mem(memory);
66 cpu->set_context_io(io);
67 cpu->set_context_intr(io);
69 cpu->set_context_debugger(new DEBUGGER(this, emu));
73 memset(rom, 0xff, sizeof(rom));
74 memset(ram, 0, sizeof(ram));
76 memory->read_bios(_T("BIOS.ROM"), rom, sizeof(rom));
78 FILEIO* fio = new FILEIO();
79 if(fio->Fopen(create_local_path(_T("RAM.BIN")), FILEIO_READ_BINARY)) {
80 fio->Fread(ram, sizeof(ram), 1);
85 memory->set_memory_r(0x0000, 0x7fff, rom);
86 memory->set_memory_rw(0x8000, 0xffff, ram);
87 memory->set_wait_rw(0x0000, 0xffff, 1);
89 // initialize all devices
90 #if defined(__GIT_REPO_VERSION)
91 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
93 for(DEVICE* device = first_device; device; device = device->next_device) {
100 FILEIO* fio = new FILEIO();
101 if(fio->Fopen(create_local_path(_T("RAM.BIN")), FILEIO_WRITE_BINARY)) {
102 fio->Fwrite(ram, sizeof(ram), 1);
107 // delete all devices
108 for(DEVICE* device = first_device; device;) {
109 DEVICE *next_device = device->next_device;
112 device = next_device;
116 DEVICE* VM::get_device(int id)
118 for(DEVICE* device = first_device; device; device = device->next_device) {
119 if(device->this_device_id == id) {
126 // ----------------------------------------------------------------------------
127 // drive virtual machine
128 // ----------------------------------------------------------------------------
133 for(DEVICE* device = first_device; device; device = device->next_device) {
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
148 DEVICE *VM::get_cpu(int index)
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 void VM::draw_screen()
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 void VM::initialize_sound(int rate, int samples)
172 // init sound manager
173 event->initialize_sound(rate, samples);
176 uint16_t* VM::create_sound(int* extra_frames)
178 return event->create_sound(extra_frames);
181 int VM::get_sound_buffer_ptr()
183 return event->get_sound_buffer_ptr();
186 #ifdef USE_SOUND_VOLUME
187 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
190 drec->set_volume(0, decibel_l, decibel_r);
192 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
193 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
194 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
203 void VM::key_down(int code, bool repeat)
210 void VM::key_up(int code)
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
219 void VM::play_tape(int drv, const _TCHAR* file_path)
223 bool remote = drec->get_remote();
225 if(drec->play_tape(file_path) && remote) {
226 // if machine already sets remote on, start playing now
231 void VM::rec_tape(int drv, const _TCHAR* file_path)
236 drec->set_remote(false);
237 io->rec_tape(file_path);
240 void VM::close_tape(int drv)
245 drec->set_remote(false);
250 bool VM::is_tape_inserted(int drv)
252 return drec->is_tape_inserted() || io->is_tape_inserted();
255 bool VM::is_tape_playing(int drv)
257 if(drec->is_tape_inserted()) {
258 return drec->is_tape_playing();
260 return io->is_tape_playing();
264 bool VM::is_tape_recording(int drv)
266 if(drec->is_tape_inserted()) {
267 return drec->is_tape_recording();
269 return io->is_tape_recording();
273 int VM::get_tape_position(int drv)
275 if(drec->is_tape_inserted()) {
276 return drec->get_tape_position();
278 return io->get_tape_position();
282 const _TCHAR* VM::get_tape_message(int drv)
284 if(drec->is_tape_inserted()) {
285 return drec->get_message();
291 void VM::push_play(int drv)
293 if(drec->is_tape_inserted()) {
294 drec->set_remote(false);
296 drec->set_remote(true);
300 void VM::push_stop(int drv)
302 if(drec->is_tape_inserted()) {
303 drec->set_remote(false);
307 void VM::push_fast_forward(int drv)
309 if(drec->is_tape_inserted()) {
310 drec->set_remote(false);
312 drec->set_remote(true);
316 void VM::push_fast_rewind(int drv)
318 if(drec->is_tape_inserted()) {
319 drec->set_remote(false);
320 drec->set_ff_rew(-1);
321 drec->set_remote(true);
325 bool VM::is_frame_skippable()
327 return event->is_frame_skippable();
330 void VM::update_config()
332 for(DEVICE* device = first_device; device; device = device->next_device) {
333 device->update_config();
337 double VM::get_current_usec()
339 if(event == NULL) return 0.0;
340 return event->get_current_usec();
343 uint64_t VM::get_current_clock_uint64()
345 if(event == NULL) return (uint64_t)0;
346 return event->get_current_clock_uint64();
349 #define STATE_VERSION 3
351 bool VM::process_state(FILEIO* state_fio, bool loading)
353 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
356 for(DEVICE* device = first_device; device; device = device->next_device) {
357 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
358 // const char *name = typeid(*device).name();
359 // But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
360 const char *name = device->get_device_name();
361 int len = strlen(name);
363 if(!state_fio->StateCheckInt32(len)) {
365 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
369 if(!state_fio->StateCheckBuffer(name, len, 1)) {
371 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
375 if(!device->process_state(state_fio, loading)) {
377 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
382 state_fio->StateArray(ram, sizeof(ram), 1);