OSDN Git Service

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