OSDN Git Service

[VM][FMTOWNS][MEMORY] Fix setup around memory banks by I/O 0404h and 0480h.
[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_TEMPLATE* 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         io->space = 0x100;
57         vdp = new MC6847(this, emu);
58         not_vsync = new NOT(this, emu);
59         psg = new AY_3_891X(this, emu);
60 #ifdef USE_DEBUGGER
61         psg->set_context_debugger(new DEBUGGER(this, emu));
62 #endif
63         cpu = new Z80(this, emu);
64         not_vsync->set_device_name(_T("NOT GATE(VSYNC)"));
65
66         joystick = new JOYSTICK(this, emu);
67         keyboard = new KEYBOARD(this, emu);
68         memory = new MEMORY(this, emu);
69         system = new SYSTEM(this, emu);
70         // set contexts
71         event->set_context_cpu(cpu);
72         event->set_context_sound(psg);
73         event->set_context_sound(drec);
74         event->set_context_sound(drec->get_context_noise_play());
75         event->set_context_sound(drec->get_context_noise_stop());
76         event->set_context_sound(drec->get_context_noise_fast());
77
78         vdp->load_font_image(create_local_path(_T("FONT.ROM")));
79         vdp->set_vram_ptr(memory->get_vram(), 0x1800);
80 //      vdp->set_context_cpu(cpu);
81         vdp->set_context_vsync(not_vsync, SIG_NOT_INPUT, 1);
82         not_vsync->set_context_out(cpu, SIG_CPU_IRQ, 1);
83
84         vdp->set_context_vsync(system, SIG_SYSTEM_PORT, 0x10);
85         drec->set_context_ear(system, SIG_SYSTEM_PORT, 0x20);
86         // bit6: printer busy
87         vdp->set_context_hsync(system, SIG_SYSTEM_PORT, 0x80);
88
89         joystick->set_context_psg(psg);
90 #ifdef _MAP1010
91         memory->set_context_keyboard(keyboard);
92 #endif
93         system->set_context_drec(drec);
94         system->set_context_vdp(vdp);
95
96         // cpu bus
97         cpu->set_context_mem(memory);
98         cpu->set_context_io(io);
99         cpu->set_context_intr(dummy);
100 #ifdef USE_DEBUGGER
101         cpu->set_context_debugger(new DEBUGGER(this, emu));
102 #endif
103
104         // i/o bus
105         io->set_iomap_single_rw(0x40, system);
106 #ifndef _MAP1010
107         io->set_iomap_range_r(0x80, 0x88, keyboard);
108 #endif
109         io->set_iomap_alias_w(0xc0, psg, 1);    // PSG data
110         io->set_iomap_alias_w(0xc1, psg, 0);    // PSG ch
111 //      io->set_iomap_alias_r(0xc0, psg, 1);
112         io->set_iomap_alias_r(0xc1, psg, 1);    // PSG data
113
114         // initialize all devices
115 #if defined(__GIT_REPO_VERSION)
116         set_git_repo_version(__GIT_REPO_VERSION);
117 #endif
118         initialize_devices();
119 }
120
121 VM::~VM()
122 {
123         // delete all devices
124         for(DEVICE* device = first_device; device;) {
125                 DEVICE *next_device = device->next_device;
126                 device->release();
127                 delete device;
128                 device = next_device;
129         }
130 }
131
132 DEVICE* VM::get_device(int id)
133 {
134         for(DEVICE* device = first_device; device; device = device->next_device) {
135                 if(device->this_device_id == id) {
136                         return device;
137                 }
138         }
139         return NULL;
140 }
141
142 // ----------------------------------------------------------------------------
143 // drive virtual machine
144 // ----------------------------------------------------------------------------
145
146 void VM::reset()
147 {
148         // reset all devices
149         for(DEVICE* device = first_device; device; device = device->next_device) {
150                 device->reset();
151         }
152 }
153
154 void VM::run()
155 {
156         event->drive();
157 }
158
159 // ----------------------------------------------------------------------------
160 // debugger
161 // ----------------------------------------------------------------------------
162
163 #ifdef USE_DEBUGGER
164 DEVICE *VM::get_cpu(int index)
165 {
166         if(index == 0) {
167                 return cpu;
168         }
169         return NULL;
170 }
171 #endif
172
173 // ----------------------------------------------------------------------------
174 // draw screen
175 // ----------------------------------------------------------------------------
176
177 void VM::draw_screen()
178 {
179         vdp->draw_screen();
180 }
181
182 // ----------------------------------------------------------------------------
183 // soud manager
184 // ----------------------------------------------------------------------------
185
186 void VM::initialize_sound(int rate, int samples)
187 {
188         // init sound manager
189         event->initialize_sound(rate, samples);
190
191         // init sound gen
192         psg->initialize_sound(rate, 1996750, samples, 0, 0);
193 }
194
195 uint16_t* VM::create_sound(int* extra_frames)
196 {
197         return event->create_sound(extra_frames);
198 }
199
200 int VM::get_sound_buffer_ptr()
201 {
202         return event->get_sound_buffer_ptr();
203 }
204
205 #ifdef USE_SOUND_VOLUME
206 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
207 {
208         if(ch == 0) {
209                 psg->set_volume(1, decibel_l, decibel_r);
210         } else if(ch == 1) {
211                 drec->set_volume(0, decibel_l, decibel_r);
212         } else if(ch == 2) {
213                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
214                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
215                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
216         }
217 }
218 #endif
219
220 // ----------------------------------------------------------------------------
221 // user interface
222 // ----------------------------------------------------------------------------
223
224 void VM::play_tape(int drv, const _TCHAR* file_path)
225 {
226         bool remote = drec->get_remote();
227
228         if(drec->play_tape(file_path) && remote) {
229                 // if machine already sets remote on, start playing now
230                 push_play(drv);
231         }
232 }
233
234 void VM::rec_tape(int drv, const _TCHAR* file_path)
235 {
236         bool remote = drec->get_remote();
237
238         if(drec->rec_tape(file_path) && remote) {
239                 // if machine already sets remote on, start recording now
240                 push_play(drv);
241         }
242 }
243
244 void VM::close_tape(int drv)
245 {
246         emu->lock_vm();
247         drec->close_tape();
248         emu->unlock_vm();
249         drec->set_remote(false);
250 }
251
252 bool VM::is_tape_inserted(int drv)
253 {
254         return drec->is_tape_inserted();
255 }
256
257 bool VM::is_tape_playing(int drv)
258 {
259         return drec->is_tape_playing();
260 }
261
262 bool VM::is_tape_recording(int drv)
263 {
264         return drec->is_tape_recording();
265 }
266
267 int VM::get_tape_position(int drv)
268 {
269         return drec->get_tape_position();
270 }
271
272 const _TCHAR* VM::get_tape_message(int drv)
273 {
274         return drec->get_message();
275 }
276
277 void VM::push_play(int drv)
278 {
279         drec->set_remote(false);
280         drec->set_ff_rew(0);
281         drec->set_remote(true);
282 }
283
284 void VM::push_stop(int drv)
285 {
286         drec->set_remote(false);
287 }
288
289 void VM::push_fast_forward(int drv)
290 {
291         drec->set_remote(false);
292         drec->set_ff_rew(1);
293         drec->set_remote(true);
294 }
295
296 void VM::push_fast_rewind(int drv)
297 {
298         drec->set_remote(false);
299         drec->set_ff_rew(-1);
300         drec->set_remote(true);
301 }
302
303 bool VM::is_frame_skippable()
304 {
305         return event->is_frame_skippable();
306 }
307
308 void VM::update_config()
309 {
310         for(DEVICE* device = first_device; device; device = device->next_device) {
311                 device->update_config();
312         }
313 }
314
315 double VM::get_current_usec()
316 {
317         if(event == NULL) return 0.0;
318         return event->get_current_usec();
319 }
320
321 uint64_t VM::get_current_clock_uint64()
322 {
323                 if(event == NULL) return (uint64_t)0;
324                 return event->get_current_clock_uint64();
325 }
326
327 #define STATE_VERSION   5
328
329 bool VM::process_state(FILEIO* state_fio, bool loading)
330 {
331         if(!(VM_TEMPLATE::process_state_core(state_fio, loading, STATE_VERSION))) {
332                 return false;
333         }
334         // Machine specified.
335         return true;
336 }