题解 | #使用子模块实现三输入数的大小比较#
使用子模块实现三输入数的大小比较
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] c_reg1;
wire [7:0] c_reg2;
sub_mod u0_submod(
.clk(clk),
.rst_n(rst_n),
.a(a),
.b(b),
.c(c_reg1)
);
sub_mod u1_submod(
.clk(clk),
.rst_n(rst_n),
.a(b),
.b(c),
.c(c_reg2)
);
sub_mod u3_submod(
.clk(clk),
.rst_n(rst_n),
.a(c_reg1),
.b(c_reg2),
.c(d)
);
endmodule
module sub_mod(
input clk,
input rst_n,
input [7:0] a,
input [7:0] b,
output [7:0]c
);
reg [7:0] c;
always@(posedge clk or negedge rst_n) begin
if(!rst_n)
c <= 8'b0;
else if(a<b)
c <= a;
else
c <= b;
end
endmodule
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] c_reg1;
wire [7:0] c_reg2;
sub_mod u0_submod(
.clk(clk),
.rst_n(rst_n),
.a(a),
.b(b),
.c(c_reg1)
);
sub_mod u1_submod(
.clk(clk),
.rst_n(rst_n),
.a(b),
.b(c),
.c(c_reg2)
);
sub_mod u3_submod(
.clk(clk),
.rst_n(rst_n),
.a(c_reg1),
.b(c_reg2),
.c(d)
);
endmodule
module sub_mod(
input clk,
input rst_n,
input [7:0] a,
input [7:0] b,
output [7:0]c
);
reg [7:0] c;
always@(posedge clk or negedge rst_n) begin
if(!rst_n)
c <= 8'b0;
else if(a<b)
c <= a;
else
c <= b;
end
endmodule