FPGA.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. using NIFPGA.lvbitx;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Threading.Tasks.Dataflow;
  9. using FxpConvert.Common;
  10. namespace NIFPGA
  11. {
  12. public sealed class FPGA
  13. {
  14. public string BitstreamMD5 { get; private set; }
  15. public string BitfileVersion { get;private set; }
  16. public string SignatureRegister { get; private set; }
  17. public string BitstreamVersion { get;private set; }
  18. public string SignatureNames { get; private set; }
  19. private List<Fifo> fifos= new List<Fifo>();
  20. private IFxpConvert _FXPConvert;
  21. private List<FPGABaseProperty> properties= new List<FPGABaseProperty>();
  22. public IReadOnlyDictionary<String,Fifo> Fifos => fifos.ToDictionary(x=>x.Name);
  23. public IReadOnlyDictionary<String,FPGABaseProperty> Properties=> properties.ToDictionary(x=>x.Name);
  24. private FPGASession session;
  25. public Boolean Error=> session.Error;
  26. public NiFpga_Status LastStatus=> session.LastStatus;
  27. public string Message=>session.Message;
  28. public FPGA(IFxpConvert convert)
  29. {
  30. _FXPConvert = convert;
  31. session = new FPGASession();
  32. Irq = new Irq(session);
  33. }
  34. public T CreateProperty<T>(string name) where T :FPGABaseProperty
  35. {
  36. var pro = properties.FirstOrDefault(x => x.Name == name);
  37. if (pro == null) throw new Exception($"Property {name} not found");
  38. return (T)pro;
  39. }
  40. public T CreateFifo<T>(string name) where T:Fifo
  41. {
  42. var fifo = fifos.FirstOrDefault(x => x.Name == name);
  43. if (fifo == null) throw new Exception($"Fifo {name} not found");
  44. return (T)fifo;
  45. }
  46. public void Parse(string path)
  47. {
  48. var val = lvbitx.Parse.ParseFile(path);
  49. InitProperies(val.VI.Registers);
  50. InitFifo(val.Project.DMA);
  51. }
  52. private void InitFifo(List<DMA> dma)
  53. {
  54. if (dma == null || dma.Count == 0) return;
  55. dma.ForEach(x =>
  56. {
  57. switch(x.Direction)
  58. {
  59. case "TargetToHost"://read
  60. switch (x.Datatype)
  61. {
  62. case Datatype.Boolean:
  63. fifos.Add(new ReadFifo<bool>(session, x.ControlSet)
  64. {
  65. Name = x.Name,
  66. });
  67. break;
  68. case Datatype.Int8:
  69. fifos.Add(new ReadFifo<sbyte>(session, x.ControlSet)
  70. {
  71. Name = x.Name,
  72. });
  73. break;
  74. case Datatype.Uint8:
  75. fifos.Add(new ReadFifo<byte>(session, x.ControlSet) { Name = x.Name });
  76. break;
  77. case Datatype.Int16:
  78. fifos.Add(new ReadFifo<short>(session, x.ControlSet) { Name = x.Name });
  79. break;
  80. case Datatype.Uint16:
  81. fifos.Add(new ReadFifo<ushort>(session, x.ControlSet) { Name = x.Name });
  82. break;
  83. case Datatype.Int32:
  84. fifos.Add(new ReadFifo<int>(session, x.ControlSet) { Name = x.Name });
  85. break;
  86. case Datatype.Uint32:
  87. fifos.Add(new ReadFifo<uint>(session, x.ControlSet) { Name = x.Name });
  88. break;
  89. case Datatype.Int64:
  90. fifos.Add(new ReadFifo<long>(session, x.ControlSet) { Name = x.Name });
  91. break;
  92. case Datatype.Uint64:
  93. fifos.Add(new ReadFifo<ulong>(session, x.ControlSet) { Name = x.Name });
  94. break;
  95. case Datatype.Float:
  96. fifos.Add(new ReadFifo<float>(session, x.ControlSet) { Name = x.Name });
  97. break;
  98. case Datatype.Double:
  99. fifos.Add(new ReadFifo<double>(session, x.ControlSet) { Name = x.Name });
  100. break;
  101. case Datatype.FXP:
  102. fifos.Add(new ReadFXPFifo(session, x.ControlSet,x.FxpTypeInfo, _FXPConvert)
  103. {
  104. Name = x.Name,
  105. });
  106. break;
  107. case Datatype.Array:
  108. break;
  109. }
  110. break;
  111. case "HostToTarget"://write
  112. switch (x.Datatype)
  113. {
  114. case Datatype.Boolean:
  115. fifos.Add(new WriteFifo<bool>(session, x.ControlSet) { Name = x.Name });
  116. break;
  117. case Datatype.Int8:
  118. fifos.Add(new WriteFifo<sbyte>(session, x.ControlSet) { Name = x.Name });
  119. break;
  120. case Datatype.Uint8:
  121. fifos.Add(new WriteFifo<byte>(session, x.ControlSet) { Name = x.Name });
  122. break;
  123. case Datatype.Int16:
  124. fifos.Add(new WriteFifo<short>(session, x.ControlSet) { Name = x.Name });
  125. break;
  126. case Datatype.Uint16:
  127. fifos.Add(new WriteFifo<ushort>(session, x.ControlSet) { Name = x.Name });
  128. break;
  129. case Datatype.Int32:
  130. fifos.Add(new WriteFifo<int>(session, x.ControlSet) { Name = x.Name });
  131. break;
  132. case Datatype.Uint32:
  133. fifos.Add(new WriteFifo<uint>(session, x.ControlSet) { Name = x.Name });
  134. break;
  135. case Datatype.Int64:
  136. fifos.Add(new WriteFifo<long>(session, x.ControlSet) { Name = x.Name });
  137. break;
  138. case Datatype.Uint64:
  139. fifos.Add(new WriteFifo<ulong>(session, x.ControlSet) { Name = x.Name });
  140. break;
  141. case Datatype.Float:
  142. fifos.Add(new WriteFifo<float>(session, x.ControlSet) { Name = x.Name });
  143. break;
  144. case Datatype.Double:
  145. fifos.Add(new WriteFifo<double>(session, x.ControlSet) { Name = x.Name });
  146. break;
  147. case Datatype.FXP:
  148. fifos.Add(new WriteFXPFifo(session, x.ControlSet, x.FxpTypeInfo, _FXPConvert) { Name = x.Name });
  149. break;
  150. case Datatype.Array:
  151. break;
  152. }
  153. break;
  154. }
  155. });
  156. }
  157. public Irq Irq { get; }
  158. public void Open(string bitfile, string resource, NiFpga_OpenAttribute attribute)
  159. {
  160. var lv = lvbitx.Parse.ParseFile(bitfile);
  161. BitfileVersion = lv.BitfileVersion;
  162. SignatureNames = lv.SignatureNames;
  163. SignatureRegister= lv.SignatureRegister;
  164. BitstreamMD5 = lv.BitstreamMD5;
  165. BitstreamVersion = lv.BitstreamVersion;
  166. InitProperies(lv.VI.Registers);
  167. InitFifo(lv.Project.DMA);
  168. session.Open(bitfile, lv.SignatureRegister, resource, attribute);
  169. }
  170. private void InitProperies(List<lvbitx.Register> registers)
  171. {
  172. if (registers == null || registers.Count == 0) return;
  173. foreach (var reg in registers)
  174. {
  175. switch (reg.Datatype)
  176. {
  177. case lvbitx.Datatype.Int16:
  178. if (!reg.Indicator)
  179. {
  180. properties.Add(new FPGAWriteProperty<short>(session, reg.Offset)
  181. {
  182. Name = reg.Name,
  183. SizeInBits = reg.SizeInBits,
  184. });
  185. }
  186. else
  187. {
  188. properties.Add(new FPGAReadProperty<short>(session, reg.Offset)
  189. {
  190. Name = reg.Name,
  191. SizeInBits = reg.SizeInBits,
  192. });
  193. }
  194. break;
  195. case lvbitx.Datatype.Boolean:
  196. if (!reg.Indicator)
  197. {
  198. properties.Add(new FPGAWriteProperty<bool>(session, reg.Offset)
  199. {
  200. Name = reg.Name,
  201. SizeInBits = reg.SizeInBits,
  202. });
  203. }
  204. else
  205. {
  206. properties.Add(new FPGAReadProperty<bool>(session, reg.Offset)
  207. {
  208. Name = reg.Name,
  209. SizeInBits = reg.SizeInBits,
  210. });
  211. }
  212. break;
  213. case lvbitx.Datatype.Int8:
  214. if (!reg.Indicator)
  215. {
  216. properties.Add(new FPGAWriteProperty<sbyte>(session, reg.Offset)
  217. {
  218. Name = reg.Name,
  219. SizeInBits = reg.SizeInBits,
  220. });
  221. }
  222. else
  223. {
  224. properties.Add(new FPGAReadProperty<sbyte>(session, reg.Offset)
  225. {
  226. Name = reg.Name,
  227. SizeInBits = reg.SizeInBits,
  228. });
  229. }
  230. break;
  231. case lvbitx.Datatype.Uint8:
  232. if (!reg.Indicator)
  233. {
  234. properties.Add(new FPGAWriteProperty<byte>(session, reg.Offset)
  235. {
  236. Name = reg.Name,
  237. SizeInBits = reg.SizeInBits,
  238. });
  239. }
  240. else
  241. {
  242. properties.Add(new FPGAReadProperty<byte>(session, reg.Offset)
  243. {
  244. Name = reg.Name,
  245. SizeInBits = reg.SizeInBits,
  246. });
  247. }
  248. break;
  249. case lvbitx.Datatype.Uint16:
  250. if (!reg.Indicator)
  251. {
  252. properties.Add(new FPGAWriteProperty<ushort>(session, reg.Offset)
  253. {
  254. Name = reg.Name,
  255. SizeInBits = reg.SizeInBits,
  256. });
  257. }
  258. else
  259. {
  260. properties.Add(new FPGAReadProperty<ushort>(session, reg.Offset)
  261. {
  262. Name = reg.Name,
  263. SizeInBits = reg.SizeInBits,
  264. });
  265. }
  266. break;
  267. case lvbitx.Datatype.Int32:
  268. if (!reg.Indicator)
  269. {
  270. properties.Add(new FPGAWriteProperty<int>(session, reg.Offset)
  271. {
  272. Name = reg.Name,
  273. SizeInBits = reg.SizeInBits,
  274. });
  275. }
  276. else
  277. {
  278. properties.Add(new FPGAReadProperty<int>(session, reg.Offset)
  279. {
  280. Name = reg.Name,
  281. SizeInBits = reg.SizeInBits,
  282. });
  283. }
  284. break;
  285. case lvbitx.Datatype.Uint32:
  286. if (!reg.Indicator)
  287. {
  288. properties.Add(new FPGAWriteProperty<uint>(session, reg.Offset)
  289. {
  290. Name = reg.Name,
  291. SizeInBits = reg.SizeInBits,
  292. });
  293. }
  294. else
  295. {
  296. properties.Add(new FPGAReadProperty<uint>(session, reg.Offset)
  297. {
  298. Name = reg.Name,
  299. SizeInBits = reg.SizeInBits,
  300. });
  301. }
  302. break;
  303. case lvbitx.Datatype.Int64:
  304. if (!reg.Indicator)
  305. {
  306. properties.Add(new FPGAWriteProperty<long>(session, reg.Offset)
  307. {
  308. Name = reg.Name,
  309. SizeInBits = reg.SizeInBits,
  310. });
  311. }
  312. else
  313. {
  314. properties.Add(new FPGAReadProperty<long>(session, reg.Offset)
  315. {
  316. Name = reg.Name,
  317. SizeInBits = reg.SizeInBits,
  318. });
  319. }
  320. break;
  321. case lvbitx.Datatype.Uint64:
  322. if (!reg.Indicator)
  323. {
  324. properties.Add(new FPGAWriteProperty<ulong>(session, reg.Offset)
  325. {
  326. Name = reg.Name,
  327. SizeInBits = reg.SizeInBits,
  328. });
  329. }
  330. else
  331. {
  332. properties.Add(new FPGAReadProperty<ulong>(session, reg.Offset)
  333. {
  334. Name = reg.Name,
  335. SizeInBits = reg.SizeInBits,
  336. });
  337. }
  338. break;
  339. case lvbitx.Datatype.Float:
  340. if (!reg.Indicator)
  341. {
  342. properties.Add(new FPGAWriteProperty<float>(session, reg.Offset)
  343. {
  344. Name = reg.Name,
  345. SizeInBits = reg.SizeInBits,
  346. });
  347. }
  348. else
  349. {
  350. properties.Add(new FPGAReadProperty<float>(session, reg.Offset)
  351. {
  352. Name = reg.Name,
  353. SizeInBits = reg.SizeInBits,
  354. });
  355. }
  356. break;
  357. case lvbitx.Datatype.Double:
  358. if (!reg.Indicator)
  359. {
  360. properties.Add(new FPGAWriteProperty<double>(session, reg.Offset)
  361. {
  362. Name = reg.Name,
  363. SizeInBits = reg.SizeInBits,
  364. });
  365. }
  366. else
  367. {
  368. properties.Add(new FPGAReadProperty<double>(session, reg.Offset)
  369. {
  370. Name = reg.Name,
  371. SizeInBits = reg.SizeInBits,
  372. });
  373. }
  374. break;
  375. case lvbitx.Datatype.FXP:
  376. if (!reg.Indicator)
  377. {
  378. properties.Add(new FPGAFXPWriteProperty(session, reg.Offset, reg.FxpTypeInfo, _FXPConvert)
  379. {
  380. Name = reg.Name,
  381. SizeInBits = reg.SizeInBits,
  382. });
  383. }
  384. else
  385. {
  386. properties.Add(new FPGAFXPReadProperty(session, reg.Offset, reg.FxpTypeInfo, _FXPConvert)
  387. {
  388. Name = reg.Name,
  389. SizeInBits = reg.SizeInBits,
  390. });
  391. }
  392. break;
  393. case lvbitx.Datatype.Array:
  394. switch (reg.ArrayValueType)
  395. {
  396. case lvbitx.Datatype.Boolean:
  397. if (!reg.Indicator)
  398. {
  399. properties.Add(new FPGAArrayWriteProperty<bool>(session, reg.Offset, reg.Size)
  400. {
  401. Name = reg.Name,
  402. SizeInBits = reg.SizeInBits,
  403. });
  404. }
  405. else
  406. {
  407. properties.Add(new FPGAArrayReadProperty<bool>(session, reg.Offset, reg.Size)
  408. {
  409. Name = reg.Name,
  410. SizeInBits = reg.SizeInBits,
  411. });
  412. }
  413. break;
  414. case lvbitx.Datatype.Int8:
  415. if (!reg.Indicator)
  416. {
  417. properties.Add(new FPGAArrayWriteProperty<sbyte>(session, reg.Offset, reg.Size)
  418. {
  419. Name = reg.Name,
  420. SizeInBits = reg.SizeInBits,
  421. });
  422. }
  423. else
  424. {
  425. properties.Add(new FPGAArrayReadProperty<sbyte>(session, reg.Offset, reg.Size)
  426. {
  427. Name = reg.Name,
  428. SizeInBits = reg.SizeInBits,
  429. });
  430. }
  431. break;
  432. case lvbitx.Datatype.Uint8:
  433. if (!reg.Indicator)
  434. {
  435. properties.Add(new FPGAArrayWriteProperty<byte>(session, reg.Offset, reg.Size)
  436. {
  437. Name = reg.Name,
  438. SizeInBits = reg.SizeInBits,
  439. });
  440. }
  441. else
  442. {
  443. properties.Add(new FPGAArrayReadProperty<byte>(session, reg.Offset, reg.Size)
  444. {
  445. Name = reg.Name,
  446. SizeInBits = reg.SizeInBits,
  447. });
  448. }
  449. break;
  450. case lvbitx.Datatype.Int16:
  451. if (!reg.Indicator)
  452. {
  453. properties.Add(new FPGAArrayWriteProperty<short>(session, reg.Offset, reg.Size)
  454. {
  455. Name = reg.Name,
  456. SizeInBits = reg.SizeInBits,
  457. });
  458. }
  459. else
  460. {
  461. properties.Add(new FPGAArrayReadProperty<short>(session, reg.Offset, reg.Size)
  462. {
  463. Name = reg.Name,
  464. SizeInBits = reg.SizeInBits,
  465. });
  466. }
  467. break;
  468. case lvbitx.Datatype.Uint16:
  469. if (!reg.Indicator)
  470. {
  471. properties.Add(new FPGAArrayWriteProperty<ushort>(session, reg.Offset, reg.Size)
  472. {
  473. Name = reg.Name,
  474. SizeInBits = reg.SizeInBits,
  475. });
  476. }
  477. else
  478. {
  479. properties.Add(new FPGAArrayReadProperty<ushort>(session, reg.Offset, reg.Size)
  480. {
  481. Name = reg.Name,
  482. SizeInBits = reg.SizeInBits,
  483. });
  484. }
  485. break;
  486. case lvbitx.Datatype.Int32:
  487. if (!reg.Indicator)
  488. {
  489. properties.Add(new FPGAArrayWriteProperty<int>(session, reg.Offset, reg.Size)
  490. {
  491. Name = reg.Name,
  492. SizeInBits = reg.SizeInBits,
  493. });
  494. }
  495. else
  496. {
  497. properties.Add(new FPGAArrayReadProperty<int>(session, reg.Offset, reg.Size)
  498. {
  499. Name = reg.Name,
  500. SizeInBits = reg.SizeInBits,
  501. });
  502. }
  503. break;
  504. case lvbitx.Datatype.Uint32:
  505. if (!reg.Indicator)
  506. {
  507. properties.Add(new FPGAArrayWriteProperty<uint>(session, reg.Offset, reg.Size)
  508. {
  509. Name = reg.Name,
  510. SizeInBits = reg.SizeInBits,
  511. });
  512. }
  513. else
  514. {
  515. properties.Add(new FPGAArrayReadProperty<uint>(session, reg.Offset, reg.Size)
  516. {
  517. Name = reg.Name,
  518. SizeInBits = reg.SizeInBits,
  519. });
  520. }
  521. break;
  522. case lvbitx.Datatype.Int64:
  523. if (!reg.Indicator)
  524. {
  525. properties.Add(new FPGAArrayWriteProperty<long>(session, reg.Offset, reg.Size)
  526. {
  527. Name = reg.Name,
  528. SizeInBits = reg.SizeInBits,
  529. });
  530. }
  531. else
  532. {
  533. properties.Add(new FPGAArrayReadProperty<long>(session, reg.Offset, reg.Size)
  534. {
  535. Name = reg.Name,
  536. SizeInBits = reg.SizeInBits,
  537. });
  538. }
  539. break;
  540. case lvbitx.Datatype.Uint64:
  541. if (!reg.Indicator)
  542. {
  543. properties.Add(new FPGAArrayWriteProperty<ulong>(session, reg.Offset, reg.Size)
  544. {
  545. Name = reg.Name,
  546. SizeInBits = reg.SizeInBits,
  547. });
  548. }
  549. else
  550. {
  551. properties.Add(new FPGAArrayReadProperty<ulong>(session, reg.Offset, reg.Size)
  552. {
  553. Name = reg.Name,
  554. SizeInBits = reg.SizeInBits,
  555. });
  556. }
  557. break;
  558. case lvbitx.Datatype.Float:
  559. if (!reg.Indicator)
  560. {
  561. properties.Add(new FPGAArrayWriteProperty<float>(session, reg.Offset, reg.Size)
  562. {
  563. Name = reg.Name,
  564. SizeInBits = reg.SizeInBits,
  565. });
  566. }
  567. else
  568. {
  569. properties.Add(new FPGAArrayReadProperty<float>(session, reg.Offset, reg.Size)
  570. {
  571. Name = reg.Name,
  572. SizeInBits = reg.SizeInBits,
  573. });
  574. }
  575. break;
  576. case lvbitx.Datatype.Double:
  577. if (!reg.Indicator)
  578. {
  579. properties.Add(new FPGAArrayWriteProperty<double>(session, reg.Offset, reg.Size)
  580. {
  581. Name = reg.Name,
  582. SizeInBits = reg.SizeInBits,
  583. });
  584. }
  585. else
  586. {
  587. properties.Add(new FPGAArrayReadProperty<double>(session, reg.Offset, reg.Size)
  588. {
  589. Name = reg.Name,
  590. SizeInBits = reg.SizeInBits,
  591. });
  592. }
  593. break;
  594. case lvbitx.Datatype.FXP:
  595. if (!reg.Indicator)
  596. {
  597. properties.Add(new FPGAArrayFXPWriteProperty(session, reg.Offset, reg.Size, reg.FxpTypeInfo, _FXPConvert)
  598. {
  599. Name = reg.Name,
  600. SizeInBits = reg.SizeInBits,
  601. });
  602. }
  603. else
  604. {
  605. properties.Add(new FPGAArrayFXPReadProperty(session, reg.Offset, reg.Size, reg.FxpTypeInfo, _FXPConvert)
  606. {
  607. Name = reg.Name,
  608. SizeInBits = reg.SizeInBits,
  609. });
  610. }
  611. break;
  612. case lvbitx.Datatype.Array:
  613. break;
  614. }
  615. break;
  616. }
  617. }
  618. }
  619. public void Close()=>session.Close();
  620. public void Run()
  621. {
  622. session.CheckResult(session.GetDelegate<Interop.NiFpgaDll_Run>()(session.Session, Interop.NiFpga_RunAttribute.NiFpga_RunAttribute_None));
  623. }
  624. public void Abort()
  625. {
  626. session.CheckResult(session.GetDelegate<Interop.NiFpgaDll_Abort>()(session.Session));
  627. }
  628. public void Reset()
  629. {
  630. session.CheckResult(session.GetDelegate<Interop.NiFpgaDll_Reset>()(session.Session));
  631. }
  632. public void Download()
  633. {
  634. session.CheckResult(session.GetDelegate<Interop.NiFpgaDll_Download>()(session.Session));
  635. }
  636. }
  637. }