OSDN Git Service

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