private void HandleStoreVar(XILSInstr xilsi) { var var = (Variable)xilsi.StaticOperand; var mms = GetVariableLayout(var); Unsigned addr = mms.BaseAddress; var addrType = TypeDescriptor.GetTypeOf(addr); int addrSize = addr.Size; var rawWordType = Mapper.MarshalInfo.GetRawWordType(); uint concatSize = (uint)(mms.Size * Mapper.MarshalInfo.WordSize); var concatType = TypeDescriptor.GetTypeOf(StdLogicVector._0s(concatSize)); var valueType = var.Type; var slvValue = Marshal.SerializeForHW(valueType.GetSampleInstance()); var rawValueType = TypeDescriptor.GetTypeOf(slvValue); if (!rawValueType.Equals(valueType)) { Emit(_iset.Convert().CreateStk(1, valueType, rawValueType)); } if (!rawValueType.Equals(concatType)) { Emit(_iset.Convert().CreateStk(1, rawValueType, concatType)); } int shiftSize = MathExt.CeilLog2(concatSize); if (mms.Layout.Size > 1) { Emit(_iset.Dup().CreateStk(1, concatType, concatType, concatType)); } Unsigned shift = Unsigned.FromULong(Mapper.MarshalInfo.WordSize, shiftSize); TypeDescriptor shiftType = TypeDescriptor.GetTypeOf(shift); for (ulong i = 0; i < mms.Layout.Size; i++) { if (!rawWordType.Equals(concatType)) { Emit(_iset.Convert().CreateStk(1, concatType, rawWordType)); } Emit(_iset.WrMemFix(Mapper.DefaultRegion, addr).CreateStk(1, rawWordType)); if (i < mms.Layout.Size - 1) { if (i + 1 < mms.Layout.Size - 1) { Emit(_iset.Dup().CreateStk(1, concatType, concatType, concatType)); } Emit(_iset.LdConst(shift).CreateStk(0, shiftType)); Emit(_iset.RShift().CreateStk(1, concatType, concatType)); } addr = (addr + Unsigned.FromULong(1, 1)).Resize(addrSize); } }
/// <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 ConvertULong(ulong src, TypeDescriptor dstType) { Contract.Requires <ArgumentNullException>(dstType != null, "dstType"); if (dstType.CILType.Equals(typeof(Unsigned))) { return(Unsigned.FromULong(src, UFix.GetFormat(dstType).IntWidth)); } else if (dstType.CILType.Equals(typeof(Signed))) { return(Signed.FromBigInt(new System.Numerics.BigInteger(src), UFix.GetFormat(dstType).IntWidth)); } else if (dstType.CILType.Equals(typeof(UFix))) { return(UFix.FromUnsigned(Unsigned.FromULong(src, UFix.GetFormat(dstType).IntWidth), UFix.GetFormat(dstType).FracWidth)); } else { return(ConvertULong(src, dstType.CILType)); } }
private void HandleLoadVar(XILSInstr xilsi) { var var = (Variable)xilsi.StaticOperand; var mms = GetVariableLayout(var); Unsigned addr = mms.BaseAddress; var addrType = TypeDescriptor.GetTypeOf(addr); int addrSize = addr.Size; var rawWordType = Mapper.MarshalInfo.GetRawWordType(); var stackTypes = new TypeDescriptor[mms.Size + 1]; for (ulong k = 0; k < mms.Size; k++) { Emit(_iset.RdMemFix(Mapper.DefaultRegion, addr) .CreateStk(RemapPreds(xilsi.Preds), 0, rawWordType)); addr = (addr + Unsigned.FromULong(1, 1)).Resize(addrSize); stackTypes[k] = rawWordType; } uint concatSize = (uint)(mms.Size * Mapper.MarshalInfo.WordSize); var concatType = TypeDescriptor.GetTypeOf(StdLogicVector._0s(concatSize)); if (mms.Size > 1) { stackTypes[mms.Size] = concatType; Emit(_iset.Concat().CreateStk(stackTypes.Length, stackTypes)); } var valueType = var.Type; var slvValue = Marshal.SerializeForHW(valueType.GetSampleInstance()); var rawValueType = TypeDescriptor.GetTypeOf(slvValue); if (!rawValueType.Equals(concatType)) { Emit(_iset.Convert().CreateStk(1, concatType, rawValueType)); } if (!rawValueType.Equals(valueType)) { Emit(_iset.Convert().CreateStk(1, rawValueType, valueType)); } }
private void ImplementLdConst(object value) { var mms = GetDataLayout(value); Unsigned addr = mms.BaseAddress; var addrType = TypeDescriptor.GetTypeOf(addr); int addrSize = addr.Size; var rawWordType = Mapper.MarshalInfo.GetRawWordType(); var stackTypes = new TypeDescriptor[mms.Size + 1]; for (ulong k = 0; k < mms.Size; k++) { Emit(_iset.RdMemFix(Mapper.DefaultRegion, addr).CreateStk(0, rawWordType)); addr = (addr + Unsigned.FromULong(1, 1)).Resize(addrSize); stackTypes[k] = rawWordType; } uint concatSize = (uint)(mms.Size * Mapper.MarshalInfo.WordSize); var concatType = TypeDescriptor.GetTypeOf(StdLogicVector._0s(concatSize)); if (mms.Size > 1) { stackTypes[mms.Size] = concatType; Emit(_iset.Concat().CreateStk(stackTypes.Length, stackTypes)); } var valueType = TypeDescriptor.GetTypeOf(value); var slvValue = StdLogicVector.Serialize(value); var rawValueType = TypeDescriptor.GetTypeOf(slvValue); if (!rawValueType.Equals(concatType)) { Emit(_iset.Convert().CreateStk(1, concatType, rawValueType)); } if (!rawValueType.Equals(valueType)) { Emit(_iset.Convert().CreateStk(1, rawValueType, valueType)); } }
private Unsigned ComputeConstAddress(Array array, long[] indices, uint nword) { MemoryMappedStorage mms = GetDataLayout(array); ArrayMemoryLayout layout = mms.Layout as ArrayMemoryLayout; if (layout.ElementsPerWord > 1) { throw new NotImplementedException("Multiple elements per word not yet implemented"); } MemoryRegion region = mms.Region; IMarshalInfo minfo = region.MarshalInfo; Unsigned addr = mms.BaseAddress; for (int i = 0; i < indices.Length; i++) { Unsigned offs = Unsigned.FromULong((ulong)indices[i] * layout.Strides[i], mms.Region.AddressWidth); addr += offs; } addr += Unsigned.FromUInt(nword, mms.Region.AddressWidth); addr = addr.Resize(mms.Region.AddressWidth); return(addr); }
private async void Process() { float[,] sample = new float[16, 2]; uint index; data_fin_out.Next = false; do { data_req.Next = false; data_ready.Next = false; index = 0; //Reading in the Samples Log.WriteLine(); Log.WriteLine("Reading in the samples..."); while (index < 16) { data_req.Next = true; do { await RisingEdge(CLK); }while (!data_valid.Cur && !data_fin_in.Cur); if (data_fin_in.Cur) { break; } sample[index, 0] = in_real.Cur; sample[index, 1] = in_imag.Cur; index++; data_req.Next = false; await RisingEdge(CLK); } if (index < 16) { break; } index = 0; ////////////////////////////////////////////////////////////////////////// /// Computation - 1D Complex DFT In-Place DIF Computation Algorithm //// ////////////////////////////////////////////////////////////////////////// //Size of FFT, N = 2**M uint N, M, len; float theta; float[,] W = new float[7, 2]; float w_real, w_imag, w_rec_real, w_rec_imag, w_temp; //Initialize M = 4; N = 16; len = N / 2; theta = (float)(8.0 * Math.Atan(1.0) / N); Log.WriteLine(); Log.WriteLine("Computing..."); //Calculate the W-values recursively w_real = (float)Math.Cos(theta); w_imag = (float)-Math.Sin(theta); w_rec_real = 1; w_rec_imag = 0; index = 0; while (index < len - 1) { w_temp = w_rec_real * w_real - w_rec_imag * w_imag; w_rec_imag = w_rec_real * w_imag + w_rec_imag * w_real; w_rec_real = w_temp; W[index, 0] = w_rec_real; W[index, 1] = w_rec_imag; index++; } float tmp_real, tmp_imag, tmp_real2, tmp_imag2; uint stage, i, j, index2, windex, incr; //Begin Computation stage = 0; len = N; incr = 1; while (stage < M) { len = len / 2; //First Iteration : With No Multiplies i = 0; while (i < N) { index = i; index2 = index + len; tmp_real = sample[index, 0] + sample[index2, 0]; tmp_imag = sample[index, 1] + sample[index2, 1]; sample[index2, 0] = sample[index, 0] - sample[index2, 0]; sample[index2, 1] = sample[index, 1] - sample[index2, 1]; sample[index, 0] = tmp_real; sample[index, 1] = tmp_imag; i = i + 2 * len; } //Remaining Iterations: Use Stored W j = 1; windex = incr - 1; while (j < len) // This loop executes N/2 times at first stage, .. once at last stage. { i = j; while (i < N) { index = i; index2 = index + len; tmp_real = sample[index, 0] + sample[index2, 0]; tmp_imag = sample[index, 1] + sample[index2, 1]; tmp_real2 = sample[index, 0] - sample[index2, 0]; tmp_imag2 = sample[index, 1] - sample[index2, 1]; sample[index2, 0] = tmp_real2 * W[windex, 0] - tmp_imag2 * W[windex, 1]; sample[index2, 1] = tmp_real2 * W[windex, 1] + tmp_imag2 * W[windex, 0]; sample[index, 0] = tmp_real; sample[index, 1] = tmp_imag; i = i + 2 * len; } windex = windex + incr; j++; } stage++; incr = 2 * incr; } ////////////////////////////////////////////////////////////////////////// //Writing out the normalized transform values in bit reversed order Unsigned bits_i, bits_index; bits_index = Unsigned.FromULong(0, 4); bits_i = Unsigned.FromULong(0, 4); i = 0; Log.WriteLine("Writing the transform values..."); while (i < 16) { bits_i = Unsigned.FromULong(i, 4); bits_index[3] = bits_i[0]; bits_index[2] = bits_i[1]; bits_index[1] = bits_i[2]; bits_index[0] = bits_i[3]; index = (uint)bits_index; out_real.Next = sample[index, 0]; out_imag.Next = sample[index, 1]; data_ready.Next = true; do { await RisingEdge(CLK); }while (!data_ack.Cur); data_ready.Next = false; i++; await RisingEdge(CLK); } index = 0; Log.WriteLine("Done..."); } while (!data_fin_in.Cur); data_fin_out.Next = true; DesignContext.ExitProcess(); }
private void HandleStelemFixAFixI(XILSInstr xilsi) { FixedArrayRef far = (FixedArrayRef)xilsi.StaticOperand; Array array = far.ArrayObj; long[] indices = far.Indices; var preds = RemapPreds(xilsi.Preds); MemoryMappedStorage mms = GetDataLayout(array); ArrayMemoryLayout layout = mms.Layout as ArrayMemoryLayout; if (layout.ElementsPerWord > 1) { throw new NotImplementedException("Multiple elements per word not yet implemented"); } MemoryRegion region = mms.Region; IMarshalInfo minfo = region.MarshalInfo; TypeDescriptor elemType = layout.ElementLayout.LayoutedType; TypeDescriptor rawElemType = TypeDescriptor.GetTypeOf( StdLogicVector._0s((long)layout.ElementLayout.SizeInBits)); if (!elemType.Equals(rawElemType)) { Emit(_iset.Convert().CreateStk(1, elemType, rawElemType)); } uint concatSize = layout.WordsPerElement * minfo.WordSize; TypeDescriptor concatType = TypeDescriptor.GetTypeOf( StdLogicVector._0s(concatSize)); if (!concatType.Equals(rawElemType)) { Emit(_iset.Convert().CreateStk(1, rawElemType, concatType)); } TypeDescriptor rawWordType = minfo.GetRawWordType(); int shiftSize = MathExt.CeilLog2(concatSize); if (layout.WordsPerElement > 1) { Emit(_iset.Dup().CreateStk(1, concatType, concatType, concatType)); } Unsigned shift = Unsigned.FromULong(minfo.WordSize, shiftSize); TypeDescriptor shiftType = TypeDescriptor.GetTypeOf(shift); TypeDescriptor dwType = minfo.GetRawWordType(); for (uint i = 0; i < layout.WordsPerElement; i++) { Unsigned addr = ComputeConstAddress(array, indices, i); Emit(_iset.LdConst(addr) .CreateStk(0, TypeDescriptor.GetTypeOf(addr))); Emit(_iset.WrMem(region) .CreateStk(preds, 2, dwType, TypeDescriptor.GetTypeOf(addr))); if (i < layout.WordsPerElement - 1) { Emit(_iset.LdConst(shift).CreateStk(0, shiftType)); Emit(_iset.RShift().CreateStk(2, concatType, shiftType, concatType)); if (i + 1 < layout.WordsPerElement - 1) { Emit(_iset.Dup().CreateStk(1, concatType, concatType, concatType)); } } } }
private void HandleStelemFixA(XILSInstr xilsi) { Array array = (Array)xilsi.StaticOperand; var preds = RemapPreds(xilsi.Preds); MemoryMappedStorage mms = GetDataLayout(array); ArrayMemoryLayout layout = mms.Layout as ArrayMemoryLayout; if (layout.ElementsPerWord > 1) { throw new NotImplementedException("Multiple elements per word not yet implemented"); } MemoryRegion region = mms.Region; IMarshalInfo minfo = region.MarshalInfo; EmitIndexComputation(array); TypeDescriptor indexType = TypeDescriptor.GetTypeOf(mms.BaseAddress); TypeDescriptor elemType = layout.ElementLayout.LayoutedType; Emit(_iset.Swap().CreateStk(2, elemType, indexType, indexType, elemType)); TypeDescriptor rawElemType = TypeDescriptor.GetTypeOf( StdLogicVector._0s((long)layout.ElementLayout.SizeInBits)); if (!elemType.Equals(rawElemType)) { Emit(_iset.Convert().CreateStk(1, elemType, rawElemType)); } uint concatSize = layout.WordsPerElement * minfo.WordSize; TypeDescriptor concatType = TypeDescriptor.GetTypeOf( StdLogicVector._0s(concatSize)); if (!concatType.Equals(rawElemType)) { Emit(_iset.Convert().CreateStk(1, rawElemType, concatType)); } TypeDescriptor rawWordType = minfo.GetRawWordType(); int shiftSize = MathExt.CeilLog2(concatSize); if (layout.WordsPerElement > 1) { Emit(_iset.Swap().CreateStk(2, indexType, rawElemType, rawElemType, indexType)); Emit(_iset.Dup().CreateStk(1, indexType, indexType, indexType)); Emit(_iset.Dig(2).CreateStk(3, concatType, indexType, indexType, indexType, indexType, concatType)); Emit(_iset.Dup().CreateStk(1, concatType, concatType, concatType)); } for (uint i = 0; i < layout.WordsPerElement; i++) { Emit(_iset.Convert().CreateStk(1, concatType, rawWordType)); if (i < layout.WordsPerElement - 1) { Emit(_iset.Dig(2).CreateStk(3, indexType, concatType, rawWordType, concatType, rawWordType, indexType)); } else { Emit(_iset.Swap().CreateStk(2, indexType, rawWordType, rawWordType, indexType)); } Emit(_iset.WrMem(region).CreateStk(preds, 2, rawWordType, indexType)); if (i < layout.WordsPerElement - 1) { Unsigned shift = Unsigned.FromULong(minfo.WordSize, shiftSize); TypeDescriptor shiftType = TypeDescriptor.GetTypeOf(shift); Emit(_iset.LdConst(shift).CreateStk(0, shiftType)); Emit(_iset.RShift().CreateStk(2, concatType, shiftType, concatType)); Emit(_iset.Swap().CreateStk(2, indexType, concatType, concatType, indexType)); if (minfo.UseStrongPow2Alignment) { if (i + 1 < layout.WordsPerElement) { Emit(_iset.Dup().CreateStk(1, indexType, indexType, indexType)); } Unsigned inc = Unsigned.FromULong(i + 1, region.AddressWidth); Emit(_iset.Or().CreateStk(2, indexType, indexType, indexType)); if (i + 1 < layout.WordsPerElement) { Emit(_iset.Dig(2).CreateStk(1, concatType, indexType, indexType, indexType, indexType, concatType)); Emit(_iset.Swap().CreateStk(2, concatType, indexType, indexType, concatType)); } } else { Unsigned inc = Unsigned.FromULong(1, region.AddressWidth); Emit(_iset.LdConst(inc).CreateStk(0, indexType)); Emit(_iset.Add().CreateStk(2, indexType, indexType, indexType)); Emit(_iset.Swap().CreateStk(2, concatType, indexType, indexType, concatType)); } } } }
private void HandleLdelemFixA(XILSInstr xilsi) { Array array = (Array)xilsi.StaticOperand; var preds = RemapPreds(xilsi.Preds); MemoryMappedStorage mms = GetDataLayout(array); ArrayMemoryLayout layout = mms.Layout as ArrayMemoryLayout; if (layout.ElementsPerWord > 1) { throw new NotImplementedException("Multiple elements per word not yet implemented"); } MemoryRegion region = mms.Region; IMarshalInfo minfo = region.MarshalInfo; EmitIndexComputation(array); TypeDescriptor indexType = TypeDescriptor.GetTypeOf(mms.BaseAddress); TypeDescriptor dwType = minfo.GetRawWordType(); if (layout.WordsPerElement > 1) { Emit(_iset.Dup().CreateStk(1, indexType, indexType, indexType)); } for (uint i = 0; i < layout.WordsPerElement; i++) { Emit(_iset.RdMem(region).CreateStk(preds, 1, indexType, dwType)); if (i < layout.WordsPerElement - 1) { Emit(_iset.Swap().CreateStk(2, indexType, dwType, dwType, indexType)); if (minfo.UseStrongPow2Alignment) { if (i + 1 < layout.WordsPerElement - 1) { Emit(_iset.Dup().CreateStk(1, indexType, indexType, indexType)); } Unsigned inc = Unsigned.FromULong(i + 1, region.AddressWidth); Emit(_iset.LdConst(inc).CreateStk(0, indexType)); Emit(_iset.Or().CreateStk(2, indexType, indexType, indexType)); } else { Unsigned inc = Unsigned.FromULong(1, region.AddressWidth); Emit(_iset.LdConst(inc).CreateStk(0, indexType)); Emit(_iset.Add().CreateStk(2, indexType, indexType, indexType)); if (i + 1 < layout.WordsPerElement - 1) { Emit(_iset.Dup().CreateStk(1, indexType, indexType, indexType)); } } } } uint concatTypeSize = minfo.WordSize * layout.WordsPerElement; TypeDescriptor concatType = TypeDescriptor.GetTypeOf( StdLogicVector._0s(concatTypeSize)); if (layout.WordsPerElement > 1) { TypeDescriptor[] stackTypes = new TypeDescriptor[layout.WordsPerElement + 1]; for (uint i = 0; i < layout.WordsPerElement; i++) { stackTypes[i] = dwType; } stackTypes[layout.WordsPerElement] = concatType; Emit(_iset.Concat().CreateStk((int)layout.WordsPerElement, stackTypes)); } TypeDescriptor elemTypeRaw = TypeDescriptor.GetTypeOf( StdLogicVector._0s((int)layout.ElementLayout.SizeInBits)); if (concatTypeSize != layout.ElementLayout.SizeInBits) { Emit(_iset.Convert().CreateStk(1, concatType, elemTypeRaw)); } TypeDescriptor elemType = layout.ElementLayout.LayoutedType; Emit(_iset.Convert().CreateStk(1, elemTypeRaw, elemType)); }
private void EmitIndexComputation(Array array) { MemoryMappedStorage mms = GetDataLayout(array); ArrayMemoryLayout layout = mms.Layout as ArrayMemoryLayout; if (layout.ElementsPerWord > 1) { throw new NotImplementedException("Multiple elements per word not yet implemented"); } MemoryRegion region = mms.Region; IMarshalInfo minfo = region.MarshalInfo; int shiftWidth = MathExt.CeilLog2(mms.Region.AddressWidth); TypeDescriptor indexType = TypeDescriptor.GetTypeOf(mms.BaseAddress); for (int i = 0; i < layout.Strides.Length; i++) { TypeDescriptor orgIndexType; if (i > 0) { orgIndexType = TypeStack.Skip(1).First(); Emit(_iset.Swap().CreateStk(2, orgIndexType, indexType, indexType, orgIndexType)); } else { orgIndexType = TypeStack.Peek(); } ulong stride = layout.Strides[layout.Strides.Length - i - 1]; if (MathExt.IsPow2(stride)) { Emit(_iset.Convert().CreateStk(1, orgIndexType, indexType)); int ishift = MathExt.CeilLog2(stride); if (ishift > 0) { Unsigned shift = Unsigned.FromULong((ulong)ishift, shiftWidth); TypeDescriptor shiftType = TypeDescriptor.GetTypeOf(shift); Emit(_iset.LdConst(shift).CreateStk(0, shiftType)); Emit(_iset.LShift().CreateStk(2, indexType, shiftType, indexType)); } } else { throw new NotImplementedException("Stride ain't a power of 2"); } if (i > 0) { Emit(_iset.Or().CreateStk(2, indexType, indexType, indexType)); } } if (mms.BaseAddress.ULongValue != 0) { Emit(_iset.LdConst(mms.BaseAddress).CreateStk(0, indexType)); if (minfo.UseStrongPow2Alignment) { Emit(_iset.Or().CreateStk(2, indexType, indexType, indexType)); } else { Emit(_iset.Add().CreateStk(2, indexType, indexType, indexType)); } } }
public static string ValueOf(ulong value) { return(ValueOf(Unsigned.FromULong(value, 64))); }
public static string ValueOf(uint value) { return(ValueOf(Unsigned.FromULong(value, 32))); }