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

Subversion Repositories yahamm

[/] [yahamm/] [trunk/] [rtl/] [vhdl/] [matrix_pkg.vhd] - Blame information for rev 8

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
-- Author:
14
-- - Nicola De Simone, ndesimone@opencores.org
15
--
16
-------------------------------------------------------------------------------
17
--
18
-- Copyright (C) 2017 Authors and OPENCORES.ORG
19
--
20
-- This source file may be used and distributed without
21
-- restriction provided that this copyright statement is not
22
-- removed from the file and that any derivative work contains
23
-- the original copyright notice and the associated disclaimer.
24
--
25
-- This source file is free software; you can redistribute it
26
-- and/or modify it under the terms of the GNU Lesser General
27
-- Public License as published by the Free Software Foundation;
28
-- either version 2.1 of the License, or (at your option) any
29
-- later version.
30
--
31
-- This source is distributed in the hope that it will be
32
-- useful, but WITHOUT ANY WARRANTY; without even the implied
33
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
34
-- PURPOSE. See the GNU Lesser General Public License for more
35
-- details.
36
--
37
--- You should have received a copy of the GNU Lesser General
38
-- Public License along with this source; if not, download it
39
-- from http://www.opencores.org/lgpl.shtml
40
--
41
-------------------------------------------------------------------------------
42
 
43 3 ndesimone
library ieee;
44
use ieee.std_logic_1164.all;
45
 
46
use std.textio.all;
47
 
48 4 ndesimone
--library ieee_proposed;
49
--use ieee_proposed.std_logic_1164_additions.all;
50
--use ieee_proposed.standard_additions.all;
51
---- pragma synthesis_off
52
--use ieee_proposed.standard_textio_additions.all;
53
---- pragma synthesis_on
54 3 ndesimone
 
55
package matrix_pkg is
56
 
57
  -----------------------------------------------------------------------------
58
  -- Here 2D arrays are used to implement matrices.  There are various
59
  -- limitations to the use of 2D arrays (e.g. limited easy slicing)
60
  -- that would maybe suggest an implementation with 1Dx1D matrices.
61
  -- But anyway this would make easy slicing either rows or columns
62
  -- but not both.  The approach used here is implementing functions
63
  -- to handle various matrix operations.  Having a 2D type allows to
64
  -- use one type definition for any matrix.
65
  -----------------------------------------------------------------------------
66
 
67
  type matrix_t is array (integer range <>, integer range<>) of bit;
68
 
69
  function get_row (
70
    constant matrix : matrix_t;
71
    constant row    : natural)
72
    return bit_vector;
73
 
74
  function get_col (
75
    constant matrix : matrix_t;
76
    constant col    : natural)
77
    return bit_vector;
78
 
79
  procedure set_col (
80
    variable M : inout matrix_t;
81
    constant icol    : natural;
82 4 ndesimone
    constant col : bit_vector);
83 3 ndesimone
 
84
-- pragma synthesis_off
85
  procedure pretty_print_matrix (
86
    constant matrix : in matrix_t);
87
 
88
  procedure pretty_print_vector (
89
    constant vin : in bit_vector);
90
-- pragma synthesis_on
91
 
92
  function find_col (
93
    constant M   : matrix_t;
94
    constant col : bit_vector)
95 4 ndesimone
    return integer;
96 3 ndesimone
 
97
  procedure swap_cols (
98
    variable M       : inout matrix_t;
99
    constant icol1, icol2 : in natural);
100
 
101 4 ndesimone
  function and_reduce (L  : BIT_VECTOR) return BIT;
102
  function or_reduce (L  : BIT_VECTOR) return BIT;
103
  function xor_reduce (L  : BIT_VECTOR) return BIT;
104
 
105 3 ndesimone
end package matrix_pkg;
106
 
107
package body matrix_pkg is
108
 
109
  -- purpose: Return matrix M with columns at positions col1 and col2
110
  -- swapped.
111
  procedure swap_cols (
112
    variable M          : inout matrix_t;
113
    constant icol1, icol2 : in natural) is
114
 
115
    variable col1, col2 : bit_vector(M'range(1));
116
    variable Mout : matrix_t(M'range(1), M'range(2));
117
 
118
  begin
119
 
120
    col1 := get_col(M, icol1);
121
    col2 := get_col(M, icol2);
122
 
123
    set_col(M, icol1, col2);
124
    set_col(M, icol2, col1);
125
 
126
  end procedure swap_cols;
127
 
128
  -- purpose: Return the matrix row.
129
  function get_row (
130
    constant matrix : matrix_t;
131
    constant row    : natural)
132
    return bit_vector is
133
    variable y : bit_vector(matrix'range(2));
134
  begin  -- function get_row
135
 
136
    for col in matrix'range(2) loop
137
      y(col) := matrix(row, col);
138
    end loop;  -- irow
139
 
140
    return y;
141
  end function get_row;
142
 
143
  -- purpose: Return the matrix col.
144
  function get_col (
145
    constant matrix : matrix_t;
146
    constant col    : natural)
147
    return bit_vector is
148
    variable y : bit_vector(matrix'range(1));
149
  begin  -- function get_col
150
 
151
    for row in matrix'range(1) loop
152
      y(row) := matrix(row, col);
153
    end loop;  -- irow
154
 
155
    return y;
156
  end function get_col;
157
 
158
  -- purpose: Set the matrix column icol to col.
159
  procedure set_col (
160
    variable M : inout matrix_t;
161
    constant icol    : natural;
162
    constant col : bit_vector) is
163
  begin  -- function get_col
164
 
165
    for row in M'range(1) loop
166
      M(row, icol) := col(row);
167
    end loop;  -- irow
168
 
169
  end procedure set_col;
170
 
171
-- pragma synthesis_off
172
 
173
  -- purpose: Print out matrix for debugging.
174
  procedure pretty_print_matrix (
175
    constant matrix : in matrix_t) is
176
    variable l : line;
177
 
178
  begin  -- procedure pretty_print_matrix
179
 
180
    for row in matrix'range(1) loop
181
 
182
      for col in matrix'range(2) loop
183
        write(l, matrix(row, col));
184
        swrite(l, " ");
185
      end loop;  -- col
186
 
187
      writeline(output, l);
188
    end loop;  -- row
189
 
190
    writeline(output, l);               -- blank line
191
 
192
  end procedure pretty_print_matrix;
193
 
194
  -- purpose: Print out vector for debugging.
195
  procedure pretty_print_vector (
196
    constant vin : in bit_vector) is
197
    variable l : line;
198
 
199
  begin  -- procedure pretty_print_matrix
200
 
201
    swrite(l, integer'image(vin'left));
202
    swrite(l, " to/downto ");
203
    swrite(l, integer'image(vin'right));
204
    swrite(l, ": ");
205
    for i in vin'range loop
206
      write(l, vin(i));
207
      swrite(l, " ");
208
    end loop;  -- i
209
 
210
    writeline(output, l);
211
    writeline(output, l);               -- blank line
212
 
213
  end procedure pretty_print_vector;
214
 
215
-- pragma synthesis_on
216
 
217
  -- purpose: Return the position of col in M or -1 if not found.
218
  function find_col (
219
    constant M   : matrix_t;                -- Any matrix
220
    constant col : bit_vector)  -- The column to find
221
    return integer is
222
    variable match : boolean;
223
  begin  -- function find_col
224
    for icol in M'range(2) loop
225
 
226
      match := true;
227
      for j in col'range loop
228
        if col(j) /= M(j, icol) then
229
          match := false;
230
        end if;
231
      end loop;
232
 
233
      if match then
234
        return icol;
235
      end if;
236
 
237
    end loop;
238
 
239
    -- not found
240
    return -1;
241
  end function find_col;
242
 
243 4 ndesimone
  function and_reduce (L : BIT_VECTOR) return BIT is
244
    variable result : BIT := '1';
245
  begin
246
    for i in l'reverse_range loop
247
      result := l(i) and result;
248
    end loop;
249
    return result;
250
  end function and_reduce;
251
 
252
  function or_reduce (L : BIT_VECTOR) return BIT is
253
    variable result : BIT := '0';
254
  begin
255
    for i in l'reverse_range loop
256
      result := l(i) or result;
257
    end loop;
258
    return result;
259
  end function or_reduce;
260
 
261
  function xor_reduce (L : BIT_VECTOR) return BIT is
262
    variable result : BIT := '0';
263
  begin
264
    for i in l'reverse_range loop
265
      result := l(i) xor result;
266
    end loop;
267
    return result;
268
  end function xor_reduce;
269
 
270 3 ndesimone
end package body matrix_pkg;

powered by: WebSVN 2.1.0

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