-
Notifications
You must be signed in to change notification settings - Fork 89
Add more tests for MemBlock and _MemIndexed #461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,6 +102,45 @@ def test_memblock_added_default_named(self): | |
| self.assertIs(pyrtl.working_block().get_memblock_by_name(mem.name), mem) | ||
|
|
||
|
|
||
| class RTLMemBlockErrorTests(unittest.TestCase): | ||
| def setUp(self): | ||
| pyrtl.reset_working_block() | ||
|
|
||
| def test_negative_bitwidth(self): | ||
| with self.assertRaises(pyrtl.PyrtlError): | ||
| pyrtl.MemBlock(-1, 1) | ||
|
|
||
| def test_negative_addrwidth(self): | ||
| with self.assertRaises(pyrtl.PyrtlError): | ||
| pyrtl.MemBlock(1, -1) | ||
|
|
||
| def test_memindex_bitwidth_more_than_addrwidth(self): | ||
|
||
| mem = pyrtl.MemBlock(1, 1) | ||
| mem_in = pyrtl.Input(2, 'mem_in') | ||
|
||
| mem_out = pyrtl.Output(1, 'mem_out') | ||
| with self.assertRaises(pyrtl.PyrtlError): | ||
| mem_out <<= mem[mem_in] | ||
|
|
||
| def test_memblock_write_data_larger_than_memory_bidwidth(self): | ||
|
||
| mem = pyrtl.MemBlock(1, 1) | ||
| mem_addr = pyrtl.Input(1, 'mem_addr') | ||
| mem_in = pyrtl.Input(2, 'mem_in') | ||
| with self.assertRaises(pyrtl.PyrtlError): | ||
| mem[mem_addr] <<= mem_in | ||
|
|
||
| def test_memblock_enable_signal_not_1_bit(self): | ||
| mem = pyrtl.MemBlock(1, 1) | ||
| mem_addr = pyrtl.Input(1, 'mem_addr') | ||
| mem_in = pyrtl.Input(1, 'mem_in') | ||
| with self.assertRaises(pyrtl.PyrtlError): | ||
| mem[mem_addr] <<= pyrtl.MemBlock.EnabledWrite(mem_in, enable=pyrtl.Input(2)) | ||
|
|
||
| def test_read_ports_exception(self): | ||
| mem = pyrtl.MemBlock(1, 1) | ||
| with self.assertRaises(pyrtl.PyrtlError): | ||
| mem.read_ports() | ||
|
|
||
|
|
||
| class MemIndexedTests(unittest.TestCase): | ||
| def setUp(self): | ||
| pyrtl.reset_working_block() | ||
|
|
@@ -229,6 +268,53 @@ def test_write_memindexed_ior(self): | |
| self.assertEqual(self.mem1.num_read_ports, 1) | ||
| self.assertEqual(self.mem2.num_write_ports, 1) | ||
|
|
||
| def test_memindexed_len(self): | ||
| self.mem = pyrtl.MemBlock(8, 1) | ||
| self.assertEqual(len(self.mem[0]), 8) | ||
| self.mem_2 = pyrtl.MemBlock(16, 1) | ||
| self.assertEqual(len(self.mem_2[0]), 16) | ||
|
|
||
| def test_memindexed_getitem(self): | ||
| mem = pyrtl.MemBlock(bitwidth=8, addrwidth=1, max_read_ports=None) | ||
| mem_in = pyrtl.Input(1, 'mem_in') | ||
| mem_out_array = [pyrtl.Output(8, 'mem_out_' + str(i)) for i in range(8)] | ||
| for i in range(8): | ||
| mem_out_array[i] <<= mem[mem_in][i] | ||
| mem_value_map = {mem: {0: 7, 1: 5}} | ||
| sim_trace = pyrtl.SimulationTrace() | ||
|
||
| sim = pyrtl.Simulation(tracer=sim_trace, memory_value_map=mem_value_map) | ||
| for i in range(len(mem_value_map[mem])): | ||
|
||
| sim.step({mem_in: i}) | ||
| binary = bin(mem_value_map[mem][i])[2:].zfill(8) | ||
| for j in range(8): | ||
| self.assertEqual(sim.inspect(mem_out_array[j]), int(binary[7 - j])) | ||
|
||
|
|
||
| def test_memindexed_sign_extended(self): | ||
| mem = pyrtl.MemBlock(bitwidth=8, addrwidth=1) | ||
| mem_in = pyrtl.Input(1, 'mem_in') | ||
| mem_out = pyrtl.Output(16, 'mem_out') | ||
| mem_out <<= mem[mem_in].sign_extended(16) | ||
| mem_value_map = {mem: {0: 0b00101101, 1: 0b10011011}} | ||
| mem_value_map_sign_extended = [0b0000000000101101, 0b1111111110011011] | ||
| sim_trace = pyrtl.SimulationTrace() | ||
| sim = pyrtl.Simulation(tracer=sim_trace, memory_value_map=mem_value_map) | ||
| for i in range(len(mem_value_map[mem])): | ||
| sim.step({mem_in: i}) | ||
| self.assertEqual(sim.inspect(mem_out), mem_value_map_sign_extended[i]) | ||
|
|
||
| def test_memindexed_zero_extended(self): | ||
| mem = pyrtl.MemBlock(bitwidth=8, addrwidth=1) | ||
| mem_in = pyrtl.Input(1, 'mem_in') | ||
| mem_out = pyrtl.Output(16, 'mem_out') | ||
| mem_out <<= mem[mem_in].zero_extended(16) | ||
| mem_value_map = {mem: {0: 0b00101101, 1: 0b10011011}} | ||
| mem_value_map_zero_extended = [0b0000000000101101, 0b0000000010011011] | ||
| sim_trace = pyrtl.SimulationTrace() | ||
| sim = pyrtl.Simulation(tracer=sim_trace, memory_value_map=mem_value_map) | ||
| for i in range(len(mem_value_map[mem])): | ||
| sim.step({mem_in: i}) | ||
| self.assertEqual(sim.inspect(mem_out), mem_value_map_zero_extended[i]) | ||
|
|
||
|
|
||
| class RTLRomBlockWiring(unittest.TestCase): | ||
| data = list(range(2**5)) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines like these are a little easier to read with keyword arguments (
pyrtl.MemBlock(bitwidth=-1, addrwidth=1)) as the reader may not remember whetherbitwidthoraddrwidthcomes first.If you agree and want to change this, please change it consistently throughout this commit