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

Subversion Repositories cryptosorter

[/] [cryptosorter/] [trunk/] [memocodeDesignContest2008/] [sort/] [SortTreeTestbench.bsv] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 kfleming
// import standard librarys
2
import Connectable::*;
3
import GetPut::*;
4
import FIFO::*;
5
import StmtFSM::*;
6
import Vector::*;
7
 
8
// import self-made library
9
import BRAMVLevelFIFO::*;
10
import VLevelFIFO::*;
11
import Sort::*;
12
import LFSR::*;
13
import Memocode08Types::*;
14
import SortTree16::*;
15
import SortTree64::*;
16
 
17
 
18
typedef 16 KMerges;
19
 
20
typedef enum{
21
  Uninitialized,
22
  Burst,
23
  Idle,
24
  FIFOStage,
25
  FIFOIdle,
26
  Done
27
} State deriving (Bits,Eq);
28
 
29
module mkSortTreeTestbench(Empty);
30
 
31
   Vector#(KMerges,FIFO#(Record)) resultFIFOs <- replicateM(mkSizedFIFO(valueof(KMerges)+1));
32
 
33
//  SortTree#(KMerges,1,Bit#(7),Bit#(1),Maybe#(Bit#(128))) sortTree <- mkBRAMSort16;
34
   let sortTree <- mkSortTree16;
35
 
36
   Reg#(Bit#(TLog#(KMerges))) slabCount <- mkReg(0);
37
 
38
   Reg#(Bit#(TMul#(2,TLog#(KMerges)))) finalCount <- mkReg(0);
39
 
40
   Reg#(State) stateMeta <- mkReg(Burst);
41
 
42
   Reg#(Bit#(128)) lastValue <- mkReg(0);
43
 
44
   Reg#(Bit#(64)) cycleCnt <- mkReg(0);
45
 
46
   Reg#(Bit#(TLog#(TAdd#(KMerges,1)))) recvCount <- mkReg(0);
47
 
48
   Reg#(Bit#(TLog#(TAdd#(KMerges,1)))) sendCount <- mkReg(0);
49
 
50
   Reg#(Bit#(8)) nextValue <- mkReg(0);
51
 
52
   rule incrCycle (True);
53
      cycleCnt <= cycleCnt + 1;
54
 
55
      $display("cycle %d",cycleCnt);
56
   endrule
57
 
58
   rule getTokInfo (True);
59
      let tokInfo = sortTree.inStream.getTokInfo();
60
 
61
      for (Integer i = 0; i < valueOf(KMerges); i = i + 1)
62
         $display("Instream Tok Info idx %d tok %d",i,tokInfo[i]);
63
 
64
   endrule
65
 
66
  for(Integer i = 0; i < valueof(KMerges); i = i + 1)
67
    begin
68
      Reg#(Bool) initialized <- mkReg(False);
69
      Reg#(Bit#(TLog#(KMerges))) count <- mkReg(0);
70
      Reg#(State) state <- mkReg(Burst);
71
 
72
      rule push ((state == Burst) && ((sortTree.inStream.getTokInfo())[i] > 1));
73
        $display("Sender %d is Bursting", i);
74
        sortTree.inStream.putDeqTok(fromInteger(i),2);
75
        sortTree.inStream.putRecord(fromInteger(i),
76
                                    tagged Valid (zeroExtend(nextValue)));
77
        nextValue <= nextValue + 101; // 101 is rel. prime to 256, and so will generate the U256 group
78
        state <= Idle;
79
      endrule
80
 
81
//      rule endpush ((state == Idle) && ((sortTree.inStream.getTokInfo())[i] > 0));
82
      rule endpush (state == Idle);
83
        $display("Sender %d is Idling", i);
84
        sortTree.inStream.putRecord(fromInteger(i), tagged Invalid);
85
        count <= count + 1;
86
        if(count + 1 == 0)
87
          begin
88
             state <= FIFOStage;
89
             sendCount <= sendCount + 1;
90
          end
91
        else
92
          begin
93
            state <= Burst;
94
          end
95
      endrule
96
 
97
      rule fifopush ((sendCount == 16) && (state == FIFOStage) && ((sortTree.inStream.getTokInfo())[i] > 0));
98
        $display("Sender %d is FIFOStage",i);
99
        sortTree.inStream.putDeqTok(fromInteger(i),1);
100
        resultFIFOs[i].deq;
101
        sortTree.inStream.putRecord(fromInteger(i), tagged Valid resultFIFOs[i].first);
102
        count <= count + 1;
103
        if(count + 1 == 0)
104
          begin
105
             state <= FIFOIdle;
106
          end
107
        else
108
          begin
109
            state <= FIFOStage;
110
          end
111
      endrule
112
 
113
      rule endfifo ((state == FIFOIdle)&& ((sortTree.inStream.getTokInfo())[i] > 0));
114
        $display("Sender %d is FIFOIdle",i);
115
        sortTree.inStream.putDeqTok(fromInteger(i),1);
116
        sortTree.inStream.putRecord(fromInteger(i), tagged Invalid);
117
        state <= Done;
118
      endrule
119
    end
120
 
121
 
122
  rule readOutResults(stateMeta == Burst);
123
    let outdata <- sortTree.getRecord();
124
    if(outdata matches tagged Valid .data)
125
      begin
126
         $display("Getting a slab result %d, recv count %d",data,recvCount);
127
         resultFIFOs[slabCount].enq(data);
128
         recvCount <= recvCount + 1;
129
         if(lastValue > data)
130
            begin
131
               $display("%m Error: %d > %d at %d", lastValue, data, finalCount);
132
               $finish();
133
            end
134
         lastValue <= data;
135
      end
136
    else
137
      begin
138
        $display("Getting a end of stream token %d",recvCount);
139
         slabCount <= slabCount + 1;
140
         lastValue <= 0;
141
         recvCount <= 0;
142
         if (recvCount != fromInteger(valueOf(KMerges)))
143
            begin
144
               $display("error merge recv token too early %d", recvCount);
145
               $finish();
146
            end
147
 
148
         if(slabCount + 1 == 0)
149
            begin
150
               stateMeta <= FIFOStage;
151
            end
152
      end
153
  endrule
154
 
155
 
156
  rule readOutResultsFinal(stateMeta == FIFOStage);
157
    $display("%m Getting a result");
158
    let outdata <- sortTree.getRecord();
159
    if(outdata matches tagged Valid .data)
160
      begin
161
        finalCount <= finalCount + 1;
162
        if(zeroExtend(finalCount) != data)
163
          begin
164
             $display("%m Error: %d != %d at %d", finalCount, data, finalCount);
165
             $finish();
166
          end
167
        if(lastValue > data)
168
          begin
169
             $display("%m Error: %d > %d at %d", lastValue, data, finalCount);
170
             $finish();
171
          end
172
        lastValue <= data;
173
      end
174
    else
175
      begin
176
        if(finalCount != 0)
177
          begin
178
             $display("%m Error: final Value: %d", finalCount);
179
             $finish();
180
          end
181
        else
182
          begin
183
            let timeVal <- $time();
184
            $display("%m PASSES at %d cycle %d", timeVal,cycleCnt);
185
            $finish();
186
          end
187
      end
188
  endrule
189
 
190
 
191
endmodule

powered by: WebSVN 2.1.0

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