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 for(DEVICE* device = first_device; device; device = device->next_device) {
110 device->initialize();
112 for(int i = 0; i < 4; i++) {
113 fdc->set_drive_type(i, DRIVE_TYPE_2D);
119 // delete all devices
120 for(DEVICE* device = first_device; device;) {
121 DEVICE *next_device = device->next_device;
124 device = next_device;
128 DEVICE* VM::get_device(int id)
130 for(DEVICE* device = first_device; device; device = device->next_device) {
131 if(device->this_device_id == id) {
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
143 DEVICE *VM::get_cpu(int index)
152 // ----------------------------------------------------------------------------
153 // drive virtual machine
154 // ----------------------------------------------------------------------------
159 for(DEVICE* device = first_device; device; device = device->next_device) {
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 void VM::draw_screen()
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 void VM::initialize_sound(int rate, int samples)
184 // init sound manager
185 event->initialize_sound(rate, samples);
188 psg->initialize_sound(rate, 3579545, 8000);
191 uint16_t* VM::create_sound(int* extra_frames)
193 return event->create_sound(extra_frames);
196 int VM::get_sound_buffer_ptr()
198 return event->get_sound_buffer_ptr();
201 #ifdef USE_SOUND_VOLUME
202 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
205 psg->set_volume(0, decibel_l, decibel_r);
207 drec->set_volume(0, decibel_l, decibel_r);
209 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
210 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
211 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
213 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
214 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
215 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
220 // ----------------------------------------------------------------------------
222 // ----------------------------------------------------------------------------
224 void VM::open_cart(int drv, const _TCHAR* file_path)
227 memory->open_cart(file_path);
232 void VM::close_cart(int drv)
235 memory->close_cart();
240 bool VM::is_cart_inserted(int drv)
243 return memory->is_cart_inserted();
249 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
251 fdc->open_disk(drv, file_path, bank);
254 void VM::close_floppy_disk(int drv)
256 fdc->close_disk(drv);
259 bool VM::is_floppy_disk_inserted(int drv)
261 return fdc->is_disk_inserted(drv);
264 void VM::is_floppy_disk_protected(int drv, bool value)
266 fdc->is_disk_protected(drv, value);
269 bool VM::is_floppy_disk_protected(int drv)
271 return fdc->is_disk_protected(drv);
274 uint32_t VM::is_floppy_disk_accessed()
276 return fdc->read_signal(0);
279 void VM::play_tape(int drv, const _TCHAR* file_path)
281 drec->play_tape(file_path);
282 // drec->set_remote(true);
285 void VM::rec_tape(int drv, const _TCHAR* file_path)
287 drec->rec_tape(file_path);
288 // drec->set_remote(true);
291 void VM::close_tape(int drv)
296 // drec->set_remote(false);
299 bool VM::is_tape_inserted(int drv)
301 return drec->is_tape_inserted();
304 bool VM::is_tape_playing(int drv)
306 return drec->is_tape_playing();
309 bool VM::is_tape_recording(int drv)
311 return drec->is_tape_recording();
314 int VM::get_tape_position(int drv)
316 return drec->get_tape_position();
319 const _TCHAR* VM::get_tape_message(int drv)
321 return drec->get_message();
324 void VM::push_play(int drv)
327 drec->set_remote(true);
330 void VM::push_stop(int drv)
332 drec->set_remote(false);
335 void VM::push_fast_forward(int drv)
338 drec->set_remote(true);
341 void VM::push_fast_rewind(int drv)
343 drec->set_ff_rew(-1);
344 drec->set_remote(true);
347 bool VM::is_frame_skippable()
349 return event->is_frame_skippable();
352 void VM::update_config()
354 for(DEVICE* device = first_device; device; device = device->next_device) {
355 device->update_config();
359 #define STATE_VERSION 3
361 #include "../../statesub.h"
362 #include "../../qt/gui/csp_logger.h"
363 extern CSP_Logger DLL_PREFIX_I *csp_logger;
365 void VM::decl_state(void)
367 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::SC_3000_HEAD")), csp_logger);
368 for(DEVICE* device = first_device; device; device = device->next_device) {
369 device->decl_state();
373 void VM::save_state(FILEIO* state_fio)
375 //state_fio->FputUint32(STATE_VERSION);
377 if(state_entry != NULL) {
378 state_entry->save_state(state_fio);
380 for(DEVICE* device = first_device; device; device = device->next_device) {
381 device->save_state(state_fio);
385 bool VM::load_state(FILEIO* state_fio)
387 //if(state_fio->FgetUint32() != STATE_VERSION) {
391 if(state_entry != NULL) {
392 mb = state_entry->load_state(state_fio);
395 emu->out_debug_log("INFO: HEADER DATA ERROR");
398 for(DEVICE* device = first_device; device; device = device->next_device) {
399 if(!device->load_state(state_fio)) {