【【简单systyem verilog 语言学习使用二--- 新adder加法器 】】

【【简单systyem verilog 语言学习使用二--- 新adder加法器 】】

adder.v

c 复制代码
module  addernew(
    input                          clk    ,
    input                          rst_n  ,
    input             [2 : 0]      in_a   ,
    input             [2 : 0]      in_b   ,
    input                          sel    ,   // 选择器
    output     reg    [4 : 0]      sum    ,
    output     reg    [4 : 0]      carry
  );


  always@(posedge  clk  or  negedge rst_n )
  begin
    if(rst_n == 0)
    begin
      carry <=  0  ;
      sum   <=  0  ; 
    end
    else
    begin
      if( sel == 0)
      begin
        {carry,sum }  <=  in_a  + in_b  ;
      end
      else
      begin
        {carry,sum }  <=  in_a * in_b  ;
      end
    end
  end

endmodule

adder_tb.sv

c 复制代码
class input_0;
  static int count = 0;
  int id;
  logic [2:0] ina; // 与 in_a 和 in_b 一致的类型
  int arr[5] = '{1, 2, 3, 4, 5};

  // constructor
  function new();
    this.id = count++;
  endfunction

  // task to show values
  task showk();
    foreach (arr[i]) begin
      automatic int k = arr[i];
      $write("%d\n", k);
    end
  endtask

  // function to run with input
  function void run(int a);
    ina = a; // 直接赋值
    $display("ina number is %3d", ina);
  endfunction
endclass

module test();
  input_0 inst_a, inst_b;
  logic clk;
  logic rst_n;
  logic sel;
  logic [2:0] in_a, in_b; // 使用 logic 代替 wire
  logic [4:0] sum, carry;

  // Generate clock
  initial begin
    clk = 0;
    forever #5 clk = ~clk;
  end

  // Adder module instantiation
  addernew uadder (
      .clk(clk),
      .rst_n(rst_n),
      .in_a(in_a), // 从 in_a 获取值
      .in_b(in_b), // 从 in_b 获取值
      .sel(sel),
      .sum(sum),
      .carry(carry)
  );

  initial begin
    rst_n = 0; // 初始化复位信号
    inst_a = new();
    inst_b = new();
    $display("inst_a id = %d", inst_a.id);
    $display("inst_b id = %d", inst_b.id);

    inst_a.showk();
    inst_b.showk();

    #10 rst_n = 1; // 释放复位信号

    sel = 1; // 选择信号
    inst_a.run(3); // 运行 inst_a
    inst_b.run(2); // 运行 inst_b

    // 赋值 in_a 和 in_b
    in_a = inst_a.ina; 
    in_b = inst_b.ina; 

    #105; // 等待一段时间

    // 打印结果
    $display("ina = %3d, inb = %3d, sel = %d, sum = %5d, carry = %5d",
             in_a, in_b, sel, sum, carry);
             $display("time = %t",$time);
  end
endmodule
相关推荐
于小汐在咯5 小时前
词根学习笔记 | Agri系列
笔记·学习
霜绛6 小时前
Unity:Json笔记——Json文件格式、JsonUtlity序列化和反序列化
学习·unity·json·游戏引擎
我命由我123458 小时前
Excel - Excel 列出一列中所有不重复数据
经验分享·学习·职场和发展·word·powerpoint·excel·职场发展
璞致电子8 小时前
fpga开发板ZYNQ 璞致 PZ7010/7020 邮票孔核心板简介-ZYNQ7000系列小系统学习板
linux·嵌入式硬件·学习·fpga开发·fpga·fpga开发板·xilinx开发板
Miki Makimura9 小时前
Reactor 模式实现:从 epoll 到高并发调试
运维·服务器·c++·学习
greatofdream9 小时前
HDLBit 个人记录
fpga开发
jiajixi10 小时前
go-swagger学习笔记
笔记·学习·golang
Mingze031411 小时前
C语言四大排序算法实战
c语言·数据结构·学习·算法·排序算法
东风西巷11 小时前
STranslate(翻译工具OCR工具) 中文绿色版
学习·ocr·电脑·软件需求
程序员东岸11 小时前
学完顺序表后,用 C 语言写了一个通讯录
数据结构·笔记·学习