OSDN Git Service

9e2a490cb46fe5c029a8714023e4e10af6adef74
[csp-qt/common_source_project-fm7.git] / source / src / vm / phc20 / phc20.cpp
1 /*
2         SANYO PHC-20 Emulator 'ePHC-20'
3
4         Author : Takeda.Toshiya
5         Date   : 2010.09.03-
6
7         [ virtual machine ]
8 */
9
10 #include "phc20.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../mc6847.h"
17 #include "../noise.h"
18 #include "../z80.h"
19
20 #ifdef USE_DEBUGGER
21 #include "../debugger.h"
22 #endif
23
24 #include "memory.h"
25
26 // ----------------------------------------------------------------------------
27 // initialize
28 // ----------------------------------------------------------------------------
29
30 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
31 {
32         // create devices
33         first_device = last_device = NULL;
34         dummy = new DEVICE(this, emu);  // must be 1st device
35         event = new EVENT(this, emu);   // must be 2nd device
36         dummy->set_device_name(_T("1st Dummy"));
37
38         drec = new DATAREC(this, emu);
39         drec->set_context_noise_play(new NOISE(this, emu));
40         drec->set_context_noise_stop(new NOISE(this, emu));
41         drec->set_context_noise_fast(new NOISE(this, emu));
42         vdp = new MC6847(this, emu);
43         cpu = new Z80(this, emu);
44         memory = new MEMORY(this, emu);
45         // set contexts
46         event->set_context_cpu(cpu);
47         event->set_context_sound(drec);
48         event->set_context_sound(drec->get_context_noise_play());
49         event->set_context_sound(drec->get_context_noise_stop());
50         event->set_context_sound(drec->get_context_noise_fast());
51         
52         drec->set_context_ear(memory, SIG_MEMORY_SYSPORT, 1);
53         vdp->set_context_vsync(memory, SIG_MEMORY_SYSPORT, 2);  // vsync / hsync?
54         
55         vdp->load_font_image(create_local_path(_T("FONT.ROM")));
56         vdp->set_vram_ptr(memory->get_vram(), 0x400);
57         vdp->set_context_cpu(cpu);
58         vdp->write_signal(SIG_MC6847_AG, 0, 0);         // force character mode
59         vdp->write_signal(SIG_MC6847_AS, 0, 0);
60         vdp->write_signal(SIG_MC6847_INTEXT, 0, 0);     // force internal character ???
61         vdp->write_signal(SIG_MC6847_CSS, 0, 0);
62         
63         memory->set_context_drec(drec);
64         
65         // cpu bus
66         cpu->set_context_mem(memory);
67         cpu->set_context_io(dummy);
68         cpu->set_context_intr(dummy);
69 #ifdef USE_DEBUGGER
70         cpu->set_context_debugger(new DEBUGGER(this, emu));
71 #endif
72         
73         // initialize all devices
74         for(DEVICE* device = first_device; device; device = device->next_device) {
75                 device->initialize();
76         }
77         decl_state();
78 }
79
80 VM::~VM()
81 {
82         // delete all devices
83         for(DEVICE* device = first_device; device;) {
84                 DEVICE *next_device = device->next_device;
85                 device->release();
86                 delete device;
87                 device = next_device;
88         }
89 }
90
91 DEVICE* VM::get_device(int id)
92 {
93         for(DEVICE* device = first_device; device; device = device->next_device) {
94                 if(device->this_device_id == id) {
95                         return device;
96                 }
97         }
98         return NULL;
99 }
100
101 // ----------------------------------------------------------------------------
102 // drive virtual machine
103 // ----------------------------------------------------------------------------
104
105 void VM::reset()
106 {
107         // reset all devices
108         for(DEVICE* device = first_device; device; device = device->next_device) {
109                 device->reset();
110         }
111 }
112
113 void VM::run()
114 {
115         event->drive();
116 }
117
118 // ----------------------------------------------------------------------------
119 // debugger
120 // ----------------------------------------------------------------------------
121
122 #ifdef USE_DEBUGGER
123 DEVICE *VM::get_cpu(int index)
124 {
125         if(index == 0) {
126                 return cpu;
127         }
128         return NULL;
129 }
130 #endif
131
132 // ----------------------------------------------------------------------------
133 // draw screen
134 // ----------------------------------------------------------------------------
135
136 void VM::draw_screen()
137 {
138         vdp->draw_screen();
139 }
140
141 // ----------------------------------------------------------------------------
142 // soud manager
143 // ----------------------------------------------------------------------------
144
145 void VM::initialize_sound(int rate, int samples)
146 {
147         // init sound manager
148         event->initialize_sound(rate, samples);
149 }
150
151 uint16_t* VM::create_sound(int* extra_frames)
152 {
153         return event->create_sound(extra_frames);
154 }
155
156 int VM::get_sound_buffer_ptr()
157 {
158         return event->get_sound_buffer_ptr();
159 }
160
161 #ifdef USE_SOUND_VOLUME
162 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
163 {
164         if(ch == 0) {
165                 drec->set_volume(0, decibel_l, decibel_r);
166         } else if(ch == 1) {
167                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
168                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
169                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
170         }
171 }
172 #endif
173
174 // ----------------------------------------------------------------------------
175 // user interface
176 // ----------------------------------------------------------------------------
177
178 void VM::play_tape(int drv, const _TCHAR* file_path)
179 {
180         drec->play_tape(file_path);
181 //      drec->set_remote(true);
182 }
183
184 void VM::rec_tape(int drv, const _TCHAR* file_path)
185 {
186         drec->rec_tape(file_path);
187 //      drec->set_remote(true);
188 }
189
190 void VM::close_tape(int drv)
191 {
192         emu->lock_vm();
193         drec->close_tape();
194         emu->unlock_vm();
195 //      drec->set_remote(false);
196 }
197
198 bool VM::is_tape_inserted(int drv)
199 {
200         return drec->is_tape_inserted();
201 }
202
203 bool VM::is_tape_playing(int drv)
204 {
205         return drec->is_tape_playing();
206 }
207
208 bool VM::is_tape_recording(int drv)
209 {
210         return drec->is_tape_recording();
211 }
212
213 int VM::get_tape_position(int drv)
214 {
215         return drec->get_tape_position();
216 }
217
218 const _TCHAR* VM::get_tape_message(int drv)
219 {
220         return drec->get_message();
221 }
222
223 void VM::push_play(int drv)
224 {
225         drec->set_ff_rew(0);
226         drec->set_remote(true);
227 }
228
229 void VM::push_stop(int drv)
230 {
231         drec->set_remote(false);
232 }
233
234 void VM::push_fast_forward(int drv)
235 {
236         drec->set_ff_rew(1);
237         drec->set_remote(true);
238 }
239
240 void VM::push_fast_rewind(int drv)
241 {
242         drec->set_ff_rew(-1);
243         drec->set_remote(true);
244 }
245
246 bool VM::is_frame_skippable()
247 {
248         return event->is_frame_skippable();
249 }
250
251 void VM::update_config()
252 {
253         for(DEVICE* device = first_device; device; device = device->next_device) {
254                 device->update_config();
255         }
256 }
257
258 #define STATE_VERSION   3
259
260 #include "../../statesub.h"
261 #include "../../qt/gui/csp_logger.h"
262 extern CSP_Logger DLL_PREFIX_I *csp_logger;
263
264 void VM::decl_state(void)
265 {
266         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PHC_20_HEAD")), csp_logger);
267         
268         for(DEVICE* device = first_device; device; device = device->next_device) {
269                 device->decl_state();
270         }
271 }
272
273 void VM::save_state(FILEIO* state_fio)
274 {
275         //state_fio->FputUint32(STATE_VERSION);
276         
277         if(state_entry != NULL) {
278                 state_entry->save_state(state_fio);
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         
291         bool mb = false;
292         if(state_entry != NULL) {
293                 mb = state_entry->load_state(state_fio);
294         }
295         if(!mb) {
296                 emu->out_debug_log("INFO: HEADER DATA ERROR");
297                 return false;
298         }
299         for(DEVICE* device = first_device; device; device = device->next_device) {
300                 if(!device->load_state(state_fio)) {
301                         return false;
302                 }
303         }
304         return true;
305 }
306