WriteFifo.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.Tracing;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using FxpConvert.Common;
  9. namespace NIFPGA
  10. {
  11. public class WriteFifo<T>:Fifo
  12. where T : unmanaged
  13. {
  14. internal WriteFifo(FPGASession session, uint fifosession) : base(session, fifosession)
  15. {
  16. }
  17. public void Write(T[] value,uint timeout,ref uint emptyElementsRemaining)
  18. {
  19. if (value.Length == 0) return;
  20. switch(value[0])
  21. {
  22. case bool:
  23. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoBool>()(_Session.Session, FifoSession,ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  24. break;
  25. case byte:
  26. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoU8>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  27. break;
  28. case sbyte:
  29. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoI8>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  30. break;
  31. case short:
  32. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoI16>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  33. break;
  34. case ushort:
  35. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoU16>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  36. break;
  37. case int:
  38. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoI32>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  39. break;
  40. case uint:
  41. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoU32>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  42. break;
  43. case long:
  44. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoI64>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  45. break;
  46. case ulong:
  47. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoU64>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  48. break;
  49. case float:
  50. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoSgl>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  51. break;
  52. case double:
  53. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoDbl>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  54. break;
  55. }
  56. }
  57. }
  58. public class WriteFXPFifo : WriteFifo<ulong>
  59. {
  60. private IFxpConvert _Convert;
  61. private NiFpga_FxpTypeInfo _TypeInfo;
  62. internal WriteFXPFifo(FPGASession session, uint fifosession, NiFpga_FxpTypeInfo typeInfo,IFxpConvert convert) : base(session, fifosession)
  63. {
  64. if(convert == null) throw new ArgumentNullException(nameof(convert));
  65. _Convert = convert;
  66. _TypeInfo = typeInfo;
  67. }
  68. public void Write(ref float value, uint timeout, uint length, ref uint emptyElementsRemaining)
  69. {
  70. if (length == 0) return;
  71. ulong[] temp= new ulong[length];
  72. _Convert.FloatConvertToDxp(ref value, _TypeInfo, ref temp[0], (uint)length);
  73. base.Write(temp, timeout, ref emptyElementsRemaining);
  74. }
  75. public void Write(ref double value, uint timeout, uint length, ref uint emptyElementsRemaining)
  76. {
  77. if (length == 0) return;
  78. ulong[] temp = new ulong[length];
  79. _Convert.DoubleConvertToDxp(ref value, _TypeInfo, ref temp[0], (uint)length);
  80. base.Write(temp, timeout, ref emptyElementsRemaining);
  81. }
  82. }
  83. }