OSDN Git Service

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