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"
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
27 VM::VM(EMU* parent_emu) : emu(parent_emu)
30 // boot_mode = config.boot_mode;
34 first_device = last_device = NULL;
35 dummy = new DEVICE(this, emu); // must be 1st device
36 event = new EVENT(this, emu); // must be 2nd device
38 drec = new DATAREC(this, emu);
39 cpu = new M6502(this, emu);
41 memory = new MEMORY(this, emu);
42 apu = new APU(this, emu);
43 ppu = new PPU(this, emu);
46 event->set_context_cpu(cpu);
47 event->set_context_sound(apu);
48 event->set_context_sound(drec);
50 memory->set_context_cpu(cpu);
51 memory->set_context_apu(apu);
52 memory->set_context_ppu(ppu);
53 memory->set_context_drec(drec);
54 memory->set_spr_ram_ptr(ppu->get_spr_ram());
55 apu->set_context_cpu(cpu);
56 apu->set_context_memory(memory);
57 ppu->set_context_cpu(cpu);
60 cpu->set_context_mem(memory);
61 cpu->set_context_intr(dummy);
63 // initialize all devices
64 for(DEVICE* device = first_device; device; device = device->next_device) {
72 for(DEVICE* device = first_device; device;) {
73 DEVICE *next_device = device->next_device;
80 DEVICE* VM::get_device(int id)
82 for(DEVICE* device = first_device; device; device = device->next_device) {
83 if(device->this_device_id == id) {
90 // ----------------------------------------------------------------------------
91 // drive virtual machine
92 // ----------------------------------------------------------------------------
97 if(boot_mode != config.boot_mode) {
99 memory->save_backup();
101 if(config.boot_mode == 0) {
102 memory->load_rom_image(_T("BASIC_V2.NES"));
103 ppu->load_rom_image(_T("BASIC_V2.NES"));
104 } else if(config.boot_mode == 1) {
105 memory->load_rom_image(_T("BASIC_V3.NES"));
106 ppu->load_rom_image(_T("BASIC_V3.NES"));
108 memory->load_rom_image(_T("PLAYBOX_BASIC.NES"));
109 ppu->load_rom_image(_T("PLAYBOX_BASIC.NES"));
111 boot_mode = config.boot_mode;
115 for(DEVICE* device = first_device; device; device = device->next_device) {
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 void VM::draw_screen()
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 void VM::initialize_sound(int rate, int samples)
140 // init sound manager
141 event->initialize_sound(rate, samples);
144 apu->initialize_sound(rate, samples);
147 uint16* VM::create_sound(int* extra_frames)
149 return event->create_sound(extra_frames);
152 int VM::sound_buffer_ptr()
154 return event->sound_buffer_ptr();
157 #ifdef USE_SOUND_VOLUME
158 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
161 apu->set_volume(0, decibel_l, decibel_r);
163 drec->set_volume(0, decibel_l, decibel_r);
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 void VM::play_tape(const _TCHAR* file_path)
174 drec->play_tape(file_path);
175 drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
178 void VM::rec_tape(const _TCHAR* file_path)
180 drec->rec_tape(file_path);
181 drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
184 void VM::close_tape()
187 drec->write_signal(SIG_DATAREC_REMOTE, 0, 1);
190 bool VM::tape_inserted()
192 return drec->tape_inserted();
195 bool VM::tape_playing()
197 return drec->tape_playing();
200 bool VM::tape_recording()
202 return drec->tape_recording();
205 int VM::tape_position()
207 return drec->tape_position();
212 return event->now_skip();
215 void VM::update_config()
217 if(boot_mode != config.boot_mode) {
218 // boot mode is changed !!!
219 // boot_mode = config.boot_mode;
222 for(DEVICE* device = first_device; device; device = device->next_device) {
223 device->update_config();
228 #define STATE_VERSION 1
230 void VM::save_state(FILEIO* state_fio)
232 state_fio->FputUint32(STATE_VERSION);
234 for(DEVICE* device = first_device; device; device = device->next_device) {
235 device->save_state(state_fio);
237 state_fio->FputInt32(boot_mode);
240 bool VM::load_state(FILEIO* state_fio)
242 if(state_fio->FgetUint32() != STATE_VERSION) {
245 for(DEVICE* device = first_device; device; device = device->next_device) {
246 if(!device->load_state(state_fio)) {
250 boot_mode = state_fio->FgetInt32();