题解 | #多功能数据处理器#
使用子模块实现三输入数的大小比较
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 ); wire [7:0] temp0; wire [7:0] temp1; compare_a_b u1_compare_a_b( .clk(clk), .rst_n(rst_n), .din0(a), .din1(b), .dout(temp0));//比较出a b中较小的那个给temp0 //原本的想法是比较出a b中较小的数给temp0,再比较temp0和c就可以直接找出最小值了, //但是这个是软件的思想,在verilog中是并行执行的,如果使用这个想法就相当于需要延后一个时钟,时序才是正确的。 /* compare_a_b u2_compare_a_b( .clk(clk), .rst_n(rst_n), .din0(a), .din1(b), .dout(temp0));*///比较出temp0 c中较小的那个数,即为最小值 compare_a_b u2_compare_a_b( .clk(clk), .rst_n(rst_n), .din0(a), .din1(c), .dout(temp1)); compare_a_b u3_compare_a_b( .clk(clk), .rst_n(rst_n), .din0(temp0), .din1(temp1), .dout(d)); endmodule module compare_a_b( input clk, input rst_n, input [7:0]din0, input [7:0]din1, output [7:0]dout ); reg [7:0] temp; always@(posedge clk&nbs***bsp;negedge rst_n)begin if(!rst_n) temp<=8'b0; else begin if (din0>din1) temp<=din1; else temp<=din0; end end assign dout=temp; endmodule