OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[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) : VM_TEMPLATE(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 #if defined(__GIT_REPO_VERSION)
130         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
131 #endif
132         for(DEVICE* device = first_device; device; device = device->next_device) {
133                 device->initialize();
134         }
135         decl_state();
136         
137         for(int i = 0; i < 4; i++) {
138                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
139         }
140 }
141
142 VM::~VM()
143 {
144         // delete all devices
145         for(DEVICE* device = first_device; device;) {
146                 DEVICE *next_device = device->next_device;
147                 device->release();
148                 delete device;
149                 device = next_device;
150         }
151 }
152
153 DEVICE* VM::get_device(int id)
154 {
155         for(DEVICE* device = first_device; device; device = device->next_device) {
156                 if(device->this_device_id == id) {
157                         return device;
158                 }
159         }
160         return NULL;
161 }
162
163 // ----------------------------------------------------------------------------
164 // drive virtual machine
165 // ----------------------------------------------------------------------------
166
167 void VM::reset()
168 {
169         // reset all devices
170         for(DEVICE* device = first_device; device; device = device->next_device) {
171                 device->reset();
172         }
173 }
174
175 void VM::run()
176 {
177         event->drive();
178 }
179
180 double VM::get_frame_rate()
181 {
182         return event->get_frame_rate();
183 }
184
185 // ----------------------------------------------------------------------------
186 // debugger
187 // ----------------------------------------------------------------------------
188
189 #ifdef USE_DEBUGGER
190 DEVICE *VM::get_cpu(int index)
191 {
192         if(index == 0) {
193                 return cpu;
194         }
195         return NULL;
196 }
197 #endif
198
199 // ----------------------------------------------------------------------------
200 // draw screen
201 // ----------------------------------------------------------------------------
202
203 void VM::draw_screen()
204 {
205         display->draw_screen();
206 }
207
208 // ----------------------------------------------------------------------------
209 // soud manager
210 // ----------------------------------------------------------------------------
211
212 void VM::initialize_sound(int rate, int samples)
213 {
214         // init sound manager
215         event->initialize_sound(rate, samples);
216         
217         // init sound gen
218         beep->initialize_sound(rate, 2400, 8000);
219         psg->initialize_sound(rate, 3579545, samples, 0, 0);
220 }
221
222 uint16_t* VM::create_sound(int* extra_frames)
223 {
224         return event->create_sound(extra_frames);
225 }
226
227 int VM::get_sound_buffer_ptr()
228 {
229         return event->get_sound_buffer_ptr();
230 }
231
232 #ifdef USE_SOUND_VOLUME
233 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
234 {
235         if(ch == 0) {
236                 psg->set_volume(1, decibel_l, decibel_r);
237         } else if(ch == 1) {
238                 beep->set_volume(0, decibel_l, decibel_r);
239         } else if(ch == 2) {
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);
243         }
244 }
245 #endif
246
247 // ----------------------------------------------------------------------------
248 // notify key
249 // ----------------------------------------------------------------------------
250
251 bool VM::get_caps_locked()
252 {
253         return key->get_caps_locked();
254 }
255
256 bool VM::get_kana_locked()
257 {
258         return key->get_kana_locked();
259 }
260
261 // ----------------------------------------------------------------------------
262 // user interface
263 // ----------------------------------------------------------------------------
264
265 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
266 {
267         fdc->open_disk(drv, file_path, bank);
268 }
269
270 void VM::close_floppy_disk(int drv)
271 {
272         fdc->close_disk(drv);
273 }
274
275 bool VM::is_floppy_disk_inserted(int drv)
276 {
277         return fdc->is_disk_inserted(drv);
278 }
279
280 void VM::is_floppy_disk_protected(int drv, bool value)
281 {
282         fdc->is_disk_protected(drv, value);
283 }
284
285 bool VM::is_floppy_disk_protected(int drv)
286 {
287         return fdc->is_disk_protected(drv);
288 }
289
290 uint32_t VM::is_floppy_disk_accessed()
291 {
292         return fdc->read_signal(0);
293 }
294
295 void VM::play_tape(int drv, const _TCHAR* file_path)
296 {
297         cmt->play_tape(file_path);
298 }
299
300 void VM::rec_tape(int drv, const _TCHAR* file_path)
301 {
302         cmt->rec_tape(file_path);
303 }
304
305 void VM::close_tape(int drv)
306 {
307         cmt->close_tape();
308 }
309
310 bool VM::is_tape_inserted(int drv)
311 {
312         return cmt->is_tape_inserted();
313 }
314
315 bool VM::is_frame_skippable()
316 {
317         return event->is_frame_skippable();
318 }
319
320 void VM::update_config()
321 {
322         for(DEVICE* device = first_device; device; device = device->next_device) {
323                 device->update_config();
324         }
325 }
326
327 #define STATE_VERSION   4
328
329 #include "../../statesub.h"
330 #include "../../qt/gui/csp_logger.h"
331 extern CSP_Logger DLL_PREFIX_I *csp_logger;
332
333 void VM::decl_state(void)
334 {
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();
338         }
339 }
340
341 void VM::save_state(FILEIO* state_fio)
342 {
343         //state_fio->FputUint32(STATE_VERSION);
344         
345         if(state_entry != NULL) {
346                 state_entry->save_state(state_fio);
347         }
348         for(DEVICE* device = first_device; device; device = device->next_device) {
349                 device->save_state(state_fio);
350         }
351 }
352
353 bool VM::load_state(FILEIO* state_fio)
354 {
355         //if(state_fio->FgetUint32() != STATE_VERSION) {
356         //      return false;
357         //}
358         bool mb = false;
359         if(state_entry != NULL) {
360                 mb = state_entry->load_state(state_fio);
361         }
362         if(!mb) {
363                 emu->out_debug_log("INFO: HEADER DATA ERROR");
364                 return false;
365         }
366         for(DEVICE* device = first_device; device; device = device->next_device) {
367                 if(!device->load_state(state_fio)) {
368                         return false;
369                 }
370         }
371         return true;
372 }
373