OSDN Git Service

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