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