Pages

Saturday, December 6, 2014

State Machine implementation in verilog : Test bench : Timing diagram

Implementing state machines in verilog is not a big task. It is very easy to implement using case statement in verilog.

Here is the state machine which I'm I'm going to implement using verilog.


This is a very simple state machine which has 4 states. But all the data are is mentioned properly.

*********************Let's look in to the verilog code.**********************************

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
`timescale 1ns / 1ps

module verilogStateMachine(
    input in, //single bit input
    input clk,      //clock
    output out    //single bit output
    );
 
//registers
reg [1:0]r_state = 2'b00;                //state of the state machine, initial state is "00"
reg r_out=0;     //register which holds the value of output 
 
always @ (posedge clk) begin
case(r_state)
2'b00 : begin
if (in==0) begin
r_out <=0;
r_state <= 2'b11;
end
else if (in==1)begin
r_out <= 0;
r_state <= 2'b01;
end
end

2'b01 : begin
if (in==0) begin
r_out <=0;
r_state <= 2'b11;
end
else if (in==1)begin
r_out <= 0;
r_state <= 2'b00;
end
end

2'b10 : begin
if (in==0) begin
r_out <=0;
r_state <= 2'b10;
end
else if (in==1)begin
r_out <= 1;
r_state <= 2'b11;
end
end

2'b11 : begin
if (in==0) begin
r_out <=0;
r_state <= 2'b10;
end
else if (in==1)begin
r_out <= 1;
r_state <= 2'b10;
end
end

endcase  
end
 
assign out = r_out;   //register value is assigned to output 
 
endmodule
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

**************************Following is the Test Bench*********************************

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
`timescale 1ns / 1ps

module TB;

// Inputs
reg in;
reg clk;

// Outputs
wire out;

// Instantiate the Unit Under Test (UUT)
verilogStateMachine uut (
.in(in), 
.clk(clk), 
.out(out)
);
initial begin
clk=0;
in=0;
end
always begin    // a virtual clock is generated for simulation purposes
clk = ~clk ;
#5;
end

always begin   //Initial state is 00. 
in = 1;     //state : 00 => 01
#100;
in = 0;     //state : 01 => 11
#100;
in = 1;     //state : 11 => 10
#100;
in = 0;     //state : 10 = > 10
#100;
end
      
endmodule

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

*****************************Timing Diagram using ISim******************************

To get the full view of the Timing diagram, in ISim : View > Zoom > To Full View



Sunday, October 14, 2012

Books I have read

01. Guru Geethaya - Chinghiz Aitmatov (2009)
02. Jamila - Chinghiz Aitmatov
03. Lebeema - Nolebeema - Chinghiz Aitmatov
04. Siyawasak Pavathna Dawasa - Chinghiz Aitmatov
05. Charitha Thunak - K.Jayathilaka
06. Oliver Twist - Charles Dickens
07. Gehenu Lamai - Karunasena Jayalath
08. Pirunu Sandak - Karunasena Jayalath
09. Madol Duwa - Martin Wickramasinghe(English Translation by Ashely Halpe)
10. Viragaya - Martin Wickramasinghe
11. Thilaka saha Thilaka - T.B.Ilangarathna
12. Ardha navakathawak hewath Manankalpitha Warthawak - K.Jayathilaka
13. Old Man and the Sea - Ernest Hemingway (Sinhala Translation)
14. Farewell Gulsari - Chinghiz Aitmatov (Sinhala Translation)
15. Hapana - T.B. Illangarathne (2014.11.12)