OSDN Git Service

Merge branch 'master' of github.com:Artanejp/common_source_project-fm7
[csp-qt/common_source_project-fm7.git] / source / src / vm / gamegear / gamegear.cpp
1 /*
2         SEGA GAME GEAR Emulator 'yaGAME GEAR'
3
4         Author : tanam
5         Date   : 2013.08.24-
6
7         [ virtual machine ]
8 */
9
10 #include "gamegear.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../disk.h"
17 #include "../i8251.h"
18 #include "../i8255.h"
19 #include "../io.h"
20 #include "../noise.h"
21 #include "../sn76489an.h"
22 #include "../315-5124.h"
23 #include "../upd765a.h"
24 #include "../z80.h"
25
26 #ifdef USE_DEBUGGER
27 #include "../debugger.h"
28 #endif
29
30 #include "keyboard.h"
31 #include "memory.h"
32 #include "system.h"
33
34 // ----------------------------------------------------------------------------
35 // initialize
36 // ----------------------------------------------------------------------------
37
38 VM::VM(EMU* parent_emu) : emu(parent_emu)
39 {
40         // create devices
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"));
45
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);
63         
64         key = new KEYBOARD(this, emu);
65         memory = new MEMORY(this, emu);
66         system = new SYSTEM(this, emu);
67         // set contexts
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());
77         
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);
88         
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);
95         
96         // cpu bus
97         cpu->set_context_mem(memory);
98         cpu->set_context_io(io);
99         cpu->set_context_intr(system);
100 #ifdef USE_DEBUGGER
101         cpu->set_context_debugger(new DEBUGGER(this, emu));
102 #endif
103         
104         // i/o bus
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
116         
117         // initialize all devices
118         for(DEVICE* device = first_device; device; device = device->next_device) {
119                 device->initialize();
120         }
121         
122         // BIOS
123         memory->bios();
124         
125         for(int i = 0; i < 4; i++) {
126                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
127         }
128 }
129
130 VM::~VM()
131 {
132         // delete all devices
133         for(DEVICE* device = first_device; device;) {
134                 DEVICE *next_device = device->next_device;
135                 device->release();
136                 delete device;
137                 device = next_device;
138         }
139 }
140
141 DEVICE* VM::get_device(int id)
142 {
143         for(DEVICE* device = first_device; device; device = device->next_device) {
144                 if(device->this_device_id == id) {
145                         return device;
146                 }
147         }
148         return NULL;
149 }
150
151 // ----------------------------------------------------------------------------
152 // drive virtual machine
153 // ----------------------------------------------------------------------------
154
155 void VM::reset()
156 {
157         // reset all devices
158         for(DEVICE* device = first_device; device; device = device->next_device) {
159                 device->reset();
160         }
161 }
162
163 void VM::run()
164 {
165         event->drive();
166 }
167
168 // ----------------------------------------------------------------------------
169 // debugger
170 // ----------------------------------------------------------------------------
171
172 #ifdef USE_DEBUGGER
173 DEVICE *VM::get_cpu(int index)
174 {
175         if(index == 0) {
176                 return cpu;
177         }
178         return NULL;
179 }
180 #endif
181
182 // ----------------------------------------------------------------------------
183 // draw screen
184 // ----------------------------------------------------------------------------
185
186 void VM::draw_screen()
187 {
188         vdp->draw_screen();
189 }
190
191 // ----------------------------------------------------------------------------
192 // soud manager
193 // ----------------------------------------------------------------------------
194
195 void VM::initialize_sound(int rate, int samples)
196 {
197         // init sound manager
198         event->initialize_sound(rate, samples);
199         
200         // init sound gen
201         psg->initialize_sound(rate, 3579545, 4000);
202 }
203
204 uint16_t* VM::create_sound(int* extra_frames)
205 {
206         return event->create_sound(extra_frames);
207 }
208
209 int VM::get_sound_buffer_ptr()
210 {
211         return event->get_sound_buffer_ptr();
212 }
213
214 #ifdef USE_SOUND_VOLUME
215 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
216 {
217         if(ch == 0) {
218                 psg->set_volume(0, decibel_l, decibel_r);
219         } else if(ch == 1) {
220                 drec->set_volume(0, decibel_l, decibel_r);
221         } else if(ch == 2) {
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);
225         } else if(ch == 3) {
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);
229         }
230 }
231 #endif
232
233 // ----------------------------------------------------------------------------
234 // user interface
235 // ----------------------------------------------------------------------------
236
237 void VM::open_cart(int drv, const _TCHAR* file_path)
238 {
239         if(drv == 0) {
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);
244                         memory->bios();
245                 } else {
246                         vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
247                         if (check_file_extension(file_path, _T(".gg"))) {
248                                 vdp->set_console(0x40);
249                         } else {
250                                 vdp->set_console(0x20);
251                         }
252                 }
253                 reset();
254         }
255 }
256
257 void VM::close_cart(int drv)
258 {
259         if(drv == 0) {
260                 memory->close_cart();
261                 reset();
262         }
263 }
264
265 bool VM::is_cart_inserted(int drv)
266 {
267         if(drv == 0) {
268                 return memory->is_cart_inserted();
269         } else {
270                 return false;
271         }
272 }
273
274 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
275 {
276         fdc->open_disk(drv, file_path, bank);
277 }
278
279 void VM::close_floppy_disk(int drv)
280 {
281         fdc->close_disk(drv);
282 }
283
284 bool VM::is_floppy_disk_inserted(int drv)
285 {
286         return fdc->is_disk_inserted(drv);
287 }
288
289 void VM::is_floppy_disk_protected(int drv, bool value)
290 {
291         fdc->is_disk_protected(drv, value);
292 }
293
294 bool VM::is_floppy_disk_protected(int drv)
295 {
296         return fdc->is_disk_protected(drv);
297 }
298
299 uint32_t VM::is_floppy_disk_accessed()
300 {
301         return fdc->read_signal(0);
302 }
303
304 void VM::play_tape(int drv, const _TCHAR* file_path)
305 {
306         drec->play_tape(file_path);
307 //      drec->set_remote(true);
308 }
309
310 void VM::rec_tape(int drv, const _TCHAR* file_path)
311 {
312         drec->rec_tape(file_path);
313 //      drec->set_remote(true);
314 }
315
316 void VM::close_tape(int drv)
317 {
318         emu->lock_vm();
319         drec->close_tape();
320         emu->unlock_vm();
321 //      drec->set_remote(false);
322 }
323
324 bool VM::is_tape_inserted(int drv)
325 {
326         return drec->is_tape_inserted();
327 }
328
329 bool VM::is_tape_playing(int drv)
330 {
331         return drec->is_tape_playing();
332 }
333
334 bool VM::is_tape_recording(int drv)
335 {
336         return drec->is_tape_recording();
337 }
338
339 int VM::get_tape_position(int drv)
340 {
341         return drec->get_tape_position();
342 }
343
344 const _TCHAR* VM::get_tape_message(int drv)
345 {
346         return drec->get_message();
347 }
348
349 void VM::push_play(int drv)
350 {
351         drec->set_ff_rew(0);
352         drec->set_remote(true);
353 }
354
355 void VM::push_stop(int drv)
356 {
357         drec->set_remote(false);
358 }
359
360 void VM::push_fast_forward(int drv)
361 {
362         drec->set_ff_rew(1);
363         drec->set_remote(true);
364 }
365
366 void VM::push_fast_rewind(int drv)
367 {
368         drec->set_ff_rew(-1);
369         drec->set_remote(true);
370 }
371
372 bool VM::is_frame_skippable()
373 {
374         return event->is_frame_skippable();
375 }
376
377 void VM::update_config()
378 {
379         for(DEVICE* device = first_device; device; device = device->next_device) {
380                 device->update_config();
381         }
382 }
383