天天看點

Verilog之case語句

verilog設計進階

時間:2014年5月6日星期二

主要收獲:

1.學會使用case語句;

2.學會使用随機函數$random。

$random:

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

2.産生0~59之間的随機數的例子:

reg[23:0]rand;

rand={$random}% 60;

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

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

(摘自昔如煙的部落格)

verilog程式:

modulealu(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

測試程式:

`timescale1ns/1ns

modulealutest;

       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;

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

仿真波形:

Verilog之case語句