OSDN Git Service

[VM][STATE][WIP] Apply csp_state_utils:: to some VMs.This is WIP.
[csp-qt/common_source_project-fm7.git] / source / src / vm / bmjr / bmjr.cpp
1 /*
2         HITACH BASIC Master Jr Emulator 'eBASICMasterJr'
3
4         Author : Takeda.Toshiya
5         Date   : 2015.08.28-
6
7         [ virtual machine ]
8 */
9
10 #include "bmjr.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../mc6800.h"
17 #include "../mc6820.h"
18 #include "../noise.h"
19
20 #ifdef USE_DEBUGGER
21 #include "../debugger.h"
22 #endif
23
24 #include "memory.h"
25
26 // ----------------------------------------------------------------------------
27 // initialize
28 // ----------------------------------------------------------------------------
29
30 VM::VM(EMU* parent_emu) : emu(parent_emu)
31 {
32         // create devices
33         first_device = last_device = NULL;
34         dummy = new DEVICE(this, emu);  // must be 1st device
35         event = new EVENT(this, emu);   // must be 2nd device
36         
37         drec = new DATAREC(this, emu);
38         drec->set_context_noise_play(new NOISE(this, emu));
39         drec->set_context_noise_stop(new NOISE(this, emu));
40         drec->set_context_noise_fast(new NOISE(this, emu));
41         cpu = new MC6800(this, emu);
42         pia = new MC6820(this, emu);
43         
44         memory = new MEMORY(this, emu);
45         
46         // Set names
47 #if defined(_USE_QT)
48         dummy->set_device_name(_T("1st Dummy"));
49 #endif
50         // set contexts
51         event->set_context_cpu(cpu);
52         event->set_context_sound(drec);
53         event->set_context_sound(memory);
54         event->set_context_sound(drec->get_context_noise_play());
55         event->set_context_sound(drec->get_context_noise_stop());
56         event->set_context_sound(drec->get_context_noise_fast());
57         
58         drec->set_context_ear(memory, SIG_MEMORY_DATAREC_EAR, 1);
59         
60         memory->set_context_drec(drec);
61         memory->set_context_cpu(cpu);
62         memory->set_context_pia(pia);
63         
64         // cpu bus
65         cpu->set_context_mem(memory);
66 #ifdef USE_DEBUGGER
67         cpu->set_context_debugger(new DEBUGGER(this, emu));
68 #endif
69         
70         // initialize all devices
71         for(DEVICE* device = first_device; device; device = device->next_device) {
72                 device->initialize();
73         }
74         decl_state();
75 }
76
77 VM::~VM()
78 {
79         // delete all devices
80         for(DEVICE* device = first_device; device;) {
81                 DEVICE *next_device = device->next_device;
82                 device->release();
83                 delete device;
84                 device = next_device;
85         }
86 }
87
88 DEVICE* VM::get_device(int id)
89 {
90         for(DEVICE* device = first_device; device; device = device->next_device) {
91                 if(device->this_device_id == id) {
92                         return device;
93                 }
94         }
95         return NULL;
96 }
97
98 // ----------------------------------------------------------------------------
99 // drive virtual machine
100 // ----------------------------------------------------------------------------
101
102 void VM::reset()
103 {
104         // reset all devices
105         for(DEVICE* device = first_device; device; device = device->next_device) {
106                 device->reset();
107         }
108 }
109
110 void VM::special_reset()
111 {
112         // reset all devices
113         for(DEVICE* device = first_device; device; device = device->next_device) {
114                 device->reset();
115         }
116 }
117
118 void VM::run()
119 {
120         event->drive();
121 }
122
123 double VM::get_frame_rate()
124 {
125         return event->get_frame_rate();
126 }
127
128 // ----------------------------------------------------------------------------
129 // debugger
130 // ----------------------------------------------------------------------------
131
132 #ifdef USE_DEBUGGER
133 DEVICE *VM::get_cpu(int index)
134 {
135         if(index == 0) {
136                 return cpu;
137         }
138         return NULL;
139 }
140 #endif
141
142 // ----------------------------------------------------------------------------
143 // draw screen
144 // ----------------------------------------------------------------------------
145
146 void VM::draw_screen()
147 {
148         memory->draw_screen();
149 }
150
151 // ----------------------------------------------------------------------------
152 // soud manager
153 // ----------------------------------------------------------------------------
154
155 void VM::initialize_sound(int rate, int samples)
156 {
157         // init sound manager
158         event->initialize_sound(rate, samples);
159 }
160
161 uint16_t* VM::create_sound(int* extra_frames)
162 {
163         return event->create_sound(extra_frames);
164 }
165
166 int VM::get_sound_buffer_ptr()
167 {
168         return event->get_sound_buffer_ptr();
169 }
170
171 #ifdef USE_SOUND_VOLUME
172 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
173 {
174         if(ch == 0) {
175                 memory->set_volume(0, decibel_l, decibel_r);
176         } else if(ch == 1) {
177                 drec->set_volume(0, decibel_l, decibel_r);
178         } else if(ch == 2) {
179                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
180                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
181                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
182         }
183 }
184 #endif
185
186 // ----------------------------------------------------------------------------
187 // notify key
188 // ----------------------------------------------------------------------------
189
190 void VM::key_down(int code, bool repeat)
191 {
192         if(!repeat) {
193                 memory->key_down(code);
194         }
195 }
196
197 void VM::key_up(int code)
198 {
199 }
200
201 // ----------------------------------------------------------------------------
202 // user interface
203 // ----------------------------------------------------------------------------
204
205 void VM::play_tape(int drv, const _TCHAR* file_path)
206 {
207         drec->play_tape(file_path);
208 //      drec->set_remote(true);
209 }
210
211 void VM::rec_tape(int drv, const _TCHAR* file_path)
212 {
213         drec->rec_tape(file_path);
214 //      drec->set_remote(true);
215 }
216
217 void VM::close_tape(int drv)
218 {
219         emu->lock_vm();
220         drec->close_tape();
221         emu->unlock_vm();
222 //      drec->set_remote(false);
223 }
224
225 bool VM::is_tape_inserted(int drv)
226 {
227         return drec->is_tape_inserted();
228 }
229
230 bool VM::is_tape_playing(int drv)
231 {
232         return drec->is_tape_playing();
233 }
234
235 bool VM::is_tape_recording(int drv)
236 {
237         return drec->is_tape_recording();
238 }
239
240 int VM::get_tape_position(int drv)
241 {
242         return drec->get_tape_position();
243 }
244
245 const _TCHAR* VM::get_tape_message(int drv)
246 {
247         return drec->get_message();
248 }
249
250 void VM::push_play(int drv)
251 {
252         drec->set_ff_rew(0);
253         drec->set_remote(true);
254 }
255
256 void VM::push_stop(int drv)
257 {
258         drec->set_remote(false);
259 }
260
261 void VM::push_fast_forward(int drv)
262 {
263         drec->set_ff_rew(1);
264         drec->set_remote(true);
265 }
266
267 void VM::push_fast_rewind(int drv)
268 {
269         drec->set_ff_rew(-1);
270         drec->set_remote(true);
271 }
272
273 bool VM::is_frame_skippable()
274 {
275         return event->is_frame_skippable();
276 }
277
278 void VM::update_config()
279 {
280         for(DEVICE* device = first_device; device; device = device->next_device) {
281                 device->update_config();
282         }
283 }
284
285 #define STATE_VERSION   3
286
287 #include "../../statesub.h"
288
289 void VM::decl_state(void)
290 {
291         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::BASIC_MASTER_HEAD")));
292         for(DEVICE* device = first_device; device; device = device->next_device) {
293                 device->decl_state();
294         }
295 }
296
297 void VM::save_state(FILEIO* state_fio)
298 {
299         //state_fio->FputUint32(STATE_VERSION);
300         
301         if(state_entry != NULL) {
302                 state_entry->save_state(state_fio);
303         }
304         for(DEVICE* device = first_device; device; device = device->next_device) {
305                 device->save_state(state_fio);
306         }
307 }
308
309 bool VM::load_state(FILEIO* state_fio)
310 {
311         //if(state_fio->FgetUint32() != STATE_VERSION) {
312         //      return false;
313         //}
314         bool mb = false;
315         if(state_entry != NULL) {
316                 mb = state_entry->load_state(state_fio);
317         }
318         if(!mb) {
319                 emu->out_debug_log("INFO: HEADER DATA ERROR");
320                 return false;
321         }
322         for(DEVICE* device = first_device; device; device = device->next_device) {
323                 if(!device->load_state(state_fio)) {
324                         return false;
325                 }
326         }
327         return true;
328 }
329