summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/7474.c
blob: bcfbeccfc4aebedee464ed6a9f5a7f58ff0cbbef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*****************************************************************************

  7474 positive-edge-triggered D-type flip-flop with preset, clear and
       complementary outputs.  There are 2 flip-flops per chips


  Pin layout and functions to access pins:

  clear_w        [1] /1CLR         VCC [14]
  d_w            [2]  1D         /2CLR [13]  clear_w
  clock_w        [3]  1CLK          2D [12]  d_w
  preset_w       [4] /1PR         2CLK [11]  clock_w
  output_r       [5]  1Q          /2PR [10]  preset_w
  output_comp_r  [6] /1Q            2Q [9]   output_r
                 [7]  GND          /2Q [8]   output_comp_r


  Truth table (all logic levels indicate the actual voltage on the line):

        INPUTS    | OUTPUTS
                  |
    PR  CLR CLK D | Q  /Q
    --------------+-------
 1  L   H   X   X | H   L
 2  H   L   X   X | L   H
 3  L   L   X   X | H   H  (Note 1)
 4  H   H  _-   X | D  /D
 5  H   H   L   X | Q0 /Q0
    --------------+-------
    L   = lo (0)
    H   = hi (1)
    X   = any state
    _-  = raising edge
    Q0  = previous state

    Note 1: Non-stable configuration

*****************************************************************************/

#include "driver.h"
#include "7474.h"


typedef struct _ttl7474_state ttl7474_state;
struct _ttl7474_state
{
	/* callback */
	void (*output_cb)(const device_config *);

	/* inputs */
	UINT8 clear;			/* pin 1/13 */
	UINT8 preset;			/* pin 4/10 */
	UINT8 clock;			/* pin 3/11 */
	UINT8 d;				/* pin 2/12 */

	/* outputs */
	UINT8 output;			/* pin 5/9 */
	UINT8 output_comp;	/* pin 6/8 */

	/* internal */
	UINT8 last_clock;
	UINT8 last_output;
	UINT8 last_output_comp;
};

INLINE ttl7474_state *get_safe_token(const device_config *device)
{
	assert(device != NULL);
	assert(device->token != NULL);
	assert(device->type == TTL7474);

	return (ttl7474_state *)device->token;
}


void ttl7474_update(const device_config *device)
{
	ttl7474_state *state = get_safe_token(device);

	if (!state->preset && state->clear)			  /* line 1 in truth table */
	{
		state->output 	 = 1;
		state->output_comp = 0;
	}
	else if (state->preset && !state->clear)	  /* line 2 in truth table */
	{
		state->output 	 = 0;
		state->output_comp = 1;
	}
	else if (!state->preset && !state->clear)	  /* line 3 in truth table */
	{
		state->output 	 = 1;
		state->output_comp = 1;
	}
	else if (!state->last_clock && state->clock)  /* line 4 in truth table */
	{
		state->output 	 =  state->d;
		state->output_comp = !state->d;
	}

	state->last_clock = state->clock;


	/* call callback if any of the outputs changed */
	if (  state->output_cb &&
	    ((state->output      != state->last_output) ||
	     (state->output_comp != state->last_output_comp)))
	{
		state->last_output = state->output;
		state->last_output_comp = state->output_comp;

		state->output_cb(device);
	}
}


void ttl7474_clear_w(const device_config *device, int data)
{
	ttl7474_state *state = get_safe_token(device);
	state->clear = data ? 1 : 0;
}

void ttl7474_preset_w(const device_config *device, int data)
{
	ttl7474_state *state = get_safe_token(device);
	state->preset = data ? 1 : 0;
}

void ttl7474_clock_w(const device_config *device, int data)
{
	ttl7474_state *state = get_safe_token(device);
	state->clock = data ? 1 : 0;
}

void ttl7474_d_w(const device_config *device, int data)
{
	ttl7474_state *state = get_safe_token(device);
	state->d = data ? 1 : 0;
}


int ttl7474_output_r(const device_config *device)
{
	ttl7474_state *state = get_safe_token(device);
	return state->output;
}

int ttl7474_output_comp_r(const device_config *device)
{
	ttl7474_state *state = get_safe_token(device);
	return state->output_comp;
}


static DEVICE_START( ttl7474 )
{
	ttl7474_config *config = (ttl7474_config *)device->inline_config;
	ttl7474_state *state = get_safe_token(device);
    state->output_cb = config->output_cb;

    state_save_register_device_item(device, 0, state->clear);
    state_save_register_device_item(device, 0, state->preset);
    state_save_register_device_item(device, 0, state->clock);
    state_save_register_device_item(device, 0, state->d);
    state_save_register_device_item(device, 0, state->output);
    state_save_register_device_item(device, 0, state->output_comp);
    state_save_register_device_item(device, 0, state->last_clock);
    state_save_register_device_item(device, 0, state->last_output);
    state_save_register_device_item(device, 0, state->last_output_comp);
}


static DEVICE_RESET( ttl7474 )
{
	ttl7474_state *state = get_safe_token(device);

	/* all inputs are open first */
    state->clear = 1;
    state->preset = 1;
    state->clock = 1;
    state->d = 1;

    state->last_clock = 1;
    state->last_output = -1;
    state->last_output_comp = -1;
}


static const char DEVTEMPLATE_SOURCE[] = __FILE__;

#define DEVTEMPLATE_ID(p,s)		p##ttl7474##s
#define DEVTEMPLATE_FEATURES	DT_HAS_START | DT_HAS_RESET | DT_HAS_INLINE_CONFIG
#define DEVTEMPLATE_NAME		"7474"
#define DEVTEMPLATE_FAMILY		"TTL"
#include "devtempl.h"