Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if net.op in '<>=' and net.dests[0].bitwidth != 1:
raise PyrtlInternalError('error, destination should be of bitwidth=1')
if net.op in '+-' and net.dests[0].bitwidth > net.args[0].bitwidth + 1:
raise PyrtlInternalError('error, upper bits of destination unassigned')
if net.op == '*' and net.dests[0].bitwidth > 2 * net.args[0].bitwidth:
raise PyrtlInternalError('error, upper bits of destination unassigned')
if net.op == 'x' and net.dests[0].bitwidth > net.args[1].bitwidth:
raise PyrtlInternalError('error, upper bits of mux output undefined')
if net.op == 'c' and net.dests[0].bitwidth > sum(x.bitwidth for x in net.args):
raise PyrtlInternalError('error, upper bits of concat output undefined')
if net.op == 's' and net.dests[0].bitwidth > len(net.op_param):
raise PyrtlInternalError('error, upper bits of select output undefined')
if net.op == 'm' and net.dests[0].bitwidth != net.op_param[1].bitwidth:
raise PyrtlInternalError('error, mem read dest bitwidth mismatch')
if net.op == '@' and net.dests != ():
raise PyrtlInternalError('error, mem write dest should be empty tuple')
for w in net.dests:
if isinstance(w, (Input, Const)):
raise PyrtlInternalError('error, Inputs, Consts cannot be destinations to a net')
for w in net.args:
if isinstance(w, Output):
raise PyrtlInternalError('error, Outputs cannot be arguments for a net')
if net.op not in self.legal_ops:
raise PyrtlInternalError('error, net op "%s" not from acceptable set %s' %
(net.op, self.legal_ops))
# operation specific checks on arguments
if net.op in 'w~rsm' and len(net.args) != 1:
raise PyrtlInternalError('error, op only allowed 1 argument')
if net.op in '&|^n+-*<>=' and len(net.args) != 2:
raise PyrtlInternalError('error, op only allowed 2 arguments')
if net.op == 'x':
if len(net.args) != 3:
raise PyrtlInternalError('error, op only allowed 3 arguments')
if net.args[1].bitwidth != net.args[2].bitwidth:
raise PyrtlInternalError('error, args have mismatched bitwidths')
if net.args[0].bitwidth != 1:
raise PyrtlInternalError('error, mux select must be a single bit')
if net.op == '@' and len(net.args) != 3:
raise PyrtlInternalError('error, op only allowed 3 arguments')
if net.op in '&|^n+-*<>=' and net.args[0].bitwidth != net.args[1].bitwidth:
raise PyrtlInternalError('error, args have mismatched bitwidths')
if net.op in 'm@' and net.args[0].bitwidth != net.op_param[1].addrwidth:
raise PyrtlInternalError('error, mem addrwidth mismatch')
if net.op == '@' and net.args[1].bitwidth != net.op_param[1].bitwidth:
raise PyrtlInternalError('error, mem bitwidth mismatch')
if net.op == '@' and net.args[2].bitwidth != 1:
""" Removes all unconnected wires from a block"""
valid_wires = set()
for logic_net in block.logic:
valid_wires.update(logic_net.args, logic_net.dests)
wire_removal_set = block.wirevector_set.difference(valid_wires)
for removed_wire in wire_removal_set:
if isinstance(removed_wire, Input):
term = " optimized away"
if keep_inputs:
valid_wires.add(removed_wire)
term = " deemed useless by optimization"
print("Input Wire, " + removed_wire.name + " has been" + term)
if isinstance(removed_wire, Output):
PyrtlInternalError("Output wire, " + removed_wire.name + " not driven")
block.wirevector_set = valid_wires
raise PyrtlInternalError('error, op_param should be None')
if net.op == 's':
if not isinstance(net.op_param, tuple):
raise PyrtlInternalError('error, select op requires tuple op_param')
for p in net.op_param:
if not isinstance(p, int):
raise PyrtlInternalError('error, select op_param requires ints')
if p < 0 or p >= net.args[0].bitwidth:
raise PyrtlInternalError('error, op_param out of bounds')
if net.op in 'm@':
if not isinstance(net.op_param, tuple):
raise PyrtlInternalError('error, mem op requires tuple op_param')
if len(net.op_param) != 2:
raise PyrtlInternalError('error, mem op requires 2 op_params in tuple')
if not isinstance(net.op_param[0], int):
raise PyrtlInternalError('error, mem op requires first operand as int')
if not isinstance(net.op_param[1], _MemReadBase):
raise PyrtlInternalError('error, mem op requires second operand of a memory type')
# check destination validity
if net.op in 'w~&|^nr' and net.dests[0].bitwidth > net.args[0].bitwidth:
raise PyrtlInternalError('error, upper bits of destination unassigned')
if net.op in '<>=' and net.dests[0].bitwidth != 1:
raise PyrtlInternalError('error, destination should be of bitwidth=1')
if net.op in '+-' and net.dests[0].bitwidth > net.args[0].bitwidth + 1:
raise PyrtlInternalError('error, upper bits of destination unassigned')
if net.op == '*' and net.dests[0].bitwidth > 2 * net.args[0].bitwidth:
raise PyrtlInternalError('error, upper bits of destination unassigned')
if net.op == 'x' and net.dests[0].bitwidth > net.args[1].bitwidth:
raise PyrtlInternalError('error, upper bits of mux output undefined')
if net.op == 'c' and net.dests[0].bitwidth > sum(x.bitwidth for x in net.args):
raise PyrtlInternalError('error, upper bits of concat output undefined')
def _bits_ports_and_isrom_from_memory(mem):
""" Helper to extract mem bits and ports for estimation. """
is_rom = False
bits = 2**mem.addrwidth * mem.bitwidth
read_ports = len(mem.readport_nets)
try:
write_ports = len(mem.writeport_nets)
except AttributeError: # dealing with ROMs
if not isinstance(mem, RomBlock):
raise PyrtlInternalError('Mem with no writeport_nets attribute'
' but not a ROM? Thats an error')
write_ports = 0
is_rom = True
ports = max(read_ports, write_ports)
return bits, ports, is_rom
if net.op not in self.legal_ops:
raise PyrtlInternalError('error, net op "%s" not from acceptable set %s' %
(net.op, self.legal_ops))
# operation specific checks on arguments
if net.op in 'w~rsm' and len(net.args) != 1:
raise PyrtlInternalError('error, op only allowed 1 argument')
if net.op in '&|^n+-*<>=' and len(net.args) != 2:
raise PyrtlInternalError('error, op only allowed 2 arguments')
if net.op == 'x':
if len(net.args) != 3:
raise PyrtlInternalError('error, op only allowed 3 arguments')
if net.args[1].bitwidth != net.args[2].bitwidth:
raise PyrtlInternalError('error, args have mismatched bitwidths')
if net.args[0].bitwidth != 1:
raise PyrtlInternalError('error, mux select must be a single bit')
if net.op == '@' and len(net.args) != 3:
raise PyrtlInternalError('error, op only allowed 3 arguments')
if net.op in '&|^n+-*<>=' and net.args[0].bitwidth != net.args[1].bitwidth:
raise PyrtlInternalError('error, args have mismatched bitwidths')
if net.op in 'm@' and net.args[0].bitwidth != net.op_param[1].addrwidth:
raise PyrtlInternalError('error, mem addrwidth mismatch')
if net.op == '@' and net.args[1].bitwidth != net.op_param[1].bitwidth:
raise PyrtlInternalError('error, mem bitwidth mismatch')
if net.op == '@' and net.args[2].bitwidth != 1:
raise PyrtlInternalError('error, mem write enable must be 1 bit')
# operation specific checks on op_params
if net.op in 'w~&|^n+-*<>=xcr' and net.op_param is not None:
raise PyrtlInternalError('error, op_param should be None')
if net.op == 's':
if not isinstance(net.op_param, tuple):
raise PyrtlInternalError('error, op only allowed 1 argument')
if net.op in '&|^n+-*<>=' and len(net.args) != 2:
raise PyrtlInternalError('error, op only allowed 2 arguments')
if net.op == 'x':
if len(net.args) != 3:
raise PyrtlInternalError('error, op only allowed 3 arguments')
if net.args[1].bitwidth != net.args[2].bitwidth:
raise PyrtlInternalError('error, args have mismatched bitwidths')
if net.args[0].bitwidth != 1:
raise PyrtlInternalError('error, mux select must be a single bit')
if net.op == '@' and len(net.args) != 3:
raise PyrtlInternalError('error, op only allowed 3 arguments')
if net.op in '&|^n+-*<>=' and net.args[0].bitwidth != net.args[1].bitwidth:
raise PyrtlInternalError('error, args have mismatched bitwidths')
if net.op in 'm@' and net.args[0].bitwidth != net.op_param[1].addrwidth:
raise PyrtlInternalError('error, mem addrwidth mismatch')
if net.op == '@' and net.args[1].bitwidth != net.op_param[1].bitwidth:
raise PyrtlInternalError('error, mem bitwidth mismatch')
if net.op == '@' and net.args[2].bitwidth != 1:
raise PyrtlInternalError('error, mem write enable must be 1 bit')
# operation specific checks on op_params
if net.op in 'w~&|^n+-*<>=xcr' and net.op_param is not None:
raise PyrtlInternalError('error, op_param should be None')
if net.op == 's':
if not isinstance(net.op_param, tuple):
raise PyrtlInternalError('error, select op requires tuple op_param')
for p in net.op_param:
if not isinstance(p, int):
raise PyrtlInternalError('error, select op_param requires ints')
if p < 0 or p >= net.args[0].bitwidth:
raise PyrtlInternalError('error, op_param out of bounds')
def sanity_check_net(self, net):
""" Check that net is a valid LogicNet. """
from .wire import Input, Output, Const
from .memory import _MemReadBase
# general sanity checks that apply to all operations
if not isinstance(net, LogicNet):
raise PyrtlInternalError('error, net must be of type LogicNet')
if not isinstance(net.args, tuple):
raise PyrtlInternalError('error, LogicNet args must be tuple')
if not isinstance(net.dests, tuple):
raise PyrtlInternalError('error, LogicNet dests must be tuple')
for w in net.args + net.dests:
self.sanity_check_wirevector(w)
if w._block is not self:
raise PyrtlInternalError('error, net references different block')
if w not in self.wirevector_set:
raise PyrtlInternalError('error, net with unknown source "%s"' % w.name)
# checks that input and output wirevectors are not misused
for w in net.dests:
if isinstance(w, (Input, Const)):
raise PyrtlInternalError('error, Inputs, Consts cannot be destinations to a net')
for w in net.args:
if isinstance(w, Output):
raise PyrtlInternalError('error, Outputs cannot be arguments for a net')
for p, (addr, data, enable) in _predicate_map[lhs][1:]:
combined_enable = select(p, truecase=enable, falsecase=combined_enable)
combined_addr = select(p, truecase=addr, falsecase=combined_addr)
combined_data = select(p, truecase=data, falsecase=combined_data)
lhs._build(combined_addr, combined_data, combined_enable)
# handle wirevector and register assignments
else:
if isinstance(lhs, Register):
result = lhs # default for registers is "self"
elif isinstance(lhs, WireVector):
result = 0 # default for wire is "0"
else:
raise PyrtlInternalError('unknown assignment in finalize')
predlist = _predicate_map[lhs]
for p, rhs in predlist:
result = select(p, truecase=rhs, falsecase=result)
lhs._build(result)