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์˜ ๊ฒฝ์šฐ ์ž…๋ ฅ ๊ฐ’์ด ๋ฐ”๋€Œ๋Š” ์ˆœ๊ฐ„ ๊ฐ’์ด ๋ณ€ํ™”ํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.