2 FUJITSU FM16pi Emulator 'eFM16pi'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
22 #include "../mb8877.h"
23 #include "../memory.h"
24 #include "../msm58321.h"
27 #include "../pcm1bit.h"
30 #include "../debugger.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
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
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);
61 sub = new SUB(this, emu);
64 dummy->set_device_name(_T("1st Dummy"));
65 event->set_device_name(_T("EVENT"));
66 cpu->set_device_name(_T("CPU(MBL8086L)"));
68 sio->set_device_name(_T("i8251(RS-232C)"));
69 pio->set_device_name(_T("i8259(SYSTEM PORT)"));
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());
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);
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);
106 pic->set_context_cpu(cpu);
108 rtc->set_context_data(sub, SIG_SUB_RTC, 0x0f, 0);
109 rtc->set_context_busy(sub, SIG_SUB_RTC, 0x10);
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);
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);
124 cpu->set_context_mem(memory);
125 cpu->set_context_io(io);
126 cpu->set_context_intr(pic);
128 cpu->set_context_debugger(new DEBUGGER(this, emu));
132 memset(ram, 0, sizeof(ram));
133 memset(kanji, 0xff, sizeof(kanji));
134 memset(cart, 0xff, sizeof(cart));
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));
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);
149 io->set_iomap_alias_rw(0x00, pic, 0);
150 io->set_iomap_alias_rw(0x02, pic, 1);
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)
164 bit3 r key irq (0=active)
165 bit4-6 w datarec control
172 bit7 e datarec output
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);
180 40H bit0-3 r rtc data
183 bit7 r nmi mask (0=masked)
184 bit0-3 w rtc data/addr
186 bit5 w rtc addr write
189 60H bit0-6 r kbd data
190 bit7 r kbd make/break (0=make)
192 io->set_iomap_single_rw(0x40, sub);
193 io->set_iomap_single_r(0x60, sub);
195 io->set_iomap_alias_rw(0x80, sio, 0);
196 io->set_iomap_alias_rw(0x82, sio, 1);
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)
207 io->set_iomap_single_w(0xa0, sub);
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)
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);
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);
233 400H bit0 w memory write protect 20000H-47FFFH
234 bit1 w memory write protect 48000H-6FFFFH
237 // initialize all devices
238 #if defined(__GIT_REPO_VERSION)
239 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
241 for(DEVICE* device = first_device; device; device = device->next_device) {
242 device->initialize();
245 //pcm->set_realtime_render(true);
251 memory->write_bios(_T("BACKUP.BIN"), ram, sizeof(ram));
253 // delete all devices
254 for(DEVICE* device = first_device; device;) {
255 DEVICE *next_device = device->next_device;
258 device = next_device;
260 for(int i = 0; i < MAX_DRIVE; i++) {
261 fdc->set_drive_type(i, DRIVE_TYPE_2D);
265 DEVICE* VM::get_device(int id)
267 for(DEVICE* device = first_device; device; device = device->next_device) {
268 if(device->this_device_id == id) {
275 // ----------------------------------------------------------------------------
276 // drive virtual machine
277 // ----------------------------------------------------------------------------
282 for(DEVICE* device = first_device; device; device = device->next_device) {
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);
291 void VM::notify_power_off()
293 // this->out_debug_log(_T("--- POWER OFF ---\n"));
294 sub->notify_power_off();
302 // ----------------------------------------------------------------------------
304 // ----------------------------------------------------------------------------
307 DEVICE *VM::get_cpu(int index)
316 // ----------------------------------------------------------------------------
318 // ----------------------------------------------------------------------------
320 void VM::draw_screen()
325 // ----------------------------------------------------------------------------
327 // ----------------------------------------------------------------------------
329 void VM::initialize_sound(int rate, int samples)
331 // init sound manager
332 event->initialize_sound(rate, samples);
335 pcm->initialize_sound(rate, 8000);
338 uint16_t* VM::create_sound(int* extra_frames)
340 return event->create_sound(extra_frames);
343 int VM::get_sound_buffer_ptr()
345 return event->get_sound_buffer_ptr();
348 #ifdef USE_SOUND_VOLUME
349 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
352 pcm->set_volume(0, decibel_l, decibel_r);
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);
361 // ----------------------------------------------------------------------------
363 // ----------------------------------------------------------------------------
365 void VM::key_down(int code, bool repeat)
370 void VM::key_up(int code)
375 // ----------------------------------------------------------------------------
377 // ----------------------------------------------------------------------------
379 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
381 fdc->open_disk(drv, file_path, bank);
384 void VM::close_floppy_disk(int drv)
386 fdc->close_disk(drv);
389 bool VM::is_floppy_disk_inserted(int drv)
391 return fdc->is_disk_inserted(drv);
394 void VM::is_floppy_disk_protected(int drv, bool value)
396 fdc->is_disk_protected(drv, value);
399 bool VM::is_floppy_disk_protected(int drv)
401 return fdc->is_disk_protected(drv);
404 uint32_t VM::is_floppy_disk_accessed()
406 return fdc->read_signal(0);
409 bool VM::is_frame_skippable()
411 return event->is_frame_skippable();
414 void VM::update_config()
416 for(DEVICE* device = first_device; device; device = device->next_device) {
417 device->update_config();
421 #define STATE_VERSION 3
423 #include "../../statesub.h"
424 #include "../../qt/gui/csp_logger.h"
425 extern CSP_Logger DLL_PREFIX_I *csp_logger;
427 void VM::decl_state(void)
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();
436 void VM::save_state(FILEIO* state_fio)
438 //state_fio->FputUint32(STATE_VERSION);
440 if(state_entry != NULL) {
441 state_entry->save_state(state_fio);
443 for(DEVICE* device = first_device; device; device = device->next_device) {
444 device->save_state(state_fio);
446 //state_fio->Fwrite(ram, sizeof(ram), 1);
449 bool VM::load_state(FILEIO* state_fio)
451 //if(state_fio->FgetUint32() != STATE_VERSION) {
455 if(state_entry != NULL) {
456 mb = state_entry->load_state(state_fio);
459 emu->out_debug_log("INFO: HEADER DATA ERROR");
462 for(DEVICE* device = first_device; device; device = device->next_device) {
463 if(!device->load_state(state_fio)) {
467 //state_fio->Fread(ram, sizeof(ram), 1);