2 SEGA SC-3000 Emulator 'eSC-3000'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
15 #include "../datarec.h"
21 #include "../sn76489an.h"
22 #include "../tms9918a.h"
23 #include "../upd765a.h"
27 #include "../debugger.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
40 first_device = last_device = NULL;
41 dummy = new DEVICE(this, emu); // must be 1st device
42 event = new EVENT(this, emu); // must be 2nd device
43 dummy->set_device_name(_T("1st Dummy"));
45 drec = new DATAREC(this, emu);
46 drec->set_context_noise_play(new NOISE(this, emu));
47 drec->set_context_noise_stop(new NOISE(this, emu));
48 drec->set_context_noise_fast(new NOISE(this, emu));
49 sio = new I8251(this, emu);
50 pio_k = new I8255(this, emu);
51 pio_k->set_device_name(_T("8255 PIO (Keyboard)"));
52 pio_f = new I8255(this, emu);
53 pio_f->set_device_name(_T("8255 PIO (Floppy I/F)"));
54 io = new IO(this, emu);
55 psg = new SN76489AN(this, emu);
56 vdp = new TMS9918A(this, emu);
57 fdc = new UPD765A(this, emu);
58 fdc->set_context_noise_seek(new NOISE(this, emu));
59 fdc->set_context_noise_head_down(new NOISE(this, emu));
60 fdc->set_context_noise_head_up(new NOISE(this, emu));
61 cpu = new Z80(this, emu);
63 key = new KEYBOARD(this, emu);
64 memory = new MEMORY(this, emu);
67 event->set_context_cpu(cpu);
68 event->set_context_sound(psg);
69 event->set_context_sound(drec);
70 event->set_context_sound(fdc->get_context_noise_seek());
71 event->set_context_sound(fdc->get_context_noise_head_down());
72 event->set_context_sound(fdc->get_context_noise_head_up());
73 event->set_context_sound(drec->get_context_noise_play());
74 event->set_context_sound(drec->get_context_noise_stop());
75 event->set_context_sound(drec->get_context_noise_fast());
77 drec->set_context_ear(pio_k, SIG_I8255_PORT_B, 0x80);
78 pio_k->set_context_port_c(key, SIG_KEYBOARD_COLUMN, 0x07, 0);
79 pio_k->set_context_port_c(drec, SIG_DATAREC_REMOTE, 0x08, 0);
80 pio_k->set_context_port_c(drec, SIG_DATAREC_MIC, 0x10, 0);
81 pio_f->set_context_port_c(fdc, SIG_UPD765A_MOTOR_NEG, 2, 0);
82 pio_f->set_context_port_c(fdc, SIG_UPD765A_TC, 4, 0);
83 pio_f->set_context_port_c(fdc, SIG_UPD765A_RESET, 8, 0);
84 pio_f->set_context_port_c(memory, SIG_MEMORY_SEL, 0x40, 0);
85 vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
86 fdc->set_context_irq(pio_f, SIG_I8255_PORT_A, 1);
87 fdc->set_context_index(pio_f, SIG_I8255_PORT_A, 4);
89 key->set_context_cpu(cpu);
90 key->set_context_pio(pio_k);
93 cpu->set_context_mem(memory);
94 cpu->set_context_io(io);
95 cpu->set_context_intr(dummy);
97 cpu->set_context_debugger(new DEBUGGER(this, emu));
101 io->set_iomap_range_rw(0x40, 0x7f, psg);
102 io->set_iomap_range_rw(0x80, 0xbf, vdp);
103 io->set_iomap_range_rw(0xc0, 0xdf, pio_k);
104 io->set_iomap_range_rw(0xe0, 0xe3, fdc);
105 io->set_iomap_range_rw(0xe4, 0xe7, pio_f);
106 io->set_iomap_range_rw(0xe8, 0xeb, sio);
108 // initialize all devices
109 #if defined(__GIT_REPO_VERSION)
110 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
112 for(DEVICE* device = first_device; device; device = device->next_device) {
113 device->initialize();
115 for(int i = 0; i < 4; i++) {
116 fdc->set_drive_type(i, DRIVE_TYPE_2D);
122 // delete all devices
123 for(DEVICE* device = first_device; device;) {
124 DEVICE *next_device = device->next_device;
127 device = next_device;
131 DEVICE* VM::get_device(int id)
133 for(DEVICE* device = first_device; device; device = device->next_device) {
134 if(device->this_device_id == id) {
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
146 DEVICE *VM::get_cpu(int index)
155 // ----------------------------------------------------------------------------
156 // drive virtual machine
157 // ----------------------------------------------------------------------------
162 for(DEVICE* device = first_device; device; device = device->next_device) {
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 void VM::draw_screen()
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 void VM::initialize_sound(int rate, int samples)
187 // init sound manager
188 event->initialize_sound(rate, samples);
191 psg->initialize_sound(rate, 3579545, 8000);
194 uint16_t* VM::create_sound(int* extra_frames)
196 return event->create_sound(extra_frames);
199 int VM::get_sound_buffer_ptr()
201 return event->get_sound_buffer_ptr();
204 #ifdef USE_SOUND_VOLUME
205 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
208 psg->set_volume(0, decibel_l, decibel_r);
210 drec->set_volume(0, decibel_l, decibel_r);
212 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
213 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
214 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
216 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
217 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
218 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
227 void VM::open_cart(int drv, const _TCHAR* file_path)
230 memory->open_cart(file_path);
235 void VM::close_cart(int drv)
238 memory->close_cart();
243 bool VM::is_cart_inserted(int drv)
246 return memory->is_cart_inserted();
252 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
254 fdc->open_disk(drv, file_path, bank);
257 void VM::close_floppy_disk(int drv)
259 fdc->close_disk(drv);
262 bool VM::is_floppy_disk_inserted(int drv)
264 return fdc->is_disk_inserted(drv);
267 void VM::is_floppy_disk_protected(int drv, bool value)
269 fdc->is_disk_protected(drv, value);
272 bool VM::is_floppy_disk_protected(int drv)
274 return fdc->is_disk_protected(drv);
277 uint32_t VM::is_floppy_disk_accessed()
279 return fdc->read_signal(0);
282 void VM::play_tape(int drv, const _TCHAR* file_path)
284 drec->play_tape(file_path);
285 // drec->set_remote(true);
288 void VM::rec_tape(int drv, const _TCHAR* file_path)
290 drec->rec_tape(file_path);
291 // drec->set_remote(true);
294 void VM::close_tape(int drv)
299 // drec->set_remote(false);
302 bool VM::is_tape_inserted(int drv)
304 return drec->is_tape_inserted();
307 bool VM::is_tape_playing(int drv)
309 return drec->is_tape_playing();
312 bool VM::is_tape_recording(int drv)
314 return drec->is_tape_recording();
317 int VM::get_tape_position(int drv)
319 return drec->get_tape_position();
322 const _TCHAR* VM::get_tape_message(int drv)
324 return drec->get_message();
327 void VM::push_play(int drv)
330 drec->set_remote(true);
333 void VM::push_stop(int drv)
335 drec->set_remote(false);
338 void VM::push_fast_forward(int drv)
341 drec->set_remote(true);
344 void VM::push_fast_rewind(int drv)
346 drec->set_ff_rew(-1);
347 drec->set_remote(true);
350 bool VM::is_frame_skippable()
352 return event->is_frame_skippable();
355 void VM::update_config()
357 for(DEVICE* device = first_device; device; device = device->next_device) {
358 device->update_config();
362 #define STATE_VERSION 3
364 #include "../../statesub.h"
365 #include "../../qt/gui/csp_logger.h"
366 extern CSP_Logger DLL_PREFIX_I *csp_logger;
368 void VM::decl_state(void)
370 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::SC_3000_HEAD")), csp_logger);
371 for(DEVICE* device = first_device; device; device = device->next_device) {
372 device->decl_state();
376 void VM::save_state(FILEIO* state_fio)
378 //state_fio->FputUint32(STATE_VERSION);
380 if(state_entry != NULL) {
381 state_entry->save_state(state_fio);
383 for(DEVICE* device = first_device; device; device = device->next_device) {
384 device->save_state(state_fio);
388 bool VM::load_state(FILEIO* state_fio)
390 //if(state_fio->FgetUint32() != STATE_VERSION) {
394 if(state_entry != NULL) {
395 mb = state_entry->load_state(state_fio);
398 emu->out_debug_log("INFO: HEADER DATA ERROR");
401 for(DEVICE* device = first_device; device; device = device->next_device) {
402 if(!device->load_state(state_fio)) {