FPGA实现开根号,仿真通过,算一次需要34个时钟周期

/******************************************
verilog开平方操作
******************************************/
module mysqrt
(
clk,//时钟
indata,//输入待开平方数
outdata,//输出开平方的结果
flag_ok,
);
input wire clk;//时钟
input wire [31:0]indata;//假设数据为32bit
output reg [15:0]outdata=0;//32bit数据开根号,输出的数据为16bit
output reg flag_ok=0;//完成一次产生一个上升沿
reg [31:0]buff_indata=0;//缓存输入参数
reg [7:0]state=0;//状态机参数
reg [7:0]cnt=0;//移位参数
reg [15:0]out_t=0;//中间变量

always@(posedge clk)
begin
    case(state)
    0:begin
        buff_indata<=indata;
        cnt<=0;
        out_t<=0;
        state<=1;
        flag_ok<=0;
    end
    1:begin
        out_t<=out_t+(16'h8000>>cnt);
        if(cnt>=16)
            state<=3;
        else
            state<=2;
    end
    2:begin
        if((out_t*out_t)>buff_indata)begin
            out_t<=out_t-(16'h8000>>cnt);
        end
        cnt<=cnt+1;
        state<=1;
    end
    3:begin
        outdata<=out_t;
        state<=0;
        flag_ok<=1;
    end    
    endcase
end
endmodule

/***********************************************************************************************************************************************/

`timescale 1ns/1ps
`include "mysqrt.v" 
module      mysqrt_t;
reg         clk=0;
reg         [31:0]indata;
reg         [31:0]cnt=0;
wire         flag_ok;
wire        [15:0]outdata;
always #1 clk=~clk;
initial
begin 
    indata=1000000;
   forever
    begin 
        if(indata>0)begin
            #100 indata=indata-1;//每个100个时钟更新一次测试值
        end
    end 
end 
mysqrt u1(.clk(clk),
           .indata(indata),
           .outdata(outdata),
           .flag_ok(flag_ok)       
           );
endmodule

/***********************************************************************************************************************************************/

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值