当前位置:网站首页>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
波形如下:
边栏推荐
- Temporary cramming before DFS examination
- Au - delà du PARM! La maîtrise de l'Université de Pékin propose diverse pour actualiser complètement le classement du raisonnement du NLP
- Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
- CODING DevSecOps 助力金融企业跑出数字加速度
- go语言编程规范梳理总结
- Usage and usage instructions of JDBC connection pool
- Severlet learning foundation
- mapper.xml文件中的注释
- Noi / 1.5 06: element maximum span value of integer sequence
- Data communication foundation - dynamic routing protocol rip
猜你喜欢
Bugku easy_ nbt
Data communication foundation - routing communication between VLANs
【简记】解决IDE golang 代码飘红报错
1330: [example 8.3] minimum steps
RepLKNet:不是大卷积不好,而是卷积不够大,31x31卷积了解一下 | CVPR 2022
DVWA range clearance tutorial
Thymeleaf uses background custom tool classes to process text
Number protection AXB function! (essence)
Summary of the third class
I spring and autumn blasting-2
随机推荐
B站做短视频,学抖音死,学YouTube生?
Redis' transaction mechanism
SQL Server learning notes
How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
MySQL table field adjustment
Bugku's Eval
How can the boss choose programmers to help me with development?
六种常用事务解决方案,你方唱罢,我登场(没有最好只有更好)
[brief notes] solve the problem of IDE golang code red and error reporting
MySQL 巨坑:update 更新慎用影响行数做判断!!!
P6183 [USACO10MAR] The Rock Game S
Data communication foundation NAT network address translation
PHP high concurrency and large traffic solution (PHP interview theory question)
Data communication foundation - route republication
Virtual base class (a little difficult)
CODING DevSecOps 助力金融企业跑出数字加速度
Data communication foundation OSPF Foundation
Number protection AXB function! (essence)
Detailed explanation of C language branch statements
Hongmeng system -- Analysis from the perspective of business