zoukankan      html  css  js  c++  java
  • 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之间随机数的样例:

    reg[23:0]rand;

    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;

           end

          

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

    endmodule

    仿真波形:


  • 相关阅读:
    orcale 多列转一行显示
    orcale 树形结构查询
    orcale 32位guid转36位guid
    orcale 树形结构查询
    win7下安装virtualbox+Ubuntu12.04笔记
    Spring调度器corn表达式详解
    MYSQL之一主多从搭建方案
    None of the configured nodes are available:[{#transport#-1}解决方案
    大数据分批次提交保存
    ORACLE时间类型字段加减简便运算
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5112545.html
Copyright © 2011-2022 走看看