module FullAdder (a, b, ci, r, co);
input a, b, ci;
output r, co;
assign r = a ^ b ^ ci;
assign co = (a&b) | ((a^b)&ci);
endmodule // FullAdder
module adder_4bit (A, B, ci, R, co);
input [3:0] A, B; // [MSB:LSB]
input ci;
output [3:0] R;
output co;
wire [2:0] c;
// FullAdder i0 (.a(A[0]), .b(B[0]), .ci(ci), .r(R[0]), .co(c[0]));
// FullAdder i1 (.a(A[1]), .b(B[1]), .ci(c[0]), .r(R[1]), .co(c[1]));
// FullAdder i2 (.a(A[2]), .b(B[2]), .ci(c[1]), .r(R[2]), .co(c[2]));
// FullAdder i3 (.a(A[3]), .b(B[3]), .ci(c[2]), .r(R[3]), .co(co));
FullAdder i[3:0] (.a(A[3:0]), .b(B[3:0]), .ci({c[2:0],ci}), .r(R[3:0]), .co({co,c[2:0]}));
endmodule // adder_4bit
module adder_nbit (a, b, m, s, co, v);
parameter N = 32; // 32 bit subtractor
input [N-1:0] a, b;
input m;
output [N-1:0] s;
output co, v;
wire [N/4-1:0] c;
wire [N-1:0] bi;
genvar i;
assign bi[0] = b[0] ^ m;
assign bi[1] = b[1] ^ m;
assign bi[2] = b[2] ^ m;
assign bi[3] = b[3] ^ m;
adder_4bit add0(.A(a[3:0]), .B(bi[3:0]), .ci(m), .R(s[3:0]), .co(c[0]));
generate
for (i=1; i<=N/4-1; i=i+1) begin:bit
assign bi[i*4] = b[i*4] ^ m;
assign bi[i*4+1] = b[i*4+1] ^ m;
assign bi[i*4+2] = b[i*4+2] ^ m;
assign bi[i*4+3] = b[i*4+3] ^ m;
adder_4bit add(.A(a[i*4+3:i*4]), .B(bi[i*4+3:i*4]), .ci(c[i-1]), .R(s[i*4+3:i*4]), .co(c[i]));
end
endgenerate
assign co = c[N/4-1]; // carry
assign v = c[N/4-2] ^ co; // overflow
endmodule // adder_nbit
`timescale 1ns/1ns
`include "FullAdder.v"
`include "adder_4bit.v"
`include "adder_nbit.v"
module subtractor_nbit_tb;
parameter N = 32;
reg [N-1:0] A, B;
reg M;
wire [N-1:0] S;
wire CO, V;
adder_nbit i0 (.a(A), .b(B), .m(M), .s(S), .co(CO), .v(V));
initial
begin
$dumpfile ("subtractor_nbit_tb.vcd");
$dumpvars(0, subtractor_nbit_tb);
M=1; // subtractor
A=0;
B=0;
#10 A=302;
#10 B=100;
#10 A=400;
#10 B=203;
#10 $finish;
end // initial begin
endmodule // adder_4bit_tb
我正在制作一个 n 位减法器和一个用 Fulladder 制作的 4 位加法器。我将参数 N 放入 n 位减法器。根据这个 N 值,它被更改为 32、36 或 40 位减法器。
当我运行模拟时,出现'compilation exited abnormally with code 255'错误。代码有什么问题?
我尝试改变b和m的异或方法和改变
for
循环的范围,但我总是得到同样的错误。
该错误消息不是很具体。如果它来自 emacs,那么直接在模拟器上运行代码。
既然你正在使用
iverilog
,我们可以在EDA Playground上运行它。但是,我们仍然看到一条通用错误消息。
但是,在 Cadence 模拟器上运行时,错误更有意义:
for (i=1; i<=N/4-1; i=i+1) begin:bit
|
xmvlog: *E,FNDKWD (testbench.sv,47|40): A SystemVerilog keyword was found where an identifier was expected.
修复此错误的一种方法是将
bit
更改为另一个名称,例如 bits
。这允许代码编译没有错误。
如果您在模拟器中禁用 SystemVerilog 功能,则原始代码可以干净地编译。例如,在没有
iverilog
选项的情况下运行 -g2012
。但是,最好编写在启用 SV 功能的情况下编译的代码。