2 MITSUBISHI Electric MULTI8 Emulator 'EmuLTI8'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
17 #include "../hd46505.h"
24 #include "../upd765a.h"
25 //#include "../ym2203.h"
26 #include "../ay_3_891x.h"
30 #include "../debugger.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
47 first_device = last_device = NULL;
48 dummy = new DEVICE(this, emu); // must be 1st device
49 event = new EVENT(this, emu); // must be 2nd device
50 dummy->set_device_name(_T("1st Dummy"));
52 beep = new BEEP(this, emu);
53 crtc = new HD46505(this, emu);
54 sio = new I8251(this, emu);
55 pit = new I8253(this, emu);
56 pio = new I8255(this, emu);
57 pic = new I8259(this, emu);
58 io = new IO(this, emu);
59 fdc = new UPD765A(this, emu);
60 fdc->set_context_noise_seek(new NOISE(this, emu));
61 fdc->set_context_noise_head_down(new NOISE(this, emu));
62 fdc->set_context_noise_head_up(new NOISE(this, emu));
63 // psg = new YM2203(this, emu);
64 psg = new AY_3_891X(this, emu);
65 cpu = new Z80(this, emu);
67 cmt = new CMT(this, emu);
68 display = new DISPLAY(this, emu);
69 floppy = new FLOPPY(this, emu);
70 kanji = new KANJI(this, emu);
71 key = new KEYBOARD(this, emu);
72 memory = new MEMORY(this, emu);
75 event->set_context_cpu(cpu);
76 event->set_context_sound(beep);
77 event->set_context_sound(psg);
78 event->set_context_sound(fdc->get_context_noise_seek());
79 event->set_context_sound(fdc->get_context_noise_head_down());
80 event->set_context_sound(fdc->get_context_noise_head_up());
82 crtc->set_context_vsync(pio, SIG_I8255_PORT_A, 0x20);
83 sio->set_context_out(cmt, SIG_CMT_OUT);
84 pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);
85 pit->set_context_ch1(pic, SIG_I8259_CHIP0 | SIG_I8259_IR5, 1);
86 pit->set_context_ch2(pic, SIG_I8259_CHIP0 | SIG_I8259_IR6, 1);
87 pit->set_constant_clock(0, CPU_CLOCKS >> 1);
88 pit->set_constant_clock(1, CPU_CLOCKS >> 1);
89 pio->set_context_port_b(display, SIG_DISPLAY_I8255_B, 0xff, 0);
90 pio->set_context_port_c(memory, SIG_MEMORY_I8255_C, 0xff, 0);
91 pic->set_context_cpu(cpu);
92 fdc->set_context_irq(pic, SIG_I8259_CHIP0 | SIG_I8259_IR0, 1);
93 fdc->set_context_drq(floppy, SIG_FLOPPY_DRQ, 1);
94 psg->set_context_port_a(cmt, SIG_CMT_REMOTE, 2, 0);
95 psg->set_context_port_a(pio, SIG_I8255_PORT_A, 2, 1);
96 psg->set_context_port_a(beep, SIG_BEEP_ON, 8, 1);
98 cmt->set_context_sio(sio);
99 display->set_vram_ptr(memory->get_vram());
100 display->set_regs_ptr(crtc->get_regs());
101 floppy->set_context_fdc(fdc);
102 kanji->set_context_pio(pio);
103 memory->set_context_pio(pio);
106 cpu->set_context_mem(memory);
107 cpu->set_context_io(io);
108 cpu->set_context_intr(pic);
110 cpu->set_context_debugger(new DEBUGGER(this, emu));
114 io->set_iomap_range_rw(0x00, 0x01, key);
115 io->set_iomap_range_rw(0x18, 0x19, psg);
116 io->set_iomap_range_rw(0x1c, 0x1d, crtc);
117 io->set_iomap_range_rw(0x20, 0x21, sio);
118 io->set_iomap_range_rw(0x24, 0x27, pit);
119 io->set_iomap_range_rw(0x28, 0x2b, pio);
120 io->set_iomap_alias_rw(0x2c, pic, I8259_ADDR_CHIP0 | 0);
121 io->set_iomap_alias_rw(0x2d, pic, I8259_ADDR_CHIP0 | 1);
122 io->set_iomap_range_rw(0x30, 0x37, display);
123 io->set_iomap_range_rw(0x40, 0x41, kanji);
124 io->set_iomap_range_rw(0x70, 0x71, fdc);
125 io->set_iomap_range_rw(0x72, 0x74, floppy);
126 io->set_iomap_single_w(0x78, memory);
128 // initialize all devices
129 #if defined(__GIT_REPO_VERSION)
130 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
132 for(DEVICE* device = first_device; device; device = device->next_device) {
133 device->initialize();
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 double VM::get_frame_rate()
182 return event->get_frame_rate();
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
190 DEVICE *VM::get_cpu(int index)
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
203 void VM::draw_screen()
205 display->draw_screen();
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
212 void VM::initialize_sound(int rate, int samples)
214 // init sound manager
215 event->initialize_sound(rate, samples);
218 beep->initialize_sound(rate, 2400, 8000);
219 psg->initialize_sound(rate, 3579545, samples, 0, 0);
222 uint16_t* VM::create_sound(int* extra_frames)
224 return event->create_sound(extra_frames);
227 int VM::get_sound_buffer_ptr()
229 return event->get_sound_buffer_ptr();
232 #ifdef USE_SOUND_VOLUME
233 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
236 psg->set_volume(1, decibel_l, decibel_r);
238 beep->set_volume(0, decibel_l, decibel_r);
240 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
241 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
242 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 bool VM::get_caps_locked()
253 return key->get_caps_locked();
256 bool VM::get_kana_locked()
258 return key->get_kana_locked();
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
267 fdc->open_disk(drv, file_path, bank);
270 void VM::close_floppy_disk(int drv)
272 fdc->close_disk(drv);
275 bool VM::is_floppy_disk_inserted(int drv)
277 return fdc->is_disk_inserted(drv);
280 void VM::is_floppy_disk_protected(int drv, bool value)
282 fdc->is_disk_protected(drv, value);
285 bool VM::is_floppy_disk_protected(int drv)
287 return fdc->is_disk_protected(drv);
290 uint32_t VM::is_floppy_disk_accessed()
292 return fdc->read_signal(0);
295 void VM::play_tape(int drv, const _TCHAR* file_path)
297 cmt->play_tape(file_path);
300 void VM::rec_tape(int drv, const _TCHAR* file_path)
302 cmt->rec_tape(file_path);
305 void VM::close_tape(int drv)
310 bool VM::is_tape_inserted(int drv)
312 return cmt->is_tape_inserted();
315 bool VM::is_frame_skippable()
317 return event->is_frame_skippable();
320 void VM::update_config()
322 for(DEVICE* device = first_device; device; device = device->next_device) {
323 device->update_config();
327 #define STATE_VERSION 4
329 #include "../../statesub.h"
330 #include "../../qt/gui/csp_logger.h"
331 extern CSP_Logger DLL_PREFIX_I *csp_logger;
333 void VM::decl_state(void)
335 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MULTI8_HEAD")), csp_logger);
336 for(DEVICE* device = first_device; device; device = device->next_device) {
337 device->decl_state();
341 void VM::save_state(FILEIO* state_fio)
343 //state_fio->FputUint32(STATE_VERSION);
345 if(state_entry != NULL) {
346 state_entry->save_state(state_fio);
348 for(DEVICE* device = first_device; device; device = device->next_device) {
349 device->save_state(state_fio);
353 bool VM::load_state(FILEIO* state_fio)
355 //if(state_fio->FgetUint32() != STATE_VERSION) {
359 if(state_entry != NULL) {
360 mb = state_entry->load_state(state_fio);
363 emu->out_debug_log("INFO: HEADER DATA ERROR");
366 for(DEVICE* device = first_device; device; device = device->next_device) {
367 if(!device->load_state(state_fio)) {