0
\$\begingroup\$

I need to convert Power data coming out of my FFT->Cordic into dBm

I know the coefficient of my ADC and FFT and all I need is for every case of power coming to my block, to convert it to the right dBm value.

I do this by compare every range of values and for every rang eof values I output specific dBm value.

This is my working code:

module pw2dBm 
#(
    parameter IN_WIDTH = 28,
    parameter OUT_WIDTH = 8
)
(
    input                           clk,
    input                           rstn,
    input                           div,
    input       [IN_WIDTH-1:0]      din,

    output reg                      dov,
    output reg                      obe,
    output reg  [OUT_WIDTH-1:0]     dout
);


    reg                 div_r;
    reg  [IN_WIDTH-1:0] din_r;

    reg                 obe_f;  // output block end flag


    initial begin
        dov     = 1'b0;
        obe     = 1'b0;
        dout    = {OUT_WIDTH{1'b0}};

        div_r   = 1'b0;
        din_r   = {IN_WIDTH{1'b0}};
        obe_f   = 1'b0;
    end


    always @(posedge clk or negedge rstn) begin 
        if (~rstn) begin
            div_r   <= 1'b0;
            din_r   <= {IN_WIDTH{1'b0}};
            obe_f   <= 1'b0;
            obe     <= 1'b0;
        end else begin
            if (div) begin
                obe_f   <= 1'b1;
                obe     <= 1'b0;

                div_r   <= 1'b1;
                din_r   <= din;
            end else begin
                if (obe_f) begin
                    obe_f   <= 1'b0;
                    obe     <= 1'b1;
                end else begin
                    obe     <= 1'b0;
                end
                div_r   <= 1'b0;
                din_r   <= {IN_WIDTH{1'b0}};
            end
        end
    end

    function reg inbetween;
        input [IN_WIDTH-1:0] low; 
        input [IN_WIDTH-1:0] value; 
        input [IN_WIDTH-1:0] high; 
    begin
        inbetween = value >= low && value < high;
    end
    endfunction

    always @(posedge clk or negedge rstn) begin : lookup_table
        if (~rstn) begin
            dov     <= 1'b0;
            dout    <= {OUT_WIDTH{1'b0}};
        end else if (div_r) begin
            dov     <= 1'b1;
            case(1) 
                inbetween(0, din_r, 1): dout <= 8'h00;  // -132.041 dBm
                inbetween(1, din_r, 2): dout <= 8'h01;  // -126.862 dBm
                inbetween(2, din_r, 3): dout <= 8'h02;  // -123.163 dBm
                inbetween(3, din_r, 4): dout <= 8'h03;  // -120.697 dBm
                inbetween(4, din_r, 5): dout <= 8'h04;  // -118.724 dBm
                inbetween(5, din_r, 6): dout <= 8'h05;  // -116.998 dBm
                inbetween(6, din_r, 7): dout <= 8'h06;  // -115.765 dBm
                inbetween(7, din_r, 8): dout <= 8'h07;  // -114.779 dBm
                inbetween(8, din_r, 9): dout <= 8'h08;  // -113.793 dBm
                inbetween(9, din_r, 10):    dout <= 8'h09;  // -112.806 dBm
                inbetween(10, din_r, 11):   dout <= 8'h0A;  // -111.820 dBm
                inbetween(11, din_r, 12):   dout <= 8'h0B;  // -111.080 dBm
                inbetween(12, din_r, 13):   dout <= 8'h0C;  // -110.340 dBm
                inbetween(13, din_r, 14):   dout <= 8'h0D;  // -109.600 dBm
                inbetween(14, din_r, 15):   dout <= 8'h0E;  // -109.107 dBm
                inbetween(15, din_r, 16):   dout <= 8'h0F;  // -108.614 dBm
                inbetween(16, din_r, 17):   dout <= 8'h10;  // -108.121 dBm
                inbetween(17, din_r, 18):   dout <= 8'h11;  // -107.628 dBm
                inbetween(18, din_r, 19):   dout <= 8'h12;  // -107.134 dBm
                inbetween(19, din_r, 20):   dout <= 8'h13;  // -106.641 dBm
                inbetween(20, din_r, 21):   dout <= 8'h14;  // -106.148 dBm
                inbetween(21, din_r, 23):   dout <= 8'h15;  // -105.655 dBm
                inbetween(23, din_r, 24):   dout <= 8'h16;  // -105.162 dBm
                inbetween(24, din_r, 26):   dout <= 8'h17;  // -104.668 dBm
                inbetween(26, din_r, 27):   dout <= 8'h18;  // -104.175 dBm
                inbetween(27, din_r, 29):   dout <= 8'h19;  // -103.682 dBm
                inbetween(29, din_r, 31):   dout <= 8'h1A;  // -103.189 dBm
                inbetween(31, din_r, 32):   dout <= 8'h1B;  // -102.696 dBm
                inbetween(32, din_r, 34):   dout <= 8'h1C;  // -102.202 dBm
                inbetween(34, din_r, 36):   dout <= 8'h1D;  // -101.709 dBm
                inbetween(36, din_r, 39):   dout <= 8'h1E;  // -101.216 dBm
                inbetween(39, din_r, 41):   dout <= 8'h1F;  // -100.723 dBm
                inbetween(41, din_r, 43):   dout <= 8'h20;  // -100.230 dBm
                inbetween(43, din_r, 46):   dout <= 8'h21;  // -99.736 dBm
                inbetween(46, din_r, 49):   dout <= 8'h22;  // -99.243 dBm
                inbetween(49, din_r, 52):   dout <= 8'h23;  // -98.750 dBm
                inbetween(52, din_r, 55):   dout <= 8'h24;  // -98.257 dBm
                inbetween(55, din_r, 58):   dout <= 8'h25;  // -97.764 dBm
                inbetween(58, din_r, 61):   dout <= 8'h26;  // -97.270 dBm
                inbetween(61, din_r, 65):   dout <= 8'h27;  // -96.777 dBm
                inbetween(65, din_r, 69):   dout <= 8'h28;  // -96.284 dBm
                inbetween(69, din_r, 73):   dout <= 8'h29;  // -95.791 dBm
                inbetween(73, din_r, 77):   dout <= 8'h2A;  // -95.298 dBm
                inbetween(77, din_r, 82):   dout <= 8'h2B;  // -94.804 dBm
                inbetween(82, din_r, 87):   dout <= 8'h2C;  // -94.311 dBm
                inbetween(87, din_r, 92):   dout <= 8'h2D;  // -93.818 dBm
                inbetween(92, din_r, 97):   dout <= 8'h2E;  // -93.325 dBm
                inbetween(97, din_r, 103):  dout <= 8'h2F;  // -92.832 dBm
                inbetween(103, din_r, 109): dout <= 8'h30;  // -92.338 dBm
                inbetween(109, din_r, 115): dout <= 8'h31;  // -91.845 dBm
                inbetween(115, din_r, 122): dout <= 8'h32;  // -91.352 dBm
                inbetween(122, din_r, 129): dout <= 8'h33;  // -90.859 dBm
                inbetween(129, din_r, 137): dout <= 8'h34;  // -90.366 dBm
                inbetween(137, din_r, 145): dout <= 8'h35;  // -89.872 dBm
                inbetween(145, din_r, 154): dout <= 8'h36;  // -89.379 dBm
                inbetween(154, din_r, 163): dout <= 8'h37;  // -88.886 dBm
                inbetween(163, din_r, 172): dout <= 8'h38;  // -88.393 dBm
                inbetween(172, din_r, 182): dout <= 8'h39;  // -87.900 dBm
                inbetween(182, din_r, 193): dout <= 8'h3A;  // -87.406 dBm
                inbetween(193, din_r, 205): dout <= 8'h3B;  // -86.913 dBm
                inbetween(205, din_r, 217): dout <= 8'h3C;  // -86.420 dBm
                inbetween(217, din_r, 229): dout <= 8'h3D;  // -85.927 dBm
                inbetween(229, din_r, 243): dout <= 8'h3E;  // -85.434 dBm
                inbetween(243, din_r, 257): dout <= 8'h3F;  // -84.940 dBm
                inbetween(257, din_r, 272): dout <= 8'h40;  // -84.447 dBm
                inbetween(272, din_r, 288): dout <= 8'h41;  // -83.954 dBm
                inbetween(288, din_r, 305): dout <= 8'h42;  // -83.461 dBm
                inbetween(305, din_r, 323): dout <= 8'h43;  // -82.968 dBm
                inbetween(323, din_r, 342): dout <= 8'h44;  // -82.474 dBm
                inbetween(342, din_r, 362): dout <= 8'h45;  // -81.981 dBm
                inbetween(362, din_r, 383): dout <= 8'h46;  // -81.488 dBm
                inbetween(383, din_r, 405): dout <= 8'h47;  // -80.995 dBm
                inbetween(405, din_r, 429): dout <= 8'h48;  // -80.502 dBm
                inbetween(429, din_r, 454): dout <= 8'h49;  // -80.009 dBm
                inbetween(454, din_r, 481): dout <= 8'h4A;  // -79.515 dBm
                inbetween(481, din_r, 509): dout <= 8'h4B;  // -79.022 dBm
                inbetween(509, din_r, 539): dout <= 8'h4C;  // -78.529 dBm
                inbetween(539, din_r, 570): dout <= 8'h4D;  // -78.036 dBm
                inbetween(570, din_r, 603): dout <= 8'h4E;  // -77.543 dBm
                inbetween(603, din_r, 639): dout <= 8'h4F;  // -77.049 dBm
                inbetween(639, din_r, 676): dout <= 8'h50;  // -76.556 dBm
                inbetween(676, din_r, 716): dout <= 8'h51;  // -76.063 dBm
                inbetween(716, din_r, 758): dout <= 8'h52;  // -75.570 dBm
                inbetween(758, din_r, 802): dout <= 8'h53;  // -75.077 dBm
                inbetween(802, din_r, 849): dout <= 8'h54;  // -74.583 dBm
                inbetween(849, din_r, 898): dout <= 8'h55;  // -74.090 dBm
                inbetween(898, din_r, 951): dout <= 8'h56;  // -73.597 dBm
                inbetween(951, din_r, 1007):    dout <= 8'h57;  // -73.104 dBm
                inbetween(1007, din_r, 1065):   dout <= 8'h58;  // -72.611 dBm
                inbetween(1065, din_r, 1128):   dout <= 8'h59;  // -72.117 dBm
                inbetween(1128, din_r, 1194):   dout <= 8'h5A;  // -71.624 dBm
                inbetween(1194, din_r, 1264):   dout <= 8'h5B;  // -71.131 dBm
                inbetween(1264, din_r, 1337):   dout <= 8'h5C;  // -70.638 dBm
                inbetween(1337, din_r, 1416):   dout <= 8'h5D;  // -70.145 dBm
                inbetween(1416, din_r, 1498):   dout <= 8'h5E;  // -69.651 dBm
                inbetween(1498, din_r, 1586):   dout <= 8'h5F;  // -69.158 dBm
                inbetween(1586, din_r, 1679):   dout <= 8'h60;  // -68.665 dBm
                inbetween(1679, din_r, 1777):   dout <= 8'h61;  // -68.172 dBm
                inbetween(1777, din_r, 1881):   dout <= 8'h62;  // -67.679 dBm
                inbetween(1881, din_r, 1991):   dout <= 8'h63;  // -67.185 dBm
                inbetween(1991, din_r, 2107):   dout <= 8'h64;  // -66.692 dBm
                inbetween(2107, din_r, 2230):   dout <= 8'h65;  // -66.199 dBm
                inbetween(2230, din_r, 2361):   dout <= 8'h66;  // -65.706 dBm
                inbetween(2361, din_r, 2498):   dout <= 8'h67;  // -65.213 dBm
                inbetween(2498, din_r, 2645):   dout <= 8'h68;  // -64.719 dBm
                inbetween(2645, din_r, 2799):   dout <= 8'h69;  // -64.226 dBm
                inbetween(2799, din_r, 2963):   dout <= 8'h6A;  // -63.733 dBm
                inbetween(2963, din_r, 3136):   dout <= 8'h6B;  // -63.240 dBm
                inbetween(3136, din_r, 3319):   dout <= 8'h6C;  // -62.747 dBm
                inbetween(3319, din_r, 3513):   dout <= 8'h6D;  // -62.253 dBm
                inbetween(3513, din_r, 3718):   dout <= 8'h6E;  // -61.760 dBm
                inbetween(3718, din_r, 3936):   dout <= 8'h6F;  // -61.267 dBm
                inbetween(3936, din_r, 4166):   dout <= 8'h70;  // -60.774 dBm
                inbetween(4166, din_r, 4409):   dout <= 8'h71;  // -60.281 dBm
                inbetween(4409, din_r, 4667):   dout <= 8'h72;  // -59.787 dBm
                inbetween(4667, din_r, 4939):   dout <= 8'h73;  // -59.294 dBm
                inbetween(4939, din_r, 5228):   dout <= 8'h74;  // -58.801 dBm
                inbetween(5228, din_r, 5534):   dout <= 8'h75;  // -58.308 dBm
                inbetween(5534, din_r, 5857):   dout <= 8'h76;  // -57.815 dBm
                inbetween(5857, din_r, 6199):   dout <= 8'h77;  // -57.321 dBm
                inbetween(6199, din_r, 6561):   dout <= 8'h78;  // -56.828 dBm
                inbetween(6561, din_r, 6945):   dout <= 8'h79;  // -56.335 dBm
                inbetween(6945, din_r, 7351):   dout <= 8'h7A;  // -55.842 dBm
                inbetween(7351, din_r, 7780):   dout <= 8'h7B;  // -55.349 dBm
                inbetween(7780, din_r, 8235):   dout <= 8'h7C;  // -54.855 dBm
                inbetween(8235, din_r, 8716):   dout <= 8'h7D;  // -54.362 dBm
                inbetween(8716, din_r, 9225):   dout <= 8'h7E;  // -53.869 dBm
                inbetween(9225, din_r, 9764):   dout <= 8'h7F;  // -53.376 dBm
                inbetween(9764, din_r, 10335):  dout <= 8'h80;  // -52.883 dBm
                inbetween(10335, din_r, 10939): dout <= 8'h81;  // -52.389 dBm
                inbetween(10939, din_r, 11578): dout <= 8'h82;  // -51.896 dBm
                inbetween(11578, din_r, 12254): dout <= 8'h83;  // -51.403 dBm
                inbetween(12254, din_r, 12970): dout <= 8'h84;  // -50.910 dBm
                inbetween(12970, din_r, 13728): dout <= 8'h85;  // -50.417 dBm
                inbetween(13728, din_r, 14530): dout <= 8'h86;  // -49.923 dBm
                inbetween(14530, din_r, 15379): dout <= 8'h87;  // -49.430 dBm
                inbetween(15379, din_r, 16278): dout <= 8'h88;  // -48.937 dBm
                inbetween(16278, din_r, 17229): dout <= 8'h89;  // -48.444 dBm
                inbetween(17229, din_r, 18236): dout <= 8'h8A;  // -47.951 dBm
                inbetween(18236, din_r, 19301): dout <= 8'h8B;  // -47.457 dBm
                inbetween(19301, din_r, 20429): dout <= 8'h8C;  // -46.964 dBm
                inbetween(20429, din_r, 21622): dout <= 8'h8D;  // -46.471 dBm
                inbetween(21622, din_r, 22886): dout <= 8'h8E;  // -45.978 dBm
                inbetween(22886, din_r, 24223): dout <= 8'h8F;  // -45.485 dBm
                inbetween(24223, din_r, 25638): dout <= 8'h90;  // -44.991 dBm
                inbetween(25638, din_r, 27136): dout <= 8'h91;  // -44.498 dBm
                inbetween(27136, din_r, 28722): dout <= 8'h92;  // -44.005 dBm
                inbetween(28722, din_r, 30400): dout <= 8'h93;  // -43.512 dBm
                inbetween(30400, din_r, 32176): dout <= 8'h94;  // -43.019 dBm
                inbetween(32176, din_r, 34056): dout <= 8'h95;  // -42.526 dBm
                inbetween(34056, din_r, 36046): dout <= 8'h96;  // -42.032 dBm
                inbetween(36046, din_r, 38152): dout <= 8'h97;  // -41.539 dBm
                inbetween(38152, din_r, 40381): dout <= 8'h98;  // -41.046 dBm
                inbetween(40381, din_r, 42740): dout <= 8'h99;  // -40.553 dBm
                inbetween(42740, din_r, 45237): dout <= 8'h9A;  // -40.060 dBm
                inbetween(45237, din_r, 47880): dout <= 8'h9B;  // -39.566 dBm
                inbetween(47880, din_r, 50677): dout <= 8'h9C;  // -39.073 dBm
                inbetween(50677, din_r, 53638): dout <= 8'h9D;  // -38.580 dBm
                inbetween(53638, din_r, 56772): dout <= 8'h9E;  // -38.087 dBm
                inbetween(56772, din_r, 60089): dout <= 8'h9F;  // -37.594 dBm
                inbetween(60089, din_r, 63600): dout <= 8'hA0;  // -37.100 dBm
                inbetween(63600, din_r, 67316): dout <= 8'hA1;  // -36.607 dBm
                inbetween(67316, din_r, 71249): dout <= 8'hA2;  // -36.114 dBm
                inbetween(71249, din_r, 75411): dout <= 8'hA3;  // -35.621 dBm
                inbetween(75411, din_r, 79817): dout <= 8'hA4;  // -35.128 dBm
                inbetween(79817, din_r, 84481): dout <= 8'hA5;  // -34.634 dBm
                inbetween(84481, din_r, 89416): dout <= 8'hA6;  // -34.141 dBm
                inbetween(89416, din_r, 94641): dout <= 8'hA7;  // -33.648 dBm
                inbetween(94641, din_r, 100170):    dout <= 8'hA8;  // -33.155 dBm
                inbetween(100170, din_r, 106022):   dout <= 8'hA9;  // -32.662 dBm
                inbetween(106022, din_r, 112217):   dout <= 8'hAA;  // -32.168 dBm
                inbetween(112217, din_r, 118773):   dout <= 8'hAB;  // -31.675 dBm
                inbetween(118773, din_r, 125712):   dout <= 8'hAC;  // -31.182 dBm
                inbetween(125712, din_r, 133057):   dout <= 8'hAD;  // -30.689 dBm
                inbetween(133057, din_r, 140831):   dout <= 8'hAE;  // -30.196 dBm
                inbetween(140831, din_r, 149059):   dout <= 8'hAF;  // -29.702 dBm
                inbetween(149059, din_r, 157768):   dout <= 8'hB0;  // -29.209 dBm
                inbetween(157768, din_r, 166985):   dout <= 8'hB1;  // -28.716 dBm
                inbetween(166985, din_r, 176741):   dout <= 8'hB2;  // -28.223 dBm
                inbetween(176741, din_r, 187067):   dout <= 8'hB3;  // -27.730 dBm
                inbetween(187067, din_r, 197997):   dout <= 8'hB4;  // -27.236 dBm
                inbetween(197997, din_r, 209565):   dout <= 8'hB5;  // -26.743 dBm
                inbetween(209565, din_r, 221808):   dout <= 8'hB6;  // -26.250 dBm
                inbetween(221808, din_r, 234768):   dout <= 8'hB7;  // -25.757 dBm
                inbetween(234768, din_r, 248484):   dout <= 8'hB8;  // -25.264 dBm
                inbetween(248484, din_r, 263001):   dout <= 8'hB9;  // -24.770 dBm
                inbetween(263001, din_r, 278367):   dout <= 8'hBA;  // -24.277 dBm
                inbetween(278367, din_r, 294631):   dout <= 8'hBB;  // -23.784 dBm
                inbetween(294631, din_r, 311844):   dout <= 8'hBC;  // -23.291 dBm
                inbetween(311844, din_r, 330064):   dout <= 8'hBD;  // -22.798 dBm
                inbetween(330064, din_r, 349348):   dout <= 8'hBE;  // -22.304 dBm
                inbetween(349348, din_r, 369758):   dout <= 8'hBF;  // -21.811 dBm
                inbetween(369758, din_r, 391361):   dout <= 8'hC0;  // -21.318 dBm
                inbetween(391361, din_r, 414226):   dout <= 8'hC1;  // -20.825 dBm
                inbetween(414226, din_r, 438427):   dout <= 8'hC2;  // -20.332 dBm
                inbetween(438427, din_r, 464042):   dout <= 8'hC3;  // -19.838 dBm
                inbetween(464042, din_r, 491154):   dout <= 8'hC4;  // -19.345 dBm
                inbetween(491154, din_r, 519849):   dout <= 8'hC5;  // -18.852 dBm
                inbetween(519849, din_r, 550221):   dout <= 8'hC6;  // -18.359 dBm
                inbetween(550221, din_r, 582367):   dout <= 8'hC7;  // -17.866 dBm
                inbetween(582367, din_r, 616392):   dout <= 8'hC8;  // -17.372 dBm
                inbetween(616392, din_r, 652404):   dout <= 8'hC9;  // -16.879 dBm
                inbetween(652404, din_r, 690521):   dout <= 8'hCA;  // -16.386 dBm
                inbetween(690521, din_r, 730864):   dout <= 8'hCB;  // -15.893 dBm
                inbetween(730864, din_r, 773565):   dout <= 8'hCC;  // -15.400 dBm
                inbetween(773565, din_r, 818760):   dout <= 8'hCD;  // -14.906 dBm
                inbetween(818760, din_r, 866595):   dout <= 8'hCE;  // -14.413 dBm
                inbetween(866595, din_r, 917226):   dout <= 8'hCF;  // -13.920 dBm
                inbetween(917226, din_r, 970814):   dout <= 8'hD0;  // -13.427 dBm
                inbetween(970814, din_r, 1027534):  dout <= 8'hD1;  // -12.934 dBm
                inbetween(1027534, din_r, 1087567): dout <= 8'hD2;  // -12.440 dBm
                inbetween(1087567, din_r, 1151107): dout <= 8'hD3;  // -11.947 dBm
                inbetween(1151107, din_r, 1218360): dout <= 8'hD4;  // -11.454 dBm
                inbetween(1218360, din_r, 1289542): dout <= 8'hD5;  // -10.961 dBm
                inbetween(1289542, din_r, 1364883): dout <= 8'hD6;  // -10.468 dBm
                inbetween(1364883, din_r, 1444626): dout <= 8'hD7;  // -9.974 dBm
                inbetween(1444626, din_r, 1529027): dout <= 8'hD8;  // -9.481 dBm
                inbetween(1529027, din_r, 1618360): dout <= 8'hD9;  // -8.988 dBm
                inbetween(1618360, din_r, 1712912): dout <= 8'hDA;  // -8.495 dBm
                inbetween(1712912, din_r, 1812988): dout <= 8'hDB;  // -8.002 dBm
                inbetween(1812988, din_r, 1918911): dout <= 8'hDC;  // -7.509 dBm
                inbetween(1918911, din_r, 2031022): dout <= 8'hDD;  // -7.015 dBm
                inbetween(2031022, din_r, 2149683): dout <= 8'hDE;  // -6.522 dBm
                inbetween(2149683, din_r, 2275277): dout <= 8'hDF;  // -6.029 dBm
                inbetween(2275277, din_r, 2408209): dout <= 8'hE0;  // -5.536 dBm
                inbetween(2408209, din_r, 2548908): dout <= 8'hE1;  // -5.043 dBm
                inbetween(2548908, din_r, 2697826): dout <= 8'hE2;  // -4.549 dBm
                inbetween(2697826, din_r, 2855445): dout <= 8'hE3;  // -4.056 dBm
                inbetween(2855445, din_r, 3022273): dout <= 8'hE4;  // -3.563 dBm
                inbetween(3022273, din_r, 3198848): dout <= 8'hE5;  // -3.070 dBm
                inbetween(3198848, din_r, 3385739): dout <= 8'hE6;  // -2.577 dBm
                inbetween(3385739, din_r, 3583549): dout <= 8'hE7;  // -2.083 dBm
                inbetween(3583549, din_r, 3792916): dout <= 8'hE8;  // -1.590 dBm
                inbetween(3792916, din_r, 4014515): dout <= 8'hE9;  // -1.097 dBm
                inbetween(4014515, din_r, 4249061): dout <= 8'hEA;  // -0.604 dBm
                inbetween(4249061, din_r, 4497310): dout <= 8'hEB;  // -0.111 dBm
                inbetween(4497310, din_r, 4760063): dout <= 8'hEC;  // 0.383 dBm
                inbetween(4760063, din_r, 5038167): dout <= 8'hED;  // 0.876 dBm
                inbetween(5038167, din_r, 5332519): dout <= 8'hEE;  // 1.369 dBm
                inbetween(5332519, din_r, 5644069): dout <= 8'hEF;  // 1.862 dBm
                inbetween(5644069, din_r, 5973820): dout <= 8'hF0;  // 2.355 dBm
                inbetween(5973820, din_r, 6322838): dout <= 8'hF1;  // 2.849 dBm
                inbetween(6322838, din_r, 6692246): dout <= 8'hF2;  // 3.342 dBm
                inbetween(6692246, din_r, 7083237): dout <= 8'hF3;  // 3.835 dBm
                inbetween(7083237, din_r, 7497071): dout <= 8'hF4;  // 4.328 dBm
                inbetween(7497071, din_r, 7935083): dout <= 8'hF5;  // 4.821 dBm
                inbetween(7935083, din_r, 8398686): dout <= 8'hF6;  // 5.315 dBm
                inbetween(8398686, din_r, 8889375): dout <= 8'hF7;  // 5.808 dBm
                inbetween(8889375, din_r, 9408732): dout <= 8'hF8;  // 6.301 dBm
                inbetween(9408732, din_r, 9958432): dout <= 8'hF9;  // 6.794 dBm
                inbetween(9958432, din_r, 10540248):    dout <= 8'hFA;  // 7.287 dBm
                inbetween(10540248, din_r, 11156056):   dout <= 8'hFB;  // 7.781 dBm
                inbetween(11156056, din_r, 11807843):   dout <= 8'hFC;  // 8.274 dBm
                inbetween(11807843, din_r, 12497710):   dout <= 8'hFD;  // 8.767 dBm
                inbetween(12497710, din_r, 13227881):   dout <= 8'hFE;  // 9.260 dBm
                inbetween(13227881, din_r, 268435455):  dout <= 8'hFF;  // 9.753 dBm                    
            endcase
        end else begin
            dov     <= 1'b0;
            dout    <= {OUT_WIDTH{1'b0}};
        end
    end
endmodule

my question is how can I make this code more efficent? This block in my project is my biggest problem with Fmax, it makes something like 10-11ns delay path. I need somehow to make it twice efficent.

I need somehow to pipeline it, but I dont really knwo to make it right. any ideas or help about it? what is the best algorithm to calculate dBm in FPGA? there is a way to make my code more efficent?

Thanks!!

\$\endgroup\$
2
  • \$\begingroup\$ Where is inbetween defined? \$\endgroup\$
    – The Photon
    Commented May 10, 2020 at 16:34
  • \$\begingroup\$ further up in the code \$\endgroup\$ Commented May 10, 2020 at 16:35

1 Answer 1

4
\$\begingroup\$

I'm very surprised you came up with this incredibly complex code! This looks like you wanted to if/else if in an imperative language.

Good news: your FPGA is not a sequential CPU, there's no advantage to doing it this way. Just approximate the logarithm to a degree that your application considers exact. Hint: 8 bits in range for logarithmic values is a lot, and actually much more than the actual dynamic range of your system in default parametrication: your input is 28 bits wide (that's already a lot!) so your whole dynamic range between the smallest possible power value (1) and the largest possible power value (2²⁸) is only 10^(8.4). Not quite sure you gain much by having about 30 steps per decibel...

First of all: no use that you do it with logarithms to the base 10 – doing it with logarithms to the base 2 is much handier in binary systems, and in the end, you can just, instead of multiplying with 10 to go from log to dB, you'd multiply by 10·log10(2), a constant that isn't any different to your multiplication.

So, how to logarithm-base-2?

I'd actually try to pipeline that right away. Something like this C code (from bit twiddling hacks)

unsigned int v;          // 32-bit value to find the log2 of 
register unsigned int r; // result of log2(v) will go here
register unsigned int shift;

r =     (v > 0xFFFF) << 4; v >>= r;
shift = (v > 0xFF  ) << 3; v >>= shift; r |= shift;
shift = (v > 0xF   ) << 2; v >>= shift; r |= shift;
shift = (v > 0x3   ) << 1; v >>= shift; r |= shift;
                                        r |= (v >> 1);

You'll notice that if you just introduce a new register v_1, v_2 and so on, and the same for r, (and one wire shift_N for each stage), for every line in above code, you end up with something that is a pipelined recipe for a log2.

So, that's it! A four-stage that needs nothing but four integer comparisons with \$2^N-1\$, which is really just a bunch of ORs on the upper bits. Should use way less space than your huge set of comparisons, and also, has a nearly minimal combinatorial depth – relevant if you're going for high clock speeds.

Finally: You're not the first one to convert powers to decibel-of-unit, see tnt's logpwr module. He's done that to achieve this Realtime Spectrum-Analyzer alike display (with a few more features, like "digital phosphor", ie. slow fading out of bursty peaks etc):

RFNoC fosphor

You'll want to watch his talk from [GRCon'15] (slides); here's a demo of it running realtime.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.