1
我需要實現一個32位加法減法器ALU來進行類分配。我有一個1位加法器減法器,工作正常,並且在select語句的幫助下完成了操作(以下給出了所有代碼)。無論如何,我面臨的問題是我無法弄清楚如何使用一個模塊的進位/借出到後續模塊。32位加法減法器ALU使用generate
module add_sub(select, i0, i1, cin, out, cout
);
input i0, i1, select, cin;
output out, cout;
wire y0, y1, y2, y3, y4, y5, y6;
wire z0, z1, z2, z3, z4;
//diff = i0 xor i1 xor cin
//borrow = cin. ~(i1 xor i2) or ~x.y
xor (y0, i1, cin);
xor (y1, i0, y0); //y1=diff or sum as only carry and borrow vary between adder and subtractor circuits
xor (y2, i1, i0);
and (y3, cin, ~y2);
and (y4, ~i0, i1);
or (y6, y5, y4); //y6 = borrow
and (z0, i0, i1);
xor (z1, i0, i1);
and (z2, cin, z1);
or (z3, z0, z2); //z3= carry out for sum
//conditional operator for assigning sum or difference. if select = 0, we add, else subtract
assign out = y1;
assign cout = select ? y6 : z3;
endmodule
該模塊在被下面給出的ALU模塊在一個循環中實例化...
module alu(sel, num1, num2, alu_cin, alu_out, alu_c
);
parameter N = 32;
input sel; //select line for add or sub
input [N-1:0] num1; //two inputs
input [N-1:0] num2;
input alu_cin;
output [N-1:0] alu_out; //32 bit output
output alu_c; // becomes final carry or borrow accordingly
genvar i;
generate for (i=0; i<=N-1; i=i+1)
begin: alu_loop
if (i == 0)
add_sub as_i (sel, num1[i], num2[i], alu_cin, alu_out[i], alu_c);
else
add_sub as_i (sel, num1[i], num2[i], alu_loop[i-1].as_i.cout[i-1], alu_out[i], alu_c);
end
endgenerate
endmodule
在試驗檯的ALU,我給合適的32位的值和選擇值因爲我需要。問題來了
add_sub as_i (sel, num1[i], num2[i], alu_loop[i-1].as_i.cout[i-1], alu_out[i], alu_c);
它說「索引不能應用於標量」。因爲我試圖模擬它。語法檢查完成。
我需要從一位模塊訪問cout,將它作爲cin傳遞給下一個模塊。 alu_c可以被覆蓋,因爲只需要最後一位。
任何幫助,將不勝感激。提前致謝。 :)所有這些都是通過Xilinx ISE通過Verilog模塊完成的。