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

Subversion Repositories heap_sorter

[/] [heap_sorter/] [trunk/] [high_speed_pipelined_4clk_per_word/] [src/] [sorter_pkg.vhd] - Blame information for rev 5

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

Line No. Rev Author Line
1 5 wzab
-------------------------------------------------------------------------------
2
-- Title      : Definitions for heap-sorter
3
-- Project    : heap-sorter
4
-------------------------------------------------------------------------------
5
-- File       : sorter_pkg.vhd
6
-- Author     : Wojciech M. Zabolotny <wzab@ise.pw.edu.pl>
7
-- Company    : 
8
-- Created    : 2010-05-14
9
-- Last update: 2011-07-11
10
-- Platform   : 
11
-- Standard   : VHDL'93
12
-------------------------------------------------------------------------------
13
-- Description: 
14
-------------------------------------------------------------------------------
15
-- Copyright (c) 2010 Wojciech M. Zabolotny
16
-- This file is published under the BSD license, so you can freely adapt
17
-- it for your own purposes.
18
-- Additionally this design has been described in my article:
19
--    Wojciech M. Zabolotny, "Dual port memory based Heapsort implementation
20
--    for FPGA", Proc. SPIE 8008, 80080E (2011); doi:10.1117/12.905281
21
-- I'd be glad if you cite this article when you publish something based
22
-- on my design.
23
-------------------------------------------------------------------------------
24
-- Revisions  :
25
-- Date        Version  Author  Description
26
-- 2010-05-14  1.0      wzab    Created
27
-------------------------------------------------------------------------------
28
library ieee;
29
use ieee.std_logic_1164.all;
30
use ieee.numeric_std.all;
31
use ieee.std_logic_textio.all;
32
use std.textio.all;
33
library work;
34
use work.sys_config.all;
35
 
36
package sorter_pkg is
37
  constant DATA_REC_WIDTH : integer := DATA_REC_SORT_KEY_WIDTH +
38
                                       DATA_REC_PAYLOAD_WIDTH + 2;
39
 
40
 
41
  subtype T_SORT_KEY is unsigned (DATA_REC_SORT_KEY_WIDTH - 1 downto 0);
42
  subtype T_PAYLOAD is std_logic_vector(DATA_REC_PAYLOAD_WIDTH - 1 downto 0);
43
 
44
  --alias T_SORT_KEY is unsigned (12 downto 0);
45
  type T_DATA_REC is record
46
    d_key     : T_SORT_KEY;
47
    init      : std_logic;
48
    valid     : std_logic;
49
    d_payload : T_PAYLOAD;
50
  end record;
51
 
52
  type T_DATA_REC_ARR is array (natural range <>) of T_DATA_REC;
53
 
54
  -- Special constant used to initially fill the sorter
55
  -- Must be sorted so, that is smaller, than any other data
56
  constant DATA_REC_INIT_DATA : T_DATA_REC := (
57
    d_key     => to_unsigned(0, DATA_REC_SORT_KEY_WIDTH),
58
    init      => '1',
59
    valid     => '0',
60
    d_payload => (others => '0')
61
    );
62
 
63
  -- Special constant used to ``flush'' the sorter at the end
64
  constant DATA_REC_END_DATA : T_DATA_REC := (
65
    d_key     => to_unsigned(0, DATA_REC_SORT_KEY_WIDTH),
66
    init      => '1',
67
    valid     => '1',
68
    d_payload => (others => '0')
69
    );
70
 
71
 
72
  function sort_cmp_lt (
73
    constant v1 : T_DATA_REC;
74
    constant v2 : T_DATA_REC)
75
    return boolean;
76
 
77
  function tdrec2stlv (
78
    constant drec : T_DATA_REC)
79
    return std_logic_vector;
80
 
81
  function stlv2tdrec (
82
    constant dstlv : std_logic_vector)
83
    return T_DATA_REC;
84
 
85
  --procedure wrstlv (
86
  --  rline         : inout line;
87
  --  constant vect :       std_logic_vector);
88
 
89
  file reports : text open write_mode is "STD_OUTPUT";
90
 
91
end sorter_pkg;
92
 
93
package body sorter_pkg is
94
 
95
  function stlv2tdrec (
96
    constant dstlv : std_logic_vector)
97
    return T_DATA_REC is
98
    variable result : T_DATA_REC;
99
    variable j      : integer := 0;
100
  begin  -- stlv2drec
101
    j                := 0;
102
    result.d_key     := unsigned(dstlv(j-1+DATA_REC_SORT_KEY_WIDTH downto j));
103
    j                := j+DATA_REC_SORT_KEY_WIDTH;
104
    result.valid     := dstlv(j);
105
    j                := j+1;
106
    result.init      := dstlv(j);
107
    j                := j+1;
108
    result.d_payload := dstlv(j-1+DATA_REC_PAYLOAD_WIDTH downto j);
109
    j                := j+DATA_REC_PAYLOAD_WIDTH;
110
    return result;
111
  end stlv2tdrec;
112
 
113
  function tdrec2stlv (
114
    constant drec : T_DATA_REC)
115
    return std_logic_vector is
116
    variable result : std_logic_vector(DATA_REC_WIDTH-1 downto 0);
117
    variable j      : integer := 0;
118
  begin  -- tdrec2stlv
119
    j                                            := 0;
120
    result(j-1+DATA_REC_SORT_KEY_WIDTH downto j) := std_logic_vector(drec.d_key);
121
    j                                            := j+DATA_REC_SORT_KEY_WIDTH;
122
    result(j)                                    := drec.valid;
123
    j                                            := j+1;
124
    result(j)                                    := drec.init;
125
    j                                            := j+1;
126
    result(j-1+DATA_REC_PAYLOAD_WIDTH downto j)  := std_logic_vector(drec.d_payload);
127
    j                                            := j+DATA_REC_PAYLOAD_WIDTH;
128
    return result;
129
  end tdrec2stlv;
130
 
131
 
132
  -- Function sort_cmp_lt returns TRUE when the first opperand is ``less'' than
133
  -- the second one
134
  function sort_cmp_lt (
135
    constant v1 : T_DATA_REC;
136
    constant v2 : T_DATA_REC)
137
    return boolean is
138
    variable rline : line;
139
    variable dcomp  : unsigned(DATA_REC_SORT_KEY_WIDTH-1 downto 0) := (others => '0');
140
  begin  -- sort_cmp_lt
141
    -- Check the special cases
142
    if (v1.init = '1') and (v2.init = '0') then
143
      -- v1 is the special record, v2 is the standard one
144
      if v1.valid = '0' then
145
        -- initialization record - ``smaller'' than all standard records
146
        return true;
147
      else
148
        -- end record - ``bigger'' than all standard records
149
        return false;
150
      end if;
151
    elsif (v1.init = '0') and (v2.init = '1') then
152
      -- v2 is the special record, v1 is the standard one      
153
      if (v2.valid = '0') then
154
        -- v2 is the initialization record - it is ``smaller'' than standard record v1
155
        return false;
156
      else
157
        -- v2 is the end record - it is ``bigger'' than standard record v1
158
        return true;
159
      end if;
160
    elsif (v1.init = '1') and (v2.init = '1') then
161
      -- both v1 and v2 are special records
162
      if (v1.valid = '0') and (v2.valid = '1') then
163
        -- v1 - initial record, v2 - end record
164
        return true;
165
      else
166
        -- v1 is end record, so it is ``bigger'' or ``equal'' to other records
167
        return false;
168
      end if;
169
    elsif (v1.init = '0') and (v2.init = '0') then
170
      -- We compare standard words
171
      -- We must consider the fact, that in longer sequences of data records
172
      -- the sort keys may wrap around
173
      -- therefore we perform subtraction modulo
174
      -- 2**DATA_REC_SORT_KEY_WIDTH and check the MSB
175
      dcomp := v1.d_key-v2.d_key;
176
      if dcomp(DATA_REC_SORT_KEY_WIDTH-1) = '1' then
177
      --if signed(v1.d_key - v2.d_key)<0 then -- old implementation
178
        return true;
179
      elsif v2.d_key = v1.d_key then
180
        if v2.valid = '1' then
181
          return true;
182
        else
183
          -- Empty data records should wait
184
          return false;
185
        end if;
186
      else
187
        return false;
188
      end if;
189
    else
190
      --assert false report "Wrong records in sort_cmp_lt" severity error;
191
      return false;
192
    end if;
193
    return false;                       -- should never happen
194
  end sort_cmp_lt;
195
 
196
 
197
  --procedure wrstlv (
198
  --  rline         : inout string;
199
  --  constant vect :       std_logic_vector) is
200
  --begin  -- stlv2str
201
  --  for i in vect'left downto vect'right loop
202
  --    case vect(i) is
203
  --      when 'U'    => write(line(rline), string'("u"));
204
  --      when 'Z'    => write(line(rline), string'("z"));
205
  --      when 'X'    => write(line(rline), string'("x"));
206
  --      when 'L'    => write(line(rline), string'("L"));
207
  --      when 'H'    => write(line(rline), string'("H"));
208
  --      when '1'    => write(line(rline), string'("1"));
209
  --      when '0'    => write(line(rline), string'("0"));
210
  --      when others => write(line(rline), string'("?"));
211
  --    end case;
212
  --  end loop;  -- i
213
  --end wrstlv;
214
 
215
end sorter_pkg;
216
 

powered by: WebSVN 2.1.0

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