Skip to content

Commit

Permalink
fix to issue #56 "Better error for with block misuse", commented-out …
Browse files Browse the repository at this point in the history
…test for #57
  • Loading branch information
timsherwood committed Mar 14, 2015
1 parent b4a891a commit 1d5730d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyrtl/helperfuncs.py
Expand Up @@ -13,7 +13,6 @@

import core
import wire
import memblock


# -----------------------------------------------------------------
Expand All @@ -31,6 +30,7 @@ def as_wires(val, bitwidth=None, truncating=True, block=None):
most operations in an attempt to coerce values into WireVectors (for example,
operations such as "x+1" where "1" needs to be converted to a Const WireVectors.)
"""
import memblock # import here to avoid cylic dependence memblock->wire->helper->memblock
block = core.working_block(block)

if isinstance(val, (int, basestring)):
Expand Down
7 changes: 7 additions & 0 deletions pyrtl/wire.py
Expand Up @@ -224,6 +224,13 @@ def __getitem__(self, item):
def __len__(self):
return self.bitwidth

def __enter__(self):
raise core.PyrtlError('error, WireVector cannot be used directly as a context. '
'(you might be missing a ConditionalUpdate in your "with" block?)')

def __exit__(self, type, value, tb):
pass # needed to allow the error raised in __enter__ to propagate up to the user

def sign_extended(self, bitwidth):
""" return a sign extended wirevector derived from self """
return self._extend_with_bit(bitwidth, self[-1])
Expand Down
22 changes: 22 additions & 0 deletions tests/test_conditional.py
Expand Up @@ -269,6 +269,28 @@ def test_condition_wire_shortcut(self):
o |= 1
self.check_trace('i 01230123\no 11101110\n')

def test_condition_nice_error_message_nested(self):
with self.assertRaises(pyrtl.PyrtlError):
i = pyrtl.Register(bitwidth=2, name='i')
o = pyrtl.WireVector(bitwidth=2, name='o')
i.next <<= i + 1
with pyrtl.ConditionalUpdate() as condition:
with condition(i<=2):
with condition(i==0):
o |= 2
with i==1:
o |= 1
with condition.default:
o |= 0

def test_condition_nice_error_message_shortcut(self):
with self.assertRaises(pyrtl.PyrtlError):
i = pyrtl.Register(bitwidth=2, name='i')
o = pyrtl.WireVector(bitwidth=2, name='o')
i.next <<= i + 1
with i<=2:
o |= 1


if __name__ == "__main__":
unittest.main()
15 changes: 13 additions & 2 deletions tests/test_memblock.py
Expand Up @@ -43,7 +43,18 @@ def test_memblock_direct_assignment_error(self):
memory = pyrtl.MemBlock(bitwidth=self.bitwidth, addrwidth=self.addrwidth, name='memory')
memory[self.mem_write_address] <<= 5

""" -- TEST for Error comparing Mem reads #57
def test_memblock_to_memblock_direct_operation(self):
memory = pyrtl.MemBlock(bitwidth=self.bitwidth, addrwidth=self.addrwidth, name='memory')
temp = (memory[self.mem_read_address1] == memory[self.mem_read_address2])
temp = (memory[self.mem_read_address1] != memory[self.mem_read_address2])
temp = (memory[self.mem_read_address1] & memory[self.mem_read_address2])
temp = (memory[self.mem_read_address1] | memory[self.mem_read_address2])
temp = (memory[self.mem_read_address1] + memory[self.mem_read_address2])
temp = (memory[self.mem_read_address1] - memory[self.mem_read_address2])
temp = (memory[self.mem_read_address1] * memory[self.mem_read_address2])
self.output1 <<= temp
"""

if __name__ == "__main__":
unittest.main()


0 comments on commit 1d5730d

Please sign in to comment.