Motion FPGA
Motion FPGA for the MachX02-7000HE Breakout Board
pwm_tb.vhd
Go to the documentation of this file.
1 -------------------------------------------------------------------------------
2 --! @file
3 --! @brief PWM test bench
4 -------------------------------------------------------------------------------
5 
6 --! Using IEEE library
7 LIBRARY ieee;
8 
9 --! Using IEEE standard logic components
10 USE ieee.std_logic_1164.ALL;
11 
12 --! Using IEE standard numeric components
13 USE ieee.numeric_std.ALL;
14 
15 --! @brief PWM test bench
16 ENTITY pwm_tb IS
17 END ENTITY pwm_tb;
18 
19 --! Architecture tb of pwm_tb entity
20 ARCHITECTURE tb OF pwm_tb IS
21 
22  --! Test bench clock period
23  CONSTANT c_clk_period : time := 10 ns;
24 
25  --! Stimulus record type
26  TYPE t_stimulus IS RECORD
27  name : string(1 TO 20); --! Stimulus name
28  rst : std_logic; --! rst input to uut
29  adv : std_logic; --! adv input to uut
30  duty : std_logic_vector(1 DOWNTO 0); --! duty input to uut
31  percent : integer; --! Expected percent
32  END RECORD t_stimulus;
33 
34  --! Stimulus array type
35  TYPE t_stimulus_array IS ARRAY(natural RANGE <>) OF t_stimulus;
36 
37  --! Test stimulus
39  (
40  (
41  name => "Hold in reset ",
42  rst => '1',
43  adv => '0',
44  duty => B"00",
45  percent => 0
46  ),
47  (
48  name => "Freeze ",
49  rst => '0',
50  adv => '0',
51  duty => B"11",
52  percent => 0
53  ),
54  (
55  name => "Running period 3 ",
56  rst => '0',
57  adv => '1',
58  duty => B"11",
59  percent => 100
60  ),
61  (
62  name => "Running period 2 ",
63  rst => '0',
64  adv => '1',
65  duty => B"10",
66  percent => 67
67  ),
68  (
69  name => "Running period 1 ",
70  rst => '0',
71  adv => '1',
72  duty => B"01",
73  percent => 33
74  ),
75  (
76  name => "Running period 0 ",
77  rst => '0',
78  adv => '1',
79  duty => B"00",
80  percent => 0
81  )
82  );
83 
84  -- Signals to uut
85  SIGNAL clk : std_logic; --! Clock input to pwm uut
86  SIGNAL rst : std_logic; --! Reset input to pwm uut
87  SIGNAL adv : std_logic; --! PWM advance input to pwm uut
88  SIGNAL duty : std_logic_vector(1 DOWNTO 0); --! Duty-cycle input to pwm uut
89  SIGNAL pwm : std_logic; --! PWM output from pwm uut
90 
91  -- Signals to on_percent
92  SIGNAL on_rst : std_logic; --! Reset input to on_percent
93  SIGNAL on_percent : integer; --! Percent output from on_percent
94 
95 BEGIN
96 
97  --! Instantiate PWM as uut
98  i_uut : ENTITY work.pwm(rtl)
99  GENERIC MAP (
100  bit_width => 2
101  )
102  PORT MAP (
103  clk_in => clk,
104  rst_in => rst,
105  pwm_adv_in => adv,
106  pwm_duty_in => duty,
107  pwm_out => pwm
108  );
109 
110  --! Instantiate on_percent
111  i_on_percent : ENTITY work.sim_on_percent(sim)
112  PORT MAP (
113  clk_in => clk,
114  rst_in => on_rst,
115  signal_in => pwm,
117  );
118 
119  --! @brief Clock generation process
120  pr_clock : PROCESS IS
121  BEGIN
122 
123  -- Low for 1/2 clock period
124  clk <= '0';
125  WAIT FOR c_clk_period / 2;
126 
127  -- High for 1/2 clock period
128  clk <= '1';
129  WAIT FOR c_clk_period / 2;
130 
131  END PROCESS pr_clock;
132 
133  --! @brief Stimulus process to drive PWM unit under test
134  pr_stimulus : PROCESS IS
135  BEGIN
136 
137  -- Initialize entity inputs
138  rst <= '1';
139  adv <= '0';
140  duty <= B"00";
141  on_rst <= '1';
142  WAIT FOR c_clk_period;
143 
144  -- Loop over stimulus
145  FOR s IN c_stimulus'range LOOP
146  -- Log start of stimulus
147  REPORT "Starting: " & c_stimulus(s).name SEVERITY note;
148 
149  -- Set stimulus inputs
150  duty <= c_stimulus(s).duty;
151  adv <= c_stimulus(s).adv;
152  rst <= c_stimulus(s).rst;
153 
154  -- Wait for pwm to stabilize
155  WAIT FOR 10 * c_clk_period;
156 
157  -- Enable pwm counting
158  on_rst <= '0';
159 
160  -- Accumuate 100 clocks
161  WAIT FOR 100 * c_clk_period;
162 
163  -- Assert outputs
164  ASSERT on_percent >= c_stimulus(s).percent - 5 AND
165  on_percent <= c_stimulus(s).percent + 5
166  REPORT "Expected pwm of " & integer'image(c_stimulus(s).percent)
167  & " but got " & integer'image(on_percent)
168  SEVERITY error;
169 
170  -- Stop pwm counting
171  on_rst <= '1';
172  END LOOP;
173 
174  -- Log end of test
175  REPORT "Finished" SEVERITY note;
176 
177  -- Finish the simulation
178  std.env.finish;
179 
180  END PROCESS pr_stimulus;
181 
182 END ARCHITECTURE tb;
183 
in rst_instd_logic
Asynchronous reset.
Definition: pwm.vhd:31
time := 10 ns c_clk_period
Test bench clock period.
Definition: pwm_tb.vhd:23
integer on_percent
Percent output from on_percent.
Definition: pwm_tb.vhd:93
string( 1 TO 20) name
Stimulus name.
Definition: pwm_tb.vhd:27
std_logic rst
rst input to uut
Definition: pwm_tb.vhd:28
in signal_instd_logic
Signal input.
std_logic on_rst
Reset input to on_percent.
Definition: pwm_tb.vhd:92
Entity to measure on-percentage of signal.
PWM entity.
Definition: pwm.vhd:25
std_logic clk
Clock input to pwm uut.
Definition: pwm_tb.vhd:85
t_stimulus_array :=((name => "Hold in reset ",rst => '1',adv => '0',duty => B"00",percent => 0),(name => "Freeze ",rst => '0',adv => '0',duty => B"11",percent => 0),(name => "Running period 3 ",rst => '0',adv => '1',duty => B"11",percent => 100),(name => "Running period 2 ",rst => '0',adv => '1',duty => B"10",percent => 67),(name => "Running period 1 ",rst => '0',adv => '1',duty => B"01",percent => 33),(name => "Running period 0 ",rst => '0',adv => '1',duty => B"00",percent => 0)) c_stimulus
Test stimulus.
Definition: pwm_tb.vhd:38
integer percent
Expected percent.
Definition: pwm_tb.vhd:31
in pwm_duty_instd_logic_vector( bit_width- 1 DOWNTO 0)
PWM duty cycle.
Definition: pwm.vhd:33
bit_widthnatural range 2 TO 32:= 8
PWM width.
Definition: pwm.vhd:28
in clk_instd_logic
Clock.
std_logic pwm
PWM output from pwm uut.
Definition: pwm_tb.vhd:89
in pwm_adv_instd_logic
PWM Advance flag.
Definition: pwm.vhd:32
PWM test bench.
Definition: pwm_tb.vhd:16
t_stimulus
Stimulus record type.
Definition: pwm_tb.vhd:26
std_logic_vector( 1 DOWNTO 0) duty
duty input to uut
Definition: pwm_tb.vhd:30
in clk_instd_logic
Clock.
Definition: pwm.vhd:30
array(natural range <> ) of t_stimulus t_stimulus_array
Stimulus array type.
Definition: pwm_tb.vhd:35
std_logic adv
adv input to uut
Definition: pwm_tb.vhd:29
_library_ ieeeieee
Using IEEE library.
in rst_instd_logic
Asynchronous reset.
out pwm_outstd_logic
PWM output.
Definition: pwm.vhd:35
out percent_outinteger
On percentage output.