2 Japan Electronics College MYCOMZ-80A Emulator 'eMYCOMZ-80A'
4 Author : Takeda.Toshiya
10 #include "mycomz80a.h"
11 #include "../../emu.h"
12 #include "../device.h"
15 #include "../datarec.h"
16 #include "../hd46505.h"
19 #include "../msm58321.h"
20 #include "../sn76489an.h"
24 #include "../debugger.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 VM::VM(EMU* parent_emu) : emu(parent_emu)
38 first_device = last_device = NULL;
39 dummy = new DEVICE(this, emu); // must be 1st device
40 event = new EVENT(this, emu); // must be 2nd device
42 drec = new DATAREC(this, emu);
43 crtc = new HD46505(this, emu);
44 pio1 = new I8255(this, emu);
45 pio2 = new I8255(this, emu);
46 pio3 = new I8255(this, emu);
47 io = new IO(this, emu);
48 rtc = new MSM58321(this, emu); // MSM5832
49 psg = new SN76489AN(this, emu);
50 cpu = new Z80(this, emu);
52 display = new DISPLAY(this, emu);
53 keyboard = new KEYBOARD(this, emu);
54 memory = new MEMORY(this, emu);
57 event->set_context_cpu(cpu);
58 event->set_context_sound(psg);
59 event->set_context_sound(drec);
61 // 00 out system control
62 // 01 in/out vram data
64 // 03 in/out crtc data
66 // pa0-7 out vram addr low, psg data
67 // pb0-7 in keyboard data
68 // pc0-2 out vram addr high
69 // pc3 out fdc format write
72 // pc6 in keyboard s4 (motor on/off)
73 // pc7 in keyboard s5 (slow)
75 // pa0 in keyboard strobe (1=pressed)
76 // pa1 in keyboard shift (1=pressed)
78 // pa3-6 in printer ctrl
79 // pa7 in crtc disptmg
80 // pb0-7 out printer data
81 // pc0 out printer strobe
82 // pc1 out printer reset
87 // pc6 out display chr/cg (0=chr,1=cg)
88 // pc7 out display freq (0=80column,1=40column)
90 // pa0-6 in fdc control
91 // pb0-3 in/out rtc data
92 // pc0-3 out rtc address
97 drec->set_context_ear(pio2, SIG_I8255_PORT_A, 4);
98 crtc->set_context_disp(pio2, SIG_I8255_PORT_A, 0x80);
99 pio1->set_context_port_a(display, SIG_DISPLAY_ADDR_L, 0xff, 0);
100 pio1->set_context_port_a(psg, SIG_SN76489AN_DATA, 0xff, 0);
101 pio1->set_context_port_c(display, SIG_DISPLAY_ADDR_H, 7, 0);
102 pio2->set_context_port_c(drec, SIG_DATAREC_MIC, 4, 0);
103 pio2->set_context_port_c(drec, SIG_DATAREC_REMOTE, 8, 0);
104 pio2->set_context_port_c(psg, SIG_SN76489AN_WE, 0x10, 0);
105 pio2->set_context_port_c(psg, SIG_SN76489AN_CS, 0x20, 0);
106 pio2->set_context_port_c(display, SIG_DISPLAY_MODE, 0xc0, 0);
107 pio3->set_context_port_b(rtc, SIG_MSM58321_DATA, 0x0f, 0);
108 pio3->set_context_port_c(rtc, SIG_MSM5832_ADDR, 0x0f, 0);
109 pio3->set_context_port_c(rtc, SIG_MSM5832_HOLD, 0x10, 0);
110 pio3->set_context_port_c(rtc, SIG_MSM58321_READ, 0x20, 0);
111 pio3->set_context_port_c(rtc, SIG_MSM58321_WRITE, 0x40, 0);
112 pio3->set_context_port_c(rtc, SIG_MSM58321_CS, 0x80, 0);
113 rtc->set_context_data(pio3, SIG_I8255_PORT_B, 0x0f, 0);
115 display->set_regs_ptr(crtc->get_regs());
116 keyboard->set_context_cpu(cpu);
117 keyboard->set_context_pio1(pio1);
118 keyboard->set_context_pio2(pio2);
121 cpu->set_context_mem(memory);
122 cpu->set_context_io(io);
123 cpu->set_context_intr(dummy);
125 cpu->set_context_debugger(new DEBUGGER(this, emu));
129 io->set_iomap_single_w(0x00, memory);
130 io->set_iomap_single_rw(0x01, display);
131 io->set_iomap_range_rw(0x02, 0x03, crtc);
132 io->set_iomap_range_rw(0x04, 0x07, pio1);
133 io->set_iomap_range_rw(0x08, 0x0b, pio2);
134 io->set_iomap_range_rw(0x0c, 0x0f, pio3);
136 // initialize all devices
137 for(DEVICE* device = first_device; device; device = device->next_device) {
138 device->initialize();
144 // delete all devices
145 for(DEVICE* device = first_device; device;) {
146 DEVICE *next_device = device->next_device;
149 device = next_device;
153 DEVICE* VM::get_device(int id)
155 for(DEVICE* device = first_device; device; device = device->next_device) {
156 if(device->this_device_id == id) {
163 // ----------------------------------------------------------------------------
164 // drive virtual machine
165 // ----------------------------------------------------------------------------
170 for(DEVICE* device = first_device; device; device = device->next_device) {
180 double VM::frame_rate()
182 return event->frame_rate();
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
190 DEVICE *VM::get_cpu(int index)
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
203 void VM::draw_screen()
205 display->draw_screen();
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
212 void VM::initialize_sound(int rate, int samples)
214 // init sound manager
215 event->initialize_sound(rate, samples);
218 psg->init(rate, 2500800, 10000);
221 uint16* VM::create_sound(int* extra_frames)
223 return event->create_sound(extra_frames);
226 int VM::sound_buffer_ptr()
228 return event->sound_buffer_ptr();
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
235 void VM::key_down(int code, bool repeat)
237 keyboard->key_down(code);
240 void VM::key_up(int code)
242 keyboard->key_up(code);
245 // ----------------------------------------------------------------------------
247 // ----------------------------------------------------------------------------
249 void VM::play_tape(const _TCHAR* file_path)
251 drec->play_tape(file_path);
252 // drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
255 void VM::rec_tape(const _TCHAR* file_path)
257 drec->rec_tape(file_path);
258 // drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
261 void VM::close_tape()
264 // drec->write_signal(SIG_DATAREC_REMOTE, 0, 1);
267 bool VM::tape_inserted()
269 return drec->tape_inserted();
272 bool VM::tape_playing()
274 return drec->tape_playing();
277 bool VM::tape_recording()
279 return drec->tape_recording();
282 int VM::tape_position()
284 return drec->tape_position();
289 return event->now_skip();
292 void VM::update_config()
294 for(DEVICE* device = first_device; device; device = device->next_device) {
295 device->update_config();
299 #define STATE_VERSION 1
301 void VM::save_state(FILEIO* state_fio)
303 state_fio->FputUint32(STATE_VERSION);
305 for(DEVICE* device = first_device; device; device = device->next_device) {
306 device->save_state(state_fio);
310 bool VM::load_state(FILEIO* state_fio)
312 if(state_fio->FgetUint32() != STATE_VERSION) {
315 for(DEVICE* device = first_device; device; device = device->next_device) {
316 if(!device->load_state(state_fio)) {