diff core/src/SFIFO.bsv @ 1:91a1f76ddd62 pygar svn.2

[svn r2] Adding initial lab 5 source
author punk
date Tue, 13 Apr 2010 17:34:33 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/src/SFIFO.bsv	Tue Apr 13 17:34:33 2010 -0400
     1.3 @@ -0,0 +1,213 @@
     1.4 +
     1.5 +import FIFO::*;
     1.6 +import ConfigReg::*;
     1.7 +import RWire::*;
     1.8 +
     1.9 +import List::*;
    1.10 +import Monad::*;
    1.11 +
    1.12 +interface SFIFO#(type alpha_T, type search_T);
    1.13 +   method Action enq(alpha_T x);
    1.14 +   method Action deq();
    1.15 +   method alpha_T first();
    1.16 +   method Action clear();
    1.17 +   method Bool find(search_T x);   
    1.18 +   method Bool find2(search_T x);
    1.19 +
    1.20 +endinterface
    1.21 +
    1.22 +module mkSFIFO#(function Bool searchfunc(search_T s, alpha_T x)) (SFIFO#(alpha_T, search_T))
    1.23 +    provisos
    1.24 +      (Bits#(alpha_T,asz));
    1.25 +
    1.26 +    Reg#(alpha_T) f0 <- mkConfigRegU();
    1.27 +    Reg#(alpha_T) f1 <- mkConfigRegU();
    1.28 +
    1.29 +    Reg#(Bool)   vf0 <- mkConfigReg(False);
    1.30 +    Reg#(Bool)   vf1 <- mkConfigReg(False);
    1.31 +
    1.32 +    PulseWire edge1      <- mkPulseWire();
    1.33 +      
    1.34 +    method Action enq(alpha_T x) if (!(vf0 && vf1));
    1.35 +      if (edge1 || !vf0)//empty or we're dequeueing
    1.36 +	begin
    1.37 +          vf0 <= True; //True
    1.38 +	  vf1 <= False;
    1.39 +	   f0 <= x;
    1.40 +  	end
    1.41 +      else // !vf1 
    1.42 +        begin
    1.43 +          vf1 <= True;
    1.44 +           f1 <= x;
    1.45 + 	end
    1.46 +    endmethod
    1.47 +										
    1.48 +    method Action deq() if (vf0);
    1.49 +      edge1.send();
    1.50 +      vf0 <= vf1;
    1.51 +       f0 <= f1;
    1.52 +      vf1 <= False;
    1.53 +    endmethod  
    1.54 +
    1.55 +    method alpha_T first() if(vf0);
    1.56 +      return (f0);
    1.57 +    endmethod  
    1.58 +
    1.59 +    method Action clear();
    1.60 +      vf0 <= False;
    1.61 +      vf1 <= False;
    1.62 +    endmethod
    1.63 +
    1.64 +    method Bool find(search_T sv);
    1.65 +      Bool nvf0 = edge1 ? False: vf0;
    1.66 +      Bool nvf1 = vf1;
    1.67 +
    1.68 +      return (nvf0 && searchfunc(sv, f0) || 
    1.69 +              nvf1 && searchfunc(sv, f1));
    1.70 +    endmethod
    1.71 +
    1.72 +    method Bool find2(search_T sv);
    1.73 +      Bool nvf0 = edge1 ? False: vf0;
    1.74 +      Bool nvf1 = vf1;
    1.75 +      
    1.76 +      return (nvf0 && searchfunc(sv, f0) || 
    1.77 +              nvf1 && searchfunc(sv, f1));
    1.78 +    endmethod
    1.79 +
    1.80 +endmodule
    1.81 +
    1.82 +module mkSFIFO1#(function Bool searchfunc(search_T s, alpha_T x)) (SFIFO#(alpha_T, search_T))
    1.83 +    provisos
    1.84 +      (Bits#(alpha_T,asz), Eq#(alpha_T));
    1.85 +
    1.86 +    Reg#(alpha_T) f0 <- mkConfigRegU;
    1.87 +
    1.88 +    Reg#(Bool)   vf0 <- mkConfigReg(False);
    1.89 +
    1.90 +    PulseWire edge1      <- mkPulseWire();
    1.91 +      
    1.92 +    method Action enq(alpha_T x) if (!vf0);
    1.93 +      vf0 <= True; //True
    1.94 +      f0 <= x;
    1.95 +    endmethod
    1.96 +										
    1.97 +    method Action deq() if (vf0);
    1.98 +      edge1.send();
    1.99 +      vf0 <= False;
   1.100 +    endmethod  
   1.101 +
   1.102 +    method alpha_T first() if(vf0);
   1.103 +      return (f0);
   1.104 +    endmethod  
   1.105 +
   1.106 +    method Action clear();
   1.107 +      vf0 <= False;
   1.108 +    endmethod
   1.109 +
   1.110 +    method Bool find(search_T sv);
   1.111 +      Bool nvf0 = edge1 ? False: vf0;
   1.112 +
   1.113 +      return (nvf0 && searchfunc(sv, f0));
   1.114 +    endmethod
   1.115 +
   1.116 +    method Bool find2(search_T sv);
   1.117 +      Bool nvf0 = edge1 ? False: vf0;
   1.118 +      return (nvf0 && searchfunc(sv, f0));
   1.119 +    endmethod
   1.120 +
   1.121 +endmodule
   1.122 +
   1.123 +module mkSizedSFIFOInternal#(Integer n, 
   1.124 +			     function Bool searchfunc1(search_T s, alpha_T x), 
   1.125 +			     function Bool searchfunc2(search_T s, alpha_T x))  (SFIFO#(alpha_T, search_T))
   1.126 +				
   1.127 +   provisos ( Bits#(alpha_T,alpha_SZ) );
   1.128 +
   1.129 +  List#(Reg#(alpha_T)) registers <- replicateM(n, mkRegU);
   1.130 +  List#(Reg#(Bool))   valids <- replicateM(n, mkReg(False));
   1.131 +
   1.132 +  function Nat getNextFree (List#(Reg#(Bool)) vs);
   1.133 +
   1.134 +    Nat res = fromInteger(n - 1);
   1.135 +
   1.136 +    for (Integer x = n - 1; x > -1; x = x - 1)
   1.137 +      res = !vs[x]._read() ? fromInteger(x) : res;
   1.138 +
   1.139 +    return res;
   1.140 +  
   1.141 +  endfunction
   1.142 +
   1.143 +  function Bool notFull();
   1.144 +  
   1.145 +    Bool full = True;
   1.146 +
   1.147 +    for (Integer x = 0; x < n; x = x + 1)
   1.148 +      full = full && valids[x]._read();
   1.149 +
   1.150 +    return !full;
   1.151 +  
   1.152 +  endfunction
   1.153 +
   1.154 +  method Action enq( alpha_T item ) if ( notFull() );
   1.155 +    
   1.156 +    Nat k = getNextFree(valids);
   1.157 +    select(valids, k)._write(True);
   1.158 +    select(registers, k)._write(item);
   1.159 +    
   1.160 +  endmethod
   1.161 +
   1.162 +  method Action deq() if ( valids[0]._read() );
   1.163 +    
   1.164 +    for (Integer x = 0; x < (n-1); x = x + 1)
   1.165 +    begin
   1.166 +    
   1.167 +      (registers[x]) <= registers[x + 1]._read();
   1.168 +      (valids[x]) <= valids[x + 1]._read();
   1.169 +    
   1.170 +    end
   1.171 +    (valids[n-1]) <= False;
   1.172 +  endmethod  
   1.173 +
   1.174 +  method alpha_T first() if ( valids[0]._read() );
   1.175 +    return registers[0]._read();  
   1.176 +  endmethod
   1.177 +
   1.178 +  method Bool find(search_T sv);
   1.179 +    Bool res = False;
   1.180 +    
   1.181 +    for (Integer x = 0; x < n; x = x + 1)
   1.182 +       if ( valids[x]._read() && searchfunc1(sv, registers[x]._read()) )
   1.183 +          res = True;
   1.184 +    
   1.185 +    return res;
   1.186 +    
   1.187 +  endmethod
   1.188 +
   1.189 +  method Bool find2(search_T sv);
   1.190 +    Bool res = False;
   1.191 +    
   1.192 +     for (Integer x = 0; x < n; x = x + 1)
   1.193 +        if ( valids[x]._read() && searchfunc2(sv, registers[x]._read()) )
   1.194 +           res = True;
   1.195 +    
   1.196 +    return res;
   1.197 +    
   1.198 +  endmethod
   1.199 +
   1.200 +  method Action clear();
   1.201 +  
   1.202 +    for (Integer x = 0; x < n; x = x + 1)
   1.203 +      (valids[x]) <= False;
   1.204 +    
   1.205 +  endmethod
   1.206 +
   1.207 +endmodule
   1.208 +
   1.209 +module mkSizedSFIFO#(Integer n, function Bool searchfunc(search_T s, alpha_T x))  (SFIFO#(alpha_T, search_T))
   1.210 +    provisos
   1.211 +      (Bits#(alpha_T,asz));
   1.212 +   
   1.213 +   let foo <- mkSizedSFIFOInternal(n, searchfunc, searchfunc);
   1.214 +   return foo;
   1.215 +
   1.216 +endmodule