HDLBits题目Exams/ece241 2014 q1c:
Assume that you have two 8-bit 2’s complement numbers, a[7:0] and b[7:0]. These numbers are added to produce s[7:0]. Also compute whether a (signed) overflow has occurred.假设您有两个 8 位 2 的补码,a[7:0] 和 b[7:0]。这些数字相加产生 s[7:0]。还要计算是否发生了(有符号的)溢出。
给出输入输出如下:(不区分正负溢出)
module top_module (
input [7:0] a,
input [7:0] b,
output [7:0] s,
output overflow
);
补码加法运算溢出判断三种方法:
[方法一]
Xf、Yf分别两个数的符号位,Zf为运算结果符号位。
当Xf =Yf =0(两数同为正),而Zf=1(结果为负)时,负溢出;
当出现Xf =Yf =1(两数同为负),而Zf=0(结果为正),正溢出.
module top_module (
input [7:0] a,
input [7:0] b,
output [7:0] s,
output overflow
);
assign s=a+b;
assign overflow=((~a[7]