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

Commit

Permalink
Merge branch 'schwarz-MVB_HASH_TABLE_SIMPLE_cocotb_Logic_Array' into …
Browse files Browse the repository at this point in the history
…'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
  • Loading branch information
jakubcabal committed Sep 3, 2024
2 parents d53d045 + 20d946b commit 0da89bc
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ async def reset(self) -> None:

@cocotb.test()
async def run_test(dut, config_file: str = "test_configs/test_config_1B.yaml", config_method: str = "script", pkt_count: int = 10000):
#Function that runs the cocotb test

#Args:
# dut: dut
# config_file: file from which is to be loaded the configuration to be uploaded into the component.
# config_method: how is the component to be configured. There are two options: directly from the file ('file'),
# or via configuration script to which is the file passed ('script').
# pkt_count: how many random packets are to be generated.
# Function that runs the cocotb test

# Args:
# dut: dut
# config_file: file from which is to be loaded the configuration to be uploaded into the component.
# config_method: how is the component to be configured. There are two options: directly from the file ('file'),
# or via configuration script to which is the file passed ('script').
# pkt_count: how many random packets are to be generated.

cocotb.start_soon(Clock(dut.CLK, 5, units='ns').start())
tb = testbench(dut, debug=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# drivers.py: Specialzed MVBDriver for MVB_HASH_TABLE_SIMPLE
# Copyright (C) 2024 CESNET z. s. p. o.
# Author(s): Ondřej Schwarz <[email protected]>
#
# SPDX-License-Identifier: BSD-3-Clause

from cocotbext.ofm.mvb.drivers import MVBDriver


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# monitors.py: Specialzed MVBMonitor for MVB_HASH_TABLE_SIMPLE
# Copyright (C) 2024 CESNET z. s. p. o.
# Author(s): Ondřej Schwarz <[email protected]>
#
# SPDX-License-Identifier: BSD-3-Clause

from cocotbext.ofm.mvb.monitors import MVBMonitor


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

if match_val[offset] == 1:
self._recv((data[offset*self._item_width:(offset+1)*self._item_width], 1)) # k match signalu pridat offset
self._recv((data[offset*self._item_width:(offset+1)*self._item_width], 1))
else:
self._recv((self._item_width * b'\x00', 0))
96 changes: 67 additions & 29 deletions comp/mvb_tools/storage/mvb_hash_table_simple/sw/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"""

import nfb
from cocotb.types import LogicArray, Range
from cocotbext.ofm.utils.math import ceildiv
from cocotbext.ofm.utils.binary_utils import int_to_bits, xor_bits, bits_to_int
import colorama
import sys
from math import log2
Expand Down Expand Up @@ -72,7 +72,7 @@ class MVB_HASH_TABLE_SIMPLE_TOOLKIT(nfb.BaseComp):
def __init__(self, inter=False, mod_path="", **kwargs) -> None:
self._name = "MVB_HASH_TABLE_SIMPLE"

self.hash_key = 2534237992 # 10884469298454947624
self.hash_key = 2534237992 # 10884469298454947624

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

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}
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
}

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

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}
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
}

"""Setting up servicer."""
try:
Expand Down Expand Up @@ -152,13 +171,24 @@ def get_hw_config(self) -> None:
self.hash_key_width = self._comp.read32(self._HASH_KEY_WIDTH)
self.table_capacity = self._comp.read32(self._TABLE_CAPACITY)

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}
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
}

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}
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
}

def test_key(self, hash_function) -> int:
"""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
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.
"""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 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.
Args:
hash_funtion: reference to a function used for calculating the hash (in other words, the hash function function :D).
Expand All @@ -182,7 +212,8 @@ def test_key(self, hash_function) -> int:
return collisions

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

def command_line(self) -> None:
"""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'."""
"""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'."""

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}")

Expand All @@ -225,9 +257,11 @@ def comm_add(self, key: int = None, data: int = None, table: str = "toeplitz", r
"""Adds a value to a chosen table, the position of the value is decided by the hash of the key.
Args:
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.
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.
data: value to be added to the table.
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.
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.
"""
if key is None or data is None:
Expand Down Expand Up @@ -408,7 +442,6 @@ def comm_remove(self, mode: str = None, table: str = None, num: int = None, sile
print(f"{colorama.Fore.RED}Error:{colorama.Style.RESET_ALL} Invalid table. Possible tables are both, toeplitz, xor.")
return

# name = params[0]
hash_function, keys, hash_table, used = params[1:5]

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

for i in range(used):
#print(f"DEBUG: self.t_keys: {self.t_keys}")
#print(f"DEBUG: i: {i}, self.t_keys[{i}] = {self.t_keys[i]}")
# print(f"DEBUG: self.t_keys: {self.t_keys}")
# print(f"DEBUG: i: {i}, self.t_keys[{i}] = {self.t_keys[i]}")
if num == hash_function(keys[i], self.hash_func_params):
del keys[i]
break
Expand Down Expand Up @@ -656,7 +689,12 @@ def comm_load(self, path: str = None, silent: bool = False) -> None:
self.hash_key_width = comp_conf["hash_key_width"]
self.num_of_tables = comp_conf["num_of_tables"]

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}
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
}

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

hash_key_bits = int_to_bits(hash_key, hash_key_width)
mvb_key_bits = int_to_bits(mvb_key, mvb_key_width)
hash_bits = hash_width * [0]
hash_key_bits = LogicArray(hash_key, Range(hash_key_width-1, 'downto', 0))
mvb_key_bits = LogicArray(mvb_key, Range(mvb_key_width-1, 'downto', 0))
hash_bits = LogicArray(0, Range(hash_width-1, 'downto', 0))

for i in range(mvb_key_width):
key_slice = hash_key_bits[i:i+hash_width]
key_hash = hash_width * [0]
for i in Range(mvb_key_width-1, 'downto', 0):
key_slice = hash_key_bits[hash_key_width-(mvb_key_width-1-i)-1 : hash_key_width-hash_width-(mvb_key_width-1-i)]
key_hash = LogicArray(0, Range(hash_width-1, 'downto', 0))

if mvb_key_bits[i] == 1:
if mvb_key_bits[i]:
key_hash = key_slice

hash_bits = xor_bits(hash_bits, key_hash, hash_width)
hash_bits = hash_bits ^ key_hash

return bits_to_int(hash_bits, len(hash_bits))
return hash_bits.integer


def simple_xor_hash(mvb_key: int, params: dict) -> int:
Expand All @@ -799,12 +837,12 @@ def simple_xor_hash(mvb_key: int, params: dict) -> int:
hash_key_width = params["hash_key_width"]
hash_width = params["hash_width"]

hash_key_bits = int_to_bits(hash_key, hash_key_width)
mvb_key_bits = int_to_bits(mvb_key, mvb_key_width)
hash_key_bits = LogicArray(hash_key, Range(hash_key_width, 'downto', 0))
mvb_key_bits = LogicArray(mvb_key, Range(mvb_key_width, 'downto', 0))

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

return bits_to_int(hash_bits[::-1], hash_width)
return hash_bits.integer


def main() -> None:
Expand Down
59 changes: 0 additions & 59 deletions python/cocotbext/cocotbext/ofm/utils/binary_utils.py

This file was deleted.

0 comments on commit 0da89bc

Please sign in to comment.