OSDN Git Service

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