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) : emu(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 for(DEVICE* device = first_device; device; device = device->next_device) {
97 for(DEVICE* device = first_device; device;) {
98 DEVICE *next_device = device->next_device;
101 device = next_device;
105 DEVICE* VM::get_device(int id)
107 for(DEVICE* device = first_device; device; device = device->next_device) {
108 if(device->this_device_id == id) {
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
120 DEVICE *VM::get_cpu(int index)
129 // ----------------------------------------------------------------------------
130 // drive virtual machine
131 // ----------------------------------------------------------------------------
136 for(DEVICE* device = first_device; device; device = device->next_device) {
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 psg->initialize_sound(rate, 3579545, 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 psg->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::open_cart(int drv, const _TCHAR* file_path)
200 memory->open_cart(file_path);
205 void VM::close_cart(int drv)
208 memory->close_cart();
213 bool VM::is_cart_inserted(int drv)
216 return memory->is_cart_inserted();
222 void VM::play_tape(int drv, const _TCHAR* file_path)
224 drec->play_tape(file_path);
225 // drec->set_remote(true);
228 void VM::rec_tape(int drv, const _TCHAR* file_path)
230 drec->rec_tape(file_path);
231 // drec->set_remote(true);
234 void VM::close_tape(int drv)
239 // drec->set_remote(false);
242 bool VM::is_tape_inserted(int drv)
244 return drec->is_tape_inserted();
247 bool VM::is_tape_playing(int drv)
249 return drec->is_tape_playing();
252 bool VM::is_tape_recording(int drv)
254 return drec->is_tape_recording();
257 int VM::get_tape_position(int drv)
259 return drec->get_tape_position();
262 const _TCHAR* VM::get_tape_message(int drv)
264 return drec->get_message();
267 void VM::push_play(int drv)
270 drec->set_remote(true);
273 void VM::push_stop(int drv)
275 drec->set_remote(false);
278 void VM::push_fast_forward(int drv)
281 drec->set_remote(true);
284 void VM::push_fast_rewind(int drv)
286 drec->set_ff_rew(-1);
287 drec->set_remote(true);
290 bool VM::is_frame_skippable()
292 return event->is_frame_skippable();
295 void VM::update_config()
297 for(DEVICE* device = first_device; device; device = device->next_device) {
298 device->update_config();
302 #define STATE_VERSION 3
304 #include "../../statesub.h"
306 void VM::decl_state(void)
308 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::RX_78_HEAD")));
309 for(DEVICE* device = first_device; device; device = device->next_device) {
310 device->decl_state();
314 void VM::save_state(FILEIO* state_fio)
316 //state_fio->FputUint32(STATE_VERSION);
318 if(state_entry != NULL) {
319 state_entry->save_state(state_fio);
321 for(DEVICE* device = first_device; device; device = device->next_device) {
322 device->save_state(state_fio);
326 bool VM::load_state(FILEIO* state_fio)
328 //if(state_fio->FgetUint32() != STATE_VERSION) {
332 if(state_entry != NULL) {
333 mb = state_entry->load_state(state_fio);
336 emu->out_debug_log("INFO: HEADER DATA ERROR");
339 for(DEVICE* device = first_device; device; device = device->next_device) {
340 if(!device->load_state(state_fio)) {