OSDN Git Service

41a4f1cf1ebcf6f75722834fe2712a854e947617
[csp-qt/common_source_project-fm7.git] / source / src / vm / fm16pi / fm16pi.cpp
1 /*
2         FUJITSU FM16pi Emulator 'eFM16pi'
3
4         Author : Takeda.Toshiya
5         Date   : 2010.12.25-
6
7         [ virtual machine ]
8 */
9
10 #include "fm16pi.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../i8251.h"
16 #include "../i8253.h"
17 #include "../i8255.h"
18 #include "../i8259.h"
19 #include "../i86.h"
20 #include "../io.h"
21 #include "../mb8877.h"
22 #include "../memory.h"
23 #include "../msm58321.h"
24 #include "../noise.h"
25 #include "../not.h"
26 #include "../pcm1bit.h"
27
28 #ifdef USE_DEBUGGER
29 #include "../debugger.h"
30 #endif
31
32 #include "sub.h"
33
34 // ----------------------------------------------------------------------------
35 // initialize
36 // ----------------------------------------------------------------------------
37
38 VM::VM(EMU* parent_emu) : emu(parent_emu)
39 {
40         // create devices
41         first_device = last_device = NULL;
42         dummy = new DEVICE(this, emu);  // must be 1st device
43         event = new EVENT(this, emu);   // must be 2nd device
44         
45         sio = new I8251(this, emu);     // for rs232c
46         pit = new I8253(this, emu);
47         pio = new I8255(this, emu);     // for system port
48         pic = new I8259(this, emu);
49         cpu = new I86(this, emu);
50         io = new IO(this, emu);
51         fdc = new MB8877(this, emu);
52         fdc->set_context_noise_seek(new NOISE(this, emu));
53         fdc->set_context_noise_head_down(new NOISE(this, emu));
54         fdc->set_context_noise_head_up(new NOISE(this, emu));
55         memory = new MEMORY(this, emu);
56         rtc = new MSM58321(this, emu);
57         not_pit = new NOT(this, emu);
58         pcm = new PCM1BIT(this, emu);
59         
60         sub = new SUB(this, emu);
61
62 #if defined(_USE_QT)
63         dummy->set_device_name(_T("1st Dummy"));
64         event->set_device_name(_T("EVENT"));
65         cpu->set_device_name(_T("CPU(MBL8086L)"));
66
67         sio->set_device_name(_T("i8251(RS-232C)"));
68         pio->set_device_name(_T("i8259(SYSTEM PORT)"));
69         
70         rtc->set_device_name(_T("MSM58321(RTC)"));
71         memory->set_device_name(_T("MEMORY"));
72
73         sub->set_device_name(_T("SUB SYSTEM"));
74 #endif
75         
76         // set contexts
77         event->set_context_cpu(cpu);
78         event->set_context_sound(pcm);
79         event->set_context_sound(fdc->get_context_noise_seek());
80         event->set_context_sound(fdc->get_context_noise_head_down());
81         event->set_context_sound(fdc->get_context_noise_head_up());
82 /*
83         IRQ 0   PIT CH.0
84         IRQ 1   SIO RXRDY
85         IRQ 2   SIO SYNDET
86         IRQ 3   SIO TXRDY
87         IRQ 4   KEY IN
88         IRQ 5   EXT IRQ
89         IRQ 6   PRN ACK
90         IRQ 7   FDC IRQ
91 */
92         sio->set_context_rxrdy(pic, SIG_I8259_IR1, 1);
93         sio->set_context_syndet(pic, SIG_I8259_IR2, 1);
94         sio->set_context_txrdy(pic, SIG_I8259_IR3, 1);
95         
96 /*
97         TIMER   Ch.0    IRQ0
98                 Ch.1    RS-232C sclock
99                 Ch.2    Speaker
100 */
101         pit->set_context_ch0(pic, SIG_I8259_IR0, 1);
102         pit->set_context_ch0(not_pit, SIG_NOT_INPUT, 1);
103         not_pit->set_context_out(pio, SIG_I8255_PORT_B, 0x20);
104         pit->set_context_ch2(pcm, SIG_PCM1BIT_SIGNAL, 1);
105         pit->set_constant_clock(0, 2457600);
106         pit->set_constant_clock(1, 2457600);
107         pit->set_constant_clock(2, 2457600);
108         
109         pic->set_context_cpu(cpu);
110         
111         rtc->set_context_data(sub, SIG_SUB_RTC, 0x0f, 0);
112         rtc->set_context_busy(sub, SIG_SUB_RTC, 0x10);
113         
114         fdc->set_context_irq(pic, SIG_I8259_IR7, 1);
115         fdc->set_context_irq(pio, SIG_I8255_PORT_B, 0x40);
116         fdc->set_context_drq(pio, SIG_I8255_PORT_B, 0x80);
117         
118         sub->set_context_cpu(cpu);
119         sub->set_context_fdc(fdc);
120         sub->set_context_pcm(pcm);
121         sub->set_context_pic(pic);
122         sub->set_context_pio(pio);
123         sub->set_context_rtc(rtc);
124         sub->set_vram_ptr(ram + 0x78000);
125         
126         // cpu bus
127         cpu->set_context_mem(memory);
128         cpu->set_context_io(io);
129         cpu->set_context_intr(pic);
130 #ifdef USE_DEBUGGER
131         cpu->set_context_debugger(new DEBUGGER(this, emu));
132 #endif
133         
134         // memory bus
135         memset(ram, 0, sizeof(ram));
136         memset(kanji, 0xff, sizeof(kanji));
137         memset(cart, 0xff, sizeof(cart));
138         
139         memory->read_bios(_T("BACKUP.BIN"), ram, sizeof(ram));
140         memory->read_bios(_T("KANJI.ROM"), kanji, sizeof(kanji));
141         memory->read_bios(_T("CART.ROM"), cart, sizeof(cart));
142         
143         memory->set_memory_rw(0x00000, 0x6ffff, ram);
144         memory->set_memory_rw(0x70000, 0x73fff, ram + 0x78000);
145         memory->set_memory_rw(0x74000, 0x77fff, ram + 0x78000);
146         memory->set_memory_rw(0x78000, 0x7bfff, ram + 0x78000);
147         memory->set_memory_rw(0x7c000, 0x7ffff, ram + 0x78000);
148         memory->set_memory_r(0x80000, 0xbffff, kanji);
149         memory->set_memory_r(0xc0000, 0xfffff, cart);
150         
151         // i/o bus
152         io->set_iomap_alias_rw(0x00, pic, 0);
153         io->set_iomap_alias_rw(0x02, pic, 1);
154 /*
155         20H     bit0-7  w       printer data
156         22H     bit0    r       printer busy (1=busy)
157                 bit1    r       printer error (0=error)
158                 bit2    r       printer acknlg (0=active)
159                 bit3    r       printer pe (1=empty)
160                 bit4    r       printer slct (1=online)
161                 bit5    r       timer irq (0=active)
162                 bit6    r       fdc irq (1=active)
163                 bit7    r       fdc drq (1=active)
164         24H     bit0    r       datarec write protect (1=protected)
165                 bit1    r       datarec counter pulse (4hz)
166                 bit2    r       datarec input
167                 bit3    r       key irq (0=active)
168                 bit4-6  w       datarec control
169                                 0,0,0   stop
170                                 0,0,1   ff
171                                 0,1,0   rec
172                                 1,0,0   pause
173                                 1,0,1   rew
174                                 1,1,0   play
175                 bit7    e       datarec output
176 */
177         io->set_iomap_alias_rw(0x20, pio, 0);
178         io->set_iomap_alias_rw(0x22, pio, 1);
179         io->set_iomap_alias_rw(0x24, pio, 2);
180         io->set_iomap_alias_w(0x26, pio, 3);
181         
182 /*
183         40H     bit0-3  r       rtc data
184                 bit4    r       rtc busy
185                 bit5    r       bcr data
186                 bit7    r       nmi mask (0=masked)
187                 bit0-3  w       rtc data/addr
188                 bit4    w       rtc cs
189                 bit5    w       rtc addr write
190                 bit6    w       rtc read
191                 bit7    w       rtc write
192         60H     bit0-6  r       kbd data
193                 bit7    r       kbd make/break (0=make)
194 */
195         io->set_iomap_single_rw(0x40, sub);
196         io->set_iomap_single_r(0x60, sub);
197         
198         io->set_iomap_alias_rw(0x80, sio, 0);
199         io->set_iomap_alias_rw(0x82, sio, 1);
200         
201 /*
202         A0H     bit0-1  w       rs-232c clock select
203                 bit2    w       printer strobe (1=on)
204                 bit3    w       printer irq reset (0=reset)
205                 bit4    w       timer irq reset (0=reset)
206                 bit5    w       power off (1=power off)
207                 bit6    w       speaker on/off (1=on)
208                 bit7    w       nmi mask (0=masked)
209 */
210         io->set_iomap_single_w(0xa0, sub);
211         
212 /*
213         C0H     bit0-7  r       fdc status register
214                 bit0-7  w       fdc command register
215         C2H     bit0-7  rw      fdc track register
216         C4H     bit0-7  rw      fdc sector register
217         C6H     bit0-7  rw      fdc data register
218         C8H     bit0    rw      floppy side register
219         CAH     bit0-1  rw      floppy drive number
220                 bit6    rw      floppy drive disable (1=disable)
221                 bit7    rw      floppy motor on/off (1=on)
222 */
223         io->set_iomap_alias_rw(0xc0, fdc, 0);
224         io->set_iomap_alias_rw(0xc2, fdc, 1);
225         io->set_iomap_alias_rw(0xc4, fdc, 2);
226         io->set_iomap_alias_rw(0xc6, fdc, 3);
227         io->set_iomap_single_rw(0xc8, sub);
228         io->set_iomap_single_rw(0xca, sub);
229         
230         io->set_iomap_alias_rw(0xe0, pit, 0);
231         io->set_iomap_alias_rw(0xe2, pit, 1);
232         io->set_iomap_alias_rw(0xe4, pit, 2);
233         io->set_iomap_alias_w(0xe6, pit, 3);
234         
235 /*
236         400H    bit0    w       memory write protect 20000H-47FFFH
237                 bit1    w       memory write protect 48000H-6FFFFH
238 */
239         
240         // initialize all devices
241         for(DEVICE* device = first_device; device; device = device->next_device) {
242                 device->initialize();
243         }
244         pcm->set_realtime_render(true);
245 }
246
247 VM::~VM()
248 {
249         // save memory
250         memory->write_bios(_T("BACKUP.BIN"), ram, sizeof(ram));
251         
252         // delete all devices
253         for(DEVICE* device = first_device; device;) {
254                 DEVICE *next_device = device->next_device;
255                 device->release();
256                 delete device;
257                 device = next_device;
258         }
259 }
260
261 DEVICE* VM::get_device(int id)
262 {
263         for(DEVICE* device = first_device; device; device = device->next_device) {
264                 if(device->this_device_id == id) {
265                         return device;
266                 }
267         }
268         return NULL;
269 }
270
271 // ----------------------------------------------------------------------------
272 // drive virtual machine
273 // ----------------------------------------------------------------------------
274
275 void VM::reset()
276 {
277         // reset all devices
278         for(DEVICE* device = first_device; device; device = device->next_device) {
279                 device->reset();
280         }
281         
282         // initial device settings
283         pio->write_signal(SIG_I8255_PORT_B, 0x3f, 0xff);        // printer disconnected
284         pio->write_signal(SIG_I8255_PORT_C, 0x0c, 0x0f);
285 }
286
287 void VM::notify_power_off()
288 {
289 //      this->out_debug_log(_T("--- POWER OFF ---\n"));
290         sub->notify_power_off();
291 }
292
293 void VM::run()
294 {
295         event->drive();
296 }
297
298 // ----------------------------------------------------------------------------
299 // debugger
300 // ----------------------------------------------------------------------------
301
302 #ifdef USE_DEBUGGER
303 DEVICE *VM::get_cpu(int index)
304 {
305         if(index == 0) {
306                 return cpu;
307         }
308         return NULL;
309 }
310 #endif
311
312 // ----------------------------------------------------------------------------
313 // draw screen
314 // ----------------------------------------------------------------------------
315
316 void VM::draw_screen()
317 {
318         sub->draw_screen();
319 }
320
321 // ----------------------------------------------------------------------------
322 // soud manager
323 // ----------------------------------------------------------------------------
324
325 void VM::initialize_sound(int rate, int samples)
326 {
327         // init sound manager
328         event->initialize_sound(rate, samples);
329         
330         // init sound gen
331         pcm->initialize_sound(rate, 8000);
332 }
333
334 uint16_t* VM::create_sound(int* extra_frames)
335 {
336         return event->create_sound(extra_frames);
337 }
338
339 int VM::get_sound_buffer_ptr()
340 {
341         return event->get_sound_buffer_ptr();
342 }
343
344 #ifdef USE_SOUND_VOLUME
345 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
346 {
347         if(ch == 0) {
348                 pcm->set_volume(0, decibel_l, decibel_r);
349         } else if(ch == 1) {
350                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
351                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
352                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
353         }
354 }
355 #endif
356
357 // ----------------------------------------------------------------------------
358 // notify key
359 // ----------------------------------------------------------------------------
360
361 void VM::key_down(int code, bool repeat)
362 {
363         sub->key_down(code);
364 }
365
366 void VM::key_up(int code)
367 {
368         sub->key_up(code);
369 }
370
371 // ----------------------------------------------------------------------------
372 // user interface
373 // ----------------------------------------------------------------------------
374
375 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
376 {
377         fdc->open_disk(drv, file_path, bank);
378 }
379
380 void VM::close_floppy_disk(int drv)
381 {
382         fdc->close_disk(drv);
383 }
384
385 bool VM::is_floppy_disk_inserted(int drv)
386 {
387         return fdc->is_disk_inserted(drv);
388 }
389
390 void VM::is_floppy_disk_protected(int drv, bool value)
391 {
392         fdc->is_disk_protected(drv, value);
393 }
394
395 bool VM::is_floppy_disk_protected(int drv)
396 {
397         return fdc->is_disk_protected(drv);
398 }
399
400 uint32_t VM::is_floppy_disk_accessed()
401 {
402         return fdc->read_signal(0);
403 }
404
405 bool VM::is_frame_skippable()
406 {
407         return event->is_frame_skippable();
408 }
409
410 void VM::update_config()
411 {
412         for(DEVICE* device = first_device; device; device = device->next_device) {
413                 device->update_config();
414         }
415 }
416
417 #define STATE_VERSION   2
418
419 void VM::save_state(FILEIO* state_fio)
420 {
421         state_fio->FputUint32(STATE_VERSION);
422         
423         for(DEVICE* device = first_device; device; device = device->next_device) {
424                 device->save_state(state_fio);
425         }
426         state_fio->Fwrite(ram, sizeof(ram), 1);
427 }
428
429 bool VM::load_state(FILEIO* state_fio)
430 {
431         if(state_fio->FgetUint32() != STATE_VERSION) {
432                 return false;
433         }
434         for(DEVICE* device = first_device; device; device = device->next_device) {
435                 if(!device->load_state(state_fio)) {
436                         return false;
437                 }
438         }
439         state_fio->Fread(ram, sizeof(ram), 1);
440         return true;
441 }
442