Verilog之case語句


verilog設計進階

時間:201456日星期二

 

主要收獲:

1. 學會使用case語句;

2. 學會使用隨機函數$random

 

$random

1. 函數說明:$random函數調用時返回一個32位的隨機數,它是一個帶符號的整形數。

2. 產生0~59之間的隨機數的例子:

reg[23:0] rand;

rand={$random} % 60;

3. 產生一個在min, max之間隨機數的例子:

reg[23:0] rand;

rand = min+{$random}%(max-min+1);

(摘自昔如煙的博客)

 

Verilog程序:

module alu(out, opcode, a, b);

    output[7:0]    out;

    reg[7:0]    out;

    input[2:0]    opcode;

    input[7:0]    a, b;

    

    always@(opcode or a or b) begin

        case(opcode)

            `plus:    out = a + b;

            `minus:    out = a - b;

            `band:    out = a & b;

            `bor:    out = a | b;

            `unegate:out= ~a;

            default: out = 8'hx;

        endcase

    end

endmodule

 

測試程序:

`timescale 1ns/1ns

 

module alutest;

    wire[7:0] out;

    reg [7:0] a, b;

    reg [2:0] opcode;

    parameter times = 5;

    

    initial begin

        a={$random}%256;

        b={$random}%256;

        opcode=3'd0;

        repeat(times) begin

            #100;

            a={$random}%256;

            b={$random}%256;

            opcode=opcode+1;

        end

        #100 $stop;

    end

    

    alu u1(out, opcode, a, b);

endmodule

 

仿真波形:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM