OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[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         decl_state();
93 }
94
95 VM::~VM()
96 {
97         // delete all devices
98         for(DEVICE* device = first_device; device;) {
99                 DEVICE *next_device = device->next_device;
100                 device->release();
101                 delete device;
102                 device = next_device;
103         }
104 }
105
106 DEVICE* VM::get_device(int id)
107 {
108         for(DEVICE* device = first_device; device; device = device->next_device) {
109                 if(device->this_device_id == id) {
110                         return device;
111                 }
112         }
113         return NULL;
114 }
115
116 // ----------------------------------------------------------------------------
117 // drive virtual machine
118 // ----------------------------------------------------------------------------
119
120 void VM::reset()
121 {
122         // load basic rom
123         if(boot_mode != config.boot_mode) {
124                 if(boot_mode != -1) {
125                         memory->save_backup();
126                 }
127                 if(config.boot_mode == 0) {
128                         memory->load_rom_image(_T("BASIC_V2.NES"));
129                         ppu->load_rom_image(_T("BASIC_V2.NES"));
130                 } else if(config.boot_mode == 1) {
131                         memory->load_rom_image(_T("BASIC_V3.NES"));
132                         ppu->load_rom_image(_T("BASIC_V3.NES"));
133                 } else if(config.boot_mode == 2) {
134                         memory->load_rom_image(_T("PLAYBOX_BASIC.NES"));
135                         ppu->load_rom_image(_T("PLAYBOX_BASIC.NES"));
136                 } else if(config.boot_mode == 3) {
137                         memory->load_rom_image(_T("VRC7_BASIC_V2.NES"));
138                         ppu->load_rom_image(_T("VRC7_BASIC_V2.NES"));
139                 } else if(config.boot_mode == 4) {
140                         memory->load_rom_image(_T("VRC7_BASIC_V3.NES"));
141                         ppu->load_rom_image(_T("VRC7_BASIC_V3.NES"));
142                 } else if(config.boot_mode == 5) {
143                         memory->load_rom_image(_T("MMC5_BASIC_V3.NES"));
144                         ppu->load_rom_image(_T("MMC5_BASIC_V3.NES"));
145                 }
146                 boot_mode = config.boot_mode;
147         }
148         
149         // reset all devices
150         for(DEVICE* device = first_device; device; device = device->next_device) {
151                 device->reset();
152         }
153 }
154
155 void VM::run()
156 {
157         event->drive();
158 }
159
160 // ----------------------------------------------------------------------------
161 // debugger
162 // ----------------------------------------------------------------------------
163
164 #ifdef USE_DEBUGGER
165 DEVICE *VM::get_cpu(int index)
166 {
167         if(index == 0) {
168                 return cpu;
169         }
170         return NULL;
171 }
172 #endif
173
174 // ----------------------------------------------------------------------------
175 // draw screen
176 // ----------------------------------------------------------------------------
177
178 void VM::draw_screen()
179 {
180         ppu->draw_screen();
181 }
182
183 // ----------------------------------------------------------------------------
184 // soud manager
185 // ----------------------------------------------------------------------------
186
187 void VM::initialize_sound(int rate, int samples)
188 {
189         // init sound manager
190         event->initialize_sound(rate, samples);
191         
192         // init sound gen
193         apu->initialize_sound(rate, samples);
194         opll->initialize_sound(rate, 3579545, samples);
195 }
196
197 uint16_t* VM::create_sound(int* extra_frames)
198 {
199         return event->create_sound(extra_frames);
200 }
201
202 int VM::get_sound_buffer_ptr()
203 {
204         return event->get_sound_buffer_ptr();
205 }
206
207 #ifdef USE_SOUND_VOLUME
208 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
209 {
210         if(ch == 0) {
211                 apu->set_volume(0, decibel_l, decibel_r);
212         } else if(ch == 1) {
213                 opll->set_volume(0, decibel_l, decibel_r);
214         } else if(ch == 2) {
215                 drec->set_volume(0, decibel_l, decibel_r);
216         } else if(ch == 3) {
217                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
218                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
219                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
220         }
221 }
222 #endif
223
224 // ----------------------------------------------------------------------------
225 // user interface
226 // ----------------------------------------------------------------------------
227
228 void VM::play_tape(int drv, const _TCHAR* file_path)
229 {
230         drec->play_tape(file_path);
231 //      drec->set_remote(true);
232 }
233
234 void VM::rec_tape(int drv, const _TCHAR* file_path)
235 {
236         drec->rec_tape(file_path);
237 //      drec->set_remote(true);
238 }
239
240 void VM::close_tape(int drv)
241 {
242         emu->lock_vm();
243         drec->close_tape();
244         emu->unlock_vm();
245 //      drec->set_remote(false);
246 }
247
248 bool VM::is_tape_inserted(int drv)
249 {
250         return drec->is_tape_inserted();
251 }
252
253 bool VM::is_tape_playing(int drv)
254 {
255         return drec->is_tape_playing();
256 }
257
258 bool VM::is_tape_recording(int drv)
259 {
260         return drec->is_tape_recording();
261 }
262
263 int VM::get_tape_position(int drv)
264 {
265         return drec->get_tape_position();
266 }
267
268 const _TCHAR* VM::get_tape_message(int drv)
269 {
270         return drec->get_message();
271 }
272
273 void VM::push_play(int drv)
274 {
275         drec->set_ff_rew(0);
276         drec->set_remote(true);
277 }
278
279 void VM::push_stop(int drv)
280 {
281         drec->set_remote(false);
282 }
283
284 void VM::push_fast_forward(int drv)
285 {
286         drec->set_ff_rew(1);
287         drec->set_remote(true);
288 }
289
290 void VM::push_fast_rewind(int drv)
291 {
292         drec->set_ff_rew(-1);
293         drec->set_remote(true);
294 }
295
296 bool VM::is_frame_skippable()
297 {
298         return event->is_frame_skippable();
299 }
300
301 void VM::update_config()
302 {
303         if(boot_mode != config.boot_mode) {
304                 // boot mode is changed !!!
305 //              boot_mode = config.boot_mode;
306                 reset();
307         } else {
308                 for(DEVICE* device = first_device; device; device = device->next_device) {
309                         device->update_config();
310                 }
311         }
312 }
313
314 #define STATE_VERSION   5
315
316 #include "../../statesub.h"
317 #include "../../qt/gui/csp_logger.h"
318 extern CSP_Logger DLL_PREFIX_I *csp_logger;
319
320 void VM::decl_state(void)
321 {
322         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FAMILY_BASIC_HEAD")), csp_logger);
323         DECL_STATE_ENTRY_INT32(boot_mode);
324         
325         for(DEVICE* device = first_device; device; device = device->next_device) {
326                 device->decl_state();
327         }
328 }
329 void VM::save_state(FILEIO* state_fio)
330 {
331         //state_fio->FputUint32(STATE_VERSION);
332         
333         if(state_entry != NULL) {
334                 state_entry->save_state(state_fio);
335         }
336         for(DEVICE* device = first_device; device; device = device->next_device) {
337                 device->save_state(state_fio);
338         }
339         //state_fio->FputInt32(boot_mode); // Move to decl_state().
340 }
341
342 bool VM::load_state(FILEIO* state_fio)
343 {
344         //if(state_fio->FgetUint32() != STATE_VERSION) {
345         //      return false;
346         //}
347         bool mb = false;
348         if(state_entry != NULL) {
349                 mb = state_entry->load_state(state_fio);
350         }
351         if(!mb) {
352                 emu->out_debug_log("INFO: HEADER DATA ERROR");
353                 return false;
354         }
355         for(DEVICE* device = first_device; device; device = device->next_device) {
356                 if(!device->load_state(state_fio)) {
357                         return false;
358                 }
359         }
360         //boot_mode = state_fio->FgetInt32();
361         return true;
362 }
363