OSDN Git Service

[VM][General] Merge Upstream 2017-12-15.
[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 "../i8237.h"
16 #include "../i8251.h"
17 #include "../i8253.h"
18 #include "../i8259.h"
19 //#if defined(HAS_I86)
20 //#include "../i86.h"
21 //#else
22 #include "../i286.h"
23 //#endif
24 #include "../io.h"
25 #include "../mb8877.h"
26 #include "../noise.h"
27 #include "../scsi_hdd.h"
28 #include "../scsi_host.h"
29 #include "../sn76489an.h"
30
31 #ifdef USE_DEBUGGER
32 #include "../debugger.h"
33 #endif
34
35 #include "../fmr50/bios.h"
36 #include "cmos.h"
37 #include "floppy.h"
38 #include "keyboard.h"
39 #include "memory.h"
40 #include "rtc.h"
41 #include "scsi.h"
42 #include "serial.h"
43 #include "system.h"
44 #include "timer.h"
45
46 // ----------------------------------------------------------------------------
47 // initialize
48 // ----------------------------------------------------------------------------
49
50 VM::VM(EMU* parent_emu) : emu(parent_emu)
51 {
52         // create devices
53         first_device = last_device = NULL;
54         dummy = new DEVICE(this, emu);  // must be 1st device
55         dummy->set_device_name(_T("1st dummy"));
56         event = new EVENT(this, emu);   // must be 2nd device
57         
58         dma = new I8237(this, emu);
59         sio_kb = new I8251(this, emu);  // keyboard
60         sio_kb->set_device_name(_T("8251 SIO (Keyboard)"));
61         sio_sub = new I8251(this, emu); // sub display
62         sio_sub->set_device_name(_T("8251 SIO (Sub System)"));
63         sio_ch1 = new I8251(this, emu); // RS-232C ch.1
64         sio_ch1->set_device_name(_T("8251 SIO (RS-232C #1)"));
65         sio_ch2 = new I8251(this, emu); // RS-232C ch.2
66         sio_ch2->set_device_name(_T("8251 SIO (RS-232C #2)"));
67         pit = new I8253(this, emu);
68         pic = new I8259(this, emu);
69 //#if defined(HAS_I86)
70 //      cpu = new I86(this, emu);
71 //#else
72         cpu = new I286(this, emu);
73 //#endif
74         io = new IO(this, emu);
75         fdc = new MB8877(this, emu);
76         fdc->set_context_noise_seek(new NOISE(this, emu));
77         fdc->set_context_noise_head_down(new NOISE(this, emu));
78         fdc->set_context_noise_head_up(new NOISE(this, emu));
79         cpu->set_device_name(_T("CPU(80C86)"));
80         
81         scsi_host = new SCSI_HOST(this, emu);
82         for(int i = 0; i < 7; i++) {
83                 if(FILEIO::IsFileExisting(create_local_path(_T("SCSI%d.DAT"), 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_context_interface(scsi_host);
88                         scsi_host->set_context_target(scsi_hdd[i]);
89                 } else {
90                         scsi_hdd[i] = NULL;
91                 }
92         }
93         psg = new SN76489AN(this, emu);
94         if(FILEIO::IsFileExisting(create_local_path(_T("IPL.ROM")))) {
95                 bios = NULL;
96         } else {
97                 bios = new BIOS(this, emu);
98         }
99         cmos = new CMOS(this, emu);
100         floppy = new FLOPPY(this, emu);
101         keyboard = new KEYBOARD(this, emu);
102         memory = new MEMORY(this, emu);
103         rtc = new RTC(this, emu);
104         scsi = new SCSI(this, emu);
105         serial = new SERIAL(this, emu);
106         system = new SYSTEM(this, emu);
107         timer = new TIMER(this, emu);
108         // set contexts
109         event->set_context_cpu(cpu);
110         event->set_context_sound(psg);
111         event->set_context_sound(fdc->get_context_noise_seek());
112         event->set_context_sound(fdc->get_context_noise_head_down());
113         event->set_context_sound(fdc->get_context_noise_head_up());
114         
115         dma->set_context_memory(memory);
116         dma->set_context_ch0(fdc);
117         dma->set_context_ch1(scsi_host);
118         dma->set_context_tc1(scsi, SIG_SCSI_TC, 1);
119         sio_kb->set_context_rxrdy(serial, SIG_SERIAL_RXRDY_KB, 1);
120         sio_kb->set_context_txrdy(serial, SIG_SERIAL_TXRDY_KB, 1);
121         sio_sub->set_context_rxrdy(serial, SIG_SERIAL_RXRDY_SUB, 1);
122         sio_sub->set_context_txrdy(serial, SIG_SERIAL_TXRDY_SUB, 1);
123         sio_ch1->set_context_rxrdy(serial, SIG_SERIAL_RXRDY_CH1, 1);
124         sio_ch1->set_context_txrdy(serial, SIG_SERIAL_TXRDY_CH1, 1);
125         sio_ch2->set_context_rxrdy(serial, SIG_SERIAL_RXRDY_CH2, 1);
126         sio_ch2->set_context_txrdy(serial, SIG_SERIAL_TXRDY_CH2, 1);
127         pit->set_context_ch0(timer, SIG_TIMER_CH0, 1);
128         pit->set_context_ch1(timer, SIG_TIMER_CH1, 1);
129         pit->set_constant_clock(0, 1000000);
130         pit->set_constant_clock(1, 1000000);
131         pic->set_context_cpu(cpu);
132         fdc->set_context_drq(dma, SIG_I8237_CH0, 1);
133         fdc->set_context_irq(floppy, SIG_FLOPPY_IRQ, 1);
134         scsi_host->set_context_irq(scsi, SIG_SCSI_IRQ, 1);
135         scsi_host->set_context_drq(scsi, SIG_SCSI_DRQ, 1);
136         
137         floppy->set_context_fdc(fdc);
138         floppy->set_context_pic(pic);
139         keyboard->set_context_sio(sio_kb);
140         memory->set_context_cpu(cpu);
141         memory->set_context_dma(dma);
142         rtc->set_context_pic(pic);
143         scsi->set_context_dma(dma);
144         scsi->set_context_pic(pic);
145         scsi->set_context_host(scsi_host);
146         serial->set_context_pic(pic);
147         serial->set_context_sio(sio_kb, sio_sub, sio_ch1, sio_ch2);
148         timer->set_context_pic(pic);
149         
150         // cpu bus
151         cpu->set_context_mem(memory);
152         cpu->set_context_io(io);
153         cpu->set_context_intr(pic);
154         if(bios) {
155                 bios->set_context_mem(memory);
156                 bios->set_context_io(io);
157                 bios->set_cmos_ptr(cmos->get_cmos());
158                 bios->set_vram_ptr(memory->get_vram());
159                 bios->set_cvram_ptr(memory->get_cvram());
160                 bios->set_kvram_ptr(memory->get_kvram());
161                 cpu->set_context_bios(bios);
162         }
163 #ifdef SINGLE_MODE_DMA
164         cpu->set_context_dma(dma);
165 #endif
166 #ifdef USE_DEBUGGER
167         cpu->set_context_debugger(new DEBUGGER(this, emu));
168 #endif
169         
170         // i/o bus
171         io->set_iomap_range_rw(0x00, 0x07, rtc);
172         io->set_iomap_range_rw(0x08, 0x09, sio_kb);
173         io->set_iomap_range_rw(0x0a, 0x0b, serial);
174         io->set_iomap_range_rw(0x10, 0x11, sio_sub);
175         io->set_iomap_range_rw(0x12, 0x13, serial);
176         io->set_iomap_single_r(0x18, system);
177         io->set_iomap_range_rw(0x1d, 0x1e, memory);
178         io->set_iomap_range_r(0x20, 0x21, system);
179         io->set_iomap_single_rw(0x26, memory);
180         io->set_iomap_range_rw(0x30, 0x33, fdc);
181         io->set_iomap_range_rw(0x34, 0x36, floppy);
182         io->set_iomap_single_w(0x40, psg);
183         io->set_iomap_range_rw(0x42, 0x43, timer);
184         io->set_iomap_range_rw(0x46, 0x47, system);
185         io->set_iomap_range_rw(0x60, 0x61, sio_ch1);
186         io->set_iomap_range_rw(0x62, 0x66, serial);
187         io->set_iomap_range_rw(0x70, 0x71, sio_ch2);
188         io->set_iomap_range_rw(0x72, 0x76, serial);
189         io->set_iomap_alias_rw(0x100, pic, I8259_ADDR_CHIP0 | 0);
190         io->set_iomap_alias_rw(0x101, pic, I8259_ADDR_CHIP0 | 1);
191         io->set_iomap_alias_rw(0x108, pic, I8259_ADDR_CHIP1 | 0);
192         io->set_iomap_alias_rw(0x10a, pic, I8259_ADDR_CHIP1 | 1);
193         io->set_iomap_range_rw(0x110, 0x11f, dma);
194         io->set_iomap_range_w(0x120, 0x123, memory);
195         io->set_iomap_range_rw(0x130, 0x133, pit);
196         io->set_iomap_range_rw(0x2f0, 0x2f3, scsi);
197         io->set_iomap_range_rw(0x300, 0x30f, memory);
198         io->set_iomap_range_rw(0xc000, 0xdfff, cmos);
199         io->set_iomap_single_rw(0xff00, system);
200         
201         // initialize all devices
202         for(DEVICE* device = first_device; device; device = device->next_device) {
203                 device->initialize();
204         }
205         if(bios) {
206                 for(int i = 0; i < MAX_DRIVE; i++) {
207                         bios->set_disk_handler(i, fdc->get_disk_handler(i));
208                 }
209         }
210 }
211
212 VM::~VM()
213 {
214         // delete all devices
215         for(DEVICE* device = first_device; device;) {
216                 DEVICE *next_device = device->next_device;
217                 device->release();
218                 delete device;
219                 device = next_device;
220         }
221 }
222
223 DEVICE* VM::get_device(int id)
224 {
225         for(DEVICE* device = first_device; device; device = device->next_device) {
226                 if(device->this_device_id == id) {
227                         return device;
228                 }
229         }
230         return NULL;
231 }
232
233 // ----------------------------------------------------------------------------
234 // drive virtual machine
235 // ----------------------------------------------------------------------------
236
237 void VM::reset()
238 {
239         // reset all devices
240         for(DEVICE* device = first_device; device; device = device->next_device) {
241                 device->reset();
242         }
243         // temporary fix...
244         for(DEVICE* device = first_device; device; device = device->next_device) {
245                 device->reset();
246         }
247         
248         // set devices
249         sio_kb->write_signal(SIG_I8251_DSR, 1, 1);
250         sio_sub->write_signal(SIG_I8251_DSR, 0, 0);
251 }
252
253 void VM::run()
254 {
255         event->drive();
256 }
257
258 // ----------------------------------------------------------------------------
259 // debugger
260 // ----------------------------------------------------------------------------
261
262 #ifdef USE_DEBUGGER
263 DEVICE *VM::get_cpu(int index)
264 {
265         if(index == 0) {
266                 return cpu;
267         }
268         return NULL;
269 }
270 #endif
271
272 // ----------------------------------------------------------------------------
273 // draw screen
274 // ----------------------------------------------------------------------------
275
276 void VM::draw_screen()
277 {
278         memory->draw_screen();
279 }
280
281 // ----------------------------------------------------------------------------
282 // soud manager
283 // ----------------------------------------------------------------------------
284
285 void VM::initialize_sound(int rate, int samples)
286 {
287         // init sound manager
288         event->initialize_sound(rate, samples);
289         
290         // init sound gen
291         psg->initialize_sound(rate, 125000, 10000);
292 }
293
294 uint16_t* VM::create_sound(int* extra_frames)
295 {
296         return event->create_sound(extra_frames);
297 }
298
299 int VM::get_sound_buffer_ptr()
300 {
301         return event->get_sound_buffer_ptr();
302 }
303
304 #ifdef USE_SOUND_VOLUME
305 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
306 {
307         if(ch == 0) {
308                 psg->set_volume(0, decibel_l, decibel_r);
309         } else if(ch == 1) {
310                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
311                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
312                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
313         }
314 }
315 #endif
316
317 // ----------------------------------------------------------------------------
318 // notify key
319 // ----------------------------------------------------------------------------
320
321 void VM::key_down(int code, bool repeat)
322 {
323         keyboard->key_down(code);
324 }
325
326 void VM::key_up(int code)
327 {
328         keyboard->key_up(code);
329 }
330
331 // ----------------------------------------------------------------------------
332 // user interface
333 // ----------------------------------------------------------------------------
334
335 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
336 {
337         fdc->open_disk(drv, file_path, bank);
338         floppy->change_disk(drv);
339 }
340
341 void VM::close_floppy_disk(int drv)
342 {
343         fdc->close_disk(drv);
344 }
345
346 bool VM::is_floppy_disk_inserted(int drv)
347 {
348         return fdc->is_disk_inserted(drv);
349 }
350
351 void VM::is_floppy_disk_protected(int drv, bool value)
352 {
353         fdc->is_disk_protected(drv, value);
354 }
355
356 bool VM::is_floppy_disk_protected(int drv)
357 {
358         return fdc->is_disk_protected(drv);
359 }
360
361 uint32_t VM::is_floppy_disk_accessed()
362 {
363         uint32_t status = fdc->read_signal(0);
364         if(bios) {
365                 status |= bios->read_signal(0);
366         }
367         return status;
368 }
369
370 uint32_t VM::is_hard_disk_accessed()
371 {
372         uint32_t status = 0;
373         for(int i = 0; i < 7; i++) {
374                 if(scsi_hdd[i] != NULL && scsi_hdd[i]->read_signal(0) != 0) {
375                         status |= 1 << i;
376                 }
377         }
378         if(bios) {
379                 status |= bios->read_signal(1);
380         }
381         return status;
382 }
383
384 bool VM::is_frame_skippable()
385 {
386         return event->is_frame_skippable();
387 }
388
389 void VM::update_config()
390 {
391         for(DEVICE* device = first_device; device; device = device->next_device) {
392                 device->update_config();
393         }
394 }
395
396 #define STATE_VERSION   6
397
398 void VM::save_state(FILEIO* state_fio)
399 {
400         state_fio->FputUint32(STATE_VERSION);
401         
402         for(DEVICE* device = first_device; device; device = device->next_device) {
403                 const char *name = typeid(*device).name() + 6; // skip "class "
404                 
405                 state_fio->FputInt32(strlen(name));
406                 state_fio->Fwrite(name, strlen(name), 1);
407                 device->save_state(state_fio);
408         }
409 }
410
411 bool VM::load_state(FILEIO* state_fio)
412 {
413         if(state_fio->FgetUint32() != STATE_VERSION) {
414                 return false;
415         }
416         for(DEVICE* device = first_device; device; device = device->next_device) {
417                 const char *name = typeid(*device).name() + 6; // skip "class "
418                 
419                 if(!(state_fio->FgetInt32() == strlen(name) && state_fio->Fcompare(name, strlen(name)))) {
420                         return false;
421                 }
422                 if(!device->load_state(state_fio)) {
423                         return false;
424                 }
425         }
426         return true;
427 }
428