OSDN Git Service

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