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μ κ²½μ° μ λ ₯ κ°μ΄ λ°λλ μκ° κ°μ΄ λ³ννλ κ²μ μ μ μλ€.