三段式状态机(附带testbench)

三段式状态机

module state_machine
(
input reset_p,
input clk_10k,
input flag_p,
input [7:0]data,
output out1
);
reg [1:0]cr_state;
reg [1:0]nx_state;
reg [2:0]count;
reg temp_out1;
assign out1=temp_out1;
parameter IDLE = 2'b00,START = 2'b01,WRITE = 2'b10,WRITEEND = 2'b11;
always@(posedge clk_10k)
begin
    if(reset_p)
        cr_state    <=  IDLE;
    else
        cr_state    <=  nx_state;
end

always@*
begin
    case(cr_state)
    IDLE:   begin
                if(flag_p)
                    nx_state    =   START;
                else
                    nx_state    =   IDLE;
            end
    START:  begin
                nx_state    =   WRITE;
            end
    WRITE:  begin
                if(count==3'd0)
                    nx_state    =   WRITEEND;
                else
                    nx_state    =   WRITE;
            end
    WRITEEND:   begin
                    nx_state    =   IDLE;
                end
    default nx_state = IDLE;
    endcase
end

always@(posedge clk_10k)
begin
    case(cr_state)
    IDLE:   begin
                temp_out1   <=  1'b0;
                count       <=  3'd0;
            end
    START:  begin
                temp_out1   <=  1'b0;
                count       <=  3'd7;
            end
    WRITE:  begin
                temp_out1   <=  data[count];
                count       <=  count-3'd1;
            end
    WRITEEND:   begin
                    temp_out1   <=  data[count];
                    count       <=  3'd0;
                end
    default:begin
                temp_out1   <=  temp_out1;
                count       <=  count;
            end
    endcase
end
endmodule

测试程序

module test(

    );
    reg reset_p;
    reg clk_10k;
    reg flag_p;
    reg [7:0]data;
    wire out1;
    initial
    begin
    reset_p=1'b0;
    clk_10k=1'b0;
    flag_p=1'b0;
    data[7:0]=8'b10110001;
    #25 reset_p=1'b1;
    #40 reset_p=1'b0;
    #60 flag_p=1'b0;
    #80 flag_p=1'b1;
    #90 flag_p=1'b0;
    end
    always #10 clk_10k=~clk_10k;
    state_machine state_machine
    (
    .reset_p(reset_p),
    .clk_10k(clk_10k),
    .flag_p(flag_p),
    .data(data),
    .out1(out1)
    );
endmodule
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个比较复杂的Verilog testbench示例,用于测试一个简单的16位ALU模块,该模块包括加、减、与、或等操作。 ``` `timescale 1ns/1ns module alu_tb; // Inputs reg [15:0] a, b; reg [3:0] op; // Outputs wire [15:0] result; // Instantiate the DUT alu dut(.a(a), .b(b), .op(op), .result(result)); // Clock reg clk = 0; // Run the clock always #5 clk = ~clk; // Test cases initial begin // Add operation op = 4'b0000; a = 16'h1234; b = 16'h5678; #10; if(result !== 16'h68AC) $display("Test case 1 failed"); // Subtract operation op = 4'b0001; a = 16'h5678; b = 16'h1234; #10; if(result !== 16'h4444) $display("Test case 2 failed"); // And operation op = 4'b0010; a = 16'hAAAA; b = 16'h5555; #10; if(result !== 16'h0000) $display("Test case 3 failed"); // Or operation op = 4'b0011; a = 16'hAAAA; b = 16'h5555; #10; if(result !== 16'hFFFF) $display("Test case 4 failed"); // Xor operation op = 4'b0100; a = 16'hAAAA; b = 16'h5555; #10; if(result !== 16'hAAAA) $display("Test case 5 failed"); // Shift left operation op = 4'b0101; a = 16'h1234; b = 5; #10; if(result !== 16'h2468) $display("Test case 6 failed"); // Shift right operation op = 4'b0110; a = 16'h1234; b = 5; #10; if(result !== 16'h0489) $display("Test case 7 failed"); // Arithmetic shift right operation op = 4'b0111; a = 16'h8000; b = 1; #10; if(result !== 16'hC000) $display("Test case 8 failed"); // Rotate left operation op = 4'b1000; a = 16'h1234; b = 4; #10; if(result !== 16'h2341) $display("Test case 9 failed"); // Rotate right operation op = 4'b1001; a = 16'h1234; b = 4; #10; if(result !== 16'h4123) $display("Test case 10 failed"); // Overflow test op = 4'b0000; a = 16'h7FFF; b = 16'h0001; #10; if(result !== 16'h8000) $display("Test case 11 failed"); $display("All test cases passed"); $finish; end endmodule ``` 该testbench包括一个时钟生成器、一个ALU模块的实例化、多个测试用例以及一个判断所有测试用例是否通过的语句。每个测试用例都包括对输入信号的设置、等待一段时间、以及对输出结果的检查。如果某个测试用例失败,会输出一个错误信息并终止仿真。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值