2 BANDAI RX-78 Emulator 'eRX-78'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
15 #include "../datarec.h"
18 #include "../sn76489an.h"
22 #include "../debugger.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
38 first_device = last_device = NULL;
39 dummy = new DEVICE(this, emu); // must be 1st device
40 event = new EVENT(this, emu); // must be 2nd device
41 dummy->set_device_name(_T("1st Dummy"));
43 drec = new DATAREC(this, emu);
44 drec->set_context_noise_play(new NOISE(this, emu));
45 drec->set_context_noise_stop(new NOISE(this, emu));
46 drec->set_context_noise_fast(new NOISE(this, emu));
47 io = new IO(this, emu);
48 psg = new SN76489AN(this, emu);
49 cpu = new Z80(this, emu);
51 cmt = new CMT(this, emu);
52 key = new KEYBOARD(this, emu);
53 memory = new MEMORY(this, emu);
54 prt = new PRINTER(this, emu);
55 vdp = new VDP(this, emu);
58 event->set_context_cpu(cpu);
59 event->set_context_sound(psg);
60 event->set_context_sound(drec);
61 event->set_context_sound(drec->get_context_noise_play());
62 event->set_context_sound(drec->get_context_noise_stop());
63 event->set_context_sound(drec->get_context_noise_fast());
65 drec->set_context_ear(cmt, SIG_CMT_IN, 1);
66 cmt->set_context_drec(drec);
67 vdp->set_context_cpu(cpu);
68 vdp->set_vram_ptr(memory->get_vram());
71 cpu->set_context_mem(memory);
72 cpu->set_context_io(io);
73 cpu->set_context_intr(dummy);
75 cpu->set_context_debugger(new DEBUGGER(this, emu));
79 io->set_iomap_range_rw(0xe2, 0xe3, prt);
80 io->set_iomap_single_rw(0xf0, cmt);
81 io->set_iomap_range_w(0xf1, 0xf2, memory);
82 io->set_iomap_single_rw(0xf4, key);
83 io->set_iomap_range_w(0xf5, 0xfc, vdp);
84 io->set_iomap_single_w(0xfe, vdp);
85 io->set_iomap_single_w(0xff, psg);
87 // initialize all devices
88 #if defined(__GIT_REPO_VERSION)
89 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
91 for(DEVICE* device = first_device; device; device = device->next_device) {
100 for(DEVICE* device = first_device; device;) {
101 DEVICE *next_device = device->next_device;
104 device = next_device;
108 DEVICE* VM::get_device(int id)
110 for(DEVICE* device = first_device; device; device = device->next_device) {
111 if(device->this_device_id == id) {
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
123 DEVICE *VM::get_cpu(int index)
132 // ----------------------------------------------------------------------------
133 // drive virtual machine
134 // ----------------------------------------------------------------------------
139 for(DEVICE* device = first_device; device; device = device->next_device) {
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 void VM::draw_screen()
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 void VM::initialize_sound(int rate, int samples)
164 // init sound manager
165 event->initialize_sound(rate, samples);
168 psg->initialize_sound(rate, 3579545, 8000);
171 uint16_t* VM::create_sound(int* extra_frames)
173 return event->create_sound(extra_frames);
176 int VM::get_sound_buffer_ptr()
178 return event->get_sound_buffer_ptr();
181 #ifdef USE_SOUND_VOLUME
182 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
185 psg->set_volume(0, decibel_l, decibel_r);
187 drec->set_volume(0, decibel_l, decibel_r);
189 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
190 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
191 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
200 void VM::open_cart(int drv, const _TCHAR* file_path)
203 memory->open_cart(file_path);
208 void VM::close_cart(int drv)
211 memory->close_cart();
216 bool VM::is_cart_inserted(int drv)
219 return memory->is_cart_inserted();
225 void VM::play_tape(int drv, const _TCHAR* file_path)
227 drec->play_tape(file_path);
228 // drec->set_remote(true);
231 void VM::rec_tape(int drv, const _TCHAR* file_path)
233 drec->rec_tape(file_path);
234 // drec->set_remote(true);
237 void VM::close_tape(int drv)
242 // drec->set_remote(false);
245 bool VM::is_tape_inserted(int drv)
247 return drec->is_tape_inserted();
250 bool VM::is_tape_playing(int drv)
252 return drec->is_tape_playing();
255 bool VM::is_tape_recording(int drv)
257 return drec->is_tape_recording();
260 int VM::get_tape_position(int drv)
262 return drec->get_tape_position();
265 const _TCHAR* VM::get_tape_message(int drv)
267 return drec->get_message();
270 void VM::push_play(int drv)
273 drec->set_remote(true);
276 void VM::push_stop(int drv)
278 drec->set_remote(false);
281 void VM::push_fast_forward(int drv)
284 drec->set_remote(true);
287 void VM::push_fast_rewind(int drv)
289 drec->set_ff_rew(-1);
290 drec->set_remote(true);
293 bool VM::is_frame_skippable()
295 return event->is_frame_skippable();
298 void VM::update_config()
300 for(DEVICE* device = first_device; device; device = device->next_device) {
301 device->update_config();
305 #define STATE_VERSION 3
307 #include "../../statesub.h"
308 #include "../../qt/gui/csp_logger.h"
309 extern CSP_Logger DLL_PREFIX_I *csp_logger;
311 void VM::decl_state(void)
313 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::RX_78_HEAD")), csp_logger);
314 for(DEVICE* device = first_device; device; device = device->next_device) {
315 device->decl_state();
319 void VM::save_state(FILEIO* state_fio)
321 //state_fio->FputUint32(STATE_VERSION);
323 if(state_entry != NULL) {
324 state_entry->save_state(state_fio);
326 for(DEVICE* device = first_device; device; device = device->next_device) {
327 device->save_state(state_fio);
331 bool VM::load_state(FILEIO* state_fio)
333 //if(state_fio->FgetUint32() != STATE_VERSION) {
337 if(state_entry != NULL) {
338 mb = state_entry->load_state(state_fio);
341 emu->out_debug_log("INFO: HEADER DATA ERROR");
344 for(DEVICE* device = first_device; device; device = device->next_device) {
345 if(!device->load_state(state_fio)) {
352 bool VM::process_state(FILEIO* state_fio, bool loading)
354 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
357 for(DEVICE* device = first_device; device; device = device->next_device) {
358 const char *name = typeid(*device).name() + 6; // skip "class "
359 int len = strlen(name);
361 if(!state_fio->StateCheckInt32(len)) {
364 if(!state_fio->StateCheckBuffer(name, len, 1)) {
367 if(!device->process_state(state_fio, loading)) {