OSDN Git Service

df2003427631cc4e78304a1f8238a7c9284ccaca
[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         for(DEVICE* device = first_device; device; device = device->next_device) {
68                 device->initialize();
69         }
70         decl_state();
71 }
72
73 VM::~VM()
74 {
75         // delete all devices
76         for(DEVICE* device = first_device; device;) {
77                 DEVICE *next_device = device->next_device;
78                 device->release();
79                 delete device;
80                 device = next_device;
81         }
82 }
83
84 DEVICE* VM::get_device(int id)
85 {
86         for(DEVICE* device = first_device; device; device = device->next_device) {
87                 if(device->this_device_id == id) {
88                         return device;
89                 }
90         }
91         return NULL;
92 }
93
94 // ----------------------------------------------------------------------------
95 // drive virtual machine
96 // ----------------------------------------------------------------------------
97
98 void VM::reset()
99 {
100         // reset all devices
101         for(DEVICE* device = first_device; device; device = device->next_device) {
102                 device->reset();
103         }
104 }
105
106 void VM::special_reset()
107 {
108         // system reset
109         for(DEVICE* device = first_device; device; device = device->next_device) {
110                 device->reset();
111         }
112         io->sysreset();
113 }
114
115 void VM::run()
116 {
117         event->drive();
118 }
119
120 // ----------------------------------------------------------------------------
121 // debugger
122 // ----------------------------------------------------------------------------
123
124 #ifdef USE_DEBUGGER
125 DEVICE *VM::get_cpu(int index)
126 {
127         if(index == 0) {
128                 return cpu;
129         }
130         return NULL;
131 }
132 #endif
133
134 // ----------------------------------------------------------------------------
135 // draw screen
136 // ----------------------------------------------------------------------------
137
138 void VM::draw_screen()
139 {
140         io->draw_screen();
141 }
142
143 // ----------------------------------------------------------------------------
144 // soud manager
145 // ----------------------------------------------------------------------------
146
147 void VM::initialize_sound(int rate, int samples)
148 {
149         // init sound manager
150         event->initialize_sound(rate, samples);
151         
152         // init sound gen
153         beep->initialize_sound(rate, 1000, 8000);
154 }
155
156 uint16_t* VM::create_sound(int* extra_frames)
157 {
158         return event->create_sound(extra_frames);
159 }
160
161 int VM::get_sound_buffer_ptr()
162 {
163         return event->get_sound_buffer_ptr();
164 }
165
166 #ifdef USE_SOUND_VOLUME
167 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
168 {
169         if(ch == 0) {
170                 beep->set_volume(0, decibel_l, decibel_r);
171         }
172 }
173 #endif
174
175 // ----------------------------------------------------------------------------
176 // notify key
177 // ----------------------------------------------------------------------------
178
179 void VM::key_down(int code, bool repeat)
180 {
181         io->key_down(code);
182 }
183
184 void VM::key_up(int code)
185 {
186         io->key_up(code);
187 }
188
189 // ----------------------------------------------------------------------------
190 // user interface
191 // ----------------------------------------------------------------------------
192
193 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
194 {
195         tf20->open_disk(drv, file_path, bank);
196 }
197
198 void VM::close_floppy_disk(int drv)
199 {
200         tf20->close_disk(drv);
201 }
202
203 bool VM::is_floppy_disk_inserted(int drv)
204 {
205         return tf20->is_disk_inserted(drv);
206 }
207
208 void VM::is_floppy_disk_protected(int drv, bool value)
209 {
210         tf20->is_disk_protected(drv, value);
211 }
212
213 bool VM::is_floppy_disk_protected(int drv)
214 {
215         return tf20->is_disk_protected(drv);
216 }
217
218 uint32_t VM::is_floppy_disk_accessed()
219 {
220         return tf20->read_signal(0);
221 }
222
223 bool VM::is_frame_skippable()
224 {
225         return event->is_frame_skippable();
226 }
227
228 void VM::update_config()
229 {
230         for(DEVICE* device = first_device; device; device = device->next_device) {
231                 device->update_config();
232         }
233 }
234
235 #define STATE_VERSION   2
236
237 #include "../../statesub.h"
238 #include "../../qt/gui/csp_logger.h"
239 extern CSP_Logger DLL_PREFIX_I *csp_logger;
240
241 void VM::decl_state(void)
242 {
243         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::HC_80_HEAD")), csp_logger);
244         for(DEVICE* device = first_device; device; device = device->next_device) {
245                 device->decl_state();
246         }
247 }
248
249 void VM::save_state(FILEIO* state_fio)
250 {
251         //state_fio->FputUint32(STATE_VERSION);
252         
253         if(state_entry != NULL) {
254                 state_entry->save_state(state_fio);
255         }
256         for(DEVICE* device = first_device; device; device = device->next_device) {
257                 device->save_state(state_fio);
258         }
259 }
260
261 bool VM::load_state(FILEIO* state_fio)
262 {
263         //if(state_fio->FgetUint32() != STATE_VERSION) {
264         //      return false;
265         //}
266         bool mb = false;
267         if(state_entry != NULL) {
268                 mb = state_entry->load_state(state_fio);
269         }
270         if(!mb) {
271                 emu->out_debug_log("INFO: HEADER DATA ERROR");
272                 return false;
273         }
274         for(DEVICE* device = first_device; device; device = device->next_device) {
275                 if(!device->load_state(state_fio)) {
276                         return false;
277                 }
278         }
279         return true;
280 }
281