graphene
Full Member level 2
Dear Friends,
I am a Verilog code for a FSM.
>> Its a simple Car window with 4 states 00,01,10,11(fullyopen, partially closed, partially opened, fully closed). I am sure you can understand.
>>Here I have 2 options, UP or DOWN. and the position with the action cannot be expressed in bubble diagram.
>>UP and DOWN are mutually exclusive events.
>> the start condition and the final condition is reset, which is nothing but fully closed state.
>>In the output here I am unable to get the states changed mov, posi are the variables (registers), I have used in this code.
I am lagging behind in some register initation or wrongly coded loops. Can some one help me out with this ?? I am also pasting the test benches alongside.
: Sorry if you find the code unorthodox.
Thank you for your times.
Here is the test bench
I am a Verilog code for a FSM.
>> Its a simple Car window with 4 states 00,01,10,11(fullyopen, partially closed, partially opened, fully closed). I am sure you can understand.
>>Here I have 2 options, UP or DOWN. and the position with the action cannot be expressed in bubble diagram.
>>UP and DOWN are mutually exclusive events.
>> the start condition and the final condition is reset, which is nothing but fully closed state.
>>In the output here I am unable to get the states changed mov, posi are the variables (registers), I have used in this code.
I am lagging behind in some register initation or wrongly coded loops. Can some one help me out with this ?? I am also pasting the test benches alongside.
: Sorry if you find the code unorthodox.
Thank you for your times.
Code:
module glass(
//win1,win2,//window1 and window2
clk,rst,//clock and reset
up,down,// upward or downward action
val,// input referring the present state of the window
mov,tmp,// output registers
posi,// current position ina 2 bit representation
incr,decr// junk registers
);
input /*win1, win2,*/clk,rst;
input up,down;//up or down
input [1:0] val;
output [1:0] mov;
//reg up, down;
output reg [1:0] posi,incr, decr,tmp;
reg [1:0] mov;// for using in case
parameter [1:0] s1=2'b00, s2=2'b01, s3=2'b10, s4=2'b11;
always@(posedge clk) begin
if (rst) begin
mov<=2'b11;
posi<=2'b11;
incr<=2'b11;
decr<=2'b11;
end else if (down) begin
if (posi == 2'b00) begin
posi=2'b00;
end else begin
decr=decr-1;
posi=decr;
end
end else if (up) begin
if (incr == 2'b11) begin
posi=2'b11;
end else begin
incr=incr+1;
posi=incr;
end
end else begin
posi<=val;
end
case(posi)
00:if(up) begin
mov<=01;
end else begin
mov<=00; end
01:if(up) begin
mov<=10;
end else if (down) begin
mov<=00;
end else
mov<=01;
10:if(up) begin
mov<=11;
end else if (down) begin
mov<=01;
end else
mov<=10;
11:if (down) begin
mov<=10;
end else
mov<=11;
default:mov<=posi;
endcase
//end
end
endmodule
Here is the test bench
Code:
module glass_tb();
//reg tb_win1,tb_win2;
reg tb_clk, tb_rst;
reg [1:0] tb_val;
reg tb_up,tb_down;
wire [1:0] tb_mov,tb_posi,tb_incr,tb_decr,tb_tmp;
initial
begin
fork
tb_clk=0;
tb_rst=0;
tb_val=2'b11;
tb_up=0;
tb_down=0;
join
#20 tb_rst=1;
#20 tb_rst=0;
#20 tb_val=2'b00;
#20 tb_up=1;
//#20 tb_var_down=0;
/*
#20 tb_var_up=1;
#20 tb_var_up=0;
#20 tb_var_up=1;
#20 tb_var_up=1;
*/
#2000 $finish;
end
glass glass_tb(/*.win1(tb_win1),.win2(tb_win2),*/.tmp(tb_tmp),.clk(tb_clk), .rst(tb_rst),.val(tb_val),
/*.var_up(tb_var_up),.var_down(tb_var_down),*/.mov(tb_mov),.posi(tb_posi), .incr(tb_incr),.decr(tb_decr)
);
always begin
#10 tb_clk=~tb_clk;
end
endmodule
Last edited: