OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[csp-qt/common_source_project-fm7.git] / source / src / vm / hc80 / hc80.cpp
1 /*
2         EPSON HC-80 Emulator 'eHC-80'
3
4         Author : Takeda.Toshiya
5         Date   : 2008.03.14 -
6
7         [ virtual machine ]
8 */
9
10 #include "hc80.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../beep.h"
16 #include "../i8251.h"
17 #include "../ptf20.h"
18 #include "../z80.h"
19
20 #ifdef USE_DEBUGGER
21 #include "../debugger.h"
22 #endif
23
24 #include "io.h"
25 #include "memory.h"
26
27 // ----------------------------------------------------------------------------
28 // initialize
29 // ----------------------------------------------------------------------------
30
31 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
32 {
33         // create devices
34         first_device = last_device = NULL;
35         dummy = new DEVICE(this, emu);  // must be 1st device
36         event = new EVENT(this, emu);   // must be 2nd device
37         dummy->set_device_name(_T("1st Dummy"));
38         
39         beep = new BEEP(this, emu);
40         sio = new I8251(this, emu);
41         tf20 = new PTF20(this, emu);
42         cpu = new Z80(this, emu);
43         
44         io = new IO(this, emu);
45         memory = new MEMORY(this, emu);
46         // set contexts
47         event->set_context_cpu(cpu);
48         event->set_context_sound(beep);
49         
50         tf20->set_context_sio(io, SIG_IO_TF20);
51         
52         io->set_context_cpu(cpu);
53         io->set_context_mem(memory);
54         io->set_context_sio(sio);
55         io->set_context_beep(beep);
56         io->set_context_tf20(tf20);
57         
58         // cpu bus
59         cpu->set_context_mem(memory);
60         cpu->set_context_io(io);
61         cpu->set_context_intr(io);
62 #ifdef USE_DEBUGGER
63         cpu->set_context_debugger(new DEBUGGER(this, emu));
64 #endif
65         
66         // initialize all devices
67 #if defined(__GIT_REPO_VERSION)
68         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
69 #endif
70         for(DEVICE* device = first_device; device; device = device->next_device) {
71                 device->initialize();
72         }
73         decl_state();
74 }
75
76 VM::~VM()
77 {
78         // delete all devices
79         for(DEVICE* device = first_device; device;) {
80                 DEVICE *next_device = device->next_device;
81                 device->release();
82                 delete device;
83                 device = next_device;
84         }
85 }
86
87 DEVICE* VM::get_device(int id)
88 {
89         for(DEVICE* device = first_device; device; device = device->next_device) {
90                 if(device->this_device_id == id) {
91                         return device;
92                 }
93         }
94         return NULL;
95 }
96
97 // ----------------------------------------------------------------------------
98 // drive virtual machine
99 // ----------------------------------------------------------------------------
100
101 void VM::reset()
102 {
103         // reset all devices
104         for(DEVICE* device = first_device; device; device = device->next_device) {
105                 device->reset();
106         }
107 }
108
109 void VM::special_reset()
110 {
111         // system reset
112         for(DEVICE* device = first_device; device; device = device->next_device) {
113                 device->reset();
114         }
115         io->sysreset();
116 }
117
118 void VM::run()
119 {
120         event->drive();
121 }
122
123 // ----------------------------------------------------------------------------
124 // debugger
125 // ----------------------------------------------------------------------------
126
127 #ifdef USE_DEBUGGER
128 DEVICE *VM::get_cpu(int index)
129 {
130         if(index == 0) {
131                 return cpu;
132         }
133         return NULL;
134 }
135 #endif
136
137 // ----------------------------------------------------------------------------
138 // draw screen
139 // ----------------------------------------------------------------------------
140
141 void VM::draw_screen()
142 {
143         io->draw_screen();
144 }
145
146 // ----------------------------------------------------------------------------
147 // soud manager
148 // ----------------------------------------------------------------------------
149
150 void VM::initialize_sound(int rate, int samples)
151 {
152         // init sound manager
153         event->initialize_sound(rate, samples);
154         
155         // init sound gen
156         beep->initialize_sound(rate, 1000, 8000);
157 }
158
159 uint16_t* VM::create_sound(int* extra_frames)
160 {
161         return event->create_sound(extra_frames);
162 }
163
164 int VM::get_sound_buffer_ptr()
165 {
166         return event->get_sound_buffer_ptr();
167 }
168
169 #ifdef USE_SOUND_VOLUME
170 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
171 {
172         if(ch == 0) {
173                 beep->set_volume(0, decibel_l, decibel_r);
174         }
175 }
176 #endif
177
178 // ----------------------------------------------------------------------------
179 // notify key
180 // ----------------------------------------------------------------------------
181
182 void VM::key_down(int code, bool repeat)
183 {
184         io->key_down(code);
185 }
186
187 void VM::key_up(int code)
188 {
189         io->key_up(code);
190 }
191
192 // ----------------------------------------------------------------------------
193 // user interface
194 // ----------------------------------------------------------------------------
195
196 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
197 {
198         tf20->open_disk(drv, file_path, bank);
199 }
200
201 void VM::close_floppy_disk(int drv)
202 {
203         tf20->close_disk(drv);
204 }
205
206 bool VM::is_floppy_disk_inserted(int drv)
207 {
208         return tf20->is_disk_inserted(drv);
209 }
210
211 void VM::is_floppy_disk_protected(int drv, bool value)
212 {
213         tf20->is_disk_protected(drv, value);
214 }
215
216 bool VM::is_floppy_disk_protected(int drv)
217 {
218         return tf20->is_disk_protected(drv);
219 }
220
221 uint32_t VM::is_floppy_disk_accessed()
222 {
223         return tf20->read_signal(0);
224 }
225
226 bool VM::is_frame_skippable()
227 {
228         return event->is_frame_skippable();
229 }
230
231 void VM::update_config()
232 {
233         for(DEVICE* device = first_device; device; device = device->next_device) {
234                 device->update_config();
235         }
236 }
237
238 #define STATE_VERSION   2
239
240 #include "../../statesub.h"
241 #include "../../qt/gui/csp_logger.h"
242 extern CSP_Logger DLL_PREFIX_I *csp_logger;
243
244 void VM::decl_state(void)
245 {
246         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::HC_80_HEAD")), csp_logger);
247         for(DEVICE* device = first_device; device; device = device->next_device) {
248                 device->decl_state();
249         }
250 }
251
252 void VM::save_state(FILEIO* state_fio)
253 {
254         //state_fio->FputUint32(STATE_VERSION);
255         
256         if(state_entry != NULL) {
257                 state_entry->save_state(state_fio);
258         }
259         for(DEVICE* device = first_device; device; device = device->next_device) {
260                 device->save_state(state_fio);
261         }
262 }
263
264 bool VM::load_state(FILEIO* state_fio)
265 {
266         //if(state_fio->FgetUint32() != STATE_VERSION) {
267         //      return false;
268         //}
269         bool mb = false;
270         if(state_entry != NULL) {
271                 mb = state_entry->load_state(state_fio);
272         }
273         if(!mb) {
274                 emu->out_debug_log("INFO: HEADER DATA ERROR");
275                 return false;
276         }
277         for(DEVICE* device = first_device; device; device = device->next_device) {
278                 if(!device->load_state(state_fio)) {
279                         return false;
280                 }
281         }
282         return true;
283 }
284