OSDN Git Service

0404feb4ac7bdf7a0f22c8642710104223d65fa4
[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 "../noise.h"
21 #include "../ym2413.h"
22 #include "../sn76489an.h"
23 #include "../315-5124.h"
24 ///#include "../upd765a.h"
25 #include "../z80.h"
26
27 #ifdef USE_DEBUGGER
28 #include "../debugger.h"
29 #endif
30
31 #include "keyboard.h"
32 #include "memory.h"
33 #include "system.h"
34
35 // ----------------------------------------------------------------------------
36 // initialize
37 // ----------------------------------------------------------------------------
38
39 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
40 {
41         // create devices
42         first_device = last_device = NULL;
43         dummy = new DEVICE(this, emu);  // must be 1st device
44         event = new EVENT(this, emu);   // must be 2nd device
45         dummy->set_device_name(_T("1st Dummy"));
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         fm = new YM2413(this, emu);
58         vdp = new _315_5124(this, emu);
59 ///     fdc = new UPD765A(this, emu);
60 ///     fdc->set_context_noise_seek(new NOISE(this, emu));
61 ///     fdc->set_context_noise_head_down(new NOISE(this, emu));
62 //      fdc->set_context_noise_head_up(new NOISE(this, emu));
63         cpu = new Z80(this, emu);
64         
65         key = new KEYBOARD(this, emu);
66         memory = new MEMORY(this, emu);
67         system = new SYSTEM(this, emu);
68
69         // set contexts
70         event->set_context_cpu(cpu);
71         event->set_context_sound(psg);
72         event->set_context_sound(fm);
73 ///     event->set_context_sound(fdc->get_context_noise_seek());
74 ///     event->set_context_sound(fdc->get_context_noise_head_down());
75 ///     event->set_context_sound(fdc->get_context_noise_head_up());
76 ///     event->set_context_sound(drec->get_context_noise_play());
77 ///     event->set_context_sound(drec->get_context_noise_stop());
78 ///     event->set_context_sound(drec->get_context_noise_fast());
79         
80
81 ///     drec->set_context_ear(pio_k, SIG_I8255_PORT_B, 0x80);
82         pio_k->set_context_port_c(key, SIG_KEYBOARD_COLUMN, 0x07, 0);
83 ///     pio_k->set_context_port_c(drec, SIG_DATAREC_REMOTE, 0x08, 0);
84 ///     pio_k->set_context_port_c(drec, SIG_DATAREC_MIC, 0x10, 0);
85 ///     pio_f->set_context_port_c(fdc, SIG_UPD765A_MOTOR_NEG, 2, 0);
86 ///     pio_f->set_context_port_c(fdc, SIG_UPD765A_TC, 4, 0);
87 ///     pio_f->set_context_port_c(fdc, SIG_UPD765A_RESET, 8, 0);
88         pio_f->set_context_port_c(memory, SIG_MEMORY_SEL, 0x40, 0);
89         vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
90 ///     fdc->set_context_irq(pio_f, SIG_I8255_PORT_A, 1);
91 ///     fdc->set_context_index(pio_f, SIG_I8255_PORT_A, 4);
92         
93         key->set_context_cpu(cpu);
94         key->set_context_pio(pio_k);
95         system->set_context_key(key);
96         vdp->set_context_key(key);
97         vdp->set_context_psg(psg);
98 ///     vdp->set_context_cpu(cpu);
99         
100         // cpu bus
101         cpu->set_context_mem(memory);
102         cpu->set_context_io(io);
103         cpu->set_context_intr(system);
104 #ifdef USE_DEBUGGER
105         cpu->set_context_debugger(new DEBUGGER(this, emu));
106 #endif
107         
108         // i/o bus
109         io->set_iomap_single_r(0x00, system);           // GG  START
110         io->set_iomap_single_w(0x80, system);           // COL TENKEY
111         io->set_iomap_single_w(0xc0, system);           // COL JOYPAD
112         io->set_iomap_range_rw(0xfc, 0xfe, system);     // COL JOYPAD
113         io->set_iomap_range_rw(0xff, 0xff, psg);        // COL PSG
114         io->set_iomap_range_rw(0x7e, 0x7f, vdp);        // SG  VDP
115         io->set_iomap_range_rw(0xbe, 0xbf, vdp);        // SG  VDP
116         io->set_iomap_range_rw(0xdc, 0xdf, pio_k);      // SG  KEY
117 ///     io->set_iomap_range_rw(0xe0, 0xe3, fdc);        // SG  FDD
118 ///     io->set_iomap_range_rw(0xe4, 0xe7, pio_f);      // SG  FDD
119 ///     io->set_iomap_range_rw(0xe8, 0xe9, sio);        // SG  SERIAL
120         io->set_iomap_range_rw(0xf0, 0xf2, fm);         // MS  FM
121         
122         // initialize all devices
123         for(DEVICE* device = first_device; device; device = device->next_device) {
124                 device->initialize();
125         }
126         
127         // BIOS
128 ///     memory->bios();
129         memory->open_cart(create_local_path(_T("SMS.ROM")));
130 ///     for(int i = 0; i < 4; i++) {
131 ///             fdc->set_drive_type(i, DRIVE_TYPE_2D);
132 ///     }
133 }
134
135 VM::~VM()
136 {
137         // delete all devices
138         for(DEVICE* device = first_device; device;) {
139                 DEVICE *next_device = device->next_device;
140                 device->release();
141                 delete device;
142                 device = next_device;
143         }
144 }
145
146 DEVICE* VM::get_device(int id)
147 {
148         for(DEVICE* device = first_device; device; device = device->next_device) {
149                 if(device->this_device_id == id) {
150                         return device;
151                 }
152         }
153         return NULL;
154 }
155
156 // ----------------------------------------------------------------------------
157 // drive virtual machine
158 // ----------------------------------------------------------------------------
159
160 void VM::reset()
161 {
162         // reset all devices
163         for(DEVICE* device = first_device; device; device = device->next_device) {
164                 device->reset();
165         }
166 }
167
168 void VM::run()
169 {
170         event->drive();
171 }
172
173 // ----------------------------------------------------------------------------
174 // debugger
175 // ----------------------------------------------------------------------------
176
177 #ifdef USE_DEBUGGER
178 DEVICE *VM::get_cpu(int index)
179 {
180         if(index == 0) {
181                 return cpu;
182         }
183         return NULL;
184 }
185 #endif
186
187 // ----------------------------------------------------------------------------
188 // draw screen
189 // ----------------------------------------------------------------------------
190
191 void VM::draw_screen()
192 {
193         vdp->draw_screen();
194 }
195
196 // ----------------------------------------------------------------------------
197 // soud manager
198 // ----------------------------------------------------------------------------
199
200 void VM::initialize_sound(int rate, int samples)
201 {
202         // init sound manager
203         event->initialize_sound(rate, samples);
204         
205         // init sound gen
206         psg->initialize_sound(rate, 3579545, 4000);
207         fm->initialize_sound(rate, 3579545, samples);
208 }
209
210 uint16_t* VM::create_sound(int* extra_frames)
211 {
212         return event->create_sound(extra_frames);
213 }
214
215 int VM::get_sound_buffer_ptr()
216 {
217         return event->get_sound_buffer_ptr();
218 }
219
220 #ifdef USE_SOUND_VOLUME
221 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
222 {
223         if(ch == 0) {
224                 psg->set_volume(0, decibel_l, decibel_r);
225         } else if(ch == 1) {
226                 fm->set_volume(0, decibel_l, decibel_r);
227 ///     } else if(ch == 2) {
228 ///             fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
229 ///             fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
230 ///             fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
231 ///     } else if(ch == 3) {
232 ///             drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
233 ///             drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
234 ///             drec->get_context_noise_fast()->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 ///uint32_t VM::is_floppy_disk_accessed()
306 ///{
307 ///     return fdc->read_signal(0);
308 ///}
309
310 ///void VM::play_tape(int drv, const _TCHAR* file_path)
311 ///{
312 ///     drec->play_tape(file_path);
313 ///     drec->set_remote(true);
314 ///}
315
316 ///void VM::rec_tape(int drv, const _TCHAR* file_path)
317 ///{
318 ///     drec->rec_tape(file_path);
319 ///     drec->set_remote(true);
320 ///}
321
322 ///void VM::close_tape(int drv)
323 ///{
324 ///     emu->lock_vm();
325 ///     drec->close_tape();
326 ///     emu->unlock_vm();
327 ///     drec->set_remote(false);
328 ///}
329
330 ///bool VM::is_tape_inserted(int drv)
331 ///{
332 ///     return drec->is_tape_inserted();
333 ///}
334
335 ///bool VM::is_tape_playing(int drv)
336 ///{
337 ///     return drec->is_tape_playing();
338 ///}
339
340 ///bool VM::is_tape_recording(int drv)
341 ///{
342 ///     return drec->is_tape_recording();
343 ///}
344
345 ///int VM::get_tape_position(int drv)
346 ///{
347 ///     return drec->get_tape_position();
348 ///}
349
350 ///const _TCHAR* VM::get_tape_message(int drv)
351 ///{
352 ///     return drec->get_message();
353 ///}
354
355 ///void VM::push_play(int drv)
356 ///{
357 ///     drec->set_ff_rew(0);
358 ///     drec->set_remote(true);
359 ///}
360
361 ///void VM::push_stop(int drv)
362 ///{
363 ///     drec->set_remote(false);
364 ///}
365
366 ///void VM::push_fast_forward(int drv)
367 ///{
368 ///     drec->set_ff_rew(1);
369 ///     drec->set_remote(true);
370 ///}
371
372 ///void VM::push_fast_rewind(int drv)
373 ///{
374 ///     drec->set_ff_rew(-1);
375 ///     drec->set_remote(true);
376 ///}
377
378 bool VM::is_frame_skippable()
379 {
380         return event->is_frame_skippable();
381 }
382
383 void VM::update_config()
384 {
385         for(DEVICE* device = first_device; device; device = device->next_device) {
386                 device->update_config();
387         }
388 }