OSDN Git Service

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