OSDN Git Service

1f88713707e753050baacfd8d3d706e2f94c16df
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz2800 / mz2800.cpp
1 /*
2         SHARP MZ-2800 Emulator 'EmuZ-2800'
3
4         Author : Takeda.Toshiya
5         Date   : 2007.08.13 -
6
7         [ virtual machine ]
8 */
9
10 #include "mz2800.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../disk.h"
16 #include "../harddisk.h"
17 #include "../i8253.h"
18 #include "../i8255.h"
19 #include "../i8259.h"
20 #include "../i286.h"
21 #include "../io.h"
22 #include "../mb8877.h"
23 #include "../mz1p17.h"
24 #include "../noise.h"
25 #include "../not.h"
26 #include "../pcm1bit.h"
27 //#include "../pcpr201.h"
28 #include "../prnfile.h"
29 #include "../rp5c01.h"
30 #include "../scsi_hdd.h"
31 #include "../scsi_host.h"
32 #include "../upd71071.h"
33 #include "../ym2203.h"
34 #include "../z80pio.h"
35 #include "../z80sio.h"
36
37 #ifdef USE_DEBUGGER
38 #include "../debugger.h"
39 #endif
40
41 #include "crtc.h"
42 #include "floppy.h"
43 #include "joystick.h"
44 #include "keyboard.h"
45 #include "./memory.h"
46 #include "mouse.h"
47 #include "printer.h"
48 #include "reset.h"
49 #include "sasi.h"
50 #include "serial.h"
51 #include "sysport.h"
52
53 using MZ2800::CRTC;
54 using MZ2800::FLOPPY;
55 using MZ2800::JOYSTICK;
56 using MZ2800::KEYBOARD;
57 using MZ2800::MEMORY;
58 using MZ2800::MOUSE;
59 using MZ2800::PRINTER;
60 using MZ2800::RESET;
61 using MZ2800::SASI;
62 using MZ2800::SERIAL;
63 using MZ2800::SYSPORT;
64
65 // ----------------------------------------------------------------------------
66 // initialize
67 // ----------------------------------------------------------------------------
68
69 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
70 {
71         // create devices
72         first_device = last_device = NULL;
73         dummy = new DEVICE(this, emu);  // must be 1st device
74         event = new EVENT(this, emu);   // must be 2nd device
75         dummy->set_device_name(_T("1st Dummy"));
76
77         cpu = new I80286(this, emu);
78         pit = new I8253(this, emu);
79         pio0 = new I8255(this, emu);
80         pic = new I8259(this, emu);
81         io = new IO(this, emu);
82         fdc = new MB8877(this, emu);
83         fdc->set_context_noise_seek(new NOISE(this, emu));
84         fdc->set_context_noise_head_down(new NOISE(this, emu));
85         fdc->set_context_noise_head_up(new NOISE(this, emu));
86         not_busy = new NOT(this, emu);
87         not_busy->set_device_name(_T("NOT GATE(PRINTER BUSY)"));
88
89         pcm = new PCM1BIT(this, emu);
90         rtc = new RP5C01(this, emu);    // RP-5C15
91         sasi_host = new SCSI_HOST(this, emu);
92         for(int i = 0; i < array_length(sasi_hdd); i++) {
93                 sasi_hdd[i] = new SASI_HDD(this, emu);
94                 sasi_hdd[i]->set_device_name(_T("SASI Hard Disk Drive #%d"), i + 1);
95                 sasi_hdd[i]->scsi_id = i;
96                 sasi_hdd[i]->bytes_per_sec = 32 * 1024; // 32KB/s
97                 sasi_hdd[i]->set_context_interface(sasi_host);
98                 sasi_host->set_context_target(sasi_hdd[i]);
99         }
100         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
101                 sasi_hdd[drv >> 1]->set_disk_handler(drv & 1, new HARDDISK(emu));
102         }
103         dma = new UPD71071(this, emu);
104 #ifdef USE_DEBUGGER
105         dma->set_context_debugger(new DEBUGGER(this, emu));
106 #endif
107         opn = new YM2203(this, emu);
108 #ifdef USE_DEBUGGER
109         opn->set_context_debugger(new DEBUGGER(this, emu));
110 #endif
111         pio1 = new Z80PIO(this, emu);
112         sio = new Z80SIO(this, emu);
113         
114         crtc = new CRTC(this, emu);
115         floppy = new FLOPPY(this, emu);
116         joystick = new JOYSTICK(this, emu);
117         keyboard = new KEYBOARD(this, emu);
118         memory = new MEMORY(this, emu);
119         mouse = new MOUSE(this, emu);
120         printer = new PRINTER(this, emu);
121         rst = new RESET(this, emu);
122         sasi = new SASI(this, emu);
123         serial = new SERIAL(this, emu);
124         sysport = new SYSPORT(this, emu);
125         
126         // set contexts
127         event->set_context_cpu(cpu);
128         event->set_context_sound(opn);
129         event->set_context_sound(pcm);
130         event->set_context_sound(fdc->get_context_noise_seek());
131         event->set_context_sound(fdc->get_context_noise_head_down());
132         event->set_context_sound(fdc->get_context_noise_head_up());
133         
134         pit->set_constant_clock(0, 31250);
135         pit->set_constant_clock(2, 31250);
136         pit->set_context_ch0(pit, SIG_I8253_CLOCK_1, 1);
137         pit->set_context_ch0(pic, SIG_I8259_CHIP0 | SIG_I8259_IR0, 1);
138         pit->set_context_ch1(pic, SIG_I8259_CHIP0 | SIG_I8259_IR6, 1);
139         pit->set_context_ch2(pic, SIG_I8259_CHIP1 | SIG_I8259_IR0, 1);
140         pio0->set_context_port_c(rst, SIG_RESET_CONTROL, 0xff, 0);
141         pio0->set_context_port_c(crtc, SIG_CRTC_MASK, 0x01, 0);
142         pio0->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x04, 0);
143         // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
144         //pcm->set_realtime_render(true);
145
146         
147         pic->set_context_cpu(cpu);
148         fdc->set_context_drq(dma, SIG_UPD71071_CH1, 1);
149         fdc->set_context_irq(pic, SIG_I8259_CHIP0 | SIG_I8259_IR5, 1);
150         not_busy->set_context_out(pic, SIG_I8259_CHIP1 | SIG_I8259_IR1, 1);
151         rtc->set_context_alarm(pic, SIG_I8259_CHIP1 | SIG_I8259_IR2, 1);
152         rtc->set_context_pulse(opn, SIG_YM2203_PORT_B, 8);
153         sasi_host->set_context_bsy(sasi, SIG_SASI_BSY, 1);
154         sasi_host->set_context_cd (sasi, SIG_SASI_CXD, 1);
155         sasi_host->set_context_io (sasi, SIG_SASI_IXO, 1);
156         sasi_host->set_context_msg(sasi, SIG_SASI_MSG, 1);
157         sasi_host->set_context_req(sasi, SIG_SASI_REQ, 1);
158         sasi_host->set_context_ack(sasi, SIG_SASI_ACK, 1);
159         dma->set_context_memory(memory);
160         dma->set_context_ch0(sasi);
161         dma->set_context_ch1(fdc);
162         dma->set_context_tc(pic, SIG_I8259_CHIP0 | SIG_I8259_IR3, 1);
163         dma->set_context_tc(sasi, SIG_SASI_TC, 1);
164         opn->set_context_irq(pic, SIG_I8259_CHIP1 | SIG_I8259_IR7, 1);
165         opn->set_context_port_a(crtc, SIG_CRTC_PALLETE, 0x04, 0);
166         opn->set_context_port_a(mouse, SIG_MOUSE_SEL, 0x08, 0);
167         pio1->set_context_port_a(crtc, SIG_CRTC_COLUMN_SIZE, 0x20, 0);
168         pio1->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0xff, 0);
169         sio->set_context_intr(pic, SIG_I8259_CHIP0 | SIG_I8259_IR2);
170         sio->set_context_dtr(1, mouse, SIG_MOUSE_DTR, 1);
171         
172         crtc->set_context_pic(pic);
173         crtc->set_context_pio(pio0);
174         crtc->set_vram_ptr(memory->get_vram());
175         crtc->set_tvram_ptr(memory->get_tvram());
176         crtc->set_kanji_ptr(memory->get_kanji());
177         crtc->set_pcg_ptr(memory->get_pcg());
178         floppy->set_context_fdc(fdc);
179         keyboard->set_context_pio0(pio0);
180         keyboard->set_context_pio1(pio1);
181         memory->set_context_crtc(crtc);
182         mouse->set_context_sio(sio);
183         if(config.printer_type == 0) {
184                 PRNFILE *prnfile = new PRNFILE(this, emu);
185                 prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
186                 printer->set_context_prn(prnfile);
187         } else if(config.printer_type == 1) {
188                 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
189                 mz1p17->mode = MZ1P17_MODE_MZ1;
190                 mz1p17->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
191                 printer->set_context_prn(mz1p17);
192 //      } else if(config.printer_type == 2) {
193 //              PCPR201 *pcpr201 = new PCPR201(this, emu);
194 //              pcpr201->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
195 //              printer->set_context_prn(pcpr201);
196         } else {
197                 printer->set_context_prn(dummy);
198         }
199         sasi->set_context_host(sasi_host);
200         sasi->set_context_dma(dma);
201         sasi->set_context_pic(pic);
202         serial->set_context_sio(sio);
203         sysport->set_context_pit(pit);
204         sysport->set_context_sio(sio);
205         
206         // cpu bus
207         cpu->set_context_mem(memory);
208         cpu->set_context_io(io);
209         cpu->set_context_intr(pic);
210 #ifdef SINGLE_MODE_DMA
211         cpu->set_context_dma(dma);
212 #endif
213 #ifdef USE_DEBUGGER
214         cpu->set_context_debugger(new DEBUGGER(this, emu));
215 #endif
216         
217         // i/o bus
218         io->set_iomap_range_rw(0x70, 0x7f, dma);
219         io->set_iomap_alias_rw(0x80, pic, I8259_ADDR_CHIP0 | 0);
220         io->set_iomap_alias_rw(0x81, pic, I8259_ADDR_CHIP0 | 1);
221         io->set_iomap_alias_rw(0x82, pic, I8259_ADDR_CHIP1 | 0);
222         io->set_iomap_alias_rw(0x83, pic, I8259_ADDR_CHIP1 | 1);
223         io->set_iomap_range_rw(0x8c, 0x8d, memory);
224         io->set_iovalue_single_r(0x8e, 0xff);   // dipswitch
225         io->set_flipflop_single_rw(0x8f, 0x00); // shut
226         io->set_iomap_range_rw(0xa0, 0xa3, serial);
227         for(uint32_t p = 0xae; p <= 0x1fae; p += 0x100) {
228                 io->set_iomap_single_w(p, crtc);
229         }
230         io->set_iomap_range_rw(0xa4, 0xa5, sasi);
231         io->set_iomap_range_rw(0xb0, 0xb3, serial);
232         io->set_iomap_single_r(0xbe, sysport);
233         io->set_iomap_range_rw(0xc8, 0xc9, opn);
234         io->set_iovalue_single_r(0xca, 0x7f);   // voice communication ???
235         for(uint32_t p = 0xcc; p <= 0xfcc; p += 0x100) {
236                 io->set_iomap_alias_rw(p, rtc, p >> 8);
237         }
238         io->set_iomap_single_w(0xcd, serial);
239         io->set_iomap_single_rw(0xce, memory);
240         io->set_iomap_range_rw(0xd8, 0xdb, fdc);
241         io->set_iomap_range_w(0xdc, 0xdf, floppy);
242         io->set_iomap_range_rw(0xe0, 0xe3, pio0);
243         io->set_iomap_range_rw(0xe4, 0xe7, pit);
244         io->set_iomap_range_rw(0xe8, 0xeb, pio1);
245         io->set_iomap_single_rw(0xef, joystick);
246 //      io->set_iomap_range_w(0xf0, 0xf3, sysport);
247         io->set_iomap_single_w(0x170, crtc);
248         io->set_iomap_single_w(0x172, crtc);
249         io->set_iomap_single_w(0x174, crtc);
250         io->set_iomap_single_w(0x176, crtc);
251         io->set_iomap_range_w(0x178, 0x17b, crtc);
252         io->set_iomap_range_rw(0x1fe, 0x1ff, printer);
253         io->set_iomap_single_w(0x270, crtc);
254         io->set_iomap_single_w(0x272, crtc);
255         io->set_iomap_single_rw(0x274, memory);
256         
257         // initialize all devices
258 #if defined(__GIT_REPO_VERSION)
259         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
260 #endif
261         for(DEVICE* device = first_device; device; device = device->next_device) {
262                 device->initialize();
263         }
264         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
265                 if(!(config.last_hard_disk_path[drv][0] != _T('\0') && FILEIO::IsFileExisting(config.last_hard_disk_path[drv]))) {
266                         create_local_path(config.last_hard_disk_path[drv], _MAX_PATH, _T("SASI%d.DAT"), drv);
267                 }
268         }
269 }
270
271 VM::~VM()
272 {
273         // delete all devices
274         for(DEVICE* device = first_device; device;) {
275                 DEVICE *next_device = device->next_device;
276                 device->release();
277                 delete device;
278                 device = next_device;
279         }
280 }
281
282 DEVICE* VM::get_device(int id)
283 {
284         for(DEVICE* device = first_device; device; device = device->next_device) {
285                 if(device->this_device_id == id) {
286                         return device;
287                 }
288         }
289         return NULL;
290 }
291
292 // ----------------------------------------------------------------------------
293 // drive virtual machine
294 // ----------------------------------------------------------------------------
295
296 void VM::reset()
297 {
298         // reset all devices
299         for(DEVICE* device = first_device; device; device = device->next_device) {
300                 device->reset();
301         }
302         // temporary fix...
303         for(DEVICE* device = first_device; device; device = device->next_device) {
304                 device->reset();
305         }
306         
307         // set initial port status
308         pio0->write_signal(SIG_I8255_PORT_B, 0x7c, 0xff);
309         opn->write_signal(SIG_YM2203_PORT_B, 0x37, 0xff);
310 }
311
312 void VM::cpu_reset()
313 {
314         cpu->reset();
315 }
316
317 void VM::run()
318 {
319         event->drive();
320 }
321
322 // ----------------------------------------------------------------------------
323 // debugger
324 // ----------------------------------------------------------------------------
325
326 #ifdef USE_DEBUGGER
327 DEVICE *VM::get_cpu(int index)
328 {
329         if(index == 0) {
330                 return cpu;
331         }
332         return NULL;
333 }
334 #endif
335
336 // ----------------------------------------------------------------------------
337 // draw screen
338 // ----------------------------------------------------------------------------
339
340 void VM::draw_screen()
341 {
342         crtc->draw_screen();
343 }
344
345 // ----------------------------------------------------------------------------
346 // soud manager
347 // ----------------------------------------------------------------------------
348
349 void VM::initialize_sound(int rate, int samples)
350 {
351         // init sound manager
352         event->initialize_sound(rate, samples);
353         
354         // init sound gen
355         opn->initialize_sound(rate, 2000000, samples, 0, -8);
356         pcm->initialize_sound(rate, 4096);
357 }
358
359 uint16_t* VM::create_sound(int* extra_frames)
360 {
361         return event->create_sound(extra_frames);
362 }
363
364 int VM::get_sound_buffer_ptr()
365 {
366         return event->get_sound_buffer_ptr();
367 }
368
369 #ifdef USE_SOUND_VOLUME
370 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
371 {
372         if(ch == 0) {
373                 opn->set_volume(0, decibel_l, decibel_r);
374         } else if(ch == 1) {
375                 opn->set_volume(1, decibel_l, decibel_r);
376         } else if(ch == 2) {
377                 pcm->set_volume(0, decibel_l, decibel_r);
378         } else if(ch == 3) {
379                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
380                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
381                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
382         }
383 }
384 #endif
385
386 // ----------------------------------------------------------------------------
387 // user interface
388 // ----------------------------------------------------------------------------
389
390 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
391 {
392         fdc->open_disk(drv, file_path, bank);
393         
394         if(fdc->get_media_type(drv) == MEDIA_TYPE_2DD) {
395                 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2D) {
396                         fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
397                 }
398         } else if(fdc->get_media_type(drv) == MEDIA_TYPE_2D) {
399                 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2DD) {
400                         fdc->set_drive_type(drv, DRIVE_TYPE_2D);
401                 }
402         }
403 }
404
405 void VM::close_floppy_disk(int drv)
406 {
407         fdc->close_disk(drv);
408 }
409
410 bool VM::is_floppy_disk_inserted(int drv)
411 {
412         return fdc->is_disk_inserted(drv);
413 }
414
415 void VM::is_floppy_disk_protected(int drv, bool value)
416 {
417         fdc->is_disk_protected(drv, value);
418 }
419
420 bool VM::is_floppy_disk_protected(int drv)
421 {
422         return fdc->is_disk_protected(drv);
423 }
424
425 uint32_t VM::is_floppy_disk_accessed()
426 {
427         return fdc->read_signal(0);
428 }
429
430 void VM::open_hard_disk(int drv, const _TCHAR* file_path)
431 {
432         if(drv < USE_HARD_DISK) {
433                 sasi_hdd[drv >> 1]->open(drv & 1, file_path, 1024);
434         }
435 }
436
437 void VM::close_hard_disk(int drv)
438 {
439         if(drv < USE_HARD_DISK) {
440                 sasi_hdd[drv >> 1]->close(drv & 1);
441         }
442 }
443
444 bool VM::is_hard_disk_inserted(int drv)
445 {
446         if(drv < USE_HARD_DISK) {
447                 return sasi_hdd[drv >> 1]->mounted(drv & 1);
448         }
449         return false;
450 }
451
452 uint32_t VM::is_hard_disk_accessed()
453 {
454         uint32_t status = 0;
455         
456         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
457                 if(sasi_hdd[drv >> 1]->accessed(drv & 1)) {
458                         status |= 1 << drv;
459                 }
460         }
461         return status;
462 }
463
464 bool VM::is_frame_skippable()
465 {
466         return event->is_frame_skippable();
467 }
468
469 void VM::update_config()
470 {
471         for(DEVICE* device = first_device; device; device = device->next_device) {
472                 device->update_config();
473         }
474 }
475
476 #define STATE_VERSION   8
477
478 bool VM::process_state(FILEIO* state_fio, bool loading)
479 {
480         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
481                 return false;
482         }
483         for(DEVICE* device = first_device; device; device = device->next_device) {
484                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
485                 // const char *name = typeid(*device).name();
486                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
487                 const char *name = device->get_device_name();
488                 int len = strlen(name);
489                 
490                 if(!state_fio->StateCheckInt32(len)) {
491                         if(loading) {
492                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
493                         }
494                         return false;
495                 }
496                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
497                         if(loading) {
498                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
499                         }
500                         return false;
501                 }
502                 if(!device->process_state(state_fio, loading)) {
503                         if(loading) {
504                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
505                         }
506                         return false;
507                 }
508         }
509         // Machine specified.
510         return true;
511 }