1 /***************************************************************************
2 
3   machine.c
4 
5   Functions to emulate general aspects of the machine (RAM, ROM, interrupts,
6   I/O ports)
7 
8 ***************************************************************************/
9 
10 #include "driver.h"
11 
12 
13 static unsigned char from_main,from_mcu;
14 static int mcu_sent = 0,main_sent = 0;
15 
16 
17 /***************************************************************************
18 
19  Fairy Land Story 68705 protection interface
20 
21  The following is ENTIRELY GUESSWORK!!!
22 
23 ***************************************************************************/
24 
25 static unsigned char portA_in,portA_out,ddrA;
26 
READ_HANDLER(flstory_68705_portA_r)27 READ_HANDLER( flstory_68705_portA_r )
28 {
29 //logerror("%04x: 68705 port A read %02x\n",cpu_get_pc(),portA_in);
30 	return (portA_out & ddrA) | (portA_in & ~ddrA);
31 }
32 
WRITE_HANDLER(flstory_68705_portA_w)33 WRITE_HANDLER( flstory_68705_portA_w )
34 {
35 //logerror("%04x: 68705 port A write %02x\n",cpu_get_pc(),data);
36 	portA_out = data;
37 }
38 
WRITE_HANDLER(flstory_68705_ddrA_w)39 WRITE_HANDLER( flstory_68705_ddrA_w )
40 {
41 	ddrA = data;
42 }
43 
44 
45 
46 /*
47  *  Port B connections:
48  *
49  *  all bits are logical 1 when read (+5V pullup)
50  *
51  *  1   W  when 1->0, enables latch which brings the command from main CPU (read from port A)
52  *  2   W  when 0->1, copies port A to the latch for the main CPU
53  */
54 
55 static unsigned char portB_in,portB_out,ddrB;
56 
READ_HANDLER(flstory_68705_portB_r)57 READ_HANDLER( flstory_68705_portB_r )
58 {
59 	return (portB_out & ddrB) | (portB_in & ~ddrB);
60 }
61 
WRITE_HANDLER(flstory_68705_portB_w)62 WRITE_HANDLER( flstory_68705_portB_w )
63 {
64 //logerror("%04x: 68705 port B write %02x\n",cpu_get_pc(),data);
65 
66 	if ((ddrB & 0x02) && (~data & 0x02) && (portB_out & 0x02))
67 	{
68 		portA_in = from_main;
69 		if (main_sent) cpu_set_irq_line(2,0,CLEAR_LINE);
70 		main_sent = 0;
71 logerror("read command %02x from main cpu\n",portA_in);
72 	}
73 	if ((ddrB & 0x04) && (data & 0x04) && (~portB_out & 0x04))
74 	{
75 logerror("send command %02x to main cpu\n",portA_out);
76 		from_mcu = portA_out;
77 		mcu_sent = 1;
78 	}
79 
80 	portB_out = data;
81 }
82 
WRITE_HANDLER(flstory_68705_ddrB_w)83 WRITE_HANDLER( flstory_68705_ddrB_w )
84 {
85 	ddrB = data;
86 }
87 
88 
89 static unsigned char portC_in,portC_out,ddrC;
90 
READ_HANDLER(flstory_68705_portC_r)91 READ_HANDLER( flstory_68705_portC_r )
92 {
93 	portC_in = 0;
94 	if (main_sent) portC_in |= 0x01;
95 	if (!mcu_sent) portC_in |= 0x02;
96 //logerror("%04x: 68705 port C read %02x\n",cpu_get_pc(),portC_in);
97 	return (portC_out & ddrC) | (portC_in & ~ddrC);
98 }
99 
WRITE_HANDLER(flstory_68705_portC_w)100 WRITE_HANDLER( flstory_68705_portC_w )
101 {
102 logerror("%04x: 68705 port C write %02x\n",cpu_get_pc(),data);
103 	portC_out = data;
104 }
105 
WRITE_HANDLER(flstory_68705_ddrC_w)106 WRITE_HANDLER( flstory_68705_ddrC_w )
107 {
108 	ddrC = data;
109 }
110 
111 
WRITE_HANDLER(flstory_mcu_w)112 WRITE_HANDLER( flstory_mcu_w )
113 {
114 logerror("%04x: mcu_w %02x\n",cpu_get_pc(),data);
115 	from_main = data;
116 	main_sent = 1;
117 	cpu_set_irq_line(2,0,ASSERT_LINE);
118 }
119 
READ_HANDLER(flstory_mcu_r)120 READ_HANDLER( flstory_mcu_r )
121 {
122 logerror("%04x: mcu_r %02x\n",cpu_get_pc(),from_mcu);
123 	mcu_sent = 0;
124 	return from_mcu;
125 }
126 
READ_HANDLER(flstory_mcu_status_r)127 READ_HANDLER( flstory_mcu_status_r )
128 {
129 	int res = 0;
130 
131 	/* bit 0 = when 1, mcu is ready to receive data from main cpu */
132 	/* bit 1 = when 1, mcu has sent data to the main cpu */
133 //logerror("%04x: mcu_status_r\n",cpu_get_pc());
134 	if (!main_sent) res |= 0x01;
135 	if (mcu_sent) res |= 0x02;
136 
137 	return res;
138 }
139