OSDN Git Service

[VM][STATE] Use namespace {VMNAME} to separate per VMs.
[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 using PHC25::JOYSTICK;
35 using PHC25::KEYBOARD;
36 using PHC25::MEMORY;
37 using PHC25::SYSTEM;
38
39 // ----------------------------------------------------------------------------
40 // initialize
41 // ----------------------------------------------------------------------------
42
43 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
44 {
45         // create devices
46         first_device = last_device = NULL;
47         dummy = new DEVICE(this, emu);  // must be 1st device
48         event = new EVENT(this, emu);   // must be 2nd device
49         dummy->set_device_name(_T("1st Dummy"));
50         
51         drec = new DATAREC(this, emu);
52         drec->set_context_noise_play(new NOISE(this, emu));
53         drec->set_context_noise_stop(new NOISE(this, emu));
54         drec->set_context_noise_fast(new NOISE(this, emu));
55         io = new IO(this, emu);
56         vdp = new MC6847(this, emu);
57         not_vsync = new NOT(this, emu);
58 //      psg = new YM2203(this, emu);
59         psg = new AY_3_891X(this, emu);
60         cpu = new Z80(this, emu);
61         not_vsync->set_device_name(_T("NOT GATE(VSYNC)"));
62         
63         joystick = new JOYSTICK(this, emu);
64         keyboard = new KEYBOARD(this, emu);
65         memory = new MEMORY(this, emu);
66         system = new SYSTEM(this, emu);
67         // set contexts
68         event->set_context_cpu(cpu);
69         event->set_context_sound(psg);
70         event->set_context_sound(drec);
71         event->set_context_sound(drec->get_context_noise_play());
72         event->set_context_sound(drec->get_context_noise_stop());
73         event->set_context_sound(drec->get_context_noise_fast());
74         
75         vdp->load_font_image(create_local_path(_T("FONT.ROM")));
76         vdp->set_vram_ptr(memory->get_vram(), 0x1800);
77 //      vdp->set_context_cpu(cpu);
78         vdp->set_context_vsync(not_vsync, SIG_NOT_INPUT, 1);
79         not_vsync->set_context_out(cpu, SIG_CPU_IRQ, 1);
80         
81         vdp->set_context_vsync(system, SIG_SYSTEM_PORT, 0x10);
82         drec->set_context_ear(system, SIG_SYSTEM_PORT, 0x20);
83         // bit6: printer busy
84         vdp->set_context_hsync(system, SIG_SYSTEM_PORT, 0x80);
85         
86         joystick->set_context_psg(psg);
87 #ifdef _MAP1010
88         memory->set_context_keyboard(keyboard);
89 #endif
90         system->set_context_drec(drec);
91         system->set_context_vdp(vdp);
92         
93         // cpu bus
94         cpu->set_context_mem(memory);
95         cpu->set_context_io(io);
96         cpu->set_context_intr(dummy);
97 #ifdef USE_DEBUGGER
98         cpu->set_context_debugger(new DEBUGGER(this, emu));
99 #endif
100         
101         // i/o bus
102         io->set_iomap_single_rw(0x40, system);
103 #ifndef _MAP1010
104         io->set_iomap_range_r(0x80, 0x88, keyboard);
105 #endif
106         io->set_iomap_alias_w(0xc0, psg, 1);    // PSG data
107         io->set_iomap_alias_w(0xc1, psg, 0);    // PSG ch
108 //      io->set_iomap_alias_r(0xc0, psg, 1);
109         io->set_iomap_alias_r(0xc1, psg, 1);    // PSG data
110         
111         // initialize all devices
112 #if defined(__GIT_REPO_VERSION)
113         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
114 #endif
115         for(DEVICE* device = first_device; device; device = device->next_device) {
116                 device->initialize();
117         }
118 }
119
120 VM::~VM()
121 {
122         // delete all devices
123         for(DEVICE* device = first_device; device;) {
124                 DEVICE *next_device = device->next_device;
125                 device->release();
126                 delete device;
127                 device = next_device;
128         }
129 }
130
131 DEVICE* VM::get_device(int id)
132 {
133         for(DEVICE* device = first_device; device; device = device->next_device) {
134                 if(device->this_device_id == id) {
135                         return device;
136                 }
137         }
138         return NULL;
139 }
140
141 // ----------------------------------------------------------------------------
142 // drive virtual machine
143 // ----------------------------------------------------------------------------
144
145 void VM::reset()
146 {
147         // reset all devices
148         for(DEVICE* device = first_device; device; device = device->next_device) {
149                 device->reset();
150         }
151 }
152
153 void VM::run()
154 {
155         event->drive();
156 }
157
158 // ----------------------------------------------------------------------------
159 // debugger
160 // ----------------------------------------------------------------------------
161
162 #ifdef USE_DEBUGGER
163 DEVICE *VM::get_cpu(int index)
164 {
165         if(index == 0) {
166                 return cpu;
167         }
168         return NULL;
169 }
170 #endif
171
172 // ----------------------------------------------------------------------------
173 // draw screen
174 // ----------------------------------------------------------------------------
175
176 void VM::draw_screen()
177 {
178         vdp->draw_screen();
179 }
180
181 // ----------------------------------------------------------------------------
182 // soud manager
183 // ----------------------------------------------------------------------------
184
185 void VM::initialize_sound(int rate, int samples)
186 {
187         // init sound manager
188         event->initialize_sound(rate, samples);
189         
190         // init sound gen
191         psg->initialize_sound(rate, 1996750, samples, 0, 0);
192 }
193
194 uint16_t* VM::create_sound(int* extra_frames)
195 {
196         return event->create_sound(extra_frames);
197 }
198
199 int VM::get_sound_buffer_ptr()
200 {
201         return event->get_sound_buffer_ptr();
202 }
203
204 #ifdef USE_SOUND_VOLUME
205 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
206 {
207         if(ch == 0) {
208                 psg->set_volume(1, decibel_l, decibel_r);
209         } else if(ch == 1) {
210                 drec->set_volume(0, decibel_l, decibel_r);
211         } else if(ch == 2) {
212                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
213                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
214                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
215         }
216 }
217 #endif
218
219 // ----------------------------------------------------------------------------
220 // user interface
221 // ----------------------------------------------------------------------------
222
223 void VM::play_tape(int drv, const _TCHAR* file_path)
224 {
225         drec->play_tape(file_path);
226 //      drec->set_remote(true);
227 }
228
229 void VM::rec_tape(int drv, const _TCHAR* file_path)
230 {
231         drec->rec_tape(file_path);
232 //      drec->set_remote(true);
233 }
234
235 void VM::close_tape(int drv)
236 {
237         emu->lock_vm();
238         drec->close_tape();
239         emu->unlock_vm();
240 //      drec->set_remote(false);
241 }
242
243 bool VM::is_tape_inserted(int drv)
244 {
245         return drec->is_tape_inserted();
246 }
247
248 bool VM::is_tape_playing(int drv)
249 {
250         return drec->is_tape_playing();
251 }
252
253 bool VM::is_tape_recording(int drv)
254 {
255         return drec->is_tape_recording();
256 }
257
258 int VM::get_tape_position(int drv)
259 {
260         return drec->get_tape_position();
261 }
262
263 const _TCHAR* VM::get_tape_message(int drv)
264 {
265         return drec->get_message();
266 }
267
268 void VM::push_play(int drv)
269 {
270         drec->set_ff_rew(0);
271         drec->set_remote(true);
272 }
273
274 void VM::push_stop(int drv)
275 {
276         drec->set_remote(false);
277 }
278
279 void VM::push_fast_forward(int drv)
280 {
281         drec->set_ff_rew(1);
282         drec->set_remote(true);
283 }
284
285 void VM::push_fast_rewind(int drv)
286 {
287         drec->set_ff_rew(-1);
288         drec->set_remote(true);
289 }
290
291 bool VM::is_frame_skippable()
292 {
293         return event->is_frame_skippable();
294 }
295
296 void VM::update_config()
297 {
298         for(DEVICE* device = first_device; device; device = device->next_device) {
299                 device->update_config();
300         }
301 }
302
303 #define STATE_VERSION   4
304
305 bool VM::process_state(FILEIO* state_fio, bool loading)
306 {
307         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
308                 return false;
309         }
310         for(DEVICE* device = first_device; device; device = device->next_device) {
311                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
312                 // const char *name = typeid(*device).name();
313                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
314                 const char *name = device->get_device_name();
315                 int len = strlen(name);
316                 
317                 if(!state_fio->StateCheckInt32(len)) {
318                         if(loading) {
319                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
320                         }
321                         return false;
322                 }
323                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
324                         if(loading) {
325                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
326                         }
327                         return false;
328                 }
329                 if(!device->process_state(state_fio, loading)) {
330                         if(loading) {
331                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
332                         }
333                         return false;
334                 }
335         }
336         // Machine specified.
337         return true;
338 }