Demapper.bsv
上传用户:aoptech
上传日期:2014-09-22
资源大小:784k
文件大小:9k
源码类别:

3G开发

开发平台:

Others

  1. //----------------------------------------------------------------------//
  2. // The MIT License 
  3. // 
  4. // Copyright (c) 2007 Alfred Man Cheuk Ng, mcn02@mit.edu 
  5. // 
  6. // Permission is hereby granted, free of charge, to any person 
  7. // obtaining a copy of this software and associated documentation 
  8. // files (the "Software"), to deal in the Software without 
  9. // restriction, including without limitation the rights to use,
  10. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the
  12. // Software is furnished to do so, subject to the following conditions:
  13. // 
  14. // The above copyright notice and this permission notice shall be
  15. // included in all copies or substantial portions of the Software.
  16. // 
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. // OTHER DEALINGS IN THE SOFTWARE.
  25. //----------------------------------------------------------------------//
  26. import Complex::*;
  27. import Controls::*;
  28. import DataTypes::*;
  29. import FIFO::*;
  30. import FixedPoint::*;
  31. import FPComplex::*;
  32. import Interfaces::*;
  33. import Vector::*;
  34. import GetPut::*;
  35. import VectorLibrary::*;
  36. // assume no overflow
  37. function FixedPoint#(ai,af) demapMult(Integer m, FixedPoint#(ai,af) fp)
  38.    provisos (Arith#(FixedPoint#(ai,af)));   
  39.    FixedPoint#(ai,af) res = 0;
  40.    for(Integer i = 0; i < m; i = i + 1)
  41.       res = res + fp;
  42.    return res;
  43. endfunction
  44. // aux functions
  45. function ViterbiMetric decodeRange(FixedPoint#(ai,af) in, FixedPoint#(ai,af) start, FixedPoint#(ai,af) incr, Bool startZero)
  46.   provisos (Add#(1,xxA,ai), Literal#(FixedPoint#(ai,af)),
  47.     Arith#(FixedPoint#(ai,af)));
  48.       let result = (in < start + incr) ?
  49.    (startZero ? 0 : 7) :
  50.    (in < start + demapMult(2,incr)) ?
  51.    (startZero ? 1 : 6) :
  52.    (in < start + demapMult(3,incr)) ?
  53.    (startZero ? 2 : 5) :
  54.    (in < start + demapMult(4,incr)) ?
  55.    (startZero ? 3 : 4) :
  56.    (in < start + demapMult(5,incr)) ?
  57.    (startZero ? 4 : 3) :
  58.    (in < start + demapMult(6,incr)) ?
  59.    (startZero ? 5 : 2) :
  60.    (in < start + demapMult(7,incr)) ?
  61.    (startZero ? 6 : 1) :
  62.    (startZero ? 7 : 0);
  63.       return result;
  64. endfunction // ViterbiMetric
  65. function ViterbiMetric decodeBPSK(Bool negateOutput,
  66.   FPComplex#(ai,af) in)
  67.   provisos (Add#(1,xxA,ai), Literal#(FixedPoint#(ai,af)),
  68.     Arith#(FixedPoint#(ai,af)));
  69.       return decodeRange(in.rel, -1, fromRational(1,4), !negateOutput);
  70. endfunction // ConfLvl
  71. function Vector#(2, ViterbiMetric) decodeQPSK(Bool negateOutput,
  72.       FPComplex#(ai,af) in)
  73.   provisos (Add#(1,xxA,ai), Literal#(FixedPoint#(ai,af)),
  74.     Arith#(FixedPoint#(ai,af)));
  75.       function ViterbiMetric decodeQPSKAux(FixedPoint#(ai,af) fp);
  76.          return decodeRange(fp, fromRational(-707106781,1000000000), fromRational(17676695,1000000000), !negateOutput);
  77.       endfunction
  78.       Vector#(2, ViterbiMetric) result = newVector;
  79.       result[0] = decodeQPSKAux(in.rel);
  80.       result[1] = decodeQPSKAux(in.img);
  81.       return result;
  82. endfunction // ConfLvl      
  83. function Vector#(4, ViterbiMetric) decodeQAM_16(Bool negateOutput,
  84. FPComplex#(ai,af) in)
  85.   provisos (Add#(1,xxA,ai), Literal#(FixedPoint#(ai,af)),
  86.     Arith#(FixedPoint#(ai,af)));
  87.       // aux funcs
  88.       function ViterbiMetric decodeQAM_16_Even(FixedPoint#(ai,af) x);
  89.  return decodeRange(x, fromRational(-316227766,1000000000), fromRational(79056942,1000000000), !negateOutput);
  90.       endfunction // ConfLvl      
  91.       
  92.       function ViterbiMetric decodeQAM_16_Odd(FixedPoint#(ai,af) x);
  93.          let result = (x < 0) ?
  94.       decodeRange(x, fromRational(-948683298,1000000000), fromRational(79056942,1000000000),!negateOutput) :
  95.       decodeRange(x, fromRational(316227766,1000000000), fromRational(79056942,1000000000),negateOutput);      
  96.  return result;
  97.       endfunction // ConfLvl      
  98.       Vector#(4, ViterbiMetric) result = newVector;
  99.       result[0] = decodeQAM_16_Even(in.rel);
  100.       result[1] = decodeQAM_16_Odd(in.rel);
  101.       result[2] = decodeQAM_16_Even(in.img);
  102.       result[3] = decodeQAM_16_Odd(in.img);
  103.       return result;
  104. endfunction
  105. function Vector#(6, ViterbiMetric) decodeQAM_64(Bool negateOutput,
  106. FPComplex#(ai,af) in)
  107.   provisos (Add#(1,xxA,ai), Literal#(FixedPoint#(ai,af)),
  108.     Arith#(FixedPoint#(ai,af)));
  109.       // aux funcs
  110.       function ViterbiMetric decodeQAM_64_0(FixedPoint#(ai,af) x);
  111.  return decodeRange(x, fromRational(-154303350,1000000000), fromRational(38575837,1000000000), !negateOutput);
  112.       endfunction // ConfLvl      
  113.       
  114.       function ViterbiMetric decodeQAM_64_1(FixedPoint#(ai,af) x);
  115.          let result = (x < 0) ?
  116.       decodeRange(x, fromRational(-771516750,1000000000), fromRational(38575837,1000000000),!negateOutput) :
  117.       decodeRange(x, fromRational(462910050,1000000000), fromRational(38575837,1000000000),negateOutput);      
  118.  return result;
  119.       endfunction
  120.       
  121.       function ViterbiMetric decodeQAM_64_2(FixedPoint#(ai,af) x);
  122.  let result = (x < fromRational(-617213400,1000000000)) ?
  123.       decodeRange(x, fromRational(-1080123450,1000000000), fromRational(38575837,1000000000),!negateOutput) :
  124.       (x < 0) ? 
  125.       decodeRange(x, fromRational(-462910050,1000000000), fromRational(38575837,1000000000),negateOutput) :
  126.       (x < fromRational(617213400,1000000000)) ?
  127.       decodeRange(x, fromRational(154303350,1000000000), fromRational(38575837,1000000000),!negateOutput) :
  128.       decodeRange(x, fromRational(771516750,1000000000), fromRational(38575837,1000000000),negateOutput);
  129.  return result;
  130.       endfunction // ConfLvl      
  131.       Vector#(6, ViterbiMetric) result = newVector;
  132.       result[0] = decodeQAM_64_0(in.rel);
  133.       result[1] = decodeQAM_64_1(in.rel);
  134.       result[2] = decodeQAM_64_2(in.rel);
  135.       result[3] = decodeQAM_64_0(in.img);
  136.       result[4] = decodeQAM_64_1(in.img);
  137.       result[5] = decodeQAM_64_2(in.img);
  138.       return result;
  139. endfunction 
  140. // mkMapper definition, 
  141. // i_n must equal no of data carriers, dividable by o_n
  142. // o_n must be multiple of 12
  143. // negateOutput: False: mapper maps 0 to -1 and 1 to 1
  144. //               TrueL mapper maps 1 to -1 and 0 to 1
  145. module mkDemapper#(function Modulation mapCtrl(ctrl_t inCtrl),
  146.    Bool negateOutput) 
  147.    (Demapper#(ctrl_t,i_n,o_n,i_prec,f_prec,ViterbiMetric))
  148.    provisos(Bits#(ctrl_t,ctrl_sz),
  149.     Add#(1,xxA,i_prec), 
  150.     Literal#(FixedPoint#(i_prec,f_prec)), 
  151.     Arith#(FixedPoint#(i_prec,f_prec)),
  152.     Mul#(2,qpsk_n,o_n), 
  153.     Mul#(4,qam16_n,o_n), 
  154.     Mul#(6,qam64_n,o_n),
  155.     Mul#(xxB,o_n,i_n),
  156.     Mul#(xxC,qpsk_n,i_n),
  157.     Mul#(xxD,qam16_n,i_n),
  158.     Mul#(xxE,qam64_n,i_n),
  159.     Log#(i_n,idx_sz));
  160.    
  161.    // constants
  162.    Bit#(idx_sz) bpskSz = fromInteger(valueOf(xxB)-1);
  163.    Bit#(idx_sz) qpskSz = fromInteger(valueOf(xxC)-1);
  164.    Bit#(idx_sz) qam16Sz = fromInteger(valueOf(xxD)-1);
  165.    Bit#(idx_sz) qam64Sz = fromInteger(valueOf(xxE)-1);
  166.       
  167.    // state elements
  168.    Reg#(Bit#(idx_sz)) counter <- mkReg(0);
  169.    FIFO#(DemapperMesg#(ctrl_t,i_n,i_prec,f_prec)) inQ <- mkLFIFO;
  170.    FIFO#(DeinterleaverMesg#(ctrl_t,o_n,ViterbiMetric)) outQ <- mkSizedFIFO(2);
  171.    
  172.    // rules
  173.    rule demap(True);
  174.       let inData = inQ.first.data; 
  175.       let ctrl = inQ.first.control;
  176.       let format = mapCtrl(ctrl);
  177.       let checkSz = case(format)
  178.        BPSK: bpskSz;
  179.        QPSK: qpskSz;
  180.        QAM_16: qam16Sz;
  181.        QAM_64: qam64Sz;
  182.     endcase;
  183.       Vector#(o_n, ViterbiMetric) outData = newVector;
  184.       Vector#(xxB,Vector#(o_n,FPComplex#(i_prec,f_prec)))     bpskVec  = unpackVec(inData); 
  185.       Vector#(xxC,Vector#(qpsk_n,FPComplex#(i_prec,f_prec)))  qpskVec  = unpackVec(inData); 
  186.       Vector#(xxD,Vector#(qam16_n,FPComplex#(i_prec,f_prec))) qam16Vec = unpackVec(inData); 
  187.       Vector#(xxE,Vector#(qam64_n,FPComplex#(i_prec,f_prec))) qam64Vec = unpackVec(inData); 
  188.       outData = case (format)
  189.    BPSK:   map(decodeBPSK(negateOutput),bpskVec[counter]);
  190.    QPSK:   packVec(map(decodeQPSK(negateOutput),qpskVec[counter]));
  191.    QAM_16: packVec(map(decodeQAM_16(negateOutput),qam16Vec[counter]));
  192.    QAM_64: packVec(map(decodeQAM_64(negateOutput),qam64Vec[counter]));
  193. endcase;
  194.       outQ.enq(Mesg{control: ctrl, data: outData});
  195.       if (counter == checkSz)
  196.  begin
  197.     inQ.deq;
  198.     counter <= 0;
  199.  end
  200.       else
  201.  counter <= counter + 1;
  202.    endrule
  203.    // methods
  204.    interface  in = fifoToPut(inQ);
  205.    interface out = fifoToGet(outQ);
  206. endmodule // mkDemapper