2 SEGA GAME GEAR Emulator 'yaGAME GEAR'
11 #include "../../emu.h"
12 #include "../device.h"
15 #include "../datarec.h"
21 #include "../sn76489an.h"
22 #include "../315-5124.h"
23 #include "../upd765a.h"
27 #include "../debugger.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 VM::VM(EMU* parent_emu) : emu(parent_emu)
41 first_device = last_device = NULL;
42 dummy = new DEVICE(this, emu); // must be 1st device
43 event = new EVENT(this, emu); // must be 2nd device
44 dummy->set_device_name(_T("1st Dummy"));
46 drec = new DATAREC(this, emu);
47 drec->set_context_noise_play(new NOISE(this, emu));
48 drec->set_context_noise_stop(new NOISE(this, emu));
49 drec->set_context_noise_fast(new NOISE(this, emu));
50 sio = new I8251(this, emu);
51 pio_k = new I8255(this, emu);
52 pio_k->set_device_name(_T("8255 PIO (Keyboard)"));
53 pio_f = new I8255(this, emu);
54 pio_f->set_device_name(_T("8255 PIO (Floppy I/F)"));
55 io = new IO(this, emu);
56 psg = new SN76489AN(this, emu);
57 vdp = new _315_5124(this, emu);
58 fdc = new UPD765A(this, emu);
59 fdc->set_context_noise_seek(new NOISE(this, emu));
60 fdc->set_context_noise_head_down(new NOISE(this, emu));
61 fdc->set_context_noise_head_up(new NOISE(this, emu));
62 cpu = new Z80(this, emu);
64 key = new KEYBOARD(this, emu);
65 memory = new MEMORY(this, emu);
66 system = new SYSTEM(this, emu);
68 event->set_context_cpu(cpu);
69 event->set_context_sound(psg);
70 event->set_context_sound(drec);
71 event->set_context_sound(fdc->get_context_noise_seek());
72 event->set_context_sound(fdc->get_context_noise_head_down());
73 event->set_context_sound(fdc->get_context_noise_head_up());
74 event->set_context_sound(drec->get_context_noise_play());
75 event->set_context_sound(drec->get_context_noise_stop());
76 event->set_context_sound(drec->get_context_noise_fast());
78 drec->set_context_ear(pio_k, SIG_I8255_PORT_B, 0x80);
79 pio_k->set_context_port_c(key, SIG_KEYBOARD_COLUMN, 0x07, 0);
80 pio_k->set_context_port_c(drec, SIG_DATAREC_REMOTE, 0x08, 0);
81 pio_k->set_context_port_c(drec, SIG_DATAREC_MIC, 0x10, 0);
82 pio_f->set_context_port_c(fdc, SIG_UPD765A_MOTOR_NEG, 2, 0);
83 pio_f->set_context_port_c(fdc, SIG_UPD765A_TC, 4, 0);
84 pio_f->set_context_port_c(fdc, SIG_UPD765A_RESET, 8, 0);
85 pio_f->set_context_port_c(memory, SIG_MEMORY_SEL, 0x40, 0);
86 fdc->set_context_irq(pio_f, SIG_I8255_PORT_A, 1);
87 fdc->set_context_index(pio_f, SIG_I8255_PORT_A, 4);
89 key->set_context_cpu(cpu);
90 key->set_context_pio(pio_k);
91 system->set_context_key(key);
92 vdp->set_context_psg(psg);
93 vdp->set_context_key(key);
94 /// vdp->set_context_cpu(cpu);
97 cpu->set_context_mem(memory);
98 cpu->set_context_io(io);
99 cpu->set_context_intr(system);
101 cpu->set_context_debugger(new DEBUGGER(this, emu));
105 io->set_iomap_range_rw(0x00, 0x06, system); // GG START
106 io->set_iomap_single_w(0x80, system); // COL TENKEY
107 io->set_iomap_single_w(0xc0, system); // COL JOYPAD
108 io->set_iomap_range_rw(0xfc, 0xfe, system); // COL JOYPAD
109 io->set_iomap_range_rw(0xff, 0xff, psg); // COL PSG
110 io->set_iomap_range_rw(0x7e, 0x7f, vdp); // SG VDP
111 io->set_iomap_range_rw(0xbe, 0xbf, vdp); // SG VDP
112 io->set_iomap_range_rw(0xdc, 0xdf, pio_k); // SG KEY
113 io->set_iomap_range_rw(0xe0, 0xe3, fdc); // SG FDD
114 io->set_iomap_range_rw(0xe4, 0xe7, pio_f); // SG FDD
115 io->set_iomap_range_rw(0xe8, 0xe9, sio); // SG SERIAL
117 // initialize all devices
118 for(DEVICE* device = first_device; device; device = device->next_device) {
119 device->initialize();
125 for(int i = 0; i < 4; i++) {
126 fdc->set_drive_type(i, DRIVE_TYPE_2D);
132 // delete all devices
133 for(DEVICE* device = first_device; device;) {
134 DEVICE *next_device = device->next_device;
137 device = next_device;
141 DEVICE* VM::get_device(int id)
143 for(DEVICE* device = first_device; device; device = device->next_device) {
144 if(device->this_device_id == id) {
151 // ----------------------------------------------------------------------------
152 // drive virtual machine
153 // ----------------------------------------------------------------------------
158 for(DEVICE* device = first_device; device; device = device->next_device) {
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
173 DEVICE *VM::get_cpu(int index)
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 void VM::draw_screen()
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 void VM::initialize_sound(int rate, int samples)
197 // init sound manager
198 event->initialize_sound(rate, samples);
201 psg->initialize_sound(rate, 3579545, 4000);
204 uint16_t* VM::create_sound(int* extra_frames)
206 return event->create_sound(extra_frames);
209 int VM::get_sound_buffer_ptr()
211 return event->get_sound_buffer_ptr();
214 #ifdef USE_SOUND_VOLUME
215 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
218 psg->set_volume(0, decibel_l, decibel_r);
220 drec->set_volume(0, decibel_l, decibel_r);
222 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
223 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
224 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
226 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
227 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
228 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 void VM::open_cart(int drv, const _TCHAR* file_path)
240 memory->open_cart(file_path);
241 if (check_file_extension(file_path, _T(".col"))) {
242 vdp->set_console(0x00);
243 vdp->set_context_irq(cpu, SIG_CPU_NMI, 1);
246 vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
247 if (check_file_extension(file_path, _T(".gg"))) {
248 vdp->set_console(0x40);
250 vdp->set_console(0x20);
257 void VM::close_cart(int drv)
260 memory->close_cart();
265 bool VM::is_cart_inserted(int drv)
268 return memory->is_cart_inserted();
274 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
276 fdc->open_disk(drv, file_path, bank);
279 void VM::close_floppy_disk(int drv)
281 fdc->close_disk(drv);
284 bool VM::is_floppy_disk_inserted(int drv)
286 return fdc->is_disk_inserted(drv);
289 void VM::is_floppy_disk_protected(int drv, bool value)
291 fdc->is_disk_protected(drv, value);
294 bool VM::is_floppy_disk_protected(int drv)
296 return fdc->is_disk_protected(drv);
299 uint32_t VM::is_floppy_disk_accessed()
301 return fdc->read_signal(0);
304 void VM::play_tape(int drv, const _TCHAR* file_path)
306 drec->play_tape(file_path);
307 // drec->set_remote(true);
310 void VM::rec_tape(int drv, const _TCHAR* file_path)
312 drec->rec_tape(file_path);
313 // drec->set_remote(true);
316 void VM::close_tape(int drv)
321 // drec->set_remote(false);
324 bool VM::is_tape_inserted(int drv)
326 return drec->is_tape_inserted();
329 bool VM::is_tape_playing(int drv)
331 return drec->is_tape_playing();
334 bool VM::is_tape_recording(int drv)
336 return drec->is_tape_recording();
339 int VM::get_tape_position(int drv)
341 return drec->get_tape_position();
344 const _TCHAR* VM::get_tape_message(int drv)
346 return drec->get_message();
349 void VM::push_play(int drv)
352 drec->set_remote(true);
355 void VM::push_stop(int drv)
357 drec->set_remote(false);
360 void VM::push_fast_forward(int drv)
363 drec->set_remote(true);
366 void VM::push_fast_rewind(int drv)
368 drec->set_ff_rew(-1);
369 drec->set_remote(true);
372 bool VM::is_frame_skippable()
374 return event->is_frame_skippable();
377 void VM::update_config()
379 for(DEVICE* device = first_device; device; device = device->next_device) {
380 device->update_config();