OSDN Git Service

[VM][STATE] Remove typeid() from state data, this seems to be buggy.
[csp-qt/common_source_project-fm7.git] / source / src / vm / yis / yis.cpp
1 /*
2         YAMAHA YIS Emulator 'eYIS'
3
4         Author : Takeda.Toshiya
5         Date   : 2017.04.13-
6
7         [ virtual machine ]
8 */
9
10 #include "yis.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../m6502.h"
16 #include "../io.h"
17 #include "../memory.h"
18 #include "../am9511.h"
19 #include "../beep.h"
20 #include "../disk.h"
21 #include "../mb8877.h"
22 #include "../mc6820.h"
23 #include "../mc6844.h"
24 #include "../mc6850.h"
25 #include "../msm58321.h"
26 #include "../noise.h"
27
28 #ifdef USE_DEBUGGER
29 #include "../debugger.h"
30 #endif
31
32 #include "calendar.h"
33 #include "display.h"
34 #include "floppy.h"
35 #include "keyboard.h"
36 #include "mapper.h"
37 #include "sound.h"
38
39 // ----------------------------------------------------------------------------
40 // initialize
41 // ----------------------------------------------------------------------------
42
43 VM::VM(EMU* parent_emu) : emu(parent_emu)
44 {
45         // create devices
46         first_device = last_device = NULL;
47         dummy = new DEVICE(this, emu);  // must be 1st device
48         event = new EVENT(this, emu);   // must be 2nd device
49         
50         cpu = new M6502(this, emu);     // YM-2002
51         io = new IO(this, emu);
52         memory = new MEMORY(this, emu);
53         apu = new AM9511(this, emu);
54         beep = new BEEP(this, emu);
55         fdc = new MB8877(this, emu);
56         fdc->set_context_noise_seek(new NOISE(this, emu));
57         fdc->set_context_noise_head_down(new NOISE(this, emu));
58         fdc->set_context_noise_head_up(new NOISE(this, emu));
59         pia = new MC6820(this, emu);    // MB8874
60         dma = new MC6844(this, emu);    // HD46504
61         acia1 = new MC6850(this, emu);  // MB8863
62         acia2 = new MC6850(this, emu);
63         rtc = new MSM58321(this, emu);  // MSM5832RS
64         
65         calendar = new CALENDAR(this, emu);
66         display = new DISPLAY(this, emu);
67         floppy = new FLOPPY(this, emu);
68         keyboard = new KEYBOARD(this, emu);
69         mapper = new MAPPER(this, emu);
70         sound = new SOUND(this, emu);
71         
72         // set contexts
73         event->set_context_cpu(cpu);
74         event->set_context_sound(beep);
75         event->set_context_sound(fdc->get_context_noise_seek());
76         event->set_context_sound(fdc->get_context_noise_head_down());
77         event->set_context_sound(fdc->get_context_noise_head_up());
78         
79         fdc->set_context_drq(dma, SIG_MC6844_TX_RQ_0, 1);
80         dma->set_context_memory(memory);
81         dma->set_context_ch0(fdc);
82         
83         calendar->set_context_rtc(rtc);
84         floppy->set_context_fdc(fdc);
85         keyboard->set_context_cpu(cpu);
86         mapper->set_context_memory(memory);
87         sound->set_context_beep(beep);
88         
89         // cpu bus
90         cpu->set_context_mem(memory);
91         cpu->set_context_intr(dummy);
92 #ifdef USE_DEBUGGER
93         cpu->set_context_debugger(new DEBUGGER(this, emu));
94 #endif
95         
96         // memory bus
97         memory->read_bios(_T("BIOS.ROM"), rom, sizeof(rom));
98         // $0000-$efff will be mapped in mapper class
99         memory->set_memory_mapped_io_rw(0xf000, 0xf0ff, io);
100         memory->set_memory_r(0xf100, 0xffff, rom + 0x100);
101         
102         // io bus
103         io->set_iomap_range_rw(0xf000, 0xf016, dma);
104         io->set_iomap_range_rw(0xf020, 0xf023, fdc);
105         io->set_iomap_single_w(0xf024, floppy);
106         io->set_iomap_single_rw(0xf030, keyboard);
107         io->set_iomap_single_r(0xf031, keyboard);
108         io->set_iomap_range_w(0xf031, 0xf032, sound);
109         io->set_iomap_range_rw(0xf034, 0xf035, apu);
110         io->set_iomap_range_rw(0xf036, 0xf037, calendar);
111         io->set_iomap_range_rw(0xf038, 0xf03b, pia);
112         io->set_iomap_range_rw(0xf03c, 0xf03d, acia1);
113         io->set_iomap_range_rw(0xf03e, 0xf03f, acia2);
114         io->set_iomap_range_rw(0xf040, 0xf04b, display);
115         io->set_iomap_range_rw(0xf04f, 0xf05f, mapper);
116 //      io->set_iomap_range_rw(0xf060, 0xf06f, keyboard);
117         
118         // initialize all devices
119         for(DEVICE* device = first_device; device; device = device->next_device) {
120                 device->initialize();
121         }
122         for(int i = 0; i < MAX_DRIVE; i++) {
123                 fdc->set_drive_type(i, DRIVE_TYPE_2DD); // 1DD
124                 fdc->set_track_size(i, 6238);
125         }
126 }
127
128 VM::~VM()
129 {
130         // delete all devices
131         for(DEVICE* device = first_device; device;) {
132                 DEVICE *next_device = device->next_device;
133                 device->release();
134                 delete device;
135                 device = next_device;
136         }
137 }
138
139 DEVICE* VM::get_device(int id)
140 {
141         for(DEVICE* device = first_device; device; device = device->next_device) {
142                 if(device->this_device_id == id) {
143                         return device;
144                 }
145         }
146         return NULL;
147 }
148
149 // ----------------------------------------------------------------------------
150 // drive virtual machine
151 // ----------------------------------------------------------------------------
152
153 void VM::reset()
154 {
155         // reset all devices
156         for(DEVICE* device = first_device; device; device = device->next_device) {
157                 device->reset();
158         }
159 }
160
161 void VM::run()
162 {
163         event->drive();
164 }
165
166 // ----------------------------------------------------------------------------
167 // debugger
168 // ----------------------------------------------------------------------------
169
170 #ifdef USE_DEBUGGER
171 DEVICE *VM::get_cpu(int index)
172 {
173         if(index == 0) {
174                 return cpu;
175         }
176         return NULL;
177 }
178 #endif
179
180 // ----------------------------------------------------------------------------
181 // draw screen
182 // ----------------------------------------------------------------------------
183
184 void VM::draw_screen()
185 {
186         display->draw_screen();
187 }
188
189 // ----------------------------------------------------------------------------
190 // soud manager
191 // ----------------------------------------------------------------------------
192
193 void VM::initialize_sound(int rate, int samples)
194 {
195         // init sound manager
196         event->initialize_sound(rate, samples);
197         
198         // init sound gen
199         beep->initialize_sound(rate, 49152 / 0x80, 8000);
200 }
201
202 uint16_t* VM::create_sound(int* extra_frames)
203 {
204         return event->create_sound(extra_frames);
205 }
206
207 int VM::get_sound_buffer_ptr()
208 {
209         return event->get_sound_buffer_ptr();
210 }
211
212 #ifdef USE_SOUND_VOLUME
213 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
214 {
215         if(ch == 0) {
216                 beep->set_volume(0, decibel_l, decibel_r);
217         } else if(ch == 1) {
218                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
219                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
220                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
221         }
222 }
223 #endif
224
225 // ----------------------------------------------------------------------------
226 // notify key
227 // ----------------------------------------------------------------------------
228
229 void VM::key_down(int code, bool repeat)
230 {
231         if(!repeat) {
232                 keyboard->key_down(code);
233         }
234 }
235
236 void VM::key_up(int code)
237 {
238 }
239
240 bool VM::get_caps_locked()
241 {
242         return keyboard->get_caps_locked();
243 }
244
245 bool VM::get_kana_locked()
246 {
247         return keyboard->get_kana_locked();
248 }
249
250 // ----------------------------------------------------------------------------
251 // user interface
252 // ----------------------------------------------------------------------------
253
254 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
255 {
256         fdc->open_disk(drv, file_path, bank);
257 }
258
259 void VM::close_floppy_disk(int drv)
260 {
261         fdc->close_disk(drv);
262 }
263
264 bool VM::is_floppy_disk_inserted(int drv)
265 {
266         return fdc->is_disk_inserted(drv);
267 }
268
269 void VM::is_floppy_disk_protected(int drv, bool value)
270 {
271         fdc->is_disk_protected(drv, value);
272 }
273
274 bool VM::is_floppy_disk_protected(int drv)
275 {
276         return fdc->is_disk_protected(drv);
277 }
278
279 uint32_t VM::is_floppy_disk_accessed()
280 {
281         return fdc->read_signal(0);
282 }
283
284 bool VM::is_frame_skippable()
285 {
286         return event->is_frame_skippable();
287 }
288
289 void VM::update_config()
290 {
291         for(DEVICE* device = first_device; device; device = device->next_device) {
292                 device->update_config();
293         }
294 }
295
296 #define STATE_VERSION   2
297
298 void VM::save_state(FILEIO* state_fio)
299 {
300         state_fio->FputUint32(STATE_VERSION);
301         
302         for(DEVICE* device = first_device; device; device = device->next_device) {
303                 device->save_state(state_fio);
304         }
305 }
306
307 bool VM::load_state(FILEIO* state_fio)
308 {
309         if(state_fio->FgetUint32() != STATE_VERSION) {
310                 return false;
311         }
312         for(DEVICE* device = first_device; device; device = device->next_device) {
313                 if(!device->load_state(state_fio)) {
314                         return false;
315                 }
316         }
317         return true;
318 }
319