1 /*****************************************************************************
2 
3   7474 positive-edge-triggered D-type flip-flop with preset, clear and
4        complementary outputs.  There are 2 flip-flops per chips
5 
6 
7   Pin layout and functions to access pins:
8 
9   clear_w        [1] /1CLR		   VCC [14]
10   d_w            [2]  1D		 /2CLR [13]  clear_w
11   clock_w        [3]  1CLK		    2D [12]  d_w
12   preset_w       [4] /1PR		  2CLK [11]  clock_w
13   output_r       [5]  1Q	      /2PR [10]  preset_w
14   output_comp_r  [6] /1Q	        2Q [9]   output_r
15 	             [7]  GND	       /2Q [8]   output_comp_r
16 
17 
18   Truth table (all logic levels indicate the actual voltage on the line):
19 
20         INPUTS    | OUTPUTS
21                   |
22 	PR	CLR	CLK D | Q  /Q
23 	--------------+-------
24  1	L	H	X   X | H	L
25  2	H   L   X   X | L   H
26  3	L   L   X   X | H   H  (Note 1)
27  4	H   H  _-   X | D  /D
28  5	H   H   L   X | Q0 /Q0
29 	--------------+-------
30 	L	= lo (0)
31 	H	= hi (1)
32 	X	= any state
33 	_-	= raising edge
34 	Q0  = previous state
35 
36 	Note 1: Non-stable configuration
37 
38 *****************************************************************************/
39 
40 #include "driver.h"
41 #include "machine/7474.h"
42 
43 
44 #define MAX_TTL7474  12
45 
46 struct TTL7474
47 {
48 	/* callback */
49 	void (*output_cb)(void);
50 
51 	/* inputs */
52 	int clear;			/* pin 1/13 */
53 	int preset;			/* pin 4/10 */
54 	int clock;			/* pin 3/11 */
55 	int d;				/* pin 2/12 */
56 
57 	/* outputs */
58 	int output;			/* pin 5/9 */
59 	int output_comp;	/* pin 6/8 */
60 
61 	/* internal */
62 	int last_clock;
63 	int last_output;
64 	int last_output_comp;
65 };
66 
67 static struct TTL7474 chips[MAX_TTL7474];
68 
69 
TTL7474_update(int which)70 void TTL7474_update(int which)
71 {
72 	if (!chips[which].preset && chips[which].clear)			  /* line 1 in truth table */
73 	{
74 		chips[which].output 	 = 1;
75 		chips[which].output_comp = 0;
76 	}
77 	else if (chips[which].preset && !chips[which].clear)	  /* line 2 in truth table */
78 	{
79 		chips[which].output 	 = 0;
80 		chips[which].output_comp = 1;
81 	}
82 	else if (!chips[which].preset && !chips[which].clear)	  /* line 3 in truth table */
83 	{
84 		chips[which].output 	 = 1;
85 		chips[which].output_comp = 1;
86 	}
87 	else if (!chips[which].last_clock && chips[which].clock)  /* line 4 in truth table */
88 	{
89 		chips[which].output 	 =  chips[which].d;
90 		chips[which].output_comp = !chips[which].d;
91 	}
92 
93 	chips[which].last_clock = chips[which].clock;
94 
95 
96 	/* call callback if any of the outputs changed */
97 	if (  chips[which].output_cb &&
98 	    ((chips[which].output      != chips[which].last_output) ||
99 	     (chips[which].output_comp != chips[which].last_output_comp)))
100 	{
101 		chips[which].last_output = chips[which].output;
102 		chips[which].last_output_comp = chips[which].output_comp;
103 
104 		chips[which].output_cb();
105 	}
106 }
107 
108 
TTL7474_clear_w(int which,int data)109 void TTL7474_clear_w(int which, int data)
110 {
111 	chips[which].clear = data ? 1 : 0;
112 }
113 
TTL7474_preset_w(int which,int data)114 void TTL7474_preset_w(int which, int data)
115 {
116 	chips[which].preset = data ? 1 : 0;
117 }
118 
TTL7474_clock_w(int which,int data)119 void TTL7474_clock_w(int which, int data)
120 {
121 	chips[which].clock = data ? 1 : 0;
122 }
123 
TTL7474_d_w(int which,int data)124 void TTL7474_d_w(int which, int data)
125 {
126 	chips[which].d = data ? 1 : 0;
127 }
128 
129 
TTL7474_output_r(int which)130 int TTL7474_output_r(int which)
131 {
132 	return chips[which].output;
133 }
134 
TTL7474_output_comp_r(int which)135 int TTL7474_output_comp_r(int which)
136 {
137 	return chips[which].output_comp;
138 }
139 
140 
TTL7474_config(int which,const struct TTL7474_interface * intf)141 void TTL7474_config(int which, const struct TTL7474_interface *intf)
142 {
143 	if (which >= MAX_TTL7474)
144 	{
145 		log_cb(RETRO_LOG_DEBUG, LOGPRE "Only %d 7474's are supported at this time.\n", MAX_TTL7474);
146 		return;
147 	}
148 
149 
150 	chips[which].output_cb = (intf ? intf->output_cb : 0);
151 
152 	/* all inputs are open first */
153     chips[which].clear = 1;
154     chips[which].preset = 1;
155     chips[which].clock = 1;
156     chips[which].d = 1;
157 
158     chips[which].last_clock = 1;
159     chips[which].last_output = -1;
160     chips[which].last_output_comp = -1;
161 }
162