-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom.vhd
61 lines (52 loc) · 1.22 KB
/
random.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
LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.std_logic_arith.all;
USE ieee.std_logic_unsigned.all;
ENTITY random IS
PORT(
i_clk : IN STD_LOGIC;
start : IN STD_LOGIC;
random : OUT STD_LOGIC
);
END random;
ARCHITECTURE behavioral OF random IS
SIGNAL r_condition : STD_LOGIC;
SIGNAL counter : STD_LOGIC_VECTOR(3 DOWNTO 0);
SIGNAL clock50Hz : BIT;
COMPONENT clockdiv IS
PORT(
CLK : IN std_logic;
div : integer;
DIVOUT : buffer BIT
);
END COMPONENT;
BEGIN
clkdiv : clockdiv
PORT MAP(
CLK => i_clk,
div => 500000,
DIVOUT => clock50Hz
);
PROCESS (clock50Hz,start)
CONSTANT max_count : STD_LOGIC_VECTOR(3 DOWNTO 0) := "1010";
CONSTANT min_count : STD_LOGIC_VECTOR(3 DOWNTO 0) := "0000";
BEGIN
IF start = '1' THEN
r_condition <= '0';
counter <= min_count;
ELSIF (clock50Hz'EVENT AND clock50Hz = '1') THEN
IF r_condition = '1' THEN
r_condition <= '0';
counter <= counter + 1;
ELSIF r_condition = '0' THEN
IF counter < max_count THEN
counter <= counter + 1;
ELSE
r_condition <= '1';
counter <= min_count;
END IF;
END IF;
END IF;
END PROCESS;
random <= r_condition;
END behavioral;