1
module SampleLogicRealTime(
2
input rst_n,
3
input iclk,
4
input trig,
5
output oclk
6
);
7
8
parameter IDLE = 2'b01,
9
SAMPLE = 2'b10;
10
11
parameter LENGTH = 9'd300;
12
13
reg [1:0] current_state, //state
14
next_state;
15
16
reg r_data_in0, //detect the risingedge reg
17
r_data_in1,
18
o_rising_edge;
19
20
reg [8:0] count; //count for delay
21
reg count_rst_n;
22
23
// sequential circuit
24
always@(posedge iclk, negedge rst_n) begin
25
if (!rst_n) begin
26
current_state <= IDLE;
27
end
28
else begin
29
current_state <= next_state;
30
end
31
end
32
// combinational circuit for state logic
33
always@(current_state,count,o_rising_edge) begin
34
next_state = IDLE;
35
36
case (current_state)
37
IDLE : next_state = o_rising_edge ? SAMPLE : IDLE;
38
SAMPLE : next_state = (count >= LENGTH) ? IDLE : SAMPLE;
39
endcase
40
end
41
42
assign oclk = (count_rst_n ==1'b0)? 1'b0 : iclk;
43
// combinational circuit for output logic
44
always@(current_state,iclk) begin
45
46
case (current_state)
47
IDLE : begin
48
count_rst_n <= 1'b0;
49
end
50
SAMPLE : begin
51
count_rst_n <= 1'b1;
52
end
53
endcase
54
end
55
56
//detect the rising edge
57
always@(posedge iclk) begin
58
r_data_in0 <= r_data_in1;
59
r_data_in1 <= trig;
60
end
61
always@(r_data_in0,r_data_in1) begin
62
o_rising_edge = ~r_data_in0 & r_data_in1; //o_rising_edge output
63
end
64
65
//counter
66
always@(posedge iclk) begin
67
if(~count_rst_n)
68
count <= 9'b0_0000_0000;
69
else
70
count <= count + 1'b1;
71
end
72
73
endmodule
74
module SampleLogicRealTime(2
input rst_n,3
input iclk,4
input trig,5
output oclk6
);7
8
parameter IDLE = 2'b01,9
SAMPLE = 2'b10;10
11
parameter LENGTH = 9'd300;12

13
reg [1:0] current_state, //state14
next_state;15
16
reg r_data_in0, //detect the risingedge reg17
r_data_in1,18
o_rising_edge;19
20
reg [8:0] count; //count for delay 21
reg count_rst_n;22

23
// sequential circuit24
always@(posedge iclk, negedge rst_n) begin25
if (!rst_n) begin26
current_state <= IDLE;27
end28
else begin29
current_state <= next_state;30
end31
end32
// combinational circuit for state logic33
always@(current_state,count,o_rising_edge) begin34
next_state = IDLE;35
36
case (current_state)37
IDLE : next_state = o_rising_edge ? SAMPLE : IDLE;38
SAMPLE : next_state = (count >= LENGTH) ? IDLE : SAMPLE;39
endcase40
end41

42
assign oclk = (count_rst_n ==1'b0)? 1'b0 : iclk;43
// combinational circuit for output logic44
always@(current_state,iclk) begin45
46
case (current_state)47
IDLE : begin 48
count_rst_n <= 1'b0;49
end50
SAMPLE : begin51
count_rst_n <= 1'b1;52
end53
endcase 54
end55
56
//detect the rising edge57
always@(posedge iclk) begin58
r_data_in0 <= r_data_in1;59
r_data_in1 <= trig;60
end61
always@(r_data_in0,r_data_in1) begin62
o_rising_edge = ~r_data_in0 & r_data_in1; //o_rising_edge output63
end64

65
//counter66
always@(posedge iclk) begin67
if(~count_rst_n)68
count <= 9'b0_0000_0000;69
else70
count <= count + 1'b1;71
end72

73
endmodule74

RTL图:

状态转换图: