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) : emu(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) {
94 for(DEVICE* device = first_device; device;) {
95 DEVICE *next_device = device->next_device;
102 DEVICE* VM::get_device(int id)
104 for(DEVICE* device = first_device; device; device = device->next_device) {
105 if(device->this_device_id == id) {
112 // ----------------------------------------------------------------------------
113 // drive virtual machine
114 // ----------------------------------------------------------------------------
119 if(boot_mode != config.boot_mode) {
120 if(boot_mode != -1) {
121 memory->save_backup();
123 if(config.boot_mode == 0) {
124 memory->load_rom_image(_T("BASIC_V2.NES"));
125 ppu->load_rom_image(_T("BASIC_V2.NES"));
126 } else if(config.boot_mode == 1) {
127 memory->load_rom_image(_T("BASIC_V3.NES"));
128 ppu->load_rom_image(_T("BASIC_V3.NES"));
129 } else if(config.boot_mode == 2) {
130 memory->load_rom_image(_T("PLAYBOX_BASIC.NES"));
131 ppu->load_rom_image(_T("PLAYBOX_BASIC.NES"));
132 } else if(config.boot_mode == 3) {
133 memory->load_rom_image(_T("VRC7_BASIC_V2.NES"));
134 ppu->load_rom_image(_T("VRC7_BASIC_V2.NES"));
135 } else if(config.boot_mode == 4) {
136 memory->load_rom_image(_T("VRC7_BASIC_V3.NES"));
137 ppu->load_rom_image(_T("VRC7_BASIC_V3.NES"));
138 } else if(config.boot_mode == 5) {
139 memory->load_rom_image(_T("MMC5_BASIC_V3.NES"));
140 ppu->load_rom_image(_T("MMC5_BASIC_V3.NES"));
142 boot_mode = config.boot_mode;
146 for(DEVICE* device = first_device; device; device = device->next_device) {
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
161 DEVICE *VM::get_cpu(int index)
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 void VM::draw_screen()
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 void VM::initialize_sound(int rate, int samples)
185 // init sound manager
186 event->initialize_sound(rate, samples);
189 apu->initialize_sound(rate, samples);
190 opll->initialize_sound(rate, 3579545, samples);
193 uint16_t* VM::create_sound(int* extra_frames)
195 return event->create_sound(extra_frames);
198 int VM::get_sound_buffer_ptr()
200 return event->get_sound_buffer_ptr();
203 #ifdef USE_SOUND_VOLUME
204 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
207 apu->set_volume(0, decibel_l, decibel_r);
209 opll->set_volume(0, decibel_l, decibel_r);
211 drec->set_volume(0, decibel_l, decibel_r);
213 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
214 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
215 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
220 // ----------------------------------------------------------------------------
222 // ----------------------------------------------------------------------------
224 void VM::play_tape(int drv, const _TCHAR* file_path)
226 drec->play_tape(file_path);
227 // drec->set_remote(true);
230 void VM::rec_tape(int drv, const _TCHAR* file_path)
232 drec->rec_tape(file_path);
233 // drec->set_remote(true);
236 void VM::close_tape(int drv)
241 // drec->set_remote(false);
244 bool VM::is_tape_inserted(int drv)
246 return drec->is_tape_inserted();
249 bool VM::is_tape_playing(int drv)
251 return drec->is_tape_playing();
254 bool VM::is_tape_recording(int drv)
256 return drec->is_tape_recording();
259 int VM::get_tape_position(int drv)
261 return drec->get_tape_position();
264 const _TCHAR* VM::get_tape_message(int drv)
266 return drec->get_message();
269 void VM::push_play(int drv)
272 drec->set_remote(true);
275 void VM::push_stop(int drv)
277 drec->set_remote(false);
280 void VM::push_fast_forward(int drv)
283 drec->set_remote(true);
286 void VM::push_fast_rewind(int drv)
288 drec->set_ff_rew(-1);
289 drec->set_remote(true);
292 bool VM::is_frame_skippable()
294 return event->is_frame_skippable();
297 void VM::update_config()
299 if(boot_mode != config.boot_mode) {
300 // boot mode is changed !!!
301 // boot_mode = config.boot_mode;
304 for(DEVICE* device = first_device; device; device = device->next_device) {
305 device->update_config();
310 #define STATE_VERSION 5
312 void VM::save_state(FILEIO* state_fio)
314 state_fio->FputUint32(STATE_VERSION);
316 for(DEVICE* device = first_device; device; device = device->next_device) {
317 device->save_state(state_fio);
319 state_fio->FputInt32(boot_mode);
322 bool VM::load_state(FILEIO* state_fio)
324 if(state_fio->FgetUint32() != STATE_VERSION) {
327 for(DEVICE* device = first_device; device; device = device->next_device) {
328 if(!device->load_state(state_fio)) {
332 boot_mode = state_fio->FgetInt32();