当前位置:网站首页>[FPGA] day19- binary to decimal (BCD code)
[FPGA] day19- binary to decimal (BCD code)
2022-08-11 03:41:00 【The spring breeze light preface】
一、为什么要用BCD码
FPGAThere is no divider in the resource,when using divisionFPGALogical units are used(LE)中的查找表(LUT)Go build the divider.If it is a remainder of a large number、取整,It will take up a lot of logical resources,Then lead to a certain path delay.
造成的影响:
①时序问题.系统时钟周期20nsNo results can be given within,频率无法达到50M,This leads to problems with the following series of data.
②资源问题.Takes up a lot of logical resources.
因此可以使用BCD码,Use speed for area
流水线,Convert speed to area
二、BCDcode implementation
Used to separate the individual digits of a decimal number,这种称为 移位加3算法 的算法 Only add, subtract and shift operations are used,It can meet the requirements of code portability,The flow of the shift plus three algorithm is as follows(It is assumed here that only 3 个数位):
1、Shift a binary number left by oneBCD数(未满 4 fill in front 0)
2、如果移动了 8 位,Then the binary numbers are there 百位、十位和个位列,计算结束
3、在任何一个 BCD 列中,if any binary number 大于或者等于 5,就把这个数 加上 3
4、回到步骤 1
三、代码实现
module binary2bcd#(parameter DIN_W = 32,DOUT_W = 40) (
input clk ,
input rst_n ,
input en ,
input [DIN_W-1:0] binary_din , //输入二进制数据
output reg [DOUT_W-1:0] bcd_dout , //输出BCD码数据
output reg bcd_dout_vld
);
//状态机参数定义
localparam IDLE = 4'b0001,
READY = 4'b0010,
SHIFT = 4'b0100,
DONE = 4'b1000;
//信号定义
reg [3:0] state_c ;
reg [3:0] state_n ;
reg [DIN_W-1:0] din_r ; //数据锁存
reg [5:0] shift_cnt ; //移位次数计数器
wire add_shift_cnt ;
wire end_shift_cnt ;
reg [3:0] mem_r0 ;
reg [3:0] mem_r1 ;
reg [3:0] mem_r2 ;
reg [3:0] mem_r3 ;
reg [3:0] mem_r4 ;
reg [3:0] mem_r5 ;
reg [3:0] mem_r6 ;
reg [3:0] mem_r7 ;
reg [3:0] mem_r8 ;
reg [3:0] mem_r9 ;
wire [3:0] mem_w0 ;
wire [3:0] mem_w1 ;
wire [3:0] mem_w2 ;
wire [3:0] mem_w3 ;
wire [3:0] mem_w4 ;
wire [3:0] mem_w5 ;
wire [3:0] mem_w6 ;
wire [3:0] mem_w7 ;
wire [3:0] mem_w8 ;
wire [3:0] mem_w9 ;
wire [39:0] bcd_res ;
wire idle2ready ;
wire shift2done ;
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
state_c <= IDLE ;
end
else begin
state_c <= state_n ;
end
end
always @(*) begin
case (state_c)
IDLE :begin
if(idle2ready)
state_n = READY ;
else
state_n = state_c;
end
READY:begin
state_n = SHIFT;
end
SHIFT:begin
if(shift2done)
state_n = DONE ;
else
state_n = state_c;
end
DONE :begin
state_n = IDLE;
end
default:state_n = IDLE;
endcase
end
assign idle2ready = state_c == IDLE && (en) ;
assign shift2done = state_c == SHIFT && (end_shift_cnt);
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
shift_cnt <= 1'd0;
end
else if(add_shift_cnt)begin
if(end_shift_cnt)begin
shift_cnt <= 1'd0;
end
else begin
shift_cnt <= shift_cnt + 1'd1;
end
end
end
assign add_shift_cnt = state_c == SHIFT;
assign end_shift_cnt = add_shift_cnt && shift_cnt == DIN_W - 1 ;
//din_r
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
din_r <= 1'b0;
end
else if(en)begin
din_r <= binary_din;
end
else if(state_c == SHIFT)begin //移位状态下,Shift left one bit per clock cycle
din_r <= din_r << 1'b1;
end
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
mem_r0 <= 0;
mem_r1 <= 0;
mem_r2 <= 0;
mem_r3 <= 0;
mem_r4 <= 0;
mem_r5 <= 0;
mem_r6 <= 0;
mem_r7 <= 0;
mem_r8 <= 0;
mem_r9 <= 0;
end
else if(idle2ready)begin
mem_r0 <= 0;
mem_r1 <= 0;
mem_r2 <= 0;
mem_r3 <= 0;
mem_r4 <= 0;
mem_r5 <= 0;
mem_r6 <= 0;
mem_r7 <= 0;
mem_r8 <= 0;
mem_r9 <= 0;
end
else if(state_c == SHIFT)begin
mem_r0 <= {
mem_w0[2:0],din_r[DIN_W-1]};
mem_r1 <= {
mem_w1[2:0],mem_w0[3]};
mem_r2 <= {
mem_w2[2:0],mem_w1[3]};
mem_r3 <= {
mem_w3[2:0],mem_w2[3]};
mem_r4 <= {
mem_w4[2:0],mem_w3[3]};
mem_r5 <= {
mem_w5[2:0],mem_w4[3]};
mem_r6 <= {
mem_w6[2:0],mem_w5[3]};
mem_r7 <= {
mem_w7[2:0],mem_w6[3]};
mem_r8 <= {
mem_w8[2:0],mem_w7[3]};
mem_r9 <= {
mem_w9[2:0],mem_w8[3]};
end
end
assign mem_w0 = (mem_r0 > 4'd4)?(mem_r0 + 4'd3):mem_r0;
assign mem_w1 = (mem_r1 > 4'd4)?(mem_r1 + 4'd3):mem_r1;
assign mem_w2 = (mem_r2 > 4'd4)?(mem_r2 + 4'd3):mem_r2;
assign mem_w3 = (mem_r3 > 4'd4)?(mem_r3 + 4'd3):mem_r3;
assign mem_w4 = (mem_r4 > 4'd4)?(mem_r4 + 4'd3):mem_r4;
assign mem_w5 = (mem_r5 > 4'd4)?(mem_r5 + 4'd3):mem_r5;
assign mem_w6 = (mem_r6 > 4'd4)?(mem_r6 + 4'd3):mem_r6;
assign mem_w7 = (mem_r7 > 4'd4)?(mem_r7 + 4'd3):mem_r7;
assign mem_w8 = (mem_r8 > 4'd4)?(mem_r8 + 4'd3):mem_r8;
assign mem_w9 = (mem_r9 > 4'd4)?(mem_r9 + 4'd3):mem_r9;
assign bcd_res = {
mem_r9,mem_r8,mem_r7,mem_r6,mem_r5,mem_r4,mem_r3,mem_r2,mem_r1,mem_r0};
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
bcd_dout <= 1'b0;
end
else if(state_c == DONE)begin
bcd_dout <= bcd_res[DOUT_W-1:0];
end
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
bcd_dout_vld <= 1'b0;
end
else begin
bcd_dout_vld <= state_c == DONE;
end
end
endmodule
边栏推荐
- [BX]和loop
- What problems should we pay attention to when building a programmatic trading system?
- 程序化交易的策略类型可以分为哪几种?
- 一次简单的 JVM 调优,学会拿去写到简历里
- STC8H development (15): GPIO drive Ci24R1 wireless module
- 互换性与测量技术——表面粗糙度选取和标注方法
- console.log alternatives you didn't know about
- 云平台下ESB产品开发步骤说明
- 【FPGA】day18-ds18b20实现温度采集
- Element's BFC attribute
猜你喜欢
DOM-DOM tree, a DOM tree has three types of nodes
Interchangeability and Measurement Technology—Surface Roughness Selection and Marking Method
Environment configuration of ESP32 (arduino arduino2.0 VScode platform which is easy to use?)
STC8H开发(十五): GPIO驱动Ci24R1无线模块
When EasyCVR is connected to the GB28181 device, what is the reason that the device is connected normally but the video cannot be played?
QueryDet:级联稀疏query加速高分辨率下的小目标检测
Qnet Weak Network Test Tool Operation Guide
Detailed explanation of VIT source code
2022-08-10 The sixth group Hiding spring study notes
[DB operation management/development solution] Shanghai Daoning provides you with an integrated development tool to improve the convenience of work - Orange
随机推荐
机器学习可以应用在哪些场景?机器学习有什么用?
[BX] and loop
【愚公系列】2022年08月 Go教学课程 035-接口和继承和转换与空接口
基于改进YOLOv5轻量化的烟火检测
Design and Realization of Employment Management System in Colleges and Universities
按摩椅控制板的开发让按摩椅变得简约智能
【ADI低功耗2k代码】基于ADuCM4050的ADXL363、TMP75的加速度、温度检测及串口打印、蜂鸣器播放音乐(孤勇者)
学编程的第十三天
UNI-APP_iphone bottom safe area
MYSQLg高级------回表
大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,如何分配?
Will oracle cardinality affect query speed?
typedef defines the structure array type
EasyCVR接入GB28181设备时,设备接入正常但视频无法播放是什么原因?
树莓派入门(5)系统备份
Kubernetes集群搭建Zabbix监控平台
CSDN blog replacement skin
机器学习是什么?详解机器学习概念
索引的创建、查看、删除
Roewe imax8ev cube battery security, what blackening and swelling are hidden behind it?