OSDN Git Service

[VM][EMU] Merge Upstream 2017-06-22.
[csp-qt/common_source_project-fm7.git] / source / src / vm / multi8 / multi8.cpp
1 /*
2         MITSUBISHI Electric MULTI8 Emulator 'EmuLTI8'
3
4         Author : Takeda.Toshiya
5         Date   : 2006.09.15 -
6
7         [ virtual machine ]
8 */
9
10 #include "multi8.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../beep.h"
16 #include "../disk.h"
17 #include "../hd46505.h"
18 #include "../i8251.h"
19 #include "../i8253.h"
20 #include "../i8255.h"
21 #include "../i8259.h"
22 #include "../io.h"
23 #include "../noise.h"
24 #include "../upd765a.h"
25 //#include "../ym2203.h"
26 #include "../ay_3_891x.h"
27 #include "../z80.h"
28
29 #ifdef USE_DEBUGGER
30 #include "../debugger.h"
31 #endif
32
33 #include "cmt.h"
34 #include "display.h"
35 #include "floppy.h"
36 #include "kanji.h"
37 #include "keyboard.h"
38 #include "memory.h"
39
40 // ----------------------------------------------------------------------------
41 // initialize
42 // ----------------------------------------------------------------------------
43
44 VM::VM(EMU* parent_emu) : emu(parent_emu)
45 {
46         // create devices
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"));
51         
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);
66         
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);
73         
74         // set contexts
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());
81
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);
97         
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);
104         
105         // cpu bus
106         cpu->set_context_mem(memory);
107         cpu->set_context_io(io);
108         cpu->set_context_intr(pic);
109 #ifdef USE_DEBUGGER
110         cpu->set_context_debugger(new DEBUGGER(this, emu));
111 #endif
112         
113         // i/o bus
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);
127         
128         // initialize all devices
129         for(DEVICE* device = first_device; device; device = device->next_device) {
130                 device->initialize();
131         }
132         for(int i = 0; i < 4; i++) {
133                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
134         }
135 }
136
137 VM::~VM()
138 {
139         // delete all devices
140         for(DEVICE* device = first_device; device;) {
141                 DEVICE *next_device = device->next_device;
142                 device->release();
143                 delete device;
144                 device = next_device;
145         }
146 }
147
148 DEVICE* VM::get_device(int id)
149 {
150         for(DEVICE* device = first_device; device; device = device->next_device) {
151                 if(device->this_device_id == id) {
152                         return device;
153                 }
154         }
155         return NULL;
156 }
157
158 // ----------------------------------------------------------------------------
159 // drive virtual machine
160 // ----------------------------------------------------------------------------
161
162 void VM::reset()
163 {
164         // reset all devices
165         for(DEVICE* device = first_device; device; device = device->next_device) {
166                 device->reset();
167         }
168 }
169
170 void VM::run()
171 {
172         event->drive();
173 }
174
175 double VM::get_frame_rate()
176 {
177         return event->get_frame_rate();
178 }
179
180 // ----------------------------------------------------------------------------
181 // debugger
182 // ----------------------------------------------------------------------------
183
184 #ifdef USE_DEBUGGER
185 DEVICE *VM::get_cpu(int index)
186 {
187         if(index == 0) {
188                 return cpu;
189         }
190         return NULL;
191 }
192 #endif
193
194 // ----------------------------------------------------------------------------
195 // draw screen
196 // ----------------------------------------------------------------------------
197
198 void VM::draw_screen()
199 {
200         display->draw_screen();
201 }
202
203 // ----------------------------------------------------------------------------
204 // soud manager
205 // ----------------------------------------------------------------------------
206
207 void VM::initialize_sound(int rate, int samples)
208 {
209         // init sound manager
210         event->initialize_sound(rate, samples);
211         
212         // init sound gen
213         beep->initialize_sound(rate, 2400, 8000);
214         psg->initialize_sound(rate, 3579545, samples, 0, 0);
215 }
216
217 uint16_t* VM::create_sound(int* extra_frames)
218 {
219         return event->create_sound(extra_frames);
220 }
221
222 int VM::get_sound_buffer_ptr()
223 {
224         return event->get_sound_buffer_ptr();
225 }
226
227 #ifdef USE_SOUND_VOLUME
228 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
229 {
230         if(ch == 0) {
231                 psg->set_volume(1, decibel_l, decibel_r);
232         } else if(ch == 1) {
233                 beep->set_volume(0, decibel_l, decibel_r);
234         } else if(ch == 2) {
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);
238         }
239 }
240 #endif
241
242 // ----------------------------------------------------------------------------
243 // notify key
244 // ----------------------------------------------------------------------------
245
246 bool VM::get_caps_locked()
247 {
248         return key->get_caps_locked();
249 }
250
251 bool VM::get_kana_locked()
252 {
253         return key->get_kana_locked();
254 }
255
256 // ----------------------------------------------------------------------------
257 // user interface
258 // ----------------------------------------------------------------------------
259
260 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
261 {
262         fdc->open_disk(drv, file_path, bank);
263 }
264
265 void VM::close_floppy_disk(int drv)
266 {
267         fdc->close_disk(drv);
268 }
269
270 bool VM::is_floppy_disk_inserted(int drv)
271 {
272         return fdc->is_disk_inserted(drv);
273 }
274
275 void VM::is_floppy_disk_protected(int drv, bool value)
276 {
277         fdc->is_disk_protected(drv, value);
278 }
279
280 bool VM::is_floppy_disk_protected(int drv)
281 {
282         return fdc->is_disk_protected(drv);
283 }
284
285 uint32_t VM::is_floppy_disk_accessed()
286 {
287         return fdc->read_signal(0);
288 }
289
290 void VM::play_tape(int drv, const _TCHAR* file_path)
291 {
292         cmt->play_tape(file_path);
293 }
294
295 void VM::rec_tape(int drv, const _TCHAR* file_path)
296 {
297         cmt->rec_tape(file_path);
298 }
299
300 void VM::close_tape(int drv)
301 {
302         cmt->close_tape();
303 }
304
305 bool VM::is_tape_inserted(int drv)
306 {
307         return cmt->is_tape_inserted();
308 }
309
310 bool VM::is_frame_skippable()
311 {
312         return event->is_frame_skippable();
313 }
314
315 void VM::update_config()
316 {
317         for(DEVICE* device = first_device; device; device = device->next_device) {
318                 device->update_config();
319         }
320 }
321
322 #define STATE_VERSION   3
323
324 void VM::save_state(FILEIO* state_fio)
325 {
326         state_fio->FputUint32(STATE_VERSION);
327         
328         for(DEVICE* device = first_device; device; device = device->next_device) {
329                 device->save_state(state_fio);
330         }
331 }
332
333 bool VM::load_state(FILEIO* state_fio)
334 {
335         if(state_fio->FgetUint32() != STATE_VERSION) {
336                 return false;
337         }
338         for(DEVICE* device = first_device; device; device = device->next_device) {
339                 if(!device->load_state(state_fio)) {
340                         return false;
341                 }
342         }
343         return true;
344 }
345