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

Subversion Repositories yahamm

[/] [yahamm/] [trunk/] [rtl/] [vhdl/] [yahamm_enc.vhd] - Blame information for rev 5

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

Line No. Rev Author Line
1 4 ndesimone
-------------------------------------------------------------------------------
2
-- Yahamm IP core
3
--
4
-- This file is part of the Yahamm project
5
-- http://www.opencores.org/cores/yahamm
6
--
7
-- Description
8
-- A hamming encoder and decoder with single-error correcting and
9
-- double-error detecting capability. The message length can be configured
10
-- through a generic. Both the code generator matrix and the parity-check
11
-- matrix are computed in the VHDL itself.
12
--
13
-- To Do:
14
-- - write docs
15
--
16
-- Author:
17
-- - Nicola De Simone, ndesimone@opencores.org
18
--
19
-------------------------------------------------------------------------------
20
--
21
-- Copyright (C) 2017 Authors and OPENCORES.ORG
22
--
23
-- This source file may be used and distributed without
24
-- restriction provided that this copyright statement is not
25
-- removed from the file and that any derivative work contains
26
-- the original copyright notice and the associated disclaimer.
27
--
28
-- This source file is free software; you can redistribute it
29
-- and/or modify it under the terms of the GNU Lesser General
30
-- Public License as published by the Free Software Foundation;
31
-- either version 2.1 of the License, or (at your option) any
32
-- later version.
33
--
34
-- This source is distributed in the hope that it will be
35
-- useful, but WITHOUT ANY WARRANTY; without even the implied
36
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
37
-- PURPOSE. See the GNU Lesser General Public License for more
38
-- details.
39
--
40
--- You should have received a copy of the GNU Lesser General
41
-- Public License along with this source; if not, download it
42
-- from http://www.opencores.org/lgpl.shtml
43
--
44
-------------------------------------------------------------------------------
45
 
46 2 ndesimone
-- CORRECT: set to true to correct errors at the cost of decreasing error
47
-- detection (see table).
48
--
49
-- SEC = single bit error corrected
50
-- SED = single bit error detected
51
-- DED = double bit error detected
52
-- TED = triple bit error detected
53
--
54
-- EXTRA_PARITY_BIT | CORRECT       FALSE           TRUE
55
-------------------------------------------------------------------------------
56
--      FALSE       |               SED-DED         SEC
57
--                  -----------------------------------------------------------
58
--      TRUE        |               SED-DED-TED     SEC-DED
59
-------------------------------------------------------------------------------
60
--
61
-- Note that, for example, SEC-DED (EXTRA_PARITY_BIT = true, CORRECT =
62
-- true) means that triple bit errors are not detected and messages
63
-- will be wrongly corrected because the correction corrects toward
64
-- the code word within the smaller hamming distance.  Practically you
65
-- usually know that something is very wrong with your communication
66
-- channel because you will also see double bit errors.  Then you
67
-- should not trust corrected data at all.
68
 
69
library ieee;
70
use ieee.std_logic_1164.all;
71
 
72
library yahamm;
73
use yahamm.matrix_pkg.all;
74 4 ndesimone
use yahamm.yahamm_pkg.all;
75 2 ndesimone
 
76
entity yahamm_enc is
77
  generic (
78
    MESSAGE_LENGTH       : natural := 5;
79
    EXTRA_PARITY_BIT : natural range 0 to 1 := 1;
80
    ONE_PARITY_BIT : boolean := false
81
    );
82
  port(
83 5 ndesimone
    clk_i, rst_i : in  std_logic;
84
    en_i       : in  std_logic := '1';          -- Synchronous output enable .
85
    data_i        : in  std_logic_vector(MESSAGE_LENGTH - 1 downto 0);  -- Input data.
86
    data_o        : out std_logic_vector(MESSAGE_LENGTH - 1 downto 0);  -- Out data.
87
    data_valid_o : out std_logic;
88
    parity_o   : out std_logic_vector(calc_nparity_bits(MESSAGE_LENGTH, ONE_PARITY_BIT) + EXTRA_PARITY_BIT - 1 downto 0)    -- Parity bits.
89 2 ndesimone
    );
90
 
91
end yahamm_enc;
92
 
93
architecture std of yahamm_enc is
94
 
95
  constant NPARITY_BITS : natural := calc_nparity_bits(MESSAGE_LENGTH, ONE_PARITY_BIT);
96
  constant BLOCK_LENGTH : natural := calc_block_length(MESSAGE_LENGTH, ONE_PARITY_BIT);
97
 
98
  constant G : matrix_t(0 to BLOCK_LENGTH + EXTRA_PARITY_BIT - 1,
99
 
100
    get_code_generator_matrix(MESSAGE_LENGTH, EXTRA_PARITY_BIT, ONE_PARITY_BIT);
101
 
102 4 ndesimone
  -- G'reverse_range(1) doesn't work in ghdl 0.34
103
  --signal code_sys : std_ulogic_vector(G'reverse_range(1)); -- systematic code
104
  signal code_sys : std_ulogic_vector(BLOCK_LENGTH + EXTRA_PARITY_BIT -1 downto 0); -- systematic code
105
 
106 5 ndesimone
  signal data_i_padded : bit_vector(BLOCK_LENGTH - NPARITY_BITS - 1 downto 0);
107 2 ndesimone
 
108 4 ndesimone
 
109 2 ndesimone
begin
110
 
111
  check_parameters(BLOCK_LENGTH, NPARITY_BITS, MESSAGE_LENGTH, EXTRA_PARITY_BIT, ONE_PARITY_BIT);
112
 
113 5 ndesimone
  data_i_padded(MESSAGE_LENGTH - 1 downto 0) <= to_bitvector(data_i);
114 2 ndesimone
  pad_gen: if MESSAGE_LENGTH < BLOCK_LENGTH - NPARITY_BITS generate
115 5 ndesimone
    -- Pad data_i with zeros on the left, so that data_i_padded'length = BLOCK_LENGTH.
116
    -- This allow the user to reduce data_i width.
117
    data_i_padded(BLOCK_LENGTH - NPARITY_BITS - 1 downto MESSAGE_LENGTH) <= (others => '0');
118 2 ndesimone
  end generate pad_gen;
119
 
120 5 ndesimone
  -- Wire systematic code signal code_sys on data_o and parity_o output ports.
121 2 ndesimone
  -- Because of the form of the code generator matrix G, data are the LSB part
122
  -- of code and parity the MSB part.
123 5 ndesimone
  parity_o <= to_slv(code_sys(code_sys'high downto code_sys'high - (EXTRA_PARITY_BIT + NPARITY_BITS) + 1));
124
  data_o <= to_slv(code_sys(MESSAGE_LENGTH - 1 downto 0));
125 2 ndesimone
 
126
  -- purpose: Sequentially encode input with output enable.
127
  -- type   : sequential
128 5 ndesimone
  -- inputs : clk_i, rst_i, d_sig
129 2 ndesimone
  -- outputs: msg_sys
130 5 ndesimone
  encode_proc: process (clk_i, rst_i) is
131 2 ndesimone
  begin  -- process encode_proc
132 5 ndesimone
    if rst_i = '1' then                   -- asynchronous reset (active high)
133 2 ndesimone
      code_sys <= (others => '0');
134 5 ndesimone
      data_valid_o <= '0';
135
    elsif rising_edge(clk_i) then         -- rising clock edge
136 2 ndesimone
 
137 5 ndesimone
      if en_i = '0' then                  -- syncronous output enable
138 2 ndesimone
        code_sys <= (others => '0');
139 5 ndesimone
        data_valid_o <= '0';
140 2 ndesimone
      else
141 5 ndesimone
        code_sys <= To_StdULogicVector(xor_multiply_vec(G, data_i_padded));
142
        data_valid_o <= '1';
143 2 ndesimone
      end if;
144
 
145
    end if;
146
  end process encode_proc;
147
 
148
end architecture std;

powered by: WebSVN 2.1.0

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