OSDN Git Service

cb44959bb3b77e7e4dbe755e22d61f9a458b4fcd
[csp-qt/common_source_project-fm7.git] / source / src / vm / fmr30 / fmr30.cpp
1 /*
2         FUJITSU FMR-30 Emulator 'eFMR-30'
3
4         Author : Takeda.Toshiya
5         Date   : 2008.12.29 -
6
7         [ virtual machine ]
8 */
9
10 #include "fmr30.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../harddisk.h"
16 #include "../i8237.h"
17 #include "../i8251.h"
18 #include "../i8253.h"
19 #include "../i8259.h"
20 //#if defined(HAS_I86)
21 //#include "../i86.h"
22 //#else
23 #include "../i286.h"
24 //#endif
25 #include "../io.h"
26 #include "../mb8877.h"
27 #include "../noise.h"
28 #include "../scsi_hdd.h"
29 #include "../scsi_host.h"
30 #include "../sn76489an.h"
31
32 #ifdef USE_DEBUGGER
33 #include "../debugger.h"
34 #endif
35
36 #include "../fmr50/bios.h"
37 #include "cmos.h"
38 #include "floppy.h"
39 #include "keyboard.h"
40 #include "memory.h"
41 #include "rtc.h"
42 #include "scsi.h"
43 #include "serial.h"
44 #include "system.h"
45 #include "timer.h"
46
47 // ----------------------------------------------------------------------------
48 // initialize
49 // ----------------------------------------------------------------------------
50
51 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
52 {
53         // create devices
54         first_device = last_device = NULL;
55         dummy = new DEVICE(this, emu);  // must be 1st device
56         dummy->set_device_name(_T("1st dummy"));
57         event = new EVENT(this, emu);   // must be 2nd device
58         
59         dma = new I8237(this, emu);
60         sio_kb = new I8251(this, emu);  // keyboard
61         sio_kb->set_device_name(_T("8251 SIO (Keyboard)"));
62         sio_sub = new I8251(this, emu); // sub display
63         sio_sub->set_device_name(_T("8251 SIO (Sub System)"));
64         sio_ch1 = new I8251(this, emu); // RS-232C ch.1
65         sio_ch1->set_device_name(_T("8251 SIO (RS-232C #1)"));
66         sio_ch2 = new I8251(this, emu); // RS-232C ch.2
67         sio_ch2->set_device_name(_T("8251 SIO (RS-232C #2)"));
68         pit = new I8253(this, emu);
69         pic = new I8259(this, emu);
70 //#if defined(HAS_I86)
71 //      cpu = new I86(this, emu);
72 //#else
73         cpu = new I286(this, emu);
74 //#endif
75         io = new IO(this, emu);
76         fdc = new MB8877(this, emu);
77         fdc->set_context_noise_seek(new NOISE(this, emu));
78         fdc->set_context_noise_head_down(new NOISE(this, emu));
79         fdc->set_context_noise_head_up(new NOISE(this, emu));
80         cpu->set_device_name(_T("CPU(80C86)"));
81         
82         scsi_host = new SCSI_HOST(this, emu);
83         for(int i = 0; i < USE_HARD_DISK; i++) {
84                 scsi_hdd[i] = new SCSI_HDD(this, emu);
85                 scsi_hdd[i]->set_device_name(_T("SCSI Hard Disk Drive #%d"), i + 1);
86                 scsi_hdd[i]->scsi_id = i;
87                 scsi_hdd[i]->set_disk_handler(0, new HARDDISK(emu));
88                 scsi_hdd[i]->set_context_interface(scsi_host);
89                 scsi_host->set_context_target(scsi_hdd[i]);
90         }
91         psg = new SN76489AN(this, emu);
92         if(FILEIO::IsFileExisting(create_local_path(_T("IPL.ROM")))) {
93                 bios = NULL;
94         } else {
95                 bios = new BIOS(this, emu);
96         }
97         cmos = new CMOS(this, emu);
98         floppy = new FLOPPY(this, emu);
99         keyboard = new KEYBOARD(this, emu);
100         memory = new MEMORY(this, emu);
101         rtc = new RTC(this, emu);
102         scsi = new SCSI(this, emu);
103         serial = new SERIAL(this, emu);
104         system = new SYSTEM(this, emu);
105         timer = new TIMER(this, emu);
106         // set contexts
107         event->set_context_cpu(cpu);
108         event->set_context_sound(psg);
109         event->set_context_sound(fdc->get_context_noise_seek());
110         event->set_context_sound(fdc->get_context_noise_head_down());
111         event->set_context_sound(fdc->get_context_noise_head_up());
112         
113         dma->set_context_memory(memory);
114         dma->set_context_ch0(fdc);
115         dma->set_context_ch1(scsi_host);
116         dma->set_context_tc1(scsi, SIG_SCSI_TC, 1);
117         sio_kb->set_context_rxrdy(serial, SIG_SERIAL_RXRDY_KB, 1);
118         sio_kb->set_context_txrdy(serial, SIG_SERIAL_TXRDY_KB, 1);
119         sio_sub->set_context_rxrdy(serial, SIG_SERIAL_RXRDY_SUB, 1);
120         sio_sub->set_context_txrdy(serial, SIG_SERIAL_TXRDY_SUB, 1);
121         sio_ch1->set_context_rxrdy(serial, SIG_SERIAL_RXRDY_CH1, 1);
122         sio_ch1->set_context_txrdy(serial, SIG_SERIAL_TXRDY_CH1, 1);
123         sio_ch2->set_context_rxrdy(serial, SIG_SERIAL_RXRDY_CH2, 1);
124         sio_ch2->set_context_txrdy(serial, SIG_SERIAL_TXRDY_CH2, 1);
125         pit->set_context_ch0(timer, SIG_TIMER_CH0, 1);
126         pit->set_context_ch1(timer, SIG_TIMER_CH1, 1);
127         pit->set_constant_clock(0, 1000000);
128         pit->set_constant_clock(1, 1000000);
129         pic->set_context_cpu(cpu);
130         fdc->set_context_drq(dma, SIG_I8237_CH0, 1);
131         fdc->set_context_irq(floppy, SIG_FLOPPY_IRQ, 1);
132         scsi_host->set_context_irq(scsi, SIG_SCSI_IRQ, 1);
133         scsi_host->set_context_drq(scsi, SIG_SCSI_DRQ, 1);
134         
135         floppy->set_context_fdc(fdc);
136         floppy->set_context_pic(pic);
137         keyboard->set_context_sio(sio_kb);
138         memory->set_context_cpu(cpu);
139         memory->set_context_dma(dma);
140         rtc->set_context_pic(pic);
141         scsi->set_context_dma(dma);
142         scsi->set_context_pic(pic);
143         scsi->set_context_host(scsi_host);
144         serial->set_context_pic(pic);
145         serial->set_context_sio(sio_kb, sio_sub, sio_ch1, sio_ch2);
146         timer->set_context_pic(pic);
147         
148         // cpu bus
149         cpu->set_context_mem(memory);
150         cpu->set_context_io(io);
151         cpu->set_context_intr(pic);
152         if(bios) {
153                 bios->set_context_mem(memory);
154                 bios->set_context_io(io);
155                 bios->set_cmos_ptr(cmos->get_cmos());
156                 bios->set_vram_ptr(memory->get_vram());
157                 bios->set_cvram_ptr(memory->get_cvram());
158                 bios->set_kvram_ptr(memory->get_kvram());
159                 cpu->set_context_bios(bios);
160         }
161 #ifdef SINGLE_MODE_DMA
162         cpu->set_context_dma(dma);
163 #endif
164 #ifdef USE_DEBUGGER
165         cpu->set_context_debugger(new DEBUGGER(this, emu));
166 #endif
167         
168         // i/o bus
169         io->set_iomap_range_rw(0x00, 0x07, rtc);
170         io->set_iomap_range_rw(0x08, 0x09, sio_kb);
171         io->set_iomap_range_rw(0x0a, 0x0b, serial);
172         io->set_iomap_range_rw(0x10, 0x11, sio_sub);
173         io->set_iomap_range_rw(0x12, 0x13, serial);
174         io->set_iomap_single_r(0x18, system);
175         io->set_iomap_range_rw(0x1d, 0x1e, memory);
176         io->set_iomap_range_r(0x20, 0x21, system);
177         io->set_iomap_single_rw(0x26, memory);
178         io->set_iomap_range_rw(0x30, 0x33, fdc);
179         io->set_iomap_range_rw(0x34, 0x36, floppy);
180         io->set_iomap_single_w(0x40, psg);
181         io->set_iomap_range_rw(0x42, 0x43, timer);
182         io->set_iomap_range_rw(0x46, 0x47, system);
183         io->set_iomap_range_rw(0x60, 0x61, sio_ch1);
184         io->set_iomap_range_rw(0x62, 0x66, serial);
185         io->set_iomap_range_rw(0x70, 0x71, sio_ch2);
186         io->set_iomap_range_rw(0x72, 0x76, serial);
187         io->set_iomap_alias_rw(0x100, pic, I8259_ADDR_CHIP0 | 0);
188         io->set_iomap_alias_rw(0x101, pic, I8259_ADDR_CHIP0 | 1);
189         io->set_iomap_alias_rw(0x108, pic, I8259_ADDR_CHIP1 | 0);
190         io->set_iomap_alias_rw(0x10a, pic, I8259_ADDR_CHIP1 | 1);
191         io->set_iomap_range_rw(0x110, 0x11f, dma);
192         io->set_iomap_range_w(0x120, 0x123, memory);
193         io->set_iomap_range_rw(0x130, 0x133, pit);
194         io->set_iomap_range_rw(0x2f0, 0x2f3, scsi);
195         io->set_iomap_range_rw(0x300, 0x30f, memory);
196         io->set_iomap_range_rw(0xc000, 0xdfff, cmos);
197         io->set_iomap_single_rw(0xff00, system);
198         
199         // initialize all devices
200         for(DEVICE* device = first_device; device; device = device->next_device) {
201                 device->initialize();
202         }
203         decl_state();
204         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
205 #ifdef OPEN_HARD_DISK_IN_RESET
206                 create_local_path(hd_file_path[drv], _MAX_PATH, _T("SCSI%d.DAT"), drv);
207 #else
208                 open_hard_disk_tmp(drv, create_local_path(_T("SCSI%d.DAT"), drv));
209 #endif
210         }
211         if(bios) {
212                 for(int drv = 0; drv < MAX_DRIVE; drv++) {
213                         bios->set_floppy_disk_handler(drv, fdc->get_disk_handler(drv));
214                 }
215                 for(int drv = 0; drv < USE_HARD_DISK; drv++) {
216                         bios->set_hard_disk_handler(drv, scsi_hdd[drv]->get_disk_handler(0));
217                 }
218         }
219 }
220
221 VM::~VM()
222 {
223         // delete all devices
224         for(DEVICE* device = first_device; device;) {
225                 DEVICE *next_device = device->next_device;
226                 device->release();
227                 delete device;
228                 device = next_device;
229         }
230 }
231
232 DEVICE* VM::get_device(int id)
233 {
234         for(DEVICE* device = first_device; device; device = device->next_device) {
235                 if(device->this_device_id == id) {
236                         return device;
237                 }
238         }
239         return NULL;
240 }
241
242 // ----------------------------------------------------------------------------
243 // drive virtual machine
244 // ----------------------------------------------------------------------------
245
246 void VM::reset()
247 {
248         // reset all devices
249         for(DEVICE* device = first_device; device; device = device->next_device) {
250                 device->reset();
251         }
252         // temporary fix...
253         for(DEVICE* device = first_device; device; device = device->next_device) {
254                 device->reset();
255         }
256         
257         // set devices
258         sio_kb->write_signal(SIG_I8251_DSR, 1, 1);
259         sio_sub->write_signal(SIG_I8251_DSR, 0, 0);
260         
261 #if defined(OPEN_HARD_DISK_IN_RESET)
262         // open/close hard disk images
263         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
264                 if(hd_file_path[drv][0] != _T('\0')) {
265                         open_hard_disk_tmp(drv, hd_file_path[drv]);
266                 } else {
267                         close_hard_disk_tmp(drv);
268                 }
269         }
270 #endif
271 }
272
273 void VM::run()
274 {
275         event->drive();
276 }
277
278 // ----------------------------------------------------------------------------
279 // debugger
280 // ----------------------------------------------------------------------------
281
282 #ifdef USE_DEBUGGER
283 DEVICE *VM::get_cpu(int index)
284 {
285         if(index == 0) {
286                 return cpu;
287         }
288         return NULL;
289 }
290 #endif
291
292 // ----------------------------------------------------------------------------
293 // draw screen
294 // ----------------------------------------------------------------------------
295
296 void VM::draw_screen()
297 {
298         memory->draw_screen();
299 }
300
301 // ----------------------------------------------------------------------------
302 // soud manager
303 // ----------------------------------------------------------------------------
304
305 void VM::initialize_sound(int rate, int samples)
306 {
307         // init sound manager
308         event->initialize_sound(rate, samples);
309         
310         // init sound gen
311         psg->initialize_sound(rate, 125000, 10000);
312 }
313
314 uint16_t* VM::create_sound(int* extra_frames)
315 {
316         return event->create_sound(extra_frames);
317 }
318
319 int VM::get_sound_buffer_ptr()
320 {
321         return event->get_sound_buffer_ptr();
322 }
323
324 #ifdef USE_SOUND_VOLUME
325 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
326 {
327         if(ch == 0) {
328                 psg->set_volume(0, decibel_l, decibel_r);
329         } else if(ch == 1) {
330                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
331                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
332                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
333         }
334 }
335 #endif
336
337 // ----------------------------------------------------------------------------
338 // notify key
339 // ----------------------------------------------------------------------------
340
341 void VM::key_down(int code, bool repeat)
342 {
343         keyboard->key_down(code);
344 }
345
346 void VM::key_up(int code)
347 {
348         keyboard->key_up(code);
349 }
350
351 // ----------------------------------------------------------------------------
352 // user interface
353 // ----------------------------------------------------------------------------
354
355 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
356 {
357         fdc->open_disk(drv, file_path, bank);
358         floppy->change_disk(drv);
359 }
360
361 void VM::close_floppy_disk(int drv)
362 {
363         fdc->close_disk(drv);
364 }
365
366 bool VM::is_floppy_disk_inserted(int drv)
367 {
368         return fdc->is_disk_inserted(drv);
369 }
370
371 void VM::is_floppy_disk_protected(int drv, bool value)
372 {
373         fdc->is_disk_protected(drv, value);
374 }
375
376 bool VM::is_floppy_disk_protected(int drv)
377 {
378         return fdc->is_disk_protected(drv);
379 }
380
381 uint32_t VM::is_floppy_disk_accessed()
382 {
383         uint32_t status = fdc->read_signal(0);
384         if(bios) {
385                 status |= bios->read_signal(0);
386         }
387         return status;
388 }
389
390 void VM::open_hard_disk(int drv, const _TCHAR* file_path)
391 {
392         if(drv < USE_HARD_DISK) {
393 #if defined(OPEN_HARD_DISK_IN_RESET)
394                 my_tcscpy_s(hd_file_path[drv], _MAX_PATH, file_path);
395 #else
396                 open_hard_disk_tmp(drv, file_path);
397 #endif
398         }
399 }
400
401 void VM::close_hard_disk(int drv)
402 {
403         if(drv < USE_HARD_DISK) {
404 #if defined(OPEN_HARD_DISK_IN_RESET)
405                 hd_file_path[drv][0] = _T('\0');
406 #else
407                 close_hard_disk_tmp(drv);
408 #endif
409         }
410 }
411
412 bool VM::is_hard_disk_inserted(int drv)
413 {
414         if(drv < USE_HARD_DISK) {
415 #if defined(OPEN_HARD_DISK_IN_RESET)
416                 return (hd_file_path[drv][0] != _T('\0'));
417 #else
418                 return is_hard_disk_inserted_tmp(drv);
419 #endif
420         }
421         return false;
422 }
423
424 uint32_t VM::is_hard_disk_accessed()
425 {
426         uint32_t status = 0;
427         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
428                 if(scsi_hdd[drv]->get_disk_handler(0)->accessed()) {
429                         status |= 1 << drv;
430                 }
431         }
432         return status;
433 }
434
435 void VM::open_hard_disk_tmp(int drv, const _TCHAR* file_path)
436 {
437         if(drv < USE_HARD_DISK) {
438                 scsi_hdd[drv]->get_disk_handler(0)->open(file_path, 512);
439         }
440 }
441
442 void VM::close_hard_disk_tmp(int drv)
443 {
444         if(drv < USE_HARD_DISK) {
445                 scsi_hdd[drv]->get_disk_handler(0)->close();
446         }
447 }
448
449 bool VM::is_hard_disk_inserted_tmp(int drv)
450 {
451         if(drv < USE_HARD_DISK) {
452                 return scsi_hdd[drv]->get_disk_handler(0)->mounted();
453         }
454         return false;
455 }
456
457 bool VM::is_frame_skippable()
458 {
459         return event->is_frame_skippable();
460 }
461
462 void VM::update_config()
463 {
464         for(DEVICE* device = first_device; device; device = device->next_device) {
465                 device->update_config();
466         }
467 }
468
469 #define STATE_VERSION   7
470
471 #include "../../statesub.h"
472 #include "../../qt/gui/csp_logger.h"
473 extern CSP_Logger DLL_PREFIX_I *csp_logger;
474
475 void VM::decl_state(void)
476 {
477         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FMR30_SERIES_HEAD")), csp_logger);
478
479         for(DEVICE* device = first_device; device; device = device->next_device) {
480                 device->decl_state();
481         }
482 }
483
484 void VM::save_state(FILEIO* state_fio)
485 {
486         //state_fio->FputUint32(STATE_VERSION);
487         if(state_entry != NULL) {
488                 state_entry->save_state(state_fio);
489         }
490         for(DEVICE* device = first_device; device; device = device->next_device) {
491                 device->save_state(state_fio);
492         }
493 }
494
495 bool VM::load_state(FILEIO* state_fio)
496 {
497         //if(state_fio->FgetUint32() != STATE_VERSION) {
498         //      return false;
499         //}
500         bool mb = false;
501         if(state_entry != NULL) {
502                 mb = state_entry->load_state(state_fio);
503         }
504         if(!mb) {
505                 emu->out_debug_log("INFO: HEADER DATA ERROR");
506                 return false;
507         }
508
509         for(DEVICE* device = first_device; device; device = device->next_device) {
510                 if(!device->load_state(state_fio)) {
511                         return false;
512                 }
513         }
514         return true;
515 }
516