OSDN Git Service

[Qt][CMake][General] Add Hitachi Basic Master Jr.
[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::frame_rate()
112 {
113         return event->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::sound_buffer_ptr()
155 {
156         return event->sound_buffer_ptr();
157 }
158
159 // ----------------------------------------------------------------------------
160 // notify key
161 // ----------------------------------------------------------------------------
162
163 void VM::key_down(int code, bool repeat)
164 {
165         if(!repeat) {
166                 memory->key_down(code);
167         }
168 }
169
170 void VM::key_up(int code)
171 {
172 }
173
174 // ----------------------------------------------------------------------------
175 // user interface
176 // ----------------------------------------------------------------------------
177
178 void VM::play_tape(const _TCHAR* file_path)
179 {
180         drec->play_tape(file_path);
181         push_play();
182 }
183
184 void VM::rec_tape(const _TCHAR* file_path)
185 {
186         drec->rec_tape(file_path);
187         push_play();
188 }
189
190 void VM::close_tape()
191 {
192         push_stop();
193         drec->close_tape();
194 }
195
196 bool VM::tape_inserted()
197 {
198         return drec->tape_inserted();
199 }
200
201 void VM::push_play()
202 {
203         drec->set_ff_rew(0);
204         drec->set_remote(true);
205 }
206
207 void VM::push_stop()
208 {
209         drec->set_remote(false);
210 }
211
212 void VM::push_fast_forward()
213 {
214         drec->set_ff_rew(1);
215         drec->set_remote(true);
216 }
217
218 void VM::push_fast_rewind()
219 {
220         drec->set_ff_rew(-1);
221         drec->set_remote(true);
222 }
223
224 bool VM::get_tape_play(void)
225 {
226         return drec->get_tape_play();
227 }
228
229 bool VM::now_skip()
230 {
231         return event->now_skip();
232 }
233
234 void VM::update_config()
235 {
236         for(DEVICE* device = first_device; device; device = device->next_device) {
237                 device->update_config();
238         }
239 }
240
241 #define STATE_VERSION   1
242
243 void VM::save_state(FILEIO* state_fio)
244 {
245         state_fio->FputUint32(STATE_VERSION);
246         
247         for(DEVICE* device = first_device; device; device = device->next_device) {
248                 device->save_state(state_fio);
249         }
250 }
251
252 bool VM::load_state(FILEIO* state_fio)
253 {
254         if(state_fio->FgetUint32() != STATE_VERSION) {
255                 return false;
256         }
257         for(DEVICE* device = first_device; device; device = device->next_device) {
258                 if(!device->load_state(state_fio)) {
259                         return false;
260                 }
261         }
262         return true;
263 }
264