OSDN Git Service

92944b88fd22c5d7c3f6a7e75de4455516ec1690
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz80k / mz80k.cpp
1 /*
2         SHARP MZ-80K/C Emulator 'EmuZ-80K'
3         SHARP MZ-1200 Emulator 'EmuZ-1200'
4
5         Author : Takeda.Toshiya
6         Date   : 2010.08.18-
7
8         SHARP MZ-80A Emulator 'EmuZ-80A'
9         Modify : Hideki Suga
10         Date   : 2014.12.10 -
11
12         [ virtual machine ]
13 */
14
15 #include "mz80k.h"
16 #include "../../emu.h"
17 #include "../device.h"
18 #include "../event.h"
19
20 #if defined(_MZ1200) || defined(_MZ80A)
21 #include "../and.h"
22 #endif
23 #include "../datarec.h"
24 #include "../i8253.h"
25 #include "../i8255.h"
26 #include "../ls393.h"
27 #include "../pcm1bit.h"
28 #include "../z80.h"
29
30 #ifdef USE_DEBUGGER
31 #include "../debugger.h"
32 #endif
33
34 #include "keyboard.h"
35 #include "memory.h"
36
37 #if defined(SUPPORT_MZ80AIF)
38 #include "../io.h"
39 #include "../disk.h"
40 #include "../mb8877.h"
41 #include "mz80aif.h"
42 #elif defined(SUPPORT_MZ80FIO)
43 #include "../io.h"
44 #include "../disk.h"
45 #include "../t3444a.h"
46 #include "mz80fio.h"
47 #endif
48
49 // ----------------------------------------------------------------------------
50 // initialize
51 // ----------------------------------------------------------------------------
52
53 VM::VM(EMU* parent_emu) : emu(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         
60 #if defined(_MZ1200) || defined(_MZ80A)
61         and_int = new AND(this, emu);
62 #endif
63         drec = new DATAREC(this, emu);
64         ctc = new I8253(this, emu);
65         pio = new I8255(this, emu);
66         counter = new LS393(this, emu);
67         pcm = new PCM1BIT(this, emu);
68         cpu = new Z80(this, emu);
69         
70         keyboard = new KEYBOARD(this, emu);
71         memory = new MEMORY(this, emu);
72         
73 #if defined(SUPPORT_MZ80AIF)
74         io = new IO(this, emu);
75         fdc = new MB8877(this, emu);    // mb8866
76         mz80aif = new MZ80AIF(this, emu);
77 #elif defined(SUPPORT_MZ80FIO)
78         io = new IO(this, emu);
79         fdc = new T3444A(this, emu);    // t3444m
80         mz80fio = new MZ80FIO(this, emu);
81 #endif
82         
83         // set contexts
84         event->set_context_cpu(cpu);
85         event->set_context_sound(pcm);
86         event->set_context_sound(drec);
87         
88 #if defined(_MZ1200) || defined(_MZ80A)
89         and_int->set_context_out(cpu, SIG_CPU_IRQ, 1);
90         and_int->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
91 #endif
92         drec->set_context_ear(pio, SIG_I8255_PORT_C, 0x20);
93         drec->set_context_remote(pio, SIG_I8255_PORT_C, 0x10);
94         ctc->set_context_ch0(counter, SIG_LS393_CLK, 1);
95         ctc->set_context_ch1(ctc, SIG_I8253_CLOCK_2, 1);
96 #if defined(_MZ1200) || defined(_MZ80A)
97         ctc->set_context_ch2(and_int, SIG_AND_BIT_0, 1);
98 #else
99         ctc->set_context_ch2(cpu, SIG_CPU_IRQ, 1);
100 #endif
101         ctc->set_constant_clock(0, 2000000);
102         ctc->set_constant_clock(1, 31250);
103         pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);
104         pio->set_context_port_c(memory, SIG_MEMORY_VGATE, 1, 0);
105         pio->set_context_port_c(drec, SIG_DATAREC_MIC, 2, 0);
106 #if defined(_MZ1200) || defined(_MZ80A)
107         pio->set_context_port_c(and_int, SIG_AND_BIT_1, 4, 0);
108 #endif
109         pio->set_context_port_c(drec, SIG_DATAREC_TRIG, 8, 0);
110         counter->set_context_1qa(pcm, SIG_PCM1BIT_SIGNAL, 1);
111         
112         keyboard->set_context_pio(pio);
113         memory->set_context_ctc(ctc);
114         memory->set_context_pio(pio);
115         
116 #if defined(SUPPORT_MZ80AIF)
117         fdc->set_context_irq(memory, SIG_MEMORY_FDC_IRQ, 1);
118         fdc->set_context_drq(memory, SIG_MEMORY_FDC_DRQ, 1);
119         mz80aif->set_context_fdc(fdc);
120 #elif defined(SUPPORT_MZ80FIO)
121         mz80fio->set_context_fdc(fdc);
122 #endif
123         
124         // cpu bus
125         cpu->set_context_mem(memory);
126 #if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
127         cpu->set_context_io(io);
128 #else
129         cpu->set_context_io(dummy);
130 #endif
131         cpu->set_context_intr(dummy);
132 #ifdef USE_DEBUGGER
133         cpu->set_context_debugger(new DEBUGGER(this, emu));
134 #endif
135         
136         // i/o bus
137 #if defined(SUPPORT_MZ80AIF)
138         io->set_iomap_range_rw(0xd8, 0xdb, fdc);
139         io->set_iomap_range_w(0xdc, 0xdd, mz80aif);
140 #elif defined(SUPPORT_MZ80FIO)
141         io->set_iomap_range_rw(0xf8, 0xfb, mz80fio);
142 #endif
143         
144         // initialize all devices
145         for(DEVICE* device = first_device; device; device = device->next_device) {
146                 device->initialize();
147         }
148 #if defined(SUPPORT_MZ80AIF)
149         for(int i = 0; i < MAX_DRIVE; i++) {
150                 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
151         }
152 #elif defined(SUPPORT_MZ80FIO)
153         for(int i = 0; i < MAX_DRIVE; i++) {
154                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
155 //              fdc->set_drive_mfm(i, false);
156         }
157 #endif
158 }
159
160 VM::~VM()
161 {
162         // delete all devices
163         for(DEVICE* device = first_device; device;) {
164                 DEVICE *next_device = device->next_device;
165                 device->release();
166                 delete device;
167                 device = next_device;
168         }
169 }
170
171 DEVICE* VM::get_device(int id)
172 {
173         for(DEVICE* device = first_device; device; device = device->next_device) {
174                 if(device->this_device_id == id) {
175                         return device;
176                 }
177         }
178         return NULL;
179 }
180
181 // ----------------------------------------------------------------------------
182 // drive virtual machine
183 // ----------------------------------------------------------------------------
184
185 void VM::reset()
186 {
187         // reset all devices
188         for(DEVICE* device = first_device; device; device = device->next_device) {
189                 device->reset();
190         }
191 #if defined(_MZ1200) || defined(_MZ80A)
192         and_int->write_signal(SIG_AND_BIT_0, 0, 1);     // CLOCK = L
193         and_int->write_signal(SIG_AND_BIT_1, 1, 1);     // INTMASK = H
194 #endif
195 }
196
197 void VM::run()
198 {
199         event->drive();
200 }
201
202 // ----------------------------------------------------------------------------
203 // debugger
204 // ----------------------------------------------------------------------------
205
206 #ifdef USE_DEBUGGER
207 DEVICE *VM::get_cpu(int index)
208 {
209         if(index == 0) {
210                 return cpu;
211         }
212         return NULL;
213 }
214 #endif
215
216 // ----------------------------------------------------------------------------
217 // draw screen
218 // ----------------------------------------------------------------------------
219
220 void VM::draw_screen()
221 {
222         memory->draw_screen();
223 }
224
225 #if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
226 int VM::access_lamp()
227 {
228         uint32 status = fdc->read_signal(0);
229         return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
230 }
231 #endif
232
233 // ----------------------------------------------------------------------------
234 // soud manager
235 // ----------------------------------------------------------------------------
236
237 void VM::initialize_sound(int rate, int samples)
238 {
239         // init sound manager
240         event->initialize_sound(rate, samples);
241         
242         // init sound gen
243         pcm->init(rate, 8000);
244 }
245
246 uint16* VM::create_sound(int* extra_frames)
247 {
248         return event->create_sound(extra_frames);
249 }
250
251 int VM::sound_buffer_ptr()
252 {
253         return event->sound_buffer_ptr();
254 }
255
256 #ifdef USE_SOUND_VOLUME
257 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
258 {
259         if(ch == 0) {
260                 pcm->set_volume(0, decibel_l, decibel_r);
261         } else if(ch == 1) {
262                 drec->set_volume(0, decibel_l, decibel_r);
263         }
264 }
265 #endif
266
267 // ----------------------------------------------------------------------------
268 // notify key
269 // ----------------------------------------------------------------------------
270
271 void VM::key_down(int code, bool repeat)
272 {
273         if(!repeat) {
274                 keyboard->key_down(code);
275         }
276 }
277
278 void VM::key_up(int code)
279 {
280 //      keyboard->key_up(code);
281 }
282
283 // ----------------------------------------------------------------------------
284 // user interface
285 // ----------------------------------------------------------------------------
286
287 #if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
288 void VM::open_disk(int drv, const _TCHAR* file_path, int bank)
289 {
290         fdc->open_disk(drv, file_path, bank);
291 }
292
293 void VM::close_disk(int drv)
294 {
295         fdc->close_disk(drv);
296 }
297
298 bool VM::disk_inserted(int drv)
299 {
300         return fdc->disk_inserted(drv);
301 }
302
303 void VM::set_disk_protected(int drv, bool value)
304 {
305         fdc->set_disk_protected(drv, value);
306 }
307
308 bool VM::get_disk_protected(int drv)
309 {
310         return fdc->get_disk_protected(drv);
311 }
312
313 #endif
314
315 void VM::play_tape(const _TCHAR* file_path)
316 {
317         drec->play_tape(file_path);
318         drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
319 }
320
321 void VM::rec_tape(const _TCHAR* file_path)
322 {
323         drec->rec_tape(file_path);
324         drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
325 }
326
327 void VM::close_tape()
328 {
329         drec->close_tape();
330         drec->write_signal(SIG_DATAREC_REMOTE, 0, 0);
331 }
332
333 bool VM::tape_inserted()
334 {
335         return drec->tape_inserted();
336 }
337
338 bool VM::tape_playing()
339 {
340         return drec->tape_playing();
341 }
342
343 bool VM::tape_recording()
344 {
345         return drec->tape_recording();
346 }
347
348 int VM::tape_position()
349 {
350         return drec->tape_position();
351 }
352
353 void VM::push_play()
354 {
355         drec->set_ff_rew(0);
356         drec->set_remote(true);
357 }
358
359 void VM::push_stop()
360 {
361         drec->set_remote(false);
362 }
363
364 void VM::push_fast_forward()
365 {
366         drec->set_ff_rew(1);
367         drec->set_remote(true);
368 }
369
370 void VM::push_fast_rewind()
371 {
372         drec->set_ff_rew(-1);
373         drec->set_remote(true);
374 }
375
376 bool VM::now_skip()
377 {
378         return event->now_skip();
379 }
380
381 void VM::update_config()
382 {
383         for(DEVICE* device = first_device; device; device = device->next_device) {
384                 device->update_config();
385         }
386 }
387
388 #define STATE_VERSION   4
389
390 void VM::save_state(FILEIO* state_fio)
391 {
392         state_fio->FputUint32(STATE_VERSION);
393         
394         for(DEVICE* device = first_device; device; device = device->next_device) {
395                 device->save_state(state_fio);
396         }
397 }
398
399 bool VM::load_state(FILEIO* state_fio)
400 {
401         if(state_fio->FgetUint32() != STATE_VERSION) {
402                 return false;
403         }
404         for(DEVICE* device = first_device; device; device = device->next_device) {
405                 if(!device->load_state(state_fio)) {
406                         return false;
407                 }
408         }
409         return true;
410 }
411