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 221

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 213 jshamlet
  constant Poly_Init         : std_logic_vector(15 downto 0) :=
72
                                (others => '0');
73 180 jshamlet
 
74
  constant User_Addr         : std_logic_vector(15 downto 2)
75
                               := Address(15 downto 2);
76
  alias  Comp_Addr           is Bus_Address(15 downto 2);
77
  alias  Reg_Addr            is Bus_Address(1 downto 0);
78 213 jshamlet
  signal Reg_Sel             : std_logic_vector(1 downto 0) :=
79
                               (others => '0');
80 180 jshamlet
  signal Addr_Match          : std_logic;
81
  signal Wr_En               : std_logic;
82 213 jshamlet
  signal Wr_Data_q           : DATA_TYPE := (others => '0');
83 180 jshamlet
  signal Rd_En               : std_logic;
84
 
85 213 jshamlet
  signal Next_Byte           : DATA_TYPE := (others => '0');
86
  signal Byte_Count          : DATA_TYPE := (others => '0');
87 180 jshamlet
 
88 213 jshamlet
  signal Calc_En             : std_logic := '0';
89
  signal Buffer_En           : std_logic := '0';
90
  signal Data                : DATA_TYPE := (others => '0');
91
  signal Exr                 : DATA_TYPE := (others => '0');
92
  signal Reg                 : std_logic_vector(15 downto 0) :=
93
                                (others => '0');
94
  signal Comp_Data           : std_logic_vector(15 downto 0) :=
95
                                (others => '0');
96 180 jshamlet
 
97
begin
98
 
99
  Addr_Match                 <= '1' when Comp_Addr = User_Addr else '0';
100
 
101
  Exr(0)                     <= Reg(0) xor Data(0);
102
  Exr(1)                     <= Reg(1) xor Data(1);
103
  Exr(2)                     <= Reg(2) xor Data(2);
104
  Exr(3)                     <= Reg(3) xor Data(3);
105
  Exr(4)                     <= Reg(4) xor Data(4);
106
  Exr(5)                     <= Reg(5) xor Data(5);
107
  Exr(6)                     <= Reg(6) xor Data(6);
108
  Exr(7)                     <= Reg(7) xor Data(7);
109
 
110
  CRC16_Calc: process( Clock, Reset )
111
  begin
112
    if( Reset = Reset_Level )then
113
      Reg_Sel                <= "00";
114
      Wr_En                  <= '0';
115
      Wr_Data_q              <= x"00";
116 191 jshamlet
      Rd_En                  <= '0';
117
      Rd_Data                <= OPEN8_NULLBUS;
118 180 jshamlet
 
119
      Byte_Count             <= x"00";
120
      Calc_En                <= '0';
121
      Buffer_En              <= '0';
122
      Data                   <= x"00";
123
      Reg                    <= x"0000";
124
    elsif( rising_edge(Clock) )then
125 191 jshamlet
      Reg_Sel                <= Reg_Addr;
126
 
127 180 jshamlet
      Wr_En                  <= Addr_Match and Wr_Enable;
128
      Wr_Data_q              <= Wr_Data;
129
 
130
      if( Wr_En = '1' )then
131
        case( Reg_Sel )is
132
          when "00" => -- Load next byte
133
            Data             <= Wr_Data_q;
134
            Calc_En          <= '1';
135
 
136
          when "01" => -- Clear accumulator and byte counter
137
            Byte_Count       <= x"00";
138
            Reg              <= Poly_Init;
139
 
140
          when others => null;
141
        end case;
142
      end if;
143
 
144 191 jshamlet
      Rd_En                  <= Addr_Match and Rd_Enable;
145
      Rd_Data                <= OPEN8_NULLBUS;
146 180 jshamlet
      if( Rd_En = '1' )then
147
        case( Reg_Sel )is
148
          when "00" => -- Read last byte
149
            Rd_Data          <= Data;
150
 
151
          when "01" => -- Read the byte counter
152
            Rd_Data          <= Byte_Count;
153
 
154
          when "10" => -- Read the lower byte of the calculated CRC
155
            Rd_Data          <= Comp_Data(7 downto 0);
156
 
157
          when "11" => -- Read the upper byte of the calculated CRC
158
            Rd_Data          <= Comp_Data(15 downto 8);
159
 
160
          when others => null;
161
        end case;
162
      end if;
163
 
164 191 jshamlet
      Calc_En                <= '0';
165
      Buffer_En              <= Calc_En;
166
 
167
      if( Calc_En = '1' )then
168
        Reg(0)               <= Reg(8)  xor            Exr(4) xor Exr(0);
169
        Reg(1)               <= Reg(9)  xor            Exr(5) xor Exr(1);
170
        Reg(2)               <= Reg(10) xor            Exr(6) xor Exr(2);
171
        Reg(3)               <= Reg(11) xor Exr(0) xor Exr(7) xor Exr(3);
172
        Reg(4)               <= Reg(12) xor Exr(1)                      ;
173
        Reg(5)               <= Reg(13) xor Exr(2)                      ;
174
        Reg(6)               <= Reg(14) xor Exr(3)                      ;
175
        Reg(7)               <= Reg(15) xor Exr(4)            xor Exr(0);
176
        Reg(8)               <= Exr(0)  xor Exr(5)            xor Exr(1);
177
        Reg(9)               <= Exr(1)  xor Exr(6)            xor Exr(2);
178
        Reg(10)              <= Exr(2)  xor Exr(7)            xor Exr(3);
179
        Reg(11)              <= Exr(3)                                  ;
180
        Reg(12)              <= Exr(4)                        xor Exr(0);
181
        Reg(13)              <= Exr(5)                        xor Exr(1);
182
        Reg(14)              <= Exr(6)                        xor Exr(2);
183
        Reg(15)              <= Exr(7)                        xor Exr(3);
184
      end if;
185
 
186
      if( Buffer_En = '1' )then
187
        Byte_Count           <= Byte_Count + 1;
188
        Comp_Data            <= Reg xor x"FFFF";
189
      end if;
190
 
191 180 jshamlet
    end if;
192
  end process;
193
 
194
end architecture;

powered by: WebSVN 2.1.0

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