OSDN Git Service

[VM][General] Merge Upstream 2017-03-30.
[csp-qt/common_source_project-fm7.git] / source / src / vm / fmr50 / fmr50.cpp
1 /*
2         FUJITSU FMR-50 Emulator 'eFMR-50'
3         FUJITSU FMR-60 Emulator 'eFMR-60'
4
5         Author : Takeda.Toshiya
6         Date   : 2008.04.28 -
7
8         [ virtual machine ]
9 */
10
11 #include "fmr50.h"
12 #include "../../emu.h"
13 #include "../device.h"
14 #include "../event.h"
15
16 #include "../hd46505.h"
17 #ifdef _FMR60
18 #include "../hd63484.h"
19 #endif
20 #include "../i8251.h"
21 #include "../i8253.h"
22 #include "../i8259.h"
23 #if defined(HAS_I286)
24 #include "../i286.h"
25 #else
26 #include "../i386.h"
27 #endif
28 #include "../io.h"
29 #include "../mb8877.h"
30 #include "../msm58321.h"
31 #include "../noise.h"
32 #include "../pcm1bit.h"
33 #include "../scsi_hdd.h"
34 #include "../scsi_host.h"
35 #include "../upd71071.h"
36
37 #ifdef USE_DEBUGGER
38 #include "../debugger.h"
39 #endif
40
41 #include "bios.h"
42 #include "cmos.h"
43 #include "floppy.h"
44 #include "keyboard.h"
45 #include "memory.h"
46 #include "scsi.h"
47 //#include "serial.h"
48 #include "timer.h"
49
50 // ----------------------------------------------------------------------------
51 // initialize
52 // ----------------------------------------------------------------------------
53
54 VM::VM(EMU* parent_emu) : emu(parent_emu)
55 {
56 /*
57         Machine ID & CPU ID
58
59         FMR-50FD/HD/LT  0xF8
60         FMR-50FX/HX     0xE0
61         FMR-50SFX/SHX   0xE8
62         FMR-50LT        0xF8
63         FMR-50NBX       0x28
64         FMR-50NB        0x60
65         FMR-50NE/T      0x08
66         FMR-CARD        0x70
67
68         80286           0x00
69         80386           0x01
70         80386SX         0x03
71         80486           0x02
72 */
73         static const int cpu_clock[] = {
74 #if defined(HAS_I286)
75                  8000000, 12000000
76 #elif defined(HAS_I386)
77                 16000000, 20000000
78 #elif defined(HAS_I486)
79                 20000000, 25000000
80 #endif
81         };
82         
83 #if defined(_FMR60) && (defined(HAS_I386) || defined(HAS_I486) || defined(HAS_PENTIUM))
84         uint8_t machine_id = 0xf0;      // FMR-70/80
85 #else
86         uint8_t machine_id = 0xf8;      // FMR-50/60
87 #endif
88         
89         FILEIO* fio = new FILEIO();
90         if(fio->Fopen(create_local_path(_T("MACHINE.ID")), FILEIO_READ_BINARY)) {
91                 machine_id = fio->Fgetc();
92                 fio->Fclose();
93         }
94         delete fio;
95         
96         machine_id &= ~7;
97 #if defined(HAS_I286)
98         machine_id |= 0;        // 286
99 #elif defined(HAS_I386)
100 //      machine_id |= 1;        // 386DX
101         machine_id |= 3;        // 386SX
102 #elif defined(HAS_I486)
103         machine_id |= 2;        // 486SX/DX
104 #endif
105         
106         // create devices
107         first_device = last_device = NULL;
108         dummy = new DEVICE(this, emu);  // must be 1st device
109         event = new EVENT(this, emu);   // must be 2nd device
110         dummy->set_device_name(_T("1st Dummy"));
111         event->set_device_name(_T("EVENT"));
112
113 #if defined(HAS_I286)
114         cpu = new I286(this, emu);
115 #else
116         cpu = new I386(this, emu);
117 #endif
118 #if defined(HAS_I286)
119         cpu->set_device_name(_T("CPU(i286)"));
120 #elif defined(HAS_I386)
121         cpu->set_device_name(_T("CPU(i386)"));
122 #elif defined(HAS_I486)
123         cpu->set_device_name(_T("CPU(i486)"));
124 #elif defined(HAS_PENTIUM)
125         cpu->set_device_name(_T("CPU(Pentium)"));
126 #endif
127         crtc = new HD46505(this, emu);
128 #ifdef _FMR60
129         acrtc = new HD63484(this, emu);
130 #endif
131         
132         sio = new I8251(this, emu);
133         pit0 = new I8253(this, emu);
134         pit0->set_device_name(_T("8253 PIT #0"));
135         pit1 = new I8253(this, emu);
136         pit1->set_device_name(_T("8253 PIT #1"));
137         pic = new I8259(this, emu);
138         io = new IO(this, emu);
139         fdc = new MB8877(this, emu);
140         fdc->set_context_noise_seek(new NOISE(this, emu));
141         fdc->set_context_noise_head_down(new NOISE(this, emu));
142         fdc->set_context_noise_head_up(new NOISE(this, emu));
143         rtc = new MSM58321(this, emu);
144         pcm = new PCM1BIT(this, emu);
145         pit0->set_device_name(_T("i8253 PIT #0"));
146         pit1->set_device_name(_T("i8253 PIT #1"));
147         
148         scsi_host = new SCSI_HOST(this, emu);
149         for(int i = 0; i < 7; i++) {
150                 if(FILEIO::IsFileExisting(create_local_path(_T("SCSI%d.DAT"), i))) {
151                         scsi_hdd[i] = new SCSI_HDD(this, emu);
152                         scsi_hdd[i]->set_device_name(_T("SCSI Hard Disk Drive #%d"), i + 1);
153                         scsi_hdd[i]->scsi_id = i;
154                         scsi_hdd[i]->set_context_interface(scsi_host);
155                         scsi_host->set_context_target(scsi_hdd[i]);
156                 } else {
157                         scsi_hdd[i] = NULL;
158                 }
159         }
160         dma = new UPD71071(this, emu);
161         if(FILEIO::IsFileExisting(create_local_path(_T("IPL.ROM")))) {
162                 bios = NULL;
163         } else {
164                 bios = new BIOS(this, emu);
165         }
166         cmos = new CMOS(this, emu);
167         floppy = new FLOPPY(this, emu);
168         keyboard = new KEYBOARD(this, emu);
169         memory = new MEMORY(this, emu);
170         scsi = new SCSI(this, emu);
171 //      serial = new SERIAL(this, emu);
172         timer = new TIMER(this, emu);
173         // set contexts
174         event->set_context_cpu(cpu, cpu_clock[config.cpu_type & 1]);
175         event->set_context_sound(pcm);
176         event->set_context_sound(fdc->get_context_noise_seek());
177         event->set_context_sound(fdc->get_context_noise_head_down());
178         event->set_context_sound(fdc->get_context_noise_head_up());
179         
180 /*      pic     0       timer
181                 1       keyboard
182                 2       rs-232c
183                 3       ex rs-232c
184                 4       (option)
185                 5       (option)
186                 6       floppy drive or dma ???
187                 7       (slave)
188                 8       scsi
189                 9       (option)
190                 10      (option)
191                 11      (option)
192                 12      printer
193                 13      (option)
194                 14      (option)
195                 15      (reserve)
196
197         dma     0       floppy drive
198                 1       hard drive
199                 2       (option)
200                 3       (reserve)
201 */
202         crtc->set_context_disp(memory, SIG_MEMORY_DISP, 1);
203         crtc->set_context_vsync(memory, SIG_MEMORY_VSYNC, 1);
204 #ifdef _FMR60
205         acrtc->set_vram_ptr((uint16_t*)memory->get_vram(), 0x80000);
206 #endif
207         pit0->set_context_ch0(timer, SIG_TIMER_CH0, 1);
208         pit0->set_context_ch1(timer, SIG_TIMER_CH1, 1);
209         pit0->set_context_ch2(pcm, SIG_PCM1BIT_SIGNAL, 1);
210         pit0->set_constant_clock(0, 307200);
211         pit0->set_constant_clock(1, 307200);
212         pit0->set_constant_clock(2, 307200);
213         pit1->set_constant_clock(1, 1228800);
214         pic->set_context_cpu(cpu);
215         fdc->set_context_drq(dma, SIG_UPD71071_CH0, 1);
216         fdc->set_context_irq(floppy, SIG_FLOPPY_IRQ, 1);
217         rtc->set_context_data(timer, SIG_TIMER_RTC, 0x0f, 0);
218         rtc->set_context_busy(timer, SIG_TIMER_RTC, 0x80);
219         scsi_host->set_context_irq(scsi, SIG_SCSI_IRQ, 1);
220         scsi_host->set_context_drq(scsi, SIG_SCSI_DRQ, 1);
221         dma->set_context_memory(memory);
222         dma->set_context_ch0(fdc);
223         dma->set_context_ch1(scsi_host);
224         
225         floppy->set_context_fdc(fdc);
226         floppy->set_context_pic(pic);
227         keyboard->set_context_pic(pic);
228         memory->set_context_cpu(cpu);
229         memory->set_machine_id(machine_id);
230         memory->set_context_crtc(crtc);
231         memory->set_chregs_ptr(crtc->get_regs());
232         scsi->set_context_dma(dma);
233         scsi->set_context_pic(pic);
234         scsi->set_context_host(scsi_host);
235         timer->set_context_pcm(pcm);
236         timer->set_context_pic(pic);
237         timer->set_context_rtc(rtc);
238         
239         // cpu bus
240         cpu->set_context_mem(memory);
241         cpu->set_context_io(io);
242         cpu->set_context_intr(pic);
243         if(bios) {
244                 bios->set_context_mem(memory);
245                 bios->set_context_io(io);
246                 bios->set_cmos_ptr(cmos->get_cmos());
247                 bios->set_vram_ptr(memory->get_vram());
248                 bios->set_cvram_ptr(memory->get_cvram());
249 #ifdef _FMR60
250                 bios->set_avram_ptr(memory->get_avram());
251 #else
252                 bios->set_kvram_ptr(memory->get_kvram());
253 #endif
254                 cpu->set_context_bios(bios);
255         }
256 #ifdef SINGLE_MODE_DMA
257         cpu->set_context_dma(dma);
258 #endif
259 #ifdef USE_DEBUGGER
260         cpu->set_context_debugger(new DEBUGGER(this, emu));
261 #endif
262         
263         // i/o bus
264         io->set_iomap_alias_rw(0x00, pic, I8259_ADDR_CHIP0 | 0);
265         io->set_iomap_alias_rw(0x02, pic, I8259_ADDR_CHIP0 | 1);
266         io->set_iomap_alias_rw(0x10, pic, I8259_ADDR_CHIP1 | 0);
267         io->set_iomap_alias_rw(0x12, pic, I8259_ADDR_CHIP1 | 1);
268         io->set_iomap_single_rw(0x20, memory);  // reset
269         io->set_iomap_single_r(0x21, memory);   // cpu misc
270         io->set_iomap_single_w(0x22, memory);   // dma
271         io->set_iomap_single_rw(0x24, memory);  // dma
272         io->set_iomap_single_r(0x26, timer);
273         io->set_iomap_single_r(0x27, timer);
274         io->set_iomap_single_r(0x30, memory);   // cpu id
275         io->set_iomap_alias_rw(0x40, pit0, 0);
276         io->set_iomap_alias_rw(0x42, pit0, 1);
277         io->set_iomap_alias_rw(0x44, pit0, 2);
278         io->set_iomap_alias_rw(0x46, pit0, 3);
279         io->set_iomap_alias_rw(0x50, pit1, 0);
280         io->set_iomap_alias_rw(0x52, pit1, 1);
281         io->set_iomap_alias_rw(0x54, pit1, 2);
282         io->set_iomap_alias_rw(0x56, pit1, 3);
283         io->set_iomap_single_rw(0x60, timer);
284         io->set_iomap_single_rw(0x70, timer);
285         io->set_iomap_single_w(0x80, timer);
286 #ifdef _FMRCARD
287         io->set_iomap_single_w(0x90, cmos);
288 #endif
289         io->set_iomap_range_rw(0xa0, 0xaf, dma);
290         io->set_iomap_alias_rw(0x200, fdc, 0);
291         io->set_iomap_alias_rw(0x202, fdc, 1);
292         io->set_iomap_alias_rw(0x204, fdc, 2);
293         io->set_iomap_alias_rw(0x206, fdc, 3);
294         io->set_iomap_single_rw(0x208, floppy);
295         io->set_iomap_single_rw(0x20c, floppy);
296         io->set_iomap_single_rw(0x400, memory); // crtc
297         io->set_iomap_single_rw(0x402, memory); // crtc
298         io->set_iomap_single_rw(0x404, memory); // crtc
299         io->set_iomap_single_w(0x408, memory);  // crtc
300         io->set_iomap_single_rw(0x40a, memory); // crtc
301         io->set_iomap_single_rw(0x40c, memory); // crtc
302         io->set_iomap_single_rw(0x40e, memory); // crtc
303         io->set_iomap_alias_rw(0x500, crtc, 0);
304         io->set_iomap_alias_rw(0x502, crtc, 1);
305 #ifdef _FMR60
306         io->set_iomap_range_rw(0x520, 0x523, acrtc);
307 #endif
308         io->set_iomap_single_rw(0x600, keyboard);
309         io->set_iomap_single_rw(0x602, keyboard);
310         io->set_iomap_single_rw(0x604, keyboard);
311         io->set_iomap_alias_rw(0xa00, sio, 0);
312         io->set_iomap_alias_rw(0xa02, sio, 1);
313 //      io->set_iomap_single_r(0xa04, serial);
314 //      io->set_iomap_single_r(0xa06, serial);
315 //      io->set_iomap_single_w(0xa08, serial);
316         io->set_iomap_single_rw(0xc30, scsi);
317         io->set_iomap_single_rw(0xc32, scsi);
318         io->set_iomap_range_rw(0x3000, 0x3fff, cmos);
319         io->set_iomap_range_rw(0xfd98, 0xfd9f, memory); // crtc
320         io->set_iomap_single_rw(0xfda0, memory);        // crtc
321         
322         // initialize all devices
323         for(DEVICE* device = first_device; device; device = device->next_device) {
324                 device->initialize();
325         }
326         if(bios) {
327                 for(int i = 0; i < MAX_DRIVE; i++) {
328                         bios->set_disk_handler(i, fdc->get_disk_handler(i));
329                 }
330         }
331 }
332
333 VM::~VM()
334 {
335         // delete all devices
336         for(DEVICE* device = first_device; device;) {
337                 DEVICE *next_device = device->next_device;
338                 device->release();
339                 delete device;
340                 device = next_device;
341         }
342 }
343
344 DEVICE* VM::get_device(int id)
345 {
346         for(DEVICE* device = first_device; device; device = device->next_device) {
347                 if(device->this_device_id == id) {
348                         return device;
349                 }
350         }
351         return NULL;
352 }
353
354 // ----------------------------------------------------------------------------
355 // drive virtual machine
356 // ----------------------------------------------------------------------------
357
358 void VM::reset()
359 {
360         // reset all devices
361         for(DEVICE* device = first_device; device; device = device->next_device) {
362                 device->reset();
363         }
364         // temporary fix...
365         for(DEVICE* device = first_device; device; device = device->next_device) {
366                 device->reset();
367         }
368 }
369
370 void VM::run()
371 {
372         event->drive();
373 }
374
375 // ----------------------------------------------------------------------------
376 // debugger
377 // ----------------------------------------------------------------------------
378
379 #ifdef USE_DEBUGGER
380 DEVICE *VM::get_cpu(int index)
381 {
382         if(index == 0) {
383                 return cpu;
384         }
385         return NULL;
386 }
387 #endif
388
389 // ----------------------------------------------------------------------------
390 // draw screen
391 // ----------------------------------------------------------------------------
392
393 void VM::draw_screen()
394 {
395         memory->draw_screen();
396 }
397
398 // ----------------------------------------------------------------------------
399 // soud manager
400 // ----------------------------------------------------------------------------
401
402 void VM::initialize_sound(int rate, int samples)
403 {
404         // init sound manager
405         event->initialize_sound(rate, samples);
406         
407         // init sound gen
408         pcm->initialize_sound(rate, 8000);
409 }
410
411 uint16_t* VM::create_sound(int* extra_frames)
412 {
413         return event->create_sound(extra_frames);
414 }
415
416 int VM::get_sound_buffer_ptr()
417 {
418         return event->get_sound_buffer_ptr();
419 }
420
421 #ifdef USE_SOUND_VOLUME
422 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
423 {
424         if(ch == 0) {
425                 pcm->set_volume(0, decibel_l, decibel_r);
426         } else if(ch == 1) {
427                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
428                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
429                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
430         }
431 }
432 #endif
433
434 // ----------------------------------------------------------------------------
435 // notify key
436 // ----------------------------------------------------------------------------
437
438 void VM::key_down(int code, bool repeat)
439 {
440         keyboard->key_down(code);
441 }
442
443 void VM::key_up(int code)
444 {
445         keyboard->key_up(code);
446 }
447
448 // ----------------------------------------------------------------------------
449 // user interface
450 // ----------------------------------------------------------------------------
451
452 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
453 {
454         fdc->open_disk(drv, file_path, bank);
455         floppy->change_disk(drv);
456 }
457
458 void VM::close_floppy_disk(int drv)
459 {
460         fdc->close_disk(drv);
461 }
462
463 bool VM::is_floppy_disk_inserted(int drv)
464 {
465         return fdc->is_disk_inserted(drv);
466 }
467
468 void VM::is_floppy_disk_protected(int drv, bool value)
469 {
470         fdc->is_disk_protected(drv, value);
471 }
472
473 bool VM::is_floppy_disk_protected(int drv)
474 {
475         return fdc->is_disk_protected(drv);
476 }
477
478 uint32_t VM::is_floppy_disk_accessed()
479 {
480         uint32_t status = fdc->read_signal(0);
481         if(bios) {
482                 status |= bios->read_signal(0);
483         }
484         return status;
485 }
486
487 uint32_t VM::is_hard_disk_accessed()
488 {
489         uint32_t status = 0;
490         for(int i = 0; i < 7; i++) {
491                 if(scsi_hdd[i] != NULL && scsi_hdd[i]->read_signal(0) != 0) {
492                         status |= 1 << i;
493                 }
494         }
495         if(bios) {
496                 status |= bios->read_signal(1);
497         }
498         return status;
499 }
500
501 bool VM::is_frame_skippable()
502 {
503         return event->is_frame_skippable();
504 }
505
506 void VM::update_config()
507 {
508         for(DEVICE* device = first_device; device; device = device->next_device) {
509                 device->update_config();
510         }
511 }
512
513 #define STATE_VERSION   4
514
515 void VM::save_state(FILEIO* state_fio)
516 {
517         state_fio->FputUint32(STATE_VERSION);
518         
519         for(DEVICE* device = first_device; device; device = device->next_device) {
520                 device->save_state(state_fio);
521         }
522 }
523
524 bool VM::load_state(FILEIO* state_fio)
525 {
526         if(state_fio->FgetUint32() != STATE_VERSION) {
527                 return false;
528         }
529         for(DEVICE* device = first_device; device; device = device->next_device) {
530                 if(!device->load_state(state_fio)) {
531                         return false;
532                 }
533         }
534         return true;
535 }
536