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) {
97 for(DEVICE* device = first_device; device;) {
98 DEVICE *next_device = device->next_device;
101 device = next_device;
105 DEVICE* VM::get_device(int id)
107 for(DEVICE* device = first_device; device; device = device->next_device) {
108 if(device->this_device_id == id) {
115 // ----------------------------------------------------------------------------
116 // drive virtual machine
117 // ----------------------------------------------------------------------------
122 if(boot_mode != config.boot_mode) {
123 if(boot_mode != -1) {
124 memory->save_backup();
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"));
145 boot_mode = config.boot_mode;
149 for(DEVICE* device = first_device; device; device = device->next_device) {
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
164 DEVICE *VM::get_cpu(int index)
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 void VM::draw_screen()
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 void VM::initialize_sound(int rate, int samples)
188 // init sound manager
189 event->initialize_sound(rate, samples);
192 apu->initialize_sound(rate, samples);
193 opll->initialize_sound(rate, 3579545, samples);
196 uint16_t* VM::create_sound(int* extra_frames)
198 return event->create_sound(extra_frames);
201 int VM::get_sound_buffer_ptr()
203 return event->get_sound_buffer_ptr();
206 #ifdef USE_SOUND_VOLUME
207 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
210 apu->set_volume(0, decibel_l, decibel_r);
212 opll->set_volume(0, decibel_l, decibel_r);
214 drec->set_volume(0, decibel_l, decibel_r);
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);
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
227 void VM::play_tape(int drv, const _TCHAR* file_path)
229 drec->play_tape(file_path);
230 // drec->set_remote(true);
233 void VM::rec_tape(int drv, const _TCHAR* file_path)
235 drec->rec_tape(file_path);
236 // drec->set_remote(true);
239 void VM::close_tape(int drv)
244 // drec->set_remote(false);
247 bool VM::is_tape_inserted(int drv)
249 return drec->is_tape_inserted();
252 bool VM::is_tape_playing(int drv)
254 return drec->is_tape_playing();
257 bool VM::is_tape_recording(int drv)
259 return drec->is_tape_recording();
262 int VM::get_tape_position(int drv)
264 return drec->get_tape_position();
267 const _TCHAR* VM::get_tape_message(int drv)
269 return drec->get_message();
272 void VM::push_play(int drv)
275 drec->set_remote(true);
278 void VM::push_stop(int drv)
280 drec->set_remote(false);
283 void VM::push_fast_forward(int drv)
286 drec->set_remote(true);
289 void VM::push_fast_rewind(int drv)
291 drec->set_ff_rew(-1);
292 drec->set_remote(true);
295 bool VM::is_frame_skippable()
297 return event->is_frame_skippable();
300 void VM::update_config()
302 if(boot_mode != config.boot_mode) {
303 // boot mode is changed !!!
304 // boot_mode = config.boot_mode;
307 for(DEVICE* device = first_device; device; device = device->next_device) {
308 device->update_config();
313 #define STATE_VERSION 6
315 bool VM::process_state(FILEIO* state_fio, bool loading)
317 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
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);
327 if(!state_fio->StateCheckInt32(len)) {
329 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
333 if(!state_fio->StateCheckBuffer(name, len, 1)) {
335 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
339 if(!device->process_state(state_fio, loading)) {
341 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
346 // Machine specified.
347 state_fio->StateInt32(boot_mode);