1 |
59 |
ghutchis |
#!/usr/bin/env python
|
2 |
|
|
# Copyright (c) 2004 Guy Hutchison (ghutchis@opencores.org)
|
3 |
|
|
#
|
4 |
|
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
5 |
|
|
# copy of this software and associated documentation files (the "Software"),
|
6 |
|
|
# to deal in the Software without restriction, including without limitation
|
7 |
|
|
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
8 |
|
|
# and/or sell copies of the Software, and to permit persons to whom the
|
9 |
|
|
# Software is furnished to do so, subject to the following conditions:
|
10 |
|
|
#
|
11 |
|
|
# The above copyright notice and this permission notice shall be included
|
12 |
|
|
# in all copies or substantial portions of the Software.
|
13 |
|
|
#
|
14 |
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15 |
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16 |
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
17 |
|
|
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
18 |
|
|
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
19 |
|
|
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
20 |
|
|
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21 |
|
|
|
22 |
67 |
ghutchis |
import string, math, re
|
23 |
59 |
ghutchis |
|
24 |
|
|
def log2 (num):
|
25 |
|
|
return math.ceil (math.log (num) / math.log (2))
|
26 |
|
|
|
27 |
67 |
ghutchis |
# function that tries to interpret a number in Verilog notation
|
28 |
|
|
def number (str):
|
29 |
|
|
try:
|
30 |
|
|
robj = re.compile ("(\d+)'([dhb])([\da-fA-F]+)")
|
31 |
|
|
mobj = robj.match (str)
|
32 |
|
|
if (mobj):
|
33 |
|
|
if mobj.group(2) == 'h': radix = 16
|
34 |
|
|
elif mobj.group(2) == 'b': radix = 2
|
35 |
|
|
else: radix = 10
|
36 |
|
|
|
37 |
|
|
return int (mobj.group(3), radix)
|
38 |
|
|
else:
|
39 |
|
|
return int(str)
|
40 |
|
|
except ValueError:
|
41 |
|
|
print "ERROR: number conversion of %s failed" % str
|
42 |
|
|
return 0
|
43 |
|
|
|
44 |
59 |
ghutchis |
def comb_block (statements):
|
45 |
|
|
result = 'always @*\n'
|
46 |
|
|
result += ' begin\n'
|
47 |
|
|
for s in statements:
|
48 |
|
|
result += ' ' + s + '\n'
|
49 |
|
|
result += ' end\n'
|
50 |
|
|
return result
|
51 |
|
|
|
52 |
|
|
def seq_block (clock, statements):
|
53 |
|
|
result = 'always @(posedge ' + clock + ')\n'
|
54 |
|
|
result += ' begin\n'
|
55 |
|
|
for s in statements:
|
56 |
|
|
result += ' ' + s + '\n'
|
57 |
|
|
result += ' end\n'
|
58 |
|
|
return result
|
59 |
|
|
|
60 |
|
|
class net:
|
61 |
|
|
def __init__ (self, type, name, width=1):
|
62 |
|
|
self.width = width
|
63 |
|
|
self.name = name
|
64 |
|
|
self.type = type
|
65 |
|
|
|
66 |
|
|
def declaration (self):
|
67 |
|
|
if (self.width == 1):
|
68 |
|
|
return self.type + ' ' + self.name + ';'
|
69 |
|
|
else:
|
70 |
|
|
return "%s [%d:0] %s;" % (self.type, self.width-1, self.name)
|
71 |
|
|
|
72 |
|
|
class port:
|
73 |
|
|
def __init__ (self, direction, name, width=1):
|
74 |
|
|
self.direction = direction
|
75 |
|
|
self.width = width
|
76 |
|
|
self.name = name
|
77 |
|
|
|
78 |
|
|
def declaration (self):
|
79 |
|
|
if (self.width == 1):
|
80 |
|
|
return self.direction + ' ' + self.name + ';'
|
81 |
|
|
else:
|
82 |
|
|
return "%s [%d:0] %s;" % (self.direction, self.width-1, self.name)
|
83 |
|
|
|
84 |
|
|
class register_group:
|
85 |
|
|
def __init__ (self, mem_mapped=0):
|
86 |
|
|
self.base_addr = 0
|
87 |
|
|
self.addr_size = 16
|
88 |
|
|
self.data_size = 8
|
89 |
|
|
self.name = ''
|
90 |
|
|
self.local_width = 1
|
91 |
|
|
self.registers = []
|
92 |
|
|
self.ports = [port ('input', 'clk'), port('input','reset')]
|
93 |
|
|
self.nets = []
|
94 |
64 |
ghutchis |
self.interrupts = 0
|
95 |
59 |
ghutchis |
if (mem_mapped):
|
96 |
|
|
self.req_pin = 'mreq_n'
|
97 |
|
|
else:
|
98 |
|
|
self.req_pin = 'iorq_n'
|
99 |
|
|
self.tv80_intf()
|
100 |
|
|
|
101 |
|
|
def tv80_intf (self):
|
102 |
|
|
self.ports.append (port ('input', 'addr', self.addr_size))
|
103 |
|
|
self.ports.append (port ('input', 'wr_data', self.data_size))
|
104 |
|
|
self.ports.append (port ('output', 'rd_data', self.data_size))
|
105 |
|
|
self.ports.append (port ('output', 'doe'))
|
106 |
|
|
self.ports.append (port ('input','rd_n'))
|
107 |
|
|
self.ports.append (port ('input', 'wr_n'))
|
108 |
|
|
self.ports.append (port ('input', self.req_pin))
|
109 |
64 |
ghutchis |
self.nets.append (net('reg','rd_data',self.data_size))
|
110 |
|
|
self.nets.append (net('reg','block_select'))
|
111 |
|
|
self.nets.append (net('reg','doe'))
|
112 |
59 |
ghutchis |
|
113 |
64 |
ghutchis |
# create a hook for post-processing to be done after all data has been
|
114 |
|
|
# added to the object.
|
115 |
|
|
def post (self):
|
116 |
67 |
ghutchis |
if (self.interrupts):
|
117 |
|
|
self.int_ports()
|
118 |
64 |
ghutchis |
|
119 |
|
|
# create port for interrupt pin, as well as port for data output enable
|
120 |
|
|
# when interrupt is asserted.
|
121 |
|
|
# This block should be called after all register data has been read.
|
122 |
|
|
def int_ports (self):
|
123 |
|
|
self.ports.append (port ('output','int_n'))
|
124 |
|
|
self.nets.append (net ('reg','int_n'))
|
125 |
67 |
ghutchis |
self.nets.append (net ('reg','int_vec',self.data_size))
|
126 |
64 |
ghutchis |
|
127 |
|
|
def int_logic (self):
|
128 |
|
|
statements = []
|
129 |
|
|
int_nets = []
|
130 |
|
|
for r in self.registers:
|
131 |
|
|
if r.interrupt: int_nets.append (r.name + "_int")
|
132 |
|
|
statements.append ("int_n = ~(" + string.join (int_nets, ' | ') + ");")
|
133 |
|
|
return comb_block (statements)
|
134 |
|
|
|
135 |
59 |
ghutchis |
def global_logic (self):
|
136 |
|
|
# create select pin for this block
|
137 |
|
|
statements = ["block_select = (addr[%d:%d] == %d) & !%s;" % (self.addr_size-1,self.local_width,self.base_addr >> self.local_width, self.req_pin)]
|
138 |
|
|
|
139 |
|
|
# create read and write selects for each register
|
140 |
|
|
for r in self.registers:
|
141 |
64 |
ghutchis |
slogic = "block_select & (addr[%d:%d] == %d) & !rd_n" % (self.local_width-1,0,r.offset)
|
142 |
67 |
ghutchis |
#if r.interrupt:
|
143 |
|
|
# slogic = "%s_int | (%s)" % (r.name, slogic)
|
144 |
64 |
ghutchis |
s = "%s_rd_sel = %s;" % (r.name,slogic)
|
145 |
59 |
ghutchis |
statements.append (s)
|
146 |
|
|
if r.write_cap():
|
147 |
|
|
s = "%s_wr_sel = block_select & (addr[%d:%d] == %d) & !wr_n;" % (r.name,self.local_width-1,0,r.offset)
|
148 |
|
|
statements.append (s)
|
149 |
|
|
|
150 |
|
|
return comb_block (statements)
|
151 |
|
|
|
152 |
|
|
def read_mux (self):
|
153 |
|
|
s = ''
|
154 |
64 |
ghutchis |
sments = []
|
155 |
|
|
rd_sel_list = []
|
156 |
|
|
# Old code for simple tri-state interface
|
157 |
|
|
#for r in self.registers:
|
158 |
|
|
# s += "assign rd_data = (%s_rd_sel) ? %s : %d'bz;\n" % (r.name, r.name, self.data_size)
|
159 |
|
|
|
160 |
67 |
ghutchis |
# create interrupt address mux
|
161 |
|
|
if (self.interrupts):
|
162 |
|
|
sments.append ("case (1'b1)")
|
163 |
|
|
for r in self.registers:
|
164 |
|
|
if r.interrupt:
|
165 |
|
|
sments.append (" %s_int : int_vec = %d;" % (r.name, r.int_value))
|
166 |
|
|
sments.append (" default : int_vec = %d'bx;" % self.data_size)
|
167 |
|
|
sments.append ("endcase")
|
168 |
|
|
|
169 |
|
|
# create data-output mux
|
170 |
64 |
ghutchis |
sments.append ("case (1'b1)")
|
171 |
59 |
ghutchis |
for r in self.registers:
|
172 |
64 |
ghutchis |
sments.append (" %s_rd_sel : rd_data = %s;" % (r.name, r.name))
|
173 |
|
|
rd_sel_list.append (r.name + "_rd_sel")
|
174 |
67 |
ghutchis |
if (self.interrupts):
|
175 |
|
|
sments.append (" default : rd_data = int_vec;")
|
176 |
|
|
else: sments.append (" default : rd_data = %d'bx;" % self.data_size)
|
177 |
64 |
ghutchis |
sments.append ("endcase")
|
178 |
59 |
ghutchis |
|
179 |
64 |
ghutchis |
sments.append ("doe = %s;" % string.join (rd_sel_list, ' | '))
|
180 |
|
|
|
181 |
|
|
return comb_block (sments)
|
182 |
59 |
ghutchis |
|
183 |
|
|
|
184 |
|
|
def verilog (self):
|
185 |
64 |
ghutchis |
self.post()
|
186 |
|
|
|
187 |
59 |
ghutchis |
result = 'module ' + self.name + ' (\n'
|
188 |
|
|
result += string.join (map (lambda x: x.name, self.ports), ',')
|
189 |
|
|
result += ');\n'
|
190 |
|
|
|
191 |
|
|
# print port list
|
192 |
|
|
for p in self.ports:
|
193 |
|
|
result += p.declaration() + '\n'
|
194 |
|
|
|
195 |
|
|
# print net list
|
196 |
|
|
for n in self.nets:
|
197 |
|
|
result += n.declaration() + '\n'
|
198 |
|
|
|
199 |
|
|
# create global logic
|
200 |
|
|
result += self.global_logic()
|
201 |
|
|
result += self.read_mux()
|
202 |
64 |
ghutchis |
if (self.interrupts > 0): result += self.int_logic()
|
203 |
59 |
ghutchis |
|
204 |
|
|
# print function blocks
|
205 |
|
|
for r in self.registers:
|
206 |
|
|
result += r.verilog_body()
|
207 |
|
|
|
208 |
64 |
ghutchis |
result += 'endmodule\n'
|
209 |
59 |
ghutchis |
return result
|
210 |
|
|
|
211 |
64 |
ghutchis |
def add_register (self, type, params):
|
212 |
|
|
#def add_register (self, name, type, width):
|
213 |
59 |
ghutchis |
if (type == 'status'):
|
214 |
64 |
ghutchis |
self.add (status_reg (params['name'],params['width']))
|
215 |
59 |
ghutchis |
elif (type == 'config'):
|
216 |
64 |
ghutchis |
self.add (config_reg (params['name'],params['width'],params['default']))
|
217 |
|
|
elif (type == 'int_fixed'):
|
218 |
|
|
r2 = config_reg (params['name'] + "_msk",params['width'],params['default'])
|
219 |
67 |
ghutchis |
r1 = int_fixed_reg (params['name'],r2,number(params['int_value']),params['width'])
|
220 |
64 |
ghutchis |
self.add (r1)
|
221 |
|
|
self.add (r2)
|
222 |
|
|
self.interrupts += 1
|
223 |
|
|
elif (type == 'soft_set'):
|
224 |
|
|
self.add (soft_set_reg(params['name'],params['width'],params['default']))
|
225 |
|
|
elif (type == 'read_stb'):
|
226 |
|
|
self.add (read_stb_reg (params['name'],params['width']))
|
227 |
|
|
elif (type == 'write_stb'):
|
228 |
67 |
ghutchis |
self.add (write_stb_reg (params['name'],params['width'],params['default']))
|
229 |
59 |
ghutchis |
else:
|
230 |
|
|
print "Unknown register type",type
|
231 |
64 |
ghutchis |
|
232 |
59 |
ghutchis |
def add (self, reg):
|
233 |
|
|
self.registers.append (reg)
|
234 |
|
|
self.ports.extend (reg.io())
|
235 |
|
|
self.nets.extend (reg.nets())
|
236 |
|
|
self.local_width = int(math.ceil (log2 (len (self.registers))))
|
237 |
|
|
rnum = 0
|
238 |
|
|
for r in self.registers:
|
239 |
|
|
r.offset = rnum
|
240 |
|
|
rnum += 1
|
241 |
|
|
|
242 |
|
|
class basic_register:
|
243 |
|
|
def __init__ (self, name='', width=0):
|
244 |
|
|
self.offset = 0
|
245 |
|
|
self.width = width
|
246 |
|
|
self.name = name
|
247 |
64 |
ghutchis |
self.interrupt = 0
|
248 |
59 |
ghutchis |
|
249 |
|
|
def verilog_body (self):
|
250 |
|
|
pass
|
251 |
|
|
|
252 |
|
|
def io (self):
|
253 |
|
|
return []
|
254 |
|
|
|
255 |
|
|
def nets (self):
|
256 |
|
|
return []
|
257 |
|
|
|
258 |
|
|
def write_cap (self):
|
259 |
|
|
return 0
|
260 |
|
|
|
261 |
64 |
ghutchis |
def id_comment (self):
|
262 |
|
|
return "// register: %s\n" % self.name
|
263 |
|
|
|
264 |
59 |
ghutchis |
class status_reg (basic_register):
|
265 |
|
|
def __init__ (self, name='', width=0):
|
266 |
|
|
basic_register.__init__(self, name, width)
|
267 |
|
|
|
268 |
|
|
def verilog_body (self):
|
269 |
64 |
ghutchis |
return ''
|
270 |
59 |
ghutchis |
|
271 |
|
|
def io (self):
|
272 |
64 |
ghutchis |
return [port('input', self.name, self.width)]
|
273 |
59 |
ghutchis |
|
274 |
|
|
def nets (self):
|
275 |
64 |
ghutchis |
return [ net('reg', self.name + '_rd_sel')]
|
276 |
59 |
ghutchis |
|
277 |
|
|
class config_reg (basic_register):
|
278 |
64 |
ghutchis |
def __init__ (self, name='', width=0, default=0):
|
279 |
59 |
ghutchis |
basic_register.__init__(self, name, width)
|
280 |
64 |
ghutchis |
self.default = default
|
281 |
59 |
ghutchis |
|
282 |
|
|
def verilog_body (self):
|
283 |
|
|
statements = ["if (reset) %s <= %d;" % (self.name, self.default),
|
284 |
64 |
ghutchis |
"else if (%s_wr_sel) %s <= %s;" % (self.name, self.name, 'wr_data')
|
285 |
59 |
ghutchis |
]
|
286 |
64 |
ghutchis |
return self.id_comment() + seq_block ('clk', statements)
|
287 |
59 |
ghutchis |
|
288 |
|
|
def io (self):
|
289 |
|
|
return [ port('output',self.name, self.width) ]
|
290 |
|
|
|
291 |
|
|
def nets (self):
|
292 |
64 |
ghutchis |
return [ net('reg', self.name, self.width),
|
293 |
|
|
net('reg', self.name + '_rd_sel'),
|
294 |
|
|
net('reg', self.name + '_wr_sel')]
|
295 |
59 |
ghutchis |
|
296 |
|
|
def write_cap (self):
|
297 |
|
|
return 1
|
298 |
64 |
ghutchis |
|
299 |
|
|
class int_fixed_reg (basic_register):
|
300 |
67 |
ghutchis |
def __init__ (self, name, mask_reg, int_value, width=0):
|
301 |
64 |
ghutchis |
basic_register.__init__(self, name, width)
|
302 |
|
|
self.mask_reg = mask_reg
|
303 |
|
|
self.interrupt = 1
|
304 |
67 |
ghutchis |
self.int_value = int_value
|
305 |
64 |
ghutchis |
|
306 |
|
|
def verilog_body (self):
|
307 |
|
|
statements = ["if (reset) %s <= %d;" % (self.name, 0),
|
308 |
|
|
"else %s <= (%s_set | %s) & ~( {%d{%s}} & %s);" %
|
309 |
|
|
(self.name, self.name, self.name, self.width, self.name + '_wr_sel', 'wr_data'),
|
310 |
|
|
"if (reset) %s_int <= 0;" % self.name,
|
311 |
|
|
"else %s_int <= |(%s & ~%s);" % (self.name, self.name, self.mask_reg.name)
|
312 |
|
|
]
|
313 |
|
|
return self.id_comment() + seq_block ('clk', statements)
|
314 |
|
|
|
315 |
|
|
def io (self):
|
316 |
|
|
return [ port('input',self.name+"_set", self.width) ]
|
317 |
|
|
|
318 |
|
|
def nets (self):
|
319 |
|
|
return [ net('reg', self.name + '_rd_sel'),
|
320 |
|
|
net('reg', self.name, self.width),
|
321 |
|
|
net('reg', self.name + '_wr_sel'),
|
322 |
|
|
net('reg', self.name + '_int')]
|
323 |
|
|
|
324 |
|
|
def write_cap (self):
|
325 |
|
|
return 1
|
326 |
|
|
|
327 |
|
|
class soft_set_reg (basic_register):
|
328 |
|
|
def __init__ (self, name='', width=0, default=0):
|
329 |
|
|
basic_register.__init__(self, name, width)
|
330 |
|
|
self.default = default
|
331 |
|
|
|
332 |
|
|
def verilog_body (self):
|
333 |
|
|
statements = ["if (reset) %s <= %d;" % (self.name, self.default),
|
334 |
|
|
"else %s <= ( ({%d{%s}} & %s) | %s) & ~(%s);" %
|
335 |
|
|
(self.name, self.width, self.name+'_wr_sel', 'wr_data',
|
336 |
|
|
self.name, self.name + '_clr')
|
337 |
|
|
]
|
338 |
|
|
return self.id_comment() + seq_block ('clk', statements)
|
339 |
|
|
|
340 |
|
|
def io (self):
|
341 |
|
|
return [ port('output',self.name, self.width),
|
342 |
|
|
port ('input',self.name+"_clr", self.width)]
|
343 |
|
|
|
344 |
|
|
def nets (self):
|
345 |
|
|
return [ net('reg', self.name, self.width),
|
346 |
|
|
net('reg', self.name + '_rd_sel'),
|
347 |
|
|
net('reg', self.name + '_wr_sel')]
|
348 |
|
|
|
349 |
|
|
def write_cap (self):
|
350 |
|
|
return 1
|
351 |
|
|
|
352 |
|
|
class write_stb_reg (config_reg):
|
353 |
|
|
def __init__ (self, name='', width=0, default=0):
|
354 |
|
|
config_reg.__init__(self, name, width, default)
|
355 |
|
|
|
356 |
|
|
def verilog_body (self):
|
357 |
|
|
statements = ["if (reset) %s <= %d;" % (self.name, self.default),
|
358 |
|
|
"else if (%s_wr_sel) %s <= %s;" % (self.name, self.name, 'wr_data'),
|
359 |
|
|
"if (reset) %s_stb <= 0;" % (self.name),
|
360 |
67 |
ghutchis |
"else if (%s_wr_sel) %s_stb <= 1;" % (self.name, self.name),
|
361 |
64 |
ghutchis |
"else %s_stb <= 0;" % (self.name)
|
362 |
|
|
]
|
363 |
|
|
return seq_block ('clk', statements)
|
364 |
|
|
|
365 |
|
|
def io (self):
|
366 |
|
|
io_list = config_reg.io (self)
|
367 |
|
|
io_list.append ( port('output',self.name+"_stb") )
|
368 |
|
|
return io_list
|
369 |
|
|
|
370 |
|
|
def nets (self):
|
371 |
|
|
net_list = config_reg.nets (self)
|
372 |
|
|
net_list.append ( net('reg', self.name + "_stb") )
|
373 |
|
|
return net_list
|
374 |
|
|
|
375 |
|
|
class read_stb_reg (status_reg):
|
376 |
|
|
def __init__ (self, name='', width=0):
|
377 |
|
|
status_reg.__init__(self, name, width)
|
378 |
|
|
|
379 |
|
|
def verilog_body (self):
|
380 |
|
|
statements = [
|
381 |
|
|
"if (reset) %s_stb <= 0;" % (self.name),
|
382 |
|
|
"else if (%s_rd_sel) %s_stb <= 1;" % (self.name, self.name),
|
383 |
|
|
"else %s_stb <= 0;" % (self.name)
|
384 |
|
|
]
|
385 |
|
|
return self.id_comment() + seq_block ('clk', statements)
|
386 |
|
|
|
387 |
|
|
def io (self):
|
388 |
|
|
io_list = status_reg.io (self)
|
389 |
|
|
io_list.append (port('output',self.name+"_stb"))
|
390 |
|
|
return io_list
|
391 |
|
|
|
392 |
|
|
def nets (self):
|
393 |
|
|
net_list = status_reg.nets(self)
|
394 |
|
|
net_list.append (net('reg',self.name + '_stb'))
|
395 |
|
|
return net_list
|