OSDN Git Service

[VM][General] Merge upstream 2016-02-13. Still don't implement OSD/Gui part of joysti...
[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 "../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) : emu(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         
38         beep = new BEEP(this, emu);
39         drec = new DATAREC(this, emu);
40         tf20 = new PTF20(this, emu);
41         cpu = new Z80(this, emu);
42         
43         io = new IO(this, emu);
44         memory = new MEMORY(this, emu);
45         
46         // set contexts
47         event->set_context_cpu(cpu);
48         event->set_context_sound(beep);
49         event->set_context_sound(drec);
50         
51         drec->set_context_ear(io, SIG_IO_DREC, 1);
52         tf20->set_context_sio(io, SIG_IO_ART);
53         
54         io->set_context_cpu(cpu);
55         io->set_context_mem(memory, memory->get_ram());
56         io->set_context_tf20(tf20);
57         io->set_context_beep(beep);
58         io->set_context_drec(drec);
59         
60         // cpu bus
61         cpu->set_context_mem(memory);
62         cpu->set_context_io(io);
63         cpu->set_context_intr(io);
64 #ifdef USE_DEBUGGER
65         cpu->set_context_debugger(new DEBUGGER(this, emu));
66 #endif
67         
68         // initialize all devices
69         for(DEVICE* device = first_device; device; device = device->next_device) {
70                 device->initialize();
71         }
72 }
73
74 VM::~VM()
75 {
76         // delete all devices
77         for(DEVICE* device = first_device; device;) {
78                 DEVICE *next_device = device->next_device;
79                 device->release();
80                 delete device;
81                 device = next_device;
82         }
83 }
84
85 DEVICE* VM::get_device(int id)
86 {
87         for(DEVICE* device = first_device; device; device = device->next_device) {
88                 if(device->this_device_id == id) {
89                         return device;
90                 }
91         }
92         return NULL;
93 }
94
95 // ----------------------------------------------------------------------------
96 // drive virtual machine
97 // ----------------------------------------------------------------------------
98
99 void VM::reset()
100 {
101         // reset all devices
102         for(DEVICE* device = first_device; device; device = device->next_device) {
103                 device->reset();
104         }
105 }
106
107 void VM::special_reset()
108 {
109         // system reset
110         for(DEVICE* device = first_device; device; device = device->next_device) {
111                 device->reset();
112         }
113         io->sysreset();
114 }
115
116 void VM::run()
117 {
118         event->drive();
119 }
120
121 // ----------------------------------------------------------------------------
122 // debugger
123 // ----------------------------------------------------------------------------
124
125 #ifdef USE_DEBUGGER
126 DEVICE *VM::get_cpu(int index)
127 {
128         if(index == 0) {
129                 return cpu;
130         }
131         return NULL;
132 }
133 #endif
134
135 // ----------------------------------------------------------------------------
136 // draw screen
137 // ----------------------------------------------------------------------------
138
139 void VM::draw_screen()
140 {
141         io->draw_screen();
142 }
143
144 int VM::access_lamp()
145 {
146         uint32 status = tf20->read_signal(0);
147         return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
148 }
149
150 // ----------------------------------------------------------------------------
151 // soud manager
152 // ----------------------------------------------------------------------------
153
154 void VM::initialize_sound(int rate, int samples)
155 {
156         // init sound manager
157         event->initialize_sound(rate, samples);
158         
159         // init sound gen
160         beep->init(rate, 1000, 8000);
161 }
162
163 uint16* VM::create_sound(int* extra_frames)
164 {
165         return event->create_sound(extra_frames);
166 }
167
168 int VM::sound_buffer_ptr()
169 {
170         return event->sound_buffer_ptr();
171 }
172
173 #ifdef USE_SOUND_VOLUME
174 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
175 {
176         if(ch == 0) {
177                 beep->set_volume(0, decibel_l, decibel_r);
178         } else if(ch == 1) {
179                 drec->set_volume(0, decibel_l, decibel_r);
180         }
181 }
182 #endif
183
184 // ----------------------------------------------------------------------------
185 // notify key
186 // ----------------------------------------------------------------------------
187
188 void VM::key_down(int code, bool repeat)
189 {
190         io->key_down(code);
191 }
192
193 void VM::key_up(int code)
194 {
195         io->key_up(code);
196 }
197
198 // ----------------------------------------------------------------------------
199 // user interface
200 // ----------------------------------------------------------------------------
201
202 void VM::open_disk(int drv, const _TCHAR* file_path, int bank)
203 {
204         tf20->open_disk(drv, file_path, bank);
205 }
206
207 void VM::close_disk(int drv)
208 {
209         tf20->close_disk(drv);
210 }
211
212 bool VM::disk_inserted(int drv)
213 {
214         return tf20->disk_inserted(drv);
215 }
216
217 void VM::set_disk_protected(int drv, bool value)
218 {
219         tf20->set_disk_protected(drv, value);
220 }
221
222 bool VM::get_disk_protected(int drv)
223 {
224         return tf20->get_disk_protected(drv);
225 }
226
227 void VM::play_tape(const _TCHAR* file_path)
228 {
229         drec->play_tape(file_path);
230 }
231
232 void VM::rec_tape(const _TCHAR* file_path)
233 {
234         drec->rec_tape(file_path);
235 }
236
237 void VM::close_tape()
238 {
239         drec->close_tape();
240 }
241
242 bool VM::tape_inserted()
243 {
244         return drec->tape_inserted();
245 }
246
247 bool VM::tape_playing()
248 {
249         return drec->tape_playing();
250 }
251
252 bool VM::tape_recording()
253 {
254         return drec->tape_recording();
255 }
256
257 int VM::tape_position()
258 {
259         return drec->tape_position();
260 }
261
262 bool VM::now_skip()
263 {
264         return event->now_skip();
265 }
266
267 void VM::update_config()
268 {
269         for(DEVICE* device = first_device; device; device = device->next_device) {
270                 device->update_config();
271         }
272 }
273
274 #define STATE_VERSION   1
275
276 void VM::save_state(FILEIO* state_fio)
277 {
278         state_fio->FputUint32(STATE_VERSION);
279         
280         for(DEVICE* device = first_device; device; device = device->next_device) {
281                 device->save_state(state_fio);
282         }
283 }
284
285 bool VM::load_state(FILEIO* state_fio)
286 {
287         if(state_fio->FgetUint32() != STATE_VERSION) {
288                 return false;
289         }
290         for(DEVICE* device = first_device; device; device = device->next_device) {
291                 if(!device->load_state(state_fio)) {
292                         return false;
293                 }
294         }
295         return true;
296 }
297