2 SHARP MZ-80K/C Emulator 'EmuZ-80K'
3 SHARP MZ-1200 Emulator 'EmuZ-1200'
5 Author : Takeda.Toshiya
8 SHARP MZ-80A Emulator 'EmuZ-80A'
16 #include "../../emu.h"
17 #include "../device.h"
20 #if defined(_MZ1200) || defined(_MZ80A)
23 #include "../datarec.h"
27 #include "../pcm1bit.h"
31 #include "../debugger.h"
37 #if defined(SUPPORT_MZ80AIF)
40 #include "../mb8877.h"
42 #elif defined(SUPPORT_MZ80FIO)
45 #include "../t3444a.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 VM::VM(EMU* parent_emu) : emu(parent_emu)
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
60 #if defined(_MZ1200) || defined(_MZ80A)
61 and_int = new AND(this, emu);
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);
70 keyboard = new KEYBOARD(this, emu);
71 memory = new MEMORY(this, emu);
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);
84 event->set_context_cpu(cpu);
85 event->set_context_sound(pcm);
86 event->set_context_sound(drec);
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);
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);
99 ctc->set_context_ch2(cpu, SIG_CPU_IRQ, 1);
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);
109 pio->set_context_port_c(drec, SIG_DATAREC_TRIG, 8, 0);
110 counter->set_context_1qa(pcm, SIG_PCM1BIT_SIGNAL, 1);
112 keyboard->set_context_pio(pio);
113 memory->set_context_ctc(ctc);
114 memory->set_context_pio(pio);
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);
125 cpu->set_context_mem(memory);
126 #if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
127 cpu->set_context_io(io);
129 cpu->set_context_io(dummy);
131 cpu->set_context_intr(dummy);
133 cpu->set_context_debugger(new DEBUGGER(this, emu));
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);
144 // initialize all devices
145 for(DEVICE* device = first_device; device; device = device->next_device) {
146 device->initialize();
148 #if defined(SUPPORT_MZ80AIF)
149 for(int i = 0; i < MAX_DRIVE; i++) {
150 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
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);
162 // delete all devices
163 for(DEVICE* device = first_device; device;) {
164 DEVICE *next_device = device->next_device;
167 device = next_device;
171 DEVICE* VM::get_device(int id)
173 for(DEVICE* device = first_device; device; device = device->next_device) {
174 if(device->this_device_id == id) {
181 // ----------------------------------------------------------------------------
182 // drive virtual machine
183 // ----------------------------------------------------------------------------
188 for(DEVICE* device = first_device; device; device = device->next_device) {
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
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
207 DEVICE *VM::get_cpu(int index)
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
220 void VM::draw_screen()
222 memory->draw_screen();
225 #if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
226 int VM::access_lamp()
228 uint32 status = fdc->read_signal(0);
229 return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 void VM::initialize_sound(int rate, int samples)
239 // init sound manager
240 event->initialize_sound(rate, samples);
243 pcm->init(rate, 8000);
246 uint16* VM::create_sound(int* extra_frames)
248 return event->create_sound(extra_frames);
251 int VM::sound_buffer_ptr()
253 return event->sound_buffer_ptr();
256 #ifdef USE_SOUND_VOLUME
257 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
260 pcm->set_volume(0, decibel_l, decibel_r);
262 drec->set_volume(0, decibel_l, decibel_r);
267 // ----------------------------------------------------------------------------
269 // ----------------------------------------------------------------------------
271 void VM::key_down(int code, bool repeat)
274 keyboard->key_down(code);
278 void VM::key_up(int code)
280 // keyboard->key_up(code);
283 // ----------------------------------------------------------------------------
285 // ----------------------------------------------------------------------------
287 #if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
288 void VM::open_disk(int drv, const _TCHAR* file_path, int bank)
290 fdc->open_disk(drv, file_path, bank);
293 void VM::close_disk(int drv)
295 fdc->close_disk(drv);
298 bool VM::disk_inserted(int drv)
300 return fdc->disk_inserted(drv);
303 void VM::set_disk_protected(int drv, bool value)
305 fdc->set_disk_protected(drv, value);
308 bool VM::get_disk_protected(int drv)
310 return fdc->get_disk_protected(drv);
315 void VM::play_tape(const _TCHAR* file_path)
317 drec->play_tape(file_path);
318 drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
321 void VM::rec_tape(const _TCHAR* file_path)
323 drec->rec_tape(file_path);
324 drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
327 void VM::close_tape()
330 drec->write_signal(SIG_DATAREC_REMOTE, 0, 0);
333 bool VM::tape_inserted()
335 return drec->tape_inserted();
338 bool VM::tape_playing()
340 return drec->tape_playing();
343 bool VM::tape_recording()
345 return drec->tape_recording();
348 int VM::tape_position()
350 return drec->tape_position();
356 drec->set_remote(true);
361 drec->set_remote(false);
364 void VM::push_fast_forward()
367 drec->set_remote(true);
370 void VM::push_fast_rewind()
372 drec->set_ff_rew(-1);
373 drec->set_remote(true);
378 return event->now_skip();
381 void VM::update_config()
383 for(DEVICE* device = first_device; device; device = device->next_device) {
384 device->update_config();
388 #define STATE_VERSION 4
390 void VM::save_state(FILEIO* state_fio)
392 state_fio->FputUint32(STATE_VERSION);
394 for(DEVICE* device = first_device; device; device = device->next_device) {
395 device->save_state(state_fio);
399 bool VM::load_state(FILEIO* state_fio)
401 if(state_fio->FgetUint32() != STATE_VERSION) {
404 for(DEVICE* device = first_device; device; device = device->next_device) {
405 if(!device->load_state(state_fio)) {