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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
* nld_truthtable.h
*
* Created on: 19 Jun 2014
* Author: andre
*/
#ifndef NLD_TRUTHTABLE_H_
#define NLD_TRUTHTABLE_H_
#include "nl_base.h"
#include "nl_factory.h"
#define NETLIB_TRUTHTABLE(_name, _nIN, _nOUT, _state) \
class NETLIB_NAME(_name) : public nld_truthtable_t<_nIN, _nOUT, _state> \
{ \
public: \
NETLIB_NAME(_name)() \
: nld_truthtable_t<_nIN, _nOUT, _state>(&m_ttbl, m_desc) { } \
private: \
static truthtable_t m_ttbl; \
static const char *m_desc[]; \
}
#define TRUTHTABLE_START(_name, _in, _out, _has_state, _def_params) \
{ \
netlist::devices::netlist_base_factory_truthtable_t *ttd = netlist::devices::nl_tt_factory_create(_in, _out, _has_state, \
# _name, # _name, "+" _def_params);
#define TT_HEAD(_x) \
ttd->m_desc.push_back(_x);
#define TT_LINE(_x) \
ttd->m_desc.push_back(_x);
#define TT_FAMILY(_x) \
ttd->m_family = setup.family_from_model(_x);
#define TRUTHTABLE_END() \
setup.factory().register_device(ttd); \
}
NETLIB_NAMESPACE_DEVICES_START()
#if 0
static inline UINT32 remove_first_bit(UINT32 v)
{
for (int i=0; i<32; i++)
if (v & (1<<i))
return v & ~(1<<i);
return v;
}
#endif
struct truthtable_desc_t
{
truthtable_desc_t(int NO, int NI, int has_state, bool *initialized,
UINT32 *outs, UINT8 *timing, netlist_time *timing_nt)
: m_NO(NO), m_NI(NI), /*m_has_state(has_state),*/ m_initialized(initialized),
m_outs(outs), m_timing(timing), m_timing_nt(timing_nt),
m_num_bits(m_NI + has_state * (m_NI + m_NO)),
m_size(1 << (m_num_bits))
{
}
void setup(const pstring_vector_t &desc, UINT32 disabled_ignore);
private:
void help(unsigned cur, pstring_vector_t list,
UINT64 state,UINT16 val, parray_t<UINT8> &timing_index);
static unsigned count_bits(UINT32 v);
static UINT32 set_bits(UINT32 v, UINT32 b);
UINT32 get_ignored_simple(UINT32 i);
UINT32 get_ignored_extended(UINT32 i);
unsigned m_NO;
unsigned m_NI;
//int m_has_state;
bool *m_initialized;
UINT32 *m_outs;
UINT8 *m_timing;
netlist_time *m_timing_nt;
/* additional values */
const UINT32 m_num_bits;
const UINT32 m_size;
};
template<unsigned m_NI, unsigned m_NO, int has_state>
class nld_truthtable_t : public device_t
{
public:
static const int m_num_bits = m_NI + has_state * (m_NI + m_NO);
static const int m_size = (1 << (m_num_bits));
struct truthtable_t
{
truthtable_t() : m_initialized(false),
m_desc(m_NO, m_NI, has_state, &m_initialized, m_outs, m_timing, m_timing_nt) {}
bool m_initialized;
UINT32 m_outs[m_size];
UINT8 m_timing[m_size * m_NO];
netlist_time m_timing_nt[16];
truthtable_desc_t m_desc;
};
nld_truthtable_t(truthtable_t *ttbl, const char *desc[])
: device_t(), m_last_state(0), m_ign(0), m_active(1), m_ttp(ttbl)
{
while (*desc != NULL && **desc != 0 )
{
m_desc.push_back(*desc);
desc++;
}
}
nld_truthtable_t(truthtable_t *ttbl, const pstring_vector_t &desc)
: device_t(), m_last_state(0), m_ign(0), m_active(1), m_ttp(ttbl)
{
m_desc = desc;
}
virtual void start() override
{
pstring header = m_desc[0];
pstring_vector_t io(header,"|");
// checks
nl_assert_always(io.size() == 2, "too many '|'");
pstring_vector_t inout(io[0], ",");
nl_assert_always(inout.size() == m_num_bits, "bitcount wrong");
pstring_vector_t out(io[1], ",");
nl_assert_always(out.size() == m_NO, "output count wrong");
for (unsigned i=0; i < m_NI; i++)
{
inout[i] = inout[i].trim();
register_input(inout[i], m_I[i]);
}
for (unsigned i=0; i < m_NO; i++)
{
out[i] = out[i].trim();
register_output(out[i], m_Q[i]);
}
// Connect output "Q" to input "_Q" if this exists
// This enables timed state without having explicit state ....
UINT32 disabled_ignore = 0;
for (unsigned i=0; i < m_NO; i++)
{
pstring tmp = "_" + out[i];
const int idx = inout.indexof(tmp);
if (idx>=0)
{
connect_late(m_Q[i], m_I[idx]);
// disable ignore for this inputs altogether.
// FIXME: This shouldn't be necessary
disabled_ignore |= (1<<idx);
}
}
m_ign = 0;
m_ttp->m_desc.setup(m_desc, disabled_ignore * 0);
#if 0
printf("%s\n", name().cstr());
for (int j=0; j < m_size; j++)
printf("%05x %04x %04x %04x\n", j, m_ttp->m_outs[j] & ((1 << m_NO)-1),
m_ttp->m_outs[j] >> m_NO, m_ttp->m_timing[j * m_NO + 0]);
for (int k=0; m_ttp->m_timing_nt[k] != netlist_time::zero; k++)
printf("%d %f\n", k, m_ttp->m_timing_nt[k].as_double() * 1000000.0);
#endif
save(NLNAME(m_last_state));
save(NLNAME(m_ign));
save(NLNAME(m_active));
}
void reset() override
{
m_active = 0;
m_ign = 0;
for (unsigned i = 0; i < m_NI; i++)
m_I[i].activate();
for (unsigned i=0; i<m_NO;i++)
if (this->m_Q[i].net().num_cons()>0)
m_active++;
m_last_state = 0;
}
template<bool doOUT>
inline void process()
{
netlist_time mt = netlist_time::zero;
UINT32 state = 0;
for (unsigned i = 0; i < m_NI; i++)
{
if (!doOUT || (m_ign & (1<<i)))
m_I[i].activate();
}
for (unsigned i = 0; i < m_NI; i++)
{
state |= (INPLOGIC(m_I[i]) << i);
if (!doOUT)
if (this->m_I[i].net().time() > mt)
mt = this->m_I[i].net().time();
}
const UINT32 nstate = state | (has_state ? (m_last_state << m_NI) : 0);
const UINT32 outstate = m_ttp->m_outs[nstate];
const UINT32 out = outstate & ((1 << m_NO) - 1);
m_ign = outstate >> m_NO;
if (has_state)
m_last_state = (state << m_NO) | out;
#if 0
for (int i = 0; i < m_NI; i++)
if (m_ign & (1 << i))
m_I[i].inactivate();
#endif
const UINT32 timebase = nstate * m_NO;
if (doOUT)
{
for (unsigned i = 0; i < m_NO; i++)
OUTLOGIC(m_Q[i], (out >> i) & 1, m_ttp->m_timing_nt[m_ttp->m_timing[timebase + i]]);
}
else
for (unsigned i = 0; i < m_NO; i++)
m_Q[i].net().set_Q_time((out >> i) & 1, mt + m_ttp->m_timing_nt[m_ttp->m_timing[timebase + i]]);
if (m_NI > 1 || has_state)
{
for (unsigned i = 0; i < m_NI; i++)
if (m_ign & (1 << i))
m_I[i].inactivate();
}
}
ATTR_HOT void update() override
{
process<true>();
}
ATTR_HOT void inc_active() override
{
nl_assert(netlist().use_deactivate());
if (has_state == 0)
if (++m_active == 1)
{
process<false>();
}
}
ATTR_HOT void dec_active() override
{
nl_assert(netlist().use_deactivate());
/* FIXME:
* Based on current measurements there is no point to disable
* 1 input devices. This should actually be a parameter so that we
* can decide for each individual gate whether it is benefitial to
* ignore deactivation.
*/
if (m_NI < 2)
return;
else if (has_state == 0)
{
if (--m_active == 0)
{
for (unsigned i = 0; i< m_NI; i++)
m_I[i].inactivate();
m_ign = (1<<m_NI)-1;
}
}
}
logic_input_t m_I[m_NI];
logic_output_t m_Q[m_NO];
private:
UINT32 m_last_state;
UINT32 m_ign;
INT32 m_active;
truthtable_t *m_ttp;
pstring_vector_t m_desc;
};
class netlist_base_factory_truthtable_t : public base_factory_t
{
P_PREVENT_COPYING(netlist_base_factory_truthtable_t)
public:
netlist_base_factory_truthtable_t(const pstring &name, const pstring &classname,
const pstring &def_param)
: base_factory_t(name, classname, def_param), m_family(netlist_family_TTL)
{}
virtual ~netlist_base_factory_truthtable_t()
{
if (!m_family->m_is_static)
pfree(m_family);
}
pstring_vector_t m_desc;
logic_family_desc_t *m_family;
};
template<unsigned m_NI, unsigned m_NO, int has_state>
class netlist_factory_truthtable_t : public netlist_base_factory_truthtable_t
{
P_PREVENT_COPYING(netlist_factory_truthtable_t)
public:
netlist_factory_truthtable_t(const pstring &name, const pstring &classname,
const pstring &def_param)
: netlist_base_factory_truthtable_t(name, classname, def_param) { }
device_t *Create() override
{
typedef nld_truthtable_t<m_NI, m_NO, has_state> tt_type;
device_t *r = palloc(tt_type(&m_ttbl, m_desc));
r->set_logic_family(m_family);
//r->init(setup, name);
return r;
}
private:
typename nld_truthtable_t<m_NI, m_NO, has_state>::truthtable_t m_ttbl;
};
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);
NETLIB_NAMESPACE_DEVICES_END()
#endif /* NLD_TRUTHTABLE_H_ */
|