LV7_求两个数的差值
题目来源于牛客网
[牛客网在线编程_Verilog篇_Verilog快速入门 (nowcoder.com)](https://www.nowcoder.com/exam/oj?page=1&tab=Verilog篇&topicId=301)
题目
描述
根据输入信号a,b的大小关系,求解两个数的差值:输入信号a,b为8bit位宽的无符号数。如果a>b,则输出a-b,如果a≤b,则输出b-a。
接口信号图如下:
输入描述:
clk:系统时钟
rst_n:复位信号,低电平有效
a,b:8bit位宽的无符号数
输出描述:文章来源:https://www.toymoban.com/news/detail-615787.html
c:8bit位宽的无符号数文章来源地址https://www.toymoban.com/news/detail-615787.html
代码
`timescale 1ns/1ns
module data_minus(
input clk,
input rst_n,
input [7:0]a,
input [7:0]b,
output reg [8:0]c
);
//*************code***********//
/*代码思路: if(a>b),输出a-b
if(a<=b),输出b-a
简单的if判断
*/
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
c <= 0;
else begin
if(a>b)
c <= a-b;
else if(a<=b)
c <= b-a;
end
end
//*************code***********//
endmodule
到了这里,关于Verilog语法学习——LV7_求两个数的差值的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!