OSDN Git Service

51333f9069c8bed7f56325be4f3134ff96f699a6
[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 // ----------------------------------------------------------------------------
29 // initialize
30 // ----------------------------------------------------------------------------
31
32 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
33 {
34         // create devices
35         first_device = last_device = NULL;
36         dummy = new DEVICE(this, emu);  // must be 1st device
37         event = new EVENT(this, emu);   // must be 2nd device
38         dummy->set_device_name(_T("1st Dummy"));
39         
40         beep = new BEEP(this, emu);
41         drec = new DATAREC(this, emu);
42         drec->set_context_noise_play(new NOISE(this, emu));
43         drec->set_context_noise_stop(new NOISE(this, emu));
44         drec->set_context_noise_fast(new NOISE(this, emu));
45         tf20 = new PTF20(this, emu);
46         cpu = new Z80(this, emu);
47         
48         io = new IO(this, emu);
49         memory = new MEMORY(this, emu);
50         // set contexts
51         event->set_context_cpu(cpu);
52         event->set_context_sound(beep);
53         event->set_context_sound(drec);
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(io, SIG_IO_DREC, 1);
59         tf20->set_context_sio(io, SIG_IO_ART);
60         
61         io->set_context_cpu(cpu);
62         io->set_context_mem(memory, memory->get_ram());
63         io->set_context_tf20(tf20);
64         io->set_context_beep(beep);
65         io->set_context_drec(drec);
66         
67         // cpu bus
68         cpu->set_context_mem(memory);
69         cpu->set_context_io(io);
70         cpu->set_context_intr(io);
71 #ifdef USE_DEBUGGER
72         cpu->set_context_debugger(new DEBUGGER(this, emu));
73 #endif
74         
75         // initialize all devices
76         for(DEVICE* device = first_device; device; device = device->next_device) {
77                 device->initialize();
78         }
79         decl_state();
80 }
81
82 VM::~VM()
83 {
84         // delete all devices
85         for(DEVICE* device = first_device; device;) {
86                 DEVICE *next_device = device->next_device;
87                 device->release();
88                 delete device;
89                 device = next_device;
90         }
91 }
92
93 DEVICE* VM::get_device(int id)
94 {
95         for(DEVICE* device = first_device; device; device = device->next_device) {
96                 if(device->this_device_id == id) {
97                         return device;
98                 }
99         }
100         return NULL;
101 }
102
103 // ----------------------------------------------------------------------------
104 // drive virtual machine
105 // ----------------------------------------------------------------------------
106
107 void VM::reset()
108 {
109         // reset all devices
110         for(DEVICE* device = first_device; device; device = device->next_device) {
111                 device->reset();
112         }
113 }
114
115 void VM::special_reset()
116 {
117         // system reset
118         for(DEVICE* device = first_device; device; device = device->next_device) {
119                 device->reset();
120         }
121         io->sysreset();
122 }
123
124 void VM::run()
125 {
126         event->drive();
127 }
128
129 // ----------------------------------------------------------------------------
130 // debugger
131 // ----------------------------------------------------------------------------
132
133 #ifdef USE_DEBUGGER
134 DEVICE *VM::get_cpu(int index)
135 {
136         if(index == 0) {
137                 return cpu;
138         }
139         return NULL;
140 }
141 #endif
142
143 // ----------------------------------------------------------------------------
144 // draw screen
145 // ----------------------------------------------------------------------------
146
147 void VM::draw_screen()
148 {
149         io->draw_screen();
150 }
151
152 // ----------------------------------------------------------------------------
153 // soud manager
154 // ----------------------------------------------------------------------------
155
156 void VM::initialize_sound(int rate, int samples)
157 {
158         // init sound manager
159         event->initialize_sound(rate, samples);
160         
161         // init sound gen
162         beep->initialize_sound(rate, 1000, 8000);
163 }
164
165 uint16_t* VM::create_sound(int* extra_frames)
166 {
167         return event->create_sound(extra_frames);
168 }
169
170 int VM::get_sound_buffer_ptr()
171 {
172         return event->get_sound_buffer_ptr();
173 }
174
175 #ifdef USE_SOUND_VOLUME
176 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
177 {
178         if(ch == 0) {
179                 beep->set_volume(0, decibel_l, decibel_r);
180         } else if(ch == 1) {
181                 drec->set_volume(0, decibel_l, decibel_r);
182         } else if(ch == 2) {
183                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
184                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
185                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
186         }
187 }
188 #endif
189
190 // ----------------------------------------------------------------------------
191 // notify key
192 // ----------------------------------------------------------------------------
193
194 void VM::key_down(int code, bool repeat)
195 {
196         io->key_down(code);
197 }
198
199 void VM::key_up(int code)
200 {
201         io->key_up(code);
202 }
203
204 // ----------------------------------------------------------------------------
205 // user interface
206 // ----------------------------------------------------------------------------
207
208 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
209 {
210         tf20->open_disk(drv, file_path, bank);
211 }
212
213 void VM::close_floppy_disk(int drv)
214 {
215         tf20->close_disk(drv);
216 }
217
218 bool VM::is_floppy_disk_inserted(int drv)
219 {
220         return tf20->is_disk_inserted(drv);
221 }
222
223 void VM::is_floppy_disk_protected(int drv, bool value)
224 {
225         tf20->is_disk_protected(drv, value);
226 }
227
228 bool VM::is_floppy_disk_protected(int drv)
229 {
230         return tf20->is_disk_protected(drv);
231 }
232
233 uint32_t VM::is_floppy_disk_accessed()
234 {
235         return tf20->read_signal(0);
236 }
237
238 void VM::play_tape(int drv, const _TCHAR* file_path)
239 {
240         drec->play_tape(file_path);
241 //      drec->set_remote(true);
242 }
243
244 void VM::rec_tape(int drv, const _TCHAR* file_path)
245 {
246         drec->rec_tape(file_path);
247 //      drec->set_remote(true);
248 }
249
250 void VM::close_tape(int drv)
251 {
252         emu->lock_vm();
253         drec->close_tape();
254         emu->unlock_vm();
255 //      drec->set_remote(false);
256 }
257
258 bool VM::is_tape_inserted(int drv)
259 {
260         return drec->is_tape_inserted();
261 }
262
263 bool VM::is_tape_playing(int drv)
264 {
265         return drec->is_tape_playing();
266 }
267
268 bool VM::is_tape_recording(int drv)
269 {
270         return drec->is_tape_recording();
271 }
272
273 int VM::get_tape_position(int drv)
274 {
275         return drec->get_tape_position();
276 }
277
278 const _TCHAR* VM::get_tape_message(int drv)
279 {
280         return drec->get_message();
281 }
282
283 void VM::push_play(int drv)
284 {
285         drec->set_ff_rew(0);
286         drec->set_remote(true);
287 }
288
289 void VM::push_stop(int drv)
290 {
291         drec->set_remote(false);
292 }
293
294 void VM::push_fast_forward(int drv)
295 {
296         drec->set_ff_rew(1);
297         drec->set_remote(true);
298 }
299
300 void VM::push_fast_rewind(int drv)
301 {
302         drec->set_ff_rew(-1);
303         drec->set_remote(true);
304 }
305
306 bool VM::is_frame_skippable()
307 {
308         return event->is_frame_skippable();
309 }
310
311 void VM::update_config()
312 {
313         for(DEVICE* device = first_device; device; device = device->next_device) {
314                 device->update_config();
315         }
316 }
317
318 #define STATE_VERSION   3
319
320 #include "../../statesub.h"
321 #include "../../qt/gui/csp_logger.h"
322 extern CSP_Logger DLL_PREFIX_I *csp_logger;
323
324 void VM::decl_state(void)
325 {
326         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::HC_40_HEAD")), csp_logger);
327         for(DEVICE* device = first_device; device; device = device->next_device) {
328                 device->decl_state();
329         }
330 }
331
332 void VM::save_state(FILEIO* state_fio)
333 {
334         //state_fio->FputUint32(STATE_VERSION);
335         
336         if(state_entry != NULL) {
337                 state_entry->save_state(state_fio);
338         }
339         for(DEVICE* device = first_device; device; device = device->next_device) {
340                 device->save_state(state_fio);
341         }
342 }
343
344 bool VM::load_state(FILEIO* state_fio)
345 {
346         //if(state_fio->FgetUint32() != STATE_VERSION) {
347         //      return false;
348         //}
349         bool mb = false;
350         if(state_entry != NULL) {
351                 mb = state_entry->load_state(state_fio);
352         }
353         if(!mb) {
354                 emu->out_debug_log("INFO: HEADER DATA ERROR");
355                 return false;
356         }
357         for(DEVICE* device = first_device; device; device = device->next_device) {
358                 if(!device->load_state(state_fio)) {
359                         return false;
360                 }
361         }
362         return true;
363 }
364