OSDN Git Service

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