verilog编程-想写一个长度可变的adder
时间:10-02
整理:3721RD
点击:
请教大家,我想用en来控制adder。en=0是4 bit的,所以sum[7]~sum[4]全是零。en=1是8bit的。程序和编译的错误在下面,谢谢指教。或者大家有没有见过类似功能的程序,帮忙指导一下。
module fulladder(a, b, cin, sum, cout);
input a, b, cin;
output sum, cout;
assign sum = a ^ b ^ cin;
assign cout = a & b | a & cin | b & cin;
endmodule
module ripple_adder(a, b, sum, cout);
input [7:0] a, b;
output [7:0] sum;
output cout;
wire [7:0] c;
assign c[0]=0;// no carry input in this example
always@(sum[/email] or en)
case (en)
0: {sum[7], sum[6],sum[5],sum[4]}=4'b0000; //编译的时候出错:sum是illegal的变量
1: sum=sum;
endcase
fulladder f0(a[0], b[0], c[0], sum[0], c[1]);
fulladder f1(a[1], b[1], c[1], sum[1], c[2]);
fulladder f2(a[2], b[2], c[2], sum[2], c[3]);
fulladder f3(a[3], b[3], c[3], sum[3], c[4]);
fulladder f4(a[4], b[4], c[4], sum[4], c[5]);
fulladder f5(a[5], b[5], c[5], sum[5], c[6]);
fulladder f6(a[6], b[6], c[6], sum[6], c[7]);
fulladder f7(a[7], b[7], c[7], sum[7], cout);
endmodule
module fulladder(a, b, cin, sum, cout);
input a, b, cin;
output sum, cout;
assign sum = a ^ b ^ cin;
assign cout = a & b | a & cin | b & cin;
endmodule
module ripple_adder(a, b, sum, cout);
input [7:0] a, b;
output [7:0] sum;
output cout;
wire [7:0] c;
assign c[0]=0;// no carry input in this example
always@(sum[/email] or en)
case (en)
0: {sum[7], sum[6],sum[5],sum[4]}=4'b0000; //编译的时候出错:sum是illegal的变量
1: sum=sum;
endcase
fulladder f0(a[0], b[0], c[0], sum[0], c[1]);
fulladder f1(a[1], b[1], c[1], sum[1], c[2]);
fulladder f2(a[2], b[2], c[2], sum[2], c[3]);
fulladder f3(a[3], b[3], c[3], sum[3], c[4]);
fulladder f4(a[4], b[4], c[4], sum[4], c[5]);
fulladder f5(a[5], b[5], c[5], sum[5], c[6]);
fulladder f6(a[6], b[6], c[6], sum[6], c[7]);
fulladder f7(a[7], b[7], c[7], sum[7], cout);
endmodule
你在google里面搜索一下吧,很多文章都又介绍