OSDN Git Service

fed194323b71ba7354c83208893030cab27b8505
[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         for(DEVICE* device = first_device; device; device = device->next_device) {
110                 device->initialize();
111         }
112         for(int i = 0; i < 4; i++) {
113                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
114         }
115 }
116
117 VM::~VM()
118 {
119         // delete all devices
120         for(DEVICE* device = first_device; device;) {
121                 DEVICE *next_device = device->next_device;
122                 device->release();
123                 delete device;
124                 device = next_device;
125         }
126 }
127
128 DEVICE* VM::get_device(int id)
129 {
130         for(DEVICE* device = first_device; device; device = device->next_device) {
131                 if(device->this_device_id == id) {
132                         return device;
133                 }
134         }
135         return NULL;
136 }
137
138 // ----------------------------------------------------------------------------
139 // debugger
140 // ----------------------------------------------------------------------------
141
142 #ifdef USE_DEBUGGER
143 DEVICE *VM::get_cpu(int index)
144 {
145         if(index == 0) {
146                 return cpu;
147         }
148         return NULL;
149 }
150 #endif
151
152 // ----------------------------------------------------------------------------
153 // drive virtual machine
154 // ----------------------------------------------------------------------------
155
156 void VM::reset()
157 {
158         // reset all devices
159         for(DEVICE* device = first_device; device; device = device->next_device) {
160                 device->reset();
161         }
162 }
163
164 void VM::run()
165 {
166         event->drive();
167 }
168
169 // ----------------------------------------------------------------------------
170 // draw screen
171 // ----------------------------------------------------------------------------
172
173 void VM::draw_screen()
174 {
175         vdp->draw_screen();
176 }
177
178 // ----------------------------------------------------------------------------
179 // soud manager
180 // ----------------------------------------------------------------------------
181
182 void VM::initialize_sound(int rate, int samples)
183 {
184         // init sound manager
185         event->initialize_sound(rate, samples);
186         
187         // init sound gen
188         psg->initialize_sound(rate, 3579545, 8000);
189 }
190
191 uint16_t* VM::create_sound(int* extra_frames)
192 {
193         return event->create_sound(extra_frames);
194 }
195
196 int VM::get_sound_buffer_ptr()
197 {
198         return event->get_sound_buffer_ptr();
199 }
200
201 #ifdef USE_SOUND_VOLUME
202 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
203 {
204         if(ch == 0) {
205                 psg->set_volume(0, decibel_l, decibel_r);
206         } else if(ch == 1) {
207                 drec->set_volume(0, decibel_l, decibel_r);
208         } else if(ch == 2) {
209                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
210                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
211                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
212         } else if(ch == 3) {
213                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
214                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
215                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
216         }
217 }
218 #endif
219
220 // ----------------------------------------------------------------------------
221 // user interface
222 // ----------------------------------------------------------------------------
223
224 void VM::open_cart(int drv, const _TCHAR* file_path)
225 {
226         if(drv == 0) {
227                 memory->open_cart(file_path);
228                 reset();
229         }
230 }
231
232 void VM::close_cart(int drv)
233 {
234         if(drv == 0) {
235                 memory->close_cart();
236                 reset();
237         }
238 }
239
240 bool VM::is_cart_inserted(int drv)
241 {
242         if(drv == 0) {
243                 return memory->is_cart_inserted();
244         } else {
245                 return false;
246         }
247 }
248
249 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
250 {
251         fdc->open_disk(drv, file_path, bank);
252 }
253
254 void VM::close_floppy_disk(int drv)
255 {
256         fdc->close_disk(drv);
257 }
258
259 bool VM::is_floppy_disk_inserted(int drv)
260 {
261         return fdc->is_disk_inserted(drv);
262 }
263
264 void VM::is_floppy_disk_protected(int drv, bool value)
265 {
266         fdc->is_disk_protected(drv, value);
267 }
268
269 bool VM::is_floppy_disk_protected(int drv)
270 {
271         return fdc->is_disk_protected(drv);
272 }
273
274 uint32_t VM::is_floppy_disk_accessed()
275 {
276         return fdc->read_signal(0);
277 }
278
279 void VM::play_tape(int drv, const _TCHAR* file_path)
280 {
281         drec->play_tape(file_path);
282 //      drec->set_remote(true);
283 }
284
285 void VM::rec_tape(int drv, const _TCHAR* file_path)
286 {
287         drec->rec_tape(file_path);
288 //      drec->set_remote(true);
289 }
290
291 void VM::close_tape(int drv)
292 {
293         emu->lock_vm();
294         drec->close_tape();
295         emu->unlock_vm();
296 //      drec->set_remote(false);
297 }
298
299 bool VM::is_tape_inserted(int drv)
300 {
301         return drec->is_tape_inserted();
302 }
303
304 bool VM::is_tape_playing(int drv)
305 {
306         return drec->is_tape_playing();
307 }
308
309 bool VM::is_tape_recording(int drv)
310 {
311         return drec->is_tape_recording();
312 }
313
314 int VM::get_tape_position(int drv)
315 {
316         return drec->get_tape_position();
317 }
318
319 const _TCHAR* VM::get_tape_message(int drv)
320 {
321         return drec->get_message();
322 }
323
324 void VM::push_play(int drv)
325 {
326         drec->set_ff_rew(0);
327         drec->set_remote(true);
328 }
329
330 void VM::push_stop(int drv)
331 {
332         drec->set_remote(false);
333 }
334
335 void VM::push_fast_forward(int drv)
336 {
337         drec->set_ff_rew(1);
338         drec->set_remote(true);
339 }
340
341 void VM::push_fast_rewind(int drv)
342 {
343         drec->set_ff_rew(-1);
344         drec->set_remote(true);
345 }
346
347 bool VM::is_frame_skippable()
348 {
349         return event->is_frame_skippable();
350 }
351
352 void VM::update_config()
353 {
354         for(DEVICE* device = first_device; device; device = device->next_device) {
355                 device->update_config();
356         }
357 }
358
359 #define STATE_VERSION   3
360
361 #include "../../statesub.h"
362 #include "../../qt/gui/csp_logger.h"
363 extern CSP_Logger DLL_PREFIX_I *csp_logger;
364
365 void VM::decl_state(void)
366 {
367         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::SC_3000_HEAD")), csp_logger);
368         for(DEVICE* device = first_device; device; device = device->next_device) {
369                 device->decl_state();
370         }
371 }
372
373 void VM::save_state(FILEIO* state_fio)
374 {
375         //state_fio->FputUint32(STATE_VERSION);
376         
377         if(state_entry != NULL) {
378                 state_entry->save_state(state_fio);
379         }
380         for(DEVICE* device = first_device; device; device = device->next_device) {
381                 device->save_state(state_fio);
382         }
383 }
384
385 bool VM::load_state(FILEIO* state_fio)
386 {
387         //if(state_fio->FgetUint32() != STATE_VERSION) {
388         //      return false;
389         //}
390         bool mb = false;
391         if(state_entry != NULL) {
392                 mb = state_entry->load_state(state_fio);
393         }
394         if(!mb) {
395                 emu->out_debug_log("INFO: HEADER DATA ERROR");
396                 return false;
397         }
398         for(DEVICE* device = first_device; device; device = device->next_device) {
399                 if(!device->load_state(state_fio)) {
400                         return false;
401                 }
402         }
403         return true;
404 }
405