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

Subversion Repositories heap_sorter

[/] [heap_sorter/] [trunk/] [simplified_version/] [src/] [sorter_pkg.vhd] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 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 7 wzab
-- Last update: 2018-03-21
10 2 wzab
-- 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 7 wzab
    invalid     : std_logic;
48 2 wzab
    d_payload : T_PAYLOAD;
49
  end record;
50
 
51
  -- Special constant used to initially fill the sorter
52
  -- Must be sorted so, that is smaller, than any other data
53
  constant DATA_REC_INIT_DATA : T_DATA_REC := (
54
    d_key     => to_unsigned(0, DATA_REC_SORT_KEY_WIDTH),
55 7 wzab
    invalid     => '1',
56 2 wzab
    d_payload => (others => '0')
57
    );
58
 
59
  -- Special constant used to ``flush'' the sorter at the end
60
  constant DATA_REC_END_DATA : T_DATA_REC := (
61
    d_key     => to_unsigned(0, DATA_REC_SORT_KEY_WIDTH),
62 7 wzab
    invalid     => '1',
63 2 wzab
    d_payload => (others => '0')
64
    );
65
 
66
 
67
  function sort_cmp_lt (
68
    constant v1 : T_DATA_REC;
69
    constant v2 : T_DATA_REC)
70
    return boolean;
71
 
72
  function tdrec2stlv (
73
    constant drec : T_DATA_REC)
74
    return std_logic_vector;
75
 
76
  function stlv2tdrec (
77
    constant dstlv : std_logic_vector)
78
    return T_DATA_REC;
79
 
80
  procedure wrstlv (
81
    rline         : inout line;
82
    constant vect :       std_logic_vector);
83
 
84
  file reports : text open write_mode is "STD_OUTPUT";
85
 
86
end sorter_pkg;
87
 
88
package body sorter_pkg is
89
 
90
  function stlv2tdrec (
91
    constant dstlv : std_logic_vector)
92
    return T_DATA_REC is
93
    variable result : T_DATA_REC;
94
    variable j      : integer := 0;
95
  begin  -- stlv2drec
96
    j                := 0;
97
    result.d_key     := unsigned(dstlv(j-1+DATA_REC_SORT_KEY_WIDTH downto j));
98
    j                := j+DATA_REC_SORT_KEY_WIDTH;
99 7 wzab
    result.invalid     := dstlv(j);
100 2 wzab
    j                := j+1;
101
    result.d_payload := dstlv(j-1+DATA_REC_PAYLOAD_WIDTH downto j);
102
    j                := j+DATA_REC_PAYLOAD_WIDTH;
103
    return result;
104
  end stlv2tdrec;
105
 
106
  function tdrec2stlv (
107
    constant drec : T_DATA_REC)
108
    return std_logic_vector is
109
    variable result : std_logic_vector(DATA_REC_WIDTH-1 downto 0);
110
    variable j      : integer := 0;
111
  begin  -- tdrec2stlv
112
    j                                            := 0;
113
    result(j-1+DATA_REC_SORT_KEY_WIDTH downto j) := std_logic_vector(drec.d_key);
114
    j                                            := j+DATA_REC_SORT_KEY_WIDTH;
115 7 wzab
    result(j)                                    := drec.invalid;
116 2 wzab
    j                                            := j+1;
117
    result(j-1+DATA_REC_PAYLOAD_WIDTH downto j)  := std_logic_vector(drec.d_payload);
118
    j                                            := j+DATA_REC_PAYLOAD_WIDTH;
119
    return result;
120
  end tdrec2stlv;
121
 
122
 
123
  -- Function sort_cmp_lt returns TRUE when the first opperand is ``less'' than
124
  -- the second one
125
  function sort_cmp_lt (
126
    constant v1 : T_DATA_REC;
127
    constant v2 : T_DATA_REC)
128
    return boolean is
129
    variable rline : line;
130 7 wzab
    variable dcomp, key1, key2  : unsigned(DATA_REC_SORT_KEY_WIDTH downto 0) := (others => '0');
131 2 wzab
  begin  -- sort_cmp_lt
132
      -- We compare standard words
133
      -- We must consider the fact, that in longer sequences of data records
134
      -- the sort keys may wrap around
135
      -- therefore we perform subtraction modulo
136 7 wzab
    -- 2**DATA_REC_SORT_KEY_WIDTH and check the MSB
137
      key1 := v1.d_key & v1.invalid;
138
      key2 := v2.d_key & v2.invalid;
139
      dcomp := key1-key2;
140
      if dcomp(DATA_REC_SORT_KEY_WIDTH) = '1' then
141 2 wzab
      --if signed(v1.d_key - v2.d_key)<0 then -- old implementation
142
        return true;
143
      else
144
        return false;
145
      end if;
146
    return false;                       -- should never happen
147
  end sort_cmp_lt;
148
 
149
 
150
  procedure wrstlv (
151
    rline         : inout line;
152
    constant vect :       std_logic_vector) is
153
  begin  -- stlv2str
154
    for i in vect'left downto vect'right loop
155
      case vect(i) is
156
        when 'U'    => write(rline, string'("u"));
157
        when 'Z'    => write(rline, string'("z"));
158
        when 'X'    => write(rline, string'("x"));
159
        when 'L'    => write(rline, string'("L"));
160
        when 'H'    => write(rline, string'("H"));
161
        when '1'    => write(rline, string'("1"));
162
        when '0'    => write(rline, string'("0"));
163
        when others => write(rline, string'("?"));
164
      end case;
165
    end loop;  -- i
166
  end wrstlv;
167
 
168
end sorter_pkg;
169
 

powered by: WebSVN 2.1.0

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