Esempio n. 1
0
        private void ArgsProcess()
        {
            UFix x, xd, xq;

            _sinFlipSignIn.Next = "0";
            _cosFlipSignIn.Next = "0";
            if (X.Cur[XFracWidth + 1] == '1')
            {
                // x is negative
                x = (-SFix.FromSigned(X.Cur.SignedValue, XFracWidth - LUTWidth - 1)).UFixValue.Resize(LUTWidth + 2, XFracWidth - LUTWidth - 1);
                _sinFlipSignIn.Next = "1";
            }
            else
            {
                // x is non-negative
                x = UFix.FromUnsigned(X.Cur[XFracWidth, 0].UnsignedValue, XFracWidth - LUTWidth - 1);
            }
            if (x.SLVValue[XFracWidth] == '1' || x.SLVValue[XFracWidth - 1] == '1')
            {
                // between Pi/2 and Pi
                xd = (_mirror - x).Resize(LUTWidth + 1, XFracWidth).Resize(LUTWidth + 1, XFracWidth - LUTWidth - 1);
                xq = (x - _mirror2).Resize(LUTWidth + 1, XFracWidth).Resize(LUTWidth + 1, XFracWidth - LUTWidth - 1);
                _cosFlipSignIn.Next = "1";
            }
            else
            {
                xd = x.Resize(LUTWidth + 1, XFracWidth - LUTWidth - 1);
                xq = (_mirror2 - x).Resize(LUTWidth + 1, XFracWidth).Resize(LUTWidth + 1, XFracWidth - LUTWidth - 1);
            }
            _x.Next  = xd;
            _xq.Next = xq;
        }
Esempio n. 2
0
 private void DiagMonSFix()
 {
     if (Clk.RisingEdge() && EnIn.Cur == '1')
     {
         Console.WriteLine("Writing variable " + MappedVariableName + ": " + SFix.FromSigned(DIn.Cur.SignedValue, _fracWidth).DoubleValue);
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Converts <paramref name="src"/> to <paramref name="dstType"/> datatype, possibly with loss of precision or overflow.
        /// </summary>
        /// <remarks>Currently, conversions between all primitive numeric CIL types, enum types, and System#-intrinsic datatypes
        /// Signed, Unsigned, SFix, UFix and StdLogicVector are supported.</remarks>
        /// <exception cref="ArgumentNullException">if <paramref name="dstType"/> is null</exception>
        /// <exception cref="NotImplementedException">if there is no known conversion to <paramref name="dstType"/></exception>
        public static object ConvertLong(long src, TypeDescriptor dstType)
        {
            Contract.Requires <ArgumentNullException>(dstType != null, "dstType");

            if (dstType.CILType.Equals(typeof(Signed)))
            {
                return(Signed.FromLong(src, SFix.GetFormat(dstType).IntWidth));
            }
            else if (dstType.CILType.Equals(typeof(Unsigned)))
            {
                return(Unsigned.FromBigInt(new System.Numerics.BigInteger(src), UFix.GetFormat(dstType).IntWidth));
            }
            else if (dstType.CILType.Equals(typeof(SFix)))
            {
                return(SFix.FromSigned(Signed.FromLong(src, SFix.GetFormat(dstType).IntWidth), SFix.GetFormat(dstType).FracWidth));
            }
            else if (dstType.CILType.Equals(typeof(StdLogicVector)))
            {
                return(StdLogicVector.FromLong(src, StdLogicVector.GetLength(dstType)));
            }
            else
            {
                return(ConvertLong(src, dstType.CILType));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Converts <paramref name="src"/> to <paramref name="dstType"/> datatype, possibly with loss of precision or overflow.
        /// </summary>
        /// <remarks>Currently, conversions between all primitive numeric CIL types and enum types are supported.</remarks>
        /// <exception cref="ArgumentNullException">if <paramref name="dstType"/> is null</exception>
        /// <exception cref="NotImplementedException">if there is no known conversion to <paramref name="dstType"/></exception>
        public static object ConvertSigned(Signed src, Type dstType)
        {
            Contract.Requires <ArgumentNullException>(dstType != null, "dstType");

            if (dstType.Equals(typeof(double)))
            {
                return(SFix.FromSigned(src, 0).DoubleValue);
            }
            else
            {
                return(ConvertValue(src.LongValue, dstType));
            }
        }
Esempio n. 5
0
        private async void StimulateAndTest()
        {
            DesignContext.Instance.FixPoint.DefaultRadix = 10;

            for (double i = -1.0; i <= 1.0; i += 0.0625)
            {
                var x = SFix.FromDouble(i, 2, _xFracWidth);
                _x.Next = x.SLVValue;
                //DesignContext.Wait(_pipeStages + 4);
                await NTicks(_pipeStages + 3);

                Console.WriteLine("X = " + x +
                                  ", Sin = " + SFix.FromSigned(_sin.Cur.SignedValue, _yFracWidth) +
                                  ", Cos = " + SFix.FromSigned(_cos.Cur.SignedValue, _yFracWidth));
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Converts <paramref name="src"/> to <paramref name="dstType"/> datatype, possibly with loss of precision or overflow.
        /// </summary>
        /// <remarks>Currently, conversions between all primitive numeric CIL types, enum types, and System#-intrinsic datatypes
        /// Signed, Unsigned, SFix, UFix and StdLogicVector are supported.</remarks>
        /// <exception cref="ArgumentNullException">if <paramref name="dstType"/> is null</exception>
        /// <exception cref="NotImplementedException">if there is no known conversion to <paramref name="dstType"/></exception>
        public static object ConvertSigned(Signed src, TypeDescriptor dstType)
        {
            Contract.Requires <ArgumentNullException>(dstType != null, "dstType");

            if (dstType.CILType.Equals(typeof(Signed)))
            {
                return(src.Resize(SFix.GetFormat(dstType).IntWidth));
            }
            else if (dstType.CILType.Equals(typeof(SFix)))
            {
                return(SFix.FromSigned(src.Resize(SFix.GetFormat(dstType).TotalWidth), SFix.GetFormat(dstType).FracWidth));
            }
            else
            {
                return(ConvertSigned(src, dstType.CILType));
            }
        }
Esempio n. 7
0
        private async void Computation()
        {
            await Tick;

            while (true)
            {
                ProgramFlow.DoNotUnroll();
                ProgramFlow.IOBarrier();
                Rdy.Next = '0';
                var sincos = MathExt.ScSinCos(SFix.FromSigned(X.Cur.SignedValue, _xFracWidth), _yFracWidth);
                Cos.Next = sincos.Item1.SLVValue;
                Sin.Next = sincos.Item2.SLVValue;
                await 12.Ticks();
                ProgramFlow.IOBarrier();
                Rdy.Next = '1';
                ProgramFlow.IOBarrier();
                await Tick;
            }
        }
Esempio n. 8
0
        private async void StimProcess()
        {
            DesignContext.Instance.FixPoint.DefaultRadix = 10;
            await Tick;

            double cur = 2.1;

            while (true)
            {
                var inval = SFix.FromDouble(cur, _mod2.InIntWidth, _mod2.FracWidth);
                _x.Next = inval.SLVValue;
                await Tick;
                var   outval = SFix.FromSigned(_r.Cur.SignedValue, _mod2.FracWidth);
                var   outd   = outval.DoubleValue;
                var   refd   = Math.IEEERemainder(cur, 2.0);
                Debug.Assert(outd >= -1.0 && outd <= 1.0);
                Console.WriteLine(cur + " mod 2 == " + outd);
                cur -= 0.1;
            }
        }
Esempio n. 9
0
        private void MemProcess()
        {
            Unsigned addr;

            if (MemRdEn.Cur == '1' || MemWrEn.Cur == '1')
            {
                addr = MemAddr.Cur;
                if (MemWrEn.Cur == '1')
                {
                    _memContent[addr].Next = SFix.FromSigned(MemDataIn.Cur.SignedValue, DFracWidth);
                }
            }
            else
            {
                addr = _unitAddr.Cur;
            }
            SFix data = _memContent[addr].Cur;

            _unitData.Next  = data;
            MemDataOut.Next = data.SLVValue;
        }
Esempio n. 10
0
        private async void StimulateAndTest()
        {
            DesignContext.Instance.FixPoint.DefaultRadix = 10;

            for (double i = -1.0; i < 1.0; i += 0.0625)
            {
                var x = SFix.FromDouble(i, 2, _xFracWidth);
                _x.Next = x.SLVValue;
                while (_rdy.Cur != '0')
                {
                    await Tick;
                }
                while (_rdy.Cur != '1')
                {
                    await Tick;
                }
                Console.WriteLine("X = " + x +
                                  ", Sin = " + SFix.FromSigned(_sin.Cur.SignedValue, _yFracWidth) +
                                  ", Cos = " + SFix.FromSigned(_cos.Cur.SignedValue, _yFracWidth));
            }
        }
Esempio n. 11
0
        public async void Process()
        {
            _X_IN.Next     = "0010110101";
            _Y_IN.Next     = "0001000000";
            _PHASE_IN.Next = "1100110111";

            SFix   x  = SFix.FromSigned(_X_IN.Cur.SignedValue, 10 - 2);
            double xd = x.DoubleValue;

            SFix   y  = SFix.FromSigned(_Y_IN.Cur.SignedValue, 10 - 2);
            double yd = y.DoubleValue;

            SFix   w  = SFix.FromSigned(_PHASE_IN.Cur.SignedValue, 10 - 3);
            double wd = w.DoubleValue;

            double X_OU1 = (Math.Cos(wd)) * xd - (Math.Sin(wd)) * yd;
            double Y_OU1 = (Math.Cos(wd)) * xd + (Math.Sin(wd)) * yd;

            double x_ou1 = xd * xd + yd * yd;
            double x_ou2 = Math.Sqrt(x_ou1);
            double w_ou  = Math.Atan2(xd, yd);

            double x_ou3 = Math.Cos(wd);
            double y_ou3 = Math.Sin(wd);

            await Tick;

            Console.WriteLine(" Rotate: " + " " + "x_out = " + _X_OUT.Cur.ToString() + " " + "y_out=" + _Y_OUT.Cur.ToString());
            Console.WriteLine(" Rotate in double : x_ou = " + X_OU1 + " y_ou = " + Y_OU1);
            Console.WriteLine(" Translate: " + " " + "x_out1 = " + _X_OUT1.Cur.ToString() + " " + "phse_out 1 =" + _PHASE_OUT1.Cur.ToString());
            Console.WriteLine(" Translate in double : x_ou = " + x_ou2 + " Winkel " + w_ou);
            Console.WriteLine(" SinAndCos: " + "  " + "x_out2 = " + _X_OUT2.Cur.ToString() + " " + "y_out2 =" + _Y_OUT2.Cur.ToString());
            Console.WriteLine("SinAndCos in double : x_ou = " + x_ou3 + "  + y_ou = " + y_ou3);
            // Console.WriteLine(" SinhAndCosh: " + "  " + "x_out2 = " + _X_OUT2.Cur.ToString() + " " + "y_out2 =" + _Y_OUT2.Cur.ToString());

            while (true)
            {
                await Tick;
            }
        }
Esempio n. 12
0
        public static SFix Rempow2(SFix value, int n)
        {
            var rem = SFix.FromSigned(Signed.One, -n);

            return(value % rem);
        }
Esempio n. 13
0
 public static SFix SignedAbs(SFix value)
 {
     return(SFix.FromSigned(
                SignedAbs(value.SignedValue),
                value.Format.FracWidth));
 }
Esempio n. 14
0
        private void HandleDiv(XILSInstr i)
        {
            var preds       = i.Preds;
            var fmtDividend = i.OperandTypes[0].GetFixFormat();
            var fmtDivisor  = i.OperandTypes[1].GetFixFormat();
            var fmtQuotient = i.ResultTypes[0].GetFixFormat();

            if (fmtDividend == null ||
                fmtDivisor == null ||
                fmtQuotient == null)
            {
                ProcessDefault(i);
                return;
            }

            if (!fmtDivisor.IsSigned)
            {
                // Xilinx divider wants it signed
                var signedType = SFix.MakeType(fmtDivisor.IntWidth + 1, fmtDivisor.FracWidth);
                Emit(DefaultInstructionSet.Instance.Convert().CreateStk(preds, 1, i.OperandTypes[1], signedType));
                var newi = i.Command.CreateStk(2, i.OperandTypes[0], signedType, i.ResultTypes[0]);
                HandleDiv(newi);
                return;
            }

            if (!fmtDividend.IsSigned)
            {
                // Xilinx divider wants it signed
                var signedType = SFix.MakeType(fmtDividend.IntWidth + 1, fmtDividend.FracWidth);
                Emit(DefaultInstructionSet.Instance.Swap().CreateStk(preds, 2,
                                                                     i.OperandTypes[0], i.OperandTypes[1],
                                                                     i.OperandTypes[1], i.OperandTypes[0]));
                Emit(DefaultInstructionSet.Instance.Convert().CreateStk(preds, 1, i.OperandTypes[0], signedType));
                Emit(DefaultInstructionSet.Instance.Swap().CreateStk(2,
                                                                     i.OperandTypes[1], signedType,
                                                                     signedType, i.OperandTypes[1]));
                var newi = i.Command.CreateStk(2, signedType, i.OperandTypes[1], i.ResultTypes[0]);
                HandleDiv(newi);
                return;
            }

            if (!fmtQuotient.IsSigned)
            {
                // Xilinx divider wants it signed
                var signedType = SFix.MakeType(fmtQuotient.IntWidth + 1, fmtQuotient.FracWidth);
                var newi       = i.Command.CreateStk(preds, 2, i.OperandTypes[0], i.OperandTypes[1], signedType);
                HandleDiv(newi);
                Emit(DefaultInstructionSet.Instance.Convert().CreateStk(1, signedType, i.ResultTypes[0]));
                return;
            }

            if (fmtDividend.TotalWidth < 4 ||
                fmtDivisor.TotalWidth < 4)
            {
                // Xilinx fixed point divider doesn't like divisions of less than 4 bits
                throw new NotImplementedException("Encountered fixed point division with less than 4 bits of either dividend or divisor. This is not supported, please adjust the division!");

                /*Emit(DefaultInstructionSet.Instance.Swap().CreateStk(preds, 2,
                 *  i.OperandTypes[0], i.OperandTypes[1],
                 *  i.OperandTypes[1], i.OperandTypes[0]));
                 * int delta = 4 - fmtDividend.TotalWidth - 4;
                 * var newDividendType = fmtDividend.IsSigned ?
                 *  SFix.MakeType(
                 * Emit(i.Command.CreateStk(1, i.OperandTypes[1]*/
            }

            int    hwQuotientTotalWidth = fmtDividend.TotalWidth;
            int    hwQuotientFracWidth  = fmtDividend.FracWidth - fmtDivisor.FracWidth;
            object hwQuotSample         = fmtDividend.IsSigned ?
                                          (object)SFix.FromDouble(0.0, hwQuotientTotalWidth - hwQuotientFracWidth, hwQuotientFracWidth) :
                                          (object)UFix.FromDouble(0.0, hwQuotientTotalWidth - hwQuotientFracWidth, hwQuotientFracWidth);
            var            hwQuotType = TypeDescriptor.GetTypeOf(hwQuotSample);
            TypeDescriptor hwQuotAndFracType;

            if (hwQuotientFracWidth >= fmtQuotient.FracWidth)
            {
                Emit(i.Command.CreateStk(preds, 2, i.OperandTypes[0], i.OperandTypes[1], hwQuotType));
                hwQuotAndFracType = hwQuotType;
            }
            else
            {
                int fracWidth = fmtQuotient.FracWidth - hwQuotientFracWidth;
                if (fracWidth > 54)
                {
                    // Xilinx divider doesn't like fractional width > 54
                    throw new NotImplementedException("Encountered fixed point division with more than 54 bits of fractional width. This is not supported, please adjust the division!");
                }
                var hwFracSample = UFix.FromDouble(0.0, -hwQuotientFracWidth, fmtQuotient.FracWidth);
                var hwFracType   = TypeDescriptor.GetTypeOf(hwFracSample);
                Emit(DefaultInstructionSet.Instance.DivQF().CreateStk(preds, 2, i.OperandTypes[0], i.OperandTypes[1], hwQuotType, hwFracType));
                var hwQuotSLV     = Marshal.SerializeForHW(hwQuotSample);
                var hwFracSLV     = Marshal.SerializeForHW(hwFracSample);
                var hwQuotSLVType = TypeDescriptor.GetTypeOf(hwQuotSLV);
                var hwFracSLVType = TypeDescriptor.GetTypeOf(hwFracSLV);
                Emit(DefaultInstructionSet.Instance.Convert().CreateStk(1, hwFracType, hwFracSLVType));
                Emit(DefaultInstructionSet.Instance.Dig(1).CreateStk(2, hwQuotType, hwFracSLVType, hwFracSLVType, hwQuotType));
                Emit(DefaultInstructionSet.Instance.Convert().CreateStk(1, hwQuotType, hwQuotSLVType));
                Emit(DefaultInstructionSet.Instance.Dig(1).CreateStk(2, hwFracSLVType, hwQuotSLVType, hwQuotSLVType, hwFracSLVType));
                var hwConcSLV     = hwQuotSLV.Concat(hwFracSLV);
                var hwConcSLVType = TypeDescriptor.GetTypeOf(hwConcSLV);
                Emit(DefaultInstructionSet.Instance.Concat().CreateStk(2, hwQuotSLVType, hwFracSLVType, hwConcSLVType));
                object hwConc;
                if (fmtDividend.IsSigned)
                {
                    hwConc = SFix.FromSigned(hwConcSLV.SignedValue, fmtQuotient.FracWidth);
                }
                else
                {
                    hwConc = UFix.FromUnsigned(hwConcSLV.UnsignedValue, fmtQuotient.FracWidth);
                }
                var hwConcType = TypeDescriptor.GetTypeOf(hwConc);
                Emit(DefaultInstructionSet.Instance.Convert().CreateStk(1, hwConcSLVType, hwConcType));
                hwQuotAndFracType = hwConcType;
            }
            if (!hwQuotAndFracType.Equals(i.ResultTypes[0]))
            {
                Emit(DefaultInstructionSet.Instance.Convert().CreateStk(1, hwQuotAndFracType, i.ResultTypes[0]));
            }
        }