Skip to content

Commit 52fbd20

Browse files
committed
All files
0 parents  commit 52fbd20

8 files changed

+1868
-0
lines changed

accel_driver.vhd

+523
Large diffs are not rendered by default.

clock_div.vhd

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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;

clock_div_tb.vhd

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
--clock_div_tb
2+
library ieee;
3+
use ieee.std_logic_1164.all;
4+
use ieee.numeric_std.all;
5+
6+
entity clock_div_tb is
7+
end clock_div_tb;
8+
9+
10+
architecture TB of clock_div_tb is
11+
12+
13+
-----------------------------------
14+
--signals
15+
-----------------------------------
16+
signal clk50MHz : std_logic := '0';
17+
signal sclk : std_logic;
18+
signal rst : std_logic;
19+
signal enable : std_logic;
20+
signal bytes : std_logic_vector (3 downto 0);
21+
signal polarity : std_logic;
22+
signal clk_active : std_logic;
23+
signal byte_flag : std_logic;
24+
25+
begin
26+
27+
-----------------------------------
28+
--units under test
29+
-----------------------------------
30+
U_CLOCK_DIV : entity work.clock_div
31+
generic map(
32+
clk_in_freq => 50000000,
33+
clk_out_freq => 1000)
34+
port map(
35+
clk_in => clk50MHz,
36+
clk_out => sclk,
37+
rst => rst,
38+
enable => enable,
39+
bytes => bytes,
40+
polarity => polarity,
41+
clk_active => clk_active,
42+
byte_flag => byte_flag);
43+
44+
clk50MHz <= not clk50MHz after 10 ns; --50MHz
45+
46+
47+
48+
process
49+
50+
begin
51+
--reset
52+
rst <= '1';
53+
enable <='0';
54+
bytes <= "0100";
55+
polarity <= '1';
56+
wait for 1 ms;
57+
58+
--go
59+
rst <= '0';
60+
wait for 1 ms;
61+
62+
enable <= '1';
63+
wait for 4 ms;
64+
65+
wait until clk_active = '0';
66+
enable <= '0';
67+
68+
69+
wait for 4 ms;
70+
enable <= '1';
71+
polarity <= '0';
72+
wait for 4 ms;
73+
74+
wait until clk_active = '0';
75+
enable <= '0';
76+
77+
78+
79+
wait for 1 ms;
80+
81+
82+
for i in 0 to 900000000 loop
83+
wait until clk50MHz'event and clk50MHz = '1';
84+
85+
end loop;
86+
87+
88+
89+
90+
91+
92+
end process;
93+
94+
95+
end TB;

decoder7seg.vhd

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--decoder7seg
2+
library ieee;
3+
use ieee.std_logic_1164.all;
4+
5+
entity decoder7seg is
6+
port (
7+
input : in std_logic_vector(3 downto 0);
8+
output : out std_logic_vector(6 downto 0));
9+
end decoder7seg;
10+
11+
architecture BHV of decoder7seg is
12+
13+
begin -- BHV
14+
15+
process(input)
16+
begin
17+
18+
case input is
19+
when "0000" => output <= "1000000";--"0000001";
20+
when "0001" => output <= "1111001";--"1001111";
21+
when "0010" => output <= "0100100";--"0010010";
22+
when "0011" => output <= "0110000";--"0000110";
23+
when "0100" => output <= "0011001";--"1001100";
24+
when "0101" => output <= "0010010";--"0100100";
25+
when "0110" => output <= "0000010";--"0100000";
26+
when "0111" => output <= "1111000";--"0001111";
27+
when "1000" => output <= "0000000";--"0000000";
28+
when "1001" => output <= "0011000";--"0001100";
29+
when "1010" => output <= "0001000";--"0001000";
30+
when "1011" => output <= "0000011";--"1100000";
31+
when "1100" => output <= "1000110";--"0110001";
32+
when "1101" => output <= "0100001";--"1000010";
33+
when "1110" => output <= "0000110";--"0110000";
34+
when "1111" => output <= "0001110";--"0111000";
35+
when others => output <= "XXXXXXX";
36+
end case;
37+
end process;
38+
39+
end BHV;

0 commit comments

Comments
 (0)