OSDN Git Service

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