FPGA学习层次化设计例5

层次化设计(例5)

设计目标

输入:A[7:0],B[7:0],C[7:0],D[7:0],SEL[3:0]
功能:SEL=0,P=A+B+C+D;
SEL=1,P=A;
SEL=2,P=B;
SEL=3,P=C;
SEL=4,P=D;
SEL为其他值,P=0;

框图

请添加图片描述

代码

顶层代码TEST5_TOP

module TEST5_TOP (
    input wire [7:0]A,
    input wire [7:0]B,
    input wire [7:0]C,
    input wire [7:0]D,
    input wire [3:0]SEL,
    output wire [26:0]P
);

wire [8:0]add_result;

ADD ADD_1(
    .A(A),
    .B(B),
    .C(C),
    .D(D),
    .add_result(add_result)
);

MUX MUX_1(
    .M1(A),
    .M2(B),
    .M3(C),
    .M4(D),
    .M5(add_result),
    .SEL(SEL),
    .P(P)
);
    
endmodule

ADD模块

 module ADD (
    input wire [7:0]A,
    input wire [7:0]B,
    input wire [7:0]C,
    input wire [7:0]D,
    output wire [8:0] add_result
);
    
assign add_result = A + B + C + D;

endmodule   

MUX模块

 module MUX (
    input wire [7:0]M1,
    input wire [7:0]M2,
    input wire [7:0]M3,
    input wire [7:0]M4,
    input wire [8:0]M5,
    input wire [3:0]SEL,
    output wire [8:0] P
);
    
assign P = (SEL == 4'd0)?M5:(
            (SEL == 4'd1)?M1:(
            (SEL == 4'd2)?M2:(
            (SEL == 4'd3)?M3:(
            (SEL == 4'd4)?M4:
            0))));


endmodule 

tb文件

 `timescale 1ns/1ps

module TEST5_TOP_tb ();

reg [7:0]A;
reg [7:0]B;
reg [7:0]C;
reg [7:0]D;
reg [3:0]SEL;
wire [8:0]P;

initial begin
    A = 8'd1;
    B = 8'd1;
    C = 8'd1;
    D = 8'd1;
    SEL= 4'd1;
end

always #10 A = {$random} % 256;
always #10 B = {$random} % 256;
always #10 C = {$random} % 256;
always #10 D = {$random} % 256;
always #10 SEL = {$random} % 17;


TEST5_TOP TEST5_TOP_1(
    .A(A),
    .B(B),
    .C(C),
    .D(D),
    .SEL(SEL),
    .P(P)
);

endmodule

仿真结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值