题解 | #使用子模块实现三输入数的大小比较#
使用子模块实现三输入数的大小比较
https://www.nowcoder.com/practice/bfc9e2f37fe84c678f6fd04dbce0ad27
`timescale 1ns/1ns
module main_mod(
input clk,
input rst_n,
input [7:0]a,
input [7:0]b,
input [7:0]c,
output [7:0]d
);
wire [7:0]e,f;
son_module ab_compare
(
.clk(clk),
.rst_n(rst_n),
.a(a),
.b(b),
.c(e)
);
son_module ac_compare
(
.clk(clk),
.rst_n(rst_n),
.a(a),
.b(c),
.c(f)
);
son_module ef_compare
(
.clk(clk),
.rst_n(rst_n),
.a(e),
.b(f),
.c(d)
);
endmodule
module son_module(
input clk,
input rst_n,
input [7:0]a,
input [7:0]b,
output [7:0]c
);
reg [7:0] c1;
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)begin
c1<=0;
end
else
begin
if(a>b)
begin
c1<=b;
end
else
c1<=a;
end
end
assign c=c1;
endmodule
本题需要注意例化模块的使用,实际上就是在主模块里用子模块的功能,输入输出需要一一对应,此外本题如果只用两次例化结果不正确,原因为在时刻一比较了ab,而在下一时刻,c已经进行了更新,相当比的是下一时刻的c
