OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[csp-qt/common_source_project-fm7.git] / source / src / vm / rx78 / rx78.cpp
1 /*
2         BANDAI RX-78 Emulator 'eRX-78'
3
4         Author : Takeda.Toshiya
5         Date   : 2006.08.21 -
6
7         [ virtual machine ]
8 */
9
10 #include "rx78.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../io.h"
17 #include "../noise.h"
18 #include "../sn76489an.h"
19 #include "../z80.h"
20
21 #ifdef USE_DEBUGGER
22 #include "../debugger.h"
23 #endif
24
25 #include "cmt.h"
26 #include "keyboard.h"
27 #include "memory.h"
28 #include "printer.h"
29 #include "vdp.h"
30
31 // ----------------------------------------------------------------------------
32 // initialize
33 // ----------------------------------------------------------------------------
34
35 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
36 {
37         // create devices
38         first_device = last_device = NULL;
39         dummy = new DEVICE(this, emu);  // must be 1st device
40         event = new EVENT(this, emu);   // must be 2nd device
41         dummy->set_device_name(_T("1st Dummy"));
42         
43         drec = new DATAREC(this, emu);
44         drec->set_context_noise_play(new NOISE(this, emu));
45         drec->set_context_noise_stop(new NOISE(this, emu));
46         drec->set_context_noise_fast(new NOISE(this, emu));
47         io = new IO(this, emu);
48         psg = new SN76489AN(this, emu);
49         cpu = new Z80(this, emu);
50         
51         cmt = new CMT(this, emu);
52         key = new KEYBOARD(this, emu);
53         memory = new MEMORY(this, emu);
54         prt = new PRINTER(this, emu);
55         vdp = new VDP(this, emu);
56         
57         // set contexts
58         event->set_context_cpu(cpu);
59         event->set_context_sound(psg);
60         event->set_context_sound(drec);
61         event->set_context_sound(drec->get_context_noise_play());
62         event->set_context_sound(drec->get_context_noise_stop());
63         event->set_context_sound(drec->get_context_noise_fast());
64         
65         drec->set_context_ear(cmt, SIG_CMT_IN, 1);
66         cmt->set_context_drec(drec);
67         vdp->set_context_cpu(cpu);
68         vdp->set_vram_ptr(memory->get_vram());
69         
70         // cpu bus
71         cpu->set_context_mem(memory);
72         cpu->set_context_io(io);
73         cpu->set_context_intr(dummy);
74 #ifdef USE_DEBUGGER
75         cpu->set_context_debugger(new DEBUGGER(this, emu));
76 #endif
77         
78         // i/o bus
79         io->set_iomap_range_rw(0xe2, 0xe3, prt);
80         io->set_iomap_single_rw(0xf0, cmt);
81         io->set_iomap_range_w(0xf1, 0xf2, memory);
82         io->set_iomap_single_rw(0xf4, key);
83         io->set_iomap_range_w(0xf5, 0xfc, vdp);
84         io->set_iomap_single_w(0xfe, vdp);
85         io->set_iomap_single_w(0xff, psg);
86         
87         // initialize all devices
88 #if defined(__GIT_REPO_VERSION)
89         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
90 #endif
91         for(DEVICE* device = first_device; device; device = device->next_device) {
92                 device->initialize();
93         }
94         decl_state();
95 }
96
97 VM::~VM()
98 {
99         // delete all devices
100         for(DEVICE* device = first_device; device;) {
101                 DEVICE *next_device = device->next_device;
102                 device->release();
103                 delete device;
104                 device = next_device;
105         }
106 }
107
108 DEVICE* VM::get_device(int id)
109 {
110         for(DEVICE* device = first_device; device; device = device->next_device) {
111                 if(device->this_device_id == id) {
112                         return device;
113                 }
114         }
115         return NULL;
116 }
117
118 // ----------------------------------------------------------------------------
119 // debugger
120 // ----------------------------------------------------------------------------
121
122 #ifdef USE_DEBUGGER
123 DEVICE *VM::get_cpu(int index)
124 {
125         if(index == 0) {
126                 return cpu;
127         }
128         return NULL;
129 }
130 #endif
131
132 // ----------------------------------------------------------------------------
133 // drive virtual machine
134 // ----------------------------------------------------------------------------
135
136 void VM::reset()
137 {
138         // reset all devices
139         for(DEVICE* device = first_device; device; device = device->next_device) {
140                 device->reset();
141         }
142 }
143
144 void VM::run()
145 {
146         event->drive();
147 }
148
149 // ----------------------------------------------------------------------------
150 // draw screen
151 // ----------------------------------------------------------------------------
152
153 void VM::draw_screen()
154 {
155         vdp->draw_screen();
156 }
157
158 // ----------------------------------------------------------------------------
159 // soud manager
160 // ----------------------------------------------------------------------------
161
162 void VM::initialize_sound(int rate, int samples)
163 {
164         // init sound manager
165         event->initialize_sound(rate, samples);
166         
167         // init sound gen
168         psg->initialize_sound(rate, 3579545, 8000);
169 }
170
171 uint16_t* VM::create_sound(int* extra_frames)
172 {
173         return event->create_sound(extra_frames);
174 }
175
176 int VM::get_sound_buffer_ptr()
177 {
178         return event->get_sound_buffer_ptr();
179 }
180
181 #ifdef USE_SOUND_VOLUME
182 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
183 {
184         if(ch == 0) {
185                 psg->set_volume(0, decibel_l, decibel_r);
186         } else if(ch == 1) {
187                 drec->set_volume(0, decibel_l, decibel_r);
188         } else if(ch == 2) {
189                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
190                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
191                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
192         }
193 }
194 #endif
195
196 // ----------------------------------------------------------------------------
197 // user interface
198 // ----------------------------------------------------------------------------
199
200 void VM::open_cart(int drv, const _TCHAR* file_path)
201 {
202         if(drv == 0) {
203                 memory->open_cart(file_path);
204                 reset();
205         }
206 }
207
208 void VM::close_cart(int drv)
209 {
210         if(drv == 0) {
211                 memory->close_cart();
212                 reset();
213         }
214 }
215
216 bool VM::is_cart_inserted(int drv)
217 {
218         if(drv == 0) {
219                 return memory->is_cart_inserted();
220         } else {
221                 return false;
222         }
223 }
224
225 void VM::play_tape(int drv, const _TCHAR* file_path)
226 {
227         drec->play_tape(file_path);
228 //      drec->set_remote(true);
229 }
230
231 void VM::rec_tape(int drv, const _TCHAR* file_path)
232 {
233         drec->rec_tape(file_path);
234 //      drec->set_remote(true);
235 }
236
237 void VM::close_tape(int drv)
238 {
239         emu->lock_vm();
240         drec->close_tape();
241         emu->unlock_vm();
242 //      drec->set_remote(false);
243 }
244
245 bool VM::is_tape_inserted(int drv)
246 {
247         return drec->is_tape_inserted();
248 }
249
250 bool VM::is_tape_playing(int drv)
251 {
252         return drec->is_tape_playing();
253 }
254
255 bool VM::is_tape_recording(int drv)
256 {
257         return drec->is_tape_recording();
258 }
259
260 int VM::get_tape_position(int drv)
261 {
262         return drec->get_tape_position();
263 }
264
265 const _TCHAR* VM::get_tape_message(int drv)
266 {
267         return drec->get_message();
268 }
269
270 void VM::push_play(int drv)
271 {
272         drec->set_ff_rew(0);
273         drec->set_remote(true);
274 }
275
276 void VM::push_stop(int drv)
277 {
278         drec->set_remote(false);
279 }
280
281 void VM::push_fast_forward(int drv)
282 {
283         drec->set_ff_rew(1);
284         drec->set_remote(true);
285 }
286
287 void VM::push_fast_rewind(int drv)
288 {
289         drec->set_ff_rew(-1);
290         drec->set_remote(true);
291 }
292
293 bool VM::is_frame_skippable()
294 {
295         return event->is_frame_skippable();
296 }
297
298 void VM::update_config()
299 {
300         for(DEVICE* device = first_device; device; device = device->next_device) {
301                 device->update_config();
302         }
303 }
304
305 #define STATE_VERSION   3
306
307 #include "../../statesub.h"
308 #include "../../qt/gui/csp_logger.h"
309 extern CSP_Logger DLL_PREFIX_I *csp_logger;
310
311 void VM::decl_state(void)
312 {
313         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::RX_78_HEAD")), csp_logger);
314         for(DEVICE* device = first_device; device; device = device->next_device) {
315                 device->decl_state();
316         }
317 }
318
319 void VM::save_state(FILEIO* state_fio)
320 {
321         //state_fio->FputUint32(STATE_VERSION);
322         
323         if(state_entry != NULL) {
324                 state_entry->save_state(state_fio);
325         }
326         for(DEVICE* device = first_device; device; device = device->next_device) {
327                 device->save_state(state_fio);
328         }
329 }
330
331 bool VM::load_state(FILEIO* state_fio)
332 {
333         //if(state_fio->FgetUint32() != STATE_VERSION) {
334         //      return false;
335         //}
336         bool mb = false;
337         if(state_entry != NULL) {
338                 mb = state_entry->load_state(state_fio);
339         }
340         if(!mb) {
341                 emu->out_debug_log("INFO: HEADER DATA ERROR");
342                 return false;
343         }
344         for(DEVICE* device = first_device; device; device = device->next_device) {
345                 if(!device->load_state(state_fio)) {
346                         return false;
347                 }
348         }
349         return true;
350 }
351