OSDN Git Service

[General] Merge upstream 2018-05-24. Still not test to build, will test.
[csp-qt/common_source_project-fm7.git] / source / src / vm / familybasic / familybasic.cpp
1 /*
2         Nintendo Family BASIC Emulator 'eFamilyBASIC'
3
4         Origin : nester
5         Author : Takeda.Toshiya
6         Date   : 2010.08.11-
7
8         [ virtual machine ]
9 */
10
11 #include "familybasic.h"
12 #include "../../emu.h"
13 #include "../device.h"
14 #include "../event.h"
15
16 #include "../datarec.h"
17 #include "../m6502.h"
18 #include "../noise.h"
19 #include "../ym2413.h"
20
21 #ifdef USE_DEBUGGER
22 #include "../debugger.h"
23 #endif
24
25 #include "memory.h"
26 #include "apu.h"
27 #include "ppu.h"
28
29 // ----------------------------------------------------------------------------
30 // initialize
31 // ----------------------------------------------------------------------------
32
33 VM::VM(EMU* parent_emu) : emu(parent_emu)
34 {
35         // check configs
36 //      boot_mode = config.boot_mode;
37         boot_mode = -1;
38         
39         // create devices
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
43         
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);
50         
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
55         
56         dummy->set_device_name(_T("1st Dummy"));
57         
58         // set contexts
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());
66         
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);
77         
78         // cpu bus
79         cpu->set_context_mem(memory);
80         cpu->set_context_intr(dummy);
81 #ifdef USE_DEBUGGER
82         cpu->set_context_debugger(new DEBUGGER(this, emu));
83 #endif
84         
85         // initialize all devices
86         for(DEVICE* device = first_device; device; device = device->next_device) {
87                 device->initialize();
88         }
89 }
90
91 VM::~VM()
92 {
93         // delete all devices
94         for(DEVICE* device = first_device; device;) {
95                 DEVICE *next_device = device->next_device;
96                 device->release();
97                 delete device;
98                 device = next_device;
99         }
100 }
101
102 DEVICE* VM::get_device(int id)
103 {
104         for(DEVICE* device = first_device; device; device = device->next_device) {
105                 if(device->this_device_id == id) {
106                         return device;
107                 }
108         }
109         return NULL;
110 }
111
112 // ----------------------------------------------------------------------------
113 // drive virtual machine
114 // ----------------------------------------------------------------------------
115
116 void VM::reset()
117 {
118         // load basic rom
119         if(boot_mode != config.boot_mode) {
120                 if(boot_mode != -1) {
121                         memory->save_backup();
122                 }
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"));
141                 }
142                 boot_mode = config.boot_mode;
143         }
144         
145         // reset all devices
146         for(DEVICE* device = first_device; device; device = device->next_device) {
147                 device->reset();
148         }
149 }
150
151 void VM::run()
152 {
153         event->drive();
154 }
155
156 // ----------------------------------------------------------------------------
157 // debugger
158 // ----------------------------------------------------------------------------
159
160 #ifdef USE_DEBUGGER
161 DEVICE *VM::get_cpu(int index)
162 {
163         if(index == 0) {
164                 return cpu;
165         }
166         return NULL;
167 }
168 #endif
169
170 // ----------------------------------------------------------------------------
171 // draw screen
172 // ----------------------------------------------------------------------------
173
174 void VM::draw_screen()
175 {
176         ppu->draw_screen();
177 }
178
179 // ----------------------------------------------------------------------------
180 // soud manager
181 // ----------------------------------------------------------------------------
182
183 void VM::initialize_sound(int rate, int samples)
184 {
185         // init sound manager
186         event->initialize_sound(rate, samples);
187         
188         // init sound gen
189         apu->initialize_sound(rate, samples);
190         opll->initialize_sound(rate, 3579545, samples);
191 }
192
193 uint16_t* VM::create_sound(int* extra_frames)
194 {
195         return event->create_sound(extra_frames);
196 }
197
198 int VM::get_sound_buffer_ptr()
199 {
200         return event->get_sound_buffer_ptr();
201 }
202
203 #ifdef USE_SOUND_VOLUME
204 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
205 {
206         if(ch == 0) {
207                 apu->set_volume(0, decibel_l, decibel_r);
208         } else if(ch == 1) {
209                 opll->set_volume(0, decibel_l, decibel_r);
210         } else if(ch == 2) {
211                 drec->set_volume(0, decibel_l, decibel_r);
212         } else if(ch == 3) {
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);
216         }
217 }
218 #endif
219
220 // ----------------------------------------------------------------------------
221 // user interface
222 // ----------------------------------------------------------------------------
223
224 void VM::play_tape(int drv, const _TCHAR* file_path)
225 {
226         drec->play_tape(file_path);
227 //      drec->set_remote(true);
228 }
229
230 void VM::rec_tape(int drv, const _TCHAR* file_path)
231 {
232         drec->rec_tape(file_path);
233 //      drec->set_remote(true);
234 }
235
236 void VM::close_tape(int drv)
237 {
238         emu->lock_vm();
239         drec->close_tape();
240         emu->unlock_vm();
241 //      drec->set_remote(false);
242 }
243
244 bool VM::is_tape_inserted(int drv)
245 {
246         return drec->is_tape_inserted();
247 }
248
249 bool VM::is_tape_playing(int drv)
250 {
251         return drec->is_tape_playing();
252 }
253
254 bool VM::is_tape_recording(int drv)
255 {
256         return drec->is_tape_recording();
257 }
258
259 int VM::get_tape_position(int drv)
260 {
261         return drec->get_tape_position();
262 }
263
264 const _TCHAR* VM::get_tape_message(int drv)
265 {
266         return drec->get_message();
267 }
268
269 void VM::push_play(int drv)
270 {
271         drec->set_ff_rew(0);
272         drec->set_remote(true);
273 }
274
275 void VM::push_stop(int drv)
276 {
277         drec->set_remote(false);
278 }
279
280 void VM::push_fast_forward(int drv)
281 {
282         drec->set_ff_rew(1);
283         drec->set_remote(true);
284 }
285
286 void VM::push_fast_rewind(int drv)
287 {
288         drec->set_ff_rew(-1);
289         drec->set_remote(true);
290 }
291
292 bool VM::is_frame_skippable()
293 {
294         return event->is_frame_skippable();
295 }
296
297 void VM::update_config()
298 {
299         if(boot_mode != config.boot_mode) {
300                 // boot mode is changed !!!
301 //              boot_mode = config.boot_mode;
302                 reset();
303         } else {
304                 for(DEVICE* device = first_device; device; device = device->next_device) {
305                         device->update_config();
306                 }
307         }
308 }
309
310 #define STATE_VERSION   5
311
312 void VM::save_state(FILEIO* state_fio)
313 {
314         state_fio->FputUint32(STATE_VERSION);
315         
316         for(DEVICE* device = first_device; device; device = device->next_device) {
317                 device->save_state(state_fio);
318         }
319         state_fio->FputInt32(boot_mode);
320 }
321
322 bool VM::load_state(FILEIO* state_fio)
323 {
324         if(state_fio->FgetUint32() != STATE_VERSION) {
325                 return false;
326         }
327         for(DEVICE* device = first_device; device; device = device->next_device) {
328                 if(!device->load_state(state_fio)) {
329                         return false;
330                 }
331         }
332         boot_mode = state_fio->FgetInt32();
333         return true;
334 }
335