OSDN Git Service

0987d12b6abbdb83c78de273734d9dc608fab8fb
[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 PHC25_MEMORY(this, emu);
61         system = new PHC25_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 }
114
115 VM::~VM()
116 {
117         // delete all devices
118         for(DEVICE* device = first_device; device;) {
119                 DEVICE *next_device = device->next_device;
120                 device->release();
121                 delete device;
122                 device = next_device;
123         }
124 }
125
126 DEVICE* VM::get_device(int id)
127 {
128         for(DEVICE* device = first_device; device; device = device->next_device) {
129                 if(device->this_device_id == id) {
130                         return device;
131                 }
132         }
133         return NULL;
134 }
135
136 // ----------------------------------------------------------------------------
137 // drive virtual machine
138 // ----------------------------------------------------------------------------
139
140 void VM::reset()
141 {
142         // reset all devices
143         for(DEVICE* device = first_device; device; device = device->next_device) {
144                 device->reset();
145         }
146 }
147
148 void VM::run()
149 {
150         event->drive();
151 }
152
153 // ----------------------------------------------------------------------------
154 // debugger
155 // ----------------------------------------------------------------------------
156
157 #ifdef USE_DEBUGGER
158 DEVICE *VM::get_cpu(int index)
159 {
160         if(index == 0) {
161                 return cpu;
162         }
163         return NULL;
164 }
165 #endif
166
167 // ----------------------------------------------------------------------------
168 // draw screen
169 // ----------------------------------------------------------------------------
170
171 void VM::draw_screen()
172 {
173         vdp->draw_screen();
174 }
175
176 // ----------------------------------------------------------------------------
177 // soud manager
178 // ----------------------------------------------------------------------------
179
180 void VM::initialize_sound(int rate, int samples)
181 {
182         // init sound manager
183         event->initialize_sound(rate, samples);
184         
185         // init sound gen
186         psg->initialize_sound(rate, 1996750, samples, 0, 0);
187 }
188
189 uint16_t* VM::create_sound(int* extra_frames)
190 {
191         return event->create_sound(extra_frames);
192 }
193
194 int VM::get_sound_buffer_ptr()
195 {
196         return event->get_sound_buffer_ptr();
197 }
198
199 #ifdef USE_SOUND_VOLUME
200 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
201 {
202         if(ch == 0) {
203                 psg->set_volume(1, decibel_l, decibel_r);
204         } else if(ch == 1) {
205                 drec->set_volume(0, decibel_l, decibel_r);
206         } else if(ch == 2) {
207                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
208                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
209                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
210         }
211 }
212 #endif
213
214 // ----------------------------------------------------------------------------
215 // user interface
216 // ----------------------------------------------------------------------------
217
218 void VM::play_tape(int drv, const _TCHAR* file_path)
219 {
220         drec->play_tape(file_path);
221 //      drec->set_remote(true);
222 }
223
224 void VM::rec_tape(int drv, const _TCHAR* file_path)
225 {
226         drec->rec_tape(file_path);
227 //      drec->set_remote(true);
228 }
229
230 void VM::close_tape(int drv)
231 {
232         emu->lock_vm();
233         drec->close_tape();
234         emu->unlock_vm();
235 //      drec->set_remote(false);
236 }
237
238 bool VM::is_tape_inserted(int drv)
239 {
240         return drec->is_tape_inserted();
241 }
242
243 bool VM::is_tape_playing(int drv)
244 {
245         return drec->is_tape_playing();
246 }
247
248 bool VM::is_tape_recording(int drv)
249 {
250         return drec->is_tape_recording();
251 }
252
253 int VM::get_tape_position(int drv)
254 {
255         return drec->get_tape_position();
256 }
257
258 const _TCHAR* VM::get_tape_message(int drv)
259 {
260         return drec->get_message();
261 }
262
263 void VM::push_play(int drv)
264 {
265         drec->set_ff_rew(0);
266         drec->set_remote(true);
267 }
268
269 void VM::push_stop(int drv)
270 {
271         drec->set_remote(false);
272 }
273
274 void VM::push_fast_forward(int drv)
275 {
276         drec->set_ff_rew(1);
277         drec->set_remote(true);
278 }
279
280 void VM::push_fast_rewind(int drv)
281 {
282         drec->set_ff_rew(-1);
283         drec->set_remote(true);
284 }
285
286 bool VM::is_frame_skippable()
287 {
288         return event->is_frame_skippable();
289 }
290
291 void VM::update_config()
292 {
293         for(DEVICE* device = first_device; device; device = device->next_device) {
294                 device->update_config();
295         }
296 }
297
298 #define STATE_VERSION   4
299
300 bool VM::process_state(FILEIO* state_fio, bool loading)
301 {
302         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
303                 return false;
304         }
305         for(DEVICE* device = first_device; device; device = device->next_device) {
306                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
307                 // const char *name = typeid(*device).name();
308                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
309                 const char *name = device->get_device_name();
310                 int len = strlen(name);
311                 
312                 if(!state_fio->StateCheckInt32(len)) {
313                         if(loading) {
314                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
315                         }
316                         return false;
317                 }
318                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
319                         if(loading) {
320                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
321                         }
322                         return false;
323                 }
324                 if(!device->process_state(state_fio, loading)) {
325                         if(loading) {
326                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
327                         }
328                         return false;
329                 }
330         }
331         // Machine specified.
332         return true;
333 }