当前位置: 首页 > news >正文

【Verilog】CRC-24

编码

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2024/08/28 17:35:32
// Design Name: 
// Module Name: CRC_24
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//module CRC_24(
clk,
rst,din,
din_p,
din_clk_p,dout,
dout_p,
dout_clk_p,
crc24
);input clk                     ;//in [1] 系统时钟
input rst                     ;//in [1] 系统复位,高有效input din                     ;//in [1] CRC待译码数据
input din_p                   ;//in [1] CRC待译码数据帧头
input din_clk_p               ;output dout                   ; 
output dout_p                 ; 
output dout_clk_p             ;
output [23:0] crc24           ;
parameter CODE_TYPE  =16'd24  ; 
parameter CODE_LENGTH=16'd1128; reg [15:0] cnts;
always @ (posedge clk or posedge rst)
beginif(rst)begincnts<=16'hffff;endelse if(din_p)begincnts<=16'd0;endelse if(cnts <= CODE_LENGTH)//864begincnts<=cnts+1'b1;endelsebegincnts<=cnts;end
endreg din_delay;
always @ (posedge clk or posedge rst)
beginif(rst)begindin_delay<=1'b0;endelse begindin_delay<=din;end
endreg [23:0] shift_regs;
always @ (posedge clk or posedge rst)
beginif(rst)beginshift_regs<=24'd0;endelse if(din_p)beginshift_regs<=24'd0;endelse if(cnts <= CODE_LENGTH-1-CODE_TYPE )//bianma -16,译码时全部移位beginshift_regs[0] <=shift_regs[15]^din_delay;shift_regs[1] <=shift_regs[0];shift_regs[2] <=shift_regs[1];shift_regs[3] <=shift_regs[2];shift_regs[4] <=shift_regs[3];shift_regs[5] <=shift_regs[4]^shift_regs[15]^din_delay;shift_regs[6] <=shift_regs[5];shift_regs[7] <=shift_regs[6];//shift_regs[8] <=shift_regs[7];shift_regs[9] <=shift_regs[8];shift_regs[10]<=shift_regs[9];shift_regs[11]<=shift_regs[10];shift_regs[12]<=shift_regs[11]^shift_regs[15]^din_delay;shift_regs[13]<=shift_regs[12];shift_regs[14]<=shift_regs[13];shift_regs[15]<=shift_regs[14];shift_regs[16]<=shift_regs[15];shift_regs[17]<=shift_regs[16];shift_regs[18]<=shift_regs[17];shift_regs[19]<=shift_regs[18];shift_regs[20]<=shift_regs[19];shift_regs[21]<=shift_regs[20];shift_regs[22]<=shift_regs[21];shift_regs[23]<=shift_regs[22];endelsebeginshift_regs<=shift_regs;	end
endassign crc24 = shift_regs;reg din_p_delay;
always @ (posedge clk or posedge rst)
beginif(rst)begindin_p_delay<=1'b0;endelsebegindin_p_delay<=din_p;end
endreg dout_p;
always @ (posedge clk or posedge rst)
beginif(rst)begindout_p<=1'b0;endelsebegindout_p<=din_p_delay;end
endreg dout;
always @ (posedge clk or posedge rst)
beginif(rst)begindout<=1'b0;endelse if(cnts<=CODE_LENGTH-CODE_TYPE-1)begindout<=din_delay;endelse if(cnts<=CODE_LENGTH-1)begindout<=shift_regs[CODE_LENGTH-1-cnts];//正常情况下,译码出来的CRC校验为0,按最终check_reg的指示来表示endelsebegindout<=din_delay;//elseend
endreg dout_clk_p;
always @ (posedge clk or posedge rst)
beginif(rst)begindout_clk_p<=1'b0;endelse if(cnts<=CODE_LENGTH-1)//CRC 32   0~311begindout_clk_p<=1'b1;endelsebegindout_clk_p<=1'b0;end
endendmodule

解码

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2024/08/28 19:37:42
// Design Name: 
// Module Name: CRC_24_Decode
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//module CRC_24_Decode(
clk,
rst,din,
din_p,
din_clk_p,dout,
dout_p,
dout_clk_p,
dout_last,check
);input clk                     ;//in [1] 系统时钟
input rst                     ;//in [1] 系统复位,高有效input din                     ;//in [1] CRC待译码数据
input din_p                   ;//in [1] CRC待译码数据帧头
input din_clk_p               ;output dout                   ; 
output dout_p                 ; 
output dout_clk_p             ;
output dout_last              ;output check                  ;parameter CODE_TYPE  =16'd24  ; 
parameter CODE_LENGTH=16'd1128; reg [15:0] cnts;
always @ (posedge clk or posedge rst)
beginif(rst)begincnts<=16'hffff;endelse if(din_p)begincnts<=16'd0;endelse if(cnts <= CODE_LENGTH)//864begincnts<=cnts+1'b1;endelsebegincnts<=cnts;end
endreg din_delay;
always @ (posedge clk or posedge rst)
beginif(rst)begindin_delay<=1'b0;endelse begindin_delay<=din;end
endreg din_p_delay;
always @ (posedge clk or posedge rst)
beginif(rst)begindin_p_delay<=1'b0;endelsebegindin_p_delay<=din_p;end
endreg [23:0] recv_regs;
always @ (posedge clk or posedge rst)
beginif(rst)beginrecv_regs <= 24'd0;endelse if(din_clk_p)beginrecv_regs <= {recv_regs[22:0],din};endelsebeginrecv_regs <= recv_regs;end
endreg [23:0] shift_regs;
always @ (posedge clk or posedge rst)
beginif(rst)beginshift_regs<=24'd0;endelse if(din_p)beginshift_regs<=24'd0;endelse if(cnts <= CODE_LENGTH-1-CODE_TYPE )//bianma -16,译码时全部移位beginshift_regs[0] <=shift_regs[15]^din_delay;shift_regs[1] <=shift_regs[0];shift_regs[2] <=shift_regs[1];shift_regs[3] <=shift_regs[2];shift_regs[4] <=shift_regs[3];shift_regs[5] <=shift_regs[4]^shift_regs[15]^din_delay;shift_regs[6] <=shift_regs[5];shift_regs[7] <=shift_regs[6];//shift_regs[8] <=shift_regs[7];shift_regs[9] <=shift_regs[8];shift_regs[10]<=shift_regs[9];shift_regs[11]<=shift_regs[10];shift_regs[12]<=shift_regs[11]^shift_regs[15]^din_delay;shift_regs[13]<=shift_regs[12];shift_regs[14]<=shift_regs[13];shift_regs[15]<=shift_regs[14];shift_regs[16]<=shift_regs[15];shift_regs[17]<=shift_regs[16];shift_regs[18]<=shift_regs[17];shift_regs[19]<=shift_regs[18];shift_regs[20]<=shift_regs[19];shift_regs[21]<=shift_regs[20];shift_regs[22]<=shift_regs[21];shift_regs[23]<=shift_regs[22];endelsebeginshift_regs<=shift_regs;	end
endreg dout_p;
always @ (posedge clk or posedge rst)
beginif(rst)begindout_p<=1'b0;endelsebegindout_p<=din_p_delay;end
endreg dout;
always @ (posedge clk or posedge rst)
beginif(rst)begindout<=1'b0;endelse if(cnts<=CODE_LENGTH-1)begindout<=din_delay;endelsebegindout<=din_delay;//elseend
endreg dout_clk_p;
always @ (posedge clk or posedge rst)
beginif(rst)begindout_clk_p<=1'b0;end//	else if(cnts<=CODE_LENGTH-1-CODE_TYPE)//CRC 32   0~311else if(cnts<=CODE_LENGTH-1)//CRC 32   0~311begindout_clk_p<=1'b1;endelsebegindout_clk_p<=1'b0;end
endreg dout_last;
always @ (posedge clk or posedge rst)
beginif(rst)begindout_last<=1'b0;end//	else if(cnts<=CODE_LENGTH-1-CODE_TYPE)//CRC 32   0~311else if(cnts==CODE_LENGTH-1)//CRC 32   0~311begindout_last<=1'b1;endelsebegindout_last<=1'b0;end
endreg check;
always @ (posedge clk or posedge rst)
beginif(rst)begincheck <= 1'b0;endelse if(recv_regs == shift_regs)begincheck <= 1'b1;endelsebegincheck <= 1'b0;end
endendmodule

测试的Test

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2024/08/28 19:23:39
// Design Name: 
// Module Name: tb_crc24
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//module tb_crc24();reg ad9361_l_clk,rst;   initial beginad9361_l_clk=0;forever #4.545 ad9361_l_clk=~ad9361_l_clk;
end
initial beginrst=1;#9.09 rst=0;
endreg [31:0] cnts;
always @ (posedge ad9361_l_clk or posedge rst)
beginif(rst)begincnts<=32'hffff_ff00;endelse if(cnts ==32'd110000)//100msbegincnts<=32'd0;endelsebegincnts<=cnts + 1'b1;end
endreg [7:0] din;
reg din_p;
reg din_clk_p;
always @ (posedge ad9361_l_clk or posedge rst)
beginif(rst)begindin_clk_p<=1'b0;endelse if((cnts>=32'd1)&&(cnts<=32'd232))begindin_clk_p<=1'b1;endelsebegindin_clk_p<=1'b0;end
endalways @ (posedge ad9361_l_clk or posedge rst)
beginif(rst)begindin<=8'd0; endelsebegincase(cnts)32'd1:begin din<=8'd1;  enddefault:begin din<=din + 8'd18; endendcaseend
endalways @ (posedge ad9361_l_clk or posedge rst)
beginif(rst)begindin_p<=1'b0; endelsebegincase(cnts)32'd1:begin din_p<=1'b1;  enddefault:begin din_p<=1'b0; endendcaseend
endwire dout,dout_p,dout_clk_p;
wire [23:0] crc24;
CRC_24 CRC(.clk        (ad9361_l_clk),.rst        (rst),.din        (din[0]),.din_p      (din_p),.din_clk_p  (din_clk_p),.dout       (dout),.dout_p     (dout_p),.dout_clk_p (dout_clk_p),.crc24      (crc24)
);wire dout_d,dout_p_d,dout_clk_p_d,check,dout_last;
CRC_24_Decode CRC_24_Decode(.clk        (ad9361_l_clk),.rst        (rst),.din        (dout),.din_p      (dout_p),.din_clk_p  (dout_clk_p),.dout       (dout_d),.dout_p     (dout_p_d),.dout_clk_p (dout_clk_p_d),.dout_last  (dout_last),.check      (check)
);endmodule

http://www.mrgr.cn/news/57175.html

相关文章:

  • GB/T28181-2022规范解读、应用场景和技术实现探究
  • 2015年-2017年 计算机技术专业 程序设计题(算法题)实战_c语言程序设计数据结构程序设计分析
  • 因为一句废话,大模型无法解决小学数学题?
  • Django请求响应对象
  • nz-alert nzType 动态赋值报错
  • U盘数据丢失不用慌,这4个工具可以帮你恢复。
  • Windows系统PyCharm右键运行.sh文件
  • 海报在线制作系统小程序源码
  • QPainter抗锯齿设置
  • 动态规划之简单多状态 dp 问题(上)
  • 【Qt】控件——Qt多元素控件、常见的多元素控件、多元素控件的使用、List Widget、Table Widget、Tree Widget
  • socket套接字
  • Spring Cloud --- Sentinel 授权规则
  • 入门介绍(一):脉冲神经网络(SNN)
  • Python 实现 excel 数据过滤
  • Java学习教程,从入门到精通,Java 基本数据类型(7)
  • 鸿蒙应用的Tabs 组件怎么使用
  • c++的头文件到底应该怎么写?
  • 【编程语言】Kotlin快速入门 - 高阶函数与运算符重载
  • 均匀随机掉落算法
  • 梦开始的地方 -- 两数求和
  • c++查看运行时类型
  • Thread类
  • react优化
  • Napkins:开源 AI 开发工具,实现截图或线框图到网页应用的快速转换
  • konva不透明度,查找,显示,隐藏