`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 ; reg [7:0] d ; reg [7:0] f ; min_mod min_mod_inst(clk,rst_n,a,b,e); always@(posedge clk or negedge rst_n) begin if(!rst_n) begin f<=0; end else begin f<=c; end end always@(posedge clk or negedge rst_n) begin if(!rst_n) d<=0; else if(f<e) d<=f; else d<=e; end endmodule module min_mod( input clk, input rst_n, input [7:0]a, input [7:0]b, output reg [7:0]e ); always@(posedge clk or negedge rst_n) begin if(!rst_n) e<=0; else if( a< b) e<=a ; else e<=b ; end endmodule
首先要审题,题目中说了,子模块的功能就是找出a b的最小值,然后传回主函数,与另一个数进行比较从而得出最小值。
主要注意的问题 :1由于子模块always也是需要1个周期来比较a,b的最小值的。那么返回结果应该耗费一个周期
因此需要将c寄存一个时钟周期,本解是用变量f寄存了一下。
如果不寄存输出的就不是当下a,b,c输出结果,c就是下一拍的