Verilog数字系统设计四
复杂组合逻辑实验2
前言
随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。以下是本篇文章正文内容,使用门级原语语句设计8位全加器和8为带超前进位的全加器,并写出测试代码进行测试。要求编制测试模块对实现的逻辑功能进行完整的测试。
一、什么是8位全加器和8为带超前进位的全加器?
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
二、编程
1.要求:
使用门级原语语句设计8位全加器和8为带超前进位的全加器,并写出测试代码进行测试。要求编制测试模块对实现的逻辑功能进行完整的测试。
2.门级原语实现8位全加器:
assign代码如下:
/***********ADD_8B**************/
module add(rs,co,ci,a,b);
input a,b,ci;
output rs,co;
xor u1(rs,a,b,ci); //sum
and u2(t0,a,b);
and u3(t1,a,ci);
and u4(t2,b,ci);
or u5(co,t0,t1,t2); //Cin
endmodule
module add_8b(rs,co,ci,a,b);
input [7:0] a,b;
input ci;
output [7:0] rs,co;
add myAdd0(rs[0],co[0],ci,a[0],b[0]);
add myAdd1(rs[1],co[1],co[0],a[1],b[1]);
add myAdd2(rs[2],co[2],co[1],a[2],b[2]);
add myAdd3(rs[3],co[3],co[2],a[3],b[3]);
add myAdd4(rs[4],co[4],co[3],a[4],b[4]);
add myAdd5(rs[5],co[5],co[4],a[5],b[5]);
add myAdd6(rs[6],co[6],co[5],a[6],b[6]);
add myAdd7(rs[7],co[7],co[6],a[7],b[7]);
endmodule
测试代码如下:
module add_8b_tb;
reg [7:0] a_t;
reg [7:0] b_t;
reg ci_t;
wire [7:0] rs_t,co_t;
add_8b myAdd_8b(
.rs(rs_t),
.co(co_t),
.ci(ci_t),
.a(a_t),
.b(b_t));
initial
begin
//out=0
a_t=8'b00000000;
b_t=8'b00000000;
ci_t=1'b0;
//out=2
#20
a_t=8'b00000001;
b_t=8'b00000001;
ci_t=1'b0;
//out=14
#20
a_t=8'b00000111;
b_t=8'b00000111;
ci_t=1'b0;
end
endmodule
测试如下:
仿真如下:
3.门级原语实现带超前进位的8位全加器:
assign代码如下:
module get_carry(co,ci,a,b);
input a,b,ci;
output co;
and u2(g,a,b);
or u3(p,a,b);
and u4(h,p,ci);
or u5(co,g,h);
endmodule
module add_8b_leading(sum,co,ci,a,b);
input [7:0] a,b;
input ci;
output [7:0] co,sum;
//get_all_carry
get_carry myAdd0(co[0],ci,a[0],b[0]);
get_carry myAdd1(co[1],co[0],a[1],b[1]);
get_carry myAdd2(co[2],co[1],a[2],b[2]);
get_carry myAdd3(co[3],co[2],a[3],b[3]);
get_carry myAdd4(co[4],co[3],a[4],b[4]);
get_carry myAdd5(co[5],co[4],a[5],b[5]);
get_carry myAdd6(co[6],co[5],a[6],b[6]);
get_carry myAdd7(co[7],co[6],a[7],b[7]);
//get_result
xor u6 (sum[0],a[0],b[0],ci);
xor u7 (sum[1],a[1],b[1],co[0]);
xor u8 (sum[2],a[2],b[2],co[1]);
xor u9 (sum[3],a[3],b[3],co[2]);
xor u10 (sum[4],a[4],b[4],co[3]);
xor u11 (sum[5],a[5],b[5],co[4]);
xor u12 (sum[6],a[6],b[6],co[5]);
xor u13 (sum[7],a[7],b[7],co[6]);
endmodule
测试代码如下:
module add_8b_leading_tb;
reg [7:0] a_t;
reg [7:0] b_t;
reg ci_t;
wire [7:0] co_t;
wire [7:0] sum_t;
add_8b_leading myAdd_8b_leading(
.sum(sum_t),
.co(co_t),
.ci(ci_t),
.a(a_t),
.b(b_t));
initial
begin
//out=0
a_t=8'b00000000;
b_t=8'b00000000;
ci_t=1'b0;
//out=2
#20
a_t=8'b00000001;
b_t=8'b00000001;
ci_t=1'b0;
//out=15
#20
a_t=8'b00000000;
b_t=8'b00001111;
ci_t=1'b0;
//out=14
#20
a_t=8'b00000111;
b_t=8'b00000111;
ci_t=1'b0;
end
endmodule
测试如下:
仿真如下:
总结
提示:
以上就是今天要分享的内容,本文仅仅简单介绍了Verilog使用门级原语的方法完成8位全加器和8为带超前进位的全加器功能。