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 224

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 224 jshamlet
-- Seth Henry      04/16/20 Modified to use Open8 bus record
45 180 jshamlet
 
46
library ieee;
47
use ieee.std_logic_1164.all;
48
use ieee.std_logic_unsigned.all;
49
 
50
library work;
51
  use work.open8_pkg.all;
52
 
53
entity o8_crc16_ccitt is
54
generic(
55
  Address                    : ADDRESS_TYPE
56
);
57
port(
58 223 jshamlet
  Open8_Bus                  : in  OPEN8_BUS_TYPE;
59 180 jshamlet
  Rd_Data                    : out DATA_TYPE
60
);
61
end entity;
62
 
63
architecture behave of o8_crc16_ccitt is
64
 
65 224 jshamlet
  alias Clock                is Open8_Bus.Clock;
66
  alias Reset                is Open8_Bus.Reset;
67
 
68 213 jshamlet
  constant Poly_Init         : std_logic_vector(15 downto 0) :=
69
                                (others => '0');
70 180 jshamlet
 
71
  constant User_Addr         : std_logic_vector(15 downto 2)
72
                               := Address(15 downto 2);
73 223 jshamlet
  alias  Comp_Addr           is Open8_Bus.Address(15 downto 2);
74
  alias  Reg_Addr            is Open8_Bus.Address(1 downto 0);
75 213 jshamlet
  signal Reg_Sel             : std_logic_vector(1 downto 0) :=
76
                               (others => '0');
77 180 jshamlet
  signal Addr_Match          : std_logic;
78
  signal Wr_En               : std_logic;
79 213 jshamlet
  signal Wr_Data_q           : DATA_TYPE := (others => '0');
80 180 jshamlet
  signal Rd_En               : std_logic;
81
 
82 213 jshamlet
  signal Next_Byte           : DATA_TYPE := (others => '0');
83
  signal Byte_Count          : DATA_TYPE := (others => '0');
84 180 jshamlet
 
85 213 jshamlet
  signal Calc_En             : std_logic := '0';
86
  signal Buffer_En           : std_logic := '0';
87
  signal Data                : DATA_TYPE := (others => '0');
88
  signal Exr                 : DATA_TYPE := (others => '0');
89
  signal Reg                 : std_logic_vector(15 downto 0) :=
90
                                (others => '0');
91
  signal Comp_Data           : std_logic_vector(15 downto 0) :=
92
                                (others => '0');
93 180 jshamlet
 
94
begin
95
 
96
  Addr_Match                 <= '1' when Comp_Addr = User_Addr else '0';
97
 
98
  Exr(0)                     <= Reg(0) xor Data(0);
99
  Exr(1)                     <= Reg(1) xor Data(1);
100
  Exr(2)                     <= Reg(2) xor Data(2);
101
  Exr(3)                     <= Reg(3) xor Data(3);
102
  Exr(4)                     <= Reg(4) xor Data(4);
103
  Exr(5)                     <= Reg(5) xor Data(5);
104
  Exr(6)                     <= Reg(6) xor Data(6);
105
  Exr(7)                     <= Reg(7) xor Data(7);
106
 
107
  CRC16_Calc: process( Clock, Reset )
108
  begin
109
    if( Reset = Reset_Level )then
110
      Reg_Sel                <= "00";
111
      Wr_En                  <= '0';
112
      Wr_Data_q              <= x"00";
113 191 jshamlet
      Rd_En                  <= '0';
114
      Rd_Data                <= OPEN8_NULLBUS;
115 180 jshamlet
 
116
      Byte_Count             <= x"00";
117
      Calc_En                <= '0';
118
      Buffer_En              <= '0';
119
      Data                   <= x"00";
120
      Reg                    <= x"0000";
121
    elsif( rising_edge(Clock) )then
122 191 jshamlet
      Reg_Sel                <= Reg_Addr;
123
 
124 223 jshamlet
      Wr_En                  <= Addr_Match and Open8_Bus.Wr_En;
125
      Wr_Data_q              <= Open8_Bus.Wr_Data;
126 180 jshamlet
 
127
      if( Wr_En = '1' )then
128
        case( Reg_Sel )is
129
          when "00" => -- Load next byte
130
            Data             <= Wr_Data_q;
131
            Calc_En          <= '1';
132
 
133
          when "01" => -- Clear accumulator and byte counter
134
            Byte_Count       <= x"00";
135
            Reg              <= Poly_Init;
136
 
137
          when others => null;
138
        end case;
139
      end if;
140
 
141 223 jshamlet
      Rd_En                  <= Addr_Match and Open8_Bus.Rd_En;
142 191 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
143 180 jshamlet
      if( Rd_En = '1' )then
144
        case( Reg_Sel )is
145
          when "00" => -- Read last byte
146
            Rd_Data          <= Data;
147
 
148
          when "01" => -- Read the byte counter
149
            Rd_Data          <= Byte_Count;
150
 
151
          when "10" => -- Read the lower byte of the calculated CRC
152
            Rd_Data          <= Comp_Data(7 downto 0);
153
 
154
          when "11" => -- Read the upper byte of the calculated CRC
155
            Rd_Data          <= Comp_Data(15 downto 8);
156
 
157
          when others => null;
158
        end case;
159
      end if;
160
 
161 191 jshamlet
      Calc_En                <= '0';
162
      Buffer_En              <= Calc_En;
163
 
164
      if( Calc_En = '1' )then
165
        Reg(0)               <= Reg(8)  xor            Exr(4) xor Exr(0);
166
        Reg(1)               <= Reg(9)  xor            Exr(5) xor Exr(1);
167
        Reg(2)               <= Reg(10) xor            Exr(6) xor Exr(2);
168
        Reg(3)               <= Reg(11) xor Exr(0) xor Exr(7) xor Exr(3);
169
        Reg(4)               <= Reg(12) xor Exr(1)                      ;
170
        Reg(5)               <= Reg(13) xor Exr(2)                      ;
171
        Reg(6)               <= Reg(14) xor Exr(3)                      ;
172
        Reg(7)               <= Reg(15) xor Exr(4)            xor Exr(0);
173
        Reg(8)               <= Exr(0)  xor Exr(5)            xor Exr(1);
174
        Reg(9)               <= Exr(1)  xor Exr(6)            xor Exr(2);
175
        Reg(10)              <= Exr(2)  xor Exr(7)            xor Exr(3);
176
        Reg(11)              <= Exr(3)                                  ;
177
        Reg(12)              <= Exr(4)                        xor Exr(0);
178
        Reg(13)              <= Exr(5)                        xor Exr(1);
179
        Reg(14)              <= Exr(6)                        xor Exr(2);
180
        Reg(15)              <= Exr(7)                        xor Exr(3);
181
      end if;
182
 
183
      if( Buffer_En = '1' )then
184
        Byte_Count           <= Byte_Count + 1;
185
        Comp_Data            <= Reg xor x"FFFF";
186
      end if;
187
 
188 180 jshamlet
    end if;
189
  end process;
190
 
191
end architecture;

powered by: WebSVN 2.1.0

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