OSDN Git Service

36f580f6c58aa2dc593c01054ddb3291d07e7b98
[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 "../sn76489an.h"
21 #include "../315-5124.h"
22 #include "../upd765a.h"
23 #include "../z80.h"
24
25 #ifdef USE_DEBUGGER
26 #include "../debugger.h"
27 #endif
28
29 #include "keyboard.h"
30 #include "memory.h"
31 #include "system.h"
32
33 // ----------------------------------------------------------------------------
34 // initialize
35 // ----------------------------------------------------------------------------
36
37 VM::VM(EMU* parent_emu) : emu(parent_emu)
38 {
39         // create devices
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
43         
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);
53         
54         key = new KEYBOARD(this, emu);
55         memory = new MEMORY(this, emu);
56         system = new SYSTEM(this, emu);
57
58         // set contexts
59         event->set_context_cpu(cpu);
60         event->set_context_sound(psg);
61         event->set_context_sound(drec);
62         
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);
73         
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);
80
81         // cpu bus
82         cpu->set_context_mem(memory);
83         cpu->set_context_io(io);
84         cpu->set_context_intr(system);
85 #ifdef USE_DEBUGGER
86         cpu->set_context_debugger(new DEBUGGER(this, emu));
87 #endif
88
89         // i/o bus
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
101
102         // initialize all devices
103         for(DEVICE* device = first_device; device; device = device->next_device) {
104                 device->initialize();
105         }
106
107         // BIOS
108         memory->bios();
109
110         for(int i = 0; i < 4; i++) {
111                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
112         }
113 }
114
115 VM::~VM()
116 {
117         // delete all devices
118         for(DEVICE* device = first_device; device;) {
119                 DEVICE *next_device = device->next_device;
120                 device->release();
121                 delete device;
122                 device = next_device;
123         }
124 }
125
126 DEVICE* VM::get_device(int id)
127 {
128         for(DEVICE* device = first_device; device; device = device->next_device) {
129                 if(device->this_device_id == id) {
130                         return device;
131                 }
132         }
133         return NULL;
134 }
135
136 // ----------------------------------------------------------------------------
137 // drive virtual machine
138 // ----------------------------------------------------------------------------
139
140 void VM::reset()
141 {
142         // reset all devices
143         for(DEVICE* device = first_device; device; device = device->next_device) {
144                 device->reset();
145         }
146 }
147
148 void VM::run()
149 {
150         event->drive();
151 }
152
153 // ----------------------------------------------------------------------------
154 // debugger
155 // ----------------------------------------------------------------------------
156
157 #ifdef USE_DEBUGGER
158 DEVICE *VM::get_cpu(int index)
159 {
160         if(index == 0) {
161                 return cpu;
162         }
163         return NULL;
164 }
165 #endif
166
167 // ----------------------------------------------------------------------------
168 // draw screen
169 // ----------------------------------------------------------------------------
170
171 void VM::draw_screen()
172 {
173         vdp->draw_screen();
174 }
175
176 int VM::access_lamp()
177 {
178         uint32 status = fdc->read_signal(0);
179         return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
180 }
181
182 // ----------------------------------------------------------------------------
183 // soud manager
184 // ----------------------------------------------------------------------------
185
186 void VM::initialize_sound(int rate, int samples)
187 {
188         // init sound manager
189         event->initialize_sound(rate, samples);
190         
191         // init sound gen
192         psg->init(rate, 3579545, 4000);
193 }
194
195 uint16* VM::create_sound(int* extra_frames)
196 {
197         return event->create_sound(extra_frames);
198 }
199
200 int VM::sound_buffer_ptr()
201 {
202         return event->sound_buffer_ptr();
203 }
204
205 // ----------------------------------------------------------------------------
206 // user interface
207 // ----------------------------------------------------------------------------
208
209 void VM::open_cart(int drv, _TCHAR* file_path)
210 {
211         if(drv == 0) {
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);
217                                 memory->bios();
218                 } else {
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);
222                         else
223                                 vdp->set_console(0x20);
224                 }
225                 reset();
226         }
227 }
228
229 void VM::close_cart(int drv)
230 {
231         if(drv == 0) {
232                 memory->close_cart();
233                 reset();
234         }
235 }
236
237 bool VM::cart_inserted(int drv)
238 {
239         if(drv == 0) {
240                 return memory->cart_inserted();
241         } else {
242                 return false;
243         }
244 }
245
246 void VM::open_disk(int drv, _TCHAR* file_path, int bank)
247 {
248         fdc->open_disk(drv, file_path, bank);
249 }
250
251 void VM::close_disk(int drv)
252 {
253         fdc->close_disk(drv);
254 }
255
256 bool VM::disk_inserted(int drv)
257 {
258         return fdc->disk_inserted(drv);
259 }
260
261 void VM::set_disk_protected(int drv, bool value)
262 {
263         fdc->set_disk_protected(drv, value);
264 }
265
266 bool VM::get_disk_protected(int drv)
267 {
268         return fdc->get_disk_protected(drv);
269 }
270
271 void VM::play_tape(_TCHAR* file_path)
272 {
273         drec->play_tape(file_path);
274 }
275
276 void VM::rec_tape(_TCHAR* file_path)
277 {
278         drec->rec_tape(file_path);
279 }
280
281 void VM::close_tape()
282 {
283         drec->close_tape();
284 }
285
286 bool VM::tape_inserted()
287 {
288         return drec->tape_inserted();
289 }
290
291 bool VM::now_skip()
292 {
293         return event->now_skip();
294 }
295
296 void VM::update_config()
297 {
298         for(DEVICE* device = first_device; device; device = device->next_device) {
299                 device->update_config();
300         }
301 }
302