OSDN Git Service

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