-
Notifications
You must be signed in to change notification settings - Fork 1
/
alarm.v
59 lines (54 loc) · 1.31 KB
/
alarm.v
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
module alarm (
input clk,
input rst_n,
input enable,
output reg beep
);
reg [27:0] tone;
reg [6:0] ramp;
wire [14:0] clkdriver;
reg [14:0] cnt;
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
tone <= 28'b0;
else begin
if(tone == 28'h FFFFFFF)
tone <= 28'b0;
else
if(enable) tone <= tone + 28'd1;
else tone <= tone;
end
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
ramp <= 7'b0;
else begin
if(tone[26] == 1)
ramp <= tone[25:19];
else
ramp <= ~tone[25:19];
end
end
assign clkdriver = {2'b01,ramp,6'b000000};
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
cnt <= 15'b0;
else begin
if(cnt == clkdriver)
cnt <= 15'b0;
else
if(enable) cnt <= cnt + 15'd1;
else cnt <= cnt;
end
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
beep <= 1'b0;
else begin
if(cnt <= (clkdriver >>> 1))
beep <= 1'b1;
else
beep <= 1'b0;
end
end
endmodule