4 * (C) 2015 K.Ohta <whatisthis.sowhat _at_ gmail.com>
6 * Feb 27, 2015 : Initial
10 #include "../../emu.h"
11 #include "../../config.h"
12 #include "../device.h"
14 #include "../memory.h"
16 #include "../debugger.h"
20 #include "../datarec.h"
23 #include "../mc6809.h"
25 #include "../ym2203.h"
26 #include "../mb8877.h"
27 //#include "../beep.h"
28 #include "../pcm1bit.h"
29 #include "../ym2203.h"
30 #if defined(_FM77AV_VARIANTS)
31 #include "./mb61vh010.h"
34 #include "./fm7_mainio.h"
35 #include "./fm7_mainmem.h"
36 #include "./fm7_display.h"
37 #include "./fm7_keyboard.h"
39 #include "./kanjirom.h"
41 VM::VM(EMU* parent_emu): emu(parent_emu)
44 first_device = last_device = NULL;
48 opn[0] = opn[1] = opn[2] = psg = NULL;
50 dummy = new DEVICE(this, emu); // must be 1st device
51 event = new EVENT(this, emu); // must be 2nd device
53 dummycpu = new DEVICE(this, emu);
55 maincpu = new MC6809(this, emu);
56 subcpu = new MC6809(this, emu);
58 z80cpu = new Z80(this, emu);
60 keyboard = new KEYBOARD(this, emu);
61 #if defined(_FM77AV_VARIANTS)
62 alu = new MB61VH010(this, emu);
65 drec = new DATAREC(this, emu);
66 pcm1bit = new PCM1BIT(this, emu);
67 // beep = new BEEP(this, emu);
68 fdc = new MB8877(this, emu);
70 opn[0] = new YM2203(this, emu); // OPN
71 opn[1] = new YM2203(this, emu); // WHG
72 opn[2] = new YM2203(this, emu); // THG
73 #if !defined(_FM77AV_VARIANTS)
74 psg = new YM2203(this, emu);
76 kanjiclass1 = new KANJIROM(this, emu, false);
77 #ifdef CAPABLE_KANJI_CLASS2
78 kanjiclass2 = new KANJIROM(this, emu, true);
80 mainio = new FM7_MAINIO(this, emu);
81 mainmem = new FM7_MAINMEM(this, emu);
82 display = new DISPLAY(this, emu);
91 for(DEVICE* device = first_device; device;) {
92 DEVICE *next_device = device->next_device;
99 DEVICE* VM::get_device(int id)
101 for(DEVICE* device = first_device; device; device = device->next_device) {
102 if(device->this_device_id == id) {
110 void VM::initialize(void)
112 #if defined(_FM8) || defined(_FM7)
122 void VM::connect_bus(void)
132 * |-> MAINCPU -> MAINMEM -> MAINIO -> MAIN DEVICES
134 * | -> SUBCPU -> SUBMEM -> SUBIO -> SUB DEVICES
138 * MAINMEM can access SUBMEM/IO, when SUBCPU is halted.
139 * MAINMEM and SUBMEM can access DISPLAY and KEYBOARD with exclusive.
140 * MAINCPU can access MAINMEM.
141 * SUBCPU can access SUBMEM.
142 * DISPLAY : R/W from MAINCPU and SUBCPU.
146 event->set_frames_per_sec(60.00);
147 event->set_lines_per_frame(400);
148 #if defined(_FM77AV40) || defined(_FM77AV20)
149 event->set_context_cpu(dummycpu, MAINCLOCK_FAST_MMR * 2);
151 event->set_context_cpu(dummycpu, SUBCLOCK_NORMAL * 2);
155 mainclock = MAINCLOCK_SLOW;
156 subclock = SUBCLOCK_SLOW;
158 if(config.cpu_type == 0) {
160 subclock = SUBCLOCK_NORMAL;
161 mainclock = MAINCLOCK_NORMAL;
164 mainclock = MAINCLOCK_SLOW;
165 subclock = SUBCLOCK_SLOW;
168 #if defined(_FM77AV40) || defined(_FM77AV20)
169 event->set_context_cpu(maincpu, MAINCLOCK_FAST_MMR);
171 event->set_context_cpu(maincpu, MAINCLOCK_NORMAL);
173 event->set_context_cpu(subcpu, SUBCLOCK_NORMAL);
176 event->set_context_cpu(z80cpu, 4000000);
177 z80cpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
180 event->set_context_sound(pcm1bit);
181 #if !defined(_FM77AV_VARIANTS)
182 mainio->set_context_psg(psg);
183 event->set_context_sound(psg);
185 event->set_context_sound(opn[0]);
186 event->set_context_sound(opn[1]);
187 event->set_context_sound(opn[2]);
188 event->set_context_sound(drec);
189 //event->register_vline_event(display);
190 event->register_frame_event(display);
191 //event->register_vline_event(mainio);
193 mainio->set_context_maincpu(maincpu);
194 mainio->set_context_subcpu(subcpu);
196 mainio->set_context_display(display);
197 mainio->set_context_kanjirom_class1(kanjiclass1);
198 mainio->set_context_mainmem(mainmem);
199 mainio->set_context_keyboard(keyboard);
201 #if defined(CAPABLE_KANJI_CLASS2)
202 mainio->set_context_kanjirom_class2(kanjiclass2);
205 keyboard->set_context_break_line(mainio, FM7_MAINIO_PUSH_BREAK, 0xffffffff);
206 keyboard->set_context_mainio(mainio);
207 keyboard->set_context_display(display);
209 keyboard->set_context_rxrdy(keyboard, SIG_FM7KEY_RXRDY, 0x01);
210 keyboard->set_context_rxrdy(display, SIG_FM7KEY_RXRDY, 0x01);
212 keyboard->set_context_key_ack(keyboard, SIG_FM7KEY_ACK, 0x01);
213 keyboard->set_context_key_ack(display, SIG_FM7KEY_ACK, 0x01);
215 drec->set_context_out(mainio, FM7_MAINIO_CMT_RECV, 0xffffffff);
216 //drec->set_context_remote(mainio, FM7_MAINIO_CMT_REMOTE, 0xffffffff);
217 mainio->set_context_datarec(drec);
218 mainmem->set_context_mainio(mainio);
219 mainmem->set_context_display(display);
220 mainmem->set_context_maincpu(maincpu);
222 display->set_context_mainio(mainio);
223 display->set_context_subcpu(subcpu);
224 display->set_context_keyboard(keyboard);
225 subcpu->set_context_bus_halt(display, SIG_FM7_SUB_HALT, 0xffffffff);
227 display->set_context_kanjiclass1(kanjiclass1);
228 #if defined(CAPABLE_KANJI_CLASS2)
229 display->set_context_kanjiclass2(kanjiclass2);
231 #if defined(_FM77AV_VARIANTS)
232 display->set_context_alu(alu);
233 alu->set_context_memory(display);
235 // Palette, VSYNC, HSYNC, Multi-page, display mode.
236 mainio->set_context_display(display);
239 mainio->set_context_fdc(fdc);
240 fdc->set_context_irq(mainio, FM7_MAINIO_FDC_IRQ, 0x1);
241 fdc->set_context_drq(mainio, FM7_MAINIO_FDC_DRQ, 0x1);
243 mainio->set_context_beep(pcm1bit);
244 //mainio->set_context_beep(beep);
246 opn[0]->set_context_irq(mainio, FM7_MAINIO_OPN_IRQ, 0xffffffff);
247 //opn[0]->set_context_port_a(mainio, FM7_MAINIO_OPNPORTA_CHANGED, 0xff, 0);
248 //opn[0]->set_context_port_b(mainio, FM7_MAINIO_OPNPORTB_CHANGED, 0xff, 0);
249 mainio->set_context_opn(opn[0], 0);
250 opn[1]->set_context_irq(mainio, FM7_MAINIO_WHG_IRQ, 0xffffffff);
251 mainio->set_context_opn(opn[1], 1);
252 opn[2]->set_context_irq(mainio, FM7_MAINIO_THG_IRQ, 0xffffffff);
253 mainio->set_context_opn(opn[2], 2);
255 subcpu->set_context_bus_halt(display, SIG_FM7_SUB_HALT, 0xffffffff);
256 subcpu->set_context_bus_clr(display, SIG_FM7_SUB_USE_CLR, 0x0000000f);
258 maincpu->set_context_mem(mainmem);
259 subcpu->set_context_mem(display);
261 maincpu->set_context_debugger(new DEBUGGER(this, emu));
262 subcpu->set_context_debugger(new DEBUGGER(this, emu));
265 for(DEVICE* device = first_device; device; device = device->next_device) {
266 device->initialize();
268 //maincpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
269 //subcpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
271 for(int i = 0; i < 2; i++) {
272 #if defined(_FM77AV20) || defined(_FM77AV40SX) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
273 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
275 fdc->set_drive_type(i, DRIVE_TYPE_2D);
277 fdc->set_drive_rpm(i, 360);
278 fdc->set_drive_mfm(i, true);
280 #if defined(_FM77) || defined(_FM77L4)
281 for(int i = 2; i < 4; i++) {
282 fdc->set_drive_type(i, DRIVE_TYPE_2HD);
283 // fdc->set_drive_rpm(i, 300);
284 // fdc->set_drive_mfm(i, true);
290 void VM::update_config()
293 switch(config.cpu_type){
295 event->set_secondary_cpu_clock(maincpu, MAINCLOCK_NORMAL);
298 event->set_secondary_cpu_clock(maincpu, MAINCLOCK_SLOW);
303 for(DEVICE* device = first_device; device; device = device->next_device) {
304 device->update_config();
306 //update_dipswitch();
312 for(DEVICE* device = first_device; device; device = device->next_device) {
315 opn[0]->SetReg(0x2e, 0); // set prescaler
316 opn[1]->SetReg(0x2e, 0); // set prescaler
317 opn[2]->SetReg(0x2e, 0); // set prescaler
320 // Parameters from XM7.
321 opn[0]->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
322 opn[1]->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
323 opn[2]->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
325 #if !defined(_FM77AV_VARIANTS)
326 psg->SetReg(0x27, 0); // stop timer
327 psg->SetReg(0x2e, 0); // set prescaler
328 psg->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
332 void VM::special_reset()
335 mainio->write_signal(FM7_MAINIO_PUSH_BREAK, 1, 1);
342 mainio->write_signal(FM7_MAINIO_PUSH_BREAK, 1, 1);
343 event->register_event(mainio, EVENT_UP_BREAK, 10000.0 * 1000.0, false, NULL);
351 double VM::frame_rate()
353 return event->frame_rate();
356 // ----------------------------------------------------------------------------
358 // ----------------------------------------------------------------------------
361 DEVICE *VM::get_cpu(int index)
365 } else if(index == 1) {
368 #if defined(_WITH_Z80)
369 else if(index == 2) {
377 // ----------------------------------------------------------------------------
379 // ----------------------------------------------------------------------------
381 void VM::draw_screen()
383 display->draw_screen();
386 int VM::access_lamp()
388 uint32 status = fdc->read_signal(0);
389 return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
392 void VM::initialize_sound(int rate, int samples)
394 // init sound manager
395 event->initialize_sound(rate, samples);
397 opn[0]->init(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
398 opn[1]->init(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
399 opn[2]->init(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
400 #if !defined(_FM77AV_VARIANTS)
401 psg->init(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
403 pcm1bit->init(rate, 2000);
404 //drec->init_pcm(rate, 0);
407 uint16* VM::create_sound(int* extra_frames)
409 uint16* p = event->create_sound(extra_frames);
413 int VM::sound_buffer_ptr()
415 int pos = event->sound_buffer_ptr();
419 // ----------------------------------------------------------------------------
421 // ----------------------------------------------------------------------------
423 void VM::key_down(int code, bool repeat)
426 keyboard->key_down(code);
430 void VM::key_up(int code)
432 keyboard->key_up(code);
435 // ----------------------------------------------------------------------------
437 // ----------------------------------------------------------------------------
439 void VM::open_disk(int drv, _TCHAR* file_path, int bank)
441 fdc->open_disk(drv, file_path, bank);
444 void VM::close_disk(int drv)
446 fdc->close_disk(drv);
449 bool VM::disk_inserted(int drv)
451 return fdc->disk_inserted(drv);
454 #if defined(USE_DISK_WRITE_PROTECT)
455 void VM::write_protect_fd(int drv, bool flag)
457 fdc->write_protect_fd(drv, flag);
460 bool VM::is_write_protect_fd(int drv)
462 return fdc->is_write_protect_fd(drv);
466 void VM::play_tape(_TCHAR* file_path)
468 bool value = drec->play_tape(file_path);
471 void VM::rec_tape(_TCHAR* file_path)
473 bool value = drec->rec_tape(file_path);
476 void VM::close_tape()
481 bool VM::tape_inserted()
483 return drec->tape_inserted();
486 #if defined(USE_TAPE_PTR)
487 int VM::get_tape_ptr(void)
489 return drec->get_tape_ptr();
495 return event->now_skip();
498 void VM::update_dipswitch()
500 // bit0 0=High 1=Standard
501 // bit2 0=5"2D 1=5"2HD
502 // io->set_iovalue_single_r(0x1ff0, (config.monitor_type & 1) | ((config.drive_type & 1) << 2));
505 void VM::set_cpu_clock(DEVICE *cpu, uint32 clocks) {
506 event->set_secondary_cpu_clock(cpu, clocks);
509 #define STATE_VERSION 1