2 BANDAI RX-78 Emulator 'eRX-78'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
15 #include "../datarec.h"
17 #include "../sn76489an.h"
21 #include "../debugger.h"
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 VM::VM(EMU* parent_emu) : emu(parent_emu)
37 first_device = last_device = NULL;
38 dummy = new DEVICE(this, emu); // must be 1st device
39 event = new EVENT(this, emu); // must be 2nd device
41 dummy->set_device_name(_T("1st Dummy"));
44 drec = new DATAREC(this, emu);
45 io = new IO(this, emu);
46 psg = new SN76489AN(this, emu);
47 cpu = new Z80(this, emu);
49 cpu->set_device_name(_T("CPU(Z80)"));
52 cmt = new CMT(this, emu);
53 key = new KEYBOARD(this, emu);
54 memory = new MEMORY(this, emu);
55 prt = new PRINTER(this, emu);
56 vdp = new VDP(this, emu);
58 cmt->set_device_name(_T("CMT I/F"));
59 key->set_device_name(_T("KEYBOARD I/F"));
60 memory->set_device_name(_T("MEMORY"));
61 prt->set_device_name(_T("PRINTER I/F"));
62 vdp->set_device_name(_T("VIDEO"));
66 event->set_context_cpu(cpu);
67 event->set_context_sound(psg);
68 event->set_context_sound(drec);
69 #if defined(USE_SOUND_FILES)
70 drec->load_sound_data(DATAREC_SNDFILE_RELAY_ON, _T("RELAY_ON.WAV"));
71 drec->load_sound_data(DATAREC_SNDFILE_RELAY_OFF, _T("RELAYOFF.WAV"));
74 drec->set_context_ear(cmt, SIG_CMT_IN, 1);
75 cmt->set_context_drec(drec);
76 vdp->set_context_cpu(cpu);
77 vdp->set_vram_ptr(memory->get_vram());
80 cpu->set_context_mem(memory);
81 cpu->set_context_io(io);
82 cpu->set_context_intr(dummy);
84 cpu->set_context_debugger(new DEBUGGER(this, emu));
88 io->set_iomap_range_rw(0xe2, 0xe3, prt);
89 io->set_iomap_single_rw(0xf0, cmt);
90 io->set_iomap_range_w(0xf1, 0xf2, memory);
91 io->set_iomap_single_rw(0xf4, key);
92 io->set_iomap_range_w(0xf5, 0xfc, vdp);
93 io->set_iomap_single_w(0xfe, vdp);
94 io->set_iomap_single_w(0xff, psg);
96 // initialize all devices
97 for(DEVICE* device = first_device; device; device = device->next_device) {
104 // delete all devices
105 for(DEVICE* device = first_device; device;) {
106 DEVICE *next_device = device->next_device;
109 device = next_device;
113 DEVICE* VM::get_device(int id)
115 for(DEVICE* device = first_device; device; device = device->next_device) {
116 if(device->this_device_id == id) {
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
128 DEVICE *VM::get_cpu(int index)
137 // ----------------------------------------------------------------------------
138 // drive virtual machine
139 // ----------------------------------------------------------------------------
144 for(DEVICE* device = first_device; device; device = device->next_device) {
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 void VM::draw_screen()
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 void VM::initialize_sound(int rate, int samples)
169 // init sound manager
170 event->initialize_sound(rate, samples);
173 psg->initialize_sound(rate, 3579545, 8000);
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 psg->set_volume(0, decibel_l, decibel_r);
192 drec->set_volume(0, decibel_l, decibel_r);
194 #if defined(USE_SOUND_FILES)
196 for(int i = 0; i < DATAREC_SNDFILE_END; i++) {
197 drec->set_volume(i + 2, decibel_l, decibel_r);
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 void VM::open_cart(int drv, const _TCHAR* file_path)
211 memory->open_cart(file_path);
216 void VM::close_cart(int drv)
219 memory->close_cart();
224 bool VM::is_cart_inserted(int drv)
227 return memory->is_cart_inserted();
233 void VM::play_tape(const _TCHAR* file_path)
235 drec->play_tape(file_path);
238 void VM::rec_tape(const _TCHAR* file_path)
240 drec->rec_tape(file_path);
243 void VM::close_tape()
250 bool VM::is_tape_inserted()
252 return drec->is_tape_inserted();
255 bool VM::is_tape_playing()
257 return drec->is_tape_playing();
260 bool VM::is_tape_recording()
262 return drec->is_tape_recording();
265 int VM::get_tape_position()
267 return drec->get_tape_position();
270 bool VM::is_frame_skippable()
272 return event->is_frame_skippable();
275 void VM::update_config()
277 for(DEVICE* device = first_device; device; device = device->next_device) {
278 device->update_config();
282 #define STATE_VERSION 1
284 void VM::save_state(FILEIO* state_fio)
286 state_fio->FputUint32(STATE_VERSION);
288 for(DEVICE* device = first_device; device; device = device->next_device) {
289 device->save_state(state_fio);
293 bool VM::load_state(FILEIO* state_fio)
295 if(state_fio->FgetUint32() != STATE_VERSION) {
298 for(DEVICE* device = first_device; device; device = device->next_device) {
299 if(!device->load_state(state_fio)) {