OSDN Git Service

846847eabddc5c64bbf89d1469834dd1aa5489e0
[csp-qt/common_source_project-fm7.git] / source / src / vm / sc3000 / sc3000.cpp
1 /*
2         SEGA SC-3000 Emulator 'eSC-3000'
3
4         Author : Takeda.Toshiya
5         Date   : 2010.08.17-
6
7         [ virtual machine ]
8 */
9
10 #include "sc3000.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../disk.h"
17 #include "../i8251.h"
18 #include "../i8255.h"
19 #include "../io.h"
20 #include "../noise.h"
21 #include "../sn76489an.h"
22 #include "../tms9918a.h"
23 #include "../upd765a.h"
24 #include "../z80.h"
25
26 #ifdef USE_DEBUGGER
27 #include "../debugger.h"
28 #endif
29
30 #include "keyboard.h"
31 #include "memory.h"
32
33 // ----------------------------------------------------------------------------
34 // initialize
35 // ----------------------------------------------------------------------------
36
37 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
38 {
39         // create devices
40         first_device = last_device = NULL;
41         dummy = new DEVICE(this, emu);  // must be 1st device
42         event = new EVENT(this, emu);   // must be 2nd device
43         dummy->set_device_name(_T("1st Dummy"));
44
45         drec = new DATAREC(this, emu);
46         drec->set_context_noise_play(new NOISE(this, emu));
47         drec->set_context_noise_stop(new NOISE(this, emu));
48         drec->set_context_noise_fast(new NOISE(this, emu));
49         sio = new I8251(this, emu);
50         pio_k = new I8255(this, emu);
51         pio_k->set_device_name(_T("8255 PIO (Keyboard)"));
52         pio_f = new I8255(this, emu);
53         pio_f->set_device_name(_T("8255 PIO (Floppy I/F)"));
54         io = new IO(this, emu);
55         psg = new SN76489AN(this, emu);
56         vdp = new TMS9918A(this, emu);
57         fdc = new UPD765A(this, emu);
58         fdc->set_context_noise_seek(new NOISE(this, emu));
59         fdc->set_context_noise_head_down(new NOISE(this, emu));
60         fdc->set_context_noise_head_up(new NOISE(this, emu));
61         cpu = new Z80(this, emu);
62
63         key = new KEYBOARD(this, emu);
64         memory = new MEMORY(this, emu);
65    
66         // set contexts
67         event->set_context_cpu(cpu);
68         event->set_context_sound(psg);
69         event->set_context_sound(drec);
70         event->set_context_sound(fdc->get_context_noise_seek());
71         event->set_context_sound(fdc->get_context_noise_head_down());
72         event->set_context_sound(fdc->get_context_noise_head_up());
73         event->set_context_sound(drec->get_context_noise_play());
74         event->set_context_sound(drec->get_context_noise_stop());
75         event->set_context_sound(drec->get_context_noise_fast());
76         
77         drec->set_context_ear(pio_k, SIG_I8255_PORT_B, 0x80);
78         pio_k->set_context_port_c(key, SIG_KEYBOARD_COLUMN, 0x07, 0);
79         pio_k->set_context_port_c(drec, SIG_DATAREC_REMOTE, 0x08, 0);
80         pio_k->set_context_port_c(drec, SIG_DATAREC_MIC, 0x10, 0);
81         pio_f->set_context_port_c(fdc, SIG_UPD765A_MOTOR_NEG, 2, 0);
82         pio_f->set_context_port_c(fdc, SIG_UPD765A_TC, 4, 0);
83         pio_f->set_context_port_c(fdc, SIG_UPD765A_RESET, 8, 0);
84         pio_f->set_context_port_c(memory, SIG_MEMORY_SEL, 0x40, 0);
85         vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
86         fdc->set_context_irq(pio_f, SIG_I8255_PORT_A, 1);
87         fdc->set_context_index(pio_f, SIG_I8255_PORT_A, 4);
88         
89         key->set_context_cpu(cpu);
90         key->set_context_pio(pio_k);
91         
92         // cpu bus
93         cpu->set_context_mem(memory);
94         cpu->set_context_io(io);
95         cpu->set_context_intr(dummy);
96 #ifdef USE_DEBUGGER
97         cpu->set_context_debugger(new DEBUGGER(this, emu));
98 #endif
99         
100         // i/o bus
101         io->set_iomap_range_rw(0x40, 0x7f, psg);
102         io->set_iomap_range_rw(0x80, 0xbf, vdp);
103         io->set_iomap_range_rw(0xc0, 0xdf, pio_k);
104         io->set_iomap_range_rw(0xe0, 0xe3, fdc);
105         io->set_iomap_range_rw(0xe4, 0xe7, pio_f);
106         io->set_iomap_range_rw(0xe8, 0xeb, sio);
107         
108         // initialize all devices
109 #if defined(__GIT_REPO_VERSION)
110         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
111 #endif
112         for(DEVICE* device = first_device; device; device = device->next_device) {
113                 device->initialize();
114         }
115         for(int i = 0; i < 4; i++) {
116                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
117         }
118 }
119
120 VM::~VM()
121 {
122         // delete all devices
123         for(DEVICE* device = first_device; device;) {
124                 DEVICE *next_device = device->next_device;
125                 device->release();
126                 delete device;
127                 device = next_device;
128         }
129 }
130
131 DEVICE* VM::get_device(int id)
132 {
133         for(DEVICE* device = first_device; device; device = device->next_device) {
134                 if(device->this_device_id == id) {
135                         return device;
136                 }
137         }
138         return NULL;
139 }
140
141 // ----------------------------------------------------------------------------
142 // debugger
143 // ----------------------------------------------------------------------------
144
145 #ifdef USE_DEBUGGER
146 DEVICE *VM::get_cpu(int index)
147 {
148         if(index == 0) {
149                 return cpu;
150         }
151         return NULL;
152 }
153 #endif
154
155 // ----------------------------------------------------------------------------
156 // drive virtual machine
157 // ----------------------------------------------------------------------------
158
159 void VM::reset()
160 {
161         // reset all devices
162         for(DEVICE* device = first_device; device; device = device->next_device) {
163                 device->reset();
164         }
165 }
166
167 void VM::run()
168 {
169         event->drive();
170 }
171
172 // ----------------------------------------------------------------------------
173 // draw screen
174 // ----------------------------------------------------------------------------
175
176 void VM::draw_screen()
177 {
178         vdp->draw_screen();
179 }
180
181 // ----------------------------------------------------------------------------
182 // soud manager
183 // ----------------------------------------------------------------------------
184
185 void VM::initialize_sound(int rate, int samples)
186 {
187         // init sound manager
188         event->initialize_sound(rate, samples);
189         
190         // init sound gen
191         psg->initialize_sound(rate, 3579545, 8000);
192 }
193
194 uint16_t* VM::create_sound(int* extra_frames)
195 {
196         return event->create_sound(extra_frames);
197 }
198
199 int VM::get_sound_buffer_ptr()
200 {
201         return event->get_sound_buffer_ptr();
202 }
203
204 #ifdef USE_SOUND_VOLUME
205 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
206 {
207         if(ch == 0) {
208                 psg->set_volume(0, decibel_l, decibel_r);
209         } else if(ch == 1) {
210                 drec->set_volume(0, decibel_l, decibel_r);
211         } else if(ch == 2) {
212                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
213                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
214                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
215         } else if(ch == 3) {
216                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
217                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
218                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
219         }
220 }
221 #endif
222
223 // ----------------------------------------------------------------------------
224 // user interface
225 // ----------------------------------------------------------------------------
226
227 void VM::open_cart(int drv, const _TCHAR* file_path)
228 {
229         if(drv == 0) {
230                 memory->open_cart(file_path);
231                 reset();
232         }
233 }
234
235 void VM::close_cart(int drv)
236 {
237         if(drv == 0) {
238                 memory->close_cart();
239                 reset();
240         }
241 }
242
243 bool VM::is_cart_inserted(int drv)
244 {
245         if(drv == 0) {
246                 return memory->is_cart_inserted();
247         } else {
248                 return false;
249         }
250 }
251
252 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
253 {
254         fdc->open_disk(drv, file_path, bank);
255 }
256
257 void VM::close_floppy_disk(int drv)
258 {
259         fdc->close_disk(drv);
260 }
261
262 bool VM::is_floppy_disk_inserted(int drv)
263 {
264         return fdc->is_disk_inserted(drv);
265 }
266
267 void VM::is_floppy_disk_protected(int drv, bool value)
268 {
269         fdc->is_disk_protected(drv, value);
270 }
271
272 bool VM::is_floppy_disk_protected(int drv)
273 {
274         return fdc->is_disk_protected(drv);
275 }
276
277 uint32_t VM::is_floppy_disk_accessed()
278 {
279         return fdc->read_signal(0);
280 }
281
282 void VM::play_tape(int drv, const _TCHAR* file_path)
283 {
284         drec->play_tape(file_path);
285 //      drec->set_remote(true);
286 }
287
288 void VM::rec_tape(int drv, const _TCHAR* file_path)
289 {
290         drec->rec_tape(file_path);
291 //      drec->set_remote(true);
292 }
293
294 void VM::close_tape(int drv)
295 {
296         emu->lock_vm();
297         drec->close_tape();
298         emu->unlock_vm();
299 //      drec->set_remote(false);
300 }
301
302 bool VM::is_tape_inserted(int drv)
303 {
304         return drec->is_tape_inserted();
305 }
306
307 bool VM::is_tape_playing(int drv)
308 {
309         return drec->is_tape_playing();
310 }
311
312 bool VM::is_tape_recording(int drv)
313 {
314         return drec->is_tape_recording();
315 }
316
317 int VM::get_tape_position(int drv)
318 {
319         return drec->get_tape_position();
320 }
321
322 const _TCHAR* VM::get_tape_message(int drv)
323 {
324         return drec->get_message();
325 }
326
327 void VM::push_play(int drv)
328 {
329         drec->set_ff_rew(0);
330         drec->set_remote(true);
331 }
332
333 void VM::push_stop(int drv)
334 {
335         drec->set_remote(false);
336 }
337
338 void VM::push_fast_forward(int drv)
339 {
340         drec->set_ff_rew(1);
341         drec->set_remote(true);
342 }
343
344 void VM::push_fast_rewind(int drv)
345 {
346         drec->set_ff_rew(-1);
347         drec->set_remote(true);
348 }
349
350 bool VM::is_frame_skippable()
351 {
352         return event->is_frame_skippable();
353 }
354
355 void VM::update_config()
356 {
357         for(DEVICE* device = first_device; device; device = device->next_device) {
358                 device->update_config();
359         }
360 }
361
362 #define STATE_VERSION   3
363
364 #include "../../statesub.h"
365 #include "../../qt/gui/csp_logger.h"
366 extern CSP_Logger DLL_PREFIX_I *csp_logger;
367
368 void VM::decl_state(void)
369 {
370         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::SC_3000_HEAD")), csp_logger);
371         for(DEVICE* device = first_device; device; device = device->next_device) {
372                 device->decl_state();
373         }
374 }
375
376 void VM::save_state(FILEIO* state_fio)
377 {
378         //state_fio->FputUint32(STATE_VERSION);
379         
380         if(state_entry != NULL) {
381                 state_entry->save_state(state_fio);
382         }
383         for(DEVICE* device = first_device; device; device = device->next_device) {
384                 device->save_state(state_fio);
385         }
386 }
387
388 bool VM::load_state(FILEIO* state_fio)
389 {
390         //if(state_fio->FgetUint32() != STATE_VERSION) {
391         //      return false;
392         //}
393         bool mb = false;
394         if(state_entry != NULL) {
395                 mb = state_entry->load_state(state_fio);
396         }
397         if(!mb) {
398                 emu->out_debug_log("INFO: HEADER DATA ERROR");
399                 return false;
400         }
401         for(DEVICE* device = first_device; device; device = device->next_device) {
402                 if(!device->load_state(state_fio)) {
403                         return false;
404                 }
405         }
406         return true;
407 }
408