1 #include "driver.h"
2 #include "cpu/z80/z80.h"
3 
4 
5 static unsigned char from_main,from_mcu;
6 static int mcu_sent = 0,main_sent = 0;
7 
8 
9 /***************************************************************************
10 
11  Return of Invaders 68705 protection interface
12 
13 ***************************************************************************/
14 
15 static unsigned char portA_in,portA_out,ddrA;
16 
READ_HANDLER(retofinv_68705_portA_r)17 READ_HANDLER( retofinv_68705_portA_r )
18 {
19 /*logerror("%04x: 68705 port A read %02x\n",activecpu_get_pc(),portA_in);*/
20 	return (portA_out & ddrA) | (portA_in & ~ddrA);
21 }
22 
WRITE_HANDLER(retofinv_68705_portA_w)23 WRITE_HANDLER( retofinv_68705_portA_w )
24 {
25 /*logerror("%04x: 68705 port A write %02x\n",activecpu_get_pc(),data);*/
26 	portA_out = data;
27 }
28 
WRITE_HANDLER(retofinv_68705_ddrA_w)29 WRITE_HANDLER( retofinv_68705_ddrA_w )
30 {
31 	ddrA = data;
32 }
33 
34 
35 
36 /*
37  *  Port B connections:
38  *
39  *  all bits are logical 1 when read (+5V pullup)
40  *
41  *  1   W  IRQ ack and enable latch which holds data from main Z80 memory
42  *  2   W  loads latch to Z80
43  */
44 
45 static unsigned char portB_in,portB_out,ddrB;
46 
READ_HANDLER(retofinv_68705_portB_r)47 READ_HANDLER( retofinv_68705_portB_r )
48 {
49 	return (portB_out & ddrB) | (portB_in & ~ddrB);
50 }
51 
WRITE_HANDLER(retofinv_68705_portB_w)52 WRITE_HANDLER( retofinv_68705_portB_w )
53 {
54 /*logerror("%04x: 68705 port B write %02x\n",activecpu_get_pc(),data);*/
55 
56 	if ((ddrB & 0x02) && (~data & 0x02) && (portB_out & 0x02))
57 	{
58 		portA_in = from_main;
59 		if (main_sent) cpu_set_irq_line(3,0,CLEAR_LINE);
60 		main_sent = 0;
61 /*logerror("read command %02x from main cpu\n",portA_in);*/
62 	}
63 	if ((ddrB & 0x04) && (data & 0x04) && (~portB_out & 0x04))
64 	{
65 /*logerror("send command %02x to main cpu\n",portA_out);*/
66 		from_mcu = portA_out;
67 		mcu_sent = 1;
68 	}
69 
70 	portB_out = data;
71 }
72 
WRITE_HANDLER(retofinv_68705_ddrB_w)73 WRITE_HANDLER( retofinv_68705_ddrB_w )
74 {
75 	ddrB = data;
76 }
77 
78 
79 /*
80  *  Port C connections:
81  *
82  *  all bits are logical 1 when read (+5V pullup)
83  *
84  *  0   R  1 when pending command Z80->68705
85  *  1   R  0 when pending command 68705->Z80
86  */
87 
88 static unsigned char portC_in,portC_out,ddrC;
89 
READ_HANDLER(retofinv_68705_portC_r)90 READ_HANDLER( retofinv_68705_portC_r )
91 {
92 	portC_in = 0;
93 	if (main_sent) portC_in |= 0x01;
94 	if (!mcu_sent) portC_in |= 0x02;
95 /*logerror("%04x: 68705 port C read %02x\n",activecpu_get_pc(),portC_in);*/
96 	return (portC_out & ddrC) | (portC_in & ~ddrC);
97 }
98 
WRITE_HANDLER(retofinv_68705_portC_w)99 WRITE_HANDLER( retofinv_68705_portC_w )
100 {
101 log_cb(RETRO_LOG_DEBUG, LOGPRE "%04x: 68705 port C write %02x\n",activecpu_get_pc(),data);
102 	portC_out = data;
103 }
104 
WRITE_HANDLER(retofinv_68705_ddrC_w)105 WRITE_HANDLER( retofinv_68705_ddrC_w )
106 {
107 	ddrC = data;
108 }
109 
110 
WRITE_HANDLER(retofinv_mcu_w)111 WRITE_HANDLER( retofinv_mcu_w )
112 {
113 log_cb(RETRO_LOG_DEBUG, LOGPRE "%04x: mcu_w %02x\n",activecpu_get_pc(),data);
114 	from_main = data;
115 	main_sent = 1;
116 	cpu_set_irq_line(3,0,ASSERT_LINE);
117 }
118 
READ_HANDLER(retofinv_mcu_r)119 READ_HANDLER( retofinv_mcu_r )
120 {
121 log_cb(RETRO_LOG_DEBUG, LOGPRE "%04x: mcu_r %02x\n",activecpu_get_pc(),from_mcu);
122 	mcu_sent = 0;
123 	return from_mcu;
124 }
125 
READ_HANDLER(retofinv_mcu_status_r)126 READ_HANDLER( retofinv_mcu_status_r )
127 {
128 	int res = 0;
129 
130 	/* bit 4 = when 1, mcu is ready to receive data from main cpu */
131 	/* bit 5 = when 1, mcu has sent data to the main cpu */
132 /*logerror("%04x: mcu_status_r\n",activecpu_get_pc());*/
133 	if (!main_sent) res |= 0x10;
134 	if (mcu_sent) res |= 0x20;
135 
136 	return res;
137 }
138