OSDN Git Service

[VM][State] Apply new state framework to some VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / familybasic / familybasic.cpp
1 /*
2         Nintendo Family BASIC Emulator 'eFamilyBASIC'
3
4         Origin : nester
5         Author : Takeda.Toshiya
6         Date   : 2010.08.11-
7
8         [ virtual machine ]
9 */
10
11 #include "familybasic.h"
12 #include "../../emu.h"
13 #include "../device.h"
14 #include "../event.h"
15
16 #include "../datarec.h"
17 #include "../m6502.h"
18 #include "../noise.h"
19 #include "../ym2413.h"
20
21 #ifdef USE_DEBUGGER
22 #include "../debugger.h"
23 #endif
24
25 #include "memory.h"
26 #include "apu.h"
27 #include "ppu.h"
28
29 // ----------------------------------------------------------------------------
30 // initialize
31 // ----------------------------------------------------------------------------
32
33 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
34 {
35         // check configs
36 //      boot_mode = config.boot_mode;
37         boot_mode = -1;
38         
39         // create devices
40         first_device = last_device = NULL;
41         dummy = new DEVICE(this, emu);  // must be 1st device
42         event = new EVENT(this, emu);   // must be 2nd device
43         
44         drec = new DATAREC(this, emu);
45         drec->set_context_noise_play(new NOISE(this, emu));
46         drec->set_context_noise_stop(new NOISE(this, emu));
47         drec->set_context_noise_fast(new NOISE(this, emu));
48 //      cpu = new M6502(this, emu);
49         opll = new YM2413(this, emu);
50         
51         memory = new MEMORY(this, emu);
52         apu = new APU(this, emu);
53         ppu = new PPU(this, emu);
54         cpu = new N2A03(this, emu); // cpu shoud be reset after other device
55         
56         dummy->set_device_name(_T("1st Dummy"));
57         
58         // set contexts
59         event->set_context_cpu(cpu);
60         event->set_context_sound(apu);
61         event->set_context_sound(drec);
62         event->set_context_sound(opll);
63         event->set_context_sound(drec->get_context_noise_play());
64         event->set_context_sound(drec->get_context_noise_stop());
65         event->set_context_sound(drec->get_context_noise_fast());
66         
67         memory->set_context_cpu(cpu);
68         memory->set_context_apu(apu);
69         memory->set_context_ppu(ppu);
70         memory->set_context_drec(drec);
71         memory->set_context_opll(opll);
72         memory->set_spr_ram_ptr(ppu->get_spr_ram());
73         apu->set_context_cpu(cpu);
74         apu->set_context_memory(memory);
75         ppu->set_context_cpu(cpu);
76         ppu->set_context_memory(memory);
77         
78         // cpu bus
79         cpu->set_context_mem(memory);
80         cpu->set_context_intr(dummy);
81 #ifdef USE_DEBUGGER
82         cpu->set_context_debugger(new DEBUGGER(this, emu));
83 #endif
84         
85         // initialize all devices
86 #if defined(__GIT_REPO_VERSION)
87         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
88 #endif
89         for(DEVICE* device = first_device; device; device = device->next_device) {
90                 device->initialize();
91         }
92 }
93
94 VM::~VM()
95 {
96         // delete all devices
97         for(DEVICE* device = first_device; device;) {
98                 DEVICE *next_device = device->next_device;
99                 device->release();
100                 delete device;
101                 device = next_device;
102         }
103 }
104
105 DEVICE* VM::get_device(int id)
106 {
107         for(DEVICE* device = first_device; device; device = device->next_device) {
108                 if(device->this_device_id == id) {
109                         return device;
110                 }
111         }
112         return NULL;
113 }
114
115 // ----------------------------------------------------------------------------
116 // drive virtual machine
117 // ----------------------------------------------------------------------------
118
119 void VM::reset()
120 {
121         // load basic rom
122         if(boot_mode != config.boot_mode) {
123                 if(boot_mode != -1) {
124                         memory->save_backup();
125                 }
126                 if(config.boot_mode == 0) {
127                         memory->load_rom_image(_T("BASIC_V2.NES"));
128                         ppu->load_rom_image(_T("BASIC_V2.NES"));
129                 } else if(config.boot_mode == 1) {
130                         memory->load_rom_image(_T("BASIC_V3.NES"));
131                         ppu->load_rom_image(_T("BASIC_V3.NES"));
132                 } else if(config.boot_mode == 2) {
133                         memory->load_rom_image(_T("PLAYBOX_BASIC.NES"));
134                         ppu->load_rom_image(_T("PLAYBOX_BASIC.NES"));
135                 } else if(config.boot_mode == 3) {
136                         memory->load_rom_image(_T("VRC7_BASIC_V2.NES"));
137                         ppu->load_rom_image(_T("VRC7_BASIC_V2.NES"));
138                 } else if(config.boot_mode == 4) {
139                         memory->load_rom_image(_T("VRC7_BASIC_V3.NES"));
140                         ppu->load_rom_image(_T("VRC7_BASIC_V3.NES"));
141                 } else if(config.boot_mode == 5) {
142                         memory->load_rom_image(_T("MMC5_BASIC_V3.NES"));
143                         ppu->load_rom_image(_T("MMC5_BASIC_V3.NES"));
144                 }
145                 boot_mode = config.boot_mode;
146         }
147         
148         // reset all devices
149         for(DEVICE* device = first_device; device; device = device->next_device) {
150                 device->reset();
151         }
152 }
153
154 void VM::run()
155 {
156         event->drive();
157 }
158
159 // ----------------------------------------------------------------------------
160 // debugger
161 // ----------------------------------------------------------------------------
162
163 #ifdef USE_DEBUGGER
164 DEVICE *VM::get_cpu(int index)
165 {
166         if(index == 0) {
167                 return cpu;
168         }
169         return NULL;
170 }
171 #endif
172
173 // ----------------------------------------------------------------------------
174 // draw screen
175 // ----------------------------------------------------------------------------
176
177 void VM::draw_screen()
178 {
179         ppu->draw_screen();
180 }
181
182 // ----------------------------------------------------------------------------
183 // soud manager
184 // ----------------------------------------------------------------------------
185
186 void VM::initialize_sound(int rate, int samples)
187 {
188         // init sound manager
189         event->initialize_sound(rate, samples);
190         
191         // init sound gen
192         apu->initialize_sound(rate, samples);
193         opll->initialize_sound(rate, 3579545, samples);
194 }
195
196 uint16_t* VM::create_sound(int* extra_frames)
197 {
198         return event->create_sound(extra_frames);
199 }
200
201 int VM::get_sound_buffer_ptr()
202 {
203         return event->get_sound_buffer_ptr();
204 }
205
206 #ifdef USE_SOUND_VOLUME
207 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
208 {
209         if(ch == 0) {
210                 apu->set_volume(0, decibel_l, decibel_r);
211         } else if(ch == 1) {
212                 opll->set_volume(0, decibel_l, decibel_r);
213         } else if(ch == 2) {
214                 drec->set_volume(0, decibel_l, decibel_r);
215         } else if(ch == 3) {
216                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
217                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
218                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
219         }
220 }
221 #endif
222
223 // ----------------------------------------------------------------------------
224 // user interface
225 // ----------------------------------------------------------------------------
226
227 void VM::play_tape(int drv, const _TCHAR* file_path)
228 {
229         drec->play_tape(file_path);
230 //      drec->set_remote(true);
231 }
232
233 void VM::rec_tape(int drv, const _TCHAR* file_path)
234 {
235         drec->rec_tape(file_path);
236 //      drec->set_remote(true);
237 }
238
239 void VM::close_tape(int drv)
240 {
241         emu->lock_vm();
242         drec->close_tape();
243         emu->unlock_vm();
244 //      drec->set_remote(false);
245 }
246
247 bool VM::is_tape_inserted(int drv)
248 {
249         return drec->is_tape_inserted();
250 }
251
252 bool VM::is_tape_playing(int drv)
253 {
254         return drec->is_tape_playing();
255 }
256
257 bool VM::is_tape_recording(int drv)
258 {
259         return drec->is_tape_recording();
260 }
261
262 int VM::get_tape_position(int drv)
263 {
264         return drec->get_tape_position();
265 }
266
267 const _TCHAR* VM::get_tape_message(int drv)
268 {
269         return drec->get_message();
270 }
271
272 void VM::push_play(int drv)
273 {
274         drec->set_ff_rew(0);
275         drec->set_remote(true);
276 }
277
278 void VM::push_stop(int drv)
279 {
280         drec->set_remote(false);
281 }
282
283 void VM::push_fast_forward(int drv)
284 {
285         drec->set_ff_rew(1);
286         drec->set_remote(true);
287 }
288
289 void VM::push_fast_rewind(int drv)
290 {
291         drec->set_ff_rew(-1);
292         drec->set_remote(true);
293 }
294
295 bool VM::is_frame_skippable()
296 {
297         return event->is_frame_skippable();
298 }
299
300 void VM::update_config()
301 {
302         if(boot_mode != config.boot_mode) {
303                 // boot mode is changed !!!
304 //              boot_mode = config.boot_mode;
305                 reset();
306         } else {
307                 for(DEVICE* device = first_device; device; device = device->next_device) {
308                         device->update_config();
309                 }
310         }
311 }
312
313 #define STATE_VERSION   6
314
315 bool VM::process_state(FILEIO* state_fio, bool loading)
316 {
317         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
318                 return false;
319         }
320         for(DEVICE* device = first_device; device; device = device->next_device) {
321                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
322                 // const char *name = typeid(*device).name();
323                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
324                 const char *name = device->get_device_name();
325                 int len = strlen(name);
326                 
327                 if(!state_fio->StateCheckInt32(len)) {
328                         if(loading) {
329                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
330                         }
331                         return false;
332                 }
333                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
334                         if(loading) {
335                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
336                         }
337                         return false;
338                 }
339                 if(!device->process_state(state_fio, loading)) {
340                         if(loading) {
341                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
342                         }
343                         return false;
344                 }
345         }
346         // Machine specified.
347         state_fio->StateInt32(boot_mode);
348         if(loading) {
349                 update_config();
350         }
351         return true;
352 }
353