「Verilog学习笔记」求最小公倍数

专栏前言

本专栏的内容主要是记录本人学习Verilog过程中的一些知识点,刷题网站用的是牛客网

题目要求求解两个数的最小公倍数,而最小公倍数可以通过两个数的乘积除以两个数的最小公约数得到。乘法是容易计算的,所以问题变成如何求解最大公约数。可以采用辗转相减法求解,

例如 :两个自然数35和14,用大数减去小数,(35,14)->(21,14)->(7,14),此时,7小于14,要做一次交换,把14作为被减数,即(14,7)->(7,7),再做一次相减,结果为0,这样也就求出了最大公约数7。

然后可以实现除法器IP核或者之间使用 '/'进行除法运算得到最小公倍数,'/'在多数的编译器中也可以进行综合。

复制代码
`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*2-1:0] mcd, a_buf, b_buf, mul_buf ; 
    reg mcd_vld ; 
    reg [1:0] state, nstate ; 
    parameter idle = 0, S0 = 1, S1 = 2, S2 = 3 ; 

    always @ (posedge clk or negedge rst_n) 
        if (!rst_n) state <= idle ; 
        else state <= nstate ; 
    
    always @ (posedge clk or negedge rst_n) 
        if (!rst_n) begin 
            nstate <= idle ; 
            mcd <= 0 ; 
            mcd_vld <= 0 ; 
            a_buf <= 0 ; 
            b_buf <= 0 ; 
            mul_buf <= 0 ; 
            vld_out <= 0 ; 
        end
        else begin 
            case (state) 
            idle : 
            if (vld_in) begin 
                a_buf <= A ; 
                b_buf <= B ; 
                nstate <= S0 ; 
                mul_buf <= A * B ; 
                mcd_vld <= 0 ; 
                vld_out <= 0 ; 
            end
            else begin 
                nstate <= idle ; 
                mcd_vld <= 0 ; 
                vld_out <= 0 ; 
            end
            S0 : 
            if (a_buf != b_buf) begin 
                if (a_buf > b_buf) begin
                    a_buf <= a_buf - b_buf ; 
                    b_buf <= b_buf ; 
                end
                else begin 
                    b_buf <= b_buf - a_buf ; 
                    a_buf <= a_buf ; 
                    vld_out <= 0 ; 
                end
                nstate <= S0 ; 
            end
            else begin 
                nstate <= S1 ; 
                vld_out <= 0 ; 
            end
            S1 : begin 
                mcd <= b_buf ; 
                mcd_vld <= 1 ; 
                nstate <= idle ; 
                vld_out <= 1 ; 
            end
            default : begin 
                nstate <= idle ; 
                vld_out <= 0 ; 
            end
            endcase
        end
            
    assign mcd_out = mcd ; 
    assign lcm_out = mul_buf / mcd ; 

endmodule
相关推荐
Slow菜鸟5 小时前
AI学习篇(五) | awesome-design-md 使用说明
人工智能·学习
ZC跨境爬虫6 小时前
跟着 MDN 学 HTML day_9:(信件语义标记)
前端·css·笔记·ui·html
狐狐生风6 小时前
LangChain 向量存储:Chroma、FAISS
人工智能·python·学习·langchain·faiss·agentai
狐狐生风6 小时前
LangChain RAG 基础
人工智能·python·学习·langchain·rag·agentai
努力努力再努力FFF9 小时前
医生对AI辅助诊断感兴趣,作为临床人员该怎么了解和学习?
人工智能·学习
OBiO20139 小时前
Cell | 突破AAV载体容量限制!路中华/姜玉武/刘太安团队开发AAVLINK系统实现大基因递送
笔记
智者知已应修善业10 小时前
【51单片机2个按键控制流水灯运行与暂停】2023-9-6
c++·经验分享·笔记·算法·51单片机
sakiko_10 小时前
UIKit学习笔记5-使用UITableView制作聊天页面
笔记·学习·swift·uikit
Alice-YUE11 小时前
【js高频八股】防抖与节流
开发语言·前端·javascript·笔记·学习·ecmascript
北山有鸟12 小时前
修改源码法和插件法
嵌入式硬件·学习