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) : VM_TEMPLATE(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 #if defined(__GIT_REPO_VERSION)
119 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
121 for(DEVICE* device = first_device; device; device = device->next_device) {
122 device->initialize();
128 for(int i = 0; i < 4; i++) {
129 fdc->set_drive_type(i, DRIVE_TYPE_2D);
135 // delete all devices
136 for(DEVICE* device = first_device; device;) {
137 DEVICE *next_device = device->next_device;
140 device = next_device;
144 DEVICE* VM::get_device(int id)
146 for(DEVICE* device = first_device; device; device = device->next_device) {
147 if(device->this_device_id == id) {
154 // ----------------------------------------------------------------------------
155 // drive virtual machine
156 // ----------------------------------------------------------------------------
161 for(DEVICE* device = first_device; device; device = device->next_device) {
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
176 DEVICE *VM::get_cpu(int index)
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 void VM::draw_screen()
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 void VM::initialize_sound(int rate, int samples)
200 // init sound manager
201 event->initialize_sound(rate, samples);
204 psg->initialize_sound(rate, 3579545, 4000);
207 uint16_t* VM::create_sound(int* extra_frames)
209 return event->create_sound(extra_frames);
212 int VM::get_sound_buffer_ptr()
214 return event->get_sound_buffer_ptr();
217 #ifdef USE_SOUND_VOLUME
218 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
221 psg->set_volume(0, decibel_l, decibel_r);
223 drec->set_volume(0, decibel_l, decibel_r);
225 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
226 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
227 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
229 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
230 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
231 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
236 // ----------------------------------------------------------------------------
238 // ----------------------------------------------------------------------------
240 void VM::open_cart(int drv, const _TCHAR* file_path)
243 memory->open_cart(file_path);
244 if (check_file_extension(file_path, _T(".col"))) {
245 vdp->set_console(0x00);
246 vdp->set_context_irq(cpu, SIG_CPU_NMI, 1);
249 vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
250 if (check_file_extension(file_path, _T(".gg"))) {
251 vdp->set_console(0x40);
253 vdp->set_console(0x20);
260 void VM::close_cart(int drv)
263 memory->close_cart();
268 bool VM::is_cart_inserted(int drv)
271 return memory->is_cart_inserted();
277 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
279 fdc->open_disk(drv, file_path, bank);
282 void VM::close_floppy_disk(int drv)
284 fdc->close_disk(drv);
287 bool VM::is_floppy_disk_inserted(int drv)
289 return fdc->is_disk_inserted(drv);
292 void VM::is_floppy_disk_protected(int drv, bool value)
294 fdc->is_disk_protected(drv, value);
297 bool VM::is_floppy_disk_protected(int drv)
299 return fdc->is_disk_protected(drv);
302 uint32_t VM::is_floppy_disk_accessed()
304 return fdc->read_signal(0);
307 void VM::play_tape(int drv, const _TCHAR* file_path)
309 drec->play_tape(file_path);
310 // drec->set_remote(true);
313 void VM::rec_tape(int drv, const _TCHAR* file_path)
315 drec->rec_tape(file_path);
316 // drec->set_remote(true);
319 void VM::close_tape(int drv)
324 // drec->set_remote(false);
327 bool VM::is_tape_inserted(int drv)
329 return drec->is_tape_inserted();
332 bool VM::is_tape_playing(int drv)
334 return drec->is_tape_playing();
337 bool VM::is_tape_recording(int drv)
339 return drec->is_tape_recording();
342 int VM::get_tape_position(int drv)
344 return drec->get_tape_position();
347 const _TCHAR* VM::get_tape_message(int drv)
349 return drec->get_message();
352 void VM::push_play(int drv)
355 drec->set_remote(true);
358 void VM::push_stop(int drv)
360 drec->set_remote(false);
363 void VM::push_fast_forward(int drv)
366 drec->set_remote(true);
369 void VM::push_fast_rewind(int drv)
371 drec->set_ff_rew(-1);
372 drec->set_remote(true);
375 bool VM::is_frame_skippable()
377 return event->is_frame_skippable();
380 void VM::update_config()
382 for(DEVICE* device = first_device; device; device = device->next_device) {
383 device->update_config();