OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [o8_crc16_ccitt.vhd] - Blame information for rev 198

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 180 jshamlet
-- Copyright (c)2020 Jeremy Seth Henry
2
-- All rights reserved.
3
--
4
-- Redistribution and use in source and binary forms, with or without
5
-- modification, are permitted provided that the following conditions are met:
6
--     * Redistributions of source code must retain the above copyright
7
--       notice, this list of conditions and the following disclaimer.
8
--     * Redistributions in binary form must reproduce the above copyright
9
--       notice, this list of conditions and the following disclaimer in the
10
--       documentation and/or other materials provided with the distribution,
11
--       where applicable (as part of a user interface, debugging port, etc.)
12
--
13
-- THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
14
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
-- DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
17
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 194 jshamlet
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22
-- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 180 jshamlet
--
24
-- VHDL Units : o8_crc16_ccitt
25
-- Description: Implements the 16-bit CCITT CRC on byte-wide data suitable for
26
--            :  use with the Open8 CPU. Logic equations were taken from
27
--            :  Intel/Altera app note AN049.
28
--
29
-- Notes      :  Writing to the byte counter will reset all registers, and to
30
--            :   should be used to clear the CRC accumulator/byte counter
31
--            :   between frames.
32
--
33
-- Register Map:
34
-- Offset  Bitfield Description                        Read/Write
35
--   0x0   AAAAAAAA Data Input register (calc on write)(R/W)
36
--   0x1   AAAAAAAA Byte Counter (clear all on write)  (R/W)
37
--   0x2   AAAAAAAA B0 of calculated CRC               (RO)
38
--   0x3   AAAAAAAA B1 of calculated CRC               (RO)
39
--
40
-- Revision History
41
-- Author          Date     Change
42
------------------ -------- ---------------------------------------------------
43
-- Seth Henry      12/19/19 Design Start
44
 
45
library ieee;
46
use ieee.std_logic_1164.all;
47
use ieee.std_logic_unsigned.all;
48
 
49
library work;
50
  use work.open8_pkg.all;
51
 
52
entity o8_crc16_ccitt is
53
generic(
54
  Reset_Level                : std_logic := '1';
55
  Address                    : ADDRESS_TYPE
56
);
57
port(
58
  Clock                      : in  std_logic;
59
  Reset                      : in  std_logic;
60
  --
61
  Bus_Address                : in  ADDRESS_TYPE;
62
  Wr_Enable                  : in  std_logic;
63
  Wr_Data                    : in  DATA_TYPE;
64
  Rd_Enable                  : in  std_logic;
65
  Rd_Data                    : out DATA_TYPE
66
);
67
end entity;
68
 
69
architecture behave of o8_crc16_ccitt is
70
 
71
  constant Poly_Init         : std_logic_vector(15 downto 0) := x"0000";
72
 
73
  constant User_Addr         : std_logic_vector(15 downto 2)
74
                               := Address(15 downto 2);
75
  alias  Comp_Addr           is Bus_Address(15 downto 2);
76
  alias  Reg_Addr            is Bus_Address(1 downto 0);
77
  signal Reg_Sel             : std_logic_vector(1 downto 0);
78
  signal Addr_Match          : std_logic;
79
  signal Wr_En               : std_logic;
80
  signal Wr_Data_q           : DATA_TYPE;
81
  signal Rd_En               : std_logic;
82
 
83
  signal Next_Byte           : DATA_TYPE;
84
  signal Byte_Count          : DATA_TYPE;
85
 
86
  signal Calc_En             : std_logic;
87
  signal Buffer_En           : std_logic;
88
  signal Data                : DATA_TYPE;
89
  signal Exr                 : DATA_TYPE;
90
  signal Reg                 : std_logic_vector(15 downto 0);
91
  signal Comp_Data           : std_logic_vector(15 downto 0);
92
 
93
begin
94
 
95
  Addr_Match                 <= '1' when Comp_Addr = User_Addr else '0';
96
 
97
  Exr(0)                     <= Reg(0) xor Data(0);
98
  Exr(1)                     <= Reg(1) xor Data(1);
99
  Exr(2)                     <= Reg(2) xor Data(2);
100
  Exr(3)                     <= Reg(3) xor Data(3);
101
  Exr(4)                     <= Reg(4) xor Data(4);
102
  Exr(5)                     <= Reg(5) xor Data(5);
103
  Exr(6)                     <= Reg(6) xor Data(6);
104
  Exr(7)                     <= Reg(7) xor Data(7);
105
 
106
  CRC16_Calc: process( Clock, Reset )
107
  begin
108
    if( Reset = Reset_Level )then
109
      Reg_Sel                <= "00";
110
      Wr_En                  <= '0';
111
      Wr_Data_q              <= x"00";
112 191 jshamlet
      Rd_En                  <= '0';
113
      Rd_Data                <= OPEN8_NULLBUS;
114 180 jshamlet
 
115
      Byte_Count             <= x"00";
116
      Calc_En                <= '0';
117
      Buffer_En              <= '0';
118
      Data                   <= x"00";
119
      Reg                    <= x"0000";
120
    elsif( rising_edge(Clock) )then
121 191 jshamlet
      Reg_Sel                <= Reg_Addr;
122
 
123 180 jshamlet
      Wr_En                  <= Addr_Match and Wr_Enable;
124
      Wr_Data_q              <= Wr_Data;
125
 
126
      if( Wr_En = '1' )then
127
        case( Reg_Sel )is
128
          when "00" => -- Load next byte
129
            Data             <= Wr_Data_q;
130
            Calc_En          <= '1';
131
 
132
          when "01" => -- Clear accumulator and byte counter
133
            Byte_Count       <= x"00";
134
            Reg              <= Poly_Init;
135
 
136
          when others => null;
137
        end case;
138
      end if;
139
 
140 191 jshamlet
      Rd_En                  <= Addr_Match and Rd_Enable;
141
      Rd_Data                <= OPEN8_NULLBUS;
142 180 jshamlet
      if( Rd_En = '1' )then
143
        case( Reg_Sel )is
144
          when "00" => -- Read last byte
145
            Rd_Data          <= Data;
146
 
147
          when "01" => -- Read the byte counter
148
            Rd_Data          <= Byte_Count;
149
 
150
          when "10" => -- Read the lower byte of the calculated CRC
151
            Rd_Data          <= Comp_Data(7 downto 0);
152
 
153
          when "11" => -- Read the upper byte of the calculated CRC
154
            Rd_Data          <= Comp_Data(15 downto 8);
155
 
156
          when others => null;
157
        end case;
158
      end if;
159
 
160 191 jshamlet
      Calc_En                <= '0';
161
      Buffer_En              <= Calc_En;
162
 
163
      if( Calc_En = '1' )then
164
        Reg(0)               <= Reg(8)  xor            Exr(4) xor Exr(0);
165
        Reg(1)               <= Reg(9)  xor            Exr(5) xor Exr(1);
166
        Reg(2)               <= Reg(10) xor            Exr(6) xor Exr(2);
167
        Reg(3)               <= Reg(11) xor Exr(0) xor Exr(7) xor Exr(3);
168
        Reg(4)               <= Reg(12) xor Exr(1)                      ;
169
        Reg(5)               <= Reg(13) xor Exr(2)                      ;
170
        Reg(6)               <= Reg(14) xor Exr(3)                      ;
171
        Reg(7)               <= Reg(15) xor Exr(4)            xor Exr(0);
172
        Reg(8)               <= Exr(0)  xor Exr(5)            xor Exr(1);
173
        Reg(9)               <= Exr(1)  xor Exr(6)            xor Exr(2);
174
        Reg(10)              <= Exr(2)  xor Exr(7)            xor Exr(3);
175
        Reg(11)              <= Exr(3)                                  ;
176
        Reg(12)              <= Exr(4)                        xor Exr(0);
177
        Reg(13)              <= Exr(5)                        xor Exr(1);
178
        Reg(14)              <= Exr(6)                        xor Exr(2);
179
        Reg(15)              <= Exr(7)                        xor Exr(3);
180
      end if;
181
 
182
      if( Buffer_En = '1' )then
183
        Byte_Count           <= Byte_Count + 1;
184
        Comp_Data            <= Reg xor x"FFFF";
185
      end if;
186
 
187 180 jshamlet
    end if;
188
  end process;
189
 
190
end architecture;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.