2 SEGA MASTER SYSTEM Emulator 'yaMASTER SYSTEM'
10 #include "mastersystem.h"
11 #include "../../emu.h"
12 #include "../device.h"
15 ///#include "../datarec.h"
16 ///#include "../disk.h"
17 ///#include "../i8251.h"
20 ///#include "../noise.h"
21 #include "../ym2413.h"
22 #include "../sn76489an.h"
23 #include "../315-5124.h"
24 ///#include "../upd765a.h"
28 #include "../debugger.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 using GAMEGEAR::KEYBOARD;
40 using GAMEGEAR::MEMORY;
41 using GAMEGEAR::SYSTEM;
43 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
46 first_device = last_device = NULL;
47 dummy = new DEVICE(this, emu); // must be 1st device
48 event = new EVENT(this, emu); // must be 2nd device
49 dummy->set_device_name(_T("1st Dummy"));
50 /// drec = new DATAREC(this, emu);
51 /// drec->set_context_noise_play(new NOISE(this, emu));
52 /// drec->set_context_noise_stop(new NOISE(this, emu));
53 /// drec->set_context_noise_fast(new NOISE(this, emu));
54 /// sio = new I8251(this, emu);
55 pio_k = new I8255(this, emu);
56 pio_k->set_device_name(_T("8255 PIO (Keyboard)"));
57 pio_f = new I8255(this, emu);
58 pio_f->set_device_name(_T("8255 PIO (Floppy I/F)"));
59 io = new IO(this, emu);
60 psg = new SN76489AN(this, emu);
61 fm = new YM2413(this, emu);
62 vdp = new _315_5124(this, emu);
63 /// fdc = new UPD765A(this, emu);
64 /// fdc->set_context_noise_seek(new NOISE(this, emu));
65 /// fdc->set_context_noise_head_down(new NOISE(this, emu));
66 // fdc->set_context_noise_head_up(new NOISE(this, emu));
67 cpu = new Z80(this, emu);
69 key = new KEYBOARD(this, emu);
70 memory = new MEMORY(this, emu);
71 system = new SYSTEM(this, emu);
74 event->set_context_cpu(cpu);
75 event->set_context_sound(psg);
76 event->set_context_sound(fm);
77 /// event->set_context_sound(fdc->get_context_noise_seek());
78 /// event->set_context_sound(fdc->get_context_noise_head_down());
79 /// event->set_context_sound(fdc->get_context_noise_head_up());
80 /// event->set_context_sound(drec->get_context_noise_play());
81 /// event->set_context_sound(drec->get_context_noise_stop());
82 /// event->set_context_sound(drec->get_context_noise_fast());
85 /// drec->set_context_ear(pio_k, SIG_I8255_PORT_B, 0x80);
86 pio_k->set_context_port_c(key, SIG_KEYBOARD_COLUMN, 0x07, 0);
87 /// pio_k->set_context_port_c(drec, SIG_DATAREC_REMOTE, 0x08, 0);
88 /// pio_k->set_context_port_c(drec, SIG_DATAREC_MIC, 0x10, 0);
89 /// pio_f->set_context_port_c(fdc, SIG_UPD765A_MOTOR_NEG, 2, 0);
90 /// pio_f->set_context_port_c(fdc, SIG_UPD765A_TC, 4, 0);
91 /// pio_f->set_context_port_c(fdc, SIG_UPD765A_RESET, 8, 0);
92 pio_f->set_context_port_c(memory, SIG_MEMORY_SEL, 0x40, 0);
93 vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
94 /// fdc->set_context_irq(pio_f, SIG_I8255_PORT_A, 1);
95 /// fdc->set_context_index(pio_f, SIG_I8255_PORT_A, 4);
97 key->set_context_cpu(cpu);
98 key->set_context_pio(pio_k);
99 system->set_context_key(key);
100 vdp->set_context_key(key);
101 vdp->set_context_psg(psg);
102 /// vdp->set_context_cpu(cpu);
105 cpu->set_context_mem(memory);
106 cpu->set_context_io(io);
107 cpu->set_context_intr(system);
109 cpu->set_context_debugger(new DEBUGGER(this, emu));
113 io->set_iomap_single_r(0x00, system); // GG START
114 io->set_iomap_single_w(0x80, system); // COL TENKEY
115 io->set_iomap_single_w(0xc0, system); // COL JOYPAD
116 io->set_iomap_range_rw(0xfc, 0xfe, system); // COL JOYPAD
117 io->set_iomap_range_rw(0xff, 0xff, psg); // COL PSG
118 io->set_iomap_range_rw(0x7e, 0x7f, vdp); // SG VDP
119 io->set_iomap_range_rw(0xbe, 0xbf, vdp); // SG VDP
120 io->set_iomap_range_rw(0xdc, 0xdf, pio_k); // SG KEY
121 /// io->set_iomap_range_rw(0xe0, 0xe3, fdc); // SG FDD
122 /// io->set_iomap_range_rw(0xe4, 0xe7, pio_f); // SG FDD
123 /// io->set_iomap_range_rw(0xe8, 0xe9, sio); // SG SERIAL
124 io->set_iomap_range_rw(0xf0, 0xf2, fm); // MS FM
126 // initialize all devices
127 #if defined(__GIT_REPO_VERSION)
128 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
130 for(DEVICE* device = first_device; device; device = device->next_device) {
131 device->initialize();
136 memory->open_cart(create_local_path(_T("SMS.ROM")));
137 /// for(int i = 0; i < 4; i++) {
138 /// fdc->set_drive_type(i, DRIVE_TYPE_2D);
144 // delete all devices
145 for(DEVICE* device = first_device; device;) {
146 DEVICE *next_device = device->next_device;
149 device = next_device;
153 DEVICE* VM::get_device(int id)
155 for(DEVICE* device = first_device; device; device = device->next_device) {
156 if(device->this_device_id == id) {
163 // ----------------------------------------------------------------------------
164 // drive virtual machine
165 // ----------------------------------------------------------------------------
170 for(DEVICE* device = first_device; device; device = device->next_device) {
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
185 DEVICE *VM::get_cpu(int index)
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 void VM::draw_screen()
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
207 void VM::initialize_sound(int rate, int samples)
209 // init sound manager
210 event->initialize_sound(rate, samples);
213 psg->initialize_sound(rate, 3579545, 4000);
214 fm->initialize_sound(rate, 3579545, samples);
217 uint16_t* VM::create_sound(int* extra_frames)
219 return event->create_sound(extra_frames);
222 int VM::get_sound_buffer_ptr()
224 return event->get_sound_buffer_ptr();
227 #ifdef USE_SOUND_VOLUME
228 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
231 psg->set_volume(0, decibel_l, decibel_r);
233 fm->set_volume(0, decibel_l, decibel_r);
234 /// } else if(ch == 2) {
235 /// fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
236 /// fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
237 /// fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
238 /// } else if(ch == 3) {
239 /// drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
240 /// drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
241 /// drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
246 // ----------------------------------------------------------------------------
248 // ----------------------------------------------------------------------------
250 void VM::open_cart(int drv, const _TCHAR* file_path)
253 memory->open_cart(file_path);
254 if (check_file_extension(file_path, _T(".col"))) {
255 vdp->set_context_irq(cpu, SIG_CPU_NMI, 1);
256 vdp->set_console(0x00);
259 vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
260 if (check_file_extension(file_path, _T(".gg"))) {
261 vdp->set_console(0x40);
263 vdp->set_console(0x20);
270 void VM::close_cart(int drv)
273 memory->close_cart();
278 bool VM::is_cart_inserted(int drv)
281 return memory->is_cart_inserted();
287 ///void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
289 /// fdc->open_disk(drv, file_path, bank);
292 ///void VM::close_floppy_disk(int drv)
294 /// fdc->close_disk(drv);
297 ///bool VM::is_floppy_disk_inserted(int drv)
299 /// return fdc->is_disk_inserted(drv);
302 ///void VM::is_floppy_disk_protected(int drv, bool value)
304 /// fdc->is_disk_protected(drv, value);
307 ///bool VM::is_floppy_disk_protected(int drv)
309 /// return fdc->is_disk_protected(drv);
312 ///uint32_t VM::is_floppy_disk_accessed()
314 /// return fdc->read_signal(0);
317 ///void VM::play_tape(int drv, const _TCHAR* file_path)
319 /// drec->play_tape(file_path);
320 /// drec->set_remote(true);
323 ///void VM::rec_tape(int drv, const _TCHAR* file_path)
325 /// drec->rec_tape(file_path);
326 /// drec->set_remote(true);
329 ///void VM::close_tape(int drv)
332 /// drec->close_tape();
333 /// emu->unlock_vm();
334 /// drec->set_remote(false);
337 ///bool VM::is_tape_inserted(int drv)
339 /// return drec->is_tape_inserted();
342 ///bool VM::is_tape_playing(int drv)
344 /// return drec->is_tape_playing();
347 ///bool VM::is_tape_recording(int drv)
349 /// return drec->is_tape_recording();
352 ///int VM::get_tape_position(int drv)
354 /// return drec->get_tape_position();
357 ///const _TCHAR* VM::get_tape_message(int drv)
359 /// return drec->get_message();
362 ///void VM::push_play(int drv)
364 /// drec->set_ff_rew(0);
365 /// drec->set_remote(true);
368 ///void VM::push_stop(int drv)
370 /// drec->set_remote(false);
373 ///void VM::push_fast_forward(int drv)
375 /// drec->set_ff_rew(1);
376 /// drec->set_remote(true);
379 ///void VM::push_fast_rewind(int drv)
381 /// drec->set_ff_rew(-1);
382 /// drec->set_remote(true);
385 bool VM::is_frame_skippable()
387 return event->is_frame_skippable();
390 void VM::update_config()
392 for(DEVICE* device = first_device; device; device = device->next_device) {
393 device->update_config();
397 #define STATE_VERSION 1
399 bool VM::process_state(FILEIO* state_fio, bool loading)
401 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
404 for(DEVICE* device = first_device; device; device = device->next_device) {
405 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
406 // const char *name = typeid(*device).name();
407 // But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
408 const char *name = device->get_device_name();
409 int len = strlen(name);
411 if(!state_fio->StateCheckInt32(len)) {
413 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
417 if(!state_fio->StateCheckBuffer(name, len, 1)) {
419 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
423 if(!device->process_state(state_fio, loading)) {
425 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
430 // Machine specified.