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 using SC3000::KEYBOARD;
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 VM::VM(EMU_TEMPLATE* parent_emu) : VM_TEMPLATE(parent_emu)
43 first_device = last_device = NULL;
44 dummy = new DEVICE(this, emu); // must be 1st device
45 event = new EVENT(this, emu); // must be 2nd device
46 dummy->set_device_name(_T("1st Dummy"));
48 drec = new DATAREC(this, emu);
49 drec->set_context_noise_play(new NOISE(this, emu));
50 drec->set_context_noise_stop(new NOISE(this, emu));
51 drec->set_context_noise_fast(new NOISE(this, emu));
52 sio = new I8251(this, emu);
53 pio_k = new I8255(this, emu);
54 pio_k->set_device_name(_T("8255 PIO (Keyboard)"));
55 pio_f = new I8255(this, emu);
56 pio_f->set_device_name(_T("8255 PIO (Floppy I/F)"));
57 io = new IO(this, emu);
59 psg = new SN76489AN(this, emu);
60 vdp = new TMS9918A(this, emu);
62 // psg->set_context_debugger(new DEBUGGER(this, emu));
63 vdp->set_context_debugger(new DEBUGGER(this, emu));
65 fdc = new UPD765A(this, emu);
67 // fdc->set_context_debugger(new DEBUGGER(this, emu));
69 fdc->set_context_noise_seek(new NOISE(this, emu));
70 fdc->set_context_noise_head_down(new NOISE(this, emu));
71 fdc->set_context_noise_head_up(new NOISE(this, emu));
72 cpu = new Z80(this, emu);
74 key = new KEYBOARD(this, emu);
75 memory = new MEMORY(this, emu);
78 event->set_context_cpu(cpu);
79 event->set_context_sound(psg);
80 event->set_context_sound(drec);
81 event->set_context_sound(fdc->get_context_noise_seek());
82 event->set_context_sound(fdc->get_context_noise_head_down());
83 event->set_context_sound(fdc->get_context_noise_head_up());
84 event->set_context_sound(drec->get_context_noise_play());
85 event->set_context_sound(drec->get_context_noise_stop());
86 event->set_context_sound(drec->get_context_noise_fast());
88 drec->set_context_ear(pio_k, SIG_I8255_PORT_B, 0x80);
89 pio_k->set_context_port_c(key, SIG_KEYBOARD_COLUMN, 0x07, 0);
90 pio_k->set_context_port_c(drec, SIG_DATAREC_REMOTE, 0x08, 0);
91 pio_k->set_context_port_c(drec, SIG_DATAREC_MIC, 0x10, 0);
92 pio_f->set_context_port_c(fdc, SIG_UPD765A_MOTOR_NEG, 2, 0);
93 pio_f->set_context_port_c(fdc, SIG_UPD765A_TC, 4, 0);
94 pio_f->set_context_port_c(fdc, SIG_UPD765A_RESET, 8, 0);
95 pio_f->set_context_port_c(memory, SIG_MEMORY_SEL, 0x40, 0);
96 vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
97 fdc->set_context_irq(pio_f, SIG_I8255_PORT_A, 1);
98 fdc->set_context_index(pio_f, SIG_I8255_PORT_A, 4);
100 key->set_context_cpu(cpu);
101 key->set_context_pio(pio_k);
104 cpu->set_context_mem(memory);
105 cpu->set_context_io(io);
106 cpu->set_context_intr(dummy);
108 cpu->set_context_debugger(new DEBUGGER(this, emu));
112 io->set_iomap_range_rw(0x40, 0x7f, psg);
113 io->set_iomap_range_rw(0x80, 0xbf, vdp);
114 io->set_iomap_range_rw(0xc0, 0xdf, pio_k);
115 io->set_iomap_range_rw(0xe0, 0xe3, fdc);
116 io->set_iomap_range_rw(0xe4, 0xe7, pio_f);
117 io->set_iomap_range_rw(0xe8, 0xeb, sio);
119 // initialize all devices
120 #if defined(__GIT_REPO_VERSION)
121 set_git_repo_version(__GIT_REPO_VERSION);
123 initialize_devices();
125 for(int i = 0; i < 4; i++) {
126 fdc->set_drive_type(i, DRIVE_TYPE_2D);
132 // delete all devices
133 for(DEVICE* device = first_device; device;) {
134 DEVICE *next_device = device->next_device;
137 device = next_device;
141 DEVICE* VM::get_device(int id)
143 for(DEVICE* device = first_device; device; device = device->next_device) {
144 if(device->this_device_id == id) {
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
156 DEVICE *VM::get_cpu(int index)
165 // ----------------------------------------------------------------------------
166 // drive virtual machine
167 // ----------------------------------------------------------------------------
172 for(DEVICE* device = first_device; device; device = device->next_device) {
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 void VM::draw_screen()
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 void VM::initialize_sound(int rate, int samples)
197 // init sound manager
198 event->initialize_sound(rate, samples);
201 psg->initialize_sound(rate, 3579545, 8000);
204 uint16_t* VM::create_sound(int* extra_frames)
206 return event->create_sound(extra_frames);
209 int VM::get_sound_buffer_ptr()
211 return event->get_sound_buffer_ptr();
214 #ifdef USE_SOUND_VOLUME
215 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
218 psg->set_volume(0, decibel_l, decibel_r);
220 drec->set_volume(0, decibel_l, decibel_r);
222 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
223 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
224 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
226 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
227 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
228 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 void VM::open_cart(int drv, const _TCHAR* file_path)
240 memory->open_cart(file_path);
245 void VM::close_cart(int drv)
248 memory->close_cart();
253 bool VM::is_cart_inserted(int drv)
256 return memory->is_cart_inserted();
262 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
264 fdc->open_disk(drv, file_path, bank);
267 void VM::close_floppy_disk(int drv)
269 fdc->close_disk(drv);
272 bool VM::is_floppy_disk_inserted(int drv)
274 return fdc->is_disk_inserted(drv);
277 void VM::is_floppy_disk_protected(int drv, bool value)
279 fdc->is_disk_protected(drv, value);
282 bool VM::is_floppy_disk_protected(int drv)
284 return fdc->is_disk_protected(drv);
287 uint32_t VM::is_floppy_disk_accessed()
289 return fdc->read_signal(0);
292 void VM::play_tape(int drv, const _TCHAR* file_path)
294 bool remote = drec->get_remote();
296 if(drec->play_tape(file_path) && remote) {
297 // if machine already sets remote on, start playing now
302 void VM::rec_tape(int drv, const _TCHAR* file_path)
304 bool remote = drec->get_remote();
306 if(drec->rec_tape(file_path) && remote) {
307 // if machine already sets remote on, start recording now
312 void VM::close_tape(int drv)
317 drec->set_remote(false);
320 bool VM::is_tape_inserted(int drv)
322 return drec->is_tape_inserted();
325 bool VM::is_tape_playing(int drv)
327 return drec->is_tape_playing();
330 bool VM::is_tape_recording(int drv)
332 return drec->is_tape_recording();
335 int VM::get_tape_position(int drv)
337 return drec->get_tape_position();
340 const _TCHAR* VM::get_tape_message(int drv)
342 return drec->get_message();
345 void VM::push_play(int drv)
347 drec->set_remote(false);
349 drec->set_remote(true);
352 void VM::push_stop(int drv)
354 drec->set_remote(false);
357 void VM::push_fast_forward(int drv)
359 drec->set_remote(false);
361 drec->set_remote(true);
364 void VM::push_fast_rewind(int drv)
366 drec->set_remote(false);
367 drec->set_ff_rew(-1);
368 drec->set_remote(true);
371 bool VM::is_frame_skippable()
373 return event->is_frame_skippable();
376 void VM::update_config()
378 for(DEVICE* device = first_device; device; device = device->next_device) {
379 device->update_config();
383 double VM::get_current_usec()
385 if(event == NULL) return 0.0;
386 return event->get_current_usec();
389 uint64_t VM::get_current_clock_uint64()
391 if(event == NULL) return (uint64_t)0;
392 return event->get_current_clock_uint64();
395 #define STATE_VERSION 4
397 bool VM::process_state(FILEIO* state_fio, bool loading)
399 if(!(VM_TEMPLATE::process_state_core(state_fio, loading, STATE_VERSION))) {
402 // Machine specified.