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

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

62

63

64

65

66

67

68

69

70

71

72

73

74

RTL图:

状态转换图:
