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 drec = new DATAREC(this, emu);
42 io = new IO(this, emu);
43 psg = new SN76489AN(this, emu);
44 cpu = new Z80(this, emu);
46 cmt = new CMT(this, emu);
47 key = new KEYBOARD(this, emu);
48 memory = new MEMORY(this, emu);
49 prt = new PRINTER(this, emu);
50 vdp = new VDP(this, emu);
53 event->set_context_cpu(cpu);
54 event->set_context_sound(psg);
55 event->set_context_sound(drec);
57 drec->set_context_ear(cmt, SIG_CMT_IN, 1);
58 cmt->set_context_drec(drec);
59 vdp->set_context_cpu(cpu);
60 vdp->set_vram_ptr(memory->get_vram());
63 cpu->set_context_mem(memory);
64 cpu->set_context_io(io);
65 cpu->set_context_intr(dummy);
67 cpu->set_context_debugger(new DEBUGGER(this, emu));
71 io->set_iomap_range_rw(0xe2, 0xe3, prt);
72 io->set_iomap_single_rw(0xf0, cmt);
73 io->set_iomap_range_w(0xf1, 0xf2, memory);
74 io->set_iomap_single_rw(0xf4, key);
75 io->set_iomap_range_w(0xf5, 0xfc, vdp);
76 io->set_iomap_single_w(0xfe, vdp);
77 io->set_iomap_single_w(0xff, psg);
79 // initialize all devices
80 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 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
111 DEVICE *VM::get_cpu(int index)
120 // ----------------------------------------------------------------------------
121 // drive virtual machine
122 // ----------------------------------------------------------------------------
127 for(DEVICE* device = first_device; device; device = device->next_device) {
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 void VM::draw_screen()
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 void VM::initialize_sound(int rate, int samples)
152 // init sound manager
153 event->initialize_sound(rate, samples);
156 psg->init(rate, 3579545, 8000);
159 uint16* VM::create_sound(int* extra_frames)
161 return event->create_sound(extra_frames);
164 int VM::sound_buffer_ptr()
166 return event->sound_buffer_ptr();
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 void VM::open_cart(int drv, const _TCHAR* file_path)
176 memory->open_cart(file_path);
181 void VM::close_cart(int drv)
184 memory->close_cart();
189 bool VM::cart_inserted(int drv)
192 return memory->cart_inserted();
198 void VM::play_tape(const _TCHAR* file_path)
200 drec->play_tape(file_path);
203 void VM::rec_tape(const _TCHAR* file_path)
205 drec->rec_tape(file_path);
208 void VM::close_tape()
213 bool VM::tape_inserted()
215 return drec->tape_inserted();
220 return event->now_skip();
223 void VM::update_config()
225 for(DEVICE* device = first_device; device; device = device->next_device) {
226 device->update_config();
230 #define STATE_VERSION 1
232 void VM::save_state(FILEIO* state_fio)
234 state_fio->FputUint32(STATE_VERSION);
236 for(DEVICE* device = first_device; device; device = device->next_device) {
237 device->save_state(state_fio);
241 bool VM::load_state(FILEIO* state_fio)
243 if(state_fio->FgetUint32() != STATE_VERSION) {
246 for(DEVICE* device = first_device; device; device = device->next_device) {
247 if(!device->load_state(state_fio)) {