Skip to content

Commit

Permalink
working rising/falling edge awaits
Browse files Browse the repository at this point in the history
  • Loading branch information
psychogenic committed Nov 23, 2024
1 parent e5c0057 commit 984ee1a
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 65 deletions.
44 changes: 35 additions & 9 deletions src/examples/tt_um_factory_test/tt_um_factory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@
import math
import gc
from ttboard.demoboard import DemoBoard
gc.collect()
print(f'mf tb: {gc.mem_free()}')
from ttboard.cocotb.clock import Clock
print(f'mf tb: {gc.mem_free()}')
from ttboard.cocotb.triggers import Timer, ClockCycles # RisingEdge, FallingEdge, Timer, ClockCycles

print(f'mf tb: {gc.mem_free()}')
from ttboard.cocotb.triggers import RisingEdge, FallingEdge, ClockCycles
import ttboard.cocotb as cocotb
from ttboard.cocotb.utils import get_sim_time

@cocotb.test()
async def test_loopback(dut):
Expand Down Expand Up @@ -59,18 +55,48 @@ async def test_counter(dut):

dut._log.info("Testing counter")
for i in range(256):
assert dut.uo_out.value == dut.uio_out.value, f"uio value unstable {dut.uio_out.value}?"
assert dut.uo_out.value == dut.uio_out.value, f"uo_out != uio_out"
assert int(dut.uo_out.value) == i, f"uio value not incremented correctly {dut.uio_out.value} != {i}"
await ClockCycles(dut.clk, 1)


dut._log.info("test_counter passed")

@cocotb.test()
async def test_edge_triggers(dut):
dut._log.info("Start")

clock = Clock(dut.clk, 10, units="us")
cocotb.start_soon(clock.start())
dut.uio_oe_pico.value = 0 # all inputs on our side

dut.ui_in.value = 0b1
dut.rst_n.value = 0
await ClockCycles(dut.clk, 10)
dut.rst_n.value = 1
await ClockCycles(dut.clk, 1)

dut._log.info(f"Testing counter, waiting on rising edge of bit 5 at {get_sim_time('us')}us")
await RisingEdge(dut.some_bit)
dut._log.info(f"Got rising edge, now {get_sim_time('us')}us value is {hex(dut.uo_out.value)}")

dut._log.info(f"Now await falling edge")
await FallingEdge(dut.some_bit)
dut._log.info(f"Got rising edge, now {get_sim_time('us')}us value is {hex(dut.uo_out.value)}")

dut._log.info("test_edge_triggers passed")


def main():
# import examples.tt_um_factory_test.tt_um_factory_test as ft
from ttboard.cocotb.dut import DUT

from ttboard.cocotb.dut import DUTWrapper

class DUT(DUTWrapper):
def __init__(self):
super().__init__()
self.tt = DemoBoard.get()
# inputs
self.some_bit = self.new_slice_attribute(self.tt.uo_out, 5)

tt = DemoBoard.get()
tt.shuttle.tt_um_factory_test.enable()
Expand Down
2 changes: 1 addition & 1 deletion src/ttboard/cocotb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def add_test(self, func, name:str=None):
self.tests_to_run[name] = func

def test(self, dut):
from ttboard.cocotb.time import SystemTime
from ttboard.cocotb.time.system import SystemTime
failures = 0
for nm in self.test_names:
SystemTime.reset()
Expand Down
8 changes: 3 additions & 5 deletions src/ttboard/cocotb/clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
@copyright: Copyright (C) 2024 Pat Deegan, https://psychogenic.com
'''
import gc
import ttboard.util.time as time
gc.collect()
from ttboard.cocotb.time import TimeValue
gc.collect()

Expand Down Expand Up @@ -42,9 +40,9 @@ def clear_all(cls):
@classmethod
def all(cls):
global _ClockForSignal
vals = _ClockForSignal.values()
vals = list(_ClockForSignal.values())
if len(vals) < 2:
return list(vals)
return vals

return sorted(vals, key=lambda x: float(x.half_period))

Expand Down Expand Up @@ -95,7 +93,7 @@ def time_is_now(self, currentTime:TimeValue):

def time_has_passed(self):
#print(f"time passed to {SystemTime.current()} next is {self.next_toggle}")
from ttboard.cocotb.time import SystemTime
from ttboard.cocotb.time.system import SystemTime
self.time_is_now(SystemTime.current())

def toggle(self):
Expand Down
3 changes: 3 additions & 0 deletions src/ttboard/cocotb/dut.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def value(self, set_to:int):
else:
self._port[self.slice_start] = set_to


def __int__(self):
return int(self.value)
def __repr__(self):
if self.slice_end is not None:
return str(self._port[self.slice_start:self.slice_end])
Expand Down
1 change: 1 addition & 0 deletions src/ttboard/cocotb/time/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .value import TimeConverter, TimeValue
36 changes: 36 additions & 0 deletions src/ttboard/cocotb/time/system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'''
Created on Nov 23, 2024
@author: Pat Deegan
@copyright: Copyright (C) 2024 Pat Deegan, https://psychogenic.com
'''
from ttboard.cocotb.time.value import TimeValue
from ttboard.cocotb.clock import Clock

class SystemTime:
_global_time = TimeValue(0, 'ns')

@classmethod
def reset(cls):
cls._global_time = TimeValue(0, 'ns')

@classmethod
def current(cls) -> TimeValue:
return cls._global_time

@classmethod
def set_units(cls, units:str):
cls._global_time = TimeValue(cls._global_time.time, units)

@classmethod
def advance(cls, time_or_timevalue, units:str=None):
if isinstance(time_or_timevalue, TimeValue):
cls._global_time += time_or_timevalue
elif isinstance(time_or_timevalue, int) and units is not None:
cls._global_time += TimeValue(time_or_timevalue, units)
else:
raise ValueError

for clk in Clock.all():
clk.time_is_now(cls._global_time)

33 changes: 1 addition & 32 deletions src/ttboard/cocotb/time.py → src/ttboard/cocotb/time/value.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'''
Created on Nov 22, 2024
Created on Nov 23, 2024
@author: Pat Deegan
@copyright: Copyright (C) 2024 Pat Deegan, https://psychogenic.com
'''
from ttboard.cocotb.clock import Clock
class TimeConverter:
UnitScales = {
'fs': 1e-15,
Expand Down Expand Up @@ -123,34 +122,4 @@ def __truediv__(self, other):

def __mul__(self, other:int):
return TimeValue(self.time*other, self.units)


class SystemTime:
_global_time = TimeValue(0, 'ns')

@classmethod
def reset(cls):
cls._global_time = TimeValue(0, 'ns')

@classmethod
def current(cls) -> TimeValue:
return cls._global_time

@classmethod
def set_units(cls, units:str):
cls._global_time = TimeValue(cls._global_time.time, units)

@classmethod
def advance(cls, time_or_timevalue, units:str=None):
if isinstance(time_or_timevalue, TimeValue):
cls._global_time += time_or_timevalue
elif isinstance(time_or_timevalue, int) and units is not None:
cls._global_time += TimeValue(time_or_timevalue, units)
else:
raise ValueError

for clk in Clock.all():
clk.time_is_now(cls._global_time)



2 changes: 1 addition & 1 deletion src/ttboard/cocotb/triggers/clockcycles.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'''
from ttboard.cocotb.triggers.awaitable import Awaitable
from ttboard.cocotb.clock import Clock
from ttboard.cocotb.time import SystemTime
from ttboard.cocotb.time.system import SystemTime

class ClockCycles(Awaitable):
def __init__(self, sig, num_cycles:int):
Expand Down
34 changes: 21 additions & 13 deletions src/ttboard/cocotb/triggers/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
'''
from ttboard.cocotb.triggers.awaitable import Awaitable
from ttboard.cocotb.clock import Clock
from ttboard.cocotb.time import SystemTime, TimeValue
from ttboard.cocotb.time.value import TimeValue
from ttboard.cocotb.time.system import SystemTime

class Edge(Awaitable):
def __init__(self, signal):
Expand All @@ -16,6 +17,10 @@ def __init__(self, signal):
self.initial_state = None
self.primed = False


@property
def signal_value(self):
return int(self.signal.value)

def prepare_for_wait(self):
return
Expand Down Expand Up @@ -64,17 +69,19 @@ def __init__(self, signal):
super().__init__(signal)

def prepare_for_wait(self):
self.initial_state = int(self.signal)
self.initial_state = self.signal_value
self.primed = False if self.initial_state else True
# print(f"Initial state: {self.initial_state} and primed {self.primed}")
return

def conditions_met(self):
if not self.primed:
if int(self.signal) == 0:
self.primed = True
else:
if int(self.signal):
if self.primed:
if self.signal_value:
return True
else:
if self.signal_value == 0:
self.primed = True




Expand All @@ -83,17 +90,18 @@ def __init__(self, signal):
super().__init__(signal)

def prepare_for_wait(self):
self.initial_state = int(self.signal)
self.initial_state = self.signal_value
self.primed = True if self.initial_state else False
return

def conditions_met(self):
if not self.primed:
if int(self.signal):
self.primed = True
else:
if int(self.signal) == 0:
if self.primed:
if self.signal_value == 0:
return True
else:
if self.signal_value:
self.primed = True




Expand Down
3 changes: 2 additions & 1 deletion src/ttboard/cocotb/triggers/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

from ttboard.cocotb.triggers.awaitable import Awaitable
from ttboard.cocotb.clock import Clock
from ttboard.cocotb.time import SystemTime, TimeValue
from ttboard.cocotb.time.value import TimeValue
from ttboard.cocotb.time.system import SystemTime
import ttboard.util.time as tm
class Timer(Awaitable):
def __init__(self, time:int, units:str):
Expand Down
6 changes: 3 additions & 3 deletions src/ttboard/cocotb/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

from ttboard.cocotb.time import SystemTime
import math
from ttboard.cocotb.time.system import SystemTime

def get_sim_time(units:str):
current = SystemTime.current()
return current.time_in(units)
return math.ceil(current.time_in(units))
3 changes: 3 additions & 0 deletions src/ttboard/types/logic_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ def __repr__(self) -> str:
def __str__(self) -> str:
return self._get_str()

def __bin__(self):
return f'0b{str(self)!r}'

def __int__(self) -> int:
return self.to_unsigned()

Expand Down

0 comments on commit 984ee1a

Please sign in to comment.