OSDN Git Service

1451c92e5854eec2b743bee679c915a77df950b4
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz5500 / mz5500.cpp
1 /*
2         SHARP MZ-5500 Emulator 'EmuZ-5500'
3
4         Author : Takeda.Toshiya
5         Date   : 2008.04.10 -
6
7         [ virtual machine ]
8 */
9
10 #include "mz5500.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../disk.h"
16 #include "../i8237.h"
17 #include "../i8255.h"
18 #include "../i8259.h"
19 #include "../i286.h"
20 #include "../io.h"
21 #include "../ls393.h"
22 #include "../rp5c01.h"
23 #include "../upd7220.h"
24 #include "../upd765a.h"
25 #include "../ym2203.h"
26 #include "../z80ctc.h"
27 #include "../z80sio.h"
28
29 #ifdef USE_DEBUGGER
30 #include "../debugger.h"
31 #endif
32
33 #include "display.h"
34 #include "keyboard.h"
35 #include "memory.h"
36 #include "sysport.h"
37
38 #include "../../fileio.h"
39
40 // ----------------------------------------------------------------------------
41 // initialize
42 // ----------------------------------------------------------------------------
43
44 VM::VM(EMU* parent_emu) : emu(parent_emu)
45 {
46         // create devices
47         first_device = last_device = NULL;
48         dummy = new DEVICE(this, emu);  // must be 1st device
49         event = new EVENT(this, emu);   // must be 2nd device
50         
51         dma = new I8237(this, emu);
52         pio = new I8255(this, emu);
53         pic = new I8259(this, emu);
54         cpu = new I286(this, emu);
55         io = new IO(this, emu);
56         div = new LS393(this, emu);
57         rtc = new RP5C01(this, emu);
58         gdc = new UPD7220(this, emu);
59         fdc = new UPD765A(this, emu);
60         psg = new YM2203(this, emu);    // AY-3-8912
61         ctc0 = new Z80CTC(this, emu);
62 #if defined(_MZ6500) || defined(_MZ6550)
63         ctc1 = new Z80CTC(this, emu);
64 #endif
65         sio = new Z80SIO(this, emu);
66         
67         display = new DISPLAY(this, emu);
68         keyboard = new KEYBOARD(this, emu);
69         memory = new MEMORY(this, emu);
70         sysport = new SYSPORT(this, emu);
71         
72         // set contexts
73         event->set_context_cpu(cpu);
74         event->set_context_sound(psg);
75         
76         dma->set_context_memory(memory);
77         dma->set_context_ch1(fdc);
78         pio->set_context_port_c(keyboard, SIG_KEYBOARD_INPUT, 3, 0);
79         pic->set_context_cpu(cpu);
80         div->set_context_2qb(ctc0, SIG_Z80CTC_TRIG_3, 1);
81 #if defined(_MZ6500) || defined(_MZ6550)
82         div->set_context_1qb(ctc1, SIG_Z80CTC_TRIG_0, 1);
83         div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_1, 1);
84         div->set_context_2qb(ctc1, SIG_Z80CTC_TRIG_2, 1);
85         div->set_context_2qd(ctc1, SIG_Z80CTC_TRIG_3, 1);
86 #endif
87         rtc->set_context_alarm(pic, SIG_I8259_IR0 | SIG_I8259_CHIP1, 1);
88         gdc->set_vram_ptr(memory->get_vram(), 0x80000);
89         gdc->set_context_vsync(pic, SIG_I8259_IR0 | SIG_I8259_CHIP0, 1);
90         fdc->set_context_irq(pic, SIG_I8259_IR1 | SIG_I8259_CHIP1, 1);
91         fdc->set_context_drq(dma, SIG_I8237_CH1, 1);
92         psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP0, 0x20, 0);
93         psg->set_context_port_a(pic, SIG_I8259_IR7 | SIG_I8259_CHIP1, 0x40, 0);
94         psg->set_context_port_a(memory, SIG_MEMORY_BANK, 0xe0, 0);
95         ctc0->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
96         ctc0->set_context_zc0(div, SIG_LS393_CLK, 1);
97         ctc0->set_context_zc1(sio, SIG_Z80SIO_TX_CLK_CH0, 1);
98         ctc0->set_context_zc1(sio, SIG_Z80SIO_RX_CLK_CH0, 1);
99         ctc0->set_context_zc2(sio, SIG_Z80SIO_TX_CLK_CH1, 1);
100         ctc0->set_context_zc2(sio, SIG_Z80SIO_RX_CLK_CH1, 1);
101 #if defined(_MZ6500) || defined(_MZ6550)
102         ctc0->set_context_child(ctc1);
103         ctc1->set_context_intr(pic, SIG_I8259_IR5 | SIG_I8259_CHIP0);
104 #endif
105         sio->set_context_intr(pic, SIG_I8259_IR1 | SIG_I8259_CHIP0);
106         
107         display->set_vram_ptr(memory->get_vram());
108         display->set_sync_ptr(gdc->get_sync());
109         display->set_ra_ptr(gdc->get_ra());
110         display->set_cs_ptr(gdc->get_cs());
111         display->set_ead_ptr(gdc->get_ead());
112         keyboard->set_context_pio(pio);
113         keyboard->set_context_pic(pic);
114         memory->set_context_cpu(cpu);
115         sysport->set_context_fdc(fdc);
116         sysport->set_context_ctc(ctc0);
117         sysport->set_context_sio(sio);
118         
119         // cpu bus
120         cpu->set_context_mem(memory);
121         cpu->set_context_io(io);
122         cpu->set_context_intr(pic);
123 #ifdef SINGLE_MODE_DMA
124         cpu->set_context_dma(dma);
125 #endif
126 #ifdef USE_DEBUGGER
127         cpu->set_context_debugger(new DEBUGGER(this, emu));
128 #endif
129         
130         // i/o bus
131         io->set_iomap_range_rw(0x00, 0x0f, dma);
132         io->set_iomap_range_rw(0x10, 0x1f, pio);
133         io->set_iomap_range_rw(0x20, 0x2f, fdc);
134         for(int i = 0x30; i < 0x40; i += 2) {
135                 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP0 | ((i >> 1) & 1));
136         }
137         for(int i = 0x40; i < 0x50; i += 2) {
138                 io->set_iomap_alias_rw(i, pic, I8259_ADDR_CHIP1 | ((i >> 1) & 1));
139         }
140         io->set_iomap_range_w(0x50, 0x5f, memory);
141         io->set_iomap_range_r(0x60, 0x6f, sysport);
142         io->set_iomap_range_w(0x70, 0x7f, sysport);
143 #if defined(_MZ6500) || defined(_MZ6550)
144         io->set_iomap_single_rw(0xcd, memory);
145 #endif
146         for(int i = 0x100; i < 0x110; i += 2) {
147                 io->set_iomap_alias_rw(i, gdc, (i >> 1) & 1);
148         }
149         io->set_iomap_range_rw(0x110, 0x17f, display);
150         io->set_iomap_range_rw(0x200, 0x20f, sio);
151         io->set_iomap_range_rw(0x210, 0x21f, ctc0);
152         io->set_iomap_range_rw(0x220, 0x22f, rtc);
153         for(int i = 0x230; i < 0x240; i++) {
154                 io->set_iomap_alias_rw(i, psg, ~i & 1);
155         }
156         io->set_iomap_range_r(0x240, 0x25f, sysport);
157         io->set_iomap_range_w(0x260, 0x26f, sysport);
158         io->set_iomap_range_r(0x270, 0x27f, sysport);
159         
160         // initialize all devices
161         for(DEVICE* device = first_device; device; device = device->next_device) {
162                 device->initialize();
163         }
164         for(int i = 0; i < 4; i++) {
165 #if defined(_MZ6500) || defined(_MZ6550)
166                 fdc->set_drive_type(i, DRIVE_TYPE_2HD);
167 #else
168                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
169 #endif
170         }
171 }
172
173 VM::~VM()
174 {
175         // delete all devices
176         for(DEVICE* device = first_device; device;) {
177                 DEVICE *next_device = device->next_device;
178                 device->release();
179                 delete device;
180                 device = next_device;
181         }
182 }
183
184 DEVICE* VM::get_device(int id)
185 {
186         for(DEVICE* device = first_device; device; device = device->next_device) {
187                 if(device->this_device_id == id) {
188                         return device;
189                 }
190         }
191         return NULL;
192 }
193
194 // ----------------------------------------------------------------------------
195 // drive virtual machine
196 // ----------------------------------------------------------------------------
197
198 void VM::reset()
199 {
200         // reset all devices
201         for(DEVICE* device = first_device; device; device = device->next_device) {
202                 device->reset();
203         }
204 }
205
206 void VM::special_reset()
207 {
208         // nmi
209         cpu->write_signal(SIG_CPU_NMI, 1, 1);
210         sysport->nmi_reset();
211 }
212
213 void VM::run()
214 {
215         event->drive();
216 }
217
218 double VM::frame_rate()
219 {
220         return event->frame_rate();
221 }
222
223 // ----------------------------------------------------------------------------
224 // debugger
225 // ----------------------------------------------------------------------------
226
227 #ifdef USE_DEBUGGER
228 DEVICE *VM::get_cpu(int index)
229 {
230         if(index == 0) {
231                 return cpu;
232         }
233         return NULL;
234 }
235 #endif
236
237 // ----------------------------------------------------------------------------
238 // draw screen
239 // ----------------------------------------------------------------------------
240
241 void VM::draw_screen()
242 {
243         display->draw_screen();
244 }
245
246 int VM::access_lamp()
247 {
248         uint32 status = fdc->read_signal(0);
249         return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
250 }
251
252 // ----------------------------------------------------------------------------
253 // soud manager
254 // ----------------------------------------------------------------------------
255
256 void VM::initialize_sound(int rate, int samples)
257 {
258         // init sound manager
259         event->initialize_sound(rate, samples);
260         
261         // init sound gen
262         psg->init(rate, 4000000, samples, 0, 0);
263 }
264
265 uint16* VM::create_sound(int* extra_frames)
266 {
267         return event->create_sound(extra_frames);
268 }
269
270 int VM::sound_buffer_ptr()
271 {
272         return event->sound_buffer_ptr();
273 }
274
275 // ----------------------------------------------------------------------------
276 // notify key
277 // ----------------------------------------------------------------------------
278
279 void VM::key_down(int code, bool repeat)
280 {
281         keyboard->key_down(code);
282 }
283
284 void VM::key_up(int code)
285 {
286 //      keyboard->key_up(code);
287 }
288
289 // ----------------------------------------------------------------------------
290 // user interface
291 // ----------------------------------------------------------------------------
292
293 void VM::open_disk(int drv, _TCHAR* file_path, int offset)
294 {
295         fdc->open_disk(drv, file_path, offset);
296 }
297
298 void VM::close_disk(int drv)
299 {
300         fdc->close_disk(drv);
301 }
302
303 bool VM::disk_inserted(int drv)
304 {
305         return fdc->disk_inserted(drv);
306 }
307
308 bool VM::now_skip()
309 {
310         return event->now_skip();
311 }
312
313 void VM::update_config()
314 {
315         for(DEVICE* device = first_device; device; device = device->next_device) {
316                 device->update_config();
317         }
318 }
319
320 #define STATE_VERSION   2
321
322 void VM::save_state(FILEIO* state_fio)
323 {
324         state_fio->FputUint32(STATE_VERSION);
325         
326         for(DEVICE* device = first_device; device; device = device->next_device) {
327                 device->save_state(state_fio);
328         }
329 }
330
331 bool VM::load_state(FILEIO* state_fio)
332 {
333         if(state_fio->FgetUint32() != STATE_VERSION) {
334                 return false;
335         }
336         for(DEVICE* device = first_device; device; device = device->next_device) {
337                 if(!device->load_state(state_fio)) {
338                         return false;
339                 }
340         }
341         return true;
342 }
343