1
|
-------------------------------------------------------------------------------------------------
|
2
|
-- Company : CNES
|
3
|
-- Author : Mickael Carl (CNES)
|
4
|
-- Copyright : Copyright (c) CNES.
|
5
|
-- Licensing : GNU GPLv3
|
6
|
-------------------------------------------------------------------------------------------------
|
7
|
-- Version : V1.1
|
8
|
-- Version history :
|
9
|
-- V1 : 2015-04-13 : Mickael Carl (CNES): Creation
|
10
|
-- V1.1 : 2016-05-03 : F.Manni (CNES) : add initialization trough reset for Raz, enable and Count_Length
|
11
|
-------------------------------------------------------------------------------------------------
|
12
|
-- File name : STD_04000_good.vhd
|
13
|
-- File Creation date : 2015-04-13
|
14
|
-- Project name : VHDL Handbook CNES Edition
|
15
|
-------------------------------------------------------------------------------------------------
|
16
|
-- Softwares : Microsoft Windows (Windows 7) - Editor (Eclipse + VEditor)
|
17
|
-------------------------------------------------------------------------------------------------
|
18
|
-- Description : Handbook example: State machine case enumeration completion: good example
|
19
|
--
|
20
|
-- Limitations : This file is an example of the VHDL handbook made by CNES. It is a stub aimed at
|
21
|
-- demonstrating good practices in VHDL and as such, its design is minimalistic.
|
22
|
-- It is provided as is, without any warranty.
|
23
|
-- This example is compliant with the Handbook version 1.
|
24
|
--
|
25
|
-------------------------------------------------------------------------------------------------
|
26
|
-- Naming conventions:
|
27
|
--
|
28
|
-- i_Port: Input entity port
|
29
|
-- o_Port: Output entity port
|
30
|
-- b_Port: Bidirectional entity port
|
31
|
-- g_My_Generic: Generic entity port
|
32
|
--
|
33
|
-- c_My_Constant: Constant definition
|
34
|
-- t_My_Type: Custom type definition
|
35
|
--
|
36
|
-- My_Signal_n: Active low signal
|
37
|
-- v_My_Variable: Variable
|
38
|
-- sm_My_Signal: FSM signal
|
39
|
-- pkg_Param: Element Param coming from a package
|
40
|
--
|
41
|
-- My_Signal_re: Rising edge detection of My_Signal
|
42
|
-- My_Signal_fe: Falling edge detection of My_Signal
|
43
|
-- My_Signal_rX: X times registered My_Signal signal
|
44
|
--
|
45
|
-- P_Process_Name: Process
|
46
|
--
|
47
|
-------------------------------------------------------------------------------------------------
|
48
|
|
49
|
library IEEE;
|
50
|
use IEEE.std_logic_1164.all;
|
51
|
use IEEE.numeric_std.all;
|
52
|
|
53
|
entity STD_04000_good is
|
54
|
port (
|
55
|
i_Clock : in std_logic; -- Clock input
|
56
|
i_Reset_n : in std_logic; -- Reset input
|
57
|
i_Start : in std_logic; -- Start counter signal
|
58
|
i_Stop : in std_logic -- Stop counter signal
|
59
|
);
|
60
|
end STD_04000_good;
|
61
|
|
62
|
--CODE
|
63
|
architecture Behavioral of STD_04000_good is
|
64
|
constant c_Length : std_logic_vector(3 downto 0) := (others => '1'); -- How long we should count
|
65
|
type t_state is (init, loading, enabled, finished); -- Enumerated type for state encoding
|
66
|
signal sm_State : t_state; -- State signal
|
67
|
signal Raz : std_logic; -- Load the length value and initialize the counter
|
68
|
signal Enable : std_logic; -- Counter enable signal
|
69
|
signal Count_Length : std_logic_vector(3 downto 0); -- Counter length for counting
|
70
|
signal End_Count : std_logic; -- End signal of counter
|
71
|
begin
|
72
|
-- A simple counter with loading length and enable signal
|
73
|
Counter : Counter
|
74
|
port map (
|
75
|
i_Clock => i_Clock,
|
76
|
i_Reset_n => i_Reset_n,
|
77
|
i_Raz => Raz,
|
78
|
i_Enable => Enable,
|
79
|
i_Length => Count_Length,
|
80
|
o_Done => End_Count
|
81
|
);
|
82
|
|
83
|
-- FSM process controlling the counter. Start or stop it in function of the input (i_Start & i_Stop),
|
84
|
-- load the length value, and wait for it to finish
|
85
|
P_FSM : process(i_Reset_n, i_Clock)
|
86
|
begin
|
87
|
if (i_Reset_n = '0') then
|
88
|
sm_State <= init;
|
89
|
Raz <= '0';
|
90
|
Enable <= '0';
|
91
|
Count_Length <= (others=>'0');
|
92
|
elsif (rising_edge(i_Clock)) then
|
93
|
case sm_State is
|
94
|
when init =>
|
95
|
-- Set the length value
|
96
|
Count_Length <= c_Length;
|
97
|
sm_State <= loading;
|
98
|
when loading =>
|
99
|
-- Load the counter and initialize it
|
100
|
Raz <= '1';
|
101
|
sm_State <= enabled;
|
102
|
when enabled =>
|
103
|
-- Start or stop counting depending on inputs until it finishes
|
104
|
Raz <= '0';
|
105
|
if (End_Count = '0') then
|
106
|
-- The counter has not finished, wait
|
107
|
Enable <= i_Start xor not i_Stop;
|
108
|
sm_State <= Enabled;
|
109
|
else
|
110
|
-- The counter has finished, nothing else to do
|
111
|
Enable <= '0';
|
112
|
sm_State <= finished;
|
113
|
end if;
|
114
|
when others =>
|
115
|
sm_State <= init;
|
116
|
end case;
|
117
|
end if;
|
118
|
end process;
|
119
|
end Behavioral;
|
120
|
--CODE
|