Skip to content

Commit 8160486

Browse files
committed
Pure and impure functions in VHDL
1 parent 126ee14 commit 8160486

File tree

7 files changed

+242
-0
lines changed

7 files changed

+242
-0
lines changed

Docs/functions/.vhd

Whitespace-only changes.

Docs/functions/_tb.vhd

Whitespace-only changes.

Docs/functions/main.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-> Functions in VHDL are subprograms that implement frequently used algorithms, take input values, and always return a value.
2+
-> Unlike procedures, functions cannot contain wait statements, meaning they consume zero simulation time.
3+
-> There are two types of functions in VHDL, pure and impure:
4+
* A pure function doesn't modify or read external signals, always returning the same value with the same arguments.
5+
* An impure function may interact with external signals and may have side effects.
6+
7+
-> Syntax for a function:
8+
[pure | impure] function <function_name> (
9+
<parameter_name>: <parameter_type> := <default_value>;
10+
...
11+
) return <return_type> is
12+
<constant_or_variable_declaration>
13+
begin
14+
<code_performed_by_the_function>
15+
return <value>;
16+
end function;
17+
18+
-> Functions must always return a value and cannot modify parameters since all parameters are treated as constants.

Docs/functions/traffic_lights.vhd

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
library ieee;
2+
use ieee.std_logic_1164.all;
3+
use ieee.numeric_std.all;
4+
5+
entity traffic_lights is
6+
generic (CLOCK_FREQUENCY : integer);
7+
port (
8+
clk : in std_logic;
9+
nRst : in std_logic;
10+
11+
NORTH_RED : out std_logic;
12+
NORTH_YELLOW : out std_logic;
13+
NORTH_GREEN : out std_logic;
14+
WEST_RED : out std_logic;
15+
WEST_YELLOW : out std_logic;
16+
WEST_GREEN : out std_logic
17+
);
18+
end entity;
19+
20+
architecture rtl of traffic_lights is
21+
type t_state is (NORTH_NEXT, NORTH_READY, NORTH, STOP_NORTH, WEST_NEXT, WEST_READY, WEST, STOP_WEST);
22+
23+
signal state : t_state;
24+
signal counter : integer range 0 to CLOCK_FREQUENCY * 60;
25+
26+
function val_counter(minutes: integer := 0; seconds: integer := 0) return integer is
27+
variable total_seconds: integer;
28+
begin
29+
30+
total_seconds := seconds + minutes * 60;
31+
return total_seconds * CLOCK_FREQUENCY - 1;
32+
33+
end function;
34+
begin
35+
36+
process(clk) is
37+
38+
impure function counter_expired(minutes: integer := 0; seconds: integer := 0) return boolean is
39+
begin
40+
41+
if counter = val_counter(minutes, seconds) then
42+
counter <= 0;
43+
return true;
44+
else
45+
return false;
46+
end if;
47+
48+
end function;
49+
50+
begin
51+
52+
if rising_edge(clk) then
53+
if nRst = '0' then
54+
state <= NORTH_NEXT;
55+
counter <= 0;
56+
NORTH_RED <= '1';
57+
NORTH_YELLOW <= '0';
58+
NORTH_GREEN <= '0';
59+
WEST_RED <= '1';
60+
WEST_YELLOW <= '0';
61+
WEST_GREEN <= '0';
62+
else
63+
NORTH_RED <= '0';
64+
NORTH_YELLOW <= '0';
65+
NORTH_GREEN <= '0';
66+
WEST_RED <= '0';
67+
WEST_YELLOW <= '0';
68+
WEST_GREEN <= '0';
69+
counter <= counter + 1;
70+
71+
case state is
72+
when NORTH_NEXT =>
73+
NORTH_RED <= '1';
74+
WEST_RED <= '1';
75+
if counter_expired(seconds => 5) then
76+
state <= NORTH_READY;
77+
end if;
78+
79+
when NORTH_READY =>
80+
NORTH_RED <= '1';
81+
NORTH_YELLOW <= '1';
82+
WEST_RED <= '1';
83+
if counter_expired(seconds => 5) then
84+
state <= NORTH;
85+
end if;
86+
87+
when NORTH =>
88+
NORTH_GREEN <= '1';
89+
WEST_RED <= '1';
90+
if counter_expired(minutes => 1) then
91+
state <= STOP_NORTH;
92+
end if;
93+
94+
when STOP_NORTH =>
95+
NORTH_YELLOW <= '1';
96+
WEST_RED <= '1';
97+
if counter_expired(seconds => 5) then
98+
state <= WEST_NEXT;
99+
end if;
100+
101+
when WEST_NEXT =>
102+
NORTH_RED <= '1';
103+
WEST_RED <= '1';
104+
if counter_expired(seconds => 5) then
105+
state <= WEST_READY;
106+
end if;
107+
108+
when WEST_READY =>
109+
NORTH_RED <= '1';
110+
WEST_YELLOW <= '1';
111+
WEST_RED <= '1';
112+
if counter_expired(seconds => 5) then
113+
state <= WEST;
114+
end if;
115+
116+
when WEST =>
117+
NORTH_RED <= '1';
118+
WEST_GREEN <= '1';
119+
if counter_expired(minutes => 1) then
120+
state <= STOP_WEST;
121+
end if;
122+
123+
when STOP_WEST =>
124+
NORTH_RED <= '1';
125+
WEST_YELLOW <= '1';
126+
if counter_expired(seconds => 5) then
127+
state <= NORTH_NEXT;
128+
end if;
129+
end case;
130+
end if;
131+
end if;
132+
133+
end process;
134+
135+
end architecture;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
library ieee;
2+
use ieee.std_logic_1164.all;
3+
use ieee.numeric_std.all;
4+
5+
entity traffic_lights_tb is
6+
end entity;
7+
8+
architecture sim of traffic_lights_tb is
9+
constant CLOCK_FREQUENCY : integer := 100;
10+
constant CLOCK_PERIOD : time := 1000 ms / CLOCK_FREQUENCY;
11+
12+
signal clk : std_logic := '1';
13+
signal nRst : std_logic := '0';
14+
15+
signal NORTH_RED : std_logic;
16+
signal NORTH_YELLOW : std_logic;
17+
signal NORTH_GREEN : std_logic;
18+
signal WEST_RED : std_logic;
19+
signal WEST_YELLOW : std_logic;
20+
signal WEST_GREEN : std_logic;
21+
begin
22+
23+
i_traffic_lights: entity work.traffic_lights(rtl)
24+
generic map (CLOCK_FREQUENCY => CLOCK_FREQUENCY)
25+
port map (
26+
clk => clk,
27+
nRst => nRst,
28+
29+
NORTH_RED => NORTH_RED,
30+
NORTH_YELLOW => NORTH_YELLOW,
31+
NORTH_GREEN => NORTH_GREEN,
32+
WEST_RED => WEST_RED,
33+
WEST_YELLOW => WEST_YELLOW,
34+
WEST_GREEN => WEST_GREEN
35+
);
36+
37+
clk <= not clk after CLOCK_PERIOD / 2;
38+
39+
process is
40+
begin
41+
42+
wait until rising_edge(clk);
43+
wait until rising_edge(clk);
44+
nRst <= '1';
45+
wait;
46+
47+
end process;
48+
49+
end architecture;

Docs/functions/transcript

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
vcom -work work -2002 -explicit -stats=none {C:/Users/Choaib ELMADI/Downloads/D.I.F.Y/7. Electronics/2. FPGA/Getting Started with VHDL/Docs/functions/traffic_lights.vhd}
2+
Model Technology ModelSim - Intel FPGA Edition vcom 2020.1 Compiler 2020.02 Feb 28 2020
3+
-- Loading package STANDARD
4+
-- Loading package TEXTIO
5+
-- Loading package std_logic_1164
6+
-- Loading package NUMERIC_STD
7+
-- Compiling entity traffic_lights
8+
-- Compiling architecture rtl of traffic_lights
9+
10+
11+
vcom -work work -2002 -explicit -stats=none {C:/Users/Choaib ELMADI/Downloads/D.I.F.Y/7. Electronics/2. FPGA/Getting Started with VHDL/Docs/functions/traffic_lights_tb.vhd}
12+
Model Technology ModelSim - Intel FPGA Edition vcom 2020.1 Compiler 2020.02 Feb 28 2020
13+
-- Loading package STANDARD
14+
-- Loading package TEXTIO
15+
-- Loading package std_logic_1164
16+
-- Loading package NUMERIC_STD
17+
-- Compiling entity traffic_lights_tb
18+
-- Compiling architecture sim of traffic_lights_tb
19+
-- Loading entity traffic_lights
20+
21+
22+
vsim -gui work.traffic_lights_tb
23+
vsim -gui work.traffic_lights_tb
24+
Start time: 16:30:29 on Oct 05,2024
25+
Loading std.standard
26+
Loading std.textio(body)
27+
Loading ieee.std_logic_1164(body)
28+
Loading ieee.numeric_std(body)
29+
Loading work.traffic_lights_tb(sim)
30+
Loading work.traffic_lights(rtl)
31+
view -new wave
32+
-new not supported on PE
33+
add wave -position insertpoint \
34+
sim:/traffic_lights_tb/i_traffic_lights/clk \
35+
sim:/traffic_lights_tb/i_traffic_lights/nRst \
36+
sim:/traffic_lights_tb/i_traffic_lights/state \
37+
sim:/traffic_lights_tb/i_traffic_lights/counter
38+
run 5 min
39+
End time: 16:35:20 on Oct 05,2024, Elapsed time: 0:04:51
40+
Errors: 0, Warnings: 7

Docs/functions/vsim.wlf

536 KB
Binary file not shown.

0 commit comments

Comments
 (0)