OSDN Git Service

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