OSDN Git Service

[VM][STATE] Use namespace {VMNAME} to separate per VMs.
[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 //#if defined(HAS_V30) || defined(HAS_I86)
22 //# include "../i86.h"
23 //#else
24 # include "../i286.h"
25 //#endif
26 #include "../io.h"
27 #include "../noise.h"
28 #include "../not.h"
29 //#include "../pcpr201.h"
30 #include "../prnfile.h"
31 #ifdef _PC98HA
32 #include "../upd4991a.h"
33 #else
34 #include "../upd1990a.h"
35 #endif
36 #include "../upd71071.h"
37 #include "../upd765a.h"
38
39 #ifdef USE_DEBUGGER
40 #include "../debugger.h"
41 #endif
42
43 #include "bios.h"
44 #include "calendar.h"
45 #include "floppy.h"
46 #include "keyboard.h"
47 #include "./memory.h"
48 #include "note.h"
49
50 using PC98HA::BIOS;
51 using PC98HA::CALENDAR;
52 using PC98HA::FLOPPY;
53 using PC98HA::KEYBOARD;
54 using PC98HA::MEMORY;
55 using PC98HA::NOTE;
56
57 // ----------------------------------------------------------------------------
58 // initialize
59 // ----------------------------------------------------------------------------
60
61 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
62 {
63         // create devices
64         first_device = last_device = NULL;
65         dummy = new DEVICE(this, emu);  // must be 1st device
66         event = new EVENT(this, emu);   // must be 2nd device
67         dummy->set_device_name(_T("1st Dummy"));
68         
69         beep = new BEEP(this, emu);
70         sio_rs = new I8251(this, emu);  // for rs232c
71         sio_rs->set_device_name(_T("8251 SIO (RS-232C)"));
72         sio_kbd = new I8251(this, emu); // for keyboard
73         sio_kbd->set_device_name(_T("8251 SIO (Keyboard)"));
74         pit = new I8253(this, emu);     // V50 internal
75         pio_sys = new I8255(this, emu); // for system port
76         pio_sys->set_device_name(_T("8255 PIO (System)"));
77         pio_prn = new I8255(this, emu); // for printer
78         pio_prn->set_device_name(_T("8251 PIO (Printer)"));
79         pic = new I8259(this, emu);     // V50 internal
80 //#if defined(HAS_V30) || defined(HAS_I86)
81 //      cpu = new I86(this, emu);       // V50
82 //#else
83         cpu = new I286(this, emu);      // V50
84 //#endif
85         io = new IO(this, emu);
86         not_busy = new NOT(this, emu);
87 #ifdef _PC98HA
88         rtc = new UPD4991A(this, emu);
89 #else
90         rtc = new UPD1990A(this, emu);
91 #endif
92         dma = new UPD71071(this, emu);  // V50 internal
93         fdc = new UPD765A(this, emu);
94         fdc->set_context_noise_seek(new NOISE(this, emu));
95         fdc->set_context_noise_head_down(new NOISE(this, emu));
96         fdc->set_context_noise_head_up(new NOISE(this, emu));
97         pic->set_device_name(_T("V50 PIC(i8259 COMPATIBLE)"));
98         cpu->set_device_name(_T("CPU (V50)"));
99         not_busy->set_device_name(_T("NOT GATE(PRINTER BUSY)"));
100         
101         if(config.printer_type == 0) {
102                 printer = new PRNFILE(this, emu);
103 //      } else if(config.printer_type == 1) {
104 //              printer = new PCPR201(this, emu);
105         } else {
106                 printer = dummy;
107         }
108         
109         bios = new BIOS(this, emu);
110         calendar = new CALENDAR(this, emu);
111         floppy = new FLOPPY(this, emu);
112         keyboard = new KEYBOARD(this, emu);
113         memory = new MEMORY(this, emu);
114         note = new NOTE(this, emu);
115         // set contexts
116         event->set_context_cpu(cpu);
117         event->set_context_sound(beep);
118         event->set_context_sound(fdc->get_context_noise_seek());
119         event->set_context_sound(fdc->get_context_noise_head_down());
120         event->set_context_sound(fdc->get_context_noise_head_up());
121 //???   sio_rs->set_context_rxrdy(pic, SIG_I8259_IR4, 1);
122         sio_kbd->set_context_rxrdy(pic, SIG_I8259_IR1, 1);
123 //      sio_kbd->set_context_out(keyboard, SIG_KEYBOARD_RECV);
124         pit->set_context_ch0(pic, SIG_I8259_IR0, 1);
125         pit->set_context_ch1(pic, SIG_I8259_IR2, 1);
126 #ifdef _PC98HA
127         pit->set_constant_clock(0, 2457600);
128         pit->set_constant_clock(1, 2457600);
129         pit->set_constant_clock(2, 2457600);
130 #else
131         pit->set_constant_clock(0, 1996800);
132         pit->set_constant_clock(1, 300);        // ???
133         pit->set_constant_clock(2, 1996800);
134 #endif
135         pio_sys->set_context_port_c(beep, SIG_BEEP_MUTE, 8, 0);
136         pio_prn->set_context_port_a(printer, SIG_PRINTER_DATA, 0xff, 0);
137         pio_prn->set_context_port_c(printer, SIG_PRINTER_STROBE, 0x80, 0);
138         if(config.printer_type == 0) {
139                 PRNFILE *prnfile = (PRNFILE *)printer;
140                 prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
141 //      } else if(config.printer_type == 1) {
142 //              PRNFILE *pcpr201 = (PCPR201 *)printer;
143 //              pcpr201->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
144         }
145         not_busy->set_context_out(pio_prn, SIG_I8255_PORT_B, 4);
146         pic->set_context_cpu(cpu);
147 #ifdef _PC98LT
148         rtc->set_context_dout(pio_sys, SIG_I8255_PORT_B, 1);
149 #endif
150         dma->set_context_memory(memory);
151         dma->set_context_ch2(fdc);      // 1MB
152         dma->set_context_ch3(fdc);      // 640KB
153         fdc->set_context_irq(pic, SIG_I8259_IR6, 1);
154         fdc->set_context_drq(dma, SIG_UPD71071_CH3, 1);
155         fdc->raise_irq_when_media_changed = true;
156         
157         bios->set_context_fdc(fdc);
158         calendar->set_context_rtc(rtc);
159         floppy->set_context_fdc(fdc);
160         keyboard->set_context_sio(sio_kbd);
161         note->set_context_pic(pic);
162         
163         // cpu bus
164         cpu->set_context_bios(bios);
165         cpu->set_context_mem(memory);
166         cpu->set_context_io(io);
167         cpu->set_context_intr(pic);
168 #ifdef SINGLE_MODE_DMA
169         cpu->set_context_dma(dma);
170 #endif
171 #ifdef USE_DEBUGGER
172         cpu->set_context_debugger(new DEBUGGER(this, emu));
173 #endif
174         
175         // i/o bus
176         io->set_iomap_alias_rw(0x00, pic, 0);
177         io->set_iomap_alias_rw(0x02, pic, 1);
178 #ifdef _PC98HA
179         io->set_iomap_range_rw(0x22, 0x23, calendar);
180 #else
181         io->set_iomap_single_w(0x20, calendar);
182 #endif
183         io->set_iomap_alias_rw(0x30, sio_rs, 0);
184         io->set_iomap_alias_rw(0x32, sio_rs, 1);
185         io->set_iomap_alias_rw(0x31, pio_sys, 0);
186         io->set_iomap_alias_rw(0x33, pio_sys, 1);
187         io->set_iomap_alias_rw(0x35, pio_sys, 2);
188         io->set_iomap_alias_rw(0x37, pio_sys, 3);
189         io->set_iomap_alias_rw(0x40, pio_prn, 0);
190         io->set_iomap_alias_rw(0x42, pio_prn, 1);
191         io->set_iomap_alias_rw(0x44, pio_prn, 2);
192         io->set_iomap_alias_rw(0x46, pio_prn, 3);
193         io->set_iomap_alias_rw(0x41, sio_kbd, 0);
194         io->set_iomap_alias_rw(0x43, sio_kbd, 1);
195         io->set_iomap_alias_rw(0x71, pit, 0);
196         io->set_iomap_alias_rw(0x73, pit, 1);
197         io->set_iomap_alias_rw(0x75, pit, 2);
198         io->set_iomap_alias_rw(0x77, pit, 3);
199 #if defined(_PC98LT) || defined(DOCKING_STATION)
200         io->set_iomap_single_r(0xc8, floppy);
201         io->set_iomap_single_rw(0xca, floppy);
202         io->set_iomap_single_rw(0xcc, floppy);
203         io->set_iomap_single_rw(0xbe, floppy);
204 #endif
205         io->set_iomap_range_rw(0xe0, 0xef, dma);
206         io->set_iomap_single_w(0x8e1, memory);
207         io->set_iomap_single_w(0x8e3, memory);
208         io->set_iomap_single_w(0x8e5, memory);
209         io->set_iomap_single_w(0x8e7, memory);
210         io->set_iomap_single_rw(0x0c10, memory);
211         io->set_iomap_single_w(0x0e8e, memory);
212         io->set_iomap_single_w(0x1e8e, memory);
213         io->set_iomap_single_rw(0x4c10, memory);
214         io->set_iomap_single_rw(0x8c10, memory);
215         io->set_iomap_single_rw(0xcc10, memory);
216         io->set_iomap_single_rw(0x0810, note);
217         io->set_iomap_single_rw(0x0812, note);
218         io->set_iomap_single_r(0x0f8e, note);
219         io->set_iomap_single_r(0x5e8e, note);
220         io->set_iomap_single_rw(0x8810, note);
221         io->set_iomap_single_w(0xc810, note);
222         
223         // initialize all devices
224 #if defined(__GIT_REPO_VERSION)
225         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
226 #endif
227         for(DEVICE* device = first_device; device; device = device->next_device) {
228                 device->initialize();
229         }
230 }
231
232 VM::~VM()
233 {
234         // delete all devices
235         for(DEVICE* device = first_device; device;) {
236                 DEVICE *next_device = device->next_device;
237                 device->release();
238                 delete device;
239                 device = next_device;
240         }
241 }
242
243 DEVICE* VM::get_device(int id)
244 {
245         for(DEVICE* device = first_device; device; device = device->next_device) {
246                 if(device->this_device_id == id) {
247                         return device;
248                 }
249         }
250         return NULL;
251 }
252
253 // ----------------------------------------------------------------------------
254 // drive virtual machine
255 // ----------------------------------------------------------------------------
256
257 void VM::reset()
258 {
259         // reset all devices
260         for(DEVICE* device = first_device; device; device = device->next_device) {
261                 device->reset();
262         }
263         
264         // initial device settings
265         pio_sys->write_signal(SIG_I8255_PORT_A, 0xe3, 0xff);
266         pio_sys->write_signal(SIG_I8255_PORT_B, 0xe0, 0xff);
267 #ifdef _PC98HA
268         pio_prn->write_signal(SIG_I8255_PORT_B, 0xde, 0xff);
269 #else
270         pio_prn->write_signal(SIG_I8255_PORT_B, 0xfc, 0xff);
271 #endif
272         beep->write_signal(SIG_BEEP_ON, 1, 1);
273         beep->write_signal(SIG_BEEP_MUTE, 1, 1);
274 }
275
276 void VM::run()
277 {
278         event->drive();
279 }
280
281 // ----------------------------------------------------------------------------
282 // debugger
283 // ----------------------------------------------------------------------------
284
285 #ifdef USE_DEBUGGER
286 DEVICE *VM::get_cpu(int index)
287 {
288         if(index == 0) {
289                 return cpu;
290         }
291         return NULL;
292 }
293 #endif
294
295 // ----------------------------------------------------------------------------
296 // draw screen
297 // ----------------------------------------------------------------------------
298
299 void VM::draw_screen()
300 {
301         memory->draw_screen();
302 }
303
304 // ----------------------------------------------------------------------------
305 // soud manager
306 // ----------------------------------------------------------------------------
307
308 void VM::initialize_sound(int rate, int samples)
309 {
310         // init sound manager
311         event->initialize_sound(rate, samples);
312         
313         // init sound gen
314         beep->initialize_sound(rate, 2400, 8000);
315 }
316
317 uint16_t* VM::create_sound(int* extra_frames)
318 {
319         return event->create_sound(extra_frames);
320 }
321
322 int VM::get_sound_buffer_ptr()
323 {
324         return event->get_sound_buffer_ptr();
325 }
326
327 #ifdef USE_SOUND_VOLUME
328 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
329 {
330         if(ch == 0) {
331                 beep->set_volume(0, decibel_l, decibel_r);
332         } else if(ch == 1) {
333                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
334                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
335                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
336         }
337 }
338 #endif
339
340 // ----------------------------------------------------------------------------
341 // notify key
342 // ----------------------------------------------------------------------------
343
344 void VM::key_down(int code, bool repeat)
345 {
346         keyboard->key_down(code);
347 }
348
349 void VM::key_up(int code)
350 {
351         keyboard->key_up(code);
352 }
353
354 bool VM::get_caps_locked()
355 {
356         return keyboard->get_caps_locked();
357 }
358
359 bool VM::get_kana_locked()
360 {
361         return keyboard->get_kana_locked();
362 }
363
364 // ----------------------------------------------------------------------------
365 // user interface
366 // ----------------------------------------------------------------------------
367
368 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
369 {
370         fdc->open_disk(drv, file_path, bank);
371 }
372
373 void VM::close_floppy_disk(int drv)
374 {
375         fdc->close_disk(drv);
376 }
377
378 bool VM::is_floppy_disk_inserted(int drv)
379 {
380         return fdc->is_disk_inserted(drv);
381 }
382
383 void VM::is_floppy_disk_protected(int drv, bool value)
384 {
385         fdc->is_disk_protected(drv, value);
386 }
387
388 bool VM::is_floppy_disk_protected(int drv)
389 {
390         return fdc->is_disk_protected(drv);
391 }
392
393 uint32_t VM::is_floppy_disk_accessed()
394 {
395         return fdc->read_signal(0);
396 }
397
398 bool VM::is_frame_skippable()
399 {
400         return event->is_frame_skippable();
401 }
402
403 void VM::update_config()
404 {
405         for(DEVICE* device = first_device; device; device = device->next_device) {
406                 device->update_config();
407         }
408 }
409
410 #define STATE_VERSION   6
411
412 bool VM::process_state(FILEIO* state_fio, bool loading)
413 {
414         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
415                 return false;
416         }
417         for(DEVICE* device = first_device; device; device = device->next_device) {
418                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
419                 // const char *name = typeid(*device).name();
420                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
421                 const char *name = device->get_device_name();
422                 int len = strlen(name);
423                 
424                 if(!state_fio->StateCheckInt32(len)) {
425                         if(loading) {
426                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
427                         }
428                         return false;
429                 }
430                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
431                         if(loading) {
432                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
433                         }
434                         return false;
435                 }
436                 if(!device->process_state(state_fio, loading)) {
437                         if(loading) {
438                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
439                         }
440                         return false;
441                 }
442         }
443         // Machine specified.
444         return true;
445 }