๐ ์ํ ์ฒ์ด๋
๐ ํ๋ก ์ฝ๋
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์ ๊ฒฝ์ฐ ์ ๋ ฅ ๊ฐ์ด ๋ฐ๋๋ ์๊ฐ ๊ฐ์ด ๋ณํํ๋ ๊ฒ์ ์ ์ ์๋ค.
'Electronic Engeneering > Hardware Description Language' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Verilog ์ํ๊ธฐ ์ค๊ณ (code, tb, RTL ํฉ์ฑํ๋ก, ํํ) (0) | 2024.12.12 |
---|