OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[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 #if defined(__GIT_REPO_VERSION)
201         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
202 #endif
203         for(DEVICE* device = first_device; device; device = device->next_device) {
204                 device->initialize();
205         }
206         decl_state();
207         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
208 #ifdef OPEN_HARD_DISK_IN_RESET
209                 create_local_path(hd_file_path[drv], _MAX_PATH, _T("SCSI%d.DAT"), drv);
210 #else
211                 open_hard_disk_tmp(drv, create_local_path(_T("SCSI%d.DAT"), drv));
212 #endif
213         }
214         if(bios) {
215                 for(int drv = 0; drv < MAX_DRIVE; drv++) {
216                         bios->set_floppy_disk_handler(drv, fdc->get_disk_handler(drv));
217                 }
218                 for(int drv = 0; drv < USE_HARD_DISK; drv++) {
219                         bios->set_hard_disk_handler(drv, scsi_hdd[drv]->get_disk_handler(0));
220                 }
221         }
222 }
223
224 VM::~VM()
225 {
226         // delete all devices
227         for(DEVICE* device = first_device; device;) {
228                 DEVICE *next_device = device->next_device;
229                 device->release();
230                 delete device;
231                 device = next_device;
232         }
233 }
234
235 DEVICE* VM::get_device(int id)
236 {
237         for(DEVICE* device = first_device; device; device = device->next_device) {
238                 if(device->this_device_id == id) {
239                         return device;
240                 }
241         }
242         return NULL;
243 }
244
245 // ----------------------------------------------------------------------------
246 // drive virtual machine
247 // ----------------------------------------------------------------------------
248
249 void VM::reset()
250 {
251         // reset all devices
252         for(DEVICE* device = first_device; device; device = device->next_device) {
253                 device->reset();
254         }
255         // temporary fix...
256         for(DEVICE* device = first_device; device; device = device->next_device) {
257                 device->reset();
258         }
259         
260         // set devices
261         sio_kb->write_signal(SIG_I8251_DSR, 1, 1);
262         sio_sub->write_signal(SIG_I8251_DSR, 0, 0);
263         
264 #if defined(OPEN_HARD_DISK_IN_RESET)
265         // open/close hard disk images
266         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
267                 if(hd_file_path[drv][0] != _T('\0')) {
268                         open_hard_disk_tmp(drv, hd_file_path[drv]);
269                 } else {
270                         close_hard_disk_tmp(drv);
271                 }
272         }
273 #endif
274 }
275
276 void VM::run()
277 {
278         event->drive();
279 }
280
281 // ----------------------------------------------------------------------------
282 // debugger
283 // ----------------------------------------------------------------------------
284
285 #ifdef USE_DEBUGGER
286 DEVICE *VM::get_cpu(int index)
287 {
288         if(index == 0) {
289                 return cpu;
290         }
291         return NULL;
292 }
293 #endif
294
295 // ----------------------------------------------------------------------------
296 // draw screen
297 // ----------------------------------------------------------------------------
298
299 void VM::draw_screen()
300 {
301         memory->draw_screen();
302 }
303
304 // ----------------------------------------------------------------------------
305 // soud manager
306 // ----------------------------------------------------------------------------
307
308 void VM::initialize_sound(int rate, int samples)
309 {
310         // init sound manager
311         event->initialize_sound(rate, samples);
312         
313         // init sound gen
314         psg->initialize_sound(rate, 125000, 10000);
315 }
316
317 uint16_t* VM::create_sound(int* extra_frames)
318 {
319         return event->create_sound(extra_frames);
320 }
321
322 int VM::get_sound_buffer_ptr()
323 {
324         return event->get_sound_buffer_ptr();
325 }
326
327 #ifdef USE_SOUND_VOLUME
328 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
329 {
330         if(ch == 0) {
331                 psg->set_volume(0, decibel_l, decibel_r);
332         } else if(ch == 1) {
333                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
334                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
335                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
336         }
337 }
338 #endif
339
340 // ----------------------------------------------------------------------------
341 // notify key
342 // ----------------------------------------------------------------------------
343
344 void VM::key_down(int code, bool repeat)
345 {
346         keyboard->key_down(code);
347 }
348
349 void VM::key_up(int code)
350 {
351         keyboard->key_up(code);
352 }
353
354 // ----------------------------------------------------------------------------
355 // user interface
356 // ----------------------------------------------------------------------------
357
358 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
359 {
360         fdc->open_disk(drv, file_path, bank);
361         floppy->change_disk(drv);
362 }
363
364 void VM::close_floppy_disk(int drv)
365 {
366         fdc->close_disk(drv);
367 }
368
369 bool VM::is_floppy_disk_inserted(int drv)
370 {
371         return fdc->is_disk_inserted(drv);
372 }
373
374 void VM::is_floppy_disk_protected(int drv, bool value)
375 {
376         fdc->is_disk_protected(drv, value);
377 }
378
379 bool VM::is_floppy_disk_protected(int drv)
380 {
381         return fdc->is_disk_protected(drv);
382 }
383
384 uint32_t VM::is_floppy_disk_accessed()
385 {
386         uint32_t status = fdc->read_signal(0);
387         if(bios) {
388                 status |= bios->read_signal(0);
389         }
390         return status;
391 }
392
393 void VM::open_hard_disk(int drv, const _TCHAR* file_path)
394 {
395         if(drv < USE_HARD_DISK) {
396 #if defined(OPEN_HARD_DISK_IN_RESET)
397                 my_tcscpy_s(hd_file_path[drv], _MAX_PATH, file_path);
398 #else
399                 open_hard_disk_tmp(drv, file_path);
400 #endif
401         }
402 }
403
404 void VM::close_hard_disk(int drv)
405 {
406         if(drv < USE_HARD_DISK) {
407 #if defined(OPEN_HARD_DISK_IN_RESET)
408                 hd_file_path[drv][0] = _T('\0');
409 #else
410                 close_hard_disk_tmp(drv);
411 #endif
412         }
413 }
414
415 bool VM::is_hard_disk_inserted(int drv)
416 {
417         if(drv < USE_HARD_DISK) {
418 #if defined(OPEN_HARD_DISK_IN_RESET)
419                 return (hd_file_path[drv][0] != _T('\0'));
420 #else
421                 return is_hard_disk_inserted_tmp(drv);
422 #endif
423         }
424         return false;
425 }
426
427 uint32_t VM::is_hard_disk_accessed()
428 {
429         uint32_t status = 0;
430         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
431                 if(scsi_hdd[drv]->get_disk_handler(0)->accessed()) {
432                         status |= 1 << drv;
433                 }
434         }
435         return status;
436 }
437
438 void VM::open_hard_disk_tmp(int drv, const _TCHAR* file_path)
439 {
440         if(drv < USE_HARD_DISK) {
441                 scsi_hdd[drv]->get_disk_handler(0)->open(file_path, 512);
442         }
443 }
444
445 void VM::close_hard_disk_tmp(int drv)
446 {
447         if(drv < USE_HARD_DISK) {
448                 scsi_hdd[drv]->get_disk_handler(0)->close();
449         }
450 }
451
452 bool VM::is_hard_disk_inserted_tmp(int drv)
453 {
454         if(drv < USE_HARD_DISK) {
455                 return scsi_hdd[drv]->get_disk_handler(0)->mounted();
456         }
457         return false;
458 }
459
460 bool VM::is_frame_skippable()
461 {
462         return event->is_frame_skippable();
463 }
464
465 void VM::update_config()
466 {
467         for(DEVICE* device = first_device; device; device = device->next_device) {
468                 device->update_config();
469         }
470 }
471
472 #define STATE_VERSION   7
473
474 #include "../../statesub.h"
475 #include "../../qt/gui/csp_logger.h"
476 extern CSP_Logger DLL_PREFIX_I *csp_logger;
477
478 void VM::decl_state(void)
479 {
480         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FMR30_SERIES_HEAD")), csp_logger);
481
482         for(DEVICE* device = first_device; device; device = device->next_device) {
483                 device->decl_state();
484         }
485 }
486
487 void VM::save_state(FILEIO* state_fio)
488 {
489         //state_fio->FputUint32(STATE_VERSION);
490         if(state_entry != NULL) {
491                 state_entry->save_state(state_fio);
492         }
493         for(DEVICE* device = first_device; device; device = device->next_device) {
494                 device->save_state(state_fio);
495         }
496 }
497
498 bool VM::load_state(FILEIO* state_fio)
499 {
500         //if(state_fio->FgetUint32() != STATE_VERSION) {
501         //      return false;
502         //}
503         bool mb = false;
504         if(state_entry != NULL) {
505                 mb = state_entry->load_state(state_fio);
506         }
507         if(!mb) {
508                 emu->out_debug_log("INFO: HEADER DATA ERROR");
509                 return false;
510         }
511
512         for(DEVICE* device = first_device; device; device = device->next_device) {
513                 if(!device->load_state(state_fio)) {
514                         return false;
515                 }
516         }
517         return true;
518 }
519