summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/devices/nld_truthtable.cpp
blob: 9cd2d206664bb84aa2afb43075338b2166dac628 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
 * nld_truthtable.c
 *
 */

#include "nld_truthtable.h"
#include "plib/plists.h"

NETLIB_NAMESPACE_DEVICES_START()

unsigned truthtable_desc_t::count_bits(UINT32 v)
{
	unsigned ret = 0;
	for (; v != 0; v = v >> 1)
	{
		if (v & 1)
			ret++;
	}
	return ret;
}

UINT32 truthtable_desc_t::set_bits(UINT32 v, UINT32 b)
{
	UINT32 ret = 0;
	int i = 0;
	for (; v != 0; v = v >> 1)
	{
		if (v & 1)
		{
			ret |= ((b&1)<<i);
			b = b >> 1;
		}
		i++;
	}
	return ret;
}

UINT32 truthtable_desc_t::get_ignored_simple(UINT32 i)
{
	UINT32 m_enable = 0;
	for (UINT32 j=0; j<m_size; j++)
	{
		if (m_outs[j] != m_outs[i])
		{
			m_enable |= (i ^ j);
		}
	}
	return m_enable ^ (m_size - 1);
}

UINT32 truthtable_desc_t::get_ignored_extended(UINT32 i)
{
	// Determine all inputs which may be ignored ...
	UINT32 nign = 0;
	for (unsigned j=0; j<m_NI; j++)
	{
		if (m_outs[i] == m_outs[i ^ (1 << j)])
			nign |= (1<<j);
	}
	/* Check all permutations of ign
	 * We have to remove those where the ignored inputs
	 * may change the output
	 */
	UINT32 bits = (1<<count_bits(nign));
	parray_t<int> t(bits);

	for (UINT32 j=1; j<bits; j++)
	{
		UINT32 tign = set_bits(nign, j);
		t[j] = 0;
		UINT32 bitsk=(1<<count_bits(tign));
		for (UINT32 k=0; k<bitsk; k++)
		{
			UINT32 b=set_bits(tign, k);
			if (m_outs[i] != m_outs[(i & ~tign) | b])
			{
				t[j] = 1;
				break;
			}
		}
	}
	UINT32 jb=0;
	UINT32 jm=0;
	for (UINT32 j=1; j<bits; j++)
	{
		unsigned nb = count_bits(j);
		if ((t[j] == 0)&& (nb>jb))
		{
			jb = nb;
			jm = j;
		}
	}
	return set_bits(nign, jm);
}

// ----------------------------------------------------------------------------------------
// desc
// ----------------------------------------------------------------------------------------

void truthtable_desc_t::help(unsigned cur, pstring_vector_t list,
		UINT64 state,UINT16 val, parray_t<UINT8> &timing_index)
{
	pstring elem = list[cur].trim();
	int start = 0;
	int end = 0;

	if (elem.equals("0"))
	{
		start = 0;
		end = 0;
	}
	else if (elem.equals("1"))
	{
		start = 1;
		end = 1;
	}
	else if (elem.equals("X"))
	{
		start = 0;
		end = 1;
	}
	else
		nl_assert_always(false, "unknown input value (not 0, 1, or X)");
	for (int i = start; i <= end; i++)
	{
		const UINT64 nstate = state | (i << cur);

		if (cur < m_num_bits - 1)
		{
			help(cur + 1, list, nstate, val, timing_index);
		}
		else
		{
			// cutoff previous inputs and outputs for ignore
			if (m_outs[nstate] != ~0U &&  m_outs[nstate] != val)
				fatalerror_e(pfmt("Error in truthtable: State {1} already set, {2} != {3}\n")
						.x(nstate,"04")(m_outs[nstate])(val) );
			m_outs[nstate] = val;
			for (unsigned j=0; j<m_NO; j++)
				m_timing[nstate * m_NO + j] = timing_index[j];
		}
	}
}

void truthtable_desc_t::setup(const pstring_vector_t &truthtable, UINT32 disabled_ignore)
{
	unsigned line = 0;

	if (*m_initialized)
		return;

	pstring ttline = truthtable[line];
	line++;
	ttline = truthtable[line];
	line++;

	for (unsigned j=0; j < m_size; j++)
		m_outs[j] = ~0L;

	for (int j=0; j < 16; j++)
		m_timing_nt[j] = netlist_time::zero;

	while (!ttline.equals(""))
	{
		pstring_vector_t io(ttline,"|");
		// checks
		nl_assert_always(io.size() == 3, "io.count mismatch");
		pstring_vector_t inout(io[0], ",");
		nl_assert_always(inout.size() == m_num_bits, "number of bits not matching");
		pstring_vector_t out(io[1], ",");
		nl_assert_always(out.size() == m_NO, "output count not matching");
		pstring_vector_t times(io[2], ",");
		nl_assert_always(times.size() == m_NO, "timing count not matching");

		UINT16 val = 0;
		parray_t<UINT8> tindex(m_NO);

		for (unsigned j=0; j<m_NO; j++)
		{
			pstring outs = out[j].trim();
			if (outs.equals("1"))
				val = val | (1 << j);
			else
				nl_assert_always(outs.equals("0"), "Unknown value (not 0 or 1");
			netlist_time t = netlist_time::from_nsec(times[j].trim().as_long());
			int k=0;
			while (m_timing_nt[k] != netlist_time::zero && m_timing_nt[k] != t)
				k++;
			m_timing_nt[k] = t;
			tindex[j] = k;
		}

		help(0, inout, 0 , val, tindex);
		if (line < truthtable.size())
			ttline = truthtable[line];
		else
			ttline = "";
		line++;
	}

	// determine ignore
	parray_t<UINT32> ign(m_size);

	for (UINT32 j=0; j < m_size; j++)
		ign[j] = ~0U;

	for (UINT32 i=0; i<m_size; i++)
	{
		if (ign[i] == ~0U)
		{
			int tign;
			if (0)
			{
				tign = get_ignored_simple(i);
				ign[i] = tign;
			}
			else
			{
				tign = get_ignored_extended(i);

				ign[i] = tign;
				/* don't need to recalculate similar ones */
				UINT32 bitsk=(1<<count_bits(tign));
				for (UINT32 k=0; k<bitsk; k++)
				{
					UINT32 b=set_bits(tign, k);
					ign[(i & ~tign) | b] = tign;
				}
			}
		}
	}
	for (UINT32 i=0; i<m_size; i++)
	{
		if (m_outs[i] == ~0U)
			throw fatalerror_e(pfmt("truthtable: found element not set {1}\n").x(i) );
		m_outs[i] |= ((ign[i] & ~disabled_ignore)  << m_NO);
	}
	*m_initialized = true;

}

#define ENTRYX(_n,_m,_h)    case (_n * 1000 + _m * 10 + _h): \
	{ typedef netlist_factory_truthtable_t<_n,_m,_h> xtype; \
		return palloc(xtype(name,classname,def_param)); } break

#define ENTRYY(_n,_m)   ENTRYX(_n,_m,0); ENTRYX(_n,_m,1)

#define ENTRY(_n) ENTRYY(_n, 1); ENTRYY(_n, 2); ENTRYY(_n, 3); ENTRYY(_n, 4); ENTRYY(_n, 5); ENTRYY(_n, 6)

netlist_base_factory_truthtable_t *nl_tt_factory_create(const unsigned ni, const unsigned no,
		const unsigned has_state,
		const pstring &name, const pstring &classname,
		const pstring &def_param)
{
	switch (ni * 1000 + no * 10 + has_state)
	{
		ENTRY(1);
		ENTRY(2);
		ENTRY(3);
		ENTRY(4);
		ENTRY(5);
		ENTRY(6);
		ENTRY(7);
		ENTRY(8);
		ENTRY(9);
		ENTRY(10);
		default:
			pstring msg = pfmt("unable to create truthtable<{1},{2},{3}>")(ni)(no)(has_state);
			nl_assert_always(false, msg);
	}
	return NULL;
}

NETLIB_NAMESPACE_DEVICES_END()