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) : emu(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 for(DEVICE* device = first_device; device; device = device->next_device) {
130 device->initialize();
132 for(int i = 0; i < 4; i++) {
133 fdc->set_drive_type(i, DRIVE_TYPE_2D);
139 // delete all devices
140 for(DEVICE* device = first_device; device;) {
141 DEVICE *next_device = device->next_device;
144 device = next_device;
148 DEVICE* VM::get_device(int id)
150 for(DEVICE* device = first_device; device; device = device->next_device) {
151 if(device->this_device_id == id) {
158 // ----------------------------------------------------------------------------
159 // drive virtual machine
160 // ----------------------------------------------------------------------------
165 for(DEVICE* device = first_device; device; device = device->next_device) {
175 double VM::get_frame_rate()
177 return event->get_frame_rate();
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
185 DEVICE *VM::get_cpu(int index)
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 void VM::draw_screen()
200 display->draw_screen();
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
207 void VM::initialize_sound(int rate, int samples)
209 // init sound manager
210 event->initialize_sound(rate, samples);
213 beep->initialize_sound(rate, 2400, 8000);
214 psg->initialize_sound(rate, 3579545, samples, 0, 0);
217 uint16_t* VM::create_sound(int* extra_frames)
219 return event->create_sound(extra_frames);
222 int VM::get_sound_buffer_ptr()
224 return event->get_sound_buffer_ptr();
227 #ifdef USE_SOUND_VOLUME
228 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
231 psg->set_volume(1, decibel_l, decibel_r);
233 beep->set_volume(0, decibel_l, decibel_r);
235 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
236 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
237 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 bool VM::get_caps_locked()
248 return key->get_caps_locked();
251 bool VM::get_kana_locked()
253 return key->get_kana_locked();
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
262 fdc->open_disk(drv, file_path, bank);
265 void VM::close_floppy_disk(int drv)
267 fdc->close_disk(drv);
270 bool VM::is_floppy_disk_inserted(int drv)
272 return fdc->is_disk_inserted(drv);
275 void VM::is_floppy_disk_protected(int drv, bool value)
277 fdc->is_disk_protected(drv, value);
280 bool VM::is_floppy_disk_protected(int drv)
282 return fdc->is_disk_protected(drv);
285 uint32_t VM::is_floppy_disk_accessed()
287 return fdc->read_signal(0);
290 void VM::play_tape(int drv, const _TCHAR* file_path)
292 cmt->play_tape(file_path);
295 void VM::rec_tape(int drv, const _TCHAR* file_path)
297 cmt->rec_tape(file_path);
300 void VM::close_tape(int drv)
305 bool VM::is_tape_inserted(int drv)
307 return cmt->is_tape_inserted();
310 bool VM::is_frame_skippable()
312 return event->is_frame_skippable();
315 void VM::update_config()
317 for(DEVICE* device = first_device; device; device = device->next_device) {
318 device->update_config();
322 #define STATE_VERSION 3
324 void VM::save_state(FILEIO* state_fio)
326 state_fio->FputUint32(STATE_VERSION);
328 for(DEVICE* device = first_device; device; device = device->next_device) {
329 device->save_state(state_fio);
333 bool VM::load_state(FILEIO* state_fio)
335 if(state_fio->FgetUint32() != STATE_VERSION) {
338 for(DEVICE* device = first_device; device; device = device->next_device) {
339 if(!device->load_state(state_fio)) {