verilog语法基础-算术运算

概述:

虽然算术运算的基本的计算,但是对于计算机器来说,是非常复杂的功能。FPGA能够进行算术运算仅仅是低位的整数运算。其中性能比较好的是加法运算,减法运算,乘法运算,和左移除法运算。其中加法运算和减法运算可以看成一种运算。本节主要讨论简单的算术运算结构。

verilog中算术运算符如下:

复制代码
// The forllowing are the arithmetic operators as defined by the Verilog language.
// 
//    + .... Addition
//    - .... Subtraction
//    * .... Multiplication
//    / .... Divide
//    % .... Modulus
//    ** ... Power Operator (i.e. 2**8 returns 256)

主要内容

  1. 加法运算

2.减法运算

3.乘法运算

4.除法运算

  1. 取模运算

6.幂运算

  1. 左移除法运算

1. 加法运算

代码

复制代码
module assign1(
	input[3:0] a,b,
	output	y1,
	output[4:0]  y2
    );
assign y1 = a[0]+b[0];    // 1位加法器
assign y2 = a+b;    // 4为加法器
endmodule

RTL结构图

技术原理图

2. 减法电路

代码

复制代码
module assign1(
	input[3:0] a,b,
	output	y1,
	output[3:0]  y2
    );
assign y2 = a-b;    // 4为加法器
endmodule

RTL结构图

技术原理图

3.乘法电路

代码

复制代码
module assign1(
	input[3:0] a,b,
	output	y1,
	output[7:0]  y2
    );
assign y2 = a*b;    // 4为加法器
endmodule

RTL结构图

技术原理图,直接使用了18*18的专用乘法器,FPGA内部并没有直接自己重新构造乘法电路

4. 除法电路

错误代码

复制代码
module assign1(
	input[3:0] a,b,
	output	y1,
	output[7:0]  y2
    );
assign y2 = a/b;    
endmodule

FPGA不能直接构造除法电路,否则报错

除以非2^n的数,都会报错

复制代码
module assign1(
	input[3:0] a,b,
	output	y1,
	output[7:0]  y2
    );
assign y2 = a/3;   
endmodule

报错结果

可用代码,只能除以2**n次方

复制代码
module assign1(
	input[3:0] a,b,
	output	y1,
	output[7:0]  y2
    );
assign y2 = a/2;    // 4为加法器
endmodule

RTL结构图,由此可知,就是重新布线

技术原理图

5. 取模运算

代码

复制代码
// 34 取模运算
module assign1(
	input[3:0] a,b,
	output	y1,
	output[7:0]  y2
    );
assign y2 = a%8;    // 4为加法器
endmodule

RTL结构图,取模运算也只支持2**n次方,他的原理就是只保留低位即可。

技术原理图

6. 幂运算**

代码

复制代码
module assign1(
	input[3:0] a,b,
	output	y1,
	output[7:0]  y2
    );
assign y2 = 3**5;    // 4为加法器
endmodule

幂运算仅仅支持常数运算,数值在软件上已经算好了,方便编程,并不会设计一个幂运算电路。

总结

  1. FPGA自己自动构造的算术电路仅仅只有加法电路

  2. 减法电路也是一种加发电路,

  3. FPGA的乘法电路是通过调用专用乘法器来实现的,并不会在综合的过程自动构造乘法电路,乘法电路的资源取决于型号。

  4. 除法和取模运算只支持2**n次方,并且只能进行常数运算,他用过连线的重新排布来实现

  5. 幂预算只能是常数运算,软件直接计算结果,并不会构造具体电路,也不会对线的排布产生影响。主要是为了方便编程。

相关推荐
FPGA小徐6 小时前
数字IC/FPGA设计基础_ILA原理与使用
fpga开发
FPGA小徐8 小时前
FPGA 部署 YOLO 完整指南:从模型量化到 Zynq PS+PL 硬件加速
yolo·fpga开发
dadaobusi13 小时前
学习:chiesel,scala
学习·fpga开发·scala
爱吃汽的小橘14 小时前
异步FIFO核心揭秘:格雷码与指针同步
fpga开发
璞致电子14 小时前
基于 P201Pro | 接收端基础知识——Costas环锁定后为何仍会错?DQPSK与前导码相位校正(6)
经验分享·fpga开发·ad9361·软件无线电·sdr·qpsk·pzsdr
璞致电子1 天前
基于 AD9361 (PZSDR‑P201Pro) 软件无线电实战综述:从板卡选型、硬件平台升级到 GNU Radio QPSK 完整收发链路
经验分享·嵌入式硬件·fpga开发·软件无线电·sdr·璞致电子
炎龙铠甲会合体1 天前
从 SPEC 到上板:在 Zynq-7020 上用 RTL 手写一个 YOLO 卷积加速器(完整教程)
yolo·fpga开发·axi·芯片验证
FPGA小徐1 天前
Vivado 按键消抖与复位模块代码修复详解:KEY_TOP、key、rst_gen 完整实战
fpga开发
炎龙铠甲会合体2 天前
AXI2ICACHE— AXI4-Full 四路组相联指令 Cache 设计与 UVM 验证
fpga开发·面试·芯片验证
bingcheby2 天前
FMQL15/20/45 (复旦微青龙系列 PSOC)新手使用说明
fpga开发