OSDN Git Service

471e9a825449f3e439a4dc40bf6a47ddeb48b4e1
[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 "../pcm1bit.h"
32 #include "../upd71071.h"
33
34 #ifdef USE_DEBUGGER
35 #include "../debugger.h"
36 #endif
37
38 #include "bios.h"
39 #include "cmos.h"
40 #include "floppy.h"
41 #include "keyboard.h"
42 #include "memory.h"
43 #include "scsi.h"
44 //#include "serial.h"
45 #include "timer.h"
46
47 // ----------------------------------------------------------------------------
48 // initialize
49 // ----------------------------------------------------------------------------
50
51 VM::VM(EMU* parent_emu) : emu(parent_emu)
52 {
53 /*
54         Machine ID & CPU ID
55
56         FMR-50FD/HD/LT  0xF8
57         FMR-50FX/HX     0xE0
58         FMR-50SFX/SHX   0xE8
59         FMR-50LT        0xF8
60         FMR-50NBX       0x28
61         FMR-50NB        0x60
62         FMR-50NE/T      0x08
63         FMR-CARD        0x70
64
65         80286           0x00
66         80386           0x01
67         80386SX         0x03
68         80486           0x02
69 */
70         static const int cpu_clock[] = {
71 #if defined(HAS_I286)
72                  8000000, 12000000
73 #elif defined(HAS_I386)
74                 16000000, 20000000
75 #elif defined(HAS_I486)
76                 20000000, 25000000
77 #endif
78         };
79         
80 #if defined(_FMR60) && (defined(HAS_I386) || defined(HAS_I486) || defined(HAS_PENTIUM))
81         uint8 machine_id = 0xf0;        // FMR-70/80
82 #else
83         uint8 machine_id = 0xf8;        // FMR-50/60
84 #endif
85         
86         FILEIO* fio = new FILEIO();
87         if(fio->Fopen(emu->bios_path(_T("MACHINE.ID")), FILEIO_READ_BINARY)) {
88                 machine_id = fio->Fgetc();
89                 fio->Fclose();
90         }
91         delete fio;
92         
93         machine_id &= ~7;
94 #if defined(HAS_I286)
95         machine_id |= 0;        // 286
96 #elif defined(HAS_I386)
97 //      machine_id |= 1;        // 386DX
98         machine_id |= 3;        // 386SX
99 #elif defined(HAS_I486)
100         machine_id |= 2;        // 486SX/DX
101 #endif
102         
103         // create devices
104         first_device = last_device = NULL;
105         dummy = new DEVICE(this, emu);  // must be 1st device
106         event = new EVENT(this, emu);   // must be 2nd device
107         
108 #if defined(HAS_I286)
109         cpu = new I286(this, emu);
110 #else
111         cpu = new I386(this, emu);
112 #endif
113         crtc = new HD46505(this, emu);
114 #ifdef _FMR60
115         acrtc = new HD63484(this, emu);
116 #endif
117         sio = new I8251(this, emu);
118         pit0 = new I8253(this, emu);
119         pit1 = new I8253(this, emu);
120         pic = new I8259(this, emu);
121         io = new IO(this, emu);
122         fdc = new MB8877(this, emu);
123         rtc = new MSM58321(this, emu);
124         pcm = new PCM1BIT(this, emu);
125         dma = new UPD71071(this, emu);
126         
127         bios = new BIOS(this, emu);
128         cmos = new CMOS(this, emu);
129         floppy = new FLOPPY(this, emu);
130         keyboard = new KEYBOARD(this, emu);
131         memory = new MEMORY(this, emu);
132         scsi = new SCSI(this, emu);
133 //      serial = new SERIAL(this, emu);
134         timer = new TIMER(this, emu);
135         
136         // set contexts
137         event->set_context_cpu(cpu, cpu_clock[config.cpu_type & 1]);
138         event->set_context_sound(pcm);
139         
140 /*      pic     0       timer
141                 1       keyboard
142                 2       rs-232c
143                 3       ex rs-232c
144                 4       (option)
145                 5       (option)
146                 6       floppy drive or dma ???
147                 7       (slave)
148                 8       scsi
149                 9       (option)
150                 10      (option)
151                 11      (option)
152                 12      printer
153                 13      (option)
154                 14      (option)
155                 15      (reserve)
156
157         dma     0       floppy drive
158                 1       hard drive
159                 2       (option)
160                 3       (reserve)
161 */
162         crtc->set_context_disp(memory, SIG_MEMORY_DISP, 1);
163         crtc->set_context_vsync(memory, SIG_MEMORY_VSYNC, 1);
164 #ifdef _FMR60
165         acrtc->set_vram_ptr((uint16*)memory->get_vram(), 0x80000);
166 #endif
167         pit0->set_context_ch0(timer, SIG_TIMER_CH0, 1);
168         pit0->set_context_ch1(timer, SIG_TIMER_CH1, 1);
169         pit0->set_context_ch2(pcm, SIG_PCM1BIT_SIGNAL, 1);
170         pit0->set_constant_clock(0, 307200);
171         pit0->set_constant_clock(1, 307200);
172         pit0->set_constant_clock(2, 307200);
173         pit1->set_constant_clock(1, 1228800);
174         pic->set_context_cpu(cpu);
175         fdc->set_context_drq(dma, SIG_UPD71071_CH0, 1);
176         fdc->set_context_irq(floppy, SIG_FLOPPY_IRQ, 1);
177         rtc->set_context_data(timer, SIG_TIMER_RTC, 0x0f, 0);
178         rtc->set_context_busy(timer, SIG_TIMER_RTC, 0x80);
179         dma->set_context_memory(memory);
180         dma->set_context_ch0(fdc);
181 //      dma->set_context_ch1(scsi);
182         
183         bios->set_context_mem(memory);
184         bios->set_context_io(io);
185         bios->set_cmos_ptr(cmos->get_cmos());
186         bios->set_vram_ptr(memory->get_vram());
187         bios->set_cvram_ptr(memory->get_cvram());
188 #ifdef _FMR60
189         bios->set_avram_ptr(memory->get_avram());
190 #else
191         bios->set_kvram_ptr(memory->get_kvram());
192 #endif
193         floppy->set_context_fdc(fdc);
194         floppy->set_context_pic(pic);
195         keyboard->set_context_pic(pic);
196         memory->set_context_cpu(cpu);
197         memory->set_machine_id(machine_id);
198         memory->set_context_crtc(crtc);
199         memory->set_chregs_ptr(crtc->get_regs());
200 //      scsi->set_context_dma(dma);
201 //      scsi->set_context_pic(pic);
202         timer->set_context_pcm(pcm);
203         timer->set_context_pic(pic);
204         timer->set_context_rtc(rtc);
205         
206         // cpu bus
207         cpu->set_context_mem(memory);
208         cpu->set_context_io(io);
209         cpu->set_context_intr(pic);
210         cpu->set_context_bios(bios);
211 #ifdef SINGLE_MODE_DMA
212         cpu->set_context_dma(dma);
213 #endif
214 #ifdef USE_DEBUGGER
215         cpu->set_context_debugger(new DEBUGGER(this, emu));
216 #endif
217         
218         // i/o bus
219         io->set_iomap_alias_rw(0x00, pic, I8259_ADDR_CHIP0 | 0);
220         io->set_iomap_alias_rw(0x02, pic, I8259_ADDR_CHIP0 | 1);
221         io->set_iomap_alias_rw(0x10, pic, I8259_ADDR_CHIP1 | 0);
222         io->set_iomap_alias_rw(0x12, pic, I8259_ADDR_CHIP1 | 1);
223         io->set_iomap_single_rw(0x20, memory);  // reset
224         io->set_iomap_single_r(0x21, memory);   // cpu misc
225         io->set_iomap_single_w(0x22, memory);   // dma
226         io->set_iomap_single_rw(0x24, memory);  // dma
227         io->set_iomap_single_r(0x26, timer);
228         io->set_iomap_single_r(0x27, timer);
229         io->set_iomap_single_r(0x30, memory);   // cpu id
230         io->set_iomap_alias_rw(0x40, pit0, 0);
231         io->set_iomap_alias_rw(0x42, pit0, 1);
232         io->set_iomap_alias_rw(0x44, pit0, 2);
233         io->set_iomap_alias_rw(0x46, pit0, 3);
234         io->set_iomap_alias_rw(0x50, pit1, 0);
235         io->set_iomap_alias_rw(0x52, pit1, 1);
236         io->set_iomap_alias_rw(0x54, pit1, 2);
237         io->set_iomap_alias_rw(0x56, pit1, 3);
238         io->set_iomap_single_rw(0x60, timer);
239         io->set_iomap_single_rw(0x70, timer);
240         io->set_iomap_single_w(0x80, timer);
241 #ifdef _FMRCARD
242         io->set_iomap_single_w(0x90, cmos);
243 #endif
244         io->set_iomap_range_rw(0xa0, 0xaf, dma);
245         io->set_iomap_alias_rw(0x200, fdc, 0);
246         io->set_iomap_alias_rw(0x202, fdc, 1);
247         io->set_iomap_alias_rw(0x204, fdc, 2);
248         io->set_iomap_alias_rw(0x206, fdc, 3);
249         io->set_iomap_single_rw(0x208, floppy);
250         io->set_iomap_single_rw(0x20c, floppy);
251         io->set_iomap_single_rw(0x400, memory); // crtc
252         io->set_iomap_single_rw(0x402, memory); // crtc
253         io->set_iomap_single_rw(0x404, memory); // crtc
254         io->set_iomap_single_w(0x408, memory);  // crtc
255         io->set_iomap_single_rw(0x40a, memory); // crtc
256         io->set_iomap_single_rw(0x40c, memory); // crtc
257         io->set_iomap_single_rw(0x40e, memory); // crtc
258         io->set_iomap_alias_rw(0x500, crtc, 0);
259         io->set_iomap_alias_rw(0x502, crtc, 1);
260 #ifdef _FMR60
261         io->set_iomap_range_rw(0x520, 0x523, acrtc);
262 #endif
263         io->set_iomap_single_rw(0x600, keyboard);
264         io->set_iomap_single_rw(0x602, keyboard);
265         io->set_iomap_single_rw(0x604, keyboard);
266         io->set_iomap_alias_rw(0xa00, sio, 0);
267         io->set_iomap_alias_rw(0xa02, sio, 1);
268 //      io->set_iomap_single_r(0xa04, serial);
269 //      io->set_iomap_single_r(0xa06, serial);
270 //      io->set_iomap_single_w(0xa08, serial);
271         io->set_iomap_single_rw(0xc30, scsi);
272         io->set_iomap_single_rw(0xc32, scsi);
273         io->set_iomap_range_rw(0x3000, 0x3fff, cmos);
274         io->set_iomap_range_rw(0xfd98, 0xfd9f, memory); // crtc
275         io->set_iomap_single_rw(0xfda0, memory);        // crtc
276         
277         // initialize all devices
278         for(DEVICE* device = first_device; device; device = device->next_device) {
279                 device->initialize();
280         }
281         for(int i = 0; i < MAX_DRIVE; i++) {
282                 bios->set_disk_handler(i, fdc->get_disk_handler(i));
283         }
284 }
285
286 VM::~VM()
287 {
288         // delete all devices
289         for(DEVICE* device = first_device; device;) {
290                 DEVICE *next_device = device->next_device;
291                 device->release();
292                 delete device;
293                 device = next_device;
294         }
295 }
296
297 DEVICE* VM::get_device(int id)
298 {
299         for(DEVICE* device = first_device; device; device = device->next_device) {
300                 if(device->this_device_id == id) {
301                         return device;
302                 }
303         }
304         return NULL;
305 }
306
307 // ----------------------------------------------------------------------------
308 // drive virtual machine
309 // ----------------------------------------------------------------------------
310
311 void VM::reset()
312 {
313         // reset all devices
314         for(DEVICE* device = first_device; device; device = device->next_device) {
315                 device->reset();
316         }
317         // temporary fix...
318         for(DEVICE* device = first_device; device; device = device->next_device) {
319                 device->reset();
320         }
321 }
322
323 void VM::run()
324 {
325         event->drive();
326 }
327
328 // ----------------------------------------------------------------------------
329 // debugger
330 // ----------------------------------------------------------------------------
331
332 #ifdef USE_DEBUGGER
333 DEVICE *VM::get_cpu(int index)
334 {
335         if(index == 0) {
336                 return cpu;
337         }
338         return NULL;
339 }
340 #endif
341
342 // ----------------------------------------------------------------------------
343 // draw screen
344 // ----------------------------------------------------------------------------
345
346 void VM::draw_screen()
347 {
348         memory->draw_screen();
349 }
350
351 int VM::access_lamp()
352 {
353         uint32 status = fdc->read_signal(0) | bios->read_signal(0);
354         return (status & 0x10) ? 4 : (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
355 }
356
357 // ----------------------------------------------------------------------------
358 // soud manager
359 // ----------------------------------------------------------------------------
360
361 void VM::initialize_sound(int rate, int samples)
362 {
363         // init sound manager
364         event->initialize_sound(rate, samples);
365         
366         // init sound gen
367         pcm->init(rate, 8000);
368 }
369
370 uint16* VM::create_sound(int* extra_frames)
371 {
372         return event->create_sound(extra_frames);
373 }
374
375 int VM::sound_buffer_ptr()
376 {
377         return event->sound_buffer_ptr();
378 }
379
380 // ----------------------------------------------------------------------------
381 // notify key
382 // ----------------------------------------------------------------------------
383
384 void VM::key_down(int code, bool repeat)
385 {
386         keyboard->key_down(code);
387 }
388
389 void VM::key_up(int code)
390 {
391         keyboard->key_up(code);
392 }
393
394 // ----------------------------------------------------------------------------
395 // user interface
396 // ----------------------------------------------------------------------------
397
398 void VM::open_disk(int drv, _TCHAR* file_path, int bank)
399 {
400         fdc->open_disk(drv, file_path, bank);
401         floppy->change_disk(drv);
402 }
403
404 void VM::close_disk(int drv)
405 {
406         fdc->close_disk(drv);
407 }
408
409 bool VM::disk_inserted(int drv)
410 {
411         return fdc->disk_inserted(drv);
412 }
413
414 bool VM::now_skip()
415 {
416         return event->now_skip();
417 }
418
419 void VM::update_config()
420 {
421         for(DEVICE* device = first_device; device; device = device->next_device) {
422                 device->update_config();
423         }
424 }
425
426 #define STATE_VERSION   1
427
428 void VM::save_state(FILEIO* state_fio)
429 {
430         state_fio->FputUint32(STATE_VERSION);
431         
432         for(DEVICE* device = first_device; device; device = device->next_device) {
433                 device->save_state(state_fio);
434         }
435 }
436
437 bool VM::load_state(FILEIO* state_fio)
438 {
439         if(state_fio->FgetUint32() != STATE_VERSION) {
440                 return false;
441         }
442         for(DEVICE* device = first_device; device; device = device->next_device) {
443                 if(!device->load_state(state_fio)) {
444                         return false;
445                 }
446         }
447         return true;
448 }
449