Skip to content

Commit

Permalink
better sleep management to slow down clocking as required
Browse files Browse the repository at this point in the history
  • Loading branch information
psychogenic committed Nov 23, 2024
1 parent 984ee1a commit a1adeca
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/ttboard/cocotb/time/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
'''
from ttboard.cocotb.time.value import TimeValue
from ttboard.cocotb.clock import Clock
import ttboard.util.time as time

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

@classmethod
def reset(cls):
Expand All @@ -25,12 +27,17 @@ def set_units(cls, units:str):
@classmethod
def advance(cls, time_or_timevalue, units:str=None):
if isinstance(time_or_timevalue, TimeValue):
cls._global_time += time_or_timevalue
tstep = time_or_timevalue
elif isinstance(time_or_timevalue, int) and units is not None:
cls._global_time += TimeValue(time_or_timevalue, units)
tstep = TimeValue(time_or_timevalue, units)
else:
raise ValueError

cls._global_time += tstep
if cls._min_sleep_time < tstep:
time.sleep_us(tstep.time_in('us'))


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

3 changes: 3 additions & 0 deletions src/ttboard/cocotb/time/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def __lt__(self, other):
def __le__(self, other):
return not (self > other)

def __ge__(self, other):
return float(self) >= float(other)

def __eq__(self, other):
return float(self) == float(other)

Expand Down
3 changes: 0 additions & 3 deletions src/ttboard/cocotb/triggers/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def run_timer(self):
# print(f"All clocks on timer: {all_clocks}")
fastest_clock = all_clocks[0]
time_increment = fastest_clock.half_period
sleep_us = fastest_clock.sleep_us
target_time = SystemTime.current() + self.time
increment_count = 0
while SystemTime.current() < target_time:
Expand All @@ -31,8 +30,6 @@ def run_timer(self):
increment_count += 1
SystemTime.advance(time_increment)

if sleep_us > 0:
tm.sleep_us(sleep_us)

def __iter__(self):
return self
Expand Down

0 comments on commit a1adeca

Please sign in to comment.