Electronic Engeneering/Hardware Description Language

Mealy FSM 회둜 (μ—°μ†λœ 0λ˜λŠ” 1 μž…λ ₯ κ²€μΆœκΈ°)

κ΅ λ―Ό 2024. 12. 13. 14:45

πŸ“„ μƒνƒœ μ²œμ΄λ„

πŸ“„ 회둜 μ½”λ“œ

module seq_det_mealy(clk, rst, din_bit, dout_bit, state_reg, next_state);
	input clk, rst, din_bit; // din_bit : μž…λ ₯κ°’
	output dout_bit; // 좜λ ₯κ°’. : 1μ΄λ‚˜ 0이 μ—°μ†μœΌλ‘œ λ“€μ–΄μ˜€λ©΄ 1을, μ•„λ‹Œ 경우 0을 좜λ ₯ν•œλ‹€.
	output reg [2:0] state_reg, next_state;
	
	// μƒνƒœ μ„ μ–Έ
	parameter start = 3'b000;
	parameter rd0_once = 3'b001;
	parameter rd1_once = 3'b010;
	parameter rd0_twice = 3'b011;
	parameter rd1_twice = 3'b100;
	
	// Next State Logic
	always @(state_reg or din_bit) begin
		case(state_reg)
			start : if			(din_bit==0) next_state <= rd0_once;
					  else if	(din_bit==1) next_state <= rd1_once;
					  else						 next_state <= start;
			rd0_once : if		 (din_bit==0) next_state <= rd0_twice;
						  else if (din_bit==1) next_state <= rd1_once;
						  else					  next_state <= start;
			rd1_once : if		 (din_bit==0) next_state <= rd0_once;
						  else if (din_bit==1) next_state <= rd1_twice;
						  else   				  next_state <= start;
			rd0_twice : if		 (din_bit==0) next_state <= rd0_twice;
						  else if (din_bit==1) next_state <= rd1_once;
						  else   				  next_state <= start;
			rd1_twice : if		 (din_bit==0) next_state <= rd0_once;
						  else if (din_bit==1) next_state <= rd1_twice;
						  else   				  next_state <= start;
			default : 							  next_state <= start;
		endcase
	end
	
	//State Register : 클락이 λ“€μ–΄μ˜¬ λ•Œ λ‹€μŒ μƒνƒœκ°’μ„ λ„˜κ²¨μ£ΌλŠ” 것.
	always @(posedge clk or posedge rst) begin
		if(rst==1) state_reg <= start;
		else 		  state_reg <= next_state;
	end
	
	//Output Logic
	assign dout_bit = (((state_reg == rd0_twice) && (din_bit == 0) || (state_reg == rd1_twice) && (din_bit ==1))) ? 1 : 0;
endmodule

 

πŸ“„ Test Bench μ½”λ“œ

module tb_seq_det_mealy;

	// Inputs
	reg clk;
	reg rst;
	reg din_bit;

	// Outputs
	wire dout_bit;
	wire [2:0] state_reg, next_state;

	// Instantiate the Unit Under Test (UUT)
	seq_det_mealy uut (
		.clk(clk), 
		.rst(rst), 
		.din_bit(din_bit), 
		.dout_bit(dout_bit),
		.state_reg(state_reg),
		.next_state(next_state)
	);
	
	always #5 clk = ~clk;

	initial begin
		clk = 0;
		rst = 1;
		din_bit = 0;
		
		#10 rst = 0;
		
		 // Test 1: Single 0, Single 1
        #10 din_bit = 0; // Start with 0
        #10 din_bit = 1; // Change to 1

        // Test 2: Two consecutive 0s
        #10 din_bit = 0; 
        #10 din_bit = 0; 

        // Test 3: Two consecutive 1s
        #10 din_bit = 1; 
        #10 din_bit = 1; 

        // Test 4: Mixed sequence (0 -> 1 -> 0 -> 0 -> 1 -> 1 -> 0)
        #10 din_bit = 0;
        #10 din_bit = 1;
        #10 din_bit = 0;
        #10 din_bit = 0;
        #10 din_bit = 1;
        #10 din_bit = 1;
        #10 din_bit = 0;

        // End simulation
        #20 $stop;
	end
endmodule

 

πŸ“„ 좜λ ₯ νŒŒν˜•

 

✏️ μ•Œκ²Œ 된 것

dout_bit의 좜λ ₯이 posedge clk이 λ“€μ–΄μ˜€λŠ” 타이밍이 μ•„λ‹Œλ° λ°”λ€ŒλŠ” 것을 보고 의문이 λ“€μ—ˆλŠ”λ°, 그건 Mealy λ¨Έμ‹ μ˜ ꡬ쑰적인 νŠΉμ„± λ•Œλ¬Έμ΄λΌκ³  ν•œλ‹€.

*Mealy μƒνƒœ 머신은 좜λ ₯이 ν˜„μž¬ μƒνƒœμ™€ μž…λ ₯에 μ˜ν•΄ κ²°μ •λ˜κΈ° λ•Œλ¬Έμ— μž…λ ₯ 값이 λ°”λ€Œλ©΄ 클럭 μ‹ ν˜Έμ™€ 상관없이 μ¦‰μ‹œ 좜λ ₯에 영ν–₯을 μ€€λ‹€.

→ μ„€κ³„ν•œ 회둜λ₯Ό 보면 posedge clk 타이밍에 맞좰 λ³€ν™”ν•˜λŠ” 것은 state_reg(ν˜„μž¬ μƒνƒœ) 값이고, next_state의 경우 μž…λ ₯ 값이 λ°”λ€ŒλŠ” μˆœκ°„ 값이 λ³€ν™”ν•˜λŠ” 것을 μ•Œ 수 μžˆλ‹€.