1
+ -- clock div
2
+ library ieee;
3
+ use ieee.std_logic_1164.all ;
4
+ use ieee.numeric_std.all ;
5
+
6
+ entity clock_div is
7
+ generic (clk_in_freq : natural := 4 ;
8
+ clk_out_freq : natural := 2 );
9
+ port (
10
+ clk_in : in std_logic ;
11
+ clk_out : out std_logic ;
12
+ rst : in std_logic ;
13
+ enable : in std_logic ;
14
+ bytes : in std_logic_vector (3 downto 0 );
15
+ polarity : in std_logic ;
16
+ clk_active : out std_logic ;
17
+ byte_flag : out std_logic
18
+ );
19
+ end clock_div ;
20
+
21
+ architecture bhv of clock_div is
22
+
23
+
24
+ constant max : natural := clk_in_freq / clk_out_freq / 2 ;
25
+ signal count, lastByteCount : integer range 0 to max* 2 ;
26
+ signal clock_signal : std_logic ;
27
+ signal byte_count : std_logic_vector (3 downto 0 );
28
+ signal bit_count : integer range 0 to 8 ;
29
+
30
+
31
+
32
+
33
+ begin
34
+
35
+ process (clk_in, rst)
36
+ begin
37
+ if (rst = '1' or enable = '0' ) then
38
+ count <= 0 ;
39
+ lastByteCount <= 0 ;
40
+ clk_active <= '0' ;
41
+ clock_signal <= polarity;
42
+ bit_count <= 0 ;
43
+ byte_count <= (others => '0' );
44
+ byte_flag <= '0' ;
45
+
46
+ elsif (clk_in'event and clk_in = '1' ) then
47
+ if (enable = '1' and byte_count < bytes) then
48
+
49
+ clk_active <= '1' ;
50
+ if (count = max- 1 ) then
51
+ count <= 0 ;
52
+ clock_signal <= not clock_signal;
53
+
54
+ if ((not clock_signal) = polarity) then
55
+ -- increment bit count
56
+ -- increment byte count after 8 bits and clean bit count
57
+ -- clear byte count after numOfBytes and clear active_clk
58
+
59
+ byte_flag <= '0' ; -- clear the byte flag
60
+
61
+ if (bit_count = 7 ) then
62
+ byte_count <= std_logic_vector (unsigned (byte_count) + 1 );
63
+ bit_count <= 0 ;
64
+ byte_flag <= '1' ; -- set byte flag for one sclk cycle
65
+
66
+ if ( byte_count = std_logic_vector (unsigned (bytes) - 1 )) then
67
+ -- clk_active <= '0';
68
+ -- clock_signal <= clock_signal; --do not toggle clock
69
+
70
+ end if ;
71
+
72
+
73
+ else
74
+ bit_count <= bit_count + 1 ;
75
+ end if ;
76
+
77
+ -- if( byte_count = bytes) then
78
+ -- clk_active <= '0';
79
+ -- end if;
80
+
81
+ end if ;
82
+ else
83
+ count <= count + 1 ;
84
+ end if ;
85
+
86
+
87
+ elsif (enable = '1' ) then
88
+
89
+ clk_active <= '1' ;
90
+ if (lastByteCount = max- 1 ) then
91
+ lastByteCount <= 0 ;
92
+ clk_active <= '0' ;
93
+ else
94
+ lastByteCount <= lastByteCount + 1 ;
95
+ end if ;
96
+ end if ;
97
+
98
+
99
+
100
+ end if ;
101
+
102
+
103
+ -- if(rst = '1' or enable = '0') then
104
+ -- count <= 0;
105
+ -- lastByteCount <= 0;
106
+ -- clk_active <= '0';
107
+ -- clock_signal <= polarity;
108
+ -- bit_count <= 0;
109
+ -- byte_count <= (others => '0');
110
+ -- byte_flag <= '0';
111
+ --
112
+ -- elsif(rising_edge(clk_in) and enable = '1' and byte_count < bytes) then
113
+ --
114
+ -- clk_active <= '1';
115
+ -- if (count = max-1) then
116
+ -- count <= 0;
117
+ -- clock_signal <= not clock_signal;
118
+ --
119
+ -- if((not clock_signal) = polarity) then
120
+ -- --increment bit count
121
+ -- --increment byte count after 8 bits and clean bit count
122
+ -- --clear byte count after numOfBytes and clear active_clk
123
+ --
124
+ -- byte_flag <= '0'; -- clear the byte flag
125
+ --
126
+ -- if(bit_count = 7) then
127
+ -- byte_count <= std_logic_vector(unsigned(byte_count) + 1);
128
+ -- bit_count <= 0;
129
+ -- byte_flag <= '1'; --set byte flag for one cycle
130
+ --
131
+ -- if( byte_count = std_logic_vector(unsigned(bytes) - 1)) then
132
+ -- --clk_active <= '0';
133
+ -- --clock_signal <= clock_signal; --do not toggle clock
134
+ --
135
+ -- end if;
136
+ --
137
+ --
138
+ -- else
139
+ -- bit_count <= bit_count + 1;
140
+ -- end if;
141
+ --
142
+ -- -- if( byte_count = bytes) then
143
+ -- -- clk_active <= '0';
144
+ -- -- end if;
145
+ --
146
+ -- end if;
147
+ -- else
148
+ -- count <= count + 1;
149
+ -- end if;
150
+ --
151
+ --
152
+ -- elsif(rising_edge(clk_in) and enable = '1') then
153
+ --
154
+ -- clk_active <= '1';
155
+ -- if (lastByteCount = max-1) then
156
+ -- lastByteCount <= 0;
157
+ -- clk_active <= '0';
158
+ -- else
159
+ -- lastByteCount <= lastByteCount + 1;
160
+ -- end if;
161
+ --
162
+ --
163
+ --
164
+ -- end if;
165
+ -- clk_out <= clock_signal;
166
+ end process ;
167
+
168
+ clk_out <= clock_signal;
169
+
170
+
171
+
172
+
173
+ end bhv ;
0 commit comments