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);
62 psg->set_context_debugger(new DEBUGGER(this, emu));
64 fm = new YM2413(this, emu);
65 vdp = new _315_5124(this, emu);
66 /// fdc = new UPD765A(this, emu);
67 /// fdc->set_context_noise_seek(new NOISE(this, emu));
68 /// fdc->set_context_noise_head_down(new NOISE(this, emu));
69 // fdc->set_context_noise_head_up(new NOISE(this, emu));
70 cpu = new Z80(this, emu);
72 cpu->set_context_debugger(new DEBUGGER(this, emu));
75 key = new KEYBOARD(this, emu);
76 memory = new MEMORY(this, emu);
77 system = new SYSTEM(this, emu);
80 event->set_context_cpu(cpu);
81 event->set_context_sound(psg);
82 event->set_context_sound(fm);
83 /// event->set_context_sound(fdc->get_context_noise_seek());
84 /// event->set_context_sound(fdc->get_context_noise_head_down());
85 /// event->set_context_sound(fdc->get_context_noise_head_up());
86 /// event->set_context_sound(drec->get_context_noise_play());
87 /// event->set_context_sound(drec->get_context_noise_stop());
88 /// event->set_context_sound(drec->get_context_noise_fast());
91 /// drec->set_context_ear(pio_k, SIG_I8255_PORT_B, 0x80);
92 pio_k->set_context_port_c(key, SIG_KEYBOARD_COLUMN, 0x07, 0);
93 /// pio_k->set_context_port_c(drec, SIG_DATAREC_REMOTE, 0x08, 0);
94 /// pio_k->set_context_port_c(drec, SIG_DATAREC_MIC, 0x10, 0);
95 /// pio_f->set_context_port_c(fdc, SIG_UPD765A_MOTOR_NEG, 2, 0);
96 /// pio_f->set_context_port_c(fdc, SIG_UPD765A_TC, 4, 0);
97 /// pio_f->set_context_port_c(fdc, SIG_UPD765A_RESET, 8, 0);
98 pio_f->set_context_port_c(memory, SIG_MEMORY_SEL, 0x40, 0);
99 vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
100 /// fdc->set_context_irq(pio_f, SIG_I8255_PORT_A, 1);
101 /// fdc->set_context_index(pio_f, SIG_I8255_PORT_A, 4);
103 key->set_context_cpu(cpu);
104 key->set_context_pio(pio_k);
105 system->set_context_key(key);
106 vdp->set_context_key(key);
107 vdp->set_context_psg(psg);
108 /// vdp->set_context_cpu(cpu);
111 cpu->set_context_mem(memory);
112 cpu->set_context_io(io);
113 cpu->set_context_intr(system);
115 cpu->set_context_debugger(new DEBUGGER(this, emu));
119 io->set_iomap_single_r(0x00, system); // GG START
120 io->set_iomap_single_w(0x80, system); // COL TENKEY
121 io->set_iomap_single_w(0xc0, system); // COL JOYPAD
122 io->set_iomap_range_rw(0xfc, 0xfe, system); // COL JOYPAD
123 io->set_iomap_range_rw(0xff, 0xff, psg); // COL PSG
124 io->set_iomap_range_rw(0x7e, 0x7f, vdp); // SG VDP
125 io->set_iomap_range_rw(0xbe, 0xbf, vdp); // SG VDP
126 io->set_iomap_range_rw(0xdc, 0xdf, pio_k); // SG KEY
127 /// io->set_iomap_range_rw(0xe0, 0xe3, fdc); // SG FDD
128 /// io->set_iomap_range_rw(0xe4, 0xe7, pio_f); // SG FDD
129 /// io->set_iomap_range_rw(0xe8, 0xe9, sio); // SG SERIAL
130 io->set_iomap_range_rw(0xf0, 0xf2, fm); // MS FM
132 // initialize all devices
133 #if defined(__GIT_REPO_VERSION)
134 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
136 for(DEVICE* device = first_device; device; device = device->next_device) {
137 device->initialize();
142 memory->open_cart(create_local_path(_T("SMS.ROM")));
143 /// for(int i = 0; i < 4; i++) {
144 /// fdc->set_drive_type(i, DRIVE_TYPE_2D);
150 // delete all devices
151 for(DEVICE* device = first_device; device;) {
152 DEVICE *next_device = device->next_device;
155 device = next_device;
159 DEVICE* VM::get_device(int id)
161 for(DEVICE* device = first_device; device; device = device->next_device) {
162 if(device->this_device_id == id) {
169 // ----------------------------------------------------------------------------
170 // drive virtual machine
171 // ----------------------------------------------------------------------------
176 for(DEVICE* device = first_device; device; device = device->next_device) {
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
191 DEVICE *VM::get_cpu(int index)
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 void VM::draw_screen()
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
213 void VM::initialize_sound(int rate, int samples)
215 // init sound manager
216 event->initialize_sound(rate, samples);
219 psg->initialize_sound(rate, 3579545, 4000);
220 fm->initialize_sound(rate, 3579545, samples);
223 uint16_t* VM::create_sound(int* extra_frames)
225 return event->create_sound(extra_frames);
228 int VM::get_sound_buffer_ptr()
230 return event->get_sound_buffer_ptr();
233 #ifdef USE_SOUND_VOLUME
234 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
237 psg->set_volume(0, decibel_l, decibel_r);
239 fm->set_volume(0, decibel_l, decibel_r);
240 /// } else if(ch == 2) {
241 /// fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
242 /// fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
243 /// fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
244 /// } else if(ch == 3) {
245 /// drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
246 /// drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
247 /// drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 void VM::open_cart(int drv, const _TCHAR* file_path)
259 memory->open_cart(file_path);
260 if (check_file_extension(file_path, _T(".col"))) {
261 vdp->set_context_irq(cpu, SIG_CPU_NMI, 1);
262 vdp->set_console(0x00);
265 vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
266 if (check_file_extension(file_path, _T(".gg"))) {
267 vdp->set_console(0x40);
269 vdp->set_console(0x20);
276 void VM::close_cart(int drv)
279 memory->close_cart();
284 bool VM::is_cart_inserted(int drv)
287 return memory->is_cart_inserted();
293 ///void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
295 /// fdc->open_disk(drv, file_path, bank);
298 ///void VM::close_floppy_disk(int drv)
300 /// fdc->close_disk(drv);
303 ///bool VM::is_floppy_disk_inserted(int drv)
305 /// return fdc->is_disk_inserted(drv);
308 ///void VM::is_floppy_disk_protected(int drv, bool value)
310 /// fdc->is_disk_protected(drv, value);
313 ///bool VM::is_floppy_disk_protected(int drv)
315 /// return fdc->is_disk_protected(drv);
318 ///uint32_t VM::is_floppy_disk_accessed()
320 /// return fdc->read_signal(0);
323 //void VM::play_tape(int drv, const _TCHAR* file_path)
325 /// bool remote = drec->get_remote();
327 /// if(drec->play_tape(file_path) && remote) {
328 /// // if machine already sets remote on, start playing now
333 ///void VM::rec_tape(int drv, const _TCHAR* file_path)
335 /// bool remote = drec->get_remote();
337 /// if(drec->rec_tape(file_path) && remote) {
338 /// // if machine already sets remote on, start recording now
343 ///void VM::close_tape(int drv)
346 /// drec->close_tape();
347 /// emu->unlock_vm();
348 /// drec->set_remote(false);
351 ///bool VM::is_tape_inserted(int drv)
353 /// return drec->is_tape_inserted();
356 ///bool VM::is_tape_playing(int drv)
358 /// return drec->is_tape_playing();
361 ///bool VM::is_tape_recording(int drv)
363 /// return drec->is_tape_recording();
366 ///int VM::get_tape_position(int drv)
368 /// return drec->get_tape_position();
371 ///const _TCHAR* VM::get_tape_message(int drv)
373 /// return drec->get_message();
376 ///void VM::push_play(int drv)
378 /// drec->set_remote(false);
379 /// drec->set_ff_rew(0);
380 /// drec->set_remote(true);
383 ///void VM::push_stop(int drv)
385 /// drec->set_remote(false);
388 ///void VM::push_fast_forward(int drv)
390 /// drec->set_remote(false);
391 /// drec->set_ff_rew(1);
392 /// drec->set_remote(true);
395 ///void VM::push_fast_rewind(int drv)
397 /// drec->set_remote(false);
398 /// drec->set_ff_rew(-1);
399 /// drec->set_remote(true);
402 bool VM::is_frame_skippable()
404 return event->is_frame_skippable();
407 void VM::update_config()
409 for(DEVICE* device = first_device; device; device = device->next_device) {
410 device->update_config();
414 #define STATE_VERSION 1
416 bool VM::process_state(FILEIO* state_fio, bool loading)
418 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
421 for(DEVICE* device = first_device; device; device = device->next_device) {
422 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
423 // const char *name = typeid(*device).name();
424 // But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
425 const char *name = device->get_device_name();
426 int len = strlen(name);
428 if(!state_fio->StateCheckInt32(len)) {
430 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
434 if(!state_fio->StateCheckBuffer(name, len, 1)) {
436 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
440 if(!device->process_state(state_fio, loading)) {
442 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
447 // Machine specified.