OSDN Git Service

20fcba8d8628ffc4b0667fc7e5d7333c3a45824c
[csp-qt/common_source_project-fm7.git] / source / src / vm / n5200 / n5200.cpp
1 /*
2         NEC N5200 Emulator 'eN5200'
3
4         Author : Takeda.Toshiya
5         Date   : 2009.06.03-
6
7         [ virtual machine ]
8 */
9
10 #include "n5200.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../beep.h"
16 #include "../i386.h"
17 #include "../i8237.h"
18 #include "../i8251.h"
19 #include "../i8253.h"
20 #include "../i8255.h"
21 #include "../i8259.h"
22 #include "../io.h"
23 #include "../noise.h"
24 #include "../upd1990a.h"
25 #include "../upd7220.h"
26 #include "../upd765a.h"
27
28 #ifdef USE_DEBUGGER
29 #include "../debugger.h"
30 #endif
31
32 #include "display.h"
33 #include "floppy.h"
34 #include "keyboard.h"
35 #include "./memory.h"
36 #include "system.h"
37
38 // ----------------------------------------------------------------------------
39 // initialize
40 // ----------------------------------------------------------------------------
41
42 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
43 {
44         // create devices
45         first_device = last_device = NULL;
46         dummy = new DEVICE(this, emu);  // must be 1st device
47         event = new EVENT(this, emu);   // must be 2nd device
48         dummy->set_device_name(_T("1st Dummy"));
49         
50         beep = new BEEP(this, emu);
51         cpu = new I386(this, emu);
52         dma = new I8237(this, emu);
53         sio_r = new I8251(this, emu);   // for rs232c
54         sio_r->set_device_name(_T("8251 SIO (RS-232C)"));
55         sio_k = new I8251(this, emu);   // for keyboard
56         sio_k->set_device_name(_T("8251 SIO (Keyboard)"));
57         pit = new I8253(this, emu);
58         pio_s = new I8255(this, emu);   // for system port
59         pio_s->set_device_name(_T("8255 PIO (System)"));
60         pio_p = new I8255(this, emu);   // for printer
61         pio_p->set_device_name(_T("8255 PIO (Printer)"));
62         pic = new I8259(this, emu);
63         io = new IO(this, emu);
64         rtc = new UPD1990A(this, emu);
65         gdc_c = new UPD7220(this, emu);
66         gdc_c->set_device_name(_T("uPD7220 GDC (Character)"));
67         gdc_g = new UPD7220(this, emu);
68         gdc_g->set_device_name(_T("uPD7220 GDC (Graphics)"));
69         fdc = new UPD765A(this, emu);
70         fdc->set_context_noise_seek(new NOISE(this, emu));
71         fdc->set_context_noise_head_down(new NOISE(this, emu));
72         fdc->set_context_noise_head_up(new NOISE(this, emu));
73         
74         display = new DISPLAY(this, emu);
75         floppy = new FLOPPY(this, emu);
76         keyboard = new KEYBOARD(this, emu);
77         memory = new N5200_MEMORY(this, emu);
78         system = new SYSTEM(this, emu);
79         
80         // set contexts
81         event->set_context_cpu(cpu);
82         event->set_context_sound(beep);
83         event->set_context_sound(fdc->get_context_noise_seek());
84         event->set_context_sound(fdc->get_context_noise_head_down());
85         event->set_context_sound(fdc->get_context_noise_head_up());
86         
87 //???   sio_r->set_context_rxrdy(pic, SIG_I8259_CHIP0 | SIG_I8259_IR4, 1);
88         sio_k->set_context_rxrdy(pic, SIG_I8259_CHIP0 | SIG_I8259_IR1, 1);
89         sio_k->set_context_rst(keyboard, SIG_KEYBOARD_RST, 1);
90         sio_k->set_context_out(keyboard, SIG_KEYBOARD_RECV);
91         pit->set_context_ch0(pic, SIG_I8259_CHIP0 | SIG_I8259_IR0, 1);
92         pit->set_constant_clock(0, 1996800);
93         pit->set_constant_clock(1, 300);        // ???
94         pit->set_constant_clock(2, 1996800);
95         pio_s->set_context_port_c(beep, SIG_BEEP_MUTE, 8, 0);
96         pic->set_context_cpu(cpu);
97         rtc->set_context_dout(pio_s, SIG_I8255_PORT_B, 1);
98         dma->set_context_memory(memory);
99         dma->set_context_ch2(fdc);      // 1MB
100         dma->set_context_ch3(fdc);      // 640KB
101         gdc_g->set_vram_ptr(memory->get_vram(), 0x20000);
102         fdc->set_context_irq(pic, SIG_I8259_IR6, 1);
103         fdc->set_context_drq(floppy, SIG_FLOPPY_DRQ, 1);
104         
105         display->set_context_pic(pic);
106         display->set_vram_ptr(memory->get_vram());
107         display->set_tvram_ptr(memory->get_tvram());
108         floppy->set_context_fdc(fdc);
109         floppy->set_context_dma(dma);
110         keyboard->set_context_sio(sio_k);
111         system->set_context_dma(dma);
112         
113         // cpu bus
114         cpu->set_context_mem(memory);
115         cpu->set_context_io(io);
116         cpu->set_context_intr(pic);
117 #ifdef USE_DEBUGGER
118         cpu->set_context_debugger(new DEBUGGER(this, emu));
119 #endif
120         
121         // i/o bus
122         io->set_iomap_alias_w(0x00, pic, 0);
123         io->set_iomap_alias_w(0x02, pic, 1);
124         io->set_iomap_alias_w(0x08, pic, 2);
125         io->set_iomap_alias_w(0x0a, pic, 3);
126         io->set_iomap_alias_w(0x01, dma, 0);
127         io->set_iomap_alias_w(0x03, dma, 1);
128         io->set_iomap_alias_w(0x05, dma, 2);
129         io->set_iomap_alias_w(0x07, dma, 3);
130         io->set_iomap_alias_w(0x09, dma, 4);
131         io->set_iomap_alias_w(0x0b, dma, 5);
132         io->set_iomap_alias_w(0x0d, dma, 6);
133         io->set_iomap_alias_w(0x0f, dma, 7);
134         io->set_iomap_alias_w(0x11, dma, 8);
135         io->set_iomap_alias_w(0x13, dma, 9);
136         io->set_iomap_alias_w(0x15, dma, 0x0a);
137         io->set_iomap_alias_w(0x17, dma, 0x0b);
138         io->set_iomap_alias_w(0x19, dma, 0x0c);
139         io->set_iomap_alias_w(0x1b, dma, 0x0d);
140         io->set_iomap_alias_w(0x1d, dma, 0x0e);
141         io->set_iomap_alias_w(0x1f, dma, 0x0f);
142         io->set_iomap_single_w(0x20, rtc);
143         io->set_iomap_single_w(0x21, system);
144         io->set_iomap_single_w(0x23, system);
145         io->set_iomap_single_w(0x25, system);
146         io->set_iomap_single_w(0x27, system);
147         io->set_iomap_single_w(0x29, system);
148         io->set_iomap_alias_w(0x30, sio_r, 0);
149         io->set_iomap_alias_w(0x32, sio_r, 1);
150         io->set_iomap_alias_w(0x31, pio_s, 0);
151         io->set_iomap_alias_w(0x33, pio_s, 1);
152         io->set_iomap_alias_w(0x35, pio_s, 2);
153         io->set_iomap_alias_w(0x37, pio_s, 3);
154         io->set_iomap_single_w(0x3b, system);
155         io->set_iomap_alias_w(0x40, pio_p, 0);
156         io->set_iomap_alias_w(0x42, pio_p, 1);
157         io->set_iomap_alias_w(0x44, pio_p, 2);
158         io->set_iomap_alias_w(0x46, pio_p, 3);
159         io->set_iomap_alias_w(0x41, sio_k, 0);
160         io->set_iomap_alias_w(0x43, sio_k, 1);
161         io->set_iomap_single_w(0x50, system);
162         io->set_iomap_single_w(0x52, system);
163         io->set_iomap_alias_w(0x60, gdc_c, 0);
164         io->set_iomap_alias_w(0x62, gdc_c, 1);
165         io->set_iomap_single_w(0x64, display);
166         io->set_iomap_single_w(0x68, display);
167         io->set_iomap_single_w(0x6a, display);
168         io->set_iomap_single_w(0x6c, display);
169         io->set_iomap_single_w(0x70, display);
170         io->set_iomap_single_w(0x72, display);
171         io->set_iomap_single_w(0x74, memory);
172         io->set_iomap_single_w(0x76, display);
173         io->set_iomap_single_w(0x78, display);
174         io->set_iomap_single_w(0x7a, display);
175         io->set_iomap_alias_w(0x71, pit, 0);
176         io->set_iomap_alias_w(0x73, pit, 1);
177         io->set_iomap_alias_w(0x75, pit, 2);
178         io->set_iomap_alias_w(0x77, pit, 3);
179         io->set_iomap_single_w(0x92, floppy);
180         io->set_iomap_single_w(0x94, floppy);
181         io->set_iomap_alias_w(0xa0, gdc_g, 0);
182         io->set_iomap_alias_w(0xa2, gdc_g, 1);
183         io->set_iomap_single_w(0xa4, display);
184         io->set_iomap_single_w(0xa6, display);
185         io->set_iomap_single_w(0xa8, display);
186         io->set_iomap_single_w(0xaa, display);
187         io->set_iomap_single_w(0xac, display);
188         io->set_iomap_single_w(0xae, display);
189         io->set_iomap_single_w(0xca, floppy);
190         io->set_iomap_single_w(0xcc, floppy);
191         io->set_iomap_single_w(0xbe, floppy);
192         
193         io->set_iomap_alias_r(0x00, pic, 0);
194         io->set_iomap_alias_r(0x02, pic, 1);
195         io->set_iomap_alias_r(0x08, pic, 2);
196         io->set_iomap_alias_r(0x0a, pic, 3);
197         io->set_iomap_alias_r(0x01, dma, 0);
198         io->set_iomap_alias_r(0x03, dma, 1);
199         io->set_iomap_alias_r(0x05, dma, 2);
200         io->set_iomap_alias_r(0x07, dma, 3);
201         io->set_iomap_alias_r(0x09, dma, 4);
202         io->set_iomap_alias_r(0x0b, dma, 5);
203         io->set_iomap_alias_r(0x0d, dma, 6);
204         io->set_iomap_alias_r(0x0f, dma, 7);
205         io->set_iomap_alias_r(0x11, dma, 8);
206         io->set_iomap_alias_r(0x13, dma, 9);
207         io->set_iomap_alias_r(0x15, dma, 0x0a);
208         io->set_iomap_alias_r(0x17, dma, 0x0b);
209         io->set_iomap_alias_r(0x19, dma, 0x0c);
210         io->set_iomap_alias_r(0x1b, dma, 0x0d);
211         io->set_iomap_alias_r(0x1d, dma, 0x0e);
212         io->set_iomap_alias_r(0x1f, dma, 0x0f);
213         io->set_iomap_alias_r(0x30, sio_r, 0);
214         io->set_iomap_alias_r(0x32, sio_r, 1);
215         io->set_iomap_alias_r(0x31, pio_s, 0);
216         io->set_iomap_alias_r(0x33, pio_s, 1);
217         io->set_iomap_alias_r(0x35, pio_s, 2);
218         io->set_iomap_alias_r(0x37, pio_s, 3);
219         io->set_iomap_single_r(0x39, system);
220         io->set_iomap_alias_r(0x40, pio_p, 0);
221         io->set_iomap_alias_r(0x42, pio_p, 1);
222         io->set_iomap_alias_r(0x44, pio_p, 2);
223         io->set_iomap_alias_r(0x46, pio_p, 3);
224         io->set_iomap_alias_r(0x41, sio_k, 0);
225         io->set_iomap_alias_r(0x43, sio_k, 1);
226         io->set_iomap_alias_r(0x60, gdc_c, 0);
227         io->set_iomap_alias_r(0x62, gdc_c, 1);
228         io->set_iomap_alias_r(0x71, pit, 0);
229         io->set_iomap_alias_r(0x73, pit, 1);
230         io->set_iomap_alias_r(0x75, pit, 2);
231         io->set_iomap_single_r(0x90, floppy);
232         io->set_iomap_single_r(0x92, floppy);
233         io->set_iomap_single_r(0x94, floppy);
234         io->set_iomap_alias_r(0xa0, gdc_g, 0);
235         io->set_iomap_alias_r(0xa2, gdc_g, 1);
236         io->set_iomap_single_r(0xc8, floppy);
237         io->set_iomap_single_r(0xca, floppy);
238         io->set_iomap_single_r(0xcc, floppy);
239         io->set_iomap_single_r(0xbe, floppy);
240         
241         // initialize all devices
242 #if defined(__GIT_REPO_VERSION)
243         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
244 #endif
245         for(DEVICE* device = first_device; device; device = device->next_device) {
246                 device->initialize();
247         }
248 }
249
250 VM::~VM()
251 {
252         // delete all devices
253         for(DEVICE* device = first_device; device;) {
254                 DEVICE *next_device = device->next_device;
255                 device->release();
256                 delete device;
257                 device = next_device;
258         }
259 }
260
261 DEVICE* VM::get_device(int id)
262 {
263         for(DEVICE* device = first_device; device; device = device->next_device) {
264                 if(device->this_device_id == id) {
265                         return device;
266                 }
267         }
268         return NULL;
269 }
270
271 // ----------------------------------------------------------------------------
272 // drive virtual machine
273 // ----------------------------------------------------------------------------
274
275 void VM::reset()
276 {
277         // reset all devices
278         for(DEVICE* device = first_device; device; device = device->next_device) {
279                 device->reset();
280         }
281         
282         // initial device settings
283         sio_k->write_signal(SIG_I8251_DSR, 1, 1);               // DSR = 1
284         pio_s->write_io8(3, 0x92);
285         pio_s->write_signal(SIG_I8255_PORT_A, 0x04, 0xff);      // SW = FDD
286         pio_s->write_signal(SIG_I8255_PORT_B, 0xe1, 0xff);
287         pio_p->write_io8(3, 0x82);
288         pio_p->write_signal(SIG_I8255_PORT_B, 0x51, 0xff);
289         beep->write_signal(SIG_BEEP_ON, 1, 1);
290         beep->write_signal(SIG_BEEP_MUTE, 1, 1);
291 }
292
293 void VM::run()
294 {
295         event->drive();
296 }
297
298 // ----------------------------------------------------------------------------
299 // debugger
300 // ----------------------------------------------------------------------------
301
302 #ifdef USE_DEBUGGER
303 DEVICE *VM::get_cpu(int index)
304 {
305         if(index == 0) {
306                 return cpu;
307         }
308         return NULL;
309 }
310 #endif
311
312 // ----------------------------------------------------------------------------
313 // draw screen
314 // ----------------------------------------------------------------------------
315
316 void VM::draw_screen()
317 {
318         display->draw_screen();
319 }
320
321 // ----------------------------------------------------------------------------
322 // soud manager
323 // ----------------------------------------------------------------------------
324
325 void VM::initialize_sound(int rate, int samples)
326 {
327         // init sound manager
328         event->initialize_sound(rate, samples);
329         
330         // init sound gen
331         beep->initialize_sound(rate, 2400, 8000);
332 }
333
334 uint16_t* VM::create_sound(int* extra_frames)
335 {
336         return event->create_sound(extra_frames);
337 }
338
339 int VM::get_sound_buffer_ptr()
340 {
341         return event->get_sound_buffer_ptr();
342 }
343
344 #ifdef USE_SOUND_VOLUME
345 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
346 {
347         if(ch == 0) {
348                 beep->set_volume(0, decibel_l, decibel_r);
349         } else if(ch == 1) {
350                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
351                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
352                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
353         }
354 }
355 #endif
356
357 // ----------------------------------------------------------------------------
358 // notify key
359 // ----------------------------------------------------------------------------
360
361 void VM::key_down(int code, bool repeat)
362 {
363         keyboard->key_down(code);
364 }
365
366 void VM::key_up(int code)
367 {
368         keyboard->key_up(code);
369 }
370
371 bool VM::get_caps_locked()
372 {
373         return keyboard->get_caps_locked();
374 }
375
376 bool VM::get_kana_locked()
377 {
378         return keyboard->get_kana_locked();
379 }
380
381 // ----------------------------------------------------------------------------
382 // user interface
383 // ----------------------------------------------------------------------------
384
385 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
386 {
387         fdc->open_disk(drv, file_path, bank);
388 }
389
390 void VM::close_floppy_disk(int drv)
391 {
392         fdc->close_disk(drv);
393 }
394
395 bool VM::is_floppy_disk_inserted(int drv)
396 {
397         return fdc->is_disk_inserted(drv);
398 }
399
400 void VM::is_floppy_disk_protected(int drv, bool value)
401 {
402         fdc->is_disk_protected(drv, value);
403 }
404
405 bool VM::is_floppy_disk_protected(int drv)
406 {
407         return fdc->is_disk_protected(drv);
408 }
409
410 uint32_t VM::is_floppy_disk_accessed()
411 {
412         return fdc->read_signal(0);
413 }
414
415 bool VM::is_frame_skippable()
416 {
417         return event->is_frame_skippable();
418 }
419
420 void VM::update_config()
421 {
422         for(DEVICE* device = first_device; device; device = device->next_device) {
423                 device->update_config();
424         }
425 }
426
427 #define STATE_VERSION   1
428
429 bool VM::process_state(FILEIO* state_fio, bool loading)
430 {
431         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
432                 return false;
433         }
434         for(DEVICE* device = first_device; device; device = device->next_device) {
435                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
436                 // const char *name = typeid(*device).name();
437                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
438                 const char *name = device->get_device_name();
439                 int len = strlen(name);
440                 
441                 if(!state_fio->StateCheckInt32(len)) {
442                         if(loading) {
443                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
444                         }
445                         return false;
446                 }
447                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
448                         if(loading) {
449                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
450                         }
451                         return false;
452                 }
453                 if(!device->process_state(state_fio, loading)) {
454                         if(loading) {
455                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
456                         }
457                         return false;
458                 }
459         }
460         // Machine specified.
461         return true;
462 }
463