<JIRA ID: ODUHIGH-332 : Change in TBS calculation part as per TS 38.214 section 5...
[o-du/l2.git] / src / 5gnrsch / sch_utils.c
index 344e84e..5e014f9 100644 (file)
 #include "sch_utils.h"
 #include "math.h"
 
-/* spec-38.213 Table 13-1 */
+#ifdef NR_TDD
+/* spec-38.213 Table 13-4 for SCS=30KHz */
+/* Note: Picking Table 13-4 and not 13-6 since band supported is n78 and
+ * corresponding minimum B/W is 10 MHz */
+int8_t coresetIdxTable[MAX_CORESET_INDEX][4] = {
+{   1,   24,   2,   0}, /* index 0  */
+{   1,   24,   2,   1}, /* index 1  */
+{   1,   24,   2,   2}, /* index 2  */
+{   1,   24,   2,   3}, /* index 3  */
+{   1,   24,   2,   4}, /* index 4  */
+{   1,   24,   3,   0}, /* index 5  */
+{   1,   24,   3,   1}, /* index 6  */
+{   1,   24,   3,   2}, /* index 7  */
+{   1,   24,   3,   3}, /* index 8  */
+{   1,   24,   3,   4}, /* index 9  */
+{   1,   48,   1,  12}, /* index 10 */
+{   1,   48,   1,  14}, /* index 11 */
+{   1,   48,   1,  16}, /* index 12 */
+{   1,   48,   2,  12}, /* index 13 */
+{   1,   48,   2,  14}, /* index 14 */
+{   1,   48,   2,  16}, /* index 15 */
+};
+#else
+/* spec-38.213 Table 13-1 for SCS=15KHz */
 int8_t coresetIdxTable[MAX_CORESET_INDEX][4] = {
 {   1,   24,   2,   0}, /* index 0  */
 {   1,   24,   2,   2}, /* index 1  */
@@ -63,6 +86,7 @@ int8_t coresetIdxTable[MAX_CORESET_INDEX][4] = {
 {   1,   96,   3,  38}, /* index 14 */
 {   0,    0,   0,   0}, /* index 15 */
 };
+#endif
 
 /* spec-38.213 Table 13-11 */
 /* m value is scaled to 2, when using it in formula, divide by 2 */
@@ -458,63 +482,6 @@ uint8_t pucchResourceSet[MAX_PUCCH_RES_SET_IDX][4] = {
 { 1,   0, 14,  0 }, /* index 15 */
 };
 
-/**
- * @brief frequency domain allocation function. 
- *
- * @details
- *
- *     Function: schAllocFreqDomRscType0
- *     
- *     This function does allocation in frequency domain resource.
- *     This is a bitmap defining  non-overlapping groups of 6 PRBs in ascending order.
- *     
- *  @param[in]  startPrb - start PRB from where the freq alloc starts.  
- *  @param[in]  prbSize - number of PRBs to be allocted.
- *  @param[in]  freqDomain - 6 bytes of info, each bit represents a group of 6 PRB.
- *  @return   void
- **/
-void schAllocFreqDomRscType0(uint16_t startPrb, uint16_t prbSize, uint8_t *freqDomain)
-{
-   uint8_t remBits = prbSize; /* each bit represents 6 PRBs */
-   uint8_t firstByte = 1;
-   uint8_t numBits,startBit,byteCount = 5;
-
-   while(remBits)
-   {
-      /* when the startPrb is not in this byteCount */
-      if(startPrb/8)
-      {
-         startPrb -= 8;
-         byteCount--;
-         continue;
-      }
-
-      /* max bytecount is 6 nearly equal to 45 bits*/
-      if(byteCount >= 6)
-          break;
-
-      /* when we are filling the second byte, then the start should be equal to 0 */
-      if(firstByte)
-         startBit = startPrb;
-      else
-         startBit = 0;
-
-      /* calculate the number of bits to be set in this byte */
-      if((remBits+startPrb) <= 8)
-         numBits = remBits;
-      else
-         numBits = 8 - startBit;
-
-      /* bit operation to set the bits */
-               SET_BITS_MSB((startBit % 8),numBits,freqDomain[byteCount])
-      firstByte = 0;
-
-      /* the ramaining bits should be subtracted with the numBits set in this byte */
-      remBits -= numBits;
-      byteCount--;
-   }
-}
-
 /**
  * @brief frequency domain allocation function. 
  *
@@ -524,21 +491,26 @@ void schAllocFreqDomRscType0(uint16_t startPrb, uint16_t prbSize, uint8_t *freqD
  *     
  *     This function finds the TBSize from table Table 5.1.3.2-1 spec 38.214
  *     
- *  @param[in]  payLoadSize - size of payload
- *  @return     TBsize from the Table
+ *  @param[in]  payLoadSize - size of payload in bytes
+ *  @return     TBsize from the Table in bytes
  **/
-uint16_t schCalcTbSize(uint16_t payLoadSize)
+uint16_t schCalcTbSize(uint32_t payLoadSize)
 {
    uint8_t tbsIndex = 0;
-       payLoadSize = payLoadSize*8;
+   uint16_t maxTbSize;
+
+   maxTbSize = tbSizeTable[TOTAL_TBSIZE_VALUES -1]/8;
+   if(payLoadSize >= maxTbSize)
+      return maxTbSize;
 
-       while(payLoadSize > tbSizeTable[tbsIndex])
-       {
-          tbsIndex++;
-       }
+   payLoadSize = payLoadSize*8;
+   while(payLoadSize > tbSizeTable[tbsIndex])
+   {
+      tbsIndex++;
+   }
 
-       /* return the TBsize in bytes */
-       return (tbSizeTable[tbsIndex]/8);
+   /* return the TBsize in bytes */
+   return (tbSizeTable[tbsIndex]/8);
 }
 
 /**
@@ -550,7 +522,7 @@ uint16_t schCalcTbSize(uint16_t payLoadSize)
  *     
  *     This function calculates the number of PRbs 
  *     
- *  @param[in]  tbSize
+ *  @param[in]  tbSize in bytes
  *  @param[in]  mcs
  *  @param[in]  number of symbols
  *  @return   number PRBs
@@ -559,27 +531,220 @@ uint16_t schCalcNumPrb(uint16_t tbSize, uint16_t mcs, uint8_t numSymbols)
 {
    uint16_t numPrb = 0;
    uint16_t nre = 0;
-       uint16_t nreDash = 0;
-       uint8_t  qm     = mcsTable[mcs][1];
-       uint16_t rValue = mcsTable[mcs][2];
-       uint8_t  numLayer = 1;       /* v value */
+   uint16_t nreDash = 0;
+   uint8_t  qm     = mcsTable[mcs][1];
+   uint16_t rValue = mcsTable[mcs][2];
+   uint8_t  numLayer = 1;       /* v value */
+
+   tbSize = tbSize * 8; //Calculate tbSize in bits
 
-   /* formula used for calculation of rbSize, 38.213 section 5.1.3.2 *
-    * Ninfo = Nre . R . Qm . v                                       *
+   /* formula used for calculation of rbSize, 38.214 section 5.1.3.2 *
+    * Ninfo = S . Nre . R . Qm . v                                       *
     * Nre' = Nsc . NsymPdsch - NdmrsSymb - Noh                       *
     * Nre = min(156,Nre') . nPrb                                     */
 
-       nre = ceil( (float)tbSize * 1024 / (qm * rValue * numLayer));
+   nre = ceil( (float)tbSize * 1024 / (qm * rValue * numLayer));
 
-       nreDash = ceil( (12 * numSymbols) - NUM_DMRS_SYMBOLS - 0);
+   nreDash = ceil( (12 * numSymbols) - NUM_DMRS_SYMBOLS - 0);
 
-       if (nreDash > 156)
-          nre = 156;
+   if (nreDash > 156)
+      nre = 156;
 
    numPrb = ceil((float)nre / nreDash);   
-       return numPrb;
+   return numPrb;
+}
+
+/**
+* @brief calculation of transport block size.
+*
+* @details
+*
+*     Function: schCalcTbSizeFromNPrb
+*
+*     This function calculates the transport block size
+*
+*  @param[in]  nPrb is num PRB
+*  @param[in]  mcs
+*  @param[in]  number of symbols
+*  @return   tbSize
+**/
+uint16_t schCalcTbSizeFromNPrb(uint16_t numPrb, uint16_t mcs, uint8_t numSymbols)
+{   
+   uint8_t  qm     = mcsTable[mcs][1];
+   uint16_t rValue = mcsTable[mcs][2];
+   uint16_t tbsIndex = 0;
+   uint32_t tbSize = 0;
+   uint32_t nre = 0;
+   uint32_t nreDash = 0;
+   uint32_t nInfo = 0;
+   uint32_t n = 0;
+   uint32_t nInfoDash = 0;
+   uint32_t c = 0;
+   const uint8_t  numLayer = 1;
+   const uint16_t numRbSc = 12;
+   const uint16_t numDmrsRes = 12;
+   const uint16_t sf = 1;
+//   uint16_t numPrbOvrHead = 0;
+   
+  /* formula used for calculation of rbSize, 38.214 section 5.1.3.2  *
+   * Ninfo = Nre . R . Qm . v   where [ NInfo is tbSize]             *
+   * Nre' = Nsc . NsymPdsch - NdmrsSymb - Noh                        *
+   * Nre = min(156,Nre') . nPrb                                      */
+
+   nreDash = MIN(156, ceil( (numRbSc * numSymbols) - numDmrsRes - 0));
+   nre = nreDash * numPrb;
+   nInfo = ceil(nre * qm * numLayer * rValue/(1024.0 * sf));
+
+   if(nInfo <= 3824)
+   {
+      n = MAX(3, (uint32_t)cmLog2(nInfo) - 6);
+      nInfoDash = MAX(24, (1<<n)*(nInfo/(1<<n)));
+      while(nInfoDash > tbSizeTable[tbsIndex])
+      {
+         tbsIndex++;
+      }
+      tbSize = tbSizeTable[tbsIndex];
+   }
+   else
+   {
+      n = (uint32_t)cmLog2(nInfo - 24) - 5;
+      nInfoDash = MAX(3840, (1<<n)*ceil((nInfo - 24)/(1<<n)));
+
+      if(rValue<256)
+      {
+         c = ceil((nInfoDash + 24)/3816);
+         tbSize = 8 * c * ceil((nInfoDash + 24)/(8 * c)) - 24;
+      }
+      else
+      {
+         if(nInfoDash > 8424)
+         {
+            c = ceil((nInfoDash + 24)/8424);
+            tbSize = 8 * c * ceil((nInfoDash + 24)/(8 * c)) - 24;
+         }
+         else
+         {
+            tbSize = 8 * ceil((nInfoDash + 24)/(8)) - 24;
+         }
+      }
+   }
+   return tbSize;
+}
+/**
+ * @brief fetching ueCb from cellCb
+ *
+ * @details
+ *
+ *     Function: schGetUeCb
+ *
+ *     This function fetched UeCb based on crnti from cellCb
+ *
+ *  @param[in]  cellCb
+ *  @param[in]  crnti
+ *  @return     ueCb
+ **/
+SchUeCb* schGetUeCb(SchCellCb *cellCb, uint16_t crnti)
+{
+   uint16_t ueIdx;
+   GET_UE_IDX(crnti, ueIdx);
+   return &(cellCb->ueCb[ueIdx -1]);
+}
+
+/**
+ * @brief initialize UL slot info
+ *
+ * @details
+ *
+ *     Function: schInitUlSlot
+ *
+ *     This function intializes UL slot of the cell
+ *
+ *  @param[in]  schUlSlotInfo
+ *  @return     void
+ **/
+void schInitUlSlot(SchUlSlotInfo *schUlSlotInfo)
+{
+   memset(schUlSlotInfo, 0, sizeof(SchUlSlotInfo));
+   schUlSlotInfo->totalPrb = MAX_NUM_RB;
+   for(uint8_t itr=0; itr<SCH_SYMBOL_PER_SLOT; itr++)
+   {
+      schUlSlotInfo->assignedPrb[itr] = 0;
+   }
+   schUlSlotInfo->resAllocBitMap = 0;
+   schUlSlotInfo->puschCurrentPrb = PUSCH_START_RB;
+   schUlSlotInfo->schPuschInfo = NULLP;
+
+}
+
+/**
+ * @brief initialize DL slot info
+ *
+ * @details
+ *
+ *     Function: schInitDlSlot
+ *
+ *     This function intializes DL slot of the cell
+ *
+ *  @param[in]  schDlSlotInfo
+ *  @return     void
+ **/
+void schInitDlSlot(SchDlSlotInfo *schDlSlotInfo)
+{
+   memset(schDlSlotInfo, 0, sizeof(SchDlSlotInfo));
+   schDlSlotInfo->totalPrb = MAX_NUM_RB;
+   for(uint8_t itr=0; itr<SCH_SYMBOL_PER_SLOT; itr++)
+   {
+      schDlSlotInfo->assignedPrb[itr] = 0;
+   }
+   schDlSlotInfo->resAllocBitMap = 0; 
+   for(uint8_t itr=0; itr<MAX_SSB_IDX; itr++)
+   {
+      memset(&schDlSlotInfo->ssbInfo[itr], 0, sizeof(SsbInfo));
+   }
+#if 0
+   //make allocation for SSB
+   if(cell->firstSsbTransmitted)
+   {
+      //TODO check if this slot and sfn are for ssb
+
+   }
+#endif
+}
+
+#ifdef NR_TDD
+
+/**
+ * @brief determines slot format
+ *
+ * @details
+ *
+ *     Function : schGetSlotSymbFrmt 
+ *      
+ *      This API is invoked to determine if current slot is DL or UL
+ *           
+ *  @param[in]  uint16_t slot
+ *  @param[in]  uint32_t bitMap from cellCb
+ *  @return  SlotConfig
+ *      -# DL    - 0 
+ *      -# UL    - 1
+ *      -# FLEXI - 2
+ **/
+SlotConfig schGetSlotSymbFrmt(uint16_t slot, uint32_t bitMap)
+{
+   SlotConfig slotFrmt;
+   int mask1 = 0, mask2 = 0;
+
+   slot = (slot%10)*2;
+   mask1 = 1<<(slot);
+   mask2 = 1<<(slot+1);
+   slotFrmt = ((mask1 & bitMap)>>slot) + (2*((mask2 & bitMap)>>(slot+1)));
+
+   //printf("\n\n\n\n*****FormatType:%d Slot:%d****\n\n\n\n", slotFrmt, slot/2);
+
+   return slotFrmt;
 }
 
+#endif
 /**********************************************************************
          End of file
 **********************************************************************/