2 SORD m5 Emulator 'Emu5'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
15 #include "../datarec.h"
17 #include "../memory.h"
18 #include "../sn76489an.h"
19 #include "../tms9918a.h"
21 #include "../z80ctc.h"
24 #include "../debugger.h"
30 #include "../../fileio.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 VM::VM(EMU* parent_emu) : emu(parent_emu)
39 first_device = last_device = NULL;
40 dummy = new DEVICE(this, emu); // must be 1st device
41 event = new EVENT(this, emu); // must be 2nd device
43 drec = new DATAREC(this, emu);
44 io = new IO(this, emu);
45 memory = new MEMORY(this, emu);
46 psg = new SN76489AN(this, emu);
47 vdp = new TMS9918A(this, emu);
48 cpu = new Z80(this, emu);
49 ctc = new Z80CTC(this, emu);
51 cmt = new CMT(this, emu);
52 key = new KEYBOARD(this, emu);
55 event->set_context_cpu(cpu);
56 event->set_context_sound(psg);
58 drec->set_context_out(cmt, SIG_CMT_IN, 1);
59 drec->set_context_end(cmt, SIG_CMT_EOT, 1);
60 vdp->set_context_irq(ctc, SIG_Z80CTC_TRIG_3, 1);
61 cmt->set_context_drec(drec);
64 cpu->set_context_mem(memory);
65 cpu->set_context_io(io);
66 cpu->set_context_intr(ctc);
68 cpu->set_context_debugger(new DEBUGGER(this, emu));
71 // z80 family daisy chain
72 ctc->set_context_intr(cpu, 0);
75 memset(ram, 0, sizeof(ram));
76 memset(ext, 0, sizeof(ext));
77 memset(ipl, 0xff, sizeof(ipl));
78 memset(cart, 0xff, sizeof(cart));
80 memory->read_bios(_T("IPL.ROM"), ipl, sizeof(ipl));
82 memory->set_memory_r(0x0000, 0x1fff, ipl);
83 memory->set_memory_r(0x2000, 0x6fff, cart);
84 memory->set_memory_rw(0x7000, 0x7fff, ram);
85 memory->set_memory_rw(0x8000, 0xffff, ext);
88 io->set_iomap_range_rw(0x00, 0x03, ctc);
89 io->set_iomap_range_rw(0x10, 0x11, vdp);
90 io->set_iomap_single_w(0x20, psg);
91 io->set_iomap_range_r(0x30, 0x37, key);
92 io->set_iomap_single_w(0x40, cmt);
93 io->set_iomap_single_rw(0x50, cmt);
95 // FD5 floppy drive uint
98 // subcpu->set_context_debugger(new DEBUGGER(this, emu));
101 // initialize all devices
102 for(DEVICE* device = first_device; device; device = device->next_device) {
103 device->initialize();
110 // delete all devices
111 for(DEVICE* device = first_device; device;) {
112 DEVICE *next_device = device->next_device;
115 device = next_device;
119 DEVICE* VM::get_device(int id)
121 for(DEVICE* device = first_device; device; device = device->next_device) {
122 if(device->this_device_id == id) {
129 // ----------------------------------------------------------------------------
130 // drive virtual machine
131 // ----------------------------------------------------------------------------
136 for(DEVICE* device = first_device; device; device = device->next_device) {
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
151 DEVICE *VM::get_cpu(int index)
155 } else if(index == 1) {
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 void VM::draw_screen()
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 void VM::initialize_sound(int rate, int samples)
177 // init sound manager
178 event->initialize_sound(rate, samples);
181 psg->init(rate, 3579545, 8000);
184 uint16* VM::create_sound(int* extra_frames)
186 return event->create_sound(extra_frames);
189 int VM::sound_buffer_ptr()
191 return event->sound_buffer_ptr();
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 void VM::open_cart(int drv, _TCHAR* file_path)
201 memset(cart, 0xff, sizeof(cart));
202 inserted = memory->read_image(file_path, cart, sizeof(cart));
207 void VM::close_cart(int drv)
210 memset(cart, 0xff, sizeof(cart));
216 bool VM::cart_inserted(int drv)
225 void VM::play_tape(_TCHAR* file_path)
227 drec->play_tape(file_path);
230 void VM::rec_tape(_TCHAR* file_path)
232 drec->rec_tape(file_path);
235 void VM::close_tape()
240 bool VM::tape_inserted()
242 return drec->tape_inserted();
247 return event->now_skip();
250 void VM::update_config()
252 for(DEVICE* device = first_device; device; device = device->next_device) {
253 device->update_config();
257 #define STATE_VERSION 1
259 void VM::save_state(FILEIO* state_fio)
261 state_fio->FputUint32(STATE_VERSION);
263 for(DEVICE* device = first_device; device; device = device->next_device) {
264 device->save_state(state_fio);
266 state_fio->Fwrite(ram, sizeof(ram), 1);
267 state_fio->Fwrite(ext, sizeof(ext), 1);
268 state_fio->FputBool(inserted);
271 bool VM::load_state(FILEIO* state_fio)
273 if(state_fio->FgetUint32() != STATE_VERSION) {
276 for(DEVICE* device = first_device; device; device = device->next_device) {
277 if(!device->load_state(state_fio)) {
281 state_fio->Fread(ram, sizeof(ram), 1);
282 state_fio->Fread(ext, sizeof(ext), 1);
283 inserted = state_fio->FgetBool();