OSDN Git Service

[VM][WIP] Pre-process to apply new state framework.Still not buildable.
[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 #if defined(__GIT_REPO_VERSION)
77         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
78 #endif
79         for(DEVICE* device = first_device; device; device = device->next_device) {
80                 device->initialize();
81         }
82         decl_state();
83 }
84
85 VM::~VM()
86 {
87         // delete all devices
88         for(DEVICE* device = first_device; device;) {
89                 DEVICE *next_device = device->next_device;
90                 device->release();
91                 delete device;
92                 device = next_device;
93         }
94 }
95
96 DEVICE* VM::get_device(int id)
97 {
98         for(DEVICE* device = first_device; device; device = device->next_device) {
99                 if(device->this_device_id == id) {
100                         return device;
101                 }
102         }
103         return NULL;
104 }
105
106 // ----------------------------------------------------------------------------
107 // drive virtual machine
108 // ----------------------------------------------------------------------------
109
110 void VM::reset()
111 {
112         // reset all devices
113         for(DEVICE* device = first_device; device; device = device->next_device) {
114                 device->reset();
115         }
116 }
117
118 void VM::special_reset()
119 {
120         // system reset
121         for(DEVICE* device = first_device; device; device = device->next_device) {
122                 device->reset();
123         }
124         io->sysreset();
125 }
126
127 void VM::run()
128 {
129         event->drive();
130 }
131
132 // ----------------------------------------------------------------------------
133 // debugger
134 // ----------------------------------------------------------------------------
135
136 #ifdef USE_DEBUGGER
137 DEVICE *VM::get_cpu(int index)
138 {
139         if(index == 0) {
140                 return cpu;
141         }
142         return NULL;
143 }
144 #endif
145
146 // ----------------------------------------------------------------------------
147 // draw screen
148 // ----------------------------------------------------------------------------
149
150 void VM::draw_screen()
151 {
152         io->draw_screen();
153 }
154
155 // ----------------------------------------------------------------------------
156 // soud manager
157 // ----------------------------------------------------------------------------
158
159 void VM::initialize_sound(int rate, int samples)
160 {
161         // init sound manager
162         event->initialize_sound(rate, samples);
163         
164         // init sound gen
165         beep->initialize_sound(rate, 1000, 8000);
166 }
167
168 uint16_t* VM::create_sound(int* extra_frames)
169 {
170         return event->create_sound(extra_frames);
171 }
172
173 int VM::get_sound_buffer_ptr()
174 {
175         return event->get_sound_buffer_ptr();
176 }
177
178 #ifdef USE_SOUND_VOLUME
179 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
180 {
181         if(ch == 0) {
182                 beep->set_volume(0, decibel_l, decibel_r);
183         } else if(ch == 1) {
184                 drec->set_volume(0, decibel_l, decibel_r);
185         } else if(ch == 2) {
186                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
187                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
188                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
189         }
190 }
191 #endif
192
193 // ----------------------------------------------------------------------------
194 // notify key
195 // ----------------------------------------------------------------------------
196
197 void VM::key_down(int code, bool repeat)
198 {
199         io->key_down(code);
200 }
201
202 void VM::key_up(int code)
203 {
204         io->key_up(code);
205 }
206
207 // ----------------------------------------------------------------------------
208 // user interface
209 // ----------------------------------------------------------------------------
210
211 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
212 {
213         tf20->open_disk(drv, file_path, bank);
214 }
215
216 void VM::close_floppy_disk(int drv)
217 {
218         tf20->close_disk(drv);
219 }
220
221 bool VM::is_floppy_disk_inserted(int drv)
222 {
223         return tf20->is_disk_inserted(drv);
224 }
225
226 void VM::is_floppy_disk_protected(int drv, bool value)
227 {
228         tf20->is_disk_protected(drv, value);
229 }
230
231 bool VM::is_floppy_disk_protected(int drv)
232 {
233         return tf20->is_disk_protected(drv);
234 }
235
236 uint32_t VM::is_floppy_disk_accessed()
237 {
238         return tf20->read_signal(0);
239 }
240
241 void VM::play_tape(int drv, const _TCHAR* file_path)
242 {
243         drec->play_tape(file_path);
244 //      drec->set_remote(true);
245 }
246
247 void VM::rec_tape(int drv, const _TCHAR* file_path)
248 {
249         drec->rec_tape(file_path);
250 //      drec->set_remote(true);
251 }
252
253 void VM::close_tape(int drv)
254 {
255         emu->lock_vm();
256         drec->close_tape();
257         emu->unlock_vm();
258 //      drec->set_remote(false);
259 }
260
261 bool VM::is_tape_inserted(int drv)
262 {
263         return drec->is_tape_inserted();
264 }
265
266 bool VM::is_tape_playing(int drv)
267 {
268         return drec->is_tape_playing();
269 }
270
271 bool VM::is_tape_recording(int drv)
272 {
273         return drec->is_tape_recording();
274 }
275
276 int VM::get_tape_position(int drv)
277 {
278         return drec->get_tape_position();
279 }
280
281 const _TCHAR* VM::get_tape_message(int drv)
282 {
283         return drec->get_message();
284 }
285
286 void VM::push_play(int drv)
287 {
288         drec->set_ff_rew(0);
289         drec->set_remote(true);
290 }
291
292 void VM::push_stop(int drv)
293 {
294         drec->set_remote(false);
295 }
296
297 void VM::push_fast_forward(int drv)
298 {
299         drec->set_ff_rew(1);
300         drec->set_remote(true);
301 }
302
303 void VM::push_fast_rewind(int drv)
304 {
305         drec->set_ff_rew(-1);
306         drec->set_remote(true);
307 }
308
309 bool VM::is_frame_skippable()
310 {
311         return event->is_frame_skippable();
312 }
313
314 void VM::update_config()
315 {
316         for(DEVICE* device = first_device; device; device = device->next_device) {
317                 device->update_config();
318         }
319 }
320
321 #define STATE_VERSION   3
322
323 #include "../../statesub.h"
324 #include "../../qt/gui/csp_logger.h"
325 extern CSP_Logger DLL_PREFIX_I *csp_logger;
326
327 void VM::decl_state(void)
328 {
329         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::HC_40_HEAD")), csp_logger);
330         for(DEVICE* device = first_device; device; device = device->next_device) {
331                 device->decl_state();
332         }
333 }
334
335 void VM::save_state(FILEIO* state_fio)
336 {
337         //state_fio->FputUint32(STATE_VERSION);
338         
339         if(state_entry != NULL) {
340                 state_entry->save_state(state_fio);
341         }
342         for(DEVICE* device = first_device; device; device = device->next_device) {
343                 device->save_state(state_fio);
344         }
345 }
346
347 bool VM::load_state(FILEIO* state_fio)
348 {
349         //if(state_fio->FgetUint32() != STATE_VERSION) {
350         //      return false;
351         //}
352         bool mb = false;
353         if(state_entry != NULL) {
354                 mb = state_entry->load_state(state_fio);
355         }
356         if(!mb) {
357                 emu->out_debug_log("INFO: HEADER DATA ERROR");
358                 return false;
359         }
360         for(DEVICE* device = first_device; device; device = device->next_device) {
361                 if(!device->load_state(state_fio)) {
362                         return false;
363                 }
364         }
365         return true;
366 }
367
368 bool VM::process_state(FILEIO* state_fio, bool loading)
369 {
370         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
371                 return false;
372         }
373         for(DEVICE* device = first_device; device; device = device->next_device) {
374                 const char *name = typeid(*device).name() + 6; // skip "class "
375                 int len = strlen(name);
376                 
377                 if(!state_fio->StateCheckInt32(len)) {
378                         return false;
379                 }
380                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
381                         return false;
382                 }
383                 if(!device->process_state(state_fio, loading)) {
384                         return false;
385                 }
386         }
387         return true;
388 }