OSDN Git Service

[VM][WIP] Use namespace to devices per VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / hc40 / hc40.cpp
1 /*
2         EPSON HC-40 Emulator 'eHC-40'
3
4         Author : Takeda.Toshiya
5         Date   : 2008.02.23 -
6
7         [ virtual machine ]
8 */
9
10 #include "hc40.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../beep.h"
16 #include "../datarec.h"
17 #include "../noise.h"
18 #include "../ptf20.h"
19 #include "../z80.h"
20
21 #ifdef USE_DEBUGGER
22 #include "../debugger.h"
23 #endif
24
25 #include "io.h"
26 #include "memory.h"
27
28 using HC40::IO;
29 using HC40::MEMORY;
30
31 // ----------------------------------------------------------------------------
32 // initialize
33 // ----------------------------------------------------------------------------
34
35 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
36 {
37         // create devices
38         first_device = last_device = NULL;
39         dummy = new DEVICE(this, emu);  // must be 1st device
40         event = new EVENT(this, emu);   // must be 2nd device
41         dummy->set_device_name(_T("1st Dummy"));
42         
43         beep = new BEEP(this, emu);
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         tf20 = new PTF20(this, emu);
49         cpu = new Z80(this, emu);
50         
51         io = new IO(this, emu);
52         memory = new MEMORY(this, emu);
53         // set contexts
54         event->set_context_cpu(cpu);
55         event->set_context_sound(beep);
56         event->set_context_sound(drec);
57         event->set_context_sound(drec->get_context_noise_play());
58         event->set_context_sound(drec->get_context_noise_stop());
59         event->set_context_sound(drec->get_context_noise_fast());
60
61         drec->set_context_ear(io, SIG_IO_DREC, 1);
62         tf20->set_context_sio(io, SIG_IO_ART);
63         
64         io->set_context_cpu(cpu);
65         io->set_context_mem(memory, memory->get_ram());
66         io->set_context_tf20(tf20);
67         io->set_context_beep(beep);
68         io->set_context_drec(drec);
69         
70         // cpu bus
71         cpu->set_context_mem(memory);
72         cpu->set_context_io(io);
73         cpu->set_context_intr(io);
74 #ifdef USE_DEBUGGER
75         cpu->set_context_debugger(new DEBUGGER(this, emu));
76 #endif
77         
78         // initialize all devices
79 #if defined(__GIT_REPO_VERSION)
80         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
81 #endif
82         for(DEVICE* device = first_device; device; device = device->next_device) {
83                 device->initialize();
84         }
85 }
86
87 VM::~VM()
88 {
89         // delete all devices
90         for(DEVICE* device = first_device; device;) {
91                 DEVICE *next_device = device->next_device;
92                 device->release();
93                 delete device;
94                 device = next_device;
95         }
96 }
97
98 DEVICE* VM::get_device(int id)
99 {
100         for(DEVICE* device = first_device; device; device = device->next_device) {
101                 if(device->this_device_id == id) {
102                         return device;
103                 }
104         }
105         return NULL;
106 }
107
108 // ----------------------------------------------------------------------------
109 // drive virtual machine
110 // ----------------------------------------------------------------------------
111
112 void VM::reset()
113 {
114         // reset all devices
115         for(DEVICE* device = first_device; device; device = device->next_device) {
116                 device->reset();
117         }
118 }
119
120 void VM::special_reset()
121 {
122         // system reset
123         for(DEVICE* device = first_device; device; device = device->next_device) {
124                 device->reset();
125         }
126         io->sysreset();
127 }
128
129 void VM::run()
130 {
131         event->drive();
132 }
133
134 // ----------------------------------------------------------------------------
135 // debugger
136 // ----------------------------------------------------------------------------
137
138 #ifdef USE_DEBUGGER
139 DEVICE *VM::get_cpu(int index)
140 {
141         if(index == 0) {
142                 return cpu;
143         }
144         return NULL;
145 }
146 #endif
147
148 // ----------------------------------------------------------------------------
149 // draw screen
150 // ----------------------------------------------------------------------------
151
152 void VM::draw_screen()
153 {
154         io->draw_screen();
155 }
156
157 // ----------------------------------------------------------------------------
158 // soud manager
159 // ----------------------------------------------------------------------------
160
161 void VM::initialize_sound(int rate, int samples)
162 {
163         // init sound manager
164         event->initialize_sound(rate, samples);
165         
166         // init sound gen
167         beep->initialize_sound(rate, 1000, 8000);
168 }
169
170 uint16_t* VM::create_sound(int* extra_frames)
171 {
172         return event->create_sound(extra_frames);
173 }
174
175 int VM::get_sound_buffer_ptr()
176 {
177         return event->get_sound_buffer_ptr();
178 }
179
180 #ifdef USE_SOUND_VOLUME
181 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
182 {
183         if(ch == 0) {
184                 beep->set_volume(0, decibel_l, decibel_r);
185         } else if(ch == 1) {
186                 drec->set_volume(0, decibel_l, decibel_r);
187         } else if(ch == 2) {
188                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
189                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
190                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
191         }
192 }
193 #endif
194
195 // ----------------------------------------------------------------------------
196 // notify key
197 // ----------------------------------------------------------------------------
198
199 void VM::key_down(int code, bool repeat)
200 {
201         io->key_down(code);
202 }
203
204 void VM::key_up(int code)
205 {
206         io->key_up(code);
207 }
208
209 // ----------------------------------------------------------------------------
210 // user interface
211 // ----------------------------------------------------------------------------
212
213 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
214 {
215         tf20->open_disk(drv, file_path, bank);
216 }
217
218 void VM::close_floppy_disk(int drv)
219 {
220         tf20->close_disk(drv);
221 }
222
223 bool VM::is_floppy_disk_inserted(int drv)
224 {
225         return tf20->is_disk_inserted(drv);
226 }
227
228 void VM::is_floppy_disk_protected(int drv, bool value)
229 {
230         tf20->is_disk_protected(drv, value);
231 }
232
233 bool VM::is_floppy_disk_protected(int drv)
234 {
235         return tf20->is_disk_protected(drv);
236 }
237
238 uint32_t VM::is_floppy_disk_accessed()
239 {
240         return tf20->read_signal(0);
241 }
242
243 void VM::play_tape(int drv, const _TCHAR* file_path)
244 {
245         drec->play_tape(file_path);
246 //      drec->set_remote(true);
247 }
248
249 void VM::rec_tape(int drv, const _TCHAR* file_path)
250 {
251         drec->rec_tape(file_path);
252 //      drec->set_remote(true);
253 }
254
255 void VM::close_tape(int drv)
256 {
257         emu->lock_vm();
258         drec->close_tape();
259         emu->unlock_vm();
260 //      drec->set_remote(false);
261 }
262
263 bool VM::is_tape_inserted(int drv)
264 {
265         return drec->is_tape_inserted();
266 }
267
268 bool VM::is_tape_playing(int drv)
269 {
270         return drec->is_tape_playing();
271 }
272
273 bool VM::is_tape_recording(int drv)
274 {
275         return drec->is_tape_recording();
276 }
277
278 int VM::get_tape_position(int drv)
279 {
280         return drec->get_tape_position();
281 }
282
283 const _TCHAR* VM::get_tape_message(int drv)
284 {
285         return drec->get_message();
286 }
287
288 void VM::push_play(int drv)
289 {
290         drec->set_ff_rew(0);
291         drec->set_remote(true);
292 }
293
294 void VM::push_stop(int drv)
295 {
296         drec->set_remote(false);
297 }
298
299 void VM::push_fast_forward(int drv)
300 {
301         drec->set_ff_rew(1);
302         drec->set_remote(true);
303 }
304
305 void VM::push_fast_rewind(int drv)
306 {
307         drec->set_ff_rew(-1);
308         drec->set_remote(true);
309 }
310
311 bool VM::is_frame_skippable()
312 {
313         return event->is_frame_skippable();
314 }
315
316 void VM::update_config()
317 {
318         for(DEVICE* device = first_device; device; device = device->next_device) {
319                 device->update_config();
320         }
321 }
322
323 #define STATE_VERSION   3
324
325 bool VM::process_state(FILEIO* state_fio, bool loading)
326 {
327         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
328                 return false;
329         }
330         for(DEVICE* device = first_device; device; device = device->next_device) {
331                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
332                 // const char *name = typeid(*device).name();
333                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
334                 const char *name = device->get_device_name();
335                 int len = strlen(name);
336                 
337                 if(!state_fio->StateCheckInt32(len)) {
338                         if(loading) {
339                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
340                         }
341                         return false;
342                 }
343                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
344                         if(loading) {
345                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
346                         }
347                         return false;
348                 }
349                 if(!device->process_state(state_fio, loading)) {
350                         if(loading) {
351                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
352                         }
353                         return false;
354                 }
355         }
356         return true;
357 }