硬汉嵌入式论坛

 找回密码
 立即注册
查看: 3873|回复: 2
收起左侧

Verilog设计练习进阶(5)-------用always块实现较复杂的组合逻辑电路

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107122
QQ
发表于 2013-1-30 13:03:05 | 显示全部楼层 |阅读模式
此程序实现简单的ALU
`define plus    3'd0
`define minus   3'd1
`define band    3'd2
`define bor     3'd3
`define unegate 3'd4
module  verilog_prj(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)    //电平敏感的 always块
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 1 ns/ 1 ps
module verilog_prj_vlg_tst();
// constants                                          
// general purpose registers
reg eachvec;
// test vector input registers
reg [7:0] a;
reg [7:0] b;
reg [2:0] opcode;
// wires                                               
wire [7:0]  out;
// assign statements (if any)                          
verilog_prj i1 (
// port map - connection between master ports and signals/registers   
    .a(a),
    .b(b),
    .opcode(opcode),
    .out(out)
);
parameter   times=5;
   initial
    begin   
        a={$random}%256;  //Give a radom number blongs to [0,25 5] .
        b={$random}%256;  //Give a radom number blongs to [0,25 5].
        opcode=3'h0;
        repeat(times)
          begin
          #100     a={$random}%256;  //Give a radom number.
                   b={$random}%256;  //Give a radom number.
                   opcode=opcode+1;
          end               
        #100  $stop;            
   end                                                
endmodule
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107122
QQ
 楼主| 发表于 2013-1-30 13:04:19 | 显示全部楼层
verilog_prj.jpg
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107122
QQ
 楼主| 发表于 2013-1-30 13:05:42 | 显示全部楼层
wave.jpg
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-19 04:22 , Processed in 0.178478 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表