summaryrefslogtreecommitdiff
path: root/part_4/mylib/pulse_gen.v
blob: d82fe49647d31a8fd26401dca2eb4d4556431fd5 (plain)
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
//------------------------------
// Module name: pulse_gen (Moore)
// Function: Generate one clock pulse on +ve edge of input
// Creator:  Peter Cheung
// Version:  1.0
// Date:     29 Jan 2014
//------------------------------

module pulse_gen(pulse, in, clk);

	output 			pulse;	// output pulse lasting one clk cycle
	input 			in;		// input, +ve edge to be detected
	input 			clk;		// clock signal

	reg [1:0]	state;
	reg	pulse;
	
	parameter	IDLE = 2'b0;	// state coding for IDLE state
	parameter	IN_HIGH = 2'b01;
	parameter	WAIT_LOW = 2'b10;  
	
	initial state = IDLE;
	
	always @ (posedge clk) 
		begin	
			pulse <= 0;		 // default output
			case (state)
			IDLE: 	if (in == 1'b1) begin
							state <= IN_HIGH;	pulse <= 1'b1; end
						else 
							state <= IDLE; 
			IN_HIGH: if (in == 1'b1) 
							state <= WAIT_LOW;
						else 
							state <= IDLE; 
			WAIT_LOW: if (in == 1'b1) 
							state <= WAIT_LOW;
						 else  
							state <= IDLE;
			default: ;
			endcase
		end			//... always
endmodule