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

Subversion Repositories yahamm

[/] [yahamm/] [trunk/] [rtl/] [vhdl/] [matrix_pkg.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 3 ndesimone
library ieee;
47
use ieee.std_logic_1164.all;
48
 
49
use std.textio.all;
50
 
51 4 ndesimone
--library ieee_proposed;
52
--use ieee_proposed.std_logic_1164_additions.all;
53
--use ieee_proposed.standard_additions.all;
54
---- pragma synthesis_off
55
--use ieee_proposed.standard_textio_additions.all;
56
---- pragma synthesis_on
57 3 ndesimone
 
58
package matrix_pkg is
59
 
60
  -----------------------------------------------------------------------------
61
  -- Here 2D arrays are used to implement matrices.  There are various
62
  -- limitations to the use of 2D arrays (e.g. limited easy slicing)
63
  -- that would maybe suggest an implementation with 1Dx1D matrices.
64
  -- But anyway this would make easy slicing either rows or columns
65
  -- but not both.  The approach used here is implementing functions
66
  -- to handle various matrix operations.  Having a 2D type allows to
67
  -- use one type definition for any matrix.
68
  -----------------------------------------------------------------------------
69
 
70
  type matrix_t is array (integer range <>, integer range<>) of bit;
71
 
72
  function get_row (
73
    constant matrix : matrix_t;
74
    constant row    : natural)
75
    return bit_vector;
76
 
77
  function get_col (
78
    constant matrix : matrix_t;
79
    constant col    : natural)
80
    return bit_vector;
81
 
82
  procedure set_col (
83
    variable M : inout matrix_t;
84
    constant icol    : natural;
85 4 ndesimone
    constant col : bit_vector);
86 3 ndesimone
 
87
-- pragma synthesis_off
88
  procedure pretty_print_matrix (
89
    constant matrix : in matrix_t);
90
 
91
  procedure pretty_print_vector (
92
    constant vin : in bit_vector);
93
-- pragma synthesis_on
94
 
95
  function find_col (
96
    constant M   : matrix_t;
97
    constant col : bit_vector)
98 4 ndesimone
    return integer;
99 3 ndesimone
 
100
  procedure swap_cols (
101
    variable M       : inout matrix_t;
102
    constant icol1, icol2 : in natural);
103
 
104 4 ndesimone
  function and_reduce (L  : BIT_VECTOR) return BIT;
105
  function or_reduce (L  : BIT_VECTOR) return BIT;
106
  function xor_reduce (L  : BIT_VECTOR) return BIT;
107
 
108 3 ndesimone
end package matrix_pkg;
109
 
110
package body matrix_pkg is
111
 
112
  -- purpose: Return matrix M with columns at positions col1 and col2
113
  -- swapped.
114
  procedure swap_cols (
115
    variable M          : inout matrix_t;
116
    constant icol1, icol2 : in natural) is
117
 
118
    variable col1, col2 : bit_vector(M'range(1));
119
    variable Mout : matrix_t(M'range(1), M'range(2));
120
 
121
  begin
122
 
123
    col1 := get_col(M, icol1);
124
    col2 := get_col(M, icol2);
125
 
126
    set_col(M, icol1, col2);
127
    set_col(M, icol2, col1);
128
 
129
  end procedure swap_cols;
130
 
131
  -- purpose: Return the matrix row.
132
  function get_row (
133
    constant matrix : matrix_t;
134
    constant row    : natural)
135
    return bit_vector is
136
    variable y : bit_vector(matrix'range(2));
137
  begin  -- function get_row
138
 
139
    for col in matrix'range(2) loop
140
      y(col) := matrix(row, col);
141
    end loop;  -- irow
142
 
143
    return y;
144
  end function get_row;
145
 
146
  -- purpose: Return the matrix col.
147
  function get_col (
148
    constant matrix : matrix_t;
149
    constant col    : natural)
150
    return bit_vector is
151
    variable y : bit_vector(matrix'range(1));
152
  begin  -- function get_col
153
 
154
    for row in matrix'range(1) loop
155
      y(row) := matrix(row, col);
156
    end loop;  -- irow
157
 
158
    return y;
159
  end function get_col;
160
 
161
  -- purpose: Set the matrix column icol to col.
162
  procedure set_col (
163
    variable M : inout matrix_t;
164
    constant icol    : natural;
165
    constant col : bit_vector) is
166
  begin  -- function get_col
167
 
168
    for row in M'range(1) loop
169
      M(row, icol) := col(row);
170
    end loop;  -- irow
171
 
172
  end procedure set_col;
173
 
174
-- pragma synthesis_off
175
 
176
  -- purpose: Print out matrix for debugging.
177
  procedure pretty_print_matrix (
178
    constant matrix : in matrix_t) is
179
    variable l : line;
180
 
181
  begin  -- procedure pretty_print_matrix
182
 
183
    for row in matrix'range(1) loop
184
 
185
      for col in matrix'range(2) loop
186
        write(l, matrix(row, col));
187
        swrite(l, " ");
188
      end loop;  -- col
189
 
190
      writeline(output, l);
191
    end loop;  -- row
192
 
193
    writeline(output, l);               -- blank line
194
 
195
  end procedure pretty_print_matrix;
196
 
197
  -- purpose: Print out vector for debugging.
198
  procedure pretty_print_vector (
199
    constant vin : in bit_vector) is
200
    variable l : line;
201
 
202
  begin  -- procedure pretty_print_matrix
203
 
204
    swrite(l, integer'image(vin'left));
205
    swrite(l, " to/downto ");
206
    swrite(l, integer'image(vin'right));
207
    swrite(l, ": ");
208
    for i in vin'range loop
209
      write(l, vin(i));
210
      swrite(l, " ");
211
    end loop;  -- i
212
 
213
    writeline(output, l);
214
    writeline(output, l);               -- blank line
215
 
216
  end procedure pretty_print_vector;
217
 
218
-- pragma synthesis_on
219
 
220
  -- purpose: Return the position of col in M or -1 if not found.
221
  function find_col (
222
    constant M   : matrix_t;                -- Any matrix
223
    constant col : bit_vector)  -- The column to find
224
    return integer is
225
    variable match : boolean;
226
  begin  -- function find_col
227
    for icol in M'range(2) loop
228
 
229
      match := true;
230
      for j in col'range loop
231
        if col(j) /= M(j, icol) then
232
          match := false;
233
        end if;
234
      end loop;
235
 
236
      if match then
237
        return icol;
238
      end if;
239
 
240
    end loop;
241
 
242
    -- not found
243
    return -1;
244
  end function find_col;
245
 
246 4 ndesimone
  function and_reduce (L : BIT_VECTOR) return BIT is
247
    variable result : BIT := '1';
248
  begin
249
    for i in l'reverse_range loop
250
      result := l(i) and result;
251
    end loop;
252
    return result;
253
  end function and_reduce;
254
 
255
  function or_reduce (L : BIT_VECTOR) return BIT is
256
    variable result : BIT := '0';
257
  begin
258
    for i in l'reverse_range loop
259
      result := l(i) or result;
260
    end loop;
261
    return result;
262
  end function or_reduce;
263
 
264
  function xor_reduce (L : BIT_VECTOR) return BIT is
265
    variable result : BIT := '0';
266
  begin
267
    for i in l'reverse_range loop
268
      result := l(i) xor result;
269
    end loop;
270
    return result;
271
  end function xor_reduce;
272
 
273 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.