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

Subversion Repositories galois_lfsr

[/] [galois_lfsr/] [trunk/] [rtl/] [user.vhdl] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 daniel.kho
/*
2
        This file is part of the Galois Linear Feedback Shift Register
3
        (galois_lfsr) project:
4
                http://www.opencores.org/project,galois_lfsr
5
 
6
        Description
7
        Synthesisable use case for Galois LFSR.
8
        This example is a CRC generator that uses a Galois LFSR.
9
 
10
        ToDo:
11
 
12
        Author(s):
13
        - Daniel C.K. Kho, daniel.kho@opencores.org | daniel.kho@tauhop.com
14
 
15
        Copyright (C) 2012-2013 Authors and OPENCORES.ORG
16
 
17
        This source file may be used and distributed without
18
        restriction provided that this copyright statement is not
19
        removed from the file and that any derivative work contains
20
        the original copyright notice and the associated disclaimer.
21
 
22
        This source file is free software; you can redistribute it
23
        and/or modify it under the terms of the GNU Lesser General
24
        Public License as published by the Free Software Foundation;
25
        either version 2.1 of the License, or (at your option) any
26
        later version.
27
 
28
        This source is distributed in the hope that it will be
29
        useful, but WITHOUT ANY WARRANTY; without even the implied
30
        warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
31
        PURPOSE. See the GNU Lesser General Public License for more
32
        details.
33
 
34
        You should have received a copy of the GNU Lesser General
35
        Public License along with this source; if not, download it
36
        from http://www.opencores.org/lgpl.shtml.
37
*/
38
library ieee; use ieee.std_logic_1164.all, ieee.numeric_std.all; use ieee.math_real.all;
39
--use work.types.all;
40
 
41
entity user is
42
        generic(
43
                parallelLoad:boolean:=false;
44
                tapVector:boolean_vector:=(
45
                        /* Example polynomial from Wikipedia. */
46
                        0|1|2|8=>true, 7 downto 3=>false
47
                )
48
        );
49
        port(
50
                /* Comment-out for simulation. */
51
                --clk,reset:in std_ulogic;
52
                msg:in unsigned(tapVector'length-1 downto 0):=9x"57";            -- Should be (tapVector'length downto 0). TODO report ModelSim bug.
53
                crc32:out unsigned(31 downto 0):=(others=>'0')
54
        );
55
end entity user;
56
 
57
architecture rtl of user is
58
        signal n,c:natural;
59
 
60
        /* Tester signals. */
61
        signal d:std_ulogic;
62
        /* synthesis translate_off */
63
        signal clk,reset:std_ulogic:='0';
64
        /* synthesis translate_on */
65
 
66
        signal loadEn,computeClk:std_ulogic;            -- clock gating.
67
        signal loaded,i_loaded:boolean;
68
        signal computed,i_computed:boolean;
69
 
70
begin
71
        /* Simulation Tester. */
72
        /* synthesis translate_off */
73
        clk<=not clk after 10 ps;
74
 
75
        process is begin
76
                reset<='0'; wait for 1 ps;
77
                reset<='1'; wait for 500 ps;
78
                reset<='0';
79
                wait;
80
        end process;
81
        /* synthesis translate_on */
82
 
83
        loadEn<=clk when reset='0' and not i_computed else '0';
84
        computeClk<=clk when reset='0' and i_loaded and not i_computed else '0';
85
 
86
        /* CRC computer using LFSR.
87
                E.g. Design a CRC-32 generator with the following polynomial (CRC-32 algorithm):
88
                        G(x) = x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10
89
                                        + x^8 + x^7 + x^5 + x^4 + x^2 + x^1 + 1
90
 
91
                Equivalent to the following hex code: 0x04C11DB7, with the MSbit discarded (assumed
92
                        present, but discarded in real implementation to save bits as it does not serve
93
                        any other purpose).
94
        */
95
        i_lfsr: entity work.lfsr(rtl)
96
                generic map(taps=>tapVector)
97
                /*generic map(taps => (
98
                        0|1|2|8=>true,
99
                        7 downto 3=>false
100
                ))*/
101
                port map(nReset=>not reset, clk=>loadEn,
102
                        load=>parallelLoad,
103
                        seed=>msg,
104
                        d=>d,
105
                        q=>crc32(msg'range)
106
        );
107
 
108
        /* Load message into LFSR. */
109
        process(reset,loadEn) is begin
110
                if reset then loaded<=false; n<=msg'length-1; d<='0';
111
                elsif rising_edge(loadEn) then
112
                        d<='0';
113
 
114
                        /* for parallel mode, LFSR automatically loads the seed in parallel. */
115
                        if parallelLoad then d<='0'; loaded<=true;
116
                        else
117
                                if not loaded then d<=msg(n); end if;
118
 
119
                                if n>0 then n<=n-1;
120
                                else loaded<=true;
121
                                end if;
122
                        end if;
123
                end if;
124
        end process;
125
 
126
        /* Shift zeroes into LFSR after message has been loaded completely. */
127
        process(reset,loaded,computeClk) is begin
128
                if reset='1' or not loaded then computed<=false; c<=msg'length-1;
129
                elsif rising_edge(computeClk) then
130
                        if c>0 then c<=c-1;
131
                        else computed<=true;
132
                        end if;
133
                end if;
134
        end process;
135
 
136
        /* Register pipelines. */
137
        process(clk) is begin
138
                if falling_edge(clk) then
139
                        i_loaded<=loaded;
140
                        i_computed<=computed;
141
                end if;
142
        end process;
143
end architecture rtl;

powered by: WebSVN 2.1.0

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