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