FPGA.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. using FxpConvert.Common;
  2. using NIFPGA.lvbitx;
  3. namespace NIFPGA
  4. {
  5. public sealed class FPGA
  6. {
  7. public string BitstreamMD5 { get; private set; } = string.Empty;
  8. public string BitfileVersion { get; private set; } = string.Empty;
  9. public string SignatureRegister { get; private set; } = string.Empty;
  10. public string BitstreamVersion { get; private set; } = string.Empty;
  11. public string SignatureNames { get; private set; } = string.Empty;
  12. private List<Fifo> fifos= new List<Fifo>();
  13. private IFxpConvert _FXPConvert;
  14. private List<FPGABaseProperty> properties= new List<FPGABaseProperty>();
  15. public IReadOnlyDictionary<String,Fifo> Fifos => fifos.ToDictionary(x=>x.Name);
  16. public IReadOnlyDictionary<String,FPGABaseProperty> Properties=> properties.ToDictionary(x=>x.Name);
  17. private FPGASession session;
  18. public Boolean Error=> session.Error;
  19. public NiFpga_Status LastStatus=> session.LastStatus;
  20. public string Message=>session.Message;
  21. public FPGA(IFxpConvert convert)
  22. {
  23. _FXPConvert = convert;
  24. session = new FPGASession();
  25. Irq = new Irq(session);
  26. }
  27. public T CreateProperty<T>(string name) where T :FPGABaseProperty
  28. {
  29. var pro = properties.FirstOrDefault(x => x.Name == name);
  30. if (pro == null) throw new Exception($"Property {name} not found");
  31. return (T)pro;
  32. }
  33. public T CreateFifo<T>(string name) where T:Fifo
  34. {
  35. var fifo = fifos.FirstOrDefault(x => x.Name == name);
  36. if (fifo == null) throw new Exception($"Fifo {name} not found");
  37. return (T)fifo;
  38. }
  39. public void Parse(string path)
  40. {
  41. var val = lvbitx.Parse.ParseFile(path);
  42. InitProperies(val.VI.Registers);
  43. InitFifo(val.Project.DMA);
  44. }
  45. private void InitFifo(List<DMA> dma)
  46. {
  47. if (dma == null || dma.Count == 0) return;
  48. dma.ForEach(x =>
  49. {
  50. switch(x.Direction)
  51. {
  52. case "TargetToHost"://read
  53. switch (x.Datatype)
  54. {
  55. case Datatype.Boolean:
  56. fifos.Add(new ReadFifo<bool>(session, x));
  57. break;
  58. case Datatype.Int8:
  59. fifos.Add(new ReadFifo<sbyte>(session, x));
  60. break;
  61. case Datatype.Uint8:
  62. fifos.Add(new ReadFifo<byte>(session, x));
  63. break;
  64. case Datatype.Int16:
  65. fifos.Add(new ReadFifo<short>(session, x));
  66. break;
  67. case Datatype.Uint16:
  68. fifos.Add(new ReadFifo<ushort>(session, x));
  69. break;
  70. case Datatype.Int32:
  71. fifos.Add(new ReadFifo<int>(session, x));
  72. break;
  73. case Datatype.Uint32:
  74. fifos.Add(new ReadFifo<uint>(session, x));
  75. break;
  76. case Datatype.Int64:
  77. fifos.Add(new ReadFifo<long>(session, x));
  78. break;
  79. case Datatype.Uint64:
  80. fifos.Add(new ReadFifo<ulong>(session, x));
  81. break;
  82. case Datatype.Float:
  83. fifos.Add(new ReadFifo<float>(session, x));
  84. break;
  85. case Datatype.Double:
  86. fifos.Add(new ReadFifo<double>(session, x));
  87. break;
  88. case Datatype.FXP:
  89. fifos.Add(new ReadFXPFifo(session, x, _FXPConvert));
  90. break;
  91. case Datatype.Array:
  92. break;
  93. }
  94. break;
  95. case "HostToTarget"://write
  96. switch (x.Datatype)
  97. {
  98. case Datatype.Boolean:
  99. fifos.Add(new WriteFifo<bool>(session, x));
  100. break;
  101. case Datatype.Int8:
  102. fifos.Add(new WriteFifo<sbyte>(session, x));
  103. break;
  104. case Datatype.Uint8:
  105. fifos.Add(new WriteFifo<byte>(session, x));
  106. break;
  107. case Datatype.Int16:
  108. fifos.Add(new WriteFifo<short>(session, x));
  109. break;
  110. case Datatype.Uint16:
  111. fifos.Add(new WriteFifo<ushort>(session, x));
  112. break;
  113. case Datatype.Int32:
  114. fifos.Add(new WriteFifo<int>(session, x));
  115. break;
  116. case Datatype.Uint32:
  117. fifos.Add(new WriteFifo<uint>(session, x));
  118. break;
  119. case Datatype.Int64:
  120. fifos.Add(new WriteFifo<long>(session, x));
  121. break;
  122. case Datatype.Uint64:
  123. fifos.Add(new WriteFifo<ulong>(session, x));
  124. break;
  125. case Datatype.Float:
  126. fifos.Add(new WriteFifo<float>(session, x));
  127. break;
  128. case Datatype.Double:
  129. fifos.Add(new WriteFifo<double>(session, x));
  130. break;
  131. case Datatype.FXP:
  132. fifos.Add(new WriteFXPFifo(session, x, _FXPConvert));
  133. break;
  134. case Datatype.Array:
  135. break;
  136. }
  137. break;
  138. }
  139. });
  140. }
  141. public Irq Irq { get; }
  142. public void Open(string bitfile, string resource, NiFpga_OpenAttribute attribute)
  143. {
  144. var lv = lvbitx.Parse.ParseFile(bitfile);
  145. BitfileVersion = lv.BitfileVersion;
  146. SignatureNames = lv.SignatureNames;
  147. SignatureRegister= lv.SignatureRegister;
  148. BitstreamMD5 = lv.BitstreamMD5;
  149. BitstreamVersion = lv.BitstreamVersion;
  150. InitProperies(lv.VI.Registers);
  151. InitFifo(lv.Project.DMA);
  152. session.Open(bitfile, lv.SignatureRegister, resource, attribute);
  153. var indicator = properties.Select(x => x.Register).ToArray();
  154. }
  155. private void InitProperies(List<lvbitx.Register> registers)
  156. {
  157. if (registers == null || registers.Count == 0) return;
  158. foreach (var reg in registers)
  159. {
  160. switch (reg.Datatype)
  161. {
  162. case lvbitx.Datatype.Int16:
  163. if (!reg.Indicator)
  164. {
  165. properties.Add(new FPGAWriteProperty<short>(session, reg));
  166. }
  167. else
  168. {
  169. properties.Add(new FPGAReadProperty<short>(session, reg));
  170. }
  171. break;
  172. case lvbitx.Datatype.Boolean:
  173. if (!reg.Indicator)
  174. {
  175. properties.Add(new FPGAWriteProperty<bool>(session, reg));
  176. }
  177. else
  178. {
  179. properties.Add(new FPGAReadProperty<bool>(session, reg));
  180. }
  181. break;
  182. case lvbitx.Datatype.Int8:
  183. if (!reg.Indicator)
  184. {
  185. properties.Add(new FPGAWriteProperty<sbyte>(session, reg));
  186. }
  187. else
  188. {
  189. properties.Add(new FPGAReadProperty<sbyte>(session, reg));
  190. }
  191. break;
  192. case lvbitx.Datatype.Uint8:
  193. if (!reg.Indicator)
  194. {
  195. properties.Add(new FPGAWriteProperty<byte>(session, reg));
  196. }
  197. else
  198. {
  199. properties.Add(new FPGAReadProperty<byte>(session, reg));
  200. }
  201. break;
  202. case lvbitx.Datatype.Uint16:
  203. if (!reg.Indicator)
  204. {
  205. properties.Add(new FPGAWriteProperty<ushort>(session, reg));
  206. }
  207. else
  208. {
  209. properties.Add(new FPGAReadProperty<ushort>(session, reg));
  210. }
  211. break;
  212. case lvbitx.Datatype.Int32:
  213. if (!reg.Indicator)
  214. {
  215. properties.Add(new FPGAWriteProperty<int>(session, reg));
  216. }
  217. else
  218. {
  219. properties.Add(new FPGAReadProperty<int>(session, reg));
  220. }
  221. break;
  222. case lvbitx.Datatype.Uint32:
  223. if (!reg.Indicator)
  224. {
  225. properties.Add(new FPGAWriteProperty<uint>(session, reg));
  226. }
  227. else
  228. {
  229. properties.Add(new FPGAReadProperty<uint>(session, reg));
  230. }
  231. break;
  232. case lvbitx.Datatype.Int64:
  233. if (!reg.Indicator)
  234. {
  235. properties.Add(new FPGAWriteProperty<long>(session, reg));
  236. }
  237. else
  238. {
  239. properties.Add(new FPGAReadProperty<long>(session, reg));
  240. }
  241. break;
  242. case lvbitx.Datatype.Uint64:
  243. if (!reg.Indicator)
  244. {
  245. properties.Add(new FPGAWriteProperty<ulong>(session, reg));
  246. }
  247. else
  248. {
  249. properties.Add(new FPGAReadProperty<ulong>(session, reg));
  250. }
  251. break;
  252. case lvbitx.Datatype.Float:
  253. if (!reg.Indicator)
  254. {
  255. properties.Add(new FPGAWriteProperty<float>(session, reg));
  256. }
  257. else
  258. {
  259. properties.Add(new FPGAReadProperty<float>(session, reg));
  260. }
  261. break;
  262. case lvbitx.Datatype.Double:
  263. if (!reg.Indicator)
  264. {
  265. properties.Add(new FPGAWriteProperty<double>(session, reg));
  266. }
  267. else
  268. {
  269. properties.Add(new FPGAReadProperty<double>(session, reg));
  270. }
  271. break;
  272. case lvbitx.Datatype.FXP:
  273. if (!reg.Indicator)
  274. {
  275. properties.Add(new FPGAFXPWriteProperty(session, reg, _FXPConvert));
  276. }
  277. else
  278. {
  279. properties.Add(new FPGAFXPReadProperty(session, reg, _FXPConvert));
  280. }
  281. break;
  282. case lvbitx.Datatype.Array:
  283. switch (reg.ArrayValueType)
  284. {
  285. case lvbitx.Datatype.Boolean:
  286. if (!reg.Indicator)
  287. {
  288. properties.Add(new FPGAArrayWriteProperty<bool>(session, reg));
  289. }
  290. else
  291. {
  292. properties.Add(new FPGAArrayReadProperty<bool>(session, reg));
  293. }
  294. break;
  295. case lvbitx.Datatype.Int8:
  296. if (!reg.Indicator)
  297. {
  298. properties.Add(new FPGAArrayWriteProperty<sbyte>(session, reg));
  299. }
  300. else
  301. {
  302. properties.Add(new FPGAArrayReadProperty<sbyte>(session, reg));
  303. }
  304. break;
  305. case lvbitx.Datatype.Uint8:
  306. if (!reg.Indicator)
  307. {
  308. properties.Add(new FPGAArrayWriteProperty<byte>(session, reg));
  309. }
  310. else
  311. {
  312. properties.Add(new FPGAArrayReadProperty<byte>(session, reg));
  313. }
  314. break;
  315. case lvbitx.Datatype.Int16:
  316. if (!reg.Indicator)
  317. {
  318. properties.Add(new FPGAArrayWriteProperty<short>(session, reg));
  319. }
  320. else
  321. {
  322. properties.Add(new FPGAArrayReadProperty<short>(session, reg));
  323. }
  324. break;
  325. case lvbitx.Datatype.Uint16:
  326. if (!reg.Indicator)
  327. {
  328. properties.Add(new FPGAArrayWriteProperty<ushort>(session, reg));
  329. }
  330. else
  331. {
  332. properties.Add(new FPGAArrayReadProperty<ushort>(session, reg));
  333. }
  334. break;
  335. case lvbitx.Datatype.Int32:
  336. if (!reg.Indicator)
  337. {
  338. properties.Add(new FPGAArrayWriteProperty<int>(session, reg));
  339. }
  340. else
  341. {
  342. properties.Add(new FPGAArrayReadProperty<int>(session, reg));
  343. }
  344. break;
  345. case lvbitx.Datatype.Uint32:
  346. if (!reg.Indicator)
  347. {
  348. properties.Add(new FPGAArrayWriteProperty<uint>(session, reg));
  349. }
  350. else
  351. {
  352. properties.Add(new FPGAArrayReadProperty<uint>(session, reg));
  353. }
  354. break;
  355. case lvbitx.Datatype.Int64:
  356. if (!reg.Indicator)
  357. {
  358. properties.Add(new FPGAArrayWriteProperty<long>(session, reg));
  359. }
  360. else
  361. {
  362. properties.Add(new FPGAArrayReadProperty<long>(session, reg));
  363. }
  364. break;
  365. case lvbitx.Datatype.Uint64:
  366. if (!reg.Indicator)
  367. {
  368. properties.Add(new FPGAArrayWriteProperty<ulong>(session, reg));
  369. }
  370. else
  371. {
  372. properties.Add(new FPGAArrayReadProperty<ulong>(session, reg));
  373. }
  374. break;
  375. case lvbitx.Datatype.Float:
  376. if (!reg.Indicator)
  377. {
  378. properties.Add(new FPGAArrayWriteProperty<float>(session, reg));
  379. }
  380. else
  381. {
  382. properties.Add(new FPGAArrayReadProperty<float>(session, reg));
  383. }
  384. break;
  385. case lvbitx.Datatype.Double:
  386. if (!reg.Indicator)
  387. {
  388. properties.Add(new FPGAArrayWriteProperty<double>(session, reg));
  389. }
  390. else
  391. {
  392. properties.Add(new FPGAArrayReadProperty<double>(session, reg));
  393. }
  394. break;
  395. case lvbitx.Datatype.FXP:
  396. if (!reg.Indicator)
  397. {
  398. properties.Add(new FPGAArrayFXPWriteProperty(session, reg,_FXPConvert));
  399. }
  400. else
  401. {
  402. properties.Add(new FPGAArrayFXPReadProperty(session, reg,_FXPConvert));
  403. }
  404. break;
  405. case lvbitx.Datatype.Array:
  406. break;
  407. }
  408. break;
  409. }
  410. }
  411. }
  412. public void Close()=>session.Close();
  413. public void Run()
  414. {
  415. session.CheckResult(session.GetDelegate<Interop.NiFpgaDll_Run>()(session.Session, Interop.NiFpga_RunAttribute.NiFpga_RunAttribute_None));
  416. }
  417. public void Abort()
  418. {
  419. session.CheckResult(session.GetDelegate<Interop.NiFpgaDll_Abort>()(session.Session));
  420. }
  421. public void Reset()
  422. {
  423. session.CheckResult(session.GetDelegate<Interop.NiFpgaDll_Reset>()(session.Session));
  424. }
  425. public void Download()
  426. {
  427. session.CheckResult(session.GetDelegate<Interop.NiFpgaDll_Download>()(session.Session));
  428. }
  429. public void FpgaFinalize()
  430. {
  431. session.CheckResult(session.GetDelegate<Interop.NiFpgaDll_Finalize>()());
  432. }
  433. }
  434. }