Skip to content

Fixed random clock period generation #105

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions lambdalib/ramlib/tests/tb_la_asyncfifo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
import string
from decimal import Decimal

import siliconcompiler
Expand All @@ -7,7 +8,6 @@
from cocotb.clock import Clock
from cocotb.triggers import ClockCycles, Timer, Combine
from cocotb.regression import TestFactory
from cocotb import utils

from lambdalib import ramlib
from lambdalib.utils._tb_common import (
Expand All @@ -30,6 +30,12 @@ def bursty_en_gen(burst_len=20):
yield en_state


def random_decimal(max: int, min: int, decimal_places=2) -> Decimal:
prefix = str(random.randint(min, max))
suffix = ''.join(random.choice(string.digits) for _ in range(decimal_places))
return Decimal(prefix + "." + suffix)


@cocotb.test()
async def test_almost_full(dut):

Expand Down Expand Up @@ -115,7 +121,7 @@ async def fifo_rd_wr_test(

await cocotb.start(Clock(dut.wr_clk, wr_clk_period_ns, units="ns").start())
# Randomize phase shift between clocks
await Timer(wr_clk_period_ns * random.random(), "ns", round_mode="round")
await Timer(wr_clk_period_ns * Decimal(random.random()), "ns", round_mode="round")
await cocotb.start(Clock(dut.rd_clk, rd_clk_period_ns, units="ns").start())

await ClockCycles(dut.wr_clk, 3)
Expand All @@ -134,21 +140,13 @@ async def fifo_rd_wr_test(


# Generate sets of tests based on the different permutations of the possible arguments to fifo_test
MAX_PERIOD_NS = 10.0
MIN_PERIOD_NS = 1.0
MAX_PERIOD_NS = 10
MIN_PERIOD_NS = 1

# Generate random clk period to test between min and max
RAND_WR_CLK_PERIOD_NS, RAND_RD_CLK_PERIOD_NS = [utils.get_time_from_sim_steps(
# Time step must be even for cocotb clock driver
steps=utils.get_sim_steps(
time=Decimal(MIN_PERIOD_NS) + (
Decimal(MAX_PERIOD_NS - MIN_PERIOD_NS)
* Decimal(random.random()).quantize(Decimal("0.00"))
),
units="ns",
round_mode="round"
) & ~1,
units="ns"
) for _ in range(0, 2)]
RAND_WR_CLK_PERIOD_NS, RAND_RD_CLK_PERIOD_NS = [
random_decimal(MAX_PERIOD_NS, MIN_PERIOD_NS) for _ in range(2)
]

# Factory to automatically generate a set of tests based on the different permutations
# of the provided test arguments
Expand Down