OpenCores
URL https://opencores.org/ocsvn/bluespec-reedsolomon/bluespec-reedsolomon/trunk

Subversion Repositories bluespec-reedsolomon

[/] [bluespec-reedsolomon/] [trunk/] [bsv-reedsolomon/] [MFIFO.bsv] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 abhiag
//----------------------------------------------------------------------//
2
// The MIT License
3
//
4
// Copyright (c) 2008 Abhinav Agarwal, Alfred Man Cheuk Ng
5
// Contact: abhiag@gmail.com
6
//
7
// Permission is hereby granted, free of charge, to any person
8
// obtaining a copy of this software and associated documentation
9
// files (the "Software"), to deal in the Software without
10
// restriction, including without limitation the rights to use,
11
// copy, modify, merge, publish, distribute, sublicense, and/or sell
12
// copies of the Software, and to permit persons to whom the
13
// Software is furnished to do so, subject to the following conditions:
14
//
15
// The above copyright notice and this permission notice shall be
16
// included in all copies or substantial portions of the Software.
17
//
18
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
// OTHER DEALINGS IN THE SOFTWARE.
26
//----------------------------------------------------------------------//
27
 
28
import FIFOF::*;
29
import RWire::*;
30
 
31
// -------------------------------------------------------------------
32
// MFIFO Interface: a fifo allow to the value of the first element to be modified
33
// -------------------------------------------------------------------
34
 
35
interface MFIFO#(numeric type sz, type a);
36
   method    Action  enq(a in);
37
   method    Action  deq();
38
   method    Action  clear();
39
   interface Reg#(a) first;
40
endinterface
41
 
42
interface MFIFOF#(numeric type sz, type a);
43
   method    Action  enq(a in);
44
   method    Action  deq();
45
   method    Action  clear();
46
   method    Bool    notEmpty();
47
   method    Bool    notFull();
48
   interface Reg#(a) first;
49
endinterface
50
 
51
// ---------------------------------------------------------
52
// MFIFO module
53
// ---------------------------------------------------------
54
// a mfifof of size 1
55
module mkMFIFOF1 (MFIFOF#(1,a))
56
   provisos (Bits#(a,a_sz));
57
 
58
   Reg#(Maybe#(a)) buffer <- mkReg(tagged Invalid);
59
   RWire#(a)       upW    <- mkRWire();
60
   RWire#(Bit#(0)) deqW   <- mkRWire();
61
 
62
   rule writeBuffer(isValid(upW.wget())
63
                    || isValid(deqW.wget()));
64
      if (isValid(deqW.wget()))
65
         buffer <= tagged Invalid;
66
      else
67
         buffer <= tagged Valid fromMaybe(?,upW.wget());
68
   endrule
69
 
70
   method Action enq(a in) if (!isValid(buffer));
71
      buffer <= tagged Valid in;
72
   endmethod
73
 
74
   method Action deq() if (isValid(buffer));
75
      deqW.wset(?);
76
   endmethod
77
 
78
   method Action clear();
79
      buffer <= tagged Invalid;
80
   endmethod
81
 
82
   method Bool notEmpty();
83
      return isValid(buffer);
84
   endmethod
85
 
86
   method Bool notFull();
87
      return !isValid(buffer);
88
   endmethod
89
 
90
   interface Reg first;
91
      method Action _write(a in) if (isValid(buffer));
92
         upW.wset(in);
93
      endmethod
94
 
95
      method a _read() if (buffer matches tagged Valid .val);
96
         return val;
97
      endmethod
98
   endinterface
99
 
100
endmodule
101
 
102
// a mfifof of size > 1
103
module mkMFIFOF (MFIFOF#(sz,a))
104
   provisos (Add#(2,xxA,sz), // sz >= 2
105
             Bits#(a,a_sz));
106
 
107
   Reg#(Maybe#(a)) buffer <- mkReg(tagged Invalid);
108
   FIFOF#(a)       fifo   <- mkSizedFIFOF(valueOf(sz)-1);
109
   RWire#(a)       upW    <- mkRWire();
110
   RWire#(Bit#(0)) deqW   <- mkRWire();
111
 
112
   let isNotEmpty = isValid(buffer) || fifo.notEmpty;
113
 
114
   rule writeBuffer(True);
115
      if (isValid(deqW.wget()))
116
         buffer <= tagged Invalid;
117
      else
118
          if (isValid(upW.wget()))
119
             buffer <= tagged Valid fromMaybe(?,upW.wget());
120
          else
121
             if (!isValid(buffer))
122
                buffer <= tagged Valid fifo.first();
123
      if (!isValid(buffer))
124
         fifo.deq();
125
   endrule
126
 
127
   method Action enq(a in);
128
      fifo.enq(in);
129
   endmethod
130
 
131
   method Action deq() if (isNotEmpty);
132
      deqW.wset(?);
133
   endmethod
134
 
135
   method Action clear();
136
      fifo.clear();
137
      buffer <= tagged Invalid;
138
   endmethod
139
 
140
   method Bool notEmpty();
141
      return isNotEmpty;
142
   endmethod
143
 
144
   method Bool notFull();
145
      return fifo.notFull();
146
   endmethod
147
 
148
   interface Reg first;
149
      method Action _write(a in) if (isNotEmpty);
150
         upW.wset(in);
151
      endmethod
152
 
153
      method a _read() if (isNotEmpty);
154
         return isValid(buffer) ? fromMaybe(?,buffer) : fifo.first();
155
      endmethod
156
   endinterface
157
 
158
endmodule
159
 
160
// a mfifo of size 1
161
module mkMFIFO1 (MFIFO#(1,a))
162
   provisos (Bits#(a,a_sz));
163
 
164
   MFIFOF#(1,a) fifo <- mkMFIFOF1();
165
 
166
   method    Action enq(a in) = fifo.enq(in);
167
   method    Action deq()     = fifo.deq();
168
   method    Action clear()   = fifo.clear();
169
   interface Reg    first     = fifo.first;
170
endmodule
171
 
172
// a mfifo of size > 1
173
module mkMFIFO (MFIFO#(sz,a))
174
   provisos (Add#(2,xxA,sz), // sz >= 2
175
             Bits#(a,a_sz));
176
 
177
   MFIFOF#(sz,a) fifo <- mkMFIFOF();
178
 
179
   method    Action enq(a in) = fifo.enq(in);
180
   method    Action deq()     = fifo.deq();
181
   method    Action clear()   = fifo.clear();
182
   interface Reg    first     = fifo.first;
183
endmodule
184
 
185
// ---------------------------------------------------------
186
// MFIFO Test module
187
// ---------------------------------------------------------
188
//(* synthesize *)
189
(* execution_order = "updOrDeqFIFO, enqFIFO, incrCounter" *)
190
module mkMFIFOTest(Empty);
191
 
192
   Reg#(Bit#(16))      counter <- mkReg(0);
193
   MFIFO#(10,Bit#(16)) fifo    <- mkMFIFO();
194
   Reg#(Bit#(16))      fstElm   = fifo.first;
195
 
196
   rule enqFIFO(True);
197
      $display("enq fifo: %d",counter);
198
      fifo.enq(counter);
199
   endrule
200
 
201
   rule updOrDeqFIFO(True);
202
      if ((counter & 16'h0007) == 16'h0007)
203
         begin
204
            $display("deq fifo: %d",fstElm);
205
            fifo.deq();
206
         end
207
      else
208
         begin
209
            $display("update fifo: %d = %d * 3",fstElm * 3,fstElm);
210
            fstElm <= fstElm * 3;
211
         end
212
   endrule
213
 
214
   rule incrCounter(True);
215
      $display("cycle: %d",counter);
216
      $display("----------------------------------------------------------");
217
      if (counter == 1000)
218
         $finish();
219
      counter <= counter + 1;
220
   endrule
221
 
222
endmodule

powered by: WebSVN 2.1.0

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