OSDN Git Service

[VM] Apply VM_TEMPLATE to all VMs.
[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         for(DEVICE* device = first_device; device; device = device->next_device) {
239                 device->initialize();
240         }
241         decl_state();
242         //pcm->set_realtime_render(true);
243 }
244
245 VM::~VM()
246 {
247         // save memory
248         memory->write_bios(_T("BACKUP.BIN"), ram, sizeof(ram));
249         
250         // delete all devices
251         for(DEVICE* device = first_device; device;) {
252                 DEVICE *next_device = device->next_device;
253                 device->release();
254                 delete device;
255                 device = next_device;
256         }
257         for(int i = 0; i < MAX_DRIVE; i++) {
258                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
259         }
260 }
261
262 DEVICE* VM::get_device(int id)
263 {
264         for(DEVICE* device = first_device; device; device = device->next_device) {
265                 if(device->this_device_id == id) {
266                         return device;
267                 }
268         }
269         return NULL;
270 }
271
272 // ----------------------------------------------------------------------------
273 // drive virtual machine
274 // ----------------------------------------------------------------------------
275
276 void VM::reset()
277 {
278         // reset all devices
279         for(DEVICE* device = first_device; device; device = device->next_device) {
280                 device->reset();
281         }
282         
283         // initial device settings
284         pio->write_signal(SIG_I8255_PORT_B, 0x3f, 0xff);        // printer disconnected
285         pio->write_signal(SIG_I8255_PORT_C, 0x0c, 0x0f);
286 }
287
288 void VM::notify_power_off()
289 {
290 //      this->out_debug_log(_T("--- POWER OFF ---\n"));
291         sub->notify_power_off();
292 }
293
294 void VM::run()
295 {
296         event->drive();
297 }
298
299 // ----------------------------------------------------------------------------
300 // debugger
301 // ----------------------------------------------------------------------------
302
303 #ifdef USE_DEBUGGER
304 DEVICE *VM::get_cpu(int index)
305 {
306         if(index == 0) {
307                 return cpu;
308         }
309         return NULL;
310 }
311 #endif
312
313 // ----------------------------------------------------------------------------
314 // draw screen
315 // ----------------------------------------------------------------------------
316
317 void VM::draw_screen()
318 {
319         sub->draw_screen();
320 }
321
322 // ----------------------------------------------------------------------------
323 // soud manager
324 // ----------------------------------------------------------------------------
325
326 void VM::initialize_sound(int rate, int samples)
327 {
328         // init sound manager
329         event->initialize_sound(rate, samples);
330         
331         // init sound gen
332         pcm->initialize_sound(rate, 8000);
333 }
334
335 uint16_t* VM::create_sound(int* extra_frames)
336 {
337         return event->create_sound(extra_frames);
338 }
339
340 int VM::get_sound_buffer_ptr()
341 {
342         return event->get_sound_buffer_ptr();
343 }
344
345 #ifdef USE_SOUND_VOLUME
346 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
347 {
348         if(ch == 0) {
349                 pcm->set_volume(0, decibel_l, decibel_r);
350         } else if(ch == 1) {
351                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
352                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
353                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
354         }
355 }
356 #endif
357
358 // ----------------------------------------------------------------------------
359 // notify key
360 // ----------------------------------------------------------------------------
361
362 void VM::key_down(int code, bool repeat)
363 {
364         sub->key_down(code);
365 }
366
367 void VM::key_up(int code)
368 {
369         sub->key_up(code);
370 }
371
372 // ----------------------------------------------------------------------------
373 // user interface
374 // ----------------------------------------------------------------------------
375
376 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
377 {
378         fdc->open_disk(drv, file_path, bank);
379 }
380
381 void VM::close_floppy_disk(int drv)
382 {
383         fdc->close_disk(drv);
384 }
385
386 bool VM::is_floppy_disk_inserted(int drv)
387 {
388         return fdc->is_disk_inserted(drv);
389 }
390
391 void VM::is_floppy_disk_protected(int drv, bool value)
392 {
393         fdc->is_disk_protected(drv, value);
394 }
395
396 bool VM::is_floppy_disk_protected(int drv)
397 {
398         return fdc->is_disk_protected(drv);
399 }
400
401 uint32_t VM::is_floppy_disk_accessed()
402 {
403         return fdc->read_signal(0);
404 }
405
406 bool VM::is_frame_skippable()
407 {
408         return event->is_frame_skippable();
409 }
410
411 void VM::update_config()
412 {
413         for(DEVICE* device = first_device; device; device = device->next_device) {
414                 device->update_config();
415         }
416 }
417
418 #define STATE_VERSION   3
419
420 #include "../../statesub.h"
421 #include "../../qt/gui/csp_logger.h"
422 extern CSP_Logger DLL_PREFIX_I *csp_logger;
423
424 void VM::decl_state(void)
425 {
426         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::FM16PI_HEAD")), csp_logger);
427         DECL_STATE_ENTRY_MULTI(void, ram, sizeof(ram));
428         for(DEVICE* device = first_device; device; device = device->next_device) {
429                 device->decl_state();
430         }
431 }
432
433 void VM::save_state(FILEIO* state_fio)
434 {
435         //state_fio->FputUint32(STATE_VERSION);
436         
437         if(state_entry != NULL) {
438                 state_entry->save_state(state_fio);
439         }
440         for(DEVICE* device = first_device; device; device = device->next_device) {
441                 device->save_state(state_fio);
442         }
443         //state_fio->Fwrite(ram, sizeof(ram), 1);
444 }
445
446 bool VM::load_state(FILEIO* state_fio)
447 {
448         //if(state_fio->FgetUint32() != STATE_VERSION) {
449         //      return false;
450         //}
451         bool mb = false;
452         if(state_entry != NULL) {
453                 mb = state_entry->load_state(state_fio);
454         }
455         if(!mb) {
456                 emu->out_debug_log("INFO: HEADER DATA ERROR");
457                 return false;
458         }
459         for(DEVICE* device = first_device; device; device = device->next_device) {
460                 if(!device->load_state(state_fio)) {
461                         return false;
462                 }
463         }
464         //state_fio->Fread(ram, sizeof(ram), 1);
465         return true;
466 }
467