2 Nintendo Family BASIC Emulator 'eFamilyBASIC'
5 Author : Takeda.Toshiya
11 #include "familybasic.h"
12 #include "../../emu.h"
13 #include "../device.h"
16 #include "../datarec.h"
19 #include "../ym2413.h"
22 #include "../debugger.h"
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
36 // boot_mode = config.boot_mode;
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
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);
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
56 dummy->set_device_name(_T("1st Dummy"));
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());
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);
79 cpu->set_context_mem(memory);
80 cpu->set_context_intr(dummy);
82 cpu->set_context_debugger(new DEBUGGER(this, emu));
85 // initialize all devices
86 for(DEVICE* device = first_device; device; device = device->next_device) {
95 for(DEVICE* device = first_device; device;) {
96 DEVICE *next_device = device->next_device;
103 DEVICE* VM::get_device(int id)
105 for(DEVICE* device = first_device; device; device = device->next_device) {
106 if(device->this_device_id == id) {
113 // ----------------------------------------------------------------------------
114 // drive virtual machine
115 // ----------------------------------------------------------------------------
120 if(boot_mode != config.boot_mode) {
121 if(boot_mode != -1) {
122 memory->save_backup();
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"));
143 boot_mode = config.boot_mode;
147 for(DEVICE* device = first_device; device; device = device->next_device) {
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
162 DEVICE *VM::get_cpu(int index)
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 void VM::draw_screen()
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 void VM::initialize_sound(int rate, int samples)
186 // init sound manager
187 event->initialize_sound(rate, samples);
190 apu->initialize_sound(rate, samples);
191 opll->initialize_sound(rate, 3579545, samples);
194 uint16_t* VM::create_sound(int* extra_frames)
196 return event->create_sound(extra_frames);
199 int VM::get_sound_buffer_ptr()
201 return event->get_sound_buffer_ptr();
204 #ifdef USE_SOUND_VOLUME
205 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
208 apu->set_volume(0, decibel_l, decibel_r);
210 opll->set_volume(0, decibel_l, decibel_r);
212 drec->set_volume(0, decibel_l, decibel_r);
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);
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
225 void VM::play_tape(int drv, const _TCHAR* file_path)
227 drec->play_tape(file_path);
228 // drec->set_remote(true);
231 void VM::rec_tape(int drv, const _TCHAR* file_path)
233 drec->rec_tape(file_path);
234 // drec->set_remote(true);
237 void VM::close_tape(int drv)
242 // drec->set_remote(false);
245 bool VM::is_tape_inserted(int drv)
247 return drec->is_tape_inserted();
250 bool VM::is_tape_playing(int drv)
252 return drec->is_tape_playing();
255 bool VM::is_tape_recording(int drv)
257 return drec->is_tape_recording();
260 int VM::get_tape_position(int drv)
262 return drec->get_tape_position();
265 const _TCHAR* VM::get_tape_message(int drv)
267 return drec->get_message();
270 void VM::push_play(int drv)
273 drec->set_remote(true);
276 void VM::push_stop(int drv)
278 drec->set_remote(false);
281 void VM::push_fast_forward(int drv)
284 drec->set_remote(true);
287 void VM::push_fast_rewind(int drv)
289 drec->set_ff_rew(-1);
290 drec->set_remote(true);
293 bool VM::is_frame_skippable()
295 return event->is_frame_skippable();
298 void VM::update_config()
300 if(boot_mode != config.boot_mode) {
301 // boot mode is changed !!!
302 // boot_mode = config.boot_mode;
305 for(DEVICE* device = first_device; device; device = device->next_device) {
306 device->update_config();
311 #define STATE_VERSION 5
313 #include "../../statesub.h"
314 #include "../../qt/gui/csp_logger.h"
315 extern CSP_Logger DLL_PREFIX_I *csp_logger;
317 void VM::decl_state(void)
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);
322 for(DEVICE* device = first_device; device; device = device->next_device) {
323 device->decl_state();
326 void VM::save_state(FILEIO* state_fio)
328 //state_fio->FputUint32(STATE_VERSION);
330 if(state_entry != NULL) {
331 state_entry->save_state(state_fio);
333 for(DEVICE* device = first_device; device; device = device->next_device) {
334 device->save_state(state_fio);
336 //state_fio->FputInt32(boot_mode); // Move to decl_state().
339 bool VM::load_state(FILEIO* state_fio)
341 //if(state_fio->FgetUint32() != STATE_VERSION) {
345 if(state_entry != NULL) {
346 mb = state_entry->load_state(state_fio);
349 emu->out_debug_log("INFO: HEADER DATA ERROR");
352 for(DEVICE* device = first_device; device; device = device->next_device) {
353 if(!device->load_state(state_fio)) {
357 //boot_mode = state_fio->FgetInt32();