OSDN Git Service

[VM][GAMEGEAR][HC40] Fix FTBFS.
[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) : emu(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 }
80
81 VM::~VM()
82 {
83         // delete all devices
84         for(DEVICE* device = first_device; device;) {
85                 DEVICE *next_device = device->next_device;
86                 device->release();
87                 delete device;
88                 device = next_device;
89         }
90 }
91
92 DEVICE* VM::get_device(int id)
93 {
94         for(DEVICE* device = first_device; device; device = device->next_device) {
95                 if(device->this_device_id == id) {
96                         return device;
97                 }
98         }
99         return NULL;
100 }
101
102 // ----------------------------------------------------------------------------
103 // drive virtual machine
104 // ----------------------------------------------------------------------------
105
106 void VM::reset()
107 {
108         // reset all devices
109         for(DEVICE* device = first_device; device; device = device->next_device) {
110                 device->reset();
111         }
112 }
113
114 void VM::special_reset()
115 {
116         // system reset
117         for(DEVICE* device = first_device; device; device = device->next_device) {
118                 device->reset();
119         }
120         io->sysreset();
121 }
122
123 void VM::run()
124 {
125         event->drive();
126 }
127
128 // ----------------------------------------------------------------------------
129 // debugger
130 // ----------------------------------------------------------------------------
131
132 #ifdef USE_DEBUGGER
133 DEVICE *VM::get_cpu(int index)
134 {
135         if(index == 0) {
136                 return cpu;
137         }
138         return NULL;
139 }
140 #endif
141
142 // ----------------------------------------------------------------------------
143 // draw screen
144 // ----------------------------------------------------------------------------
145
146 void VM::draw_screen()
147 {
148         io->draw_screen();
149 }
150
151 // ----------------------------------------------------------------------------
152 // soud manager
153 // ----------------------------------------------------------------------------
154
155 void VM::initialize_sound(int rate, int samples)
156 {
157         // init sound manager
158         event->initialize_sound(rate, samples);
159         
160         // init sound gen
161         beep->initialize_sound(rate, 1000, 8000);
162 }
163
164 uint16_t* VM::create_sound(int* extra_frames)
165 {
166         return event->create_sound(extra_frames);
167 }
168
169 int VM::get_sound_buffer_ptr()
170 {
171         return event->get_sound_buffer_ptr();
172 }
173
174 #ifdef USE_SOUND_VOLUME
175 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
176 {
177         if(ch == 0) {
178                 beep->set_volume(0, decibel_l, decibel_r);
179         } else if(ch == 1) {
180                 drec->set_volume(0, decibel_l, decibel_r);
181         } else if(ch == 2) {
182                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
183                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
184                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
185         }
186 }
187 #endif
188
189 // ----------------------------------------------------------------------------
190 // notify key
191 // ----------------------------------------------------------------------------
192
193 void VM::key_down(int code, bool repeat)
194 {
195         io->key_down(code);
196 }
197
198 void VM::key_up(int code)
199 {
200         io->key_up(code);
201 }
202
203 // ----------------------------------------------------------------------------
204 // user interface
205 // ----------------------------------------------------------------------------
206
207 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
208 {
209         tf20->open_disk(drv, file_path, bank);
210 }
211
212 void VM::close_floppy_disk(int drv)
213 {
214         tf20->close_disk(drv);
215 }
216
217 bool VM::is_floppy_disk_inserted(int drv)
218 {
219         return tf20->is_disk_inserted(drv);
220 }
221
222 void VM::is_floppy_disk_protected(int drv, bool value)
223 {
224         tf20->is_disk_protected(drv, value);
225 }
226
227 bool VM::is_floppy_disk_protected(int drv)
228 {
229         return tf20->is_disk_protected(drv);
230 }
231
232 uint32_t VM::is_floppy_disk_accessed()
233 {
234         return tf20->read_signal(0);
235 }
236
237 void VM::play_tape(int drv, const _TCHAR* file_path)
238 {
239         drec->play_tape(file_path);
240 }
241
242 void VM::rec_tape(int drv, const _TCHAR* file_path)
243 {
244         drec->rec_tape(file_path);
245 }
246
247 void VM::close_tape(int drv)
248 {
249         emu->lock_vm();
250         drec->close_tape();
251         emu->unlock_vm();
252 }
253
254 bool VM::is_tape_inserted(int drv)
255 {
256         return drec->is_tape_inserted();
257 }
258
259 bool VM::is_tape_playing(int drv)
260 {
261         return drec->is_tape_playing();
262 }
263
264 bool VM::is_tape_recording(int drv)
265 {
266         return drec->is_tape_recording();
267 }
268
269 int VM::get_tape_position(int drv)
270 {
271         return drec->get_tape_position();
272 }
273
274 const _TCHAR* VM::get_tape_message(int drv)
275 {
276         return drec->get_message();
277 }
278
279 bool VM::is_frame_skippable()
280 {
281         return event->is_frame_skippable();
282 }
283
284 void VM::update_config()
285 {
286         for(DEVICE* device = first_device; device; device = device->next_device) {
287                 device->update_config();
288         }
289 }
290
291 #define STATE_VERSION   2
292
293 void VM::save_state(FILEIO* state_fio)
294 {
295         state_fio->FputUint32(STATE_VERSION);
296         
297         for(DEVICE* device = first_device; device; device = device->next_device) {
298                 device->save_state(state_fio);
299         }
300 }
301
302 bool VM::load_state(FILEIO* state_fio)
303 {
304         if(state_fio->FgetUint32() != STATE_VERSION) {
305                 return false;
306         }
307         for(DEVICE* device = first_device; device; device = device->next_device) {
308                 if(!device->load_state(state_fio)) {
309                         return false;
310                 }
311         }
312         return true;
313 }
314