OSDN Git Service

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