OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[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) : VM_TEMPLATE(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 #if defined(__GIT_REPO_VERSION)
119         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
120 #endif
121         for(DEVICE* device = first_device; device; device = device->next_device) {
122                 device->initialize();
123         }
124         
125         // BIOS
126         memory->bios();
127         
128         for(int i = 0; i < 4; i++) {
129                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
130         }
131 }
132
133 VM::~VM()
134 {
135         // delete all devices
136         for(DEVICE* device = first_device; device;) {
137                 DEVICE *next_device = device->next_device;
138                 device->release();
139                 delete device;
140                 device = next_device;
141         }
142 }
143
144 DEVICE* VM::get_device(int id)
145 {
146         for(DEVICE* device = first_device; device; device = device->next_device) {
147                 if(device->this_device_id == id) {
148                         return device;
149                 }
150         }
151         return NULL;
152 }
153
154 // ----------------------------------------------------------------------------
155 // drive virtual machine
156 // ----------------------------------------------------------------------------
157
158 void VM::reset()
159 {
160         // reset all devices
161         for(DEVICE* device = first_device; device; device = device->next_device) {
162                 device->reset();
163         }
164 }
165
166 void VM::run()
167 {
168         event->drive();
169 }
170
171 // ----------------------------------------------------------------------------
172 // debugger
173 // ----------------------------------------------------------------------------
174
175 #ifdef USE_DEBUGGER
176 DEVICE *VM::get_cpu(int index)
177 {
178         if(index == 0) {
179                 return cpu;
180         }
181         return NULL;
182 }
183 #endif
184
185 // ----------------------------------------------------------------------------
186 // draw screen
187 // ----------------------------------------------------------------------------
188
189 void VM::draw_screen()
190 {
191         vdp->draw_screen();
192 }
193
194 // ----------------------------------------------------------------------------
195 // soud manager
196 // ----------------------------------------------------------------------------
197
198 void VM::initialize_sound(int rate, int samples)
199 {
200         // init sound manager
201         event->initialize_sound(rate, samples);
202         
203         // init sound gen
204         psg->initialize_sound(rate, 3579545, 4000);
205 }
206
207 uint16_t* VM::create_sound(int* extra_frames)
208 {
209         return event->create_sound(extra_frames);
210 }
211
212 int VM::get_sound_buffer_ptr()
213 {
214         return event->get_sound_buffer_ptr();
215 }
216
217 #ifdef USE_SOUND_VOLUME
218 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
219 {
220         if(ch == 0) {
221                 psg->set_volume(0, decibel_l, decibel_r);
222         } else if(ch == 1) {
223                 drec->set_volume(0, decibel_l, decibel_r);
224         } else if(ch == 2) {
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);
228         } else if(ch == 3) {
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);
232         }
233 }
234 #endif
235
236 // ----------------------------------------------------------------------------
237 // user interface
238 // ----------------------------------------------------------------------------
239
240 void VM::open_cart(int drv, const _TCHAR* file_path)
241 {
242         if(drv == 0) {
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);
247                         memory->bios();
248                 } else {
249                         vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
250                         if (check_file_extension(file_path, _T(".gg"))) {
251                                 vdp->set_console(0x40);
252                         } else {
253                                 vdp->set_console(0x20);
254                         }
255                 }
256                 reset();
257         }
258 }
259
260 void VM::close_cart(int drv)
261 {
262         if(drv == 0) {
263                 memory->close_cart();
264                 reset();
265         }
266 }
267
268 bool VM::is_cart_inserted(int drv)
269 {
270         if(drv == 0) {
271                 return memory->is_cart_inserted();
272         } else {
273                 return false;
274         }
275 }
276
277 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
278 {
279         fdc->open_disk(drv, file_path, bank);
280 }
281
282 void VM::close_floppy_disk(int drv)
283 {
284         fdc->close_disk(drv);
285 }
286
287 bool VM::is_floppy_disk_inserted(int drv)
288 {
289         return fdc->is_disk_inserted(drv);
290 }
291
292 void VM::is_floppy_disk_protected(int drv, bool value)
293 {
294         fdc->is_disk_protected(drv, value);
295 }
296
297 bool VM::is_floppy_disk_protected(int drv)
298 {
299         return fdc->is_disk_protected(drv);
300 }
301
302 uint32_t VM::is_floppy_disk_accessed()
303 {
304         return fdc->read_signal(0);
305 }
306
307 void VM::play_tape(int drv, const _TCHAR* file_path)
308 {
309         drec->play_tape(file_path);
310 //      drec->set_remote(true);
311 }
312
313 void VM::rec_tape(int drv, const _TCHAR* file_path)
314 {
315         drec->rec_tape(file_path);
316 //      drec->set_remote(true);
317 }
318
319 void VM::close_tape(int drv)
320 {
321         emu->lock_vm();
322         drec->close_tape();
323         emu->unlock_vm();
324 //      drec->set_remote(false);
325 }
326
327 bool VM::is_tape_inserted(int drv)
328 {
329         return drec->is_tape_inserted();
330 }
331
332 bool VM::is_tape_playing(int drv)
333 {
334         return drec->is_tape_playing();
335 }
336
337 bool VM::is_tape_recording(int drv)
338 {
339         return drec->is_tape_recording();
340 }
341
342 int VM::get_tape_position(int drv)
343 {
344         return drec->get_tape_position();
345 }
346
347 const _TCHAR* VM::get_tape_message(int drv)
348 {
349         return drec->get_message();
350 }
351
352 void VM::push_play(int drv)
353 {
354         drec->set_ff_rew(0);
355         drec->set_remote(true);
356 }
357
358 void VM::push_stop(int drv)
359 {
360         drec->set_remote(false);
361 }
362
363 void VM::push_fast_forward(int drv)
364 {
365         drec->set_ff_rew(1);
366         drec->set_remote(true);
367 }
368
369 void VM::push_fast_rewind(int drv)
370 {
371         drec->set_ff_rew(-1);
372         drec->set_remote(true);
373 }
374
375 bool VM::is_frame_skippable()
376 {
377         return event->is_frame_skippable();
378 }
379
380 void VM::update_config()
381 {
382         for(DEVICE* device = first_device; device; device = device->next_device) {
383                 device->update_config();
384         }
385 }
386