OSDN Git Service

[VM][UI][Qt] General : Merge upstream 2015-08-01.
[csp-qt/common_source_project-fm7.git] / source / src / vm / pc98ha / pc98ha.cpp
1 /*
2         NEC PC-98LT Emulator 'ePC-98LT'
3         NEC PC-98HA Emulator 'eHANDY98'
4
5         Author : Takeda.Toshiya
6         Date   : 2008.06.09 -
7
8         [ virtual machine ]
9 */
10
11 #include "pc98ha.h"
12 #include "../../emu.h"
13 #include "../device.h"
14 #include "../event.h"
15
16 #include "../beep.h"
17 #include "../i8251.h"
18 #include "../i8253.h"
19 #include "../i8255.h"
20 #include "../i8259.h"
21 #include "../i86.h"
22 #include "../io.h"
23 #ifdef _PC98HA
24 #include "../upd4991a.h"
25 #else
26 #include "../upd1990a.h"
27 #endif
28 #include "../upd71071.h"
29 #include "../upd765a.h"
30
31 #ifdef USE_DEBUGGER
32 #include "../debugger.h"
33 #endif
34
35 #include "bios.h"
36 #include "calendar.h"
37 #include "floppy.h"
38 #include "keyboard.h"
39 #include "memory.h"
40 #include "note.h"
41 #include "printer.h"
42
43 // ----------------------------------------------------------------------------
44 // initialize
45 // ----------------------------------------------------------------------------
46
47 VM::VM(EMU* parent_emu) : emu(parent_emu)
48 {
49         // create devices
50         first_device = last_device = NULL;
51         dummy = new DEVICE(this, emu);  // must be 1st device
52         event = new EVENT(this, emu);   // must be 2nd device
53         
54         beep = new BEEP(this, emu);
55         sio_rs = new I8251(this, emu);  // for rs232c
56         sio_kbd = new I8251(this, emu); // for keyboard
57         pit = new I8253(this, emu);     // V50 internal
58         pio_sys = new I8255(this, emu); // for system port
59         pio_prn = new I8255(this, emu); // for printer
60         pic = new I8259(this, emu);     // V50 internal
61         cpu = new I86(this, emu);       // V50
62         io = new IO(this, emu);
63 #ifdef _PC98HA
64         rtc = new UPD4991A(this, emu);
65 #else
66         rtc = new UPD1990A(this, emu);
67 #endif
68         dma = new UPD71071(this, emu);  // V50 internal
69         fdc = new UPD765A(this, emu);
70         
71         bios = new BIOS(this, emu);
72         calendar = new CALENDAR(this, emu);
73         floppy = new FLOPPY(this, emu);
74         keyboard = new KEYBOARD(this, emu);
75         memory = new MEMORY(this, emu);
76         note = new NOTE(this, emu);
77         printer = new PRINTER(this, emu);
78         
79         // set contexts
80         event->set_context_cpu(cpu);
81         event->set_context_sound(beep);
82         
83 //???   sio_rs->set_context_rxrdy(pic, SIG_I8259_IR4, 1);
84         sio_kbd->set_context_rxrdy(pic, SIG_I8259_IR1, 1);
85 //      sio_kbd->set_context_out(keyboard, SIG_KEYBOARD_RECV);
86         pit->set_context_ch0(pic, SIG_I8259_IR0, 1);
87         pit->set_context_ch1(pic, SIG_I8259_IR2, 1);
88 #ifdef _PC98HA
89         pit->set_constant_clock(0, 2457600);
90         pit->set_constant_clock(1, 2457600);
91         pit->set_constant_clock(2, 2457600);
92 #else
93         pit->set_constant_clock(0, 1996800);
94         pit->set_constant_clock(1, 300);        // ???
95         pit->set_constant_clock(2, 1996800);
96 #endif
97         pio_sys->set_context_port_c(beep, SIG_BEEP_MUTE, 8, 0);
98         pio_prn->set_context_port_a(printer, SIG_PRINTER_OUT, 0xff, 0);
99         pio_prn->set_context_port_c(printer, SIG_PRINTER_STB, 0x80, 0);
100         pic->set_context_cpu(cpu);
101 #ifdef _PC98LT
102         rtc->set_context_dout(pio_sys, SIG_I8255_PORT_B, 1);
103 #endif
104         dma->set_context_memory(memory);
105         dma->set_context_ch2(fdc);      // 1MB
106         dma->set_context_ch3(fdc);      // 640KB
107         fdc->set_context_irq(pic, SIG_I8259_IR6, 1);
108         fdc->set_context_drq(dma, SIG_UPD71071_CH3, 1);
109         fdc->raise_irq_when_media_changed = true;
110         
111         bios->set_context_fdc(fdc);
112         calendar->set_context_rtc(rtc);
113         floppy->set_context_fdc(fdc);
114         keyboard->set_context_sio(sio_kbd);
115         note->set_context_pic(pic);
116         printer = new PRINTER(this, emu);
117         
118         // cpu bus
119         cpu->set_context_bios(bios);
120         cpu->set_context_mem(memory);
121         cpu->set_context_io(io);
122         cpu->set_context_intr(pic);
123 #ifdef SINGLE_MODE_DMA
124         cpu->set_context_dma(dma);
125 #endif
126 #ifdef USE_DEBUGGER
127         cpu->set_context_debugger(new DEBUGGER(this, emu));
128 #endif
129         
130         // i/o bus
131         io->set_iomap_alias_rw(0x00, pic, 0);
132         io->set_iomap_alias_rw(0x02, pic, 1);
133 #ifdef _PC98HA
134         io->set_iomap_range_rw(0x22, 0x23, calendar);
135 #else
136         io->set_iomap_single_w(0x20, calendar);
137 #endif
138         io->set_iomap_alias_rw(0x30, sio_rs, 0);
139         io->set_iomap_alias_rw(0x32, sio_rs, 1);
140         io->set_iomap_alias_rw(0x31, pio_sys, 0);
141         io->set_iomap_alias_rw(0x33, pio_sys, 1);
142         io->set_iomap_alias_rw(0x35, pio_sys, 2);
143         io->set_iomap_alias_rw(0x37, pio_sys, 3);
144         io->set_iomap_alias_rw(0x40, pio_prn, 0);
145         io->set_iomap_alias_rw(0x42, pio_prn, 1);
146         io->set_iomap_alias_rw(0x44, pio_prn, 2);
147         io->set_iomap_alias_rw(0x46, pio_prn, 3);
148         io->set_iomap_alias_rw(0x41, sio_kbd, 0);
149         io->set_iomap_alias_rw(0x43, sio_kbd, 1);
150         io->set_iomap_alias_rw(0x71, pit, 0);
151         io->set_iomap_alias_rw(0x73, pit, 1);
152         io->set_iomap_alias_rw(0x75, pit, 2);
153         io->set_iomap_alias_rw(0x77, pit, 3);
154 #if defined(_PC98LT) || defined(DOCKING_STATION)
155         io->set_iomap_single_r(0xc8, floppy);
156         io->set_iomap_single_rw(0xca, floppy);
157         io->set_iomap_single_rw(0xcc, floppy);
158         io->set_iomap_single_rw(0xbe, floppy);
159 #endif
160         io->set_iomap_range_rw(0xe0, 0xef, dma);
161         io->set_iomap_single_w(0x8e1, memory);
162         io->set_iomap_single_w(0x8e3, memory);
163         io->set_iomap_single_w(0x8e5, memory);
164         io->set_iomap_single_w(0x8e7, memory);
165         io->set_iomap_single_rw(0x0c10, memory);
166         io->set_iomap_single_w(0x0e8e, memory);
167         io->set_iomap_single_w(0x1e8e, memory);
168         io->set_iomap_single_rw(0x4c10, memory);
169         io->set_iomap_single_rw(0x8c10, memory);
170         io->set_iomap_single_rw(0xcc10, memory);
171         io->set_iomap_single_rw(0x0810, note);
172         io->set_iomap_single_rw(0x0812, note);
173         io->set_iomap_single_r(0x0f8e, note);
174         io->set_iomap_single_r(0x5e8e, note);
175         io->set_iomap_single_rw(0x8810, note);
176         io->set_iomap_single_w(0xc810, note);
177         
178         // initialize all devices
179         for(DEVICE* device = first_device; device; device = device->next_device) {
180                 device->initialize();
181         }
182 }
183
184 VM::~VM()
185 {
186         // delete all devices
187         for(DEVICE* device = first_device; device;) {
188                 DEVICE *next_device = device->next_device;
189                 device->release();
190                 delete device;
191                 device = next_device;
192         }
193 }
194
195 DEVICE* VM::get_device(int id)
196 {
197         for(DEVICE* device = first_device; device; device = device->next_device) {
198                 if(device->this_device_id == id) {
199                         return device;
200                 }
201         }
202         return NULL;
203 }
204
205 // ----------------------------------------------------------------------------
206 // drive virtual machine
207 // ----------------------------------------------------------------------------
208
209 void VM::reset()
210 {
211         // reset all devices
212         for(DEVICE* device = first_device; device; device = device->next_device) {
213                 device->reset();
214         }
215         
216         // initial device settings
217         pio_sys->write_signal(SIG_I8255_PORT_A, 0xe3, 0xff);
218         pio_sys->write_signal(SIG_I8255_PORT_B, 0xe0, 0xff);
219 #ifdef _PC98HA
220         pio_prn->write_signal(SIG_I8255_PORT_B, 0xde, 0xff);
221 #else
222         pio_prn->write_signal(SIG_I8255_PORT_B, 0xfc, 0xff);
223 #endif
224         beep->write_signal(SIG_BEEP_ON, 1, 1);
225         beep->write_signal(SIG_BEEP_MUTE, 1, 1);
226 }
227
228 void VM::run()
229 {
230         event->drive();
231 }
232
233 // ----------------------------------------------------------------------------
234 // debugger
235 // ----------------------------------------------------------------------------
236
237 #ifdef USE_DEBUGGER
238 DEVICE *VM::get_cpu(int index)
239 {
240         if(index == 0) {
241                 return cpu;
242         }
243         return NULL;
244 }
245 #endif
246
247 // ----------------------------------------------------------------------------
248 // draw screen
249 // ----------------------------------------------------------------------------
250
251 void VM::draw_screen()
252 {
253         memory->draw_screen();
254 }
255
256 int VM::access_lamp()
257 {
258         uint32 status = fdc->read_signal(0);
259         return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
260 }
261
262 // ----------------------------------------------------------------------------
263 // soud manager
264 // ----------------------------------------------------------------------------
265
266 void VM::initialize_sound(int rate, int samples)
267 {
268         // init sound manager
269         event->initialize_sound(rate, samples);
270         
271         // init sound gen
272         beep->init(rate, 2400, 8000);
273 }
274
275 uint16* VM::create_sound(int* extra_frames)
276 {
277         return event->create_sound(extra_frames);
278 }
279
280 int VM::sound_buffer_ptr()
281 {
282         return event->sound_buffer_ptr();
283 }
284
285 // ----------------------------------------------------------------------------
286 // notify key
287 // ----------------------------------------------------------------------------
288
289 void VM::key_down(int code, bool repeat)
290 {
291         keyboard->key_down(code);
292 }
293
294 void VM::key_up(int code)
295 {
296         keyboard->key_up(code);
297 }
298
299 // ----------------------------------------------------------------------------
300 // user interface
301 // ----------------------------------------------------------------------------
302
303 void VM::open_disk(int drv, _TCHAR* file_path, int bank)
304 {
305         fdc->open_disk(drv, file_path, bank);
306 }
307
308 void VM::close_disk(int drv)
309 {
310         fdc->close_disk(drv);
311 }
312
313 bool VM::disk_inserted(int drv)
314 {
315         return fdc->disk_inserted(drv);
316 }
317
318
319 void VM::set_disk_protected(int drv, bool value)
320 {
321         fdc->set_disk_protected(drv, value);
322 }
323
324 bool VM::get_disk_protected(int drv)
325 {
326         return fdc->get_disk_protected(drv);
327 }
328
329 bool VM::now_skip()
330 {
331         return event->now_skip();
332 }
333
334 void VM::update_config()
335 {
336         for(DEVICE* device = first_device; device; device = device->next_device) {
337                 device->update_config();
338         }
339 }
340
341 #define STATE_VERSION   1
342
343 void VM::save_state(FILEIO* state_fio)
344 {
345         state_fio->FputUint32(STATE_VERSION);
346         
347         for(DEVICE* device = first_device; device; device = device->next_device) {
348                 device->save_state(state_fio);
349         }
350 }
351
352 bool VM::load_state(FILEIO* state_fio)
353 {
354         if(state_fio->FgetUint32() != STATE_VERSION) {
355                 return false;
356         }
357         for(DEVICE* device = first_device; device; device = device->next_device) {
358                 if(!device->load_state(state_fio)) {
359                         return false;
360                 }
361         }
362         return true;
363 }
364