|
3 | 3 | import os |
4 | 4 | import sys |
5 | 5 | import collections |
| 6 | +import copy |
6 | 7 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
7 | 8 |
|
8 | 9 | import vtypes |
@@ -30,6 +31,8 @@ def __init__(self, name=None): |
30 | 31 | self.items = [] |
31 | 32 | self.tmp_count = 0 |
32 | 33 |
|
| 34 | + #--------------------------------------------------------------------------- |
| 35 | + # User interface for variables |
33 | 36 | #--------------------------------------------------------------------------- |
34 | 37 | def Input(self, name, width=None, length=None, signed=False, value=None): |
35 | 38 | t = vtypes.Input(name, width, length, signed, value) |
@@ -130,6 +133,8 @@ def TmpLocalparam(self, value, width=None, signed=False, length=None): |
130 | 133 | self.tmp_count += 1 |
131 | 134 | return self.Localparam(name, value, width, signed, length) |
132 | 135 |
|
| 136 | + #--------------------------------------------------------------------------- |
| 137 | + # User interface for control statements |
133 | 138 | #--------------------------------------------------------------------------- |
134 | 139 | def Always(self, *sensitivity): |
135 | 140 | t = vtypes.Always(*sensitivity) |
@@ -200,6 +205,103 @@ def Instance(self, module, instname, params=None, ports=None): |
200 | 205 | self.submodule[module.name] = module |
201 | 206 | return t |
202 | 207 |
|
| 208 | + #--------------------------------------------------------------------------- |
| 209 | + # User intarface for reset assignments |
| 210 | + #--------------------------------------------------------------------------- |
| 211 | + def reset(self): |
| 212 | + ret = [] |
| 213 | + for vname, var in self.variable.items(): |
| 214 | + r = var.reset() |
| 215 | + if r: ret.append(r) |
| 216 | + return tuple(ret) |
| 217 | + |
| 218 | + #--------------------------------------------------------------------------- |
| 219 | + # User interface for accessing internal information |
| 220 | + #--------------------------------------------------------------------------- |
| 221 | + def get_params(self): |
| 222 | + return list(self.global_constant.values()) |
| 223 | + |
| 224 | + def get_localparams(self): |
| 225 | + return list(self.constant.values()) |
| 226 | + |
| 227 | + def get_ports(self): |
| 228 | + return list(self.io_variable.values()) |
| 229 | + |
| 230 | + def get_vars(self): |
| 231 | + return list(self.variable.values()) |
| 232 | + |
| 233 | + #--------------------------------------------------------------------------- |
| 234 | + def copy_params(self, src): |
| 235 | + ret = collections.OrderedDict() |
| 236 | + for key, obj in src.global_constant.items(): |
| 237 | + copy_obj = copy.deepcopy(obj) |
| 238 | + self.add_object( copy_obj ) |
| 239 | + ret[key] = copy_obj |
| 240 | + return ret |
| 241 | + |
| 242 | + def copy_localparams(self, src): |
| 243 | + ret = collections.OrderedDict() |
| 244 | + for key, obj in src.constant.items(): |
| 245 | + copy_obj = copy.deepcopy(obj) |
| 246 | + self.add_object( copy_obj ) |
| 247 | + ret[key] = copy_obj |
| 248 | + return ret |
| 249 | + |
| 250 | + def copy_ports(self, src): |
| 251 | + ret = collections.OrderedDict() |
| 252 | + for key, obj in src.io_variable.items(): |
| 253 | + copy_obj = copy.deepcopy(obj) |
| 254 | + self.add_object( copy_obj ) |
| 255 | + ret[key] = copy_obj |
| 256 | + return ret |
| 257 | + |
| 258 | + def copy_vars(self, src): |
| 259 | + ret = collections.OrderedDict() |
| 260 | + for key, obj in src.variable.items(): |
| 261 | + copy_obj = copy.deepcopy(obj) |
| 262 | + self.add_object( copy_obj ) |
| 263 | + ret[key] = copy_obj |
| 264 | + return ret |
| 265 | + |
| 266 | + def copy_sim_ports(self, src): |
| 267 | + ret = collections.OrderedDict() |
| 268 | + for key, obj in src.io_variable.items(): |
| 269 | + copy_obj = self.get_corresponding_variable(obj)(key, copy.deepcopy(obj.width)) |
| 270 | + self.add_object( copy_obj ) |
| 271 | + ret[key] = copy_obj |
| 272 | + return ret |
| 273 | + |
| 274 | + #--------------------------------------------------------------------------- |
| 275 | + def connect_params(self, targ): |
| 276 | + ret = [] |
| 277 | + for key, obj in targ.global_constant.items(): |
| 278 | + if (key not in self.global_constant) and (key not in self.constant): |
| 279 | + raise IndexError("No such constant in module %s" % self.name) |
| 280 | + if key in self.global_constant: |
| 281 | + ret.append( (key, self.global_constant[key]) ) |
| 282 | + elif key in self.constant: |
| 283 | + ret.append( (key, self.constant[key]) ) |
| 284 | + return tuple(ret) |
| 285 | + |
| 286 | + def connect_ports(self, targ): |
| 287 | + ret = [] |
| 288 | + for key, obj in targ.io_variable.items(): |
| 289 | + if (key not in self.io_variable) and (key not in self.variable): |
| 290 | + raise IndexError("No such IO in module %s" % self.name) |
| 291 | + if key in self.io_variable: |
| 292 | + ret.append( (key, self.io_variable[key]) ) |
| 293 | + elif key in self.variable: |
| 294 | + ret.append( (key, self.variable[key]) ) |
| 295 | + return tuple(ret) |
| 296 | + |
| 297 | + #--------------------------------------------------------------------------- |
| 298 | + # User interface for code generation |
| 299 | + #--------------------------------------------------------------------------- |
| 300 | + def to_verilog(self, filename=None): |
| 301 | + return to_verilog.write_verilog(self, filename) |
| 302 | + |
| 303 | + #--------------------------------------------------------------------------- |
| 304 | + # Internal methods |
203 | 305 | #--------------------------------------------------------------------------- |
204 | 306 | def add_object(self, obj): |
205 | 307 | self.items.append(obj) |
@@ -331,17 +433,16 @@ def is_wire(self, name): |
331 | 433 | return False |
332 | 434 |
|
333 | 435 | #--------------------------------------------------------------------------- |
334 | | - def to_verilog(self, filename=None): |
335 | | - return to_verilog.write_verilog(self, filename) |
| 436 | + def get_corresponding_variable(self, var, use_wire=False): |
| 437 | + if isinstance(var, vtypes.Input): |
| 438 | + if use_wire: return vtypes.Wire |
| 439 | + return vtypes.Reg |
| 440 | + if isinstance(var, vtypes.Output): |
| 441 | + return vtypes.Wire |
| 442 | + if isinstance(var, vtypes.Inout): |
| 443 | + return vtypes.Wire |
| 444 | + raise TypeError('No corresponding IO type for %s' % str(type(var))) |
336 | 445 |
|
337 | | - #--------------------------------------------------------------------------- |
338 | | - def reset(self): |
339 | | - ret = [] |
340 | | - for vname, var in self.variable.items(): |
341 | | - r = var.reset() |
342 | | - if r: ret.append(r) |
343 | | - return tuple(ret) |
344 | | - |
345 | 446 | #------------------------------------------------------------------------------- |
346 | 447 | class StubModule(vtypes.VeriloggenNode): |
347 | 448 | """ Verilog Module class """ |
|
0 commit comments