Skip to content

Commit

Permalink
counter: add register to set counter value
Browse files Browse the repository at this point in the history
- Setting the counter takes precedence over a trigger event, that way the
block will output the set value at least for one tick
- It is possible to set a value while the block is disabled (this allows to
control the pos bus value without having to actually count)
- Timing tests were added for two cases: setting a value while enabled and
setting a value while disabled
- Python counter simulation script was updated to consider the set register
  • Loading branch information
EmilioPeJu committed Jul 18, 2024
1 parent 3fbd543 commit 36e7381
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
5 changes: 5 additions & 0 deletions modules/counter/counter.block.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ description: Counter OUT update mode (on internal counter value change or on ENA
0: On-Change
1: On-Disable

[SET]
type: param int
description: set current value of a counter
wstb: true

[START]
type: param int
description: Counter start value
Expand Down
13 changes: 12 additions & 1 deletion modules/counter/counter.timing.ini
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,15 @@ scope: counter.block.ini
10 : TRIG=0 -> CARRY=0
11 : TRIG=1 -> OUT=-3
12 : TRIG=0
13 : ENABLE=0
13 : ENABLE=0

[Manual set value]
3 : START=20
6 : ENABLE=1 -> OUT=20
9 : TRIG=1 -> OUT=21
10 : SET=10, TRIG=0 -> OUT=10
11 : TRIG=1 -> OUT=11
12 : TRIG=0, ENABLE=0

[Manual set value disabled]
3 : SET=42 -> OUT=42
8 changes: 6 additions & 2 deletions modules/counter/counter_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


class CounterSimulation(BlockSimulation):
ENABLE, TRIG, DIR, TRIG_EDGE, OUT_MODE, START, STEP, MAX, MIN, CARRY, OUT = PROPERTIES
ENABLE, TRIG, DIR, TRIG_EDGE, OUT_MODE, SET, START, STEP, MAX, MIN, CARRY, OUT = PROPERTIES

def __init__(self):
self.counter = 0
Expand Down Expand Up @@ -50,6 +50,8 @@ def on_changes(self, ts, changes):
if self.OUT_MODE == MODE_OnChange:
if changes.get(NAMES.ENABLE, None) is 1:
self.OUT = self.START
elif NAMES.SET in changes:
self.OUT = self.SET
elif changes.get(NAMES.ENABLE, None) is 0:
self.CARRY = 0
elif self.ENABLE and NAMES.TRIG in changes:
Expand All @@ -75,6 +77,8 @@ def on_changes(self, ts, changes):
if changes.get(NAMES.ENABLE, None) is 1:
self.counter = self.START
self.overflow = 0
elif NAMES.SET in changes:
self.counter = self.SET
elif self.ENABLE and NAMES.TRIG in changes:
# process trigger on selected edge
if got_trigger:
Expand All @@ -93,4 +97,4 @@ def on_changes(self, ts, changes):
self.overflow = 1
elif changes.get(NAMES.ENABLE, None) is 0:
self.OUT = self.counter
self.CARRY = self.overflow
self.CARRY = self.overflow
5 changes: 5 additions & 0 deletions modules/counter/hdl/counter.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ port (
TRIG_EDGE : in std_logic_vector(31 downto 0) := (others => '0');
TRIG_EDGE_WSTB : in std_logic;
OUT_MODE : in std_logic_vector(31 downto 0);
SET : in std_logic_vector(31 downto 0);
SET_WSTB : in std_logic;
START : in std_logic_vector(31 downto 0);
START_WSTB : in std_logic;
STEP : in std_logic_vector(31 downto 0);
Expand Down Expand Up @@ -130,6 +132,9 @@ begin
if (enable_rise = '1') then
counter <= signed(START);
carry_latch <= '0';
elsif (SET_WSTB = '1') then
counter <= signed(SET);
carry_latch <= '0';
-- Drop the carry signal on falling enable
elsif (enable_fall = '1') then
counter_carry <= '0';
Expand Down

0 comments on commit 36e7381

Please sign in to comment.