OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[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 #if defined(__GIT_REPO_VERSION)
75         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
76 #endif
77         for(DEVICE* device = first_device; device; device = device->next_device) {
78                 device->initialize();
79         }
80         decl_state();
81 }
82
83 VM::~VM()
84 {
85         // delete all devices
86         for(DEVICE* device = first_device; device;) {
87                 DEVICE *next_device = device->next_device;
88                 device->release();
89                 delete device;
90                 device = next_device;
91         }
92 }
93
94 DEVICE* VM::get_device(int id)
95 {
96         for(DEVICE* device = first_device; device; device = device->next_device) {
97                 if(device->this_device_id == id) {
98                         return device;
99                 }
100         }
101         return NULL;
102 }
103
104 // ----------------------------------------------------------------------------
105 // drive virtual machine
106 // ----------------------------------------------------------------------------
107
108 void VM::reset()
109 {
110         // reset all devices
111         for(DEVICE* device = first_device; device; device = device->next_device) {
112                 device->reset();
113         }
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         vdp->draw_screen();
142 }
143
144 // ----------------------------------------------------------------------------
145 // soud manager
146 // ----------------------------------------------------------------------------
147
148 void VM::initialize_sound(int rate, int samples)
149 {
150         // init sound manager
151         event->initialize_sound(rate, samples);
152 }
153
154 uint16_t* VM::create_sound(int* extra_frames)
155 {
156         return event->create_sound(extra_frames);
157 }
158
159 int VM::get_sound_buffer_ptr()
160 {
161         return event->get_sound_buffer_ptr();
162 }
163
164 #ifdef USE_SOUND_VOLUME
165 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
166 {
167         if(ch == 0) {
168                 drec->set_volume(0, decibel_l, decibel_r);
169         } else if(ch == 1) {
170                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
171                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
172                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
173         }
174 }
175 #endif
176
177 // ----------------------------------------------------------------------------
178 // user interface
179 // ----------------------------------------------------------------------------
180
181 void VM::play_tape(int drv, const _TCHAR* file_path)
182 {
183         drec->play_tape(file_path);
184 //      drec->set_remote(true);
185 }
186
187 void VM::rec_tape(int drv, const _TCHAR* file_path)
188 {
189         drec->rec_tape(file_path);
190 //      drec->set_remote(true);
191 }
192
193 void VM::close_tape(int drv)
194 {
195         emu->lock_vm();
196         drec->close_tape();
197         emu->unlock_vm();
198 //      drec->set_remote(false);
199 }
200
201 bool VM::is_tape_inserted(int drv)
202 {
203         return drec->is_tape_inserted();
204 }
205
206 bool VM::is_tape_playing(int drv)
207 {
208         return drec->is_tape_playing();
209 }
210
211 bool VM::is_tape_recording(int drv)
212 {
213         return drec->is_tape_recording();
214 }
215
216 int VM::get_tape_position(int drv)
217 {
218         return drec->get_tape_position();
219 }
220
221 const _TCHAR* VM::get_tape_message(int drv)
222 {
223         return drec->get_message();
224 }
225
226 void VM::push_play(int drv)
227 {
228         drec->set_ff_rew(0);
229         drec->set_remote(true);
230 }
231
232 void VM::push_stop(int drv)
233 {
234         drec->set_remote(false);
235 }
236
237 void VM::push_fast_forward(int drv)
238 {
239         drec->set_ff_rew(1);
240         drec->set_remote(true);
241 }
242
243 void VM::push_fast_rewind(int drv)
244 {
245         drec->set_ff_rew(-1);
246         drec->set_remote(true);
247 }
248
249 bool VM::is_frame_skippable()
250 {
251         return event->is_frame_skippable();
252 }
253
254 void VM::update_config()
255 {
256         for(DEVICE* device = first_device; device; device = device->next_device) {
257                 device->update_config();
258         }
259 }
260
261 #define STATE_VERSION   3
262
263 #include "../../statesub.h"
264 #include "../../qt/gui/csp_logger.h"
265 extern CSP_Logger DLL_PREFIX_I *csp_logger;
266
267 void VM::decl_state(void)
268 {
269         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PHC_20_HEAD")), csp_logger);
270         
271         for(DEVICE* device = first_device; device; device = device->next_device) {
272                 device->decl_state();
273         }
274 }
275
276 void VM::save_state(FILEIO* state_fio)
277 {
278         //state_fio->FputUint32(STATE_VERSION);
279         
280         if(state_entry != NULL) {
281                 state_entry->save_state(state_fio);
282         }
283         for(DEVICE* device = first_device; device; device = device->next_device) {
284                 device->save_state(state_fio);
285         }
286 }
287
288 bool VM::load_state(FILEIO* state_fio)
289 {
290         //if(state_fio->FgetUint32() != STATE_VERSION) {
291         //      return false;
292         //}
293         
294         bool mb = false;
295         if(state_entry != NULL) {
296                 mb = state_entry->load_state(state_fio);
297         }
298         if(!mb) {
299                 emu->out_debug_log("INFO: HEADER DATA ERROR");
300                 return false;
301         }
302         for(DEVICE* device = first_device; device; device = device->next_device) {
303                 if(!device->load_state(state_fio)) {
304                         return false;
305                 }
306         }
307         return true;
308 }
309