FPGA.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: FPGA.cpp
  4. * Description: FPGA
  5. *
  6. * IO implementation for constrained platforms where
  7. * inputs are contained in a header files and output is
  8. * only stdout.
  9. *
  10. * $Date: 20. June 2019
  11. * $Revision: V1.0.0
  12. *
  13. * Target Processor: Cortex-M cores
  14. * -------------------------------------------------------------------- */
  15. /*
  16. * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved.
  17. *
  18. * SPDX-License-Identifier: Apache-2.0
  19. *
  20. * Licensed under the Apache License, Version 2.0 (the License); you may
  21. * not use this file except in compliance with the License.
  22. * You may obtain a copy of the License at
  23. *
  24. * www.apache.org/licenses/LICENSE-2.0
  25. *
  26. * Unless required by applicable law or agreed to in writing, software
  27. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  28. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  29. * See the License for the specific language governing permissions and
  30. * limitations under the License.
  31. */
  32. #include "Test.h"
  33. #include <string>
  34. #include <cstddef>
  35. #include "FPGA.h"
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include "Generators.h"
  39. namespace Client
  40. {
  41. struct offsetOrGen
  42. {
  43. int kind;
  44. unsigned long offset;
  45. Testing::param_t *data;
  46. Testing::nbSamples_t nbInputSamples;
  47. Testing::nbSamples_t nbOutputSamples;
  48. int dimensions;
  49. };
  50. FPGA::FPGA(const char *testDesc,const char *patterns)
  51. {
  52. this->m_testDesc=testDesc;
  53. this->m_patterns=patterns;
  54. this->currentDesc=testDesc;
  55. this->path=new std::vector<std::string>();
  56. this->patternOffsets=new std::vector<unsigned long>();
  57. this->patternSizes=new std::vector<unsigned long>();
  58. this->parameterOffsets=new std::vector<struct offsetOrGen>();
  59. this->parameterSizes=new std::vector<unsigned long>();
  60. this->outputNames=new std::vector<std::string>();
  61. }
  62. void FPGA::DeleteParams()
  63. {
  64. for (std::vector<struct offsetOrGen>::iterator it = this->parameterOffsets->begin() ; it != this->parameterOffsets->end(); ++it)
  65. {
  66. if (it->kind==1)
  67. {
  68. if (it->data)
  69. {
  70. free(it->data);
  71. it->data = NULL;
  72. }
  73. }
  74. }
  75. }
  76. FPGA::~FPGA()
  77. {
  78. delete(this->path);
  79. delete(this->patternOffsets);
  80. delete(this->patternSizes);
  81. this->DeleteParams();
  82. delete(this->parameterOffsets);
  83. delete(this->parameterSizes);
  84. delete(this->outputNames);
  85. }
  86. /** Read word 32 from C array
  87. */
  88. void FPGA::read32(unsigned long *r)
  89. {
  90. unsigned char a,b,c,d;
  91. unsigned long v;
  92. a = *this->currentDesc++;
  93. b = *this->currentDesc++;
  94. c = *this->currentDesc++;
  95. d = *this->currentDesc++;
  96. //printf("%d %d %d %d\n",a,b,c,d);
  97. v = a | (b << 8) | (c << 16) | (d << 24);
  98. *r = v;
  99. }
  100. /** Read null terminated C string C array
  101. */
  102. void FPGA::readStr(char *str)
  103. {
  104. char *p = str;
  105. while(*this->currentDesc != 0)
  106. {
  107. *p++ = *this->currentDesc++;
  108. }
  109. *p++ = 0;
  110. this->currentDesc++;
  111. }
  112. void FPGA::readChar(char *c)
  113. {
  114. *c = *this->currentDesc;
  115. this->currentDesc++;
  116. }
  117. /** Get output path from output ID
  118. */
  119. std::string FPGA::getOutputPath(Testing::outputID_t id)
  120. {
  121. char fmt[256];
  122. std::string tmp;
  123. tmp += this->testDir;
  124. sprintf(fmt,"/%s_%ld.txt",(*this->outputNames)[id].c_str(),this->currentId);
  125. tmp += std::string(fmt);
  126. //printf("%s\n",tmp.c_str());
  127. return(tmp);
  128. }
  129. /** Read the number of parameters for all the tests in a suite
  130. Used for benchmarking. Same functions executed with
  131. different initializations controlled by the parameters.
  132. */
  133. Testing::nbParameters_t FPGA::ReadNbParameters()
  134. {
  135. unsigned long nb;
  136. this->read32(&nb);
  137. return(nb);
  138. }
  139. void FPGA::ReadTestIdentification()
  140. {
  141. char tmp[255];
  142. unsigned long kind;
  143. unsigned long theId;
  144. char hasPath;
  145. char hasParamID;
  146. Testing::PatternID_t paramID;
  147. //printf("Read ident\n");
  148. this->read32(&kind);
  149. this->read32(&theId);
  150. this->readChar(&hasParamID);
  151. this->m_hasParam=false;
  152. if (hasParamID == 'y')
  153. {
  154. this->m_hasParam=true;
  155. this->read32(&paramID);
  156. this->currentParam=paramID;
  157. }
  158. this->readChar(&hasPath);
  159. if (hasPath == 'y')
  160. {
  161. this->readStr(tmp);
  162. //printf("-->%s\n",tmp);
  163. currentPath.assign(tmp);
  164. }
  165. this->currentKind=kind;
  166. this->currentId=theId;
  167. switch(kind)
  168. {
  169. case 1:
  170. printf("S: t \n");
  171. break;
  172. case 2:
  173. printf("S: s %ld\n",this->currentId);
  174. break;
  175. case 3:
  176. printf("S: g %ld\n",this->currentId);
  177. break;
  178. default:
  179. printf("S: u\n");
  180. }
  181. //printf("End read ident\n\n");
  182. }
  183. void FPGA::recomputeTestDir()
  184. {
  185. this->testDir = ".";
  186. int start = 1;
  187. std::vector<std::string>::const_iterator iter;
  188. for (iter = this->path->begin(); iter != this->path->end(); ++iter)
  189. {
  190. if (start)
  191. {
  192. this->testDir = *iter;
  193. start =0;
  194. }
  195. else
  196. {
  197. if (!(*iter).empty())
  198. {
  199. this->testDir += "/" + *iter;
  200. }
  201. }
  202. }
  203. }
  204. void FPGA::ReadIdentification()
  205. {
  206. this->ReadTestIdentification();
  207. this->path->push_back(currentPath);
  208. this->recomputeTestDir();
  209. }
  210. /** There is only stdout available for "FPGA".
  211. So status output and data output are interleaved.
  212. Status is starting with "S: "
  213. */
  214. void FPGA::DispStatus(Testing::TestStatus status
  215. ,Testing::errorID_t error
  216. ,unsigned long lineNb
  217. ,Testing::cycles_t cycles)
  218. {
  219. if (status == Testing::kTestFailed)
  220. {
  221. printf("S: %ld %ld %ld 0 N\n",this->currentId,error,lineNb);
  222. }
  223. else
  224. {
  225. #ifdef EXTBENCH
  226. printf("S: %ld 0 0 t Y\n",this->currentId);
  227. #else
  228. printf("S: %ld 0 0 %u Y\n",this->currentId, cycles);
  229. #endif
  230. }
  231. }
  232. void FPGA::EndGroup()
  233. {
  234. printf("S: p\n");
  235. this->path->pop_back();
  236. }
  237. /** Read pattern list
  238. Different from semihosting.
  239. We read offset and sizes for the patterns
  240. rather than file names.
  241. */
  242. void FPGA::ReadPatternList()
  243. {
  244. unsigned long offset,nb;
  245. unsigned long nbPatterns;
  246. this->read32(&nbPatterns);
  247. this->patternOffsets->clear();
  248. this->patternSizes->clear();
  249. std::string tmpstr;
  250. for(int i=0;i<nbPatterns;i++)
  251. {
  252. this->read32(&offset);
  253. this->read32(&nb);
  254. this->patternOffsets->push_back(offset);
  255. this->patternSizes->push_back(nb);
  256. }
  257. }
  258. /** Read parameters list
  259. Different from semihosting.
  260. We read offset and sizes for the parameters
  261. rather than file names.
  262. */
  263. void FPGA::ReadParameterList(Testing::nbParameters_t nbParams)
  264. {
  265. unsigned long offset,nb;
  266. unsigned long nbValues;
  267. char paramKind;
  268. this->read32(&nbValues);
  269. this->DeleteParams();
  270. this->parameterOffsets->clear();
  271. this->parameterSizes->clear();
  272. std::string tmpstr;
  273. for(int i=0;i<nbValues;i++)
  274. {
  275. this->readChar(&paramKind);
  276. struct offsetOrGen gen;
  277. if (paramKind == 'p')
  278. {
  279. gen.kind=0;
  280. this->read32(&offset);
  281. this->read32(&nb);
  282. gen.offset=offset;
  283. gen.kind=0;
  284. gen.nbInputSamples=nb;
  285. gen.dimensions = nbParams;
  286. }
  287. else
  288. {
  289. unsigned long kind,nbInputSamples,nbOutputSamples,dimensions,len,sample;
  290. Testing::param_t *p,*current;
  291. size_t length;
  292. // Generator kind
  293. this->read32(&kind);
  294. this->read32(&nbInputSamples);
  295. this->read32(&nbOutputSamples);
  296. this->read32(&dimensions);
  297. p=(Testing::param_t*)malloc(sizeof(Testing::param_t)*(nbInputSamples));
  298. current=p;
  299. for(int i=0;i < nbInputSamples; i ++)
  300. {
  301. this->read32(&sample);
  302. *current++ = (Testing::param_t)sample;
  303. }
  304. gen.kind=1;
  305. gen.data=p;
  306. gen.nbInputSamples = nbInputSamples;
  307. gen.nbOutputSamples = nbOutputSamples;
  308. gen.dimensions = dimensions;
  309. }
  310. this->parameterOffsets->push_back(gen);
  311. this->parameterSizes->push_back(nb);
  312. }
  313. }
  314. void FPGA::ReadOutputList()
  315. {
  316. char tmp[256];
  317. unsigned long nbOutputs;
  318. this->read32(&nbOutputs);
  319. this->outputNames->clear();
  320. std::string tmpstr;
  321. for(int i=0;i<nbOutputs;i++)
  322. {
  323. this->readStr(tmp);
  324. tmpstr.assign(tmp);
  325. this->outputNames->push_back(tmpstr);
  326. }
  327. }
  328. unsigned long FPGA::getPatternOffset(Testing::PatternID_t id)
  329. {
  330. return((*this->patternOffsets)[id]);
  331. }
  332. Testing::nbSamples_t FPGA::GetPatternSize(Testing::PatternID_t id)
  333. {
  334. return((Testing::nbSamples_t)((*this->patternSizes)[id]));
  335. }
  336. unsigned long FPGA::getParameterOffset(Testing::PatternID_t id)
  337. {
  338. return((*this->parameterOffsets)[id].offset);
  339. }
  340. struct offsetOrGen FPGA::getParameterDesc(Testing::PatternID_t id)
  341. {
  342. return((*this->parameterOffsets)[id]);
  343. }
  344. void FPGA::DumpParams(std::vector<Testing::param_t>& params)
  345. {
  346. bool begin=true;
  347. printf("b ");
  348. for(std::vector<Testing::param_t>::iterator it = params.begin(); it != params.end(); ++it)
  349. {
  350. if (!begin)
  351. {
  352. printf(",");
  353. }
  354. printf("%d",*it);
  355. begin=false;
  356. }
  357. printf("\n");
  358. }
  359. Testing::param_t* FPGA::ImportParams(Testing::PatternID_t id,Testing::nbParameterEntries_t &nbEntries,Testing::ParameterKind &paramKind)
  360. {
  361. nbEntries=0;
  362. unsigned long offset,i;
  363. Testing::param_t *p;
  364. uint32_t val;
  365. Testing::nbSamples_t len;
  366. struct offsetOrGen gen = this->getParameterDesc(id);
  367. if (gen.kind == 0)
  368. {
  369. offset=gen.offset;
  370. paramKind=Testing::kStaticBuffer;
  371. nbEntries = gen.nbInputSamples / gen.dimensions;
  372. const char *patternStart = this->m_patterns + offset;
  373. return((Testing::param_t*)patternStart);
  374. }
  375. else
  376. {
  377. Testing::param_t* result;
  378. // Output samples is number of parameter line
  379. len=gen.nbOutputSamples * gen.dimensions;
  380. paramKind=Testing::kDynamicBuffer;
  381. result=(Testing::param_t*)malloc(len*sizeof(Testing::param_t));
  382. switch(gen.dimensions)
  383. {
  384. case 1:
  385. generate1(result,gen.data,nbEntries);
  386. break;
  387. case 2:
  388. generate2(result,gen.data,nbEntries);
  389. break;
  390. case 3:
  391. generate3(result,gen.data,nbEntries);
  392. break;
  393. case 4:
  394. generate4(result,gen.data,nbEntries);
  395. break;
  396. default:
  397. generate1(result,gen.data,nbEntries);
  398. break;
  399. }
  400. return(result);
  401. }
  402. }
  403. bool FPGA::hasParam()
  404. {
  405. return(this->m_hasParam);
  406. }
  407. Testing::PatternID_t FPGA::getParamID()
  408. {
  409. return(this->currentParam);
  410. }
  411. void FPGA::ImportPattern_f64(Testing::PatternID_t id,char* p,Testing::nbSamples_t nb)
  412. {
  413. unsigned long offset,i;
  414. offset=this->getPatternOffset(id);
  415. const char *patternStart = this->m_patterns + offset;
  416. const float64_t *src = (const float64_t*)patternStart;
  417. float64_t *dst = (float64_t*)p;
  418. for(i=0; i < nb; i++)
  419. {
  420. *dst++ = *src++;
  421. }
  422. }
  423. void FPGA::ImportPattern_f32(Testing::PatternID_t id,char* p,Testing::nbSamples_t nb)
  424. {
  425. unsigned long offset,i;
  426. offset=this->getPatternOffset(id);
  427. const char *patternStart = this->m_patterns + offset;
  428. const float32_t *src = (const float32_t*)patternStart;
  429. float32_t *dst = (float32_t*)p;
  430. for(i=0; i < nb; i++)
  431. {
  432. *dst++ = *src++;
  433. }
  434. }
  435. void FPGA::ImportPattern_q31(Testing::PatternID_t id,char* p,Testing::nbSamples_t nb)
  436. {
  437. unsigned long offset,i;
  438. offset=this->getPatternOffset(id);
  439. const char *patternStart = this->m_patterns + offset;
  440. const q31_t *src = (const q31_t*)patternStart;
  441. q31_t *dst = (q31_t*)p;
  442. for(i=0; i < nb; i++)
  443. {
  444. *dst++ = *src++;
  445. }
  446. }
  447. void FPGA::ImportPattern_q15(Testing::PatternID_t id,char* p,Testing::nbSamples_t nb)
  448. {
  449. unsigned long offset,i;
  450. offset=this->getPatternOffset(id);
  451. const char *patternStart = this->m_patterns + offset;
  452. const q15_t *src = (const q15_t*)patternStart;
  453. q15_t *dst = (q15_t*)p;
  454. for(i=0; i < nb; i++)
  455. {
  456. *dst++ = *src++;
  457. }
  458. }
  459. void FPGA::ImportPattern_q7(Testing::PatternID_t id,char* p,Testing::nbSamples_t nb)
  460. {
  461. unsigned long offset,i;
  462. offset=this->getPatternOffset(id);
  463. const char *patternStart = this->m_patterns + offset;
  464. const q7_t *src = (const q7_t*)patternStart;
  465. q7_t *dst = (q7_t*)p;
  466. for(i=0; i < nb; i++)
  467. {
  468. *dst++ = *src++;
  469. }
  470. }
  471. void FPGA::ImportPattern_u32(Testing::PatternID_t id,char* p,Testing::nbSamples_t nb)
  472. {
  473. unsigned long offset,i;
  474. offset=this->getPatternOffset(id);
  475. const char *patternStart = this->m_patterns + offset;
  476. const uint32_t *src = (const uint32_t*)patternStart;
  477. uint32_t *dst = (uint32_t*)p;
  478. for(i=0; i < nb; i++)
  479. {
  480. *dst++ = *src++;
  481. }
  482. }
  483. void FPGA::ImportPattern_u16(Testing::PatternID_t id,char* p,Testing::nbSamples_t nb)
  484. {
  485. unsigned long offset,i;
  486. offset=this->getPatternOffset(id);
  487. const char *patternStart = this->m_patterns + offset;
  488. const uint16_t *src = (const uint16_t*)patternStart;
  489. uint16_t *dst = (uint16_t*)p;
  490. for(i=0; i < nb; i++)
  491. {
  492. *dst++ = *src++;
  493. }
  494. }
  495. void FPGA::ImportPattern_u8(Testing::PatternID_t id,char* p,Testing::nbSamples_t nb)
  496. {
  497. unsigned long offset,i;
  498. offset=this->getPatternOffset(id);
  499. const char *patternStart = this->m_patterns + offset;
  500. const uint8_t *src = (const uint8_t*)patternStart;
  501. uint8_t *dst = (uint8_t*)p;
  502. for(i=0; i < nb; i++)
  503. {
  504. *dst++ = *src++;
  505. }
  506. }
  507. /** Dump patterns.
  508. There is only stdout available for "FPGA".
  509. So status output and data output are interleaved.
  510. Data is starting with "D: "
  511. */
  512. void FPGA::DumpPattern_f64(Testing::outputID_t id,Testing::nbSamples_t nb, float64_t* data)
  513. {
  514. std::string fileName = this->getOutputPath(id);
  515. printf("D: %s\n",fileName.c_str());
  516. Testing::nbSamples_t i=0;
  517. uint64_t t;
  518. float64_t v;
  519. for(i=0; i < nb; i++)
  520. {
  521. v = data[i];
  522. t = TOINT64(v);
  523. printf("D: 0x%016llx\n",t);
  524. }
  525. printf("D: END\n");
  526. }
  527. void FPGA::DumpPattern_f32(Testing::outputID_t id,Testing::nbSamples_t nb, float32_t* data)
  528. {
  529. std::string fileName = this->getOutputPath(id);
  530. printf("D: %s\n",fileName.c_str());
  531. Testing::nbSamples_t i=0;
  532. uint32_t t;
  533. float32_t v;
  534. for(i=0; i < nb; i++)
  535. {
  536. v = data[i];
  537. t = TOINT32(v);
  538. printf("D: 0x%08x\n",t);
  539. }
  540. printf("D: END\n");
  541. }
  542. void FPGA::DumpPattern_q31(Testing::outputID_t id,Testing::nbSamples_t nb, q31_t* data)
  543. {
  544. std::string fileName = this->getOutputPath(id);
  545. printf("D: %s\n",fileName.c_str());
  546. Testing::nbSamples_t i=0;
  547. uint32_t t;
  548. q31_t v;
  549. for(i=0; i < nb; i++)
  550. {
  551. v = data[i];
  552. t = (uint32_t)v;
  553. printf("D: 0x%08x\n",t);
  554. }
  555. printf("D: END\n");
  556. }
  557. void FPGA::DumpPattern_q15(Testing::outputID_t id,Testing::nbSamples_t nb, q15_t* data)
  558. {
  559. std::string fileName = this->getOutputPath(id);
  560. printf("D: %s\n",fileName.c_str());
  561. Testing::nbSamples_t i=0;
  562. uint32_t t;
  563. q15_t v;
  564. for(i=0; i < nb; i++)
  565. {
  566. v = data[i];
  567. t = (uint32_t)v;
  568. printf("D: 0x%08x\n",t);
  569. }
  570. printf("D: END\n");
  571. }
  572. void FPGA::DumpPattern_q7(Testing::outputID_t id,Testing::nbSamples_t nb, q7_t* data)
  573. {
  574. std::string fileName = this->getOutputPath(id);
  575. printf("D: %s\n",fileName.c_str());
  576. Testing::nbSamples_t i=0;
  577. uint32_t t;
  578. q7_t v;
  579. for(i=0; i < nb; i++)
  580. {
  581. v = data[i];
  582. t = (uint32_t)v;
  583. printf("D: 0x%08x\n",t);
  584. }
  585. printf("D: END\n");
  586. }
  587. void FPGA::DumpPattern_u32(Testing::outputID_t id,Testing::nbSamples_t nb, uint32_t* data)
  588. {
  589. std::string fileName = this->getOutputPath(id);
  590. printf("D: %s\n",fileName.c_str());
  591. Testing::nbSamples_t i=0;
  592. uint32_t t;
  593. uint32_t v;
  594. for(i=0; i < nb; i++)
  595. {
  596. v = data[i];
  597. t = (uint32_t)v;
  598. printf("D: 0x%08x\n",t);
  599. }
  600. printf("D: END\n");
  601. }
  602. void FPGA::DumpPattern_u16(Testing::outputID_t id,Testing::nbSamples_t nb, uint16_t* data)
  603. {
  604. std::string fileName = this->getOutputPath(id);
  605. printf("D: %s\n",fileName.c_str());
  606. Testing::nbSamples_t i=0;
  607. uint32_t t;
  608. uint16_t v;
  609. for(i=0; i < nb; i++)
  610. {
  611. v = data[i];
  612. t = (uint32_t)v;
  613. printf("D: 0x%08x\n",t);
  614. }
  615. printf("D: END\n");
  616. }
  617. void FPGA::DumpPattern_u8(Testing::outputID_t id,Testing::nbSamples_t nb, uint8_t* data)
  618. {
  619. std::string fileName = this->getOutputPath(id);
  620. printf("D: %s\n",fileName.c_str());
  621. Testing::nbSamples_t i=0;
  622. uint32_t t;
  623. uint8_t v;
  624. for(i=0; i < nb; i++)
  625. {
  626. v = data[i];
  627. t = (uint32_t)v;
  628. printf("D: 0x%08x\n",t);
  629. }
  630. printf("D: END\n");
  631. }
  632. Testing::testID_t FPGA::CurrentTestID()
  633. {
  634. return(this->currentId);
  635. }
  636. }