题解 | #求最小公倍数#
求最小公倍数
https://www.nowcoder.com/practice/ce067d6beee2413c8a26d37ca1a9431f
`timescale 1ns/1ns module lcm#( parameter DATA_W = 8) ( input [DATA_W-1:0] A, input [DATA_W-1:0] B, input vld_in, input rst_n, input clk, output wire [DATA_W*2-1:0] lcm_out, output wire [DATA_W-1:0] mcd_out, output reg vld_out ); reg [DATA_W-1:0] minusA_reg, minusB_reg; wire [DATA_W-1:0] minusA, minusB; always@(posedge clk or negedge rst_n) begin if(!rst_n) begin minusA_reg <= 'd0; minusB_reg <= 'd0; end else begin minusA_reg <= vld_in?A:(minusA - minusB); minusB_reg <= vld_in?B:minusB; end end assign minusA = (minusA_reg>minusB_reg)?minusA_reg:minusB_reg; assign minusB = (minusA_reg>minusB_reg)?minusB_reg:minusA_reg; reg [DATA_W*2-1:0] lcm_out_reg; reg [DATA_W-1:0] mcd_out_reg; always@(posedge clk or negedge rst_n) begin if(!rst_n) begin vld_out <= 1'b0; {lcm_out_reg,mcd_out_reg} <= 'd0; end else begin vld_out <= (minusA_reg==minusB_reg)&(minusA_reg!='d0); lcm_out_reg <= ((minusA_reg==minusB_reg)&(minusA_reg!='d0))?(A*B/minusB):'d0; mcd_out_reg <= ((minusA_reg==minusB_reg)&(minusA_reg!='d0))?minusB:'d0; end end assign mcd_out = mcd_out_reg; assign lcm_out = lcm_out_reg; endmodule