OSDN Git Service

[VM][FM7] Add UARTs.RS-232C, MODEM and MIDI.Temporally implements.
[csp-qt/common_source_project-fm7.git] / source / src / vm / fm7 / fm7.cpp
1 /*
2  * FM7 -> VM
3  * (C) 2015 K.Ohta <whatisthis.sowhat _at_ gmail.com>
4  * History:
5  *   Feb 27, 2015 : Initial
6  */
7
8 #include "fm7.h"
9 #include "../../emu.h"
10 #include "../../config.h"
11 #include "../device.h"
12 #include "../event.h"
13 #include "../memory.h"
14 #include "../prnfile.h"
15
16 #ifdef USE_DEBUGGER
17 #include "../debugger.h"
18 #endif
19
20 #include "../datarec.h"
21 #include "../disk.h"
22
23 #include "../mc6809.h"
24 #include "../z80.h"
25 #include "../mb8877.h"
26 #include "../noise.h"
27
28 #include "../pcm1bit.h"
29 #include "../ym2203.h"
30 #include "../ay_3_891x.h"
31 #include "../and.h"
32 #include "../or.h"
33 #include "../i8251.h"
34
35 #if defined(_FM77AV_VARIANTS)
36 #include "mb61vh010.h"
37 #include "../beep.h"
38 #endif
39 #if defined(HAS_DMA)
40 #include "hd6844.h"
41 #endif
42 #if defined(_FM8)
43 #include "./bubblecasette.h"
44 #endif
45
46 #include "./fm7_mainio.h"
47 #include "./fm7_mainmem.h"
48 #include "./fm7_display.h"
49 #include "./fm7_keyboard.h"
50 #include "./joystick.h"
51
52 #include "./kanjirom.h"
53 #if defined(CAPABLE_JCOMMCARD)
54 #include "./jcommcard.h"
55 #endif
56
57 VM::VM(EMU* parent_emu): emu(parent_emu)
58 {
59         
60         first_device = last_device = NULL;
61 #if defined(_FM8)
62         psg = NULL;
63 #else   
64 # if defined(_FM77AV_VARIANTS)
65         opn[0] = opn[1] = opn[2] = NULL;
66 # else   
67         opn[0] = opn[1] = opn[2] = NULL;
68         psg = NULL; 
69 # endif
70 #endif
71         for(int i = 0; i < 3; i++) uart[i] = NULL;
72         
73         dummy = new DEVICE(this, emu);  // must be 1st device
74         event = new EVENT(this, emu);   // must be 2nd device
75         
76         dummycpu = new DEVICE(this, emu);
77         maincpu = new MC6809(this, emu);
78         subcpu = new MC6809(this, emu);
79         g_substat_display = new AND(this, emu);
80         g_substat_mainhalt = new AND(this, emu);
81
82         
83 #ifdef WITH_Z80
84         if((config.dipswitch & FM7_DIPSW_Z80CARD_ON) != 0) {
85                 z80cpu = new Z80(this, emu);
86         } else {
87                 z80cpu = NULL;
88         }
89         g_mainstat = new AND(this, emu);
90         g_intr = new OR(this, emu);
91
92         g_intr_irq = new AND(this, emu);
93         g_intr_firq = new AND(this, emu);
94         g_nmi = new AND(this, emu);
95 #endif
96 #if defined(CAPABLE_JCOMMCARD)
97         if((config.dipswitch & FM7_DIPSW_JSUBCARD_ON) != 0) {
98                 jsubcpu = new MC6809(this, parent_emu);
99                 jcommcard = new FM7_JCOMMCARD(this, parent_emu);
100         } else {
101                 jsubcpu = NULL;
102                 jcommcard = NULL;
103         }
104 #endif
105 # if defined(_FM77AV40) || defined(_FM77AV40SX) || defined(_FM77AV40EX) || defined(_FM77AV20) || defined(_FM77AV20EX)
106         uart[0] = new I8251(this, emu);
107 # else
108 #  if defined(CAPABLE_JCOMMCARD)
109         if((config.dipswitch & FM7_DIPSW_JSUBCARD_ON) != 0) uart[0] = new I8251(this, emu);
110 #  endif
111         if(((config.dipswitch & FM7_DIPSW_RS232C_ON) != 0) && (uart[0] == NULL)) uart[0] = new I8251(this, emu);
112 # endif
113         if((config.dipswitch & FM7_DIPSW_MODEM_ON) != 0) uart[1] = new I8251(this, emu);
114         if((config.dipswitch & FM7_DIPSW_MIDI_ON) != 0) uart[2] = new I8251(this, emu);
115
116                 
117         // basic devices
118         // I/Os
119 #if defined(HAS_DMA)
120         dmac = new HD6844(this, emu);
121 #endif   
122 #if defined(_FM8)
123 #  if defined(USE_AY_3_8910_AS_PSG)
124         psg = new AY_3_891X(this, emu);
125 #  else
126         psg = new YM2203(this, emu);
127 #  endif
128 #else   
129         opn[0] = new YM2203(this, emu); // OPN
130         opn[1] = new YM2203(this, emu); // WHG
131         opn[2] = new YM2203(this, emu); // THG
132 # if !defined(_FM77AV_VARIANTS)
133 #  if defined(USE_AY_3_8910_AS_PSG)
134         psg = new AY_3_891X(this, emu);
135 #  else
136         psg = new YM2203(this, emu);
137 #  endif
138 # endif 
139 #endif
140 #if defined(_FM8)
141         for(int i = 0; i < 2; i++) bubble_casette[i] = new BUBBLECASETTE(this, emu);
142 #endif
143         drec = NULL;
144         drec = new DATAREC(this, emu);
145         drec->set_context_noise_play(new NOISE(this, emu));
146         drec->set_context_noise_stop(new NOISE(this, emu));
147         drec->set_context_noise_fast(new NOISE(this, emu));
148         pcm1bit = new PCM1BIT(this, emu);
149
150         connect_320kfdc = connect_1Mfdc = false;
151         fdc = NULL;
152 #if defined(_FM8) || defined(_FM7) || defined(_FMNEW7)
153         if(((config.dipswitch & FM7_DIPSW_CONNECT_320KFDC) != 0) ||
154            ((config.dipswitch & FM7_DIPSW_CONNECT_1MFDC) != 0)) {
155 #endif          
156                 fdc = new MB8877(this, emu);
157                 fdc->set_context_noise_seek(new NOISE(this, emu));
158                 fdc->set_context_noise_head_down(new NOISE(this, emu));
159                 fdc->set_context_noise_head_up(new NOISE(this, emu));
160 #if defined(_FM8) || defined(_FM7) || defined(_FMNEW7)
161                 if((config.dipswitch & FM7_DIPSW_CONNECT_320KFDC) != 0) {
162                         connect_320kfdc = true;
163                 }
164                 if((config.dipswitch & FM7_DIPSW_CONNECT_1MFDC) != 0) {
165                         connect_1Mfdc = true;
166                 }
167 #elif defined(_FM77_VARIANTS)
168                 connect_320kfdc = true;
169                 if((config.dipswitch & FM7_DIPSW_CONNECT_1MFDC) != 0) {
170                         connect_1Mfdc = true;
171                 }
172 #else   // AV or later.
173                 connect_320kfdc = true;
174                 // 1MFDD??
175 #endif          
176 #if defined(_FM8) || defined(_FM7) || defined(_FMNEW7)
177         }
178 #endif  
179         joystick  = new JOYSTICK(this, emu);
180         printer = new PRNFILE(this, emu);
181 #if defined(_FM77AV_VARIANTS)
182         alu = new MB61VH010(this, emu);
183         keyboard_beep = new BEEP(this, emu);
184 #endif  
185         keyboard = new KEYBOARD(this, emu);
186         display = new DISPLAY(this, emu);       
187
188         mainio  = new FM7_MAINIO(this, emu);
189         mainmem = new FM7_MAINMEM(this, emu);
190
191 #if defined(_FM8) || defined(_FM7) || defined(_FMNEW7)
192         if((config.dipswitch & FM7_DIPSW_CONNECT_KANJIROM) != 0) {
193                 kanjiclass1 = new KANJIROM(this, emu, false);
194         } else {
195                 kanjiclass1 = NULL;
196         }
197 #else
198         kanjiclass1 = new KANJIROM(this, emu, false);
199 #endif  
200 #ifdef CAPABLE_KANJI_CLASS2
201         kanjiclass2 = new KANJIROM(this, emu, true);
202 #endif
203
204 # if defined(_FM77AV20) || defined(_FM77AV40) || defined(_FM77AV20EX) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
205         g_rs232c_dtr = new AND(this, emu);
206         g_rs232c_dtr->set_mask(SIG_AND_BIT_0);
207         g_rs232c_dtr->set_mask(SIG_AND_BIT_1);
208
209         // DCD
210 #endif
211 #ifdef WITH_Z80
212         g_mainstat->set_mask(SIG_AND_BIT_0);
213         g_mainstat->set_mask(SIG_AND_BIT_1);
214         maincpu->set_context_bus_ba(g_mainstat, SIG_AND_BIT_0, 0xffffffff);
215         maincpu->set_context_bus_bs(g_mainstat, SIG_AND_BIT_1, 0xffffffff);
216         g_mainstat->set_context_out(mainio, FM7_MAINIO_RUN_Z80, 0xffffffff);
217
218         if(z80cpu != NULL) {
219                 z80cpu->set_context_busack(mainio, FM7_MAINIO_RUN_6809, 0xffffffff);
220                 mainio->set_context_z80cpu(z80cpu);
221         }
222 #endif
223 #if defined(_USE_QT)
224         event->set_device_name(_T("EVENT"));
225         dummy->set_device_name(_T("1st Dummy"));
226         
227         maincpu->set_device_name(_T("MAINCPU(MC6809B)"));
228         subcpu->set_device_name(_T("SUBCPU(MC6809B)"));
229         dummycpu->set_device_name(_T("DUMMY CPU"));
230 #if defined(CAPABLE_JCOMMCARD)
231         if(jsubcpu != NULL) {
232                 jsubcpu->set_device_name(_T("J.COMM BOARD CPU(MC6809)"));
233         }
234         if(jcommcard != NULL) {
235                 jcommcard->set_device_name(_T("Japanese COMM BOARD"));
236         }
237 # ifdef WITH_Z80
238         if(z80cpu != NULL) z80cpu->set_device_name(_T("Z80 CPU BOARD"));
239 # endif
240         if(fdc != NULL) fdc->set_device_name(_T("MB8877 FDC(320KB)"));
241         
242         if(uart[0] != NULL) {
243                 uart[0]->set_device_name(_T("RS-232C BOARD(I8251 SIO)"));
244         }
245 # if defined(CAPABLE_JCOMMCARD)
246         if((config.dipswitch & FM7_DIPSW_JSUBCARD_ON) != 0) {
247                 if(uart[0] != NULL) uart[0]->set_device_name(_T("J.COMM BOARD RS-232C(I8251 SIO)"));
248         }
249 # elif defined(_FM77AV20) || defined(_FM77AV40) || defined(_FM77AV20EX) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
250         if(uart[0] != NULL) uart[0]->set_device_name(_T("RS-232C(I8251 SIO)"));
251 # endif
252                 
253         if(uart[1] != NULL) {
254                 uart[1]->set_device_name(_T("MODEM BOARD(I8251 SIO)"));
255         }
256         if(uart[2] != NULL) {
257                 uart[2]->set_device_name(_T("MIDI BOARD(I8251 SIO)"));
258         }
259                                                 
260         // basic devices
261         // I/Os
262 # if defined(_FM8)
263         psg->set_device_name(_T("AY-3-8910 PSG"));
264 # else  
265         opn[0]->set_device_name(_T("YM2203 OPN"));
266         opn[1]->set_device_name(_T("YM2203 WHG"));
267         opn[2]->set_device_name(_T("YM2203 THG"));
268 #  if !defined(_FM77AV_VARIANTS)
269         psg->set_device_name(_T("AY-3-8910 PSG"));
270 #  endif
271 # endif
272         pcm1bit->set_device_name(_T("BEEP"));
273         printer->set_device_name(_T("PRINTER I/F"));
274 # if defined(_FM77AV_VARIANTS)
275         keyboard_beep->set_device_name(_T("BEEP(KEYBOARD)"));
276 # endif 
277         if(kanjiclass1 != NULL) kanjiclass1->set_device_name(_T("KANJI ROM CLASS1"));
278 # ifdef CAPABLE_KANJI_CLASS2
279         if(kanjiclass2 != NULL) kanjiclass2->set_device_name(_T("KANJI ROM CLASS2"));
280 # endif
281 # if defined(_FM8)
282         bubble_casette[0]->set_device_name(_T("BUBBLE CASETTE #0"));
283         bubble_casette[1]->set_device_name(_T("BUBBLE CASETTE #1"));
284 # endif 
285 #endif
286 # if defined(_FM77AV20) || defined(_FM77AV40) || defined(_FM77AV20EX) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
287         g_rs232c_dtr->set_device_name(_T("RS232C DTR(AND)"));
288 #endif
289 #ifdef WITH_Z80
290         g_intr->set_device_name(_T("Z80 INTR(OR)"));
291         g_mainstat->set_device_name(_T("Z80 HALT/RUN(AND)"));
292         g_intr_irq->set_device_name(_T("Z80 IRQ(AND)"));
293         g_intr_firq->set_device_name(_T("Z80 FIRQ(AND)"));
294         g_nmi->set_device_name(_T("Z80 NMI(AND)"));
295 #endif
296         g_substat_display->set_device_name(_T("DISPLAY STATUS(AND)"));
297         g_substat_mainhalt->set_device_name(_T("SUBSYSTEM HALT STATUS(AND)"));
298 #endif  
299         this->connect_bus();
300         
301 }
302
303 VM::~VM()
304 {
305         // delete all devices
306         for(DEVICE* device = first_device; device;) {
307                 DEVICE *next_device = device->next_device;
308                 device->release();
309                 delete device;
310                 device = next_device;
311         }
312 }
313
314 DEVICE* VM::get_device(int id)
315 {
316         for(DEVICE* device = first_device; device; device = device->next_device) {
317                 if(device->this_device_id == id) {
318                         return device;
319                 }
320         }
321         return NULL;
322 }
323
324 void VM::connect_bus(void)
325 {
326         uint32_t mainclock;
327         uint32_t subclock;
328
329         /*
330          * CLASS CONSTRUCTION
331          *
332          * VM 
333          *  |-> MAINCPU -> MAINMEM -> MAINIO -> MAIN DEVICES
334          *  |             |        |      
335          *  | -> SUBCPU  -> SUBMEM  -> SUBIO -> SUB DEVICES
336          *  | -> DISPLAY
337          *  | -> KEYBOARD
338          *
339          *  MAINMEM can access SUBMEM/IO, when SUBCPU is halted.
340          *  MAINMEM and SUBMEM can access DISPLAY and KEYBOARD with exclusive.
341          *  MAINCPU can access MAINMEM.
342          *  SUBCPU  can access SUBMEM.
343          *  DISPLAY : R/W from MAINCPU and SUBCPU.
344          *  KEYBOARD : R/W
345          *
346          */
347         event->set_frames_per_sec(FRAMES_PER_SEC);
348         event->set_lines_per_frame(LINES_PER_FRAME);
349         //event->set_context_cpu(dummycpu, (CPU_CLOCKS * 3) / 8); // MAYBE FIX With eFM77AV40/20.
350         // With slow clock (for dummycpu), some softwares happen troubles,
351         // Use faster clock for dummycpu. 20160319 K.Ohta
352         event->set_context_cpu(dummycpu, SUBCLOCK_NORMAL);
353
354 #if defined(_FM8)
355         mainclock = MAINCLOCK_SLOW;
356         subclock = SUBCLOCK_SLOW;
357 #else
358         if(config.cpu_type == 0) {
359                 // 2MHz
360                 subclock = SUBCLOCK_NORMAL;
361                 mainclock = MAINCLOCK_NORMAL;
362         } else {
363                 // 1.2MHz
364                 mainclock = MAINCLOCK_SLOW;
365                 subclock = SUBCLOCK_SLOW;
366         }
367         //if((config.dipswitch & FM7_DIPSW_CYCLESTEAL) != 0) subclock = subclock / 3;
368 #endif
369         event->set_context_cpu(maincpu, mainclock);
370         event->set_context_cpu(subcpu,  subclock);
371    
372 #ifdef WITH_Z80
373         if(z80cpu != NULL) {
374                 event->set_context_cpu(z80cpu,  4000000);
375                 z80cpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
376         }
377         maincpu->write_signal(SIG_CPU_HALTREQ, 0, 1);
378
379         g_intr_irq->set_mask(SIG_AND_BIT_0);
380         g_intr_irq->set_mask(SIG_AND_BIT_1);
381         
382         g_intr_firq->set_mask(SIG_AND_BIT_0);
383         g_intr_firq->set_mask(SIG_AND_BIT_1);
384         
385         g_nmi->set_mask(SIG_AND_BIT_0);
386         g_nmi->set_mask(SIG_AND_BIT_1);
387
388         mainio->set_context_irq(g_intr_irq, SIG_AND_BIT_1, 0xffffffff);
389         g_intr_irq->set_context_out(g_intr, SIG_OR_BIT_0, 0xffffffff);
390         
391         mainio->set_context_firq(g_intr_firq, SIG_AND_BIT_1, 0xffffffff);
392         g_intr_firq->set_context_out(g_intr, SIG_OR_BIT_0, 0xffffffff);
393         
394         if(z80cpu != NULL) g_intr->set_context_out(z80cpu, SIG_CPU_IRQ, 0xffffffff);
395
396         mainio->set_context_nmi(g_nmi, SIG_AND_BIT_1, 0xffffffff);
397         if(z80cpu != NULL) g_nmi->set_context_out(z80cpu, SIG_CPU_NMI, 0xffffffff);
398 #endif
399 #if defined(CAPABLE_JCOMMCARD)
400         if((jsubcpu != NULL) && (jcommcard != NULL)) {
401                 event->set_context_cpu(jsubcpu,  JCOMMCARD_CLOCK);
402                 jcommcard->set_context_cpu(jsubcpu);
403                 jsubcpu->set_context_bus_ba(jcommcard, FM7_JCOMMCARD_BUS_BA, 0x00000001);
404                 jsubcpu->set_context_bus_bs(jcommcard, FM7_JCOMMCARD_BUS_BS, 0x00000001);
405                 mainio->set_context_jcommcard(jcommcard);
406         }
407 #endif
408         event->set_context_sound(pcm1bit);
409 #if defined(_FM8)
410         event->set_context_sound(psg);
411         if(drec != NULL) event->set_context_sound(drec);
412 #else
413         event->set_context_sound(opn[0]);
414         event->set_context_sound(opn[1]);
415         event->set_context_sound(opn[2]);
416 # if !defined(_FM77AV_VARIANTS)
417         event->set_context_sound(psg);
418 # endif
419         event->set_context_sound(drec);
420         if(fdc != NULL) {
421                 event->set_context_sound(fdc->get_context_noise_seek());
422                 event->set_context_sound(fdc->get_context_noise_head_down());
423                 event->set_context_sound(fdc->get_context_noise_head_up());
424         }
425         if(drec != NULL) {
426                 event->set_context_sound(drec->get_context_noise_play());
427                 event->set_context_sound(drec->get_context_noise_stop());
428                 event->set_context_sound(drec->get_context_noise_fast());
429         }
430 # if defined(_FM77AV_VARIANTS)
431         event->set_context_sound(keyboard_beep);
432 # endif
433 #endif   
434 #if !defined(_FM77AV_VARIANTS) && !defined(_FM77L4)
435         event->register_vline_event(display);
436         event->register_frame_event(display);
437 #endif  
438         mainio->set_context_maincpu(maincpu);
439         mainio->set_context_subcpu(subcpu);
440         
441         mainio->set_context_display(display);
442         mainio->set_context_irq(maincpu, SIG_CPU_IRQ, 0xffffffff);
443         mainio->set_context_firq(maincpu, SIG_CPU_FIRQ, 0xffffffff);
444         mainio->set_context_nmi(maincpu, SIG_CPU_NMI, 0xffffffff);
445
446         mainio->set_context_uart(0, uart[0]); /* $FD06- : RS232C */
447         mainio->set_context_uart(1, uart[1]); /* $FD40- : MODEM */
448         mainio->set_context_uart(2, uart[2]); /* $FDEA- : MIDI */
449
450 #if defined(_FM77AV20) || defined(_FM77AV40) || defined(_FM77AV20EX) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
451         mainio->set_context_rs232c_dtr(g_rs232c_dtr);
452         if(uart[0] != NULL) uart[0]->set_context_dtr(g_rs232c_dtr, SIG_AND_BIT_1, 0xffffffff);
453 #endif
454         if(uart[0] != NULL) {
455                 uart[0]->set_context_rxrdy(mainio, FM7_MAINIO_UART0_RXRDY, 0xffffffff);
456                 uart[0]->set_context_txrdy(mainio, FM7_MAINIO_UART0_TXRDY, 0xffffffff);
457                 uart[0]->set_context_syndet(mainio, FM7_MAINIO_UART0_SYNDET, 0xffffffff);
458         }
459         if(uart[1] != NULL) {
460                 uart[1]->set_context_rxrdy(mainio, FM7_MAINIO_MODEM_RXRDY, 0xffffffff);
461                 uart[1]->set_context_txrdy(mainio, FM7_MAINIO_MODEM_TXRDY, 0xffffffff);
462                 uart[1]->set_context_syndet(mainio, FM7_MAINIO_MODEM_SYNDET, 0xffffffff);
463         }
464         if(uart[2] != NULL) {
465                 uart[2]->set_context_rxrdy(mainio, FM7_MAINIO_MIDI_RXRDY, 0xffffffff);
466                 uart[2]->set_context_txrdy(mainio, FM7_MAINIO_MIDI_TXRDY, 0xffffffff);
467                 uart[2]->set_context_syndet(mainio, FM7_MAINIO_MIDI_SYNDET, 0xffffffff);
468         }
469         
470 #if defined(_FM8) || defined(_FM7) || defined(_FMNEW7)
471         if((config.dipswitch & FM7_DIPSW_CONNECT_KANJIROM) != 0) {
472                 mainio->set_context_kanjirom_class1(kanjiclass1);
473         }
474 #else
475         mainio->set_context_kanjirom_class1(kanjiclass1);
476 #endif  
477         mainio->set_context_mainmem(mainmem);
478         mainio->set_context_keyboard(keyboard);
479         mainio->set_context_printer(printer);
480         mainio->set_context_printer_reset(printer, SIG_PRINTER_RESET, 0xffffffff);
481         mainio->set_context_printer_strobe(printer, SIG_PRINTER_STROBE, 0xffffffff);
482         mainio->set_context_printer_select(printer, SIG_PRINTER_SELECT, 0xffffffff);
483 #if defined(CAPABLE_KANJI_CLASS2)
484         mainio->set_context_kanjirom_class2(kanjiclass2);
485 #endif
486 #if defined(_FM8)
487         for(int i = 0; i < 2; i++) mainio->set_context_bubble(bubble_casette[i], i);
488 #endif  
489         keyboard->set_context_break_line(mainio, FM7_MAINIO_PUSH_BREAK, 0xffffffff);
490         keyboard->set_context_int_line(mainio, FM7_MAINIO_KEYBOARDIRQ, 0xffffffff);
491         keyboard->set_context_int_line(display, SIG_FM7_SUB_KEY_FIRQ, 0xffffffff);
492 #if defined(_FM77AV_VARIANTS)
493         keyboard->set_context_beep(keyboard_beep);
494 #endif  
495         keyboard->set_context_rxrdy(display, SIG_FM7KEY_RXRDY, 0x01);
496         keyboard->set_context_key_ack(display, SIG_FM7KEY_ACK, 0x01);
497    
498         if(drec != NULL) {
499                 drec->set_context_ear(mainio, FM7_MAINIO_CMT_RECV, 0xffffffff);
500                 //drec->set_context_remote(mainio, FM7_MAINIO_CMT_REMOTE, 0xffffffff);
501                 mainio->set_context_datarec(drec);
502         }
503         mainmem->set_context_mainio(mainio);
504         mainmem->set_context_display(display);
505         mainmem->set_context_maincpu(maincpu);
506 #if defined(CAPABLE_DICTROM)
507         mainmem->set_context_kanjirom_class1(kanjiclass1);
508 #endif  
509         display->set_context_mainio(mainio);
510         display->set_context_subcpu(subcpu);
511         display->set_context_keyboard(keyboard);
512
513         mainio->set_context_clock_status(mainmem, FM7_MAINIO_CLOCKMODE, 0xffffffff);
514         mainio->set_context_clock_status(display, SIG_DISPLAY_CLOCK, 0xffffffff);
515
516         g_substat_display->set_mask(SIG_AND_BIT_0);
517         g_substat_display->set_mask(SIG_AND_BIT_1);
518         subcpu->set_context_bus_ba(g_substat_display, SIG_AND_BIT_0, 0xffffffff);
519         subcpu->set_context_bus_bs(g_substat_display, SIG_AND_BIT_1, 0xffffffff);
520         g_substat_display->set_context_out(display, SIG_FM7_SUB_HALT, 0xffffffff);
521
522         g_substat_mainhalt->set_mask(SIG_AND_BIT_0);
523         g_substat_mainhalt->set_mask(SIG_AND_BIT_1);
524         subcpu->set_context_bus_ba(g_substat_mainhalt, SIG_AND_BIT_0, 0xffffffff);
525         subcpu->set_context_bus_bs(g_substat_mainhalt, SIG_AND_BIT_1, 0xffffffff);
526         g_substat_mainhalt->set_context_out(mainmem, SIG_FM7_SUB_HALT, 0xffffffff);
527
528 #if defined(_FM77_VARIANTS) || defined(_FM77AV_VARIANTS)
529         display->set_context_kanjiclass1(kanjiclass1);
530 #endif  
531 #if defined(CAPABLE_KANJI_CLASS2)
532         display->set_context_kanjiclass2(kanjiclass2);
533 #endif   
534 #if defined(_FM77AV_VARIANTS)
535         display->set_context_alu(alu);
536         alu->set_context_memory(display);
537         alu->set_direct_access_offset(DISPLAY_VRAM_DIRECT_ACCESS);
538 #endif  
539         // Palette, VSYNC, HSYNC, Multi-page, display mode. 
540         mainio->set_context_display(display);
541 #if defined(_FM8) || (_FM7) || (_FMNEW7)
542         if(connect_320kfdc || connect_1Mfdc) {
543 #endif          
544                 //FDC
545                 fdc->set_context_irq(mainio, FM7_MAINIO_FDC_IRQ, 0x1);
546                 fdc->set_context_drq(mainio, FM7_MAINIO_FDC_DRQ, 0x1);
547                 mainio->set_context_fdc(fdc);
548 #if defined(_FM8) || (_FM7) || (_FMNEW7)
549         }
550 #endif  
551         // SOUND
552         mainio->set_context_beep(pcm1bit);
553 #if defined(_FM8)       
554         mainio->set_context_psg(psg);
555 #else
556 # if !defined(_FM77AV_VARIANTS)
557         mainio->set_context_psg(psg);
558 # endif
559         opn[0]->set_context_irq(mainio, FM7_MAINIO_OPN_IRQ, 0xffffffff);
560         mainio->set_context_opn(opn[0], 0);
561         joystick->set_context_opn(opn[0]);
562         mainio->set_context_joystick(joystick);
563         opn[0]->set_context_port_b(joystick, FM7_JOYSTICK_MOUSE_STROBE, 0xff, 0);
564         
565         opn[1]->set_context_irq(mainio, FM7_MAINIO_WHG_IRQ, 0xffffffff);
566         mainio->set_context_opn(opn[1], 1);
567         opn[2]->set_context_irq(mainio, FM7_MAINIO_THG_IRQ, 0xffffffff);
568         mainio->set_context_opn(opn[2], 2);
569 #endif   
570         subcpu->set_context_bus_clr(display, SIG_FM7_SUB_USE_CLR, 0x0000000f);
571    
572         event->register_frame_event(joystick);
573 #if defined(HAS_DMA)
574         dmac->set_context_src(fdc, 0);
575         dmac->set_context_dst(mainmem, 0);
576         dmac->set_context_int_line(mainio, 0, FM7_MAINIO_DMA_INT, 0xffffffff);
577         dmac->set_context_drq_line(maincpu, 1, SIG_CPU_BUSREQ, 0xffffffff);
578         mainio->set_context_dmac(dmac);
579 #endif
580         
581         // MEMORIES must set before initialize().
582         maincpu->set_context_mem(mainmem);
583         subcpu->set_context_mem(display);
584 #ifdef WITH_Z80
585         if(z80cpu != NULL) z80cpu->set_context_mem(mainmem);
586 #endif
587 #if defined(CAPABLE_JCOMMCARD)
588         if((jsubcpu != NULL) && (jcommcard != NULL)) {
589                 jsubcpu->set_context_mem(jcommcard);
590         }
591 #endif
592 #ifdef USE_DEBUGGER
593         maincpu->set_context_debugger(new DEBUGGER(this, emu));
594         subcpu->set_context_debugger(new DEBUGGER(this, emu));
595 # ifdef WITH_Z80
596         if(z80cpu != NULL) z80cpu->set_context_debugger(new DEBUGGER(this, emu));
597 # endif
598 # if defined(CAPABLE_JCOMMCARD)
599         if(jsubcpu != NULL) {
600                 jsubcpu->set_context_debugger(new DEBUGGER(this, emu));
601         }
602 # endif
603 #endif
604         for(DEVICE* device = first_device; device; device = device->next_device) {
605                 device->initialize();
606         }
607
608 #if defined(WITH_Z80)
609         g_intr_irq->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_IRQ_ON) != 0) ? 1 : 0, 1);
610         g_intr_firq->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_FIRQ_ON) != 0) ? 1 : 0, 1);
611         g_nmi->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_NMI_ON) != 0) ? 1 : 0, 1);
612 #endif
613         // Disks
614 #if defined(_FM8) || (_FM7) || (_FMNEW7)
615         if(connect_320kfdc) {
616 #endif          
617                 for(int i = 0; i < 4; i++) {
618 #if defined(_FM77AV20) || defined(_FM77AV20EX) || \
619         defined(_FM77AV40SX) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
620                         fdc->set_drive_type(i, DRIVE_TYPE_2DD);
621 #else
622                         fdc->set_drive_type(i, DRIVE_TYPE_2D);
623 #endif
624                         fdc->set_drive_rpm(i, 360);
625                         fdc->set_drive_mfm(i, true);
626                 }
627 #if defined(_FM8) || (_FM7) || (_FMNEW7)
628         }
629 #endif  
630         
631 #if defined(_FM8) || (_FM7) || (_FMNEW7)
632         if(connect_1Mfdc) {
633 #endif
634 // ToDo: Implement another FDC for 1MB (2HD or 8''), this is used by FM-8 to FM-77? Not FM77AV or later? I still know this.
635 //#if defined(_FM77) || defined(_FM77L4)
636 //              for(int i = 0; i < 4; i++) {
637 //                      fdc->set_drive_type(i, DRIVE_TYPE_2HD);
638 //                      fdc->set_drive_rpm(i, 360);
639 //                      fdc->set_drive_mfm(i, true);
640 //              }
641 //#endif
642 #if defined(_FM8) || (_FM7) || (_FMNEW7)
643         }
644 #endif  
645 }  
646
647 void VM::update_config()
648 {
649         for(DEVICE* device = first_device; device; device = device->next_device) {
650                 device->update_config();
651         }
652         update_dipswitch();
653 }
654
655 void VM::reset()
656 {
657         // reset all devices
658         for(DEVICE* device = first_device; device; device = device->next_device) {
659                 device->reset();
660         }
661 #if !defined(_FM77AV_VARIANTS) || defined(_FM8)
662 # if defined(USE_AY_3_8910_AS_PSG)
663         psg->set_reg(0x2e, 0);  // set prescaler
664         psg->write_signal(SIG_AY_3_891X_MUTE, 0x00, 0x01); // Okay?
665 # else  
666         psg->set_reg(0x27, 0); // stop timer
667         psg->set_reg(0x2e, 0);  // set prescaler
668         psg->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
669 #endif
670 #endif
671 #if !defined(_FM8)
672         for(int i = 0; i < 3; i++) {
673                 opn[i]->set_reg(0x27, 0); // stop timer
674                 opn[i]->set_reg(0x2e, 0);       // set prescaler
675                 opn[i]->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
676         }
677 #endif
678 }
679
680 void VM::special_reset()
681 {
682         // BREAK + RESET
683         mainio->write_signal(FM7_MAINIO_PUSH_BREAK, 1, 1);
684         mainio->reset();
685         mainmem->reset();
686         
687 #if defined(FM77AV_VARIANTS)    
688         mainio->write_signal(FM7_MAINIO_HOT_RESET, 1, 1);
689 #endif  
690         display->reset();
691         subcpu->reset();
692         mainio->write_signal(FM7_MAINIO_PUSH_BREAK, 1, 1);
693         maincpu->reset();
694         event->register_event(mainio, EVENT_UP_BREAK, 10000.0 * 1000.0, false, NULL);
695 }
696
697 void VM::run()
698 {
699         event->drive();
700 }
701
702 double VM::get_frame_rate()
703 {
704         return event->get_frame_rate();
705 }
706
707
708
709 // ----------------------------------------------------------------------------
710 // debugger
711 // ----------------------------------------------------------------------------
712
713 #ifdef USE_DEBUGGER
714 DEVICE *VM::get_cpu(int index)
715 {
716         if(index == 0) {
717                 return maincpu;
718         } else if(index == 1) {
719                 return subcpu;
720         }
721 #if defined(WITH_Z80)
722         else if(index == 2) {
723 # if defined(CAPABLE_JCOMMCARD)
724                 if(z80cpu == NULL) {
725                         return jsubcpu;
726                 }
727 # endif
728                 return z80cpu;
729         }
730 # if defined(CAPABLE_JCOMMCARD)
731         else if(index == 3) {
732                 return jsubcpu;
733         }
734 # endif
735 #else
736 # if defined(CAPABLE_JCOMMCARD)
737         else if(index == 2) {
738                 return jsubcpu;
739         }
740 # endif
741 #endif
742         return NULL;
743 }
744 #endif
745
746 // ----------------------------------------------------------------------------
747 // draw screen
748 // ----------------------------------------------------------------------------
749
750 void VM::draw_screen()
751 {
752         display->draw_screen();
753 }
754
755 void VM::initialize_sound(int rate, int samples)
756 {
757         // init sound manager
758         event->initialize_sound(rate, samples);
759         // init sound gen
760 #if defined(_FM8)
761         psg->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
762 #else   
763         opn[0]->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
764         opn[1]->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
765         opn[2]->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
766 # if !defined(_FM77AV_VARIANTS)   
767         psg->initialize_sound(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
768 # endif
769 # if defined(_FM77AV_VARIANTS)
770         keyboard_beep->initialize_sound(rate, 2400.0, 512);
771 # endif
772 #endif  
773         pcm1bit->initialize_sound(rate, 2000);
774         //drec->initialize_sound(rate, 0);
775 }
776
777 uint16_t* VM::create_sound(int* extra_frames)
778 {
779         uint16_t* p = event->create_sound(extra_frames);
780         return p;
781 }
782
783 int VM::get_sound_buffer_ptr()
784 {
785         int pos = event->get_sound_buffer_ptr();
786         return pos; 
787 }
788
789 #ifdef USE_SOUND_VOLUME
790 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
791 {
792 #if !defined(_FM77AV_VARIANTS)
793         if(ch-- == 0) {
794                 psg->set_volume(0, decibel_l, decibel_r);
795                 psg->set_volume(1, decibel_l, decibel_r);
796         } else
797 #endif
798 #if !defined(_FM8)              
799         if(ch-- == 0) {
800                 opn[0]->set_volume(0, decibel_l, decibel_r);
801         } else if(ch-- == 0) {
802                 opn[0]->set_volume(1, decibel_l, decibel_r);
803         } else if(ch-- == 0) {
804                 opn[1]->set_volume(0, decibel_l, decibel_r);
805         } else if(ch-- == 0) {
806                 opn[1]->set_volume(1, decibel_l, decibel_r);
807         } else if(ch-- == 0) {
808                 opn[2]->set_volume(0, decibel_l, decibel_r);
809         } else if(ch-- == 0) {
810                 opn[2]->set_volume(1, decibel_l, decibel_r);
811         } else
812 #endif  
813         if(ch-- == 0) {
814                 pcm1bit->set_volume(0, decibel_l, decibel_r);
815         } else if(ch-- == 0) {
816                 if(drec != NULL) drec->set_volume(0, decibel_l, decibel_r);
817         }
818 #if defined(_FM77AV_VARIANTS)
819         else if(ch-- == 0) {
820                 keyboard_beep->set_volume(0, decibel_l, decibel_r);
821         }
822 #endif
823         else if(ch-- == 0) {
824                 if(fdc != NULL) {
825                         fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
826                         fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
827                         fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
828                 }
829         } else if(ch-- == 0) {
830                 if(drec != NULL) {
831                         drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
832                         drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
833                         drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
834                 }
835         }
836 }
837 #endif
838
839 // ----------------------------------------------------------------------------
840 // notify key
841 // ----------------------------------------------------------------------------
842
843 void VM::key_down(int code, bool repeat)
844 {
845         if(!repeat) {
846                 keyboard->key_down(code);
847         }
848 }
849
850 void VM::key_up(int code)
851 {
852         keyboard->key_up(code);
853 }
854
855 bool VM::get_caps_locked()
856 {
857         return keyboard->get_caps_locked();
858 }
859
860 bool VM::get_kana_locked()
861 {
862         return keyboard->get_kana_locked();
863 }
864
865 // Get INS status.Important with FM-7 series (^_^;
866 uint32_t VM::get_extra_leds()
867 {
868         return keyboard->read_signal(SIG_FM7KEY_LED_STATUS);
869 }
870
871
872 // ----------------------------------------------------------------------------
873 // user interface
874 // ----------------------------------------------------------------------------
875
876 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
877 {
878         if(fdc != NULL) {
879                 fdc->open_disk(drv, file_path, bank);
880         }
881 }
882
883 void VM::close_floppy_disk(int drv)
884 {
885         if(fdc != NULL) {
886                 fdc->close_disk(drv);
887         }
888 }
889
890 bool VM::is_floppy_disk_inserted(int drv)
891 {
892         if(fdc != NULL) {
893                 return fdc->is_disk_inserted(drv);
894         } else {
895                 return false;
896         }
897 }
898
899 void VM::is_floppy_disk_protected(int drv, bool value)
900 {
901         if(fdc != NULL) {
902                 fdc->is_disk_protected(drv, value);
903         }
904 }
905
906 bool VM::is_floppy_disk_protected(int drv)
907 {
908         if(fdc != NULL) {
909                 return fdc->is_disk_protected(drv);
910         } else {
911                 return false;
912         }
913 }
914
915 uint32_t VM::is_floppy_disk_accessed()
916 {
917         // WILLFIX : Multiple FDC for 1M FD.
918 #if defined(_FM8) || (_FM7) || (_FMNEW7)
919         if(connect_320kfdc || connect_1Mfdc) {
920 #endif          
921                 return fdc->read_signal(0);
922 #if defined(_FM8) || (_FM7) || (_FMNEW7)
923         } else {
924                 return 0x00000000;
925         }
926 #endif  
927 }
928
929 void VM::play_tape(int drv, const _TCHAR* file_path)
930 {
931         if(drec != NULL) drec->play_tape(file_path);
932 }
933
934 void VM::rec_tape(int drv, const _TCHAR* file_path)
935 {
936         if(drec != NULL) drec->rec_tape(file_path);
937 }
938
939 void VM::close_tape(int drv)
940 {
941         emu->lock_vm();
942         if(drec != NULL) drec->close_tape();
943         emu->unlock_vm();
944 }
945
946 bool VM::is_tape_inserted(int drv)
947 {
948         if(drec != NULL) {
949                 return drec->is_tape_inserted();
950         }
951         return false;
952 }
953
954 bool VM::is_tape_playing(int drv)
955 {
956         if(drec != NULL) {
957                 return drec->is_tape_playing();
958         }
959         return false;
960 }
961
962 bool VM::is_tape_recording(int drv)
963 {
964         if(drec != NULL) {
965                 return drec->is_tape_recording();
966         }
967         return false;
968 }
969
970 int VM::get_tape_position(int drv)
971 {
972         if(drec != NULL) {
973                 return drec->get_tape_position();
974         }
975         return 0;
976 }
977
978 const _TCHAR* VM::get_tape_message(int drv)
979 {
980         if(drec != NULL) {
981                 return drec->get_message();
982         }
983         return NULL;
984 }
985
986 void VM::push_play(int drv)
987 {
988         if(drec != NULL) {
989                 drec->set_ff_rew(0);
990                 drec->set_remote(true);
991         }
992 }
993
994
995 void VM::push_stop(int drv)
996 {
997         if(drec != NULL) {
998                 drec->set_remote(false);
999         }
1000 }
1001
1002 void VM::push_fast_forward(int drv)
1003 {
1004         if(drec != NULL) {
1005                 drec->set_ff_rew(1);
1006                 drec->set_remote(true);
1007         }
1008 }
1009
1010 void VM::push_fast_rewind(int drv)
1011 {
1012         if(drec != NULL) {
1013                 drec->set_ff_rew(-1);
1014                 drec->set_remote(true);
1015         }
1016 }
1017
1018 void VM::push_apss_forward(int drv)
1019 {
1020         if(drec != NULL) {
1021                 drec->do_apss(1);
1022         }
1023 }
1024
1025 void VM::push_apss_rewind(int drv)
1026 {
1027         if(drec != NULL) {
1028                 drec->do_apss(-1);
1029         }
1030 }
1031
1032 bool VM::is_frame_skippable()
1033 {
1034         return event->is_frame_skippable();
1035 }
1036
1037 void VM::update_dipswitch()
1038 {
1039 #if defined(WITH_Z80)
1040         g_intr_irq->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_IRQ_ON) != 0) ? 1 : 0, 1);
1041         g_intr_firq->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_FIRQ_ON) != 0) ? 1 : 0, 1);
1042         g_nmi->write_signal(SIG_AND_BIT_0, ((config.dipswitch & FM7_DIPSW_Z80_NMI_ON) != 0) ? 1 : 0, 1);
1043 #endif
1044 }
1045
1046 void VM::set_cpu_clock(DEVICE *cpu, uint32_t clocks) {
1047         event->set_secondary_cpu_clock(cpu, clocks);
1048 }
1049
1050 #if defined(USE_BUBBLE1)
1051 void VM::open_bubble_casette(int drv, const _TCHAR *path, int bank)
1052 {
1053         if((drv >= 2) || (drv < 0)) return;
1054         if(bubble_casette[drv] == NULL) return;
1055         bubble_casette[drv]->open((_TCHAR *)path, bank);
1056 }
1057
1058 void VM::close_bubble_casette(int drv)
1059 {
1060         if((drv >= 2) || (drv < 0)) return;
1061         if(bubble_casette[drv] == NULL) return;
1062         bubble_casette[drv]->close();
1063 }
1064
1065 bool VM::is_bubble_casette_inserted(int drv)
1066 {
1067         if((drv >= 2) || (drv < 0)) return false;
1068         if(bubble_casette[drv] == NULL) return false;
1069         return bubble_casette[drv]->is_bubble_inserted();
1070 }
1071
1072 bool VM::is_bubble_casette_protected(int drv)
1073 {
1074         if((drv >= 2) || (drv < 0)) return false;
1075         if(bubble_casette[drv] == NULL) return false;
1076         return bubble_casette[drv]->is_bubble_protected();
1077 }
1078
1079 void VM::is_bubble_casette_protected(int drv, bool flag)
1080 {
1081         if((drv >= 2) || (drv < 0)) return;
1082         if(bubble_casette[drv] == NULL) return;
1083         bubble_casette[drv]->set_bubble_protect(flag);
1084 }
1085 #endif
1086
1087
1088 #define STATE_VERSION   8
1089 void VM::save_state(FILEIO* state_fio)
1090 {
1091         state_fio->FputUint32_BE(STATE_VERSION);
1092         state_fio->FputBool(connect_320kfdc);
1093         state_fio->FputBool(connect_1Mfdc);
1094         for(DEVICE* device = first_device; device; device = device->next_device) {
1095                 const char *name = typeid(*device).name() + 6; // skip "class "
1096                 
1097                 state_fio->FputInt32(strlen(name));
1098                 state_fio->Fwrite(name, strlen(name), 1);
1099                 device->save_state(state_fio);
1100         }
1101 }
1102
1103 bool VM::load_state(FILEIO* state_fio)
1104 {
1105         uint32_t version = state_fio->FgetUint32_BE();
1106         if(version != STATE_VERSION) {
1107                 return false;
1108         }
1109         connect_320kfdc = state_fio->FgetBool();
1110         connect_1Mfdc = state_fio->FgetBool();
1111         for(DEVICE* device = first_device; device; device = device->next_device) {
1112                 const char *name = typeid(*device).name() + 6; // skip "class "
1113                 
1114                 if(!(state_fio->FgetInt32() == strlen(name) && state_fio->Fcompare(name, strlen(name)))) {
1115                         printf("Load Error: DEVID=%d\n", device->this_device_id);
1116                         return false;
1117                 }
1118                 if(!device->load_state(state_fio)) {
1119                         printf("Load Error: DEVID=%d\n", device->this_device_id);
1120                         return false;
1121                 }
1122         }
1123         return true;
1124 }
1125
1126 #ifdef USE_DIG_RESOLUTION
1127 void VM::get_screen_resolution(int *w, int *h)
1128 {
1129         switch(display->get_screen_mode()) {
1130         case DISPLAY_MODE_8_200L:
1131         case DISPLAY_MODE_8_200L_TEXT:
1132                 *w = 640;
1133                 *h = 200;
1134                 break;
1135         case DISPLAY_MODE_8_400L:
1136         case DISPLAY_MODE_8_400L_TEXT:
1137                 *w = 640;
1138                 *h = 400;
1139                 break;
1140         case DISPLAY_MODE_4096:
1141         case DISPLAY_MODE_256k:
1142                 *w = 320;
1143                 *h = 200;
1144                 break;
1145         default:
1146                 *w = 640;
1147                 *h = 200;
1148                 break;
1149         }
1150 }
1151 #endif
1152
1153 bool VM::is_screen_changed()
1154 {
1155         bool f = true;
1156 #if defined(USE_MINIMUM_RENDERING)
1157         f = display->screen_update();
1158         display->reset_screen_update();
1159 #endif  
1160         return f;
1161 }