OSDN Git Service

ed715a46f62b9e3b6501ceaad86582cb03a3d309
[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 "../i8253.h"
16 #include "../i8255.h"
17 #include "../i8259.h"
18 #include "../i286.h"
19 #include "../io.h"
20 #include "../mb8877.h"
21 #include "../mz1p17.h"
22 #include "../noise.h"
23 #include "../not.h"
24 #include "../pcm1bit.h"
25 //#include "../pcpr201.h"
26 #include "../prnfile.h"
27 #include "../rp5c01.h"
28 //#include "../sasi.h"
29 #include "../upd71071.h"
30 #include "../ym2203.h"
31 #include "../z80pio.h"
32 #include "../z80sio.h"
33
34 #ifdef USE_DEBUGGER
35 #include "../debugger.h"
36 #endif
37
38 #include "crtc.h"
39 #include "floppy.h"
40 #include "joystick.h"
41 #include "keyboard.h"
42 #include "memory.h"
43 #include "mouse.h"
44 #include "printer.h"
45 #include "reset.h"
46 #include "serial.h"
47 #include "sysport.h"
48
49 // ----------------------------------------------------------------------------
50 // initialize
51 // ----------------------------------------------------------------------------
52
53 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
54 {
55         // create devices
56         first_device = last_device = NULL;
57         dummy = new DEVICE(this, emu);  // must be 1st device
58         event = new EVENT(this, emu);   // must be 2nd device
59         dummy->set_device_name(_T("1st Dummy"));
60
61         cpu = new I286(this, emu);
62         pit = new I8253(this, emu);
63         pio0 = new I8255(this, emu);
64         pic = new I8259(this, emu);
65         io = new IO(this, emu);
66         fdc = new MB8877(this, emu);
67         fdc->set_context_noise_seek(new NOISE(this, emu));
68         fdc->set_context_noise_head_down(new NOISE(this, emu));
69         fdc->set_context_noise_head_up(new NOISE(this, emu));
70         not_busy = new NOT(this, emu);
71         not_busy->set_device_name(_T("NOT GATE(PRINTER BUSY)"));
72
73         pcm = new PCM1BIT(this, emu);
74         rtc = new RP5C01(this, emu);    // RP-5C15
75 //      sasi = new SASI(this, emu);
76         dma = new UPD71071(this, emu);
77         opn = new YM2203(this, emu);
78         pio1 = new Z80PIO(this, emu);
79         sio = new Z80SIO(this, emu);
80         
81         crtc = new CRTC(this, emu);
82         floppy = new FLOPPY(this, emu);
83         joystick = new JOYSTICK(this, emu);
84         keyboard = new KEYBOARD(this, emu);
85         memory = new MEMORY(this, emu);
86         mouse = new MOUSE(this, emu);
87         printer = new PRINTER(this, emu);
88         rst = new RESET(this, emu);
89         serial = new SERIAL(this, emu);
90         sysport = new SYSPORT(this, emu);
91         
92         // set contexts
93         event->set_context_cpu(cpu);
94         event->set_context_sound(opn);
95         event->set_context_sound(pcm);
96         event->set_context_sound(fdc->get_context_noise_seek());
97         event->set_context_sound(fdc->get_context_noise_head_down());
98         event->set_context_sound(fdc->get_context_noise_head_up());
99         
100         pit->set_constant_clock(0, 31250);
101         pit->set_constant_clock(2, 31250);
102         pit->set_context_ch0(pit, SIG_I8253_CLOCK_1, 1);
103         pit->set_context_ch0(pic, SIG_I8259_CHIP0 | SIG_I8259_IR0, 1);
104         pit->set_context_ch1(pic, SIG_I8259_CHIP0 | SIG_I8259_IR6, 1);
105         pit->set_context_ch2(pic, SIG_I8259_CHIP1 | SIG_I8259_IR0, 1);
106         pio0->set_context_port_c(rst, SIG_RESET_CONTROL, 0xff, 0);
107         pio0->set_context_port_c(crtc, SIG_CRTC_MASK, 0x01, 0);
108         pio0->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x04, 0);
109         // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
110         //pcm->set_realtime_render(true);
111
112         
113         pic->set_context_cpu(cpu);
114         fdc->set_context_drq(dma, SIG_UPD71071_CH1, 1);
115         fdc->set_context_irq(pic, SIG_I8259_CHIP0 | SIG_I8259_IR5, 1);
116         not_busy->set_context_out(pic, SIG_I8259_CHIP1 | SIG_I8259_IR1, 1);
117         rtc->set_context_alarm(pic, SIG_I8259_CHIP1 | SIG_I8259_IR2, 1);
118         rtc->set_context_pulse(opn, SIG_YM2203_PORT_B, 8);
119 //      sasi->set_context_drq(dma, SIG_UPD71071_CH0, 1);
120 //      sasi->set_context_irq(pic, SIG_I8259_CHIP0 | SIG_I8259_IR4, 1);
121         dma->set_context_memory(memory);
122 //      dma->set_context_ch0(sasi);
123         dma->set_context_ch1(fdc);
124         dma->set_context_tc(pic, SIG_I8259_CHIP0 | SIG_I8259_IR3, 1);
125         opn->set_context_irq(pic, SIG_I8259_CHIP1 | SIG_I8259_IR7, 1);
126         opn->set_context_port_a(crtc, SIG_CRTC_PALLETE, 0x04, 0);
127         opn->set_context_port_a(mouse, SIG_MOUSE_SEL, 0x08, 0);
128         pio1->set_context_port_a(crtc, SIG_CRTC_COLUMN_SIZE, 0x20, 0);
129         pio1->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0xff, 0);
130         sio->set_context_intr(pic, SIG_I8259_CHIP0 | SIG_I8259_IR2);
131         sio->set_context_dtr(1, mouse, SIG_MOUSE_DTR, 1);
132         
133         crtc->set_context_pic(pic);
134         crtc->set_context_pio(pio0);
135         crtc->set_vram_ptr(memory->get_vram());
136         crtc->set_tvram_ptr(memory->get_tvram());
137         crtc->set_kanji_ptr(memory->get_kanji());
138         crtc->set_pcg_ptr(memory->get_pcg());
139         floppy->set_context_fdc(fdc);
140         keyboard->set_context_pio0(pio0);
141         keyboard->set_context_pio1(pio1);
142         memory->set_context_crtc(crtc);
143         mouse->set_context_sio(sio);
144         if(config.printer_type == 0) {
145                 PRNFILE *prnfile = new PRNFILE(this, emu);
146                 prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
147                 printer->set_context_prn(prnfile);
148         } else if(config.printer_type == 1) {
149                 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
150                 mz1p17->mode = MZ1P17_MODE_MZ1;
151                 mz1p17->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
152                 printer->set_context_prn(mz1p17);
153 //      } else if(config.printer_type == 2) {
154 //              PCPR201 *pcpr201 = new PCPR201(this, emu);
155 //              pcpr201->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
156 //              printer->set_context_prn(pcpr201);
157         } else {
158                 printer->set_context_prn(dummy);
159         }
160         serial->set_context_sio(sio);
161         sysport->set_context_pit(pit);
162         sysport->set_context_sio(sio);
163         
164         // cpu bus
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_range_rw(0x70, 0x7f, dma);
177         io->set_iomap_alias_rw(0x80, pic, I8259_ADDR_CHIP0 | 0);
178         io->set_iomap_alias_rw(0x81, pic, I8259_ADDR_CHIP0 | 1);
179         io->set_iomap_alias_rw(0x82, pic, I8259_ADDR_CHIP1 | 0);
180         io->set_iomap_alias_rw(0x83, pic, I8259_ADDR_CHIP1 | 1);
181         io->set_iomap_range_rw(0x8c, 0x8d, memory);
182         io->set_iovalue_single_r(0x8e, 0xff);   // dipswitch
183         io->set_flipflop_single_rw(0x8f, 0x00); // shut
184         io->set_iomap_range_rw(0xa0, 0xa3, serial);
185         for(uint32_t p = 0xae; p <= 0x1fae; p += 0x100) {
186                 io->set_iomap_single_w(p, crtc);
187         }
188 //      io->set_iomap_single_rw(0xaf, sasi);
189         io->set_iomap_range_rw(0xb0, 0xb3, serial);
190         io->set_iomap_single_r(0xbe, sysport);
191         io->set_iomap_range_rw(0xc8, 0xc9, opn);
192         io->set_iovalue_single_r(0xca, 0x7f);   // voice communication ???
193         for(uint32_t p = 0xcc; p <= 0xfcc; p += 0x100) {
194                 io->set_iomap_alias_rw(p, rtc, p >> 8);
195         }
196         io->set_iomap_single_w(0xcd, serial);
197         io->set_iomap_single_rw(0xce, memory);
198         io->set_iomap_range_rw(0xd8, 0xdb, fdc);
199         io->set_iomap_range_w(0xdc, 0xdf, floppy);
200         io->set_iomap_range_rw(0xe0, 0xe3, pio0);
201         io->set_iomap_range_rw(0xe4, 0xe7, pit);
202         io->set_iomap_range_rw(0xe8, 0xeb, pio1);
203         io->set_iomap_single_rw(0xef, joystick);
204 //      io->set_iomap_range_w(0xf0, 0xf3, sysport);
205         io->set_iomap_single_w(0x170, crtc);
206         io->set_iomap_single_w(0x172, crtc);
207         io->set_iomap_single_w(0x174, crtc);
208         io->set_iomap_single_w(0x176, crtc);
209         io->set_iomap_range_w(0x178, 0x17b, crtc);
210         io->set_iomap_range_rw(0x1fe, 0x1ff, printer);
211         io->set_iomap_single_w(0x270, crtc);
212         io->set_iomap_single_w(0x272, crtc);
213         io->set_iomap_single_rw(0x274, memory);
214         
215         // initialize all devices
216         for(DEVICE* device = first_device; device; device = device->next_device) {
217                 device->initialize();
218         }
219         decl_state();
220 }
221
222 VM::~VM()
223 {
224         // delete all devices
225         for(DEVICE* device = first_device; device;) {
226                 DEVICE *next_device = device->next_device;
227                 device->release();
228                 delete device;
229                 device = next_device;
230         }
231 }
232
233 DEVICE* VM::get_device(int id)
234 {
235         for(DEVICE* device = first_device; device; device = device->next_device) {
236                 if(device->this_device_id == id) {
237                         return device;
238                 }
239         }
240         return NULL;
241 }
242
243 // ----------------------------------------------------------------------------
244 // drive virtual machine
245 // ----------------------------------------------------------------------------
246
247 void VM::reset()
248 {
249         // reset all devices
250         for(DEVICE* device = first_device; device; device = device->next_device) {
251                 device->reset();
252         }
253         // temporary fix...
254         for(DEVICE* device = first_device; device; device = device->next_device) {
255                 device->reset();
256         }
257         
258         // set initial port status
259         pio0->write_signal(SIG_I8255_PORT_B, 0x7c, 0xff);
260         opn->write_signal(SIG_YM2203_PORT_B, 0x37, 0xff);
261 }
262
263 void VM::cpu_reset()
264 {
265         cpu->reset();
266 }
267
268 void VM::run()
269 {
270         event->drive();
271 }
272
273 // ----------------------------------------------------------------------------
274 // debugger
275 // ----------------------------------------------------------------------------
276
277 #ifdef USE_DEBUGGER
278 DEVICE *VM::get_cpu(int index)
279 {
280         if(index == 0) {
281                 return cpu;
282         }
283         return NULL;
284 }
285 #endif
286
287 // ----------------------------------------------------------------------------
288 // draw screen
289 // ----------------------------------------------------------------------------
290
291 void VM::draw_screen()
292 {
293         crtc->draw_screen();
294 }
295
296 // ----------------------------------------------------------------------------
297 // soud manager
298 // ----------------------------------------------------------------------------
299
300 void VM::initialize_sound(int rate, int samples)
301 {
302         // init sound manager
303         event->initialize_sound(rate, samples);
304         
305         // init sound gen
306         opn->initialize_sound(rate, 2000000, samples, 0, -8);
307         pcm->initialize_sound(rate, 4096);
308 }
309
310 uint16_t* VM::create_sound(int* extra_frames)
311 {
312         return event->create_sound(extra_frames);
313 }
314
315 int VM::get_sound_buffer_ptr()
316 {
317         return event->get_sound_buffer_ptr();
318 }
319
320 #ifdef USE_SOUND_VOLUME
321 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
322 {
323         if(ch == 0) {
324                 opn->set_volume(0, decibel_l, decibel_r);
325         } else if(ch == 1) {
326                 opn->set_volume(1, decibel_l, decibel_r);
327         } else if(ch == 2) {
328                 pcm->set_volume(0, decibel_l, decibel_r);
329         } else if(ch == 3) {
330                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
331                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
332                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
333         }
334 }
335 #endif
336
337 // ----------------------------------------------------------------------------
338 // user interface
339 // ----------------------------------------------------------------------------
340
341 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
342 {
343         fdc->open_disk(drv, file_path, bank);
344 }
345
346 void VM::close_floppy_disk(int drv)
347 {
348         fdc->close_disk(drv);
349 }
350
351 bool VM::is_floppy_disk_inserted(int drv)
352 {
353         return fdc->is_disk_inserted(drv);
354 }
355
356 void VM::is_floppy_disk_protected(int drv, bool value)
357 {
358         fdc->is_disk_protected(drv, value);
359 }
360
361 bool VM::is_floppy_disk_protected(int drv)
362 {
363         return fdc->is_disk_protected(drv);
364 }
365
366 uint32_t VM::is_floppy_disk_accessed()
367 {
368         return fdc->read_signal(0);
369 }
370
371 bool VM::is_frame_skippable()
372 {
373         return event->is_frame_skippable();
374 }
375
376 void VM::update_config()
377 {
378         for(DEVICE* device = first_device; device; device = device->next_device) {
379                 device->update_config();
380         }
381 }
382
383 #define STATE_VERSION   4
384
385 #include "../../statesub.h"
386 #include "../../qt/gui/csp_logger.h"
387 extern CSP_Logger DLL_PREFIX_I *csp_logger;
388
389 void VM::decl_state(void)
390 {
391         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MZ_2800_HEAD")), csp_logger);
392         for(DEVICE* device = first_device; device; device = device->next_device) {
393                 device->decl_state();
394         }
395 }
396
397 void VM::save_state(FILEIO* state_fio)
398 {
399         //state_fio->FputUint32(STATE_VERSION);
400         
401         if(state_entry != NULL) {
402                 state_entry->save_state(state_fio);
403         }
404         for(DEVICE* device = first_device; device; device = device->next_device) {
405                 device->save_state(state_fio);
406         }
407 }
408
409 bool VM::load_state(FILEIO* state_fio)
410 {
411         //if(state_fio->FgetUint32() != STATE_VERSION) {
412         //      return false;
413         //}
414         bool mb = false;
415         if(state_entry != NULL) {
416                 mb = state_entry->load_state(state_fio);
417         }
418         if(!mb) {
419                 emu->out_debug_log("INFO: HEADER DATA ERROR");
420                 return false;
421         }
422         for(DEVICE* device = first_device; device; device = device->next_device) {
423                 if(!device->load_state(state_fio)) {
424                         return false;
425                 }
426         }
427         return true;
428 }
429