Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 0da89bc

Browse files
committed
Merge branch 'schwarz-MVB_HASH_TABLE_SIMPLE_cocotb_Logic_Array' into 'main'
Nahrazení vlastní utility binary utils za LogicArray z cocotb.types v konfiguračním skriptu pro MVB_HASH_TABLE_SIMPLE a oprava stylu pomocí flake8 See merge request ndk/ofm!408
2 parents d53d045 + 20d946b commit 0da89bc

File tree

5 files changed

+88
-97
lines changed

5 files changed

+88
-97
lines changed

comp/mvb_tools/storage/mvb_hash_table_simple/cocotb/cocotb_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ async def reset(self) -> None:
139139

140140
@cocotb.test()
141141
async def run_test(dut, config_file: str = "test_configs/test_config_1B.yaml", config_method: str = "script", pkt_count: int = 10000):
142-
#Function that runs the cocotb test
143-
144-
#Args:
145-
# dut: dut
146-
# config_file: file from which is to be loaded the configuration to be uploaded into the component.
147-
# config_method: how is the component to be configured. There are two options: directly from the file ('file'),
148-
# or via configuration script to which is the file passed ('script').
149-
# pkt_count: how many random packets are to be generated.
142+
# Function that runs the cocotb test
143+
144+
# Args:
145+
# dut: dut
146+
# config_file: file from which is to be loaded the configuration to be uploaded into the component.
147+
# config_method: how is the component to be configured. There are two options: directly from the file ('file'),
148+
# or via configuration script to which is the file passed ('script').
149+
# pkt_count: how many random packets are to be generated.
150150

151151
cocotb.start_soon(Clock(dut.CLK, 5, units='ns').start())
152152
tb = testbench(dut, debug=True)

comp/mvb_tools/storage/mvb_hash_table_simple/cocotb/drivers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# drivers.py: Specialzed MVBDriver for MVB_HASH_TABLE_SIMPLE
2+
# Copyright (C) 2024 CESNET z. s. p. o.
3+
# Author(s): Ondřej Schwarz <[email protected]>
4+
#
5+
# SPDX-License-Identifier: BSD-3-Clause
6+
17
from cocotbext.ofm.mvb.drivers import MVBDriver
28

39

comp/mvb_tools/storage/mvb_hash_table_simple/cocotb/monitors.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# monitors.py: Specialzed MVBMonitor for MVB_HASH_TABLE_SIMPLE
2+
# Copyright (C) 2024 CESNET z. s. p. o.
3+
# Author(s): Ondřej Schwarz <[email protected]>
4+
#
5+
# SPDX-License-Identifier: BSD-3-Clause
6+
17
from cocotbext.ofm.mvb.monitors import MVBMonitor
28

39

@@ -11,6 +17,6 @@ def receive_data(self, data, offset):
1117
self.log.debug(f"MATCH: {match_val}")
1218

1319
if match_val[offset] == 1:
14-
self._recv((data[offset*self._item_width:(offset+1)*self._item_width], 1)) # k match signalu pridat offset
20+
self._recv((data[offset*self._item_width:(offset+1)*self._item_width], 1))
1521
else:
1622
self._recv((self._item_width * b'\x00', 0))

comp/mvb_tools/storage/mvb_hash_table_simple/sw/toolkit.py

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"""
2424

2525
import nfb
26+
from cocotb.types import LogicArray, Range
2627
from cocotbext.ofm.utils.math import ceildiv
27-
from cocotbext.ofm.utils.binary_utils import int_to_bits, xor_bits, bits_to_int
2828
import colorama
2929
import sys
3030
from math import log2
@@ -72,7 +72,7 @@ class MVB_HASH_TABLE_SIMPLE_TOOLKIT(nfb.BaseComp):
7272
def __init__(self, inter=False, mod_path="", **kwargs) -> None:
7373
self._name = "MVB_HASH_TABLE_SIMPLE"
7474

75-
self.hash_key = 2534237992 # 10884469298454947624
75+
self.hash_key = 2534237992 # 10884469298454947624
7676

7777
"""Setting defaults to component configuration parametres."""
7878
self.conected_to_comp = False
@@ -83,7 +83,12 @@ def __init__(self, inter=False, mod_path="", **kwargs) -> None:
8383
self.hash_key_width = 32
8484
self.num_of_tables = 2
8585

86-
self.hash_func_params = {"hash_key": self.hash_key, "mvb_key_width": self.mvb_key_width, "hash_key_width": self.hash_key_width, "hash_width": self.hash_width}
86+
self.hash_func_params = {
87+
"hash_key": self.hash_key,
88+
"mvb_key_width": self.mvb_key_width,
89+
"hash_key_width": self.hash_key_width,
90+
"hash_width": self.hash_width
91+
}
8792

8893
"""Setting script parametres."""
8994
self.t_keys = list()
@@ -96,7 +101,21 @@ def __init__(self, inter=False, mod_path="", **kwargs) -> None:
96101
self.x_used = 0
97102
self.x_params = ["SIMPLE_XOR", simple_xor_hash, self.x_keys, self.x_hash_table, self.x_used]
98103

99-
self.commands = {"add": self.comm_add, "replace": self.comm_replace, "remove": self.comm_remove, "clear": self.comm_clear, "list": self.comm_list, "commit": self.comm_commit, "save": self.comm_save, "load": self.comm_load, "hash": self.comm_hash, "testkey": self.comm_testkey, "comparehashes": self.comm_comparehashes, "hwconfig": self.comm_hwconfig, "help": self.comm_help}
104+
self.commands = {
105+
"add": self.comm_add,
106+
"replace": self.comm_replace,
107+
"remove": self.comm_remove,
108+
"clear": self.comm_clear,
109+
"list": self.comm_list,
110+
"commit": self.comm_commit,
111+
"save": self.comm_save,
112+
"load": self.comm_load,
113+
"hash": self.comm_hash,
114+
"testkey": self.comm_testkey,
115+
"comparehashes": self.comm_comparehashes,
116+
"hwconfig": self.comm_hwconfig,
117+
"help": self.comm_help
118+
}
100119

101120
"""Setting up servicer."""
102121
try:
@@ -152,13 +171,24 @@ def get_hw_config(self) -> None:
152171
self.hash_key_width = self._comp.read32(self._HASH_KEY_WIDTH)
153172
self.table_capacity = self._comp.read32(self._TABLE_CAPACITY)
154173

155-
self.hash_func_params = {"hash_key": self.hash_key, "mvb_key_width": self.mvb_key_width, "hash_key_width": self.hash_key_width, "hash_width": self.hash_width}
174+
self.hash_func_params = {
175+
"hash_key": self.hash_key,
176+
"mvb_key_width": self.mvb_key_width,
177+
"hash_key_width": self.hash_key_width,
178+
"hash_width": self.hash_width
179+
}
156180

157-
self.hash_func_params = {"hash_key": self.hash_key, "mvb_key_width": self.mvb_key_width, "hash_key_width": self.hash_key_width, "hash_width": self.hash_width}
181+
self.hash_func_params = {
182+
"hash_key": self.hash_key,
183+
"mvb_key_width": self.mvb_key_width,
184+
"hash_key_width": self.hash_key_width,
185+
"hash_width": self.hash_width
186+
}
158187

159188
def test_key(self, hash_function) -> int:
160-
"""Tests the requested hash function for internal collisions caused by the key, where collision is a situation, where same hash is calcuted for one or more
161-
different keys from the whole range of the key's values. If lot of collisions occur, it's best to consider using a different hash key.
189+
"""Tests the requested hash function for internal collisions caused by the key, where collision is a situation,
190+
where same hash is calcuted for one or more different keys from the whole range of the key's values. If lot of
191+
collisions occur, it's best to consider using a different hash key.
162192
163193
Args:
164194
hash_funtion: reference to a function used for calculating the hash (in other words, the hash function function :D).
@@ -182,7 +212,8 @@ def test_key(self, hash_function) -> int:
182212
return collisions
183213

184214
def test_collisions(self, hash_function1, hash_function2) -> list:
185-
"""Tests collisions between two different hash functions, where collision is a situation, where for the same key both hash functions calculate the same hash.
215+
"""Tests collisions between two different hash functions, where collision is a situation, where for the same key both
216+
hash functions calculate the same hash.
186217
187218
Args:
188219
hash_function1: reference to a function used for calculating the hash (in other words, the hash function function :D).
@@ -201,7 +232,8 @@ def test_collisions(self, hash_function1, hash_function2) -> list:
201232
return collisions
202233

203234
def command_line(self) -> None:
204-
"""Main interface of the interactive mode used to input commands. Runs until the 'exit' or 'quit' commands. All the command line commands are prefixed with 'comm'."""
235+
"""Main interface of the interactive mode used to input commands. Runs until the 'exit' or 'quit' commands.
236+
All the command line commands are prefixed with 'comm'."""
205237

206238
print(f"{colorama.Fore.BLUE + colorama.Style.BRIGHT}MVB HASH TABLE SIMPLE TOOLKIT, version 0.1\nCopyright (C) 2024 CESNET z. s. p. o.\nInput command 'help' for more info.\n{colorama.Style.RESET_ALL}")
207239

@@ -225,9 +257,11 @@ def comm_add(self, key: int = None, data: int = None, table: str = "toeplitz", r
225257
"""Adds a value to a chosen table, the position of the value is decided by the hash of the key.
226258
227259
Args:
228-
key: mvb key, to which is the added value to be connected. The hash that decided position of the value in table is calculated from this number.
260+
key: mvb key, to which is the added value to be connected. The hash that decided position of the value in table
261+
is calculated from this number.
229262
data: value to be added to the table.
230-
table: decided to which table is the value to be added to. Toeplitz table is chosen by default, other option is 'xor' for adding to the simple xor table.
263+
table: decided to which table is the value to be added to. Toeplitz table is chosen by default, other option is
264+
'xor' for adding to the simple xor table.
231265
232266
"""
233267
if key is None or data is None:
@@ -408,7 +442,6 @@ def comm_remove(self, mode: str = None, table: str = None, num: int = None, sile
408442
print(f"{colorama.Fore.RED}Error:{colorama.Style.RESET_ALL} Invalid table. Possible tables are both, toeplitz, xor.")
409443
return
410444

411-
# name = params[0]
412445
hash_function, keys, hash_table, used = params[1:5]
413446

414447
if mode == "record":
@@ -432,8 +465,8 @@ def comm_remove(self, mode: str = None, table: str = None, num: int = None, sile
432465
hash_table[num][0] = False
433466

434467
for i in range(used):
435-
#print(f"DEBUG: self.t_keys: {self.t_keys}")
436-
#print(f"DEBUG: i: {i}, self.t_keys[{i}] = {self.t_keys[i]}")
468+
# print(f"DEBUG: self.t_keys: {self.t_keys}")
469+
# print(f"DEBUG: i: {i}, self.t_keys[{i}] = {self.t_keys[i]}")
437470
if num == hash_function(keys[i], self.hash_func_params):
438471
del keys[i]
439472
break
@@ -656,7 +689,12 @@ def comm_load(self, path: str = None, silent: bool = False) -> None:
656689
self.hash_key_width = comp_conf["hash_key_width"]
657690
self.num_of_tables = comp_conf["num_of_tables"]
658691

659-
self.hash_func_params = {"hash_key": self.hash_key, "mvb_key_width": self.mvb_key_width, "hash_key_width": self.hash_key_width, "hash_width": self.hash_width}
692+
self.hash_func_params = {
693+
"hash_key": self.hash_key,
694+
"mvb_key_width": self.mvb_key_width,
695+
"hash_key_width": self.hash_key_width,
696+
"hash_width": self.hash_width
697+
}
660698

661699
for i in range(self.num_of_tables):
662700
clear_table = "toeplitz" if i == 0 else "xor"
@@ -766,20 +804,20 @@ def toeplitz_hash(mvb_key: int, params: dict) -> int:
766804
hash_key_width = params["hash_key_width"]
767805
hash_width = params["hash_width"]
768806

769-
hash_key_bits = int_to_bits(hash_key, hash_key_width)
770-
mvb_key_bits = int_to_bits(mvb_key, mvb_key_width)
771-
hash_bits = hash_width * [0]
807+
hash_key_bits = LogicArray(hash_key, Range(hash_key_width-1, 'downto', 0))
808+
mvb_key_bits = LogicArray(mvb_key, Range(mvb_key_width-1, 'downto', 0))
809+
hash_bits = LogicArray(0, Range(hash_width-1, 'downto', 0))
772810

773-
for i in range(mvb_key_width):
774-
key_slice = hash_key_bits[i:i+hash_width]
775-
key_hash = hash_width * [0]
811+
for i in Range(mvb_key_width-1, 'downto', 0):
812+
key_slice = hash_key_bits[hash_key_width-(mvb_key_width-1-i)-1 : hash_key_width-hash_width-(mvb_key_width-1-i)]
813+
key_hash = LogicArray(0, Range(hash_width-1, 'downto', 0))
776814

777-
if mvb_key_bits[i] == 1:
815+
if mvb_key_bits[i]:
778816
key_hash = key_slice
779817

780-
hash_bits = xor_bits(hash_bits, key_hash, hash_width)
818+
hash_bits = hash_bits ^ key_hash
781819

782-
return bits_to_int(hash_bits, len(hash_bits))
820+
return hash_bits.integer
783821

784822

785823
def simple_xor_hash(mvb_key: int, params: dict) -> int:
@@ -799,12 +837,12 @@ def simple_xor_hash(mvb_key: int, params: dict) -> int:
799837
hash_key_width = params["hash_key_width"]
800838
hash_width = params["hash_width"]
801839

802-
hash_key_bits = int_to_bits(hash_key, hash_key_width)
803-
mvb_key_bits = int_to_bits(mvb_key, mvb_key_width)
840+
hash_key_bits = LogicArray(hash_key, Range(hash_key_width, 'downto', 0))
841+
mvb_key_bits = LogicArray(mvb_key, Range(mvb_key_width, 'downto', 0))
804842

805-
hash_bits = xor_bits(mvb_key_bits[::-1], hash_key_bits[::-1], hash_width)
843+
hash_bits = mvb_key_bits[hash_width-1 : 0] ^ hash_key_bits[hash_width-1 : 0]
806844

807-
return bits_to_int(hash_bits[::-1], hash_width)
845+
return hash_bits.integer
808846

809847

810848
def main() -> None:

python/cocotbext/cocotbext/ofm/utils/binary_utils.py

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)