OSDN Git Service

[VM][General][WIP] Apply new (Upstream 2016-02-21) APIs to VMs.
[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
19 #ifdef USE_DEBUGGER
20 #include "../debugger.h"
21 #endif
22
23 #include "memory.h"
24
25 // ----------------------------------------------------------------------------
26 // initialize
27 // ----------------------------------------------------------------------------
28
29 VM::VM(EMU* parent_emu) : emu(parent_emu)
30 {
31         // create devices
32         first_device = last_device = NULL;
33         dummy = new DEVICE(this, emu);  // must be 1st device
34         event = new EVENT(this, emu);   // must be 2nd device
35         
36         drec = new DATAREC(this, emu);
37         cpu = new MC6800(this, emu);
38         pia = new MC6820(this, emu);
39         
40         memory = new MEMORY(this, emu);
41         
42         // set contexts
43         event->set_context_cpu(cpu);
44         event->set_context_sound(drec);
45         event->set_context_sound(memory);
46         
47         drec->set_context_ear(memory, SIG_MEMORY_DATAREC_EAR, 1);
48         
49         memory->set_context_drec(drec);
50         memory->set_context_cpu(cpu);
51         memory->set_context_pia(pia);
52         
53         // cpu bus
54         cpu->set_context_mem(memory);
55 #ifdef USE_DEBUGGER
56         cpu->set_context_debugger(new DEBUGGER(this, emu));
57 #endif
58         
59         // initialize all devices
60         for(DEVICE* device = first_device; device; device = device->next_device) {
61                 device->initialize();
62         }
63 }
64
65 VM::~VM()
66 {
67         // delete all devices
68         for(DEVICE* device = first_device; device;) {
69                 DEVICE *next_device = device->next_device;
70                 device->release();
71                 delete device;
72                 device = next_device;
73         }
74 }
75
76 DEVICE* VM::get_device(int id)
77 {
78         for(DEVICE* device = first_device; device; device = device->next_device) {
79                 if(device->this_device_id == id) {
80                         return device;
81                 }
82         }
83         return NULL;
84 }
85
86 // ----------------------------------------------------------------------------
87 // drive virtual machine
88 // ----------------------------------------------------------------------------
89
90 void VM::reset()
91 {
92         // reset all devices
93         for(DEVICE* device = first_device; device; device = device->next_device) {
94                 device->reset();
95         }
96 }
97
98 void VM::special_reset()
99 {
100         // reset all devices
101         for(DEVICE* device = first_device; device; device = device->next_device) {
102                 device->reset();
103         }
104 }
105
106 void VM::run()
107 {
108         event->drive();
109 }
110
111 double VM::get_frame_rate()
112 {
113         return event->get_frame_rate();
114 }
115
116 // ----------------------------------------------------------------------------
117 // debugger
118 // ----------------------------------------------------------------------------
119
120 #ifdef USE_DEBUGGER
121 DEVICE *VM::get_cpu(int index)
122 {
123         if(index == 0) {
124                 return cpu;
125         }
126         return NULL;
127 }
128 #endif
129
130 // ----------------------------------------------------------------------------
131 // draw screen
132 // ----------------------------------------------------------------------------
133
134 void VM::draw_screen()
135 {
136         memory->draw_screen();
137 }
138
139 // ----------------------------------------------------------------------------
140 // soud manager
141 // ----------------------------------------------------------------------------
142
143 void VM::initialize_sound(int rate, int samples)
144 {
145         // init sound manager
146         event->initialize_sound(rate, samples);
147 }
148
149 uint16* VM::create_sound(int* extra_frames)
150 {
151         return event->create_sound(extra_frames);
152 }
153
154 int VM::get_sound_buffer_ptr()
155 {
156         return event->get_sound_buffer_ptr();
157 }
158
159 #ifdef USE_SOUND_VOLUME
160 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
161 {
162         if(ch == 0) {
163                 memory->set_volume(0, decibel_l, decibel_r);
164         } else if(ch == 1) {
165                 drec->set_volume(0, decibel_l, decibel_r);
166         }
167 }
168 #endif
169
170 // ----------------------------------------------------------------------------
171 // notify key
172 // ----------------------------------------------------------------------------
173
174 void VM::key_down(int code, bool repeat)
175 {
176         if(!repeat) {
177                 memory->key_down(code);
178         }
179 }
180
181 void VM::key_up(int code)
182 {
183 }
184
185 // ----------------------------------------------------------------------------
186 // user interface
187 // ----------------------------------------------------------------------------
188
189 void VM::play_tape(const _TCHAR* file_path)
190 {
191         drec->play_tape(file_path);
192         push_play();
193 }
194
195 void VM::rec_tape(const _TCHAR* file_path)
196 {
197         drec->rec_tape(file_path);
198         push_play();
199 }
200
201 void VM::close_tape()
202 {
203         push_stop();
204         drec->close_tape();
205 }
206
207 bool VM::is_tape_inserted()
208 {
209         return drec->is_tape_inserted();
210 }
211
212 bool VM::is_tape_playing()
213 {
214         return drec->is_tape_playing();
215 }
216
217 bool VM::is_tape_recording()
218 {
219         return drec->is_tape_recording();
220 }
221
222 int VM::get_tape_position()
223 {
224         return drec->get_tape_position();
225 }
226
227 void VM::push_play()
228 {
229         drec->set_ff_rew(0);
230         drec->set_remote(true);
231 }
232
233 void VM::push_stop()
234 {
235         drec->set_remote(false);
236 }
237
238 void VM::push_fast_forward()
239 {
240         drec->set_ff_rew(1);
241         drec->set_remote(true);
242 }
243
244 void VM::push_fast_rewind()
245 {
246         drec->set_ff_rew(-1);
247         drec->set_remote(true);
248 }
249
250 bool VM::is_frame_skippable()
251 {
252         return event->is_frame_skippable();
253 }
254
255 void VM::update_config()
256 {
257         for(DEVICE* device = first_device; device; device = device->next_device) {
258                 device->update_config();
259         }
260 }
261
262 #define STATE_VERSION   1
263
264 void VM::save_state(FILEIO* state_fio)
265 {
266         state_fio->FputUint32(STATE_VERSION);
267         
268         for(DEVICE* device = first_device; device; device = device->next_device) {
269                 device->save_state(state_fio);
270         }
271 }
272
273 bool VM::load_state(FILEIO* state_fio)
274 {
275         if(state_fio->FgetUint32() != STATE_VERSION) {
276                 return false;
277         }
278         for(DEVICE* device = first_device; device; device = device->next_device) {
279                 if(!device->load_state(state_fio)) {
280                         return false;
281                 }
282         }
283         return true;
284 }
285