题解 | #使用子模块实现三输入数的大小比较#
使用子模块实现三输入数的大小比较
http://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
);
reg [7:0]data1;
reg [7:0]data2;
reg [7:0]d;
always@(posedge clk or negedge rst_n)
begin
if(!rst_n) begin
data1 <= 0;
end
else if (a < b)
data1 <= a;
else data1 <= b;
end
always@(posedge clk or negedge rst_n)
begin
if(!rst_n) begin
data2 <= 0;
end
else data2 <= c;
end
always@(posedge clk or negedge rst_n)
begin
if(!rst_n) begin
d <= 0;
end
else if (data1 < data2)
d <= data1;
else d <= data2;
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
);
reg [7:0]data1;
reg [7:0]data2;
reg [7:0]d;
always@(posedge clk or negedge rst_n)
begin
if(!rst_n) begin
data1 <= 0;
end
else if (a < b)
data1 <= a;
else data1 <= b;
end
always@(posedge clk or negedge rst_n)
begin
if(!rst_n) begin
data2 <= 0;
end
else data2 <= c;
end
always@(posedge clk or negedge rst_n)
begin
if(!rst_n) begin
d <= 0;
end
else if (data1 < data2)
d <= data1;
else d <= data2;
end
endmodule