2 Hino Electronics CEFUCOM-21 Emulator 'eCEFUCOM-21'
4 Author : Takeda.Toshiya
10 #include "cefucom21.h"
11 #include "../../emu.h"
12 #include "../device.h"
15 #include "../ay_3_891x.h"
16 #include "../datarec.h"
19 #include "../mc6847.h"
21 #include "../memory.h"
23 #include "../rp5c01.h"
25 #include "../z80ctc.h"
26 #include "../z80pio.h"
29 #include "../debugger.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
44 first_device = last_device = NULL;
45 dummy = new DEVICE(this, emu); // must be 1st device
46 event = new EVENT(this, emu); // must be 2nd device
48 mcu_psg = new AY_3_891X(this, emu);
50 mcu_psg->set_context_debugger(new DEBUGGER(this, emu));
52 mcu_drec = new DATAREC(this, emu);
53 mcu_drec->set_context_noise_play(new NOISE(this, emu));
54 mcu_drec->set_context_noise_stop(new NOISE(this, emu));
55 mcu_drec->set_context_noise_fast(new NOISE(this, emu));
56 mcu_io = new IO(this, emu);
57 mcu_vdp = new MC6847(this, emu);
58 mcu_mem = new MEMORY(this, emu);
60 mcu_not = new NOT(this, emu);
61 mcu_cpu = new Z80(this, emu);
62 mcu_pio = new Z80PIO(this, emu);
63 mcu = new MCU(this, emu);
65 pcu_pio1 = new I8255(this, emu);
66 pcu_pio2 = new I8255(this, emu);
67 pcu_pio3 = new I8255(this, emu);
68 pcu_io = new IO(this, emu);
69 pcu_mem = new MEMORY(this, emu);
71 pcu_rtc = new RP5C01(this, emu);
72 pcu_cpu = new Z80(this, emu);
73 pcu_ctc1 = new Z80CTC(this, emu);
74 pcu_ctc2 = new Z80CTC(this, emu);
75 pcu_pio = new Z80PIO(this, emu);
76 pcu = new PCU(this, emu);
79 event->set_context_cpu(mcu_cpu);
80 event->set_context_cpu(pcu_cpu);
81 event->set_context_sound(mcu_psg);
82 event->set_context_sound(mcu_drec);
83 event->set_context_sound(mcu_drec->get_context_noise_play());
84 event->set_context_sound(mcu_drec->get_context_noise_stop());
85 event->set_context_sound(mcu_drec->get_context_noise_fast());
87 mcu_vdp->load_font_image(create_local_path(_T("FONT.ROM")));
88 mcu_vdp->set_vram_ptr(vram, 0x1800);
90 mcu_vdp->set_context_vsync(mcu_not, SIG_NOT_INPUT, 1);
91 mcu_not->set_context_out(mcu_cpu, SIG_CPU_IRQ, 1);
94 mcu_vdp->set_context_vsync(mcu, SIG_MCU_SYSPORT, 0x10);
95 mcu_drec->set_context_ear(mcu, SIG_MCU_SYSPORT, 0x20);
97 mcu_vdp->set_context_hsync(mcu, SIG_MCU_SYSPORT, 0x80);
99 // mcu_vdp->set_context_vsync(pcu_ctc1, SIG_Z80CTC_TRIG_1, 1);
100 // mcu_vdp->set_context_vsync(pcu_ctc2, SIG_Z80CTC_TRIG_1, 1);
101 mcu_vdp->set_context_vsync(mcu_pio, SIG_Z80PIO_PORT_A, 0x40);
103 mcu_pio->set_context_port_a(pcu_pio, SIG_Z80PIO_PORT_A, 0x38, -3);
104 mcu_pio->set_context_port_b(pcu_pio, SIG_Z80PIO_PORT_B, 0xff, 0);
105 mcu_pio->set_context_ready_a(pcu_pio, SIG_Z80PIO_STROBE_A, 1);
106 mcu_pio->set_context_ready_b(pcu_pio, SIG_Z80PIO_STROBE_B, 1);
107 mcu_pio->set_hand_shake(0, true);
108 mcu_pio->set_hand_shake(1, true);
110 mcu->set_context_drec(mcu_drec);
111 mcu->set_context_psg(mcu_psg);
112 mcu->set_context_vdp(mcu_vdp);
114 pcu_pio->set_context_port_a(mcu_pio, SIG_Z80PIO_PORT_A, 0x38, -3);
115 pcu_pio->set_context_port_b(mcu_pio, SIG_Z80PIO_PORT_B, 0xff, 0);
116 pcu_pio->set_context_ready_a(mcu_pio, SIG_Z80PIO_STROBE_A, 1);
117 pcu_pio->set_context_ready_b(mcu_pio, SIG_Z80PIO_STROBE_B, 1);
118 pcu_pio->set_hand_shake(0, true);
119 pcu_pio->set_hand_shake(1, true);
122 mcu_cpu->set_context_mem(mcu_mem);
123 mcu_cpu->set_context_io(mcu_io);
124 mcu_cpu->set_context_intr(dummy);
126 mcu_cpu->set_context_debugger(new DEBUGGER(this, emu));
129 pcu_cpu->set_context_mem(pcu_mem);
130 pcu_cpu->set_context_io(pcu_io);
131 pcu_cpu->set_context_intr(dummy);
133 pcu_cpu->set_context_debugger(new DEBUGGER(this, emu));
136 // z80 family daisy chain
137 DEVICE* parent_dev = NULL;
140 #define Z80_DAISY_CHAIN(cpu, dev) { \
141 if(parent_dev == NULL) { \
142 cpu->set_context_intr(dev); \
144 parent_dev->set_context_child(dev); \
146 dev->set_context_intr(cpu, level++); \
149 Z80_DAISY_CHAIN(pcu_cpu, pcu_ctc1);
150 Z80_DAISY_CHAIN(pcu_cpu, pcu_ctc2);
151 Z80_DAISY_CHAIN(pcu_cpu, pcu_pio );
154 Z80_DAISY_CHAIN(mcu_cpu, mcu_pio );
158 memset(mcu_rom, 0xff, sizeof(mcu_rom));
159 memset(mcu_ram, 0x00, sizeof(mcu_ram));
160 memset(pcu_rom, 0xff, sizeof(pcu_rom));
161 memset(pcu_ram, 0x00, sizeof(pcu_ram));
163 memset(vram, 0x00, sizeof(vram));
164 memset(cram, 0x00, sizeof(cram));
166 mcu_mem->read_bios(_T("BASIC.ROM"), mcu_rom, sizeof(mcu_rom));
168 memset(mcu_rom + 0x7800, 0, 0x800);
170 // memset(mcu_rom, 0, 0x8000);
172 mcu_mem->set_memory_r (0x0000, 0x6fff, mcu_rom);
173 mcu_mem->set_memory_rw(0x6000, 0x77ff, vram);
174 mcu_mem->set_memory_r (0x7800, 0x7fff, mcu_rom + 0x7800);
175 // mcu_mem->set_memory_rw(0x8000, 0xffff, mcu_ram);
176 mcu_mem->set_memory_rw(0x8000, 0xefff, mcu_ram);
177 mcu_mem->set_memory_rw(0xf000, 0xffff, cram);
179 pcu_mem->read_bios(_T("MENU.ROM"), pcu_rom, sizeof(pcu_rom));
181 memset(pcu_rom, 0, 0x8000);
183 pcu_mem->set_memory_r (0x0000, 0x7fff, pcu_rom);
184 pcu_mem->set_memory_rw(0x8000, 0xffff, pcu_ram);
185 pcu_mem->set_memory_rw(0xc000, 0xcfff, cram);
188 mcu_io->set_iomap_single_rw(0x40, mcu);
189 mcu_io->set_iomap_range_r (0x80, 0x89, mcu);
190 mcu_io->set_iomap_alias_w (0xc0, mcu_psg, 1); // PSG data
191 mcu_io->set_iomap_alias_w (0xc1, mcu_psg, 0); // PSG ch
192 // mcu_io->set_iomap_alias_r (0xc0, mcu_psg, 1);
193 mcu_io->set_iomap_alias_r (0xc1, mcu_psg, 1); // PSG data
194 mcu_io->set_iomap_range_rw (0xe0, 0xe3, mcu_pio);
196 pcu_io->cpu_index = 1;
198 pcu_io->set_iomap_range_rw (0x00, 0x03, pcu_ctc1);
199 pcu_io->set_iomap_range_rw (0x08, 0x0b, pcu_ctc2);
200 pcu_io->set_iomap_alias_rw (0x10, pcu_pio, 0);
201 pcu_io->set_iomap_alias_rw (0x11, pcu_pio, 2);
202 pcu_io->set_iomap_alias_rw (0x12, pcu_pio, 1);
203 pcu_io->set_iomap_alias_rw (0x13, pcu_pio, 3);
204 pcu_io->set_iomap_range_rw (0x20, 0x2f, pcu_rtc);
205 pcu_io->set_iomap_range_rw (0x38, 0x39, pcu);
206 pcu_io->set_iomap_range_rw (0x60, 0x63, pcu_pio1);
207 pcu_io->set_iomap_range_rw (0x64, 0x67, pcu_pio2);
208 pcu_io->set_iomap_range_rw (0x68, 0x6b, pcu_pio3);
210 // initialize all devices
211 for(DEVICE* device = first_device; device; device = device->next_device) {
212 device->initialize();
218 // delete all devices
219 for(DEVICE* device = first_device; device;) {
220 DEVICE *next_device = device->next_device;
223 device = next_device;
227 DEVICE* VM::get_device(int id)
229 for(DEVICE* device = first_device; device; device = device->next_device) {
230 if(device->this_device_id == id) {
237 // ----------------------------------------------------------------------------
238 // drive virtual machine
239 // ----------------------------------------------------------------------------
244 for(DEVICE* device = first_device; device; device = device->next_device) {
247 mcu_vdp->write_signal(SIG_MC6847_GM, 6, 7);
248 mcu_vdp->write_signal(SIG_MC6847_CSS, 0, 0);
249 mcu_vdp->write_signal(SIG_MC6847_AG, 0, 0);
251 // pcu_cpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
264 DEVICE *VM::get_cpu(int index)
268 } else if(index == 1) {
275 // ----------------------------------------------------------------------------
277 // ----------------------------------------------------------------------------
279 void VM::draw_screen()
281 mcu_vdp->draw_screen();
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 void VM::initialize_sound(int rate, int samples)
290 // init sound manager
291 event->initialize_sound(rate, samples);
294 mcu_psg->initialize_sound(rate, 1996750, samples, 0, 0);
297 uint16_t* VM::create_sound(int* extra_frames)
299 return event->create_sound(extra_frames);
302 int VM::get_sound_buffer_ptr()
304 return event->get_sound_buffer_ptr();
307 #ifdef USE_SOUND_VOLUME
308 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
311 mcu_psg->set_volume(1, decibel_l, decibel_r);
313 mcu_drec->set_volume(0, decibel_l, decibel_r);
315 mcu_drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
316 mcu_drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
317 mcu_drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
322 // ----------------------------------------------------------------------------
324 // ----------------------------------------------------------------------------
326 void VM::play_tape(int drv, const _TCHAR* file_path)
328 bool remote = mcu_drec->get_remote();
330 if(mcu_drec->play_tape(file_path) && remote) {
331 // if machine already sets remote on, start playing now
336 void VM::rec_tape(int drv, const _TCHAR* file_path)
338 bool remote = mcu_drec->get_remote();
340 if(mcu_drec->rec_tape(file_path) && remote) {
341 // if machine already sets remote on, start recording now
346 void VM::close_tape(int drv)
349 mcu_drec->close_tape();
351 mcu_drec->set_remote(false);
354 bool VM::is_tape_inserted(int drv)
356 return mcu_drec->is_tape_inserted();
359 bool VM::is_tape_playing(int drv)
361 return mcu_drec->is_tape_playing();
364 bool VM::is_tape_recording(int drv)
366 return mcu_drec->is_tape_recording();
369 int VM::get_tape_position(int drv)
371 return mcu_drec->get_tape_position();
374 const _TCHAR* VM::get_tape_message(int drv)
376 return mcu_drec->get_message();
379 void VM::push_play(int drv)
381 mcu_drec->set_remote(false);
382 mcu_drec->set_ff_rew(0);
383 mcu_drec->set_remote(true);
386 void VM::push_stop(int drv)
388 mcu_drec->set_remote(false);
391 void VM::push_fast_forward(int drv)
393 mcu_drec->set_remote(false);
394 mcu_drec->set_ff_rew(1);
395 mcu_drec->set_remote(true);
398 void VM::push_fast_rewind(int drv)
400 mcu_drec->set_remote(false);
401 mcu_drec->set_ff_rew(-1);
402 mcu_drec->set_remote(true);
405 bool VM::is_frame_skippable()
407 return event->is_frame_skippable();
410 void VM::update_config()
412 for(DEVICE* device = first_device; device; device = device->next_device) {
413 device->update_config();
417 #define STATE_VERSION 5
419 bool VM::process_state(FILEIO* state_fio, bool loading)
421 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
424 for(DEVICE* device = first_device; device; device = device->next_device) {
425 const char *name = typeid(*device).name() + 6; // skip "class "
426 int len = strlen(name);
428 if(!state_fio->StateCheckInt32(len)) {
431 if(!state_fio->StateCheckBuffer(name, len, 1)) {
434 if(!device->process_state(state_fio, loading)) {