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

Subversion Repositories numbert_sort_device

[/] [numbert_sort_device/] [trunk/] [main/] [stack_sorter.sv] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 leshabiruk
 
2
////////////////////////////////////////////////////////////
3
//
4
// number sorting device, sequential, O(N)
5
//
6
//      autor:   Alexey Birukov, leshabirukov@opencores.org
7
//      license: LGPL
8
//
9
//      notes:
10
//      reset signal is not implemented, to make reset wait R_SZ clocks
11
//      while hold==0 and is_input==0 to empty the buffer
12
//      or implement reset signal by yourself
13
//
14
//      to make sorting in increasing order, use module call like this:
15
//  Sorting_Stack #(HBIT,R_SZ) cstack ( clk, hold, is_input, -1-data_in, _data_out      );
16
//  wire [HBIT:0] data_out= -1-_data_out;
17
//
18
////////////////////////////////////////////////////////////
19
 
20
// linear buffer implementation
21
//      sequential, stable, can be partly readed, decreasing order
22
//      reset signal is not implemented, to make reset wait R_SZ clocks
23
//      while hold==0 and is_input==0
24
 
25
//      see sort_stack_algorithm.png to catch the idea
26
 
27
 
28
 
29
module Sorting_Stack ( clk, hold, is_input, data_in, data_out   );
30
 
31
parameter HBIT= 15;                             //      size of number in bits
32
parameter R_SZ= 256;                            //      capacity, max sequence size
33
 
34
parameter _R_SZ= (R_SZ+1)/2;    //      not to modify
35
 
36
input clk;
37
input hold;                                                     // 1 - to freeze state
38
input is_input;                                 //      1 - while loading
39
 
40
input [HBIT:0] data_in;                 //      load one number at a clock
41
output [HBIT:0] data_out;               //      while is_input==0, max value popping out here
42
 
43
wire [HBIT:0] in_prev[_R_SZ];
44
wire [HBIT:0] in_next[_R_SZ];
45
wire [HBIT:0] out[_R_SZ];
46
 
47
// storage
48
Cell_Compare #(HBIT) ribbon[_R_SZ] ( clk, hold, is_input,       in_prev, in_next, out );
49
 
50
// wiring
51
generate
52
  genvar i,j;
53
  for (i=0; i<_R_SZ-1; i=i+1)
54
  begin : block_name01
55
                assign in_prev[i+1]= out[i];
56
                assign in_next[i]= out[i+1];
57
  end
58
  assign in_prev[0]= data_in;
59
  assign data_out= out[0];
60
  assign in_next[_R_SZ-1]= 0;
61
endgenerate
62
 
63
endmodule
64
 
65
 
66
 
67
 
68
 
69
 
70
 
71
 
72
module Cell_Compare ( clk, hold, is_input, in_prev, in_next, out );
73
 
74
parameter HBIT= 15;
75
 
76
 
77
input clk;
78
input hold;
79
 
80
input is_input;
81
 
82
input [HBIT:0] in_prev;
83
input [HBIT:0] in_next;
84
 
85
output [HBIT:0] out= is_input ? lower : higher;
86
 
87
bit [HBIT:0] higher;
88
bit [HBIT:0] lower;
89
 
90
wire [HBIT:0] cand_h= is_input ? higher : lower;
91
wire [HBIT:0] cand_l= is_input ? in_prev : in_next;
92
 
93
always@(posedge clk )
94
if (~hold)
95
begin
96
        higher <= ( cand_h >= cand_l ) ? cand_h : cand_l;
97
        lower  <= ( cand_h >= cand_l ) ? cand_l : cand_h;
98
end
99
endmodule
100
 
101
 
102
 
103
 
104
 
105
 

powered by: WebSVN 2.1.0

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