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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
#!/usr/bin/env python
# Generate the body of ieee.std_logic_1164 from a template.
# This file is part of GHDL.
# Copyright (C) 2015 Tristan Gingold
#
# GHDL is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any later
# version.
#
# GHDL is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with GCC; see the file COPYING3. If not see
# <http://www.gnu.org/licenses/>.
import re
import sys
binary_funcs = [ "and", "nand", "or", "nor", "xor" ]
# Python modelisation of std_ulogic type.
std_logic = "UX01ZWLH-"
# Normalization map.
ux01_map = { 'U': 'U',
'X': 'X', 'Z': 'X', 'W': 'X', '-': 'X',
'0': '0', 'L': '0',
'1': '1', 'H': '1' }
def sl_and(a,b):
"""and definition"""
na = ux01_map[a]
nb = ux01_map[b]
if na == '0' or nb == '0':
return '0'
if na == 'U' or nb == 'U':
return 'U'
if na == 'X' or nb == 'X':
return 'X'
return '1'
def sl_not(a):
"""not definition"""
na = ux01_map[a]
if na == 'U':
return 'U'
if na == 'X':
return 'X'
if na == '1':
return '0'
else:
return '1'
def sl_or(a,b):
"or definition"
return sl_not(sl_and (sl_not(a), sl_not(b)))
def sl_xor(a,b):
"xor definition"
return sl_or(sl_and(a, sl_not(b)),
sl_and(sl_not(a), b))
# Stream to write.
out=sys.stdout
def w(s):
"Write S to the output"
out.write(s)
def gen_log_table2(name, func):
"Generate a logic table for binary operator NAME using its model FUNC"
w("""
constant {0}_table : table_2d :=
-- UX01ZWLH-
(""".format(name))
for a in std_logic:
w('"')
for b in std_logic:
w(func (a, b))
w('"')
if a != '-':
w(',')
else:
w(' ')
w(' -- {}\n'.format(a))
w(' ')
w(');\n')
def gen_log_table1(name, func):
"Generate a logic table for unary operator NAME using its model FUNC"
w("""
constant {0}_table : table_1d :=
-- UX01ZWLH-
""".format(name))
w('"')
for b in std_logic:
w(func (b))
w('";\n')
def disp_tables():
"Generate logic tables"
gen_log_table2("and", sl_and)
gen_log_table2("nand", lambda a,b : sl_not(sl_and(a, b)))
gen_log_table2("or", sl_or)
gen_log_table2("nor", lambda a,b : sl_not(sl_or(a,b)))
gen_log_table2("xor", sl_xor)
if "xnor" in binary_funcs:
gen_log_table2("xnor", lambda a,b : sl_not(sl_xor(a,b)))
gen_log_table1("not", sl_not)
vec_types = ['ulogic', 'logic']
def disp_scalar_binary(fun):
"Generate scalar binary function body"
w("""
function "{0}" (l, r : std_ulogic) return UX01 is
begin
return {0}_table (l, r);
end "{0}";\n""".format(fun))
def disp_scalar_unary(fun):
"Generate scalar unary function body"
w("""
function "{0}" (l : std_ulogic) return UX01 is
begin
return {0}_table (l);
end "{0}";\n""".format(fun))
def disp_vec_binary(func, typ):
"Generate vector binary function body"
w("""
function "{0}" (l, r : std_{1}_vector) return std_{1}_vector
is
subtype res_type is std_{1}_vector (1 to l'length);
alias la : res_type is l;
alias ra : std_{1}_vector (1 to r'length) is r;
variable res : res_type;
begin
if la'length /= ra'length then
assert false
report "arguments of overloaded '{0}' operator are not of the same length"
severity failure;
else
for I in res_type'range loop
res (I) := {0}_table (la (I), ra (I));
end loop;
end if;
return res;
end "{0}";\n""".format(func, typ))
def disp_vec_unary(func, typ):
"Generate vector unary function body"
w("""
function "{0}" (l : std_{1}_vector) return std_{1}_vector
is
subtype res_type is std_{1}_vector (1 to l'length);
alias la : res_type is l;
variable res : res_type;
begin
for I in res_type'range loop
res (I) := {0}_table (la (I));
end loop;
return res;
end "{0}";\n""".format(func, typ))
def disp_all_log_funcs():
"Generate all function bodies for logic operators"
for f in binary_funcs:
disp_scalar_binary(f)
disp_scalar_unary("not")
for v in vec_types:
typ = "std_" + v + "_vector"
for f in binary_funcs:
disp_vec_binary(f, v)
disp_vec_unary("not", v)
def disp_sv_to_bv_conv(typ):
"Generate logic vector to bit vector function body"
w("""
function to_bitvector (s : std_{0}_vector; xmap : bit := '0')
return bit_vector
is
subtype res_range is natural range s'length - 1 downto 0;
alias as : std_{0}_vector (res_range) is s;
variable res : bit_vector (res_range);
variable b : bit;
begin
for I in res_range loop
-- Inline for efficiency.
case as (I) is
when '0' | 'L' =>
b := '0';
when '1' | 'H' =>
b := '1';
when others =>
b := xmap;
end case;
res (I) := b;
end loop;
return res;
end to_bitvector;\n""".format(typ))
def disp_bv_to_sv_conv(typ):
"Generate bit vector to logic vector function body"
w("""
function to_std{0}vector (b : bit_vector) return std_{0}_vector is
subtype res_range is natural range b'length - 1 downto 0;
alias ab : bit_vector (res_range) is b;
variable res : std_{0}_vector (res_range);
begin
for I in res_range loop
res (I) := bit_to_std (ab (I));
end loop;
return res;
end to_std{0}vector;\n""".format(typ))
def disp_sv_to_sv_conv(s,d):
"Generate logic vector to logic vector function body"
w("""
function to_std{1}vector (b : std_{0}_vector) return std_{1}_vector
is
subtype res_type is std_{1}_vector (b'length - 1 downto 0);
begin
return res_type (b);
end to_std{1}vector;\n""".format(s,d))
def disp_all_conv_funcs():
"Generate conversion function bodies"
for v in vec_types:
disp_sv_to_bv_conv(v)
for v in vec_types:
disp_bv_to_sv_conv(v)
disp_sv_to_sv_conv('logic', 'ulogic')
disp_sv_to_sv_conv('ulogic', 'logic')
def disp_conv_vec_vec(typ, v):
"Generate function body for vector conversion"
utyp = typ.upper();
w("""
function to_{1} (s : std_{2}_vector) return std_{2}_vector
is
subtype res_type is std_{2}_vector (1 to s'length);
alias sa : res_type is s;
variable res : res_type;
begin
for i in res_type'range loop
res (i) := std_to_{0} (sa (i));
end loop;
return res;
end to_{1};\n""".format(typ, utyp, v))
def disp_conv_std(typ):
"Generate function body for scalar conversion"
utyp = typ.upper();
w("""
function to_{1} (s : std_ulogic) return {1} is
begin
return std_to_{0} (s);
end to_{1};\n""".format(typ, utyp))
def disp_conv_bv_vec(typ, v):
"Generate function body for bit vector conversion"
utyp = typ.upper();
w("""
function to_{1} (b : bit_vector) return std_{2}_vector
is
subtype res_range is natural range 1 to b'length;
alias ba : bit_vector (res_range) is b;
variable res : std_{2}_vector (res_range);
begin
for i in res_range loop
res (i) := bit_to_x01 (ba (i));
end loop;
return res;
end to_{1};\n""".format(typ, utyp, v))
def disp_conv_b_t(typ):
"Generate function body for bit conversion"
utyp = typ.upper();
w("""
function to_{1} (b : bit) return {1} is
begin
return bit_to_x01 (b);
end to_{1};\n""".format(typ, utyp))
def disp_all_norm_funcs():
"Generate all function bodies for conversion"
for typ in [ "x01", "x01z", "ux01" ]:
for v in vec_types:
disp_conv_vec_vec(typ, v)
disp_conv_std (typ)
for v in vec_types:
disp_conv_bv_vec(typ, v)
disp_conv_b_t(typ)
def disp_all_isx_funcs():
"Generate all function bodies for isx functions"
for v in vec_types:
w("""
function is_X (s : std_{0}_vector) return boolean is
begin
for i in s'range loop
if std_x (s (i)) then
return true;
end if;
end loop;
return false;
end is_X;\n""".format(v))
w("""
function is_X (s : std_ulogic) return boolean is
begin
return std_x (s);
end is_X;\n""")
# Patterns to replace
pats = {' @TAB\n' : disp_tables,
' @LOG\n' : disp_all_log_funcs,
' @CONV\n': disp_all_conv_funcs,
' @NORM\n': disp_all_norm_funcs,
' @ISX\n' : disp_all_isx_funcs }
spec_file='std_logic_1164.vhdl'
proto_file='std_logic_1164-body.proto'
def gen_body():
w('-- This file was generated from ' + proto_file + '\n')
for line in open(proto_file):
if line in pats:
pats[line]()
continue
w(line)
# Copy spec
out=open('std_logic_1164.v87', 'w')
for line in open(spec_file):
if '"xnor"' in line:
w("--" + line[2:])
else:
w(line)
out.close()
out=open('std_logic_1164.v93', 'w')
for line in open(spec_file):
w(line)
out.close()
# Generate bodies
out=open('std_logic_1164-body.v87', 'w')
gen_body()
out.close()
binary_funcs.append("xnor")
out=open('std_logic_1164-body.v93', 'w')
gen_body()
out.close()
|