当前位置:网站首页>verilog实现计算最大公约数和最小公倍数
verilog实现计算最大公约数和最小公倍数
2022-07-05 15:18:00 【不想上体育课】
设计一个时序电路,输入2个无符号数,位宽可以通过参数DATA_W确定,输出这两个数的最小公倍数和最大公约数。
解:在求解最大公约数与最小公倍数时,通常使用辗转相除法计算得到最大公约数,然后利用两数之积除最大公约数得到最小公倍数;
举例如下:
34 与 20;
(1)34 - 20 = 14;
(2)20 - 14 = 6;
(3)14 - 6 = 8;
(4)8 - 6 =2;
(5)6 - 2 = 4;
(6)4 - 2 = 2;
(7)2 - 2 = 0;
所以最大公约数为2;
则最小公倍数 = 34 * 20 /2 =340;
实现代码如下:
`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] A_reg;
reg [DATA_W-1:0] B_reg;
reg [DATA_W*2-1:0] lcm ;
reg [DATA_W*2-1:0] lcm_reg;
reg [DATA_W-1:0] mcd_reg;
reg [1:0] state;
parameter IDLE = 2'b00;
parameter st_1 = 2'b01;
parameter st_2 = 2'b10;
[email protected](posedge clk or negedge rst_n)
if(rst_n == 1'b0)
begin
vld_out<= 1'b0;
A_reg <= 'd0;
B_reg <= 'd0;
lcm <= 'd0;
mcd_reg <= 'd0;
lcm_reg <= 'd0;
state <= IDLE;
end
else case(state)
IDLE:
begin
vld_out <= 1'b0;
if(vld_in == 1'b1)
begin
A_reg <= A;
B_reg <= B;
lcm_reg<= A*B;
state <= st_1;
end
else
begin
A_reg <= A_reg;
B_reg <= B_reg;
lcm_reg<= lcm_reg;
state <= IDLE;
end
end
st_1:
if(A_reg == B_reg)
state <= st_2;
else
begin
state <= st_1;
if(A_reg > B_reg)
A_reg <= A_reg - B_reg;
else if(A_reg < B_reg)
B_reg <= B_reg - A_reg;
end
st_2:
begin
vld_out <= 1'b1;
lcm <= lcm_reg;
mcd_reg <= A_reg;
state <= IDLE;
end
endcase
assign lcm_out = lcm / mcd_reg;
assign mcd_out = mcd_reg;
endmodule仿真代码如下:
`timescale 1ns/1ns
module tb_lcm();
parameter DATA_W = 8;
reg [DATA_W-1:0] A;
reg [DATA_W-1:0] B;
reg vld_in;
reg rst_n;
reg clk;
wire [DATA_W*2-1:0] lcm_out;
wire [DATA_W-1:0] mcd_out;
wire vld_out;
initial
begin
A <= 'd0;
B <= 'd0;
vld_in <= 1'b0;
rst_n <= 1'b0;
clk = 1'b0;
#10
rst_n <= 1'b1;
A <= 'd6;
B <= 'd7;
vld_in <= 1'b1;
#2000
A <= 'd12;
B <= 'd8;
vld_in <= 1'b1;
#1000
A <= 'd15;
B <= 'd20;
vld_in <= 1'b1;
end
always #10 clk = ~clk;
lcm
#(
.DATA_W (DATA_W)
)
lcm_u
(
.A (A ),
.B (B ),
.vld_in (vld_in ),
.rst_n (rst_n ),
.clk (clk ),
.lcm_out (lcm_out),
.mcd_out (mcd_out),
.vld_out (vld_out)
);
endmodule波形如下:

边栏推荐
- Huiyuan, 30, is going to have a new owner
- 【简记】解决IDE golang 代码飘红报错
- Can I pass the PMP Exam in 20 days?
- Creation and optimization of MySQL index
- Summary of the third class
- Bugku's eyes are not real
- How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
- Transfer the idea of "Zhongtai" to the code
- Common PHP interview questions (1) (written PHP interview questions)
- Example of lvgl display picture
猜你喜欢

JS knowledge points-01

Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking

F. Min cost string problem solving Report

lv_ font_ Conv offline conversion

Bubble sort, insert sort

Bugku easy_ nbt

Data communication foundation - route republication

Thymeleaf uses background custom tool classes to process text

Good article inventory

基于OpenHarmony的智能金属探测器
随机推荐
做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史
复现Thinkphp 2.x 任意代码执行漏洞
Data communication foundation - route republication
P1451 calculate the number of cells / 1329: [example 8.2] cells
keep-alive
Analytic hierarchy process of mathematical modeling (including Matlab code)
Can gbase 8A view the location of SQL statement history?
机械臂速成小指南(九):正运动学分析
ICML 2022 | explore the best architecture and training method of language model
Appium automation test foundation - appium basic operation API (II)
JS topic - console log()
Go language programming specification combing summary
How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
MySQL table field adjustment
美团优选管理层变动:老将刘薇调岗,前阿里高管加盟
Maximum common subsequence
Bugku telnet
mapper. Comments in XML files
Linear DP (basic questions have been updated)
swiper. JS to achieve barrage effect