Hardware Reference
In-Depth Information
1 //Module header:-----------------------------------------------
2 module car_alarm_with_chirps (
3
input logic remt, sen, clk, rst,
4
output logic siren);
5
6 //Declarations:------------------------------------------------
7
8
//FSM-related declarations:
9
typedef enum logic [3:0] {disarmed, armed, alarm, chirp1,
10
chirp2, chirp3, chirp4, chirp5, wait1, wait2} state;
11
state pr_state, nx_state;
12
13
//Timer-related declarations:
14
const logic [23:0] chirpON = 15_000_000; //0.3s @fclk=50MHz
15
const logic [23:0] chirpOFF = 15_000_000;
16
logic [23:0] t, tmax; --range≥max(chirpON,chirpOFF)
17
18 //Statements:--------------------------------------------------
19
20
//Timer (using strategy #2):
21
always_ff @(posedge clk, posedge rst)
22
if (rst) t <= 0;
23
else if (t < tmax) t <= t + 1;
24
else t <= 0;
25
26
//FSM state register:
27
always_ff @(posedge clk, posedge rst)
28
if (rst) pr_state <= disarmed;
29
else pr_state <= nx_state;
30
31
//FSM combinational logic:
32
always_comb
33
case (pr_state)
34
disarmed: begin
35
siren <= 1'b0;
36
tmax <= 0;
37
if (remt) nx_state <= chirp1;
38
else nx_state <= disarmed;
39
end
40
chirp1: begin
41
siren <= 1'b1;
42
tmax <= chirpON-1;
43
if (t==tmax) nx_state <= wait1;
44
else nx_state <= chirp1;
45
end
46
wait1: begin
47
siren <= 1'b0;
48
tmax <= 0;
49
if (~remt) nx_state <= armed;
50
else nx_state <= wait1;
51
end
52
armed: begin
53
siren <= 1'b0;
54
tmax <= 0;
55
if (sen) nx_state <= alarm;
Search WWH ::




Custom Search