OSDN Git Service

[General][WIP] Merge upstream 2017-03-20.Still not implement UIs.
[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 // ----------------------------------------------------------------------------
28 // initialize
29 // ----------------------------------------------------------------------------
30
31 VM::VM(EMU* parent_emu) : emu(parent_emu)
32 {
33         // create devices
34         first_device = last_device = NULL;
35         dummy = new DEVICE(this, emu);  // must be 1st device
36         event = new EVENT(this, emu);   // must be 2nd device
37         dummy->set_device_name(_T("1st Dummy"));
38         
39         drec = new DATAREC(this, emu);
40         drec->set_context_noise_play(new NOISE(this, emu));
41         drec->set_context_noise_stop(new NOISE(this, emu));
42         drec->set_context_noise_fast(new NOISE(this, emu));
43         psg = new SN76489AN(this, emu);
44         vdp = new TMS9918A(this, emu);
45         cpu = new TMS9995(this, emu);
46         
47         memory = new MEMORY(this, emu);
48         
49         // set contexts
50         event->set_context_cpu(cpu);
51         event->set_context_sound(psg);
52         event->set_context_sound(drec);
53         event->set_context_sound(drec->get_context_noise_play());
54         event->set_context_sound(drec->get_context_noise_stop());
55         event->set_context_sound(drec->get_context_noise_fast());
56         
57         drec->set_context_ear(memory, 0, 1);
58         memory->set_context_cmt(drec);
59         memory->set_context_cpu(cpu);
60         memory->set_context_psg(psg);
61         memory->set_context_vdp(vdp);
62         
63         // cpu bus
64         cpu->set_context_mem(memory);
65         cpu->set_context_io(memory);
66 #ifdef USE_DEBUGGER
67         cpu->set_context_debugger(new DEBUGGER(this, emu));
68 #endif
69         
70         // initialize all devices
71         for(DEVICE* device = first_device; device; device = device->next_device) {
72                 device->initialize();
73         }
74 }
75
76 VM::~VM()
77 {
78         // delete all devices
79         for(DEVICE* device = first_device; device;) {
80                 DEVICE *next_device = device->next_device;
81                 device->release();
82                 delete device;
83                 device = next_device;
84         }
85 }
86
87 DEVICE* VM::get_device(int id)
88 {
89         for(DEVICE* device = first_device; device; device = device->next_device) {
90                 if(device->this_device_id == id) {
91                         return device;
92                 }
93         }
94         return NULL;
95 }
96
97 // ----------------------------------------------------------------------------
98 // drive virtual machine
99 // ----------------------------------------------------------------------------
100
101 void VM::reset()
102 {
103         // reset all devices
104         for(DEVICE* device = first_device; device; device = device->next_device) {
105                 device->reset();
106         }
107 }
108
109 void VM::run()
110 {
111         event->drive();
112 }
113
114 // ----------------------------------------------------------------------------
115 // debugger
116 // ----------------------------------------------------------------------------
117
118 #ifdef USE_DEBUGGER
119 DEVICE *VM::get_cpu(int index)
120 {
121         if(index == 0) {
122                 return cpu;
123         }
124         return NULL;
125 }
126 #endif
127
128 // ----------------------------------------------------------------------------
129 // draw screen
130 // ----------------------------------------------------------------------------
131
132 void VM::draw_screen()
133 {
134         vdp->draw_screen();
135 }
136
137 // ----------------------------------------------------------------------------
138 // soud manager
139 // ----------------------------------------------------------------------------
140
141 void VM::initialize_sound(int rate, int samples)
142 {
143         // init sound manager
144         event->initialize_sound(rate, samples);
145         
146         // init sound gen
147         psg->initialize_sound(rate, 3579545, 8000);
148 }
149
150 uint16_t* VM::create_sound(int* extra_frames)
151 {
152         return event->create_sound(extra_frames);
153 }
154
155 int VM::get_sound_buffer_ptr()
156 {
157         return event->get_sound_buffer_ptr();
158 }
159
160 #ifdef USE_SOUND_VOLUME
161 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
162 {
163         if(ch == 0) {
164                 psg->set_volume(0, decibel_l, decibel_r);
165         } else if(ch == 1) {
166                 drec->set_volume(0, decibel_l, decibel_r);
167         } else if(ch == 2) {
168                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
169                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
170                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
171         }
172 }
173 #endif
174
175 // ----------------------------------------------------------------------------
176 // user interface
177 // ----------------------------------------------------------------------------
178
179 void VM::open_cart(int drv, const _TCHAR* file_path)
180 {
181         if(drv == 0) {
182                 memory->open_cart(file_path);
183                 reset();
184         }
185 }
186
187 void VM::close_cart(int drv)
188 {
189         if(drv == 0) {
190                 memory->close_cart();
191                 reset();
192         }
193 }
194
195 bool VM::is_cart_inserted(int drv)
196 {
197         if(drv == 0) {
198                 return memory->is_cart_inserted();
199         } else {
200                 return false;
201         }
202 }
203
204 void VM::play_tape(int drv, const _TCHAR* file_path)
205 {
206         drec->play_tape(file_path);
207 }
208
209 void VM::rec_tape(int drv, const _TCHAR* file_path)
210 {
211         drec->rec_tape(file_path);
212 }
213
214 void VM::close_tape(int drv)
215 {
216         emu->lock_vm();
217         drec->close_tape();
218         emu->unlock_vm();
219 }
220
221 bool VM::is_tape_inserted(int drv)
222 {
223         return drec->is_tape_inserted();
224 }
225
226 bool VM::is_tape_playing(int drv)
227 {
228         return drec->is_tape_playing();
229 }
230
231 bool VM::is_tape_recording(int drv)
232 {
233         return drec->is_tape_recording();
234 }
235
236 int VM::get_tape_position(int drv)
237 {
238         return drec->get_tape_position();
239 }
240
241 const _TCHAR* VM::get_tape_message(int drv)
242 {
243         return drec->get_message();
244 }
245
246 bool VM::is_frame_skippable()
247 {
248         return event->is_frame_skippable();
249 }
250
251 void VM::update_config()
252 {
253         for(DEVICE* device = first_device; device; device = device->next_device) {
254                 device->update_config();
255         }
256 }
257
258 #define STATE_VERSION   3
259
260 void VM::save_state(FILEIO* state_fio)
261 {
262         state_fio->FputUint32(STATE_VERSION);
263         
264         for(DEVICE* device = first_device; device; device = device->next_device) {
265                 device->save_state(state_fio);
266         }
267 }
268
269 bool VM::load_state(FILEIO* state_fio)
270 {
271         if(state_fio->FgetUint32() != STATE_VERSION) {
272                 return false;
273         }
274         for(DEVICE* device = first_device; device; device = device->next_device) {
275                 if(!device->load_state(state_fio)) {
276                         return false;
277                 }
278         }
279         return true;
280 }
281