Question

I have a program written in Verilog and I want to convert it into a FSM automatically. Is this possible (just to visualize it)?

Here is the code :

module pci(reset,clk,frame,irdy,trdy,devsel,idsel,ad,cbe,par,stop,inta,led_out);
    input reset;
    input clk;
    input frame;
    input irdy;
    output trdy;
    output devsel;
    input idsel;
    inout [31:0] ad;
    input [3:0] cbe;
    inout par;
    output stop;
    output inta;
    output [3:0] led_out;

parameter DEVICE_ID = 16'h9500;
parameter VENDOR_ID = 16'h106d;     // Sequent!
parameter DEVICE_CLASS = 24'hFF0000;    // Misc
parameter DEVICE_REV = 8'h01;
parameter SUBSYSTEM_ID = 16'h0001;  // Card identifier
parameter SUBSYSTEM_VENDOR_ID = 16'hBEBE; // Card identifier
parameter DEVSEL_TIMING = 2'b00;    // Fast!

reg [2:0] state;
reg [31:0] data;

reg [1:0] enable;
parameter EN_NONE = 0;
parameter EN_RD = 1;
parameter EN_WR = 2;
parameter EN_TR = 3;

reg memen; // respond to baseaddr?
reg [7:0] baseaddr;
reg [5:0] address;

parameter ST_IDLE = 3'b000;
parameter ST_BUSY = 3'b010;
parameter ST_MEMREAD = 3'b100;
parameter ST_MEMWRITE = 3'b101;
parameter ST_CFGREAD = 3'b110;
parameter ST_CFGWRITE = 3'b111;

parameter MEMREAD = 4'b0110;
parameter MEMWRITE = 4'b0111;
parameter CFGREAD = 4'b1010;
parameter CFGWRITE = 4'b1011;

`define LED
`ifdef LED
reg [3:0] led;
`endif

`undef STATE_DEBUG_LED
`ifdef STATE_DEBUG_LED
assign led_out = ~state;
`else
`ifdef LED
assign led_out = ~led;  // board is wired for active low LEDs
`endif
`endif

assign ad = (enable == EN_RD) ? data : 32'bZ;
assign trdy = (enable == EN_NONE) ? 'bZ : (enable == EN_TR ? 1 : 0);
assign par = (enable == EN_RD) ? 0 : 'bZ;
reg devsel;

assign stop = 1'bZ;
assign inta = 1'bZ;

wire cfg_hit = ((cbe == CFGREAD || cbe == CFGWRITE) && idsel && ad[1:0] == 2'b00);
wire addr_hit = ((cbe == MEMREAD || cbe == MEMWRITE) && memen && ad[31:12] == {12'b0, baseaddr});
wire hit = cfg_hit | addr_hit;

always @(posedge clk)
begin
    if (~reset) begin
        state <= ST_IDLE;
        enable <= EN_NONE;
        baseaddr <= 0;
        devsel <= 'bZ;
        memen <= 0;
`ifdef LED
        led <= 0;
`endif
    end
    else    begin

    case (state)
        ST_IDLE: begin
            enable <= EN_NONE;
            devsel <= 'bZ;
            if (~frame) begin
                address <= ad[7:2];
                if (hit) begin
                    state <= {1'b1, cbe[3], cbe[0]};
                    devsel <= 0;
                    // pipeline the write enable
                    if (cbe[0])
                        enable <= EN_WR;
                end
                else begin
                    state <= ST_BUSY;
                    enable <= EN_NONE;
                end
            end
        end

        ST_BUSY: begin
            devsel <= 'bZ;
            enable <= EN_NONE;
            if (frame)
                state <= ST_IDLE;
        end

        ST_CFGREAD: begin
            enable <= EN_RD;
            if (~irdy || trdy) begin
                case (address)
                    0: data <= { DEVICE_ID, VENDOR_ID };
                    1: data <= { 5'b0, DEVSEL_TIMING, 9'b0,  14'b0, memen, 1'b0};
                    2: data <= { DEVICE_CLASS, DEVICE_REV };
                    4: data <= { 12'b0, baseaddr, 8'b0, 4'b0010 }; // baseaddr + request mem < 1Mbyte
                    11: data <= {SUBSYSTEM_ID, SUBSYSTEM_VENDOR_ID };
                    16: data <= { 24'b0, baseaddr };
                    default: data <= 'h00000000;
                endcase
                address <= address + 1;
            end
            if (frame && ~irdy && ~trdy) begin
                devsel <= 1;
                state <= ST_IDLE;
                enable <= EN_TR;
            end
        end

        ST_CFGWRITE: begin
            enable <= EN_WR;
            if (~irdy) begin
                case (address)
                    4: baseaddr <= ad[19:12];  // XXX examine cbe
                    1: memen <= ad[1];
                    default: ;
                endcase
                address <= address + 1;
                if (frame) begin
                    devsel <= 1;
                    state <= ST_IDLE;
                    enable <= EN_TR;
                end
            end
        end

        ST_MEMREAD: begin
            enable <= EN_RD;
            if (~irdy || trdy) begin
                case (address)
`ifdef LED
                    0: data <= { 28'b0, led };
`endif
                    default: data <= 'h00000000;
                endcase
                address <= address + 1;
            end
            if (frame && ~irdy && ~trdy) begin
                devsel <= 1;
                state <= ST_IDLE;
                enable <= EN_TR;
            end
        end

        ST_MEMWRITE: begin
            enable <= EN_WR;
            if (~irdy) begin
                case (address)
`ifdef LED
                    0: led <= ad[3:0];
`endif
                    default: ;
                endcase
                address <= address + 1;
                if (frame) begin
                    devsel <= 1;
                    state <= ST_IDLE;
                    enable <= EN_TR;
                end
            end
        end

    endcase
    end
end
endmodule

If there is no automatic way, could you explain a way of doing this?

Here is an FSM made with hand but can't test so ...

fsm

Does it seem ok?

Was it helpful?

Solution 2

It is sometimes easier to write the code and have the documentation generated from that. Sometimes you inherit legacy code without documentation, in these situations especially if new to a language tools to help visualise what is happening can be quite useful.

With cadence tools you can run your code with 'code coverage' then imc can load the coverage data and run FSM Analysis.

I have included a simple FSM below and show the generated state diagram.

module simple_fsm(); 
  //Inputs to FSM
  logic clk;
  logic rst_n;

  logic [1:0] state    ;
  logic [1:0] nextstate;
  logic       turn_on  ;
  logic       turn_off ;

  localparam  S_OFF    = 2'b00;
  localparam  S_GO_ON  = 2'b01;
  localparam  S_ON     = 2'b10;
  localparam  S_GO_OFF = 2'b11;

  // State FlipFlop
  always @(posedge clk or negedge rst_n) begin
    if (~rst_n) begin
      state <= 2'b0;
    end
    else begin
      state <= nextstate;
    end
  end

  //Nextstate Logic
  always @* begin
    case (state)
      2'd0 : if (turn_on) begin
        nextstate = S_GO_ON;
      end
      2'd1 : nextstate = S_ON;
      2'd2 : if (turn_off) begin
        nextstate = S_GO_OFF ;
      end
      2'd3 : nextstate = S_OFF;
    endcase
  end    

//TB clk
initial begin
 #1ns;
 clk = 0;
 forever begin
  #20ns;
  clk = ~clk;
 end
end

//The Test
initial begin
  rst_n    = 1'b0;
  turn_on  = 1'b0;
  turn_off = 1'b0;
  @(posedge clk);
  @(posedge clk);
  rst_n = 1'b1 ;

  @(posedge clk);
  turn_on = 1'b1;
  @(posedge clk);
  turn_on = 1'b0;

  @(posedge clk);
  @(posedge clk);
  #100ms;

  $finish();
end
endmodule

Execute with :

$ irun simple_fsm.sv -coverage all -covdut simple_fsm
$ imc &

Load cov_work (folder created by above simulation) in imc, select simple_fsm and choose FSM Analysis.

simple_fsm visualisation

imc also helps to visualise your test coverage as well. Arcs and states that have not been hit are shown in red.

We have seen that there are some tools which can visualise the FSM, another part of the question is; is the syntax of the purposed FSM suitable for these tools.

@vermaete has reported that Modelsim SE can not see the FSM. From imc I get :

OPs FSM from Question

Which does not seem to cover the complexity of the code, and is shown as only having 2 reachable states, IDLE and BUSY. I would recommend if OP is going down the route of using tools to visualise, adopt a simpler (syntax) FSM structure so that the tools can parse it better.

OTHER TIPS

The better and expensive simulators can detect FSM's in the code and make a visualization of it. E.g. the Modelsim SE version. These can be nice to understand code and check the coveage. But making you're own drawing of a 6-state FSM is not that hard.

The way to check if it's OK is to write a simulation and check that the behaviour is what you want. There is no point getting a bubble diagram out and seeing if it matches your hand-drawn one, as you have no way of knowing if your hand-drawn diagram is correct...

case(segmentRead)
            //-------------------
            SEGMENT0: begin
                READ_Ready_EEPROM <= 1'b0;
                READ_RDSR_Enable <= 1'b0;
                Read_Enable <= 1'b0;
                READ_RDSR_DATA_REG <= 8'b0;
//              READ_DATA_REG <= 8'b0;
            end 
            //-------------------
            SEGMENT2: begin          
                READ_RDSR_Enable <= 1'b1;
                READ_RDSR_DATA_REG <= 8'b0;
            end
//          //-------------------
            SEGMENT3: begin          
                READ_RDSR_Enable <= 1'b0;   
                READ_RDSR_DATA_REG <= RDSR_Data;
            end
            //-------------------
            SEGMENT4: begin
                Read_Enable <= 1'b1;
            end
            //-------------------
            SEGMENT5: begin
                Read_Enable <= 1'b0;
                READ_DATA_REG <= Read_Data;
            end
            //-------------------
            SEGMENT6: begin
                READ_Ready_EEPROM <= 1'b1;
            end
            //-------------------
        endcase
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top