OSDN Git Service

[GENERAL] Merge upstream 2017-03-04. Still checking FTBFSs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / gamegear / mastersystem.cpp
1 /*
2         SEGA MASTER SYSTEM Emulator 'yaMASTER SYSTEM'
3
4         Author : tanam
5         Date   : 2013.10.20-
6
7         [ virtual machine ]
8 */
9
10 #include "mastersystem.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 "../ym2413.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         
45 #if defined(_USE_QT)
46         dummy->set_device_name(_T("1st Dummy"));
47         event->set_device_name(_T("EVENT"));
48 #endif  
49 ///     drec = new DATAREC(this, emu);
50 ///     sio = new I8251(this, emu);
51         pio_k = new I8255(this, emu);
52         pio_f = new I8255(this, emu);
53         io = new IO(this, emu);
54         psg = new SN76489AN(this, emu);
55         fm = new YM2413(this, emu);
56         vdp = new _315_5124(this, emu);
57 ///     fdc = new UPD765A(this, emu);
58         cpu = new Z80(this, emu);
59 #if defined(_USE_QT)
60         pio_k->set_device_name(_T("i8255(SG KEYBOARD)"));
61         pio_f->set_device_name(_T("i8255(SG FDD)"));
62         io->set_device_name(_T("I/O BUS"));
63         psg->set_device_name(_T("SN76489AN PSG"));
64         fm->set_device_name(_T("YM2413 OPL"));
65         vdp->set_device_name(_T("SEGA 315-5124 VDP"));
66         cpu->set_device_name(_T("CPU(Z80)"));
67 #endif
68         
69         key = new KEYBOARD(this, emu);
70         memory = new MEMORY(this, emu);
71         system = new SYSTEM(this, emu);
72 #if defined(_USE_QT)
73         key->set_device_name(_T("KEYBOARD"));
74         memory->set_device_name(_T("MEMORY"));
75         system->set_device_name(_T("SYSTEM"));
76 #endif
77
78         // set contexts
79         event->set_context_cpu(cpu);
80         event->set_context_sound(psg);
81         event->set_context_sound(fm);
82
83 ///     drec->set_context_ear(pio_k, SIG_I8255_PORT_B, 0x80);
84         pio_k->set_context_port_c(key, SIG_KEYBOARD_COLUMN, 0x07, 0);
85 ///     pio_k->set_context_port_c(drec, SIG_DATAREC_REMOTE, 0x08, 0);
86 ///     pio_k->set_context_port_c(drec, SIG_DATAREC_MIC, 0x10, 0);
87 ///     pio_f->set_context_port_c(fdc, SIG_UPD765A_MOTOR_NEG, 2, 0);
88 ///     pio_f->set_context_port_c(fdc, SIG_UPD765A_TC, 4, 0);
89 ///     pio_f->set_context_port_c(fdc, SIG_UPD765A_RESET, 8, 0);
90         pio_f->set_context_port_c(memory, SIG_MEMORY_SEL, 0x40, 0);
91         vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
92 ///     fdc->set_context_irq(pio_f, SIG_I8255_PORT_A, 1);
93 ///     fdc->set_context_index(pio_f, SIG_I8255_PORT_A, 4);
94         
95         key->set_context_cpu(cpu);
96         key->set_context_pio(pio_k);
97         system->set_context_key(key);
98         vdp->set_context_key(key);
99         vdp->set_context_psg(psg);
100 ///     vdp->set_context_cpu(cpu);
101
102         // cpu bus
103         cpu->set_context_mem(memory);
104         cpu->set_context_io(io);
105         cpu->set_context_intr(system);
106 #ifdef USE_DEBUGGER
107         cpu->set_context_debugger(new DEBUGGER(this, emu));
108 #endif
109
110         // i/o bus
111         io->set_iomap_single_r(0x00, system);           // GG  START
112         io->set_iomap_single_w(0x80, system);           // COL TENKEY
113         io->set_iomap_single_w(0xc0, system);           // COL JOYPAD
114         io->set_iomap_range_rw(0xfc, 0xfe, system);     // COL JOYPAD
115         io->set_iomap_range_rw(0xff, 0xff, psg);        // COL PSG
116         io->set_iomap_range_rw(0x7e, 0x7f, vdp);        // SG  VDP
117         io->set_iomap_range_rw(0xbe, 0xbf, vdp);        // SG  VDP
118         io->set_iomap_range_rw(0xdc, 0xdf, pio_k);      // SG  KEY
119 ///     io->set_iomap_range_rw(0xe0, 0xe3, fdc);        // SG  FDD
120 ///     io->set_iomap_range_rw(0xe4, 0xe7, pio_f);      // SG  FDD
121 ///     io->set_iomap_range_rw(0xe8, 0xe9, sio);        // SG  SERIAL
122         io->set_iomap_range_rw(0xf0, 0xf2, fm);         // MS  FM
123
124         // initialize all devices
125         for(DEVICE* device = first_device; device; device = device->next_device) {
126                 device->initialize();
127         }
128
129         // BIOS
130 ///     memory->bios();
131         memory->open_cart(create_local_path(_T("SMS.ROM")));
132 ///     for(int i = 0; i < 4; i++) {
133 ///             fdc->set_drive_type(i, DRIVE_TYPE_2D);
134 ///     }
135 }
136
137 VM::~VM()
138 {
139         // delete all devices
140         for(DEVICE* device = first_device; device;) {
141                 DEVICE *next_device = device->next_device;
142                 device->release();
143                 delete device;
144                 device = next_device;
145         }
146 }
147
148 DEVICE* VM::get_device(int id)
149 {
150         for(DEVICE* device = first_device; device; device = device->next_device) {
151                 if(device->this_device_id == id) {
152                         return device;
153                 }
154         }
155         return NULL;
156 }
157
158 // ----------------------------------------------------------------------------
159 // drive virtual machine
160 // ----------------------------------------------------------------------------
161
162 void VM::reset()
163 {
164         // reset all devices
165         for(DEVICE* device = first_device; device; device = device->next_device) {
166                 device->reset();
167         }
168 }
169
170 void VM::run()
171 {
172         event->drive();
173 }
174
175 // ----------------------------------------------------------------------------
176 // debugger
177 // ----------------------------------------------------------------------------
178
179 #ifdef USE_DEBUGGER
180 DEVICE *VM::get_cpu(int index)
181 {
182         if(index == 0) {
183                 return cpu;
184         }
185         return NULL;
186 }
187 #endif
188
189 // ----------------------------------------------------------------------------
190 // draw screen
191 // ----------------------------------------------------------------------------
192
193 void VM::draw_screen()
194 {
195         vdp->draw_screen();
196 }
197
198 ///uint32_t VM::get_access_lamp_status()
199 ///{
200 ///     uint32_t status = fdc->read_signal(0);
201 ///     return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
202 ///}
203
204 // ----------------------------------------------------------------------------
205 // soud manager
206 // ----------------------------------------------------------------------------
207
208 void VM::initialize_sound(int rate, int samples)
209 {
210         // init sound manager
211         event->initialize_sound(rate, samples);
212         
213         // init sound gen
214         psg->initialize_sound(rate, 3579545, 4000);
215         fm->initialize_sound(rate, 3579545, samples);
216 }
217
218 uint16_t* VM::create_sound(int* extra_frames)
219 {
220         return event->create_sound(extra_frames);
221 }
222
223 int VM::get_sound_buffer_ptr()
224 {
225         return event->get_sound_buffer_ptr();
226 }
227
228 #ifdef USE_SOUND_VOLUME
229 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
230 {
231         if(ch == 0) {
232                 psg->set_volume(0, decibel_l, decibel_r);
233         } else if(ch == 1) {
234                 fm->set_volume(0, decibel_l, decibel_r);
235         }
236 }
237 #endif
238
239 // ----------------------------------------------------------------------------
240 // user interface
241 // ----------------------------------------------------------------------------
242
243 void VM::open_cart(int drv, const _TCHAR* file_path)
244 {
245         if(drv == 0) {
246                 memory->open_cart(file_path);
247                 if (check_file_extension(file_path, _T(".col"))) {
248                         vdp->set_context_irq(cpu, SIG_CPU_NMI, 1);
249                         vdp->set_console(0x00);
250                         memory->bios();
251                 } else {
252                         vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
253                         if (check_file_extension(file_path, _T(".gg"))) {
254                                 vdp->set_console(0x40);
255                         } else {
256                                 vdp->set_console(0x20);
257                         }
258                 }
259                 reset();
260         }
261 }
262
263 void VM::close_cart(int drv)
264 {
265         if(drv == 0) {
266                 memory->close_cart();
267                 reset();
268         }
269 }
270
271 bool VM::is_cart_inserted(int drv)
272 {
273         if(drv == 0) {
274                 return memory->is_cart_inserted();
275         } else {
276                 return false;
277         }
278 }
279
280 ///void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
281 ///{
282 ///     fdc->open_disk(drv, file_path, bank);
283 ///}
284
285 ///void VM::close_floppy_disk(int drv)
286 ///{
287 ///     fdc->close_disk(drv);
288 ///}
289
290 ///bool VM::is_floppy_disk_inserted(int drv)
291 ///{
292 ///     return fdc->is_disk_inserted(drv);
293 ///}
294
295 ///void VM::is_floppy_disk_protected(int drv, bool value)
296 ///{
297 ///     fdc->is_disk_protected(drv, value);
298 ///}
299
300 ///bool VM::is_floppy_disk_protected(int drv)
301 ///{
302 ///     return fdc->is_disk_protected(drv);
303 ///}
304
305 ///void VM::play_tape(const _TCHAR* file_path)
306 ///{
307 ///     drec->play_tape(file_path);
308 ///}
309
310 ///void VM::rec_tape(const _TCHAR* file_path)
311 ///{
312 ///     drec->rec_tape(file_path);
313 ///}
314
315 ///void VM::close_tape()
316 ///{
317 ///     emu->lock_vm();
318 ///     drec->close_tape();
319 ///     emu->unlock_vm();
320 ///}
321
322 ///bool VM::is_tape_inserted()
323 ///{
324 ///     return drec->is_tape_inserted();
325 ///}
326
327 ///bool VM::is_tape_playing()
328 ///{
329 ///     return drec->is_tape_playing();
330 ///}
331
332 ///bool VM::is_tape_recording()
333 ///{
334 ///     return drec->is_tape_recording();
335 ///}
336
337 ///int VM::get_tape_position()
338 ///{
339 ///     return drec->get_tape_position();
340 ///}
341
342 bool VM::is_frame_skippable()
343 {
344         return event->is_frame_skippable();
345 }
346
347 void VM::update_config()
348 {
349         for(DEVICE* device = first_device; device; device = device->next_device) {
350                 device->update_config();
351         }
352 }