Motion FPGA
Motion FPGA for the MachX02-7000HE Breakout Board
pwm_device_tb.vhd
Go to the documentation of this file.
1 -------------------------------------------------------------------------------
2 --! @file
3 --! @brief PWM device 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 device test bench
16 ENTITY pwm_device_tb IS
17 END ENTITY pwm_device_tb;
18 
19 --! Architecture tb of pwm_device_tb entity
20 ARCHITECTURE tb OF pwm_device_tb IS
21 
22  --! Test bench clock period
23  CONSTANT c_clk_period : time := 10 ns;
24 
25  --! Type for percentage array
26  TYPE t_percent_array IS ARRAY(0 TO 3) OF integer;
27 
28  --! Stimulus record type
29  TYPE t_stimulus IS RECORD
30  name : string(1 TO 30); --! Stimulus name
31  rst : std_logic; --! Reset input to pwm_device
32  data_wr : std_logic_vector(31 DOWNTO 0); --! Write data to pwm_device
33  data_rd : std_logic_vector(31 DOWNTO 0); --! Expected read data from pwm_device
34  percent : t_percent_array; --! Expected pwm percents
35  END RECORD t_stimulus;
36 
37  --! Stimulus array type
38  TYPE t_stimulus_array IS ARRAY(natural RANGE <>) OF t_stimulus;
39 
40  --! Test stimulus
42  (
43  (
44  name => "Reset ",
45  rst => '1',
46  data_wr => X"FFFFFFFF",
47  data_rd => X"00000000",
48  percent => (0, 0, 0, 0)
49  ),
50  (
51  name => "Set 0, 0, 0, 0 ",
52  rst => '0',
53  data_wr => X"00000000",
54  data_rd => X"00000000",
55  percent => (0, 0, 0, 0)
56  ),
57  (
58  name => "Set 255, 255, 255, 255 ",
59  rst => '0',
60  data_wr => X"FFFFFFFF",
61  data_rd => X"FFFFFFFF",
62  percent => (100, 100, 100, 100)
63  ),
64  (
65  name => "Set 127, 127, 127, 127 ",
66  rst => '0',
67  data_wr => X"7F7F7F7F",
68  data_rd => X"7F7F7F7F",
69  percent => (50, 50, 50, 50)
70  ),
71  (
72  name => "Set 0, 85, 170, 255 ",
73  rst => '0',
74  data_wr => X"FFAA5500",
75  data_rd => X"FFAA5500",
76  percent => (0, 33, 67, 100)
77  ),
78  (
79  name => "Set 0, 0, 0, 0 ",
80  rst => '0',
81  data_wr => X"00000000",
82  data_rd => X"00000000",
83  percent => (0, 0, 0, 0)
84  )
85  );
86 
87  -- Signals to uut
88  SIGNAL clk : std_logic; --! Clock input to uut
89  SIGNAL rst : std_logic; --! Reset input to uut
90  SIGNAL dat_wr_done : std_logic; --! Data write done input to uut
91  SIGNAL dat_wr_reg : std_logic_vector(31 DOWNTO 0); --! Data write register input to uut
92  SIGNAL dat_rd_reg : std_logic_vector(31 DOWNTO 0); --! Data read register output from uut
93  SIGNAL pwm_out : std_logic_vector(3 DOWNTO 0); --! PWM outputs from uut
94 
95  -- Signals to on_percent
96  SIGNAL on_rst : std_logic; --! Reset input to on_percent
97  SIGNAL on_percent : t_percent_array; --! Percent output from on_percent
98 
99  --! Function to create string from std_logic_vector
100  FUNCTION to_string (
101  vector : std_logic_vector) RETURN string
102  IS
103 
104  VARIABLE v_str : string(1 TO vector'length);
105 
106  BEGIN
107 
108  FOR i IN vector'range LOOP
109  v_str(i + 1) := std_logic'image(vector(i))(2);
110  END LOOP;
111 
112  RETURN v_str;
113 
114  END FUNCTION to_string;
115 
116 BEGIN
117 
118  --! Instantiate PWM device as uut
119  i_uut : ENTITY work.pwm_device(rtl)
120  PORT MAP (
121  clk_in => clk,
122  rst_in => rst,
126  pwm_adv_in => '1',
127  pwm_out => pwm_out
128  );
129 
130  --! Generate on_percent measuring entities
131  g_on_percent : FOR i IN 0 TO 3 GENERATE
132 
133  --! Instantiate on_percent
134  i_on_percent : ENTITY work.sim_on_percent(sim)
135  PORT MAP (
136  clk_in => clk,
137  rst_in => on_rst,
138  signal_in => pwm_out(i),
139  percent_out => on_percent(i)
140  );
141 
142  END GENERATE g_on_percent;
143 
144  --! @brief Clock generator process
145  --!
146  --! This generates the clk signal and the adv signal
147  pr_clock : PROCESS IS
148  BEGIN
149 
150  clk <= '0';
151  WAIT FOR c_clk_period / 2;
152 
153  clk <= '1';
154  WAIT FOR c_clk_period / 2;
155 
156  END PROCESS pr_clock;
157 
158  --! @brief Stimulus process to drive PWM unit under test
159  pr_stimulus : PROCESS IS
160  BEGIN
161 
162  -- Initialize entity inputs
163  rst <= '1';
164  on_rst <= '1';
165  dat_wr_reg <= (OTHERS => '0');
166  dat_wr_done <= '0';
167  WAIT FOR c_clk_period;
168 
169  -- Loop over stimulus
170  FOR s IN c_stimulus'range LOOP
171 
172  -- Log start of stimulus
173  REPORT "Starting: " & c_stimulus(s).name SEVERITY note;
174 
175  -- Perform write to device
176  rst <= c_stimulus(s).rst;
178  dat_wr_done <= '1';
179  WAIT FOR c_clk_period;
180  dat_wr_done <= '0';
181 
182  -- Wait for pwms to stabilize
183  WAIT FOR 256 * c_clk_period;
184 
185  -- Enable pwm counting
186  on_rst <= '0';
187 
188  -- Accumuate 256*10 clocks
189  WAIT FOR 2560 * c_clk_period;
190 
191  -- Assert pwm channels
192  FOR i IN 0 TO 3 LOOP
193 
194  -- Assert pwm channel
195  ASSERT on_percent(i) >= c_stimulus(s).percent(i) - 5 AND
196  on_percent(i) <= c_stimulus(s).percent(i) + 5
197  REPORT "PWM channel " &
198  integer'image(i) &
199  " expected pwm of " &
200  integer'image(c_stimulus(s).percent(i)) &
201  " but got " &
202  integer'image(on_percent(i))
203  SEVERITY error;
204 
205  END LOOP;
206 
207  -- Assert read from device
208  ASSERT dat_rd_reg = c_stimulus(s).data_rd
209  REPORT "Expected read of " &
210  to_string(c_stimulus(s).data_rd) &
211  " but got " &
212  to_string(dat_rd_reg)
213  SEVERITY error;
214 
215  -- Stop pwm counting
216  on_rst <= '1';
217 
218  END LOOP;
219 
220  -- Log end of test
221  REPORT "Finished" SEVERITY note;
222 
223  -- Finish the simulation
224  std.env.finish;
225 
226  END PROCESS pr_stimulus;
227 
228 END ARCHITECTURE tb;
in clk_instd_logic
Clock.
Definition: pwm_device.vhd:24
in rst_instd_logic
Asynchronous reset.
Definition: pwm_device.vhd:25
std_logic dat_wr_done
Data write done input to uut.
t_stimulus
Stimulus record type.
std_logic rst
Reset input to pwm_device.
std_logic_vector( 31 DOWNTO 0) dat_wr_reg
Data write register input to uut.
t_percent_array on_percent
Percent output from on_percent.
std_logic_vector( 31 DOWNTO 0) data_wr
Write data to pwm_device.
( 0 TO 3) integer t_percent_array
Type for percentage array.
PWM device test bench.
std_logic clk
Clock input to uut.
t_percent_array percent
Expected pwm percents.
in dat_wr_reg_instd_logic_vector( 31 DOWNTO 0)
Device Write Register value.
Definition: pwm_device.vhd:27
std_logic_vector( 3 DOWNTO 0) pwm_out
PWM outputs from uut.
in signal_instd_logic
Signal input.
Entity to measure on-percentage of signal.
out dat_rd_reg_outstd_logic_vector( 31 DOWNTO 0)
Device Read Register value.
Definition: pwm_device.vhd:28
string( 1 TO 30) name
Stimulus name.
std_logic on_rst
Reset input to on_percent.
in pwm_adv_instd_logic
PWM Advance flag.
Definition: pwm_device.vhd:29
in clk_instd_logic
Clock.
_library_ ieeeieee
Using IEEE library.
time := 10 ns c_clk_period
Test bench clock period.
string to_stringvector,
Function to create string from std_logic_vector.
std_logic_vector( 31 DOWNTO 0) data_rd
Expected read data from pwm_device.
t_stimulus_array :=((name => "Reset ",rst => '1',data_wr => X"FFFFFFFF",data_rd => X"00000000",percent =>( 0, 0, 0, 0)),(name => "Set 0, 0, 0, 0 ",rst => '0',data_wr => X"00000000",data_rd => X"00000000",percent =>( 0, 0, 0, 0)),(name => "Set 255, 255, 255, 255 ",rst => '0',data_wr => X"FFFFFFFF",data_rd => X"FFFFFFFF",percent =>( 100, 100, 100, 100)),(name => "Set 127, 127, 127, 127 ",rst => '0',data_wr => X"7F7F7F7F",data_rd => X"7F7F7F7F",percent =>( 50, 50, 50, 50)),(name => "Set 0, 85, 170, 255 ",rst => '0',data_wr => X"FFAA5500",data_rd => X"FFAA5500",percent =>( 0, 33, 67, 100)),(name => "Set 0, 0, 0, 0 ",rst => '0',data_wr => X"00000000",data_rd => X"00000000",percent =>( 0, 0, 0, 0))) c_stimulus
Test stimulus.
PWM device entity.
Definition: pwm_device.vhd:22
in rst_instd_logic
Asynchronous reset.
std_logic_vector( 31 DOWNTO 0) dat_rd_reg
Data read register output from uut.
out pwm_outstd_logic_vector( 3 DOWNTO 0)
PWM outputs.
Definition: pwm_device.vhd:31
array(natural range <> ) of t_stimulus t_stimulus_array
Stimulus array type.
out percent_outinteger
On percentage output.
in dat_wr_done_instd_logic
Device Write Done flag.
Definition: pwm_device.vhd:26