//Load Data 不能為0x00
//==========================
//FPGA產生基於CRC校驗碼生成器
//==========================
module CCITT(SW, LEDR, LEDG , CLOCK_50 ,KEY ,HEX0 ,HEX1 ,HEX2,HEX3 ,HEX4 );
input [17:0] SW; // toggle switches
input [3:0] KEY; // Push bottom
input CLOCK_50; //Clock 27MHz , 50Mhz
output [17:0] LEDR; // red LEDS
output [8:0] LEDG; // green LEDs
output [6:0] HEX0,HEX1,HEX2,HEX3 ,HEX4; //7-segment display
//set original program input , output
// input rst, /*async reset,active low*/
// input clk, /*clock input*/
// input [7:0] data_in, /*parallel data input pins */
// input d_valid, /* data valid,start to generate CRC, active high*/
// output reg[15:0] crc
wire [7:0] segout0; //HEX 0
wire [7:0] segout1; //HEX 1
wire [7:0] segout2; //HEX 2
wire [7:0] segout3; //HEX 3
wire clk_1hz;
//clk_div_1hz(clk_in , Reset, clk_out);
clk_div_1hz u2(CLOCK_50,KEY[0], clk_1hz);
_7seg UUT0(.hex((LEDR[3:0])),.seg(segout0));
_7seg UUT1(.hex((LEDR[7:4])),.seg(segout1));
_7seg UUT2(.hex((LEDR[11:8])),.seg(segout2));
_7seg UUT3(.hex((LEDR[15:12])),.seg(segout3));
assign HEX0=segout0[6:0];
assign HEX1=segout1[6:0];
assign HEX2=segout2[6:0];
assign HEX3=segout3[6:0];
CRC_Gen(
SW[17], /*async reset,active low*/
clk_1hz, /*clock signal*/
SW[7:0], /*parallel data input pins */
SW[16], /* data valid,start to generate CRC, active high*/
LEDR[15:0] /*CRC output*/
);
endmodule
//=================================
module CRC_Gen(
input rst, /*async reset,active low*/
input clk, /*clock input*/
input [7:0] data_in, /*parallel data input pins */
input d_valid, /* data valid,start to generate CRC, active high*/
output reg[15:0] crc
);
integer i;
reg feedback;
reg [15:0] crc_tmp;
/*
* sequential process
*/
always @(posedge clk or negedge rst)
begin
if(!rst)
crc <= 16'b0; /*触?器中的初始值十分重要 */
else if(d_valid==1'b0)
crc <= 16'b0;
else
crc <= crc_tmp;
end
/*
* combination process
*/
always@( data_in or crc)
begin
crc_tmp = crc;
for(i=7; i>=0; i=i-1)
begin
feedback = crc_tmp[15] ^ data_in[i];
crc_tmp[15] = crc_tmp[14];
crc_tmp[14] = crc_tmp[13];
crc_tmp[13] = crc_tmp[12];
crc_tmp[12] = crc_tmp[11] ^ feedback;
crc_tmp[11] = crc_tmp[10] ;
crc_tmp[10] = crc_tmp[9];
crc_tmp[9] = crc_tmp[8];
crc_tmp[8] = crc_tmp[7];
crc_tmp[7] = crc_tmp[6];
crc_tmp[6] = crc_tmp[5];
crc_tmp[5] = crc_tmp[4] ^ feedback;
crc_tmp[4] = crc_tmp[3];
crc_tmp[3] = crc_tmp[2];
crc_tmp[2] = crc_tmp[1];
crc_tmp[1] = crc_tmp[0];
crc_tmp[0] = feedback;
end
end
endmodule
//-----------------------------------------
// 1Hz Generator
//-----------------------------------------
module clk_div_1hz(clk_in , Reset, clk_out);
input clk_in ;
input Reset;
output reg clk_out;
integer i;
always@(posedge clk_in or negedge Reset) begin
if (!Reset) begin
i=0;
clk_out=0;
end
else begin
i= i+1 ;
if (i>=24_999_999) begin
clk_out = ~clk_out;
i=0;
end
end
end
endmodule
//-----------------------------------------
//Common-cathod seven segment display
//using case.....endcase statement
//Filename : sevenseg_case.v
//-----------------------------------------
module _7seg(hex , seg);
input [3:0] hex;
output [7:0] seg;
reg [7:0] seg;
// segment encoding
// 0
// ---
// 5 | | 1
// --- <- 6
// 4 | | 2
// ---
// 3
always @(hex)
begin
case (hex)
// Dot point is always disable
4'b0001 : seg = 8'b11111001; //1 = F9H
4'b0010 : seg = 8'b10100100; //2 = A4H
4'b0011 : seg = 8'b10110000; //3 = B0H
4'b0100 : seg = 8'b10011001; //4 = 99H
4'b0101 : seg = 8'b10010010; //5 = 92H
4'b0110 : seg = 8'b10000010; //6 = 82H
4'b0111 : seg = 8'b11111000; //7 = F8H
4'b1000 : seg = 8'b10000000; //8 = 80H
4'b1001 : seg = 8'b10010000; //9 = 90H
4'b1010 : seg = 8'b10001000; //A = 88H
4'b1011 : seg = 8'b10000011; //b = 83H
4'b1100 : seg = 8'b11000110; //C = C6H
4'b1101 : seg = 8'b10100001; //d = A1H
4'b1110 : seg = 8'b10000110; //E = 86H
4'b1111 : seg = 8'b10001110; //F = 8EH
default : seg = 8'b11000000; //0 = C0H
endcase
end
endmodule
沒有留言:
張貼留言