OSDN Git Service

[General] Merge upstream 2018-05-24. Still not test to build, will test.
[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) : emu(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         for(DEVICE* device = first_device; device; device = device->next_device) {
89                 device->initialize();
90         }
91 }
92
93 VM::~VM()
94 {
95         // delete all devices
96         for(DEVICE* device = first_device; device;) {
97                 DEVICE *next_device = device->next_device;
98                 device->release();
99                 delete device;
100                 device = next_device;
101         }
102 }
103
104 DEVICE* VM::get_device(int id)
105 {
106         for(DEVICE* device = first_device; device; device = device->next_device) {
107                 if(device->this_device_id == id) {
108                         return device;
109                 }
110         }
111         return NULL;
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 // drive virtual machine
130 // ----------------------------------------------------------------------------
131
132 void VM::reset()
133 {
134         // reset all devices
135         for(DEVICE* device = first_device; device; device = device->next_device) {
136                 device->reset();
137         }
138 }
139
140 void VM::run()
141 {
142         event->drive();
143 }
144
145 // ----------------------------------------------------------------------------
146 // draw screen
147 // ----------------------------------------------------------------------------
148
149 void VM::draw_screen()
150 {
151         vdp->draw_screen();
152 }
153
154 // ----------------------------------------------------------------------------
155 // soud manager
156 // ----------------------------------------------------------------------------
157
158 void VM::initialize_sound(int rate, int samples)
159 {
160         // init sound manager
161         event->initialize_sound(rate, samples);
162         
163         // init sound gen
164         psg->initialize_sound(rate, 3579545, 8000);
165 }
166
167 uint16_t* VM::create_sound(int* extra_frames)
168 {
169         return event->create_sound(extra_frames);
170 }
171
172 int VM::get_sound_buffer_ptr()
173 {
174         return event->get_sound_buffer_ptr();
175 }
176
177 #ifdef USE_SOUND_VOLUME
178 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
179 {
180         if(ch == 0) {
181                 psg->set_volume(0, decibel_l, decibel_r);
182         } else if(ch == 1) {
183                 drec->set_volume(0, decibel_l, decibel_r);
184         } else if(ch == 2) {
185                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
186                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
187                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
188         }
189 }
190 #endif
191
192 // ----------------------------------------------------------------------------
193 // user interface
194 // ----------------------------------------------------------------------------
195
196 void VM::open_cart(int drv, const _TCHAR* file_path)
197 {
198         if(drv == 0) {
199                 memory->open_cart(file_path);
200                 reset();
201         }
202 }
203
204 void VM::close_cart(int drv)
205 {
206         if(drv == 0) {
207                 memory->close_cart();
208                 reset();
209         }
210 }
211
212 bool VM::is_cart_inserted(int drv)
213 {
214         if(drv == 0) {
215                 return memory->is_cart_inserted();
216         } else {
217                 return false;
218         }
219 }
220
221 void VM::play_tape(int drv, const _TCHAR* file_path)
222 {
223         drec->play_tape(file_path);
224 //      drec->set_remote(true);
225 }
226
227 void VM::rec_tape(int drv, const _TCHAR* file_path)
228 {
229         drec->rec_tape(file_path);
230 //      drec->set_remote(true);
231 }
232
233 void VM::close_tape(int drv)
234 {
235         emu->lock_vm();
236         drec->close_tape();
237         emu->unlock_vm();
238 //      drec->set_remote(false);
239 }
240
241 bool VM::is_tape_inserted(int drv)
242 {
243         return drec->is_tape_inserted();
244 }
245
246 bool VM::is_tape_playing(int drv)
247 {
248         return drec->is_tape_playing();
249 }
250
251 bool VM::is_tape_recording(int drv)
252 {
253         return drec->is_tape_recording();
254 }
255
256 int VM::get_tape_position(int drv)
257 {
258         return drec->get_tape_position();
259 }
260
261 const _TCHAR* VM::get_tape_message(int drv)
262 {
263         return drec->get_message();
264 }
265
266 void VM::push_play(int drv)
267 {
268         drec->set_ff_rew(0);
269         drec->set_remote(true);
270 }
271
272 void VM::push_stop(int drv)
273 {
274         drec->set_remote(false);
275 }
276
277 void VM::push_fast_forward(int drv)
278 {
279         drec->set_ff_rew(1);
280         drec->set_remote(true);
281 }
282
283 void VM::push_fast_rewind(int drv)
284 {
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 #define STATE_VERSION   3
302
303 void VM::save_state(FILEIO* state_fio)
304 {
305         state_fio->FputUint32(STATE_VERSION);
306         
307         for(DEVICE* device = first_device; device; device = device->next_device) {
308                 device->save_state(state_fio);
309         }
310 }
311
312 bool VM::load_state(FILEIO* state_fio)
313 {
314         if(state_fio->FgetUint32() != STATE_VERSION) {
315                 return false;
316         }
317         for(DEVICE* device = first_device; device; device = device->next_device) {
318                 if(!device->load_state(state_fio)) {
319                         return false;
320                 }
321         }
322         return true;
323 }
324