2 EPSON HC-40 Emulator 'eHC-40'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
16 #include "../datarec.h"
22 #include "../debugger.h"
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 VM::VM(EMU* 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 beep = new BEEP(this, emu);
41 drec = new DATAREC(this, emu);
42 drec->set_context_noise_play(new NOISE(this, emu));
43 drec->set_context_noise_stop(new NOISE(this, emu));
44 drec->set_context_noise_fast(new NOISE(this, emu));
45 tf20 = new PTF20(this, emu);
46 cpu = new Z80(this, emu);
48 io = new IO(this, emu);
49 memory = new MEMORY(this, emu);
51 event->set_context_cpu(cpu);
52 event->set_context_sound(beep);
53 event->set_context_sound(drec);
54 event->set_context_sound(drec->get_context_noise_play());
55 event->set_context_sound(drec->get_context_noise_stop());
56 event->set_context_sound(drec->get_context_noise_fast());
58 drec->set_context_ear(io, SIG_IO_DREC, 1);
59 tf20->set_context_sio(io, SIG_IO_ART);
61 io->set_context_cpu(cpu);
62 io->set_context_mem(memory, memory->get_ram());
63 io->set_context_tf20(tf20);
64 io->set_context_beep(beep);
65 io->set_context_drec(drec);
68 cpu->set_context_mem(memory);
69 cpu->set_context_io(io);
70 cpu->set_context_intr(io);
72 cpu->set_context_debugger(new DEBUGGER(this, emu));
75 // initialize all devices
76 #if defined(__GIT_REPO_VERSION)
77 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
79 for(DEVICE* device = first_device; device; device = device->next_device) {
88 for(DEVICE* device = first_device; device;) {
89 DEVICE *next_device = device->next_device;
96 DEVICE* VM::get_device(int id)
98 for(DEVICE* device = first_device; device; device = device->next_device) {
99 if(device->this_device_id == id) {
106 // ----------------------------------------------------------------------------
107 // drive virtual machine
108 // ----------------------------------------------------------------------------
113 for(DEVICE* device = first_device; device; device = device->next_device) {
118 void VM::special_reset()
121 for(DEVICE* device = first_device; device; device = device->next_device) {
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
137 DEVICE *VM::get_cpu(int index)
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 void VM::draw_screen()
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 void VM::initialize_sound(int rate, int samples)
161 // init sound manager
162 event->initialize_sound(rate, samples);
165 beep->initialize_sound(rate, 1000, 8000);
168 uint16_t* VM::create_sound(int* extra_frames)
170 return event->create_sound(extra_frames);
173 int VM::get_sound_buffer_ptr()
175 return event->get_sound_buffer_ptr();
178 #ifdef USE_SOUND_VOLUME
179 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
182 beep->set_volume(0, decibel_l, decibel_r);
184 drec->set_volume(0, decibel_l, decibel_r);
186 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
187 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
188 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
197 void VM::key_down(int code, bool repeat)
202 void VM::key_up(int code)
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
213 tf20->open_disk(drv, file_path, bank);
216 void VM::close_floppy_disk(int drv)
218 tf20->close_disk(drv);
221 bool VM::is_floppy_disk_inserted(int drv)
223 return tf20->is_disk_inserted(drv);
226 void VM::is_floppy_disk_protected(int drv, bool value)
228 tf20->is_disk_protected(drv, value);
231 bool VM::is_floppy_disk_protected(int drv)
233 return tf20->is_disk_protected(drv);
236 uint32_t VM::is_floppy_disk_accessed()
238 return tf20->read_signal(0);
241 void VM::play_tape(int drv, const _TCHAR* file_path)
243 drec->play_tape(file_path);
244 // drec->set_remote(true);
247 void VM::rec_tape(int drv, const _TCHAR* file_path)
249 drec->rec_tape(file_path);
250 // drec->set_remote(true);
253 void VM::close_tape(int drv)
258 // drec->set_remote(false);
261 bool VM::is_tape_inserted(int drv)
263 return drec->is_tape_inserted();
266 bool VM::is_tape_playing(int drv)
268 return drec->is_tape_playing();
271 bool VM::is_tape_recording(int drv)
273 return drec->is_tape_recording();
276 int VM::get_tape_position(int drv)
278 return drec->get_tape_position();
281 const _TCHAR* VM::get_tape_message(int drv)
283 return drec->get_message();
286 void VM::push_play(int drv)
289 drec->set_remote(true);
292 void VM::push_stop(int drv)
294 drec->set_remote(false);
297 void VM::push_fast_forward(int drv)
300 drec->set_remote(true);
303 void VM::push_fast_rewind(int drv)
305 drec->set_ff_rew(-1);
306 drec->set_remote(true);
309 bool VM::is_frame_skippable()
311 return event->is_frame_skippable();
314 void VM::update_config()
316 for(DEVICE* device = first_device; device; device = device->next_device) {
317 device->update_config();
321 #define STATE_VERSION 3
323 #include "../../statesub.h"
324 #include "../../qt/gui/csp_logger.h"
325 extern CSP_Logger DLL_PREFIX_I *csp_logger;
327 void VM::decl_state(void)
329 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::HC_40_HEAD")), csp_logger);
330 for(DEVICE* device = first_device; device; device = device->next_device) {
331 device->decl_state();
335 void VM::save_state(FILEIO* state_fio)
337 //state_fio->FputUint32(STATE_VERSION);
339 if(state_entry != NULL) {
340 state_entry->save_state(state_fio);
342 for(DEVICE* device = first_device; device; device = device->next_device) {
343 device->save_state(state_fio);
347 bool VM::load_state(FILEIO* state_fio)
349 //if(state_fio->FgetUint32() != STATE_VERSION) {
353 if(state_entry != NULL) {
354 mb = state_entry->load_state(state_fio);
357 emu->out_debug_log("INFO: HEADER DATA ERROR");
360 for(DEVICE* device = first_device; device; device = device->next_device) {
361 if(!device->load_state(state_fio)) {
368 bool VM::process_state(FILEIO* state_fio, bool loading)
370 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
373 for(DEVICE* device = first_device; device; device = device->next_device) {
374 const char *name = typeid(*device).name() + 6; // skip "class "
375 int len = strlen(name);
377 if(!state_fio->StateCheckInt32(len)) {
380 if(!state_fio->StateCheckBuffer(name, len, 1)) {
383 if(!device->process_state(state_fio, loading)) {