OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[csp-qt/common_source_project-fm7.git] / source / src / vm / phc25 / phc25.cpp
1 /*
2         SANYO PHC-25 Emulator 'ePHC-25'
3         SEIKO MAP-1010 Emulator 'eMAP-1010'
4
5         Author : Takeda.Toshiya
6         Date   : 2010.08.03-
7
8         [ virtual machine ]
9 */
10
11 #include "phc25.h"
12 #include "../../emu.h"
13 #include "../device.h"
14 #include "../event.h"
15
16 #include "../datarec.h"
17 #include "../io.h"
18 #include "../mc6847.h"
19 #include "../noise.h"
20 #include "../not.h"
21 //#include "../ym2203.h"
22 #include "../ay_3_891x.h"
23 #include "../z80.h"
24
25 #ifdef USE_DEBUGGER
26 #include "../debugger.h"
27 #endif
28
29 #include "joystick.h"
30 #include "keyboard.h"
31 #include "memory.h"
32 #include "system.h"
33
34 // ----------------------------------------------------------------------------
35 // initialize
36 // ----------------------------------------------------------------------------
37
38 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
39 {
40         // create devices
41         first_device = last_device = NULL;
42         dummy = new DEVICE(this, emu);  // must be 1st device
43         event = new EVENT(this, emu);   // must be 2nd device
44         dummy->set_device_name(_T("1st Dummy"));
45         
46         drec = new DATAREC(this, emu);
47         drec->set_context_noise_play(new NOISE(this, emu));
48         drec->set_context_noise_stop(new NOISE(this, emu));
49         drec->set_context_noise_fast(new NOISE(this, emu));
50         io = new IO(this, emu);
51         vdp = new MC6847(this, emu);
52         not_vsync = new NOT(this, emu);
53 //      psg = new YM2203(this, emu);
54         psg = new AY_3_891X(this, emu);
55         cpu = new Z80(this, emu);
56         not_vsync->set_device_name(_T("NOT GATE(VSYNC)"));
57         
58         joystick = new JOYSTICK(this, emu);
59         keyboard = new KEYBOARD(this, emu);
60         memory = new MEMORY(this, emu);
61         system = new SYSTEM(this, emu);
62         // set contexts
63         event->set_context_cpu(cpu);
64         event->set_context_sound(psg);
65         event->set_context_sound(drec);
66         event->set_context_sound(drec->get_context_noise_play());
67         event->set_context_sound(drec->get_context_noise_stop());
68         event->set_context_sound(drec->get_context_noise_fast());
69         
70         vdp->load_font_image(create_local_path(_T("FONT.ROM")));
71         vdp->set_vram_ptr(memory->get_vram(), 0x1800);
72 //      vdp->set_context_cpu(cpu);
73         vdp->set_context_vsync(not_vsync, SIG_NOT_INPUT, 1);
74         not_vsync->set_context_out(cpu, SIG_CPU_IRQ, 1);
75         
76         vdp->set_context_vsync(system, SIG_SYSTEM_PORT, 0x10);
77         drec->set_context_ear(system, SIG_SYSTEM_PORT, 0x20);
78         // bit6: printer busy
79         vdp->set_context_hsync(system, SIG_SYSTEM_PORT, 0x80);
80         
81         joystick->set_context_psg(psg);
82 #ifdef _MAP1010
83         memory->set_context_keyboard(keyboard);
84 #endif
85         system->set_context_drec(drec);
86         system->set_context_vdp(vdp);
87         
88         // cpu bus
89         cpu->set_context_mem(memory);
90         cpu->set_context_io(io);
91         cpu->set_context_intr(dummy);
92 #ifdef USE_DEBUGGER
93         cpu->set_context_debugger(new DEBUGGER(this, emu));
94 #endif
95         
96         // i/o bus
97         io->set_iomap_single_rw(0x40, system);
98 #ifndef _MAP1010
99         io->set_iomap_range_r(0x80, 0x88, keyboard);
100 #endif
101         io->set_iomap_alias_w(0xc0, psg, 1);    // PSG data
102         io->set_iomap_alias_w(0xc1, psg, 0);    // PSG ch
103 //      io->set_iomap_alias_r(0xc0, psg, 1);
104         io->set_iomap_alias_r(0xc1, psg, 1);    // PSG data
105         
106         // initialize all devices
107 #if defined(__GIT_REPO_VERSION)
108         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
109 #endif
110         for(DEVICE* device = first_device; device; device = device->next_device) {
111                 device->initialize();
112         }
113         decl_state();
114 }
115
116 VM::~VM()
117 {
118         // delete all devices
119         for(DEVICE* device = first_device; device;) {
120                 DEVICE *next_device = device->next_device;
121                 device->release();
122                 delete device;
123                 device = next_device;
124         }
125 }
126
127 DEVICE* VM::get_device(int id)
128 {
129         for(DEVICE* device = first_device; device; device = device->next_device) {
130                 if(device->this_device_id == id) {
131                         return device;
132                 }
133         }
134         return NULL;
135 }
136
137 // ----------------------------------------------------------------------------
138 // drive virtual machine
139 // ----------------------------------------------------------------------------
140
141 void VM::reset()
142 {
143         // reset all devices
144         for(DEVICE* device = first_device; device; device = device->next_device) {
145                 device->reset();
146         }
147 }
148
149 void VM::run()
150 {
151         event->drive();
152 }
153
154 // ----------------------------------------------------------------------------
155 // debugger
156 // ----------------------------------------------------------------------------
157
158 #ifdef USE_DEBUGGER
159 DEVICE *VM::get_cpu(int index)
160 {
161         if(index == 0) {
162                 return cpu;
163         }
164         return NULL;
165 }
166 #endif
167
168 // ----------------------------------------------------------------------------
169 // draw screen
170 // ----------------------------------------------------------------------------
171
172 void VM::draw_screen()
173 {
174         vdp->draw_screen();
175 }
176
177 // ----------------------------------------------------------------------------
178 // soud manager
179 // ----------------------------------------------------------------------------
180
181 void VM::initialize_sound(int rate, int samples)
182 {
183         // init sound manager
184         event->initialize_sound(rate, samples);
185         
186         // init sound gen
187         psg->initialize_sound(rate, 1996750, samples, 0, 0);
188 }
189
190 uint16_t* VM::create_sound(int* extra_frames)
191 {
192         return event->create_sound(extra_frames);
193 }
194
195 int VM::get_sound_buffer_ptr()
196 {
197         return event->get_sound_buffer_ptr();
198 }
199
200 #ifdef USE_SOUND_VOLUME
201 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
202 {
203         if(ch == 0) {
204                 psg->set_volume(1, decibel_l, decibel_r);
205         } else if(ch == 1) {
206                 drec->set_volume(0, decibel_l, decibel_r);
207         } else if(ch == 2) {
208                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
209                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
210                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
211         }
212 }
213 #endif
214
215 // ----------------------------------------------------------------------------
216 // user interface
217 // ----------------------------------------------------------------------------
218
219 void VM::play_tape(int drv, const _TCHAR* file_path)
220 {
221         drec->play_tape(file_path);
222 //      drec->set_remote(true);
223 }
224
225 void VM::rec_tape(int drv, const _TCHAR* file_path)
226 {
227         drec->rec_tape(file_path);
228 //      drec->set_remote(true);
229 }
230
231 void VM::close_tape(int drv)
232 {
233         emu->lock_vm();
234         drec->close_tape();
235         emu->unlock_vm();
236 //      drec->set_remote(false);
237 }
238
239 bool VM::is_tape_inserted(int drv)
240 {
241         return drec->is_tape_inserted();
242 }
243
244 bool VM::is_tape_playing(int drv)
245 {
246         return drec->is_tape_playing();
247 }
248
249 bool VM::is_tape_recording(int drv)
250 {
251         return drec->is_tape_recording();
252 }
253
254 int VM::get_tape_position(int drv)
255 {
256         return drec->get_tape_position();
257 }
258
259 const _TCHAR* VM::get_tape_message(int drv)
260 {
261         return drec->get_message();
262 }
263
264 void VM::push_play(int drv)
265 {
266         drec->set_ff_rew(0);
267         drec->set_remote(true);
268 }
269
270 void VM::push_stop(int drv)
271 {
272         drec->set_remote(false);
273 }
274
275 void VM::push_fast_forward(int drv)
276 {
277         drec->set_ff_rew(1);
278         drec->set_remote(true);
279 }
280
281 void VM::push_fast_rewind(int drv)
282 {
283         drec->set_ff_rew(-1);
284         drec->set_remote(true);
285 }
286
287 bool VM::is_frame_skippable()
288 {
289         return event->is_frame_skippable();
290 }
291
292 void VM::update_config()
293 {
294         for(DEVICE* device = first_device; device; device = device->next_device) {
295                 device->update_config();
296         }
297 }
298
299 #define STATE_VERSION   4
300
301 #include "../../statesub.h"
302 #include "../../qt/gui/csp_logger.h"
303 extern CSP_Logger DLL_PREFIX_I *csp_logger;
304
305 void VM::decl_state(void)
306 {
307 #if defined(_PHC25)
308         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PHC_25_HEAD")), csp_logger);
309 #elif defined(_MAP1010)
310         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MAP_1010_HEAD")), csp_logger);
311 #else
312         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PHC_25_SERIES_HEAD")), csp_logger);
313 #endif
314         
315         for(DEVICE* device = first_device; device; device = device->next_device) {
316                 device->decl_state();
317         }
318 }
319
320 void VM::save_state(FILEIO* state_fio)
321 {
322         //state_fio->FputUint32(STATE_VERSION);
323         if(state_entry != NULL) {
324                 state_entry->save_state(state_fio);
325         }
326         for(DEVICE* device = first_device; device; device = device->next_device) {
327                 device->save_state(state_fio);
328         }
329 }
330
331 bool VM::load_state(FILEIO* state_fio)
332 {
333         //if(state_fio->FgetUint32() != STATE_VERSION) {
334         //      return false;
335         //}
336         bool mb = false;
337         if(state_entry != NULL) {
338                 mb = state_entry->load_state(state_fio);
339         }
340         if(!mb) {
341                 emu->out_debug_log("INFO: HEADER DATA ERROR");
342                 return false;
343         }
344
345         for(DEVICE* device = first_device; device; device = device->next_device) {
346                 if(!device->load_state(state_fio)) {
347                         return false;
348                 }
349         }
350         return true;
351 }
352