OSDN Git Service

[VM][Qt][OSD][Genaral] Sync to upstream ; phase 1: Merege upstream 20151217.
[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 ///     drec = new DATAREC(this, emu);
46 ///     sio = new I8251(this, emu);
47         pio_k = new I8255(this, emu);
48         pio_f = new I8255(this, emu);
49         io = new IO(this, emu);
50         psg = new SN76489AN(this, emu);
51         fm = new YM2413(this, emu);
52         vdp = new _315_5124(this, emu);
53 ///     fdc = new UPD765A(this, emu);
54         cpu = new Z80(this, emu);
55         
56         key = new KEYBOARD(this, emu);
57         memory = new MEMORY(this, emu);
58         system = new SYSTEM(this, emu);
59
60         // set contexts
61         event->set_context_cpu(cpu);
62         event->set_context_sound(psg);
63         event->set_context_sound(fm);
64
65 ///     drec->set_context_ear(pio_k, SIG_I8255_PORT_B, 0x80);
66         pio_k->set_context_port_c(key, SIG_KEYBOARD_COLUMN, 0x07, 0);
67 ///     pio_k->set_context_port_c(drec, SIG_DATAREC_REMOTE, 0x08, 0);
68 ///     pio_k->set_context_port_c(drec, SIG_DATAREC_MIC, 0x10, 0);
69 ///     pio_f->set_context_port_c(fdc, SIG_UPD765A_MOTOR_NEG, 2, 0);
70 ///     pio_f->set_context_port_c(fdc, SIG_UPD765A_TC, 4, 0);
71 ///     pio_f->set_context_port_c(fdc, SIG_UPD765A_RESET, 8, 0);
72         pio_f->set_context_port_c(memory, SIG_MEMORY_SEL, 0x40, 0);
73         vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
74 ///     fdc->set_context_irq(pio_f, SIG_I8255_PORT_A, 1);
75 ///     fdc->set_context_index(pio_f, SIG_I8255_PORT_A, 4);
76         
77         key->set_context_cpu(cpu);
78         key->set_context_pio(pio_k);
79         system->set_context_key(key);
80         vdp->set_context_key(key);
81         vdp->set_context_psg(psg);
82 ///     vdp->set_context_cpu(cpu);
83
84         // cpu bus
85         cpu->set_context_mem(memory);
86         cpu->set_context_io(io);
87         cpu->set_context_intr(system);
88 #ifdef USE_DEBUGGER
89         cpu->set_context_debugger(new DEBUGGER(this, emu));
90 #endif
91
92         // i/o bus
93         io->set_iomap_single_r(0x00, system);           // GG  START
94         io->set_iomap_single_w(0x80, system);           // COL TENKEY
95         io->set_iomap_single_w(0xc0, system);           // COL JOYPAD
96         io->set_iomap_range_rw(0xfc, 0xfe, system);     // COL JOYPAD
97         io->set_iomap_range_rw(0xff, 0xff, psg);        // COL PSG
98         io->set_iomap_range_rw(0x7e, 0x7f, vdp);        // SG  VDP
99         io->set_iomap_range_rw(0xbe, 0xbf, vdp);        // SG  VDP
100         io->set_iomap_range_rw(0xdc, 0xdf, pio_k);      // SG  KEY
101 ///     io->set_iomap_range_rw(0xe0, 0xe3, fdc);        // SG  FDD
102 ///     io->set_iomap_range_rw(0xe4, 0xe7, pio_f);      // SG  FDD
103 ///     io->set_iomap_range_rw(0xe8, 0xe9, sio);        // SG  SERIAL
104         io->set_iomap_range_rw(0xf0, 0xf2, fm);         // MS  FM
105
106         // initialize all devices
107         for(DEVICE* device = first_device; device; device = device->next_device) {
108                 device->initialize();
109         }
110
111         // BIOS
112 ///     memory->bios();
113         memory->open_cart(create_local_path(_T("SMS.ROM")));
114 ///     for(int i = 0; i < 4; i++) {
115 ///             fdc->set_drive_type(i, DRIVE_TYPE_2D);
116 ///     }
117 }
118
119 VM::~VM()
120 {
121         // delete all devices
122         for(DEVICE* device = first_device; device;) {
123                 DEVICE *next_device = device->next_device;
124                 device->release();
125                 delete device;
126                 device = next_device;
127         }
128 }
129
130 DEVICE* VM::get_device(int id)
131 {
132         for(DEVICE* device = first_device; device; device = device->next_device) {
133                 if(device->this_device_id == id) {
134                         return device;
135                 }
136         }
137         return NULL;
138 }
139
140 // ----------------------------------------------------------------------------
141 // drive virtual machine
142 // ----------------------------------------------------------------------------
143
144 void VM::reset()
145 {
146         // reset all devices
147         for(DEVICE* device = first_device; device; device = device->next_device) {
148                 device->reset();
149         }
150 }
151
152 void VM::run()
153 {
154         event->drive();
155 }
156
157 // ----------------------------------------------------------------------------
158 // debugger
159 // ----------------------------------------------------------------------------
160
161 #ifdef USE_DEBUGGER
162 DEVICE *VM::get_cpu(int index)
163 {
164         if(index == 0) {
165                 return cpu;
166         }
167         return NULL;
168 }
169 #endif
170
171 // ----------------------------------------------------------------------------
172 // draw screen
173 // ----------------------------------------------------------------------------
174
175 void VM::draw_screen()
176 {
177         vdp->draw_screen();
178 }
179
180 ///int VM::access_lamp()
181 ///{
182 ///     uint32 status = fdc->read_signal(0);
183 ///     return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
184 ///}
185
186 // ----------------------------------------------------------------------------
187 // soud manager
188 // ----------------------------------------------------------------------------
189
190 void VM::initialize_sound(int rate, int samples)
191 {
192         // init sound manager
193         event->initialize_sound(rate, samples);
194         
195         // init sound gen
196         psg->init(rate, 3579545, 4000);
197         fm->init(rate, 3579545, samples);
198 }
199
200 uint16* VM::create_sound(int* extra_frames)
201 {
202         return event->create_sound(extra_frames);
203 }
204
205 int VM::sound_buffer_ptr()
206 {
207         return event->sound_buffer_ptr();
208 }
209
210 // ----------------------------------------------------------------------------
211 // user interface
212 // ----------------------------------------------------------------------------
213
214 void VM::open_cart(int drv, const _TCHAR* file_path)
215 {
216         if(drv == 0) {
217                 memory->open_cart(file_path);
218                 if (strstr(file_path, ".col") || 
219                         strstr(file_path, ".COL")) {
220                                 vdp->set_context_irq(cpu, SIG_CPU_NMI, 1);
221                                 vdp->set_console(0x00);
222                                 memory->bios();
223                 } else {
224                         vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
225                         if (strstr(file_path, ".gg") || 
226                                 strstr(file_path, ".GG")) vdp->set_console(0x40);
227                         else
228                                 vdp->set_console(0x20);
229                 }
230                 reset();
231         }
232 }
233
234 void VM::close_cart(int drv)
235 {
236         if(drv == 0) {
237                 memory->close_cart();
238                 reset();
239         }
240 }
241
242 bool VM::cart_inserted(int drv)
243 {
244         if(drv == 0) {
245                 return memory->cart_inserted();
246         } else {
247                 return false;
248         }
249 }
250
251 ///void VM::open_disk(int drv, const _TCHAR* file_path, int bank)
252 ///{
253 ///     fdc->open_disk(drv, file_path, bank);
254 ///}
255
256 ///void VM::close_disk(int drv)
257 ///{
258 ///     fdc->close_disk(drv);
259 ///}
260
261 ///bool VM::disk_inserted(int drv)
262 ///{
263 ///     return fdc->disk_inserted(drv);
264 ///}
265
266 ///void VM::set_disk_protected(int drv, bool value)
267 ///{
268 ///     fdc->set_disk_protected(drv, value);
269 ///}
270
271 ///bool VM::get_disk_protected(int drv)
272 ///{
273 ///     return fdc->get_disk_protected(drv);
274 ///}
275
276 ///void VM::play_tape(const _TCHAR* file_path)
277 ///{
278 ///     drec->play_tape(file_path);
279 ///}
280
281 ///void VM::rec_tape(const _TCHAR* file_path)
282 ///{
283 ///     drec->rec_tape(file_path);
284 ///}
285
286 ///void VM::close_tape()
287 ///{
288 ///     drec->close_tape();
289 ///}
290
291 ///bool VM::tape_inserted()
292 ///{
293 ///     return drec->tape_inserted();
294 ///}
295
296 ///bool VM::tape_playing()
297 ///{
298 ///     return drec->tape_playing();
299 ///}
300
301 ///bool VM::tape_recording()
302 ///{
303 ///     return drec->tape_recording();
304 ///}
305
306 ///int VM::tape_position()
307 ///{
308 ///     return drec->tape_position();
309 ///}
310
311 bool VM::now_skip()
312 {
313         return event->now_skip();
314 }
315
316 void VM::update_config()
317 {
318         for(DEVICE* device = first_device; device; device = device->next_device) {
319                 device->update_config();
320         }
321 }