Verilog 中的进位检测
Carry-out bit detection in Verilog
刚开始学习Verilog。无论如何,我想不通为什么我的代码没有将溢出输出到进位位cout
。所以,加上8'h~42 + 8'h45 + 1'b1 = 9'h103。但是,我似乎无法在 cout
中得到溢出(如下面的代码所示)。第 9 位被截断。我得到的只是 003(进位位仍然是 0),而不是 103。不知道我哪里出错了。希望有人能帮忙!
注意 - Matrix_A
和 Matrix_B
十六进制值是从 COE 文件中获取的,分别为 42 和 45。
wire [0:0] cout;
wire [7:0] matrix_A;
wire [7:0] matrix_B;
wire [7:0] matrix_BA;
assign {cout,matrix_BA} = (~matrix_A + matrix_B + 1'b1);
在 Edaplayground 测试。我看不出有什么不妥......
编辑:实际上,我的例子和你的有点不同。重写设计和测试台给了我一个提示:当你在 assign
中添加三个组件时,它们都被“提升”到 9 位,但是当你取反 matrix_A
时,提升的位也被否定了。
~42h + 45h + 1 = ~01000010 + 01000101 + 1 = (promoted to 9 bits) =
~001000010 + 001000101 + 000000001 = (applying negation) =
110111101 + 001000101 + 000000001 = 1000000011 (a 10 bit result, which is
truncated to 9 bits) = 000000011 , making your carry to be 0.
试试这个:
assign {cout,matrix_BA} = {1'b0, ~matrix_A} + {1'b0, matrix_B} + 9'b1;
你可以在这里自己测试一下:https://www.edaplayground.com/x/iGQP
刚开始学习Verilog。无论如何,我想不通为什么我的代码没有将溢出输出到进位位cout
。所以,加上8'h~42 + 8'h45 + 1'b1 = 9'h103。但是,我似乎无法在 cout
中得到溢出(如下面的代码所示)。第 9 位被截断。我得到的只是 003(进位位仍然是 0),而不是 103。不知道我哪里出错了。希望有人能帮忙!
注意 - Matrix_A
和 Matrix_B
十六进制值是从 COE 文件中获取的,分别为 42 和 45。
wire [0:0] cout;
wire [7:0] matrix_A;
wire [7:0] matrix_B;
wire [7:0] matrix_BA;
assign {cout,matrix_BA} = (~matrix_A + matrix_B + 1'b1);
在 Edaplayground 测试。我看不出有什么不妥......
编辑:实际上,我的例子和你的有点不同。重写设计和测试台给了我一个提示:当你在 assign
中添加三个组件时,它们都被“提升”到 9 位,但是当你取反 matrix_A
时,提升的位也被否定了。
~42h + 45h + 1 = ~01000010 + 01000101 + 1 = (promoted to 9 bits) =
~001000010 + 001000101 + 000000001 = (applying negation) =
110111101 + 001000101 + 000000001 = 1000000011 (a 10 bit result, which is
truncated to 9 bits) = 000000011 , making your carry to be 0.
试试这个:
assign {cout,matrix_BA} = {1'b0, ~matrix_A} + {1'b0, matrix_B} + 9'b1;
你可以在这里自己测试一下:https://www.edaplayground.com/x/iGQP