OSDN Git Service

00c49ea68e7addbf480c1a1fba989ea05c7ea499
[csp-qt/common_source_project-fm7.git] / source / src / vm / qc10 / qc10.cpp
1 /*
2         EPSON QC-10 Emulator 'eQC-10'
3
4         Author : Takeda.Toshiya
5         Date   : 2008.02.13 -
6
7         [ virtual machine ]
8 */
9
10 #include "qc10.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../disk.h"
16 #include "../hd146818p.h"
17 #include "../i8237.h"
18 #include "../i8253.h"
19 #include "../i8255.h"
20 #include "../i8259.h"
21 #include "../io.h"
22 #include "../pcm1bit.h"
23 #include "../upd7220.h"
24 #include "../upd765a.h"
25 #include "../z80.h"
26 #include "../z80sio.h"
27
28 #ifdef USE_DEBUGGER
29 #include "../debugger.h"
30 #endif
31
32 #include "display.h"
33 #include "floppy.h"
34 #include "keyboard.h"
35 #include "memory.h"
36 #include "mfont.h"
37
38 // ----------------------------------------------------------------------------
39 // initialize
40 // ----------------------------------------------------------------------------
41
42 VM::VM(EMU* parent_emu) : emu(parent_emu)
43 {
44         // create devices
45         first_device = last_device = NULL;
46         dummy = new DEVICE(this, emu);  // must be 1st device
47         event = new EVENT(this, emu);   // must be 2nd device
48         
49         rtc = new HD146818P(this, emu);
50         dma0 = new I8237(this, emu);
51         dma1 = new I8237(this, emu);
52         pit0 = new I8253(this, emu);
53         pit1 = new I8253(this, emu);
54         pio = new I8255(this, emu);
55         pic = new I8259(this, emu);
56         io = new IO(this, emu);
57         pcm = new PCM1BIT(this, emu);
58         gdc = new UPD7220(this, emu);
59         fdc = new UPD765A(this, emu);
60         cpu = new Z80(this, emu);
61         sio = new Z80SIO(this, emu);    // uPD7201
62         
63         display = new DISPLAY(this, emu);
64         floppy = new FLOPPY(this, emu);
65         keyboard = new KEYBOARD(this, emu);
66         memory = new MEMORY(this, emu);
67         mfont = new MFONT(this, emu);
68         
69         // set contexts
70         event->set_context_cpu(cpu);
71         event->set_context_sound(pcm);
72         
73         rtc->set_context_intr(pic, SIG_I8259_IR2 | SIG_I8259_CHIP1, 1);
74         dma0->set_context_memory(memory);
75         dma0->set_context_ch0(fdc);
76         dma0->set_context_ch1(gdc);
77 #ifdef SINGLE_MODE_DMA
78         dma0->set_context_child_dma(dma1);
79 #endif
80         dma1->set_context_memory(memory);
81         pit0->set_context_ch0(memory, SIG_MEMORY_PCM, 1);
82         pit0->set_context_ch1(pic, SIG_I8259_IR5 | SIG_I8259_CHIP1, 1);
83         pit0->set_context_ch2(pic, SIG_I8259_IR1 | SIG_I8259_CHIP0, 1);
84         pit0->set_constant_clock(2, CPU_CLOCKS >> 1);   // 1.9968MHz
85         pit1->set_context_ch0(pcm, SIG_PCM1BIT_SIGNAL, 1);
86         pit1->set_context_ch1(pit0, SIG_I8253_CLOCK_0, 1);
87         pit1->set_context_ch1(pit0, SIG_I8253_CLOCK_1, 1);
88         pit1->set_context_ch1(sio, SIG_Z80SIO_TX_CLK_CH0, 1);
89         pit1->set_context_ch1(sio, SIG_Z80SIO_RX_CLK_CH0, 1);
90         pit1->set_context_ch2(sio, SIG_Z80SIO_TX_CLK_CH1, 1);
91         pit1->set_context_ch2(sio, SIG_Z80SIO_RX_CLK_CH1, 1);
92         pit1->set_constant_clock(0, CPU_CLOCKS >> 1);   // 1.9968MHz
93         pit1->set_constant_clock(1, CPU_CLOCKS >> 1);   // 1.9968MHz
94         pit1->set_constant_clock(2, CPU_CLOCKS >> 1);   // 1.9968MHz
95         pio->set_context_port_c(pic, SIG_I8259_IR0 | SIG_I8259_CHIP1, 8, 0);
96         pic->set_context_cpu(cpu);
97         gdc->set_context_drq(dma0, SIG_I8237_CH1, 1);
98         gdc->set_vram_ptr(display->get_vram(), VRAM_SIZE);
99         // IR5 of I8259 #0 is from light pen
100         fdc->set_context_irq(pic, SIG_I8259_IR6 | SIG_I8259_CHIP0, 1);
101         fdc->set_context_irq(memory, SIG_MEMORY_FDC_IRQ, 1);
102         fdc->set_context_drq(dma0, SIG_I8237_CH0, 1);
103         sio->set_context_intr(pic, SIG_I8259_IR4 | SIG_I8259_CHIP0);
104         sio->set_context_send(0, keyboard, SIG_KEYBOARD_RECV);
105 //      sio->set_tx_clock(0, 1200 * 16);        // 1200 baud for keyboard
106 //      sio->set_rx_clock(0, 1200 * 16);        // clock is from 8253 ch1 (1.9968MHz/104)
107 //      sio->set_tx_clock(1, 9600 * 16);        // 9600 baud for RS-232C
108 //      sio->set_rx_clock(1, 9600 * 16);        // clock is from 8253 ch2 (1.9968MHz/13)
109         
110         display->set_context_gdc(gdc);
111         display->set_sync_ptr(gdc->get_sync());
112         display->set_zoom_ptr(gdc->get_zoom());
113         display->set_ra_ptr(gdc->get_ra());
114         display->set_cs_ptr(gdc->get_cs());
115         display->set_ead_ptr(gdc->get_ead());
116         floppy->set_context_fdc(fdc);
117         floppy->set_context_mem(memory);
118         keyboard->set_context_sio(sio);
119         memory->set_context_pit(pit0);
120         memory->set_context_pcm(pcm);
121         memory->set_context_fdc(fdc);
122         mfont->set_context_pic(pic);
123         
124         // cpu bus
125         cpu->set_context_mem(memory);
126         cpu->set_context_io(io);
127         cpu->set_context_intr(pic);
128 #ifdef SINGLE_MODE_DMA
129         cpu->set_context_dma(dma0);
130 #endif
131 #ifdef USE_DEBUGGER
132         cpu->set_context_debugger(new DEBUGGER(this, emu));
133 #endif
134         
135         // i/o bus
136         io->set_iomap_range_rw(0x00, 0x03, pit0);
137         io->set_iomap_range_rw(0x04, 0x07, pit1);
138         io->set_iomap_alias_rw(0x08, pic, I8259_ADDR_CHIP0 | 0);
139         io->set_iomap_alias_rw(0x09, pic, I8259_ADDR_CHIP0 | 1);
140         io->set_iomap_alias_rw(0x0c, pic, I8259_ADDR_CHIP1 | 0);
141         io->set_iomap_alias_rw(0x0d, pic, I8259_ADDR_CHIP1 | 1);
142         io->set_iomap_alias_rw(0x10, sio, 0);
143         io->set_iomap_alias_rw(0x11, sio, 2);
144         io->set_iomap_alias_rw(0x12, sio, 1);
145         io->set_iomap_alias_rw(0x13, sio, 3);
146         io->set_iomap_range_rw(0x14, 0x17, pio);
147         io->set_iomap_range_rw(0x18, 0x1b, memory);
148         io->set_iomap_range_w(0x1c, 0x23, memory);
149         io->set_iomap_range_rw(0x2c, 0x2d, display);
150         io->set_iomap_range_r(0x30, 0x33, memory);
151         io->set_iomap_range_w(0x30, 0x33, floppy);
152         io->set_iomap_range_rw(0x34, 0x35, fdc);
153         io->set_iomap_range_rw(0x38, 0x3b, gdc);
154         io->set_iomap_range_rw(0x3c, 0x3d, rtc);
155         io->set_iomap_range_rw(0x40, 0x4f, dma0);
156         io->set_iomap_range_rw(0x50, 0x5f, dma1);
157         io->set_iomap_range_rw(0xfc, 0xfd, mfont);
158         
159         // initialize all devices
160         for(DEVICE* device = first_device; device; device = device->next_device) {
161                 device->initialize();
162         }
163         for(int i = 0; i < 4; i++) {
164                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
165         }
166 }
167
168 VM::~VM()
169 {
170         // delete all devices
171         for(DEVICE* device = first_device; device;) {
172                 DEVICE *next_device = device->next_device;
173                 device->release();
174                 delete device;
175                 device = next_device;
176         }
177 }
178
179 DEVICE* VM::get_device(int id)
180 {
181         for(DEVICE* device = first_device; device; device = device->next_device) {
182                 if(device->this_device_id == id) {
183                         return device;
184                 }
185         }
186         return NULL;
187 }
188
189 // ----------------------------------------------------------------------------
190 // drive virtual machine
191 // ----------------------------------------------------------------------------
192
193 void VM::reset()
194 {
195         // reset all devices
196         for(DEVICE* device = first_device; device; device = device->next_device) {
197                 device->reset();
198         }
199 }
200
201 void VM::run()
202 {
203         event->drive();
204 }
205
206 double VM::frame_rate()
207 {
208         return event->frame_rate();
209 }
210
211 // ----------------------------------------------------------------------------
212 // debugger
213 // ----------------------------------------------------------------------------
214
215 #ifdef USE_DEBUGGER
216 DEVICE *VM::get_cpu(int index)
217 {
218         if(index == 0) {
219                 return cpu;
220         }
221         return NULL;
222 }
223 #endif
224
225 // ----------------------------------------------------------------------------
226 // draw screen
227 // ----------------------------------------------------------------------------
228
229 void VM::draw_screen()
230 {
231         display->draw_screen();
232 }
233
234 int VM::access_lamp()
235 {
236         uint32 status = fdc->read_signal(0);
237         return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
238 }
239
240 // ----------------------------------------------------------------------------
241 // soud manager
242 // ----------------------------------------------------------------------------
243
244 void VM::initialize_sound(int rate, int samples)
245 {
246         // init sound manager
247         event->initialize_sound(rate, samples);
248         
249         // init sound gen
250         pcm->init(rate, 4000);
251 }
252
253 uint16* VM::create_sound(int* extra_frames)
254 {
255         return event->create_sound(extra_frames);
256 }
257
258 int VM::sound_buffer_ptr()
259 {
260         return event->sound_buffer_ptr();
261 }
262
263 // ----------------------------------------------------------------------------
264 // notify key
265 // ----------------------------------------------------------------------------
266
267 void VM::key_down(int code, bool repeat)
268 {
269         keyboard->key_down(code);
270 }
271
272 void VM::key_up(int code)
273 {
274         keyboard->key_up(code);
275 }
276
277 // ----------------------------------------------------------------------------
278 // user interface
279 // ----------------------------------------------------------------------------
280
281 void VM::open_disk(int drv, _TCHAR* file_path, int bank)
282 {
283         fdc->open_disk(drv, file_path, bank);
284 }
285
286 void VM::close_disk(int drv)
287 {
288         fdc->close_disk(drv);
289 }
290
291 bool VM::disk_inserted(int drv)
292 {
293         return fdc->disk_inserted(drv);
294 }
295
296 void VM::set_disk_protected(int drv, bool value)
297 {
298         fdc->set_disk_protected(drv, value);
299 }
300
301 bool VM::get_disk_protected(int drv)
302 {
303         return fdc->get_disk_protected(drv);
304 }
305
306 bool VM::now_skip()
307 {
308         return event->now_skip();
309 }
310
311 void VM::update_config()
312 {
313         for(DEVICE* device = first_device; device; device = device->next_device) {
314                 device->update_config();
315         }
316 }
317
318 #define STATE_VERSION   1
319
320 void VM::save_state(FILEIO* state_fio)
321 {
322         state_fio->FputUint32(STATE_VERSION);
323         
324         for(DEVICE* device = first_device; device; device = device->next_device) {
325                 device->save_state(state_fio);
326         }
327 }
328
329 bool VM::load_state(FILEIO* state_fio)
330 {
331         if(state_fio->FgetUint32() != STATE_VERSION) {
332                 return false;
333         }
334         for(DEVICE* device = first_device; device; device = device->next_device) {
335                 if(!device->load_state(state_fio)) {
336                         return false;
337                 }
338         }
339         return true;
340 }
341