Verilog与C++的类比

本文通过对比Verilog中的module与C++中的class,阐述了Verilog如何实例化module以及模块间如何连接。文章指出,Verilog的端口声明方式类似于早期C语言的函数定义,而在调用时,Verilog允许使用名称绑定,提高了可读性和错误检查能力。此外,文章还讨论了Verilog的测试驱动开发特点和与VHDL在接口与实现分离上的差异,以及Verilog与C++在模板概念上的相似之处。
摘要由CSDN通过智能技术生成

1. Verilog中的module对应C++中的class它们都可以实例化。例如可以写一个FullAdder module,表示全加器这种器件。

 

 

module FullAdder(a, b, cin, sum, cout);

  input a, b, cin;

  output sum, cout;

 

 

  assign {cout, sum} = a + b + cin;

endmodule

 

 

然后在执行8-bit补码加减运算的ALU module中实例化8FullAdder,表示ALU用到了8FullAdder

 

 

module ALU(a, b, result, cout, is_add);

  input[7:0]  a, b;

  input       is_add;

  output[7:0] result;

  output      cout;

 

 

  wire[7:0] b_not = ~b;

  wire[7:0] b_in = is_add ? b : b_not;

  wire[7:0] carry;

 

  assign carry[0] = is_add ? 1'b0 : 1'b1;

 

 

  // module 实例化

  // 8-bit ripple adder

  FullAdder fa0(a[0], b_in[0], carry[0], result[0], carry[1]);

  FullAdder fa1(a[1], b_in[1], carry[1], result[1], carry[2]);

  FullAdder fa2(a[2], b_in[2], carry[2], result[2], carry[3]);

  FullAdder fa3(a[3], b_in[3], carry[3], result[3], carry[4]);

  FullAdder fa4(a[4], b_in[4], carry[4], result[4], carry[5]);

  FullAdder fa5(a[5], b_in[5], carry[5], result[5], carry[6]);

  FullAdder fa6(a[6], b_in[6], carry[6], result[6], carry[7]);

  FullAdder fa7(a[7], b_in[7], carry[7], result[7], cout);

endmodule

 

 

对应在C++

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值