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 / pyuta / pyuta.cpp
1 /*
2         TOMY PyuTa Emulator 'ePyuTa'
3
4         Author : Takeda.Toshiya
5         Date   : 2007.07.15 -
6
7         [ virtual machine ]
8 */
9
10 #include "pyuta.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../noise.h"
17 #include "../sn76489an.h"
18 #include "../tms9918a.h"
19 #include "../tms9995.h"
20
21 #ifdef USE_DEBUGGER
22 #include "../debugger.h"
23 #endif
24
25 #include "./memory.h"
26
27 using PYUTA::MEMORY;
28 // ----------------------------------------------------------------------------
29 // initialize
30 // ----------------------------------------------------------------------------
31
32 VM::VM(EMU_TEMPLATE* parent_emu) : VM_TEMPLATE(parent_emu)
33 {
34         // create devices
35         first_device = last_device = NULL;
36         dummy = new DEVICE(this, emu);  // must be 1st device
37         event = new EVENT(this, emu);   // must be 2nd device
38         dummy->set_device_name(_T("1st Dummy"));
39         
40         drec = new DATAREC(this, emu);
41         drec->set_context_noise_play(new NOISE(this, emu));
42         drec->set_context_noise_stop(new NOISE(this, emu));
43         drec->set_context_noise_fast(new NOISE(this, emu));
44         psg = new SN76489AN(this, emu);
45         vdp = new TMS9918A(this, emu);
46 #ifdef USE_DEBUGGER
47 //      psg->set_context_debugger(new DEBUGGER(this, emu));
48         vdp->set_context_debugger(new DEBUGGER(this, emu));
49 #endif
50         cpu = new TMS9995(this, emu);
51         
52         memory = new MEMORY(this, emu);
53         
54         // set contexts
55         event->set_context_cpu(cpu);
56         event->set_context_sound(psg);
57         event->set_context_sound(drec);
58         event->set_context_sound(drec->get_context_noise_play());
59         event->set_context_sound(drec->get_context_noise_stop());
60         event->set_context_sound(drec->get_context_noise_fast());
61         
62         drec->set_context_ear(memory, 0, 1);
63         memory->set_context_cmt(drec);
64         memory->set_context_cpu(cpu);
65         memory->set_context_psg(psg);
66         memory->set_context_vdp(vdp);
67         
68         // cpu bus
69         cpu->set_context_mem(memory);
70         cpu->set_context_io(memory);
71 #ifdef USE_DEBUGGER
72         cpu->set_context_debugger(new DEBUGGER(this, emu));
73 #endif
74         
75         // initialize all devices
76 #if defined(__GIT_REPO_VERSION)
77         set_git_repo_version(__GIT_REPO_VERSION);
78 #endif
79         initialize_devices();
80 }
81
82 VM::~VM()
83 {
84         // delete all devices
85         for(DEVICE* device = first_device; device;) {
86                 DEVICE *next_device = device->next_device;
87                 device->release();
88                 delete device;
89                 device = next_device;
90         }
91 }
92
93 DEVICE* VM::get_device(int id)
94 {
95         for(DEVICE* device = first_device; device; device = device->next_device) {
96                 if(device->this_device_id == id) {
97                         return device;
98                 }
99         }
100         return NULL;
101 }
102
103 // ----------------------------------------------------------------------------
104 // drive virtual machine
105 // ----------------------------------------------------------------------------
106
107 void VM::reset()
108 {
109         // reset all devices
110         for(DEVICE* device = first_device; device; device = device->next_device) {
111                 device->reset();
112         }
113 }
114
115 void VM::run()
116 {
117         event->drive();
118 }
119
120 // ----------------------------------------------------------------------------
121 // debugger
122 // ----------------------------------------------------------------------------
123
124 #ifdef USE_DEBUGGER
125 DEVICE *VM::get_cpu(int index)
126 {
127         if(index == 0) {
128                 return cpu;
129         }
130         return NULL;
131 }
132 #endif
133
134 // ----------------------------------------------------------------------------
135 // draw screen
136 // ----------------------------------------------------------------------------
137
138 void VM::draw_screen()
139 {
140         vdp->draw_screen();
141 }
142
143 // ----------------------------------------------------------------------------
144 // soud manager
145 // ----------------------------------------------------------------------------
146
147 void VM::initialize_sound(int rate, int samples)
148 {
149         // init sound manager
150         event->initialize_sound(rate, samples);
151         
152         // init sound gen
153         psg->initialize_sound(rate, 3579545, 8000);
154 }
155
156 uint16_t* VM::create_sound(int* extra_frames)
157 {
158         return event->create_sound(extra_frames);
159 }
160
161 int VM::get_sound_buffer_ptr()
162 {
163         return event->get_sound_buffer_ptr();
164 }
165
166 #ifdef USE_SOUND_VOLUME
167 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
168 {
169         if(ch == 0) {
170                 psg->set_volume(0, decibel_l, decibel_r);
171         } else if(ch == 1) {
172                 drec->set_volume(0, decibel_l, decibel_r);
173         } else if(ch == 2) {
174                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
175                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
176                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
177         }
178 }
179 #endif
180
181 // ----------------------------------------------------------------------------
182 // user interface
183 // ----------------------------------------------------------------------------
184
185 void VM::open_cart(int drv, const _TCHAR* file_path)
186 {
187         if(drv == 0) {
188                 memory->open_cart(file_path);
189                 reset();
190         }
191 }
192
193 void VM::close_cart(int drv)
194 {
195         if(drv == 0) {
196                 memory->close_cart();
197                 reset();
198         }
199 }
200
201 bool VM::is_cart_inserted(int drv)
202 {
203         if(drv == 0) {
204                 return memory->is_cart_inserted();
205         } else {
206                 return false;
207         }
208 }
209
210 void VM::play_tape(int drv, const _TCHAR* file_path)
211 {
212         bool remote = drec->get_remote();
213         
214         if(drec->play_tape(file_path) && remote) {
215                 // if machine already sets remote on, start playing now
216                 push_play(drv);
217         }
218 }
219
220 void VM::rec_tape(int drv, const _TCHAR* file_path)
221 {
222         bool remote = drec->get_remote();
223         
224         if(drec->rec_tape(file_path) && remote) {
225                 // if machine already sets remote on, start recording now
226                 push_play(drv);
227         }
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_remote(false);
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_remote(false);
278         drec->set_ff_rew(1);
279         drec->set_remote(true);
280 }
281
282 void VM::push_fast_rewind(int drv)
283 {
284         drec->set_remote(false);
285         drec->set_ff_rew(-1);
286         drec->set_remote(true);
287 }
288
289 bool VM::is_frame_skippable()
290 {
291         return event->is_frame_skippable();
292 }
293
294 void VM::update_config()
295 {
296         for(DEVICE* device = first_device; device; device = device->next_device) {
297                 device->update_config();
298         }
299 }
300
301 double VM::get_current_usec()
302 {
303         if(event == NULL) return 0.0;
304         return event->get_current_usec();
305 }
306
307 uint64_t VM::get_current_clock_uint64()
308 {
309                 if(event == NULL) return (uint64_t)0;
310                 return event->get_current_clock_uint64();
311 }
312
313 #define STATE_VERSION   5
314
315 bool VM::process_state(FILEIO* state_fio, bool loading)
316 {
317         if(!(VM_TEMPLATE::process_state_core(state_fio, loading, STATE_VERSION))) {
318                 return false;
319         }
320         // Machine specified.
321         return true;
322 }