在Verilog中自动增加案例状态

时间:2018-08-17 07:48:43

标签: verilog fsm

我想编写一个可综合的状态机,以有序的顺序读取/写入叉骨命令。

当前,我定义了一些Verilog宏:

<a class="btn btn-primary primary-bg btn-lg  col-md-4 m-2 btn-cus" href="">
    <?php foreach($voyage_info as $voyage) {  ?>
        <h3>Voyage <?=$voyage->voyage_number?></h3> 
        <small>Schedule</small>
    <?php }  ?>
</a>

并在我的FSM流程中使用:

`define WB_READ(READ_ADDR) \
        begin \
        wb_addr_o <= UART_DIV;\
        wb_stb_o  <= 1'b1; wb_cyc_o  <= 1'b1; wb_we_o <= 1'b0; end

`define WB_WRITE(WR_ADDR, WVALUE) \
        begin \
        wb_addr_o <= WR_ADDR;\
        wb_wdat_o <= WVALUE;\
        wb_stb_o  <= 1'b1; wb_cyc_o  <= 1'b1; wb_we_o <= 1'b1; end\

`define WB_NOPE \
        begin\
        wb_stb_o  <= 1'b0; wb_cyc_o  <= 1'b0; wb_we_o <= 1'b0; end

每个时间计数为偶数,WB_NOPE状态为“已执行”,每次奇数时,将执行给定的命令。

这在模拟中有效,但是如果我想在状态机的中间添加命令,则必须重新缩进所有{7'hxx,1'b1}状态。并在末尾增加if(count <...)。

有人知道如何(使用宏?)来改进它以避免它吗?

1 个答案:

答案 0 :(得分:1)

您只需在case语句中使用一个整数值,然后为每个步骤递增即可。这是代码的简化版本,可以满足您的要求(或者至少足够接近,您可以对其进行修复:-))

module testcase (input logic clk,input logic rst);

enum logic [1:0] { READ,WRITE,NOP } op;
logic [7:0] count;

`define WB_READ  begin op <= READ; end
`define WB_WRITE begin op <= WRITE; end
`define WB_NOPE  begin op <= NOP; end
logic [6:0] fsm_step_number;

  always @(posedge clk or posedge rst)
    if(rst) begin
      count <= 8'h00; 
      op <= NOP;
    end
    else begin
        fsm_step_number=1;
        case(count)
            {(fsm_step_number++), 1'b1}: `WB_READ
            {(fsm_step_number++), 1'b1}: `WB_READ
            {(fsm_step_number++), 1'b1}: `WB_WRITE
            {(fsm_step_number++), 1'b1}: `WB_WRITE
            {(fsm_step_number++), 1'b1}: `WB_WRITE
            default: `WB_NOPE
        endcase
        if (count < {(fsm_step_number), 1'b1})
            count <= count + 1;
    end

    assert

endmodule