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 / 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 }
75
76 VM::~VM()
77 {
78         // delete all devices
79         for(DEVICE* device = first_device; device;) {
80                 DEVICE *next_device = device->next_device;
81                 device->release();
82                 delete device;
83                 device = next_device;
84         }
85 }
86
87 DEVICE* VM::get_device(int id)
88 {
89         for(DEVICE* device = first_device; device; device = device->next_device) {
90                 if(device->this_device_id == id) {
91                         return device;
92                 }
93         }
94         return NULL;
95 }
96
97 // ----------------------------------------------------------------------------
98 // drive virtual machine
99 // ----------------------------------------------------------------------------
100
101 void VM::reset()
102 {
103         // reset all devices
104         for(DEVICE* device = first_device; device; device = device->next_device) {
105                 device->reset();
106         }
107 }
108
109 void VM::special_reset()
110 {
111         // reset all devices
112         for(DEVICE* device = first_device; device; device = device->next_device) {
113                 device->reset();
114         }
115 }
116
117 void VM::run()
118 {
119         event->drive();
120 }
121
122 double VM::get_frame_rate()
123 {
124         return event->get_frame_rate();
125 }
126
127 // ----------------------------------------------------------------------------
128 // debugger
129 // ----------------------------------------------------------------------------
130
131 #ifdef USE_DEBUGGER
132 DEVICE *VM::get_cpu(int index)
133 {
134         if(index == 0) {
135                 return cpu;
136         }
137         return NULL;
138 }
139 #endif
140
141 // ----------------------------------------------------------------------------
142 // draw screen
143 // ----------------------------------------------------------------------------
144
145 void VM::draw_screen()
146 {
147         memory->draw_screen();
148 }
149
150 // ----------------------------------------------------------------------------
151 // soud manager
152 // ----------------------------------------------------------------------------
153
154 void VM::initialize_sound(int rate, int samples)
155 {
156         // init sound manager
157         event->initialize_sound(rate, samples);
158 }
159
160 uint16_t* VM::create_sound(int* extra_frames)
161 {
162         return event->create_sound(extra_frames);
163 }
164
165 int VM::get_sound_buffer_ptr()
166 {
167         return event->get_sound_buffer_ptr();
168 }
169
170 #ifdef USE_SOUND_VOLUME
171 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
172 {
173         if(ch == 0) {
174                 memory->set_volume(0, decibel_l, decibel_r);
175         } else if(ch == 1) {
176                 drec->set_volume(0, decibel_l, decibel_r);
177         } else if(ch == 2) {
178                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
179                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
180                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
181         }
182 }
183 #endif
184
185 // ----------------------------------------------------------------------------
186 // notify key
187 // ----------------------------------------------------------------------------
188
189 void VM::key_down(int code, bool repeat)
190 {
191         if(!repeat) {
192                 memory->key_down(code);
193         }
194 }
195
196 void VM::key_up(int code)
197 {
198 }
199
200 // ----------------------------------------------------------------------------
201 // user interface
202 // ----------------------------------------------------------------------------
203
204 void VM::play_tape(int drv, const _TCHAR* file_path)
205 {
206         drec->play_tape(file_path);
207 //      drec->set_remote(true);
208 }
209
210 void VM::rec_tape(int drv, const _TCHAR* file_path)
211 {
212         drec->rec_tape(file_path);
213 //      drec->set_remote(true);
214 }
215
216 void VM::close_tape(int drv)
217 {
218         emu->lock_vm();
219         drec->close_tape();
220         emu->unlock_vm();
221 //      drec->set_remote(false);
222 }
223
224 bool VM::is_tape_inserted(int drv)
225 {
226         return drec->is_tape_inserted();
227 }
228
229 bool VM::is_tape_playing(int drv)
230 {
231         return drec->is_tape_playing();
232 }
233
234 bool VM::is_tape_recording(int drv)
235 {
236         return drec->is_tape_recording();
237 }
238
239 int VM::get_tape_position(int drv)
240 {
241         return drec->get_tape_position();
242 }
243
244 const _TCHAR* VM::get_tape_message(int drv)
245 {
246         return drec->get_message();
247 }
248
249 void VM::push_play(int drv)
250 {
251         drec->set_ff_rew(0);
252         drec->set_remote(true);
253 }
254
255 void VM::push_stop(int drv)
256 {
257         drec->set_remote(false);
258 }
259
260 void VM::push_fast_forward(int drv)
261 {
262         drec->set_ff_rew(1);
263         drec->set_remote(true);
264 }
265
266 void VM::push_fast_rewind(int drv)
267 {
268         drec->set_ff_rew(-1);
269         drec->set_remote(true);
270 }
271
272 bool VM::is_frame_skippable()
273 {
274         return event->is_frame_skippable();
275 }
276
277 void VM::update_config()
278 {
279         for(DEVICE* device = first_device; device; device = device->next_device) {
280                 device->update_config();
281         }
282 }
283
284 #define STATE_VERSION   3
285
286 void VM::save_state(FILEIO* state_fio)
287 {
288         state_fio->FputUint32(STATE_VERSION);
289         
290         for(DEVICE* device = first_device; device; device = device->next_device) {
291                 device->save_state(state_fio);
292         }
293 }
294
295 bool VM::load_state(FILEIO* state_fio)
296 {
297         if(state_fio->FgetUint32() != STATE_VERSION) {
298                 return false;
299         }
300         for(DEVICE* device = first_device; device; device = device->next_device) {
301                 if(!device->load_state(state_fio)) {
302                         return false;
303                 }
304         }
305         return true;
306 }
307