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 #if defined(__GIT_REPO_VERSION)
87 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
89 for(DEVICE* device = first_device; device; device = device->next_device) {
98 for(DEVICE* device = first_device; device;) {
99 DEVICE *next_device = device->next_device;
102 device = next_device;
106 DEVICE* VM::get_device(int id)
108 for(DEVICE* device = first_device; device; device = device->next_device) {
109 if(device->this_device_id == id) {
116 // ----------------------------------------------------------------------------
117 // drive virtual machine
118 // ----------------------------------------------------------------------------
123 if(boot_mode != config.boot_mode) {
124 if(boot_mode != -1) {
125 memory->save_backup();
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"));
146 boot_mode = config.boot_mode;
150 for(DEVICE* device = first_device; device; device = device->next_device) {
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
165 DEVICE *VM::get_cpu(int index)
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 void VM::draw_screen()
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 void VM::initialize_sound(int rate, int samples)
189 // init sound manager
190 event->initialize_sound(rate, samples);
193 apu->initialize_sound(rate, samples);
194 opll->initialize_sound(rate, 3579545, samples);
197 uint16_t* VM::create_sound(int* extra_frames)
199 return event->create_sound(extra_frames);
202 int VM::get_sound_buffer_ptr()
204 return event->get_sound_buffer_ptr();
207 #ifdef USE_SOUND_VOLUME
208 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
211 apu->set_volume(0, decibel_l, decibel_r);
213 opll->set_volume(0, decibel_l, decibel_r);
215 drec->set_volume(0, decibel_l, decibel_r);
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);
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
228 void VM::play_tape(int drv, const _TCHAR* file_path)
230 drec->play_tape(file_path);
231 // drec->set_remote(true);
234 void VM::rec_tape(int drv, const _TCHAR* file_path)
236 drec->rec_tape(file_path);
237 // drec->set_remote(true);
240 void VM::close_tape(int drv)
245 // drec->set_remote(false);
248 bool VM::is_tape_inserted(int drv)
250 return drec->is_tape_inserted();
253 bool VM::is_tape_playing(int drv)
255 return drec->is_tape_playing();
258 bool VM::is_tape_recording(int drv)
260 return drec->is_tape_recording();
263 int VM::get_tape_position(int drv)
265 return drec->get_tape_position();
268 const _TCHAR* VM::get_tape_message(int drv)
270 return drec->get_message();
273 void VM::push_play(int drv)
276 drec->set_remote(true);
279 void VM::push_stop(int drv)
281 drec->set_remote(false);
284 void VM::push_fast_forward(int drv)
287 drec->set_remote(true);
290 void VM::push_fast_rewind(int drv)
292 drec->set_ff_rew(-1);
293 drec->set_remote(true);
296 bool VM::is_frame_skippable()
298 return event->is_frame_skippable();
301 void VM::update_config()
303 if(boot_mode != config.boot_mode) {
304 // boot mode is changed !!!
305 // boot_mode = config.boot_mode;
308 for(DEVICE* device = first_device; device; device = device->next_device) {
309 device->update_config();
314 #define STATE_VERSION 5
316 #include "../../statesub.h"
317 #include "../../qt/gui/csp_logger.h"
318 extern CSP_Logger DLL_PREFIX_I *csp_logger;
320 void VM::decl_state(void)
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);
325 for(DEVICE* device = first_device; device; device = device->next_device) {
326 device->decl_state();
329 void VM::save_state(FILEIO* state_fio)
331 //state_fio->FputUint32(STATE_VERSION);
333 if(state_entry != NULL) {
334 state_entry->save_state(state_fio);
336 for(DEVICE* device = first_device; device; device = device->next_device) {
337 device->save_state(state_fio);
339 //state_fio->FputInt32(boot_mode); // Move to decl_state().
342 bool VM::load_state(FILEIO* state_fio)
344 //if(state_fio->FgetUint32() != STATE_VERSION) {
348 if(state_entry != NULL) {
349 mb = state_entry->load_state(state_fio);
352 emu->out_debug_log("INFO: HEADER DATA ERROR");
355 for(DEVICE* device = first_device; device; device = device->next_device) {
356 if(!device->load_state(state_fio)) {
360 //boot_mode = state_fio->FgetInt32();