2 SEGA GAME GEAR Emulator 'yaGAME GEAR'
11 #include "../../emu.h"
12 #include "../device.h"
15 #include "../datarec.h"
20 #include "../sn76489an.h"
21 #include "../315-5124.h"
22 #include "../upd765a.h"
26 #include "../debugger.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 VM::VM(EMU* parent_emu) : emu(parent_emu)
40 first_device = last_device = NULL;
41 dummy = new DEVICE(this, emu); // must be 1st device
42 event = new EVENT(this, emu); // must be 2nd device
44 drec = new DATAREC(this, emu);
45 sio = new I8251(this, emu);
46 pio_k = new I8255(this, emu);
47 pio_f = new I8255(this, emu);
48 io = new IO(this, emu);
49 psg = new SN76489AN(this, emu);
50 vdp = new _315_5124(this, emu);
51 fdc = new UPD765A(this, emu);
52 cpu = new Z80(this, emu);
54 key = new KEYBOARD(this, emu);
55 memory = new MEMORY(this, emu);
56 system = new SYSTEM(this, emu);
59 event->set_context_cpu(cpu);
60 event->set_context_sound(psg);
61 event->set_context_sound(drec);
63 drec->set_context_out(pio_k, SIG_I8255_PORT_B, 0x80);
64 pio_k->set_context_port_c(key, SIG_KEYBOARD_COLUMN, 0x07, 0);
65 pio_k->set_context_port_c(drec, SIG_DATAREC_REMOTE, 0x08, 0);
66 pio_k->set_context_port_c(drec, SIG_DATAREC_OUT, 0x10, 0);
67 pio_f->set_context_port_c(fdc, SIG_UPD765A_MOTOR_NEG, 2, 0);
68 pio_f->set_context_port_c(fdc, SIG_UPD765A_TC, 4, 0);
69 pio_f->set_context_port_c(fdc, SIG_UPD765A_RESET, 8, 0);
70 pio_f->set_context_port_c(memory, SIG_MEMORY_SEL, 0x40, 0);
71 fdc->set_context_irq(pio_f, SIG_I8255_PORT_A, 1);
72 fdc->set_context_index(pio_f, SIG_I8255_PORT_A, 4);
74 key->set_context_cpu(cpu);
75 key->set_context_pio(pio_k);
76 system->set_context_key(key);
77 vdp->set_context_psg(psg);
78 vdp->set_context_key(key);
79 /// vdp->set_context_cpu(cpu);
82 cpu->set_context_mem(memory);
83 cpu->set_context_io(io);
84 cpu->set_context_intr(system);
86 cpu->set_context_debugger(new DEBUGGER(this, emu));
90 io->set_iomap_range_rw(0x00, 0x06, system); // GG START
91 io->set_iomap_single_w(0x80, system); // COL TENKEY
92 io->set_iomap_single_w(0xc0, system); // COL JOYPAD
93 io->set_iomap_range_rw(0xfc, 0xfe, system); // COL JOYPAD
94 io->set_iomap_range_rw(0xff, 0xff, psg); // COL PSG
95 io->set_iomap_range_rw(0x7e, 0x7f, vdp); // SG VDP
96 io->set_iomap_range_rw(0xbe, 0xbf, vdp); // SG VDP
97 io->set_iomap_range_rw(0xdc, 0xdf, pio_k); // SG KEY
98 io->set_iomap_range_rw(0xe0, 0xe3, fdc); // SG FDD
99 io->set_iomap_range_rw(0xe4, 0xe7, pio_f); // SG FDD
100 io->set_iomap_range_rw(0xe8, 0xe9, sio); // SG SERIAL
102 // initialize all devices
103 for(DEVICE* device = first_device; device; device = device->next_device) {
104 device->initialize();
110 for(int i = 0; i < 4; i++) {
111 fdc->set_drive_type(i, DRIVE_TYPE_2D);
117 // delete all devices
118 for(DEVICE* device = first_device; device;) {
119 DEVICE *next_device = device->next_device;
122 device = next_device;
126 DEVICE* VM::get_device(int id)
128 for(DEVICE* device = first_device; device; device = device->next_device) {
129 if(device->this_device_id == id) {
136 // ----------------------------------------------------------------------------
137 // drive virtual machine
138 // ----------------------------------------------------------------------------
143 for(DEVICE* device = first_device; device; device = device->next_device) {
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
158 DEVICE *VM::get_cpu(int index)
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 void VM::draw_screen()
176 int VM::access_lamp()
178 uint32 status = fdc->read_signal(0);
179 return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 void VM::initialize_sound(int rate, int samples)
188 // init sound manager
189 event->initialize_sound(rate, samples);
192 psg->init(rate, 3579545, 4000);
195 uint16* VM::create_sound(int* extra_frames)
197 return event->create_sound(extra_frames);
200 int VM::sound_buffer_ptr()
202 return event->sound_buffer_ptr();
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
209 void VM::open_cart(int drv, _TCHAR* file_path)
212 memory->open_cart(file_path);
213 if (strstr(file_path, ".col") ||
214 strstr(file_path, ".COL")) {
215 vdp->set_console(0x00);
216 vdp->set_context_irq(cpu, SIG_CPU_NMI, 1);
219 vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
220 if (strstr(file_path, ".gg") ||
221 strstr(file_path, ".GG")) vdp->set_console(0x40);
223 vdp->set_console(0x20);
229 void VM::close_cart(int drv)
232 memory->close_cart();
237 bool VM::cart_inserted(int drv)
240 return memory->cart_inserted();
246 void VM::open_disk(int drv, _TCHAR* file_path, int bank)
248 fdc->open_disk(drv, file_path, bank);
251 void VM::close_disk(int drv)
253 fdc->close_disk(drv);
256 bool VM::disk_inserted(int drv)
258 return fdc->disk_inserted(drv);
261 void VM::set_disk_protected(int drv, bool value)
263 fdc->set_disk_protected(drv, value);
266 bool VM::get_disk_protected(int drv)
268 return fdc->get_disk_protected(drv);
271 void VM::play_tape(_TCHAR* file_path)
273 drec->play_tape(file_path);
276 void VM::rec_tape(_TCHAR* file_path)
278 drec->rec_tape(file_path);
281 void VM::close_tape()
286 bool VM::tape_inserted()
288 return drec->tape_inserted();
293 return event->now_skip();
296 void VM::update_config()
298 for(DEVICE* device = first_device; device; device = device->next_device) {
299 device->update_config();