Skip to content

Commit dc5a6f7

Browse files
committed
uart vc: use an enumerated type for parity
1 parent ac0952b commit dc5a6f7

7 files changed

Lines changed: 53 additions & 34 deletions

File tree

examples/vhdl/uart/src/test/tb_uart_rx.vhd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ architecture tb of tb_uart_rx is
3535
signal num_overflows : integer := 0;
3636

3737
constant uart_bfm : uart_master_t := new_uart_master(initial_baud_rate => baud_rate,
38-
initial_parity => parity);
38+
initial_parity => int_to_parity(parity));
39+
3940
constant uart_stream : stream_master_t := as_stream(uart_bfm);
4041

4142
constant axi_stream_bfm : axi_stream_slave_t := new_axi_stream_slave(data_length => tdata'length);

examples/vhdl/uart/src/test/tb_uart_tx.vhd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ architecture tb of tb_uart_tx is
3636

3737
shared variable rnd_stimuli, rnd_expected : RandomPType;
3838
constant uart_bfm : uart_slave_t := new_uart_slave(initial_baud_rate => baud_rate,
39-
initial_parity => parity,
39+
initial_parity => int_to_parity(parity),
4040
data_length => tdata'length);
4141
constant uart_stream : stream_slave_t := as_stream(uart_bfm);
4242

vunit/vhdl/com/src/com_types.vhd

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ package com_types_pkg is
4848
duplicate_actor_name_error,
4949
new_actor_from_root_id_error);
5050

51+
type parity_t is (
52+
PARITY_NONE,
53+
PARITY_ODD,
54+
PARITY_EVEN);
55+
5156
subtype com_error_t is com_status_t range timeout to new_actor_from_root_id_error;
5257

5358
-- All fields of the actor type are private
@@ -406,6 +411,9 @@ package com_types_pkg is
406411

407412
-- Private
408413
impure function is_valid(code : integer) return boolean;
414+
415+
function int_to_parity(value : integer) return parity_t;
416+
409417
end package;
410418

411419
package body com_types_pkg is
@@ -889,4 +897,18 @@ package body com_types_pkg is
889897
return (p_id_number => value);
890898
end;
891899

900+
function int_to_parity(value: integer) return parity_t is
901+
begin
902+
case value is
903+
when 0 =>
904+
return PARITY_NONE;
905+
when 1 =>
906+
return PARITY_ODD;
907+
when 2 =>
908+
return PARITY_EVEN;
909+
when others =>
910+
return PARITY_NONE;
911+
end case;
912+
end function int_to_parity;
913+
892914
end package body com_types_pkg;

vunit/vhdl/verification_components/src/uart_master.vhd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ begin
3131
procedure uart_send(data : std_logic_vector;
3232
signal tx : out std_logic;
3333
baud_rate : integer;
34-
parity : natural
34+
parity : parity_t
3535
) is
3636
constant time_per_bit : time := (10**9 / baud_rate) * 1 ns;
3737

@@ -48,9 +48,9 @@ begin
4848
send_bit(data(i));
4949
end loop;
5050

51-
if parity = 1 then
51+
if parity = PARITY_ODD then
5252
send_bit(odd_parity(data));
53-
elsif parity = 2 then
53+
elsif parity = PARITY_EVEN then
5454
send_bit(even_parity(data));
5555
end if;
5656

@@ -59,7 +59,7 @@ begin
5959

6060
variable msg : msg_t;
6161
variable baud_rate : natural := uart.p_baud_rate;
62-
variable parity : natural := uart.p_parity;
62+
variable parity : parity_t := uart.p_parity;
6363
variable msg_type : msg_type_t;
6464
begin
6565
receive(net, uart.p_actor, msg);
@@ -72,7 +72,7 @@ begin
7272
elsif msg_type = uart_set_baud_rate_msg then
7373
baud_rate := pop(msg);
7474
elsif msg_type = uart_set_parity_msg then
75-
parity := pop(msg);
75+
parity := int_to_parity(pop(msg));
7676
else
7777
unexpected_msg_type(msg_type);
7878
end if;

vunit/vhdl/verification_components/src/uart_pkg.vhd

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ package uart_pkg is
1818
type uart_master_t is record
1919
p_actor : actor_t;
2020
p_baud_rate : natural;
21-
p_parity : natural;
21+
p_parity : parity_t;
2222
p_idle_state : std_logic;
2323
end record uart_master_t;
2424

2525
type uart_slave_t is record
2626
p_actor : actor_t;
2727
p_baud_rate : natural;
28-
p_parity : natural;
28+
p_parity : parity_t;
2929
p_idle_state : std_logic;
3030
p_data_length : positive;
3131
end record;
@@ -42,22 +42,22 @@ package uart_pkg is
4242
-- 0 = no parity, 1 = odd parity, 2 = even parity
4343
procedure set_parity(signal net : inout network_t;
4444
uart_master : uart_master_t;
45-
parity : natural);
45+
parity : parity_t);
4646

4747
procedure set_parity(signal net : inout network_t;
4848
uart_slave : uart_slave_t;
49-
parity : natural);
49+
parity : parity_t);
5050

5151
constant default_baud_rate : natural := 115200;
5252
constant default_idle_state : std_logic := '1';
5353
constant default_data_length : positive := 8;
54-
constant default_parity : natural := 0;
54+
constant default_parity : parity_t := PARITY_NONE;
5555

5656
impure function new_uart_master(initial_baud_rate : natural := default_baud_rate;
57-
initial_parity : natural := default_parity;
57+
initial_parity : parity_t := default_parity;
5858
idle_state : std_logic := default_idle_state) return uart_master_t;
5959
impure function new_uart_slave(initial_baud_rate : natural := default_baud_rate;
60-
initial_parity : natural := default_parity;
60+
initial_parity : parity_t := default_parity;
6161
idle_state : std_logic := default_idle_state;
6262
data_length : positive := default_data_length) return uart_slave_t;
6363

@@ -68,7 +68,7 @@ package uart_pkg is
6868

6969
constant uart_set_baud_rate_msg : msg_type_t := new_msg_type("uart set baud rate");
7070

71-
constant uart_set_parity_msg : msg_type_t := new_msg_type("uart set parity rate");
71+
constant uart_set_parity_msg : msg_type_t := new_msg_type("uart set parity");
7272

7373
function even_parity(data : std_logic_vector) return std_logic;
7474
function odd_parity (data : std_logic_vector) return std_logic;
@@ -78,7 +78,7 @@ end package;
7878
package body uart_pkg is
7979

8080
impure function new_uart_master(initial_baud_rate : natural := default_baud_rate;
81-
initial_parity : natural := default_parity;
81+
initial_parity : parity_t := default_parity;
8282
idle_state : std_logic := default_idle_state) return uart_master_t is
8383
begin
8484
return (p_actor => new_actor,
@@ -89,7 +89,7 @@ package body uart_pkg is
8989

9090
impure function new_uart_slave(
9191
initial_baud_rate : natural := default_baud_rate;
92-
initial_parity : natural := default_parity;
92+
initial_parity : parity_t := default_parity;
9393
idle_state : std_logic := default_idle_state;
9494
data_length : positive := default_data_length
9595
) return uart_slave_t is
@@ -147,28 +147,24 @@ package body uart_pkg is
147147

148148
procedure set_parity(signal net : inout network_t;
149149
actor : actor_t;
150-
parity : natural) is
150+
parity : parity_t) is
151151
variable msg : msg_t := new_msg(uart_set_parity_msg);
152152
begin
153-
if parity > 2 then
154-
report "Invalid parity value: " & to_string(parity)
155-
severity error;
156-
end if;
157153

158-
push(msg, parity);
154+
push(msg, parity_t'pos(parity));
159155
send(net, actor, msg);
160156
end;
161157

162158
procedure set_parity(signal net : inout network_t;
163159
uart_master : uart_master_t;
164-
parity : natural) is
160+
parity : parity_t) is
165161
begin
166162
set_parity(net, uart_master.p_actor, parity);
167163
end;
168164

169165
procedure set_parity(signal net : inout network_t;
170166
uart_slave : uart_slave_t;
171-
parity : natural) is
167+
parity : parity_t) is
172168
begin
173169
set_parity(net, uart_slave.p_actor, parity);
174170
end;

vunit/vhdl/verification_components/src/uart_slave.vhd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ end entity;
2424

2525
architecture a of uart_slave is
2626
signal baud_rate : natural := uart.p_baud_rate;
27-
signal parity : natural := uart.p_parity;
27+
signal parity : parity_t := uart.p_parity;
2828
signal local_event : std_logic := '0';
2929
constant data_queue : queue_t := new_queue;
3030
begin
@@ -39,7 +39,7 @@ begin
3939
if msg_type = uart_set_baud_rate_msg then
4040
baud_rate <= pop(msg);
4141
elsif msg_type = uart_set_parity_msg then
42-
parity <= pop(msg);
42+
parity <= int_to_parity(pop(msg));
4343
elsif msg_type = stream_pop_msg then
4444
reply_msg := new_msg;
4545
if not (length(data_queue) > 0) then
@@ -59,7 +59,7 @@ begin
5959
procedure uart_recv(variable data : out std_logic_vector;
6060
signal rx : in std_logic;
6161
baud_rate : integer;
62-
parity : natural) is
62+
parity : parity_t) is
6363
constant time_per_bit : time := (10**9 / baud_rate) * 1 ns;
6464
constant time_per_half_bit : time := (10**9 / (2*baud_rate)) * 1 ns;
6565
variable parity_bit : std_logic;
@@ -74,7 +74,7 @@ begin
7474
wait for time_per_bit;
7575
end loop;
7676

77-
if parity = 1 then
77+
if parity = PARITY_ODD then
7878
parity_bit := rx;
7979
parity_calc := odd_parity(data);
8080
wait for 0 ns;
@@ -85,7 +85,7 @@ begin
8585
end if;
8686

8787
wait for time_per_bit;
88-
elsif parity = 2 then
88+
elsif parity = PARITY_EVEN then
8989
parity_bit := rx;
9090
parity_calc := even_parity(data);
9191
wait for 0 ns;

vunit/vhdl/verification_components/test/tb_uart.vhd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ begin
9090
test_baud_rate(7000);
9191
test_baud_rate(200000);
9292
elsif run("test_parity_odd") then
93-
set_parity(net, master_uart_p, 1);
94-
set_parity(net, slave_uart_p, 1);
93+
set_parity(net, master_uart_p, PARITY_ODD);
94+
set_parity(net, slave_uart_p, PARITY_ODD);
9595

9696
for i in 0 to 7 loop
9797
push_stream(net, master_stream_p,
@@ -101,8 +101,8 @@ begin
101101
end loop;
102102

103103
elsif run("test_parity_even") then
104-
set_parity(net, master_uart_p, 2);
105-
set_parity(net, slave_uart_p, 2);
104+
set_parity(net, master_uart_p, PARITY_EVEN);
105+
set_parity(net, slave_uart_p, PARITY_EVEN);
106106

107107
for i in 0 to 7 loop
108108
push_stream(net, master_stream_p,

0 commit comments

Comments
 (0)