64_scheme_2.v 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. `timescale 1ns/1ps
  2. module approx_multiplier_1 (input [63:0]a, input [63:0]b, output reg [127:0]y);
  3. integer i,j,k,l,sum1,sum2,sum,num;
  4. reg [9:0]m; // m and n are the two partial numbers
  5. reg [9:0]n;
  6. always @(a or b)
  7. begin
  8. if (a[63] || a[62] || b[63] || b[62] )
  9. begin
  10. num=10;
  11. end
  12. else if (a[61] || a[60] || a[59] || a[58] || b[61] || b[60] || b[59] || b[58])
  13. begin
  14. num=9;
  15. end
  16. else if (a[57] || a[56] || a[55] || b[57] || b[56] || b[55])
  17. begin
  18. num=8;
  19. end
  20. else
  21. begin
  22. num=7;
  23. end
  24. if (a[63]==1)
  25. begin
  26. k=63;
  27. end
  28. else if(a[62]==1)
  29. begin
  30. k=62;
  31. end
  32. else if(a[61]==1)
  33. begin
  34. k=61;
  35. end
  36. else if(a[60]==1)
  37. begin
  38. k=60;
  39. end
  40. else if(a[59]==1)
  41. begin
  42. k=59;
  43. end
  44. else if(a[58]==1)
  45. begin
  46. k=58;
  47. end
  48. else if (a[57]==1)
  49. begin
  50. k=57;
  51. end
  52. else if(a[56]==1)
  53. begin
  54. k=56;
  55. end
  56. else if(a[55]==1)
  57. begin
  58. k=55;
  59. end
  60. else if(a[54]==1)
  61. begin
  62. k=54;
  63. end
  64. else if(a[53]==1)
  65. begin
  66. k=53;
  67. end
  68. else if(a[52]==1)
  69. begin
  70. k=52;
  71. end
  72. else if(a[51]==1)
  73. begin
  74. k=51;
  75. end
  76. else if(a[50]==1)
  77. begin
  78. k=50;
  79. end
  80. else if(a[49]==1)
  81. begin
  82. k=49;
  83. end
  84. else if(a[48]==1)
  85. begin
  86. k=48;
  87. end
  88. else if(a[47]==1)
  89. begin
  90. k=47;
  91. end
  92. else if(a[46]==1)
  93. begin
  94. k=46;
  95. end
  96. else if(a[45]==1)
  97. begin
  98. k=45;
  99. end
  100. else if(a[44]==1)
  101. begin
  102. k=44;
  103. end
  104. else if(a[43]==1)
  105. begin
  106. k=43;
  107. end
  108. else if(a[42]==1)
  109. begin
  110. k=42;
  111. end
  112. else if(a[41]==1)
  113. begin
  114. k=41;
  115. end
  116. else if(a[40]==1)
  117. begin
  118. k=40;
  119. end
  120. else if(a[39]==1)
  121. begin
  122. k=39;
  123. end
  124. else if(a[38]==1)
  125. begin
  126. k=38;
  127. end
  128. else if(a[37]==1)
  129. begin
  130. k=37;
  131. end
  132. else if(a[36]==1)
  133. begin
  134. k=36;
  135. end
  136. else if(a[35]==1)
  137. begin
  138. k=35;
  139. end
  140. else if(a[34]==1)
  141. begin
  142. k=34;
  143. end
  144. else if(a[33]==1)
  145. begin
  146. k=33;
  147. end
  148. else if (a[32]==1)
  149. begin
  150. k=32;
  151. end
  152. else if(a[31]==1)
  153. begin
  154. k=31;
  155. end
  156. else if(a[30]==1)
  157. begin
  158. k=30;
  159. end
  160. else if(a[29]==1)
  161. begin
  162. k=29;
  163. end
  164. else if(a[28]==1)
  165. begin
  166. k=28;
  167. end
  168. else if(a[27]==1)
  169. begin
  170. k=27;
  171. end
  172. else if(a[26]==1)
  173. begin
  174. k=26;
  175. end
  176. else if (a[25]==1)
  177. begin
  178. k=25;
  179. end
  180. else if(a[24]==1)
  181. begin
  182. k=24;
  183. end
  184. else if(a[23]==1)
  185. begin
  186. k=23;
  187. end
  188. else if(a[22]==1)
  189. begin
  190. k=22;
  191. end
  192. else if(a[21]==1)
  193. begin
  194. k=21;
  195. end
  196. else if(a[20]==1)
  197. begin
  198. k=20;
  199. end
  200. else if(a[19]==1)
  201. begin
  202. k=19;
  203. end
  204. else if(a[18]==1)
  205. begin
  206. k=18;
  207. end
  208. else if(a[17]==1)
  209. begin
  210. k=17;
  211. end
  212. else if(a[16]==1)
  213. begin
  214. k=16;
  215. end
  216. else if(a[15]==1)
  217. begin
  218. k=15;
  219. end
  220. else if(a[14]==1)
  221. begin
  222. k=14;
  223. end
  224. else if(a[13]==1)
  225. begin
  226. k=13;
  227. end
  228. else if(a[12]==1)
  229. begin
  230. k=12;
  231. end
  232. else if(a[11]==1)
  233. begin
  234. k=11;
  235. end
  236. else if(a[10]==1)
  237. begin
  238. k=10;
  239. end
  240. else if(a[9]==1)
  241. begin
  242. k=9;
  243. end
  244. else if(a[8]==1)
  245. begin
  246. k=8;
  247. end
  248. else if(a[7]==1)
  249. begin
  250. k=7;
  251. end
  252. else if(a[6]==1)
  253. begin
  254. k=6;
  255. end
  256. else if(a[5]==1)
  257. begin
  258. k=5;
  259. end
  260. else if(a[4]==1)
  261. begin
  262. k=4;
  263. end
  264. else if(a[3]==1)
  265. begin
  266. k=3;
  267. end
  268. else if(a[2]==1)
  269. begin
  270. k=2;
  271. end
  272. else if(a[1]==1)
  273. begin
  274. k=1;
  275. end
  276. else
  277. begin
  278. k=0;
  279. end
  280. if (b[63]==1)
  281. begin
  282. l=63;
  283. end
  284. else if(b[62]==1)
  285. begin
  286. l=62;
  287. end
  288. else if(b[61]==1)
  289. begin
  290. l=61;
  291. end
  292. else if(b[60]==1)
  293. begin
  294. l=60;
  295. end
  296. else if(b[59]==1)
  297. begin
  298. l=59;
  299. end
  300. else if(b[58]==1)
  301. begin
  302. l=58;
  303. end
  304. else if (b[57]==1)
  305. begin
  306. l=57;
  307. end
  308. else if(b[56]==1)
  309. begin
  310. l=56;
  311. end
  312. else if(b[55]==1)
  313. begin
  314. l=55;
  315. end
  316. else if(b[54]==1)
  317. begin
  318. l=54;
  319. end
  320. else if(b[53]==1)
  321. begin
  322. l=53;
  323. end
  324. else if(b[52]==1)
  325. begin
  326. l=52;
  327. end
  328. else if(b[51]==1)
  329. begin
  330. l=51;
  331. end
  332. else if(b[50]==1)
  333. begin
  334. l=50;
  335. end
  336. else if(b[49]==1)
  337. begin
  338. l=49;
  339. end
  340. else if(b[48]==1)
  341. begin
  342. l=48;
  343. end
  344. else if(b[47]==1)
  345. begin
  346. l=47;
  347. end
  348. else if(b[46]==1)
  349. begin
  350. l=46;
  351. end
  352. else if(b[45]==1)
  353. begin
  354. l=45;
  355. end
  356. else if(b[44]==1)
  357. begin
  358. l=44;
  359. end
  360. else if(b[43]==1)
  361. begin
  362. l=43;
  363. end
  364. else if(b[42]==1)
  365. begin
  366. l=42;
  367. end
  368. else if(b[41]==1)
  369. begin
  370. l=41;
  371. end
  372. else if(b[40]==1)
  373. begin
  374. l=40;
  375. end
  376. else if(b[39]==1)
  377. begin
  378. l=39;
  379. end
  380. else if(b[38]==1)
  381. begin
  382. l=38;
  383. end
  384. else if(b[37]==1)
  385. begin
  386. l=37;
  387. end
  388. else if(b[36]==1)
  389. begin
  390. l=36;
  391. end
  392. else if(b[35]==1)
  393. begin
  394. l=35;
  395. end
  396. else if(b[34]==1)
  397. begin
  398. l=34;
  399. end
  400. else if(b[33]==1)
  401. begin
  402. l=33;
  403. end
  404. else if (b[32]==1)
  405. begin
  406. l=32;
  407. end
  408. else if(b[31]==1)
  409. begin
  410. l=31;
  411. end
  412. else if(b[30]==1)
  413. begin
  414. l=30;
  415. end
  416. else if(b[29]==1)
  417. begin
  418. l=29;
  419. end
  420. else if(b[28]==1)
  421. begin
  422. l=28;
  423. end
  424. else if(b[27]==1)
  425. begin
  426. l=27;
  427. end
  428. else if(b[26]==1)
  429. begin
  430. l=26;
  431. end
  432. else if (b[25]==1)
  433. begin
  434. l=25;
  435. end
  436. else if(b[24]==1)
  437. begin
  438. l=24;
  439. end
  440. else if(b[23]==1)
  441. begin
  442. l=23;
  443. end
  444. else if(b[22]==1)
  445. begin
  446. l=22;
  447. end
  448. else if(b[21]==1)
  449. begin
  450. l=21;
  451. end
  452. else if(b[20]==1)
  453. begin
  454. l=20;
  455. end
  456. else if(b[19]==1)
  457. begin
  458. l=19;
  459. end
  460. else if(b[18]==1)
  461. begin
  462. l=18;
  463. end
  464. else if(b[17]==1)
  465. begin
  466. l=17;
  467. end
  468. else if(b[16]==1)
  469. begin
  470. l=16;
  471. end
  472. else if(b[15]==1)
  473. begin
  474. l=15;
  475. end
  476. else if(b[14]==1)
  477. begin
  478. l=14;
  479. end
  480. else if(b[13]==1)
  481. begin
  482. l=13;
  483. end
  484. else if(b[12]==1)
  485. begin
  486. l=12;
  487. end
  488. else if(b[11]==1)
  489. begin
  490. l=11;
  491. end
  492. else if(b[10]==1)
  493. begin
  494. l=10;
  495. end
  496. else if(b[9]==1)
  497. begin
  498. l=9;
  499. end
  500. else if(b[8]==1)
  501. begin
  502. l=8;
  503. end
  504. else if(b[7]==1)
  505. begin
  506. l=7;
  507. end
  508. else if(b[6]==1)
  509. begin
  510. l=6;
  511. end
  512. else if(b[5]==1)
  513. begin
  514. l=5;
  515. end
  516. else if(b[4]==1)
  517. begin
  518. l=4;
  519. end
  520. else if(b[3]==1)
  521. begin
  522. l=3;
  523. end
  524. else if(b[2]==1)
  525. begin
  526. l=2;
  527. end
  528. else if(b[1]==1)
  529. begin
  530. l=1;
  531. end
  532. else
  533. begin
  534. l=0;
  535. end
  536. m=0;
  537. n=0;
  538. sum1 = k-num;
  539. sum2 = l-num;
  540. if (sum1<0)
  541. begin
  542. sum1 = -1;
  543. end
  544. if (sum2 <0)
  545. begin
  546. sum2 = -1;
  547. end
  548. sum = sum1+sum2+2; // number of zeros which will be appended at the end of multiplication
  549. if (num==10)
  550. begin
  551. for (i=0;i<10;i=i+1)
  552. begin
  553. m[10-1-i]=a[k-i];
  554. n[10-1-i]=b[l-i];
  555. end
  556. end
  557. if (num==9)
  558. begin
  559. for (i=0;i<9;i=i+1)
  560. begin
  561. m[9-1-i]=a[k-i];
  562. n[9-1-i]=b[l-i];
  563. end
  564. end
  565. if (num==8)
  566. begin
  567. for (i=0;i<8;i=i+1)
  568. begin
  569. m[8-1-i]=a[k-i];
  570. n[8-1-i]=b[l-i];
  571. end
  572. end
  573. else
  574. begin
  575. for (i=0;i<7;i=i+1)
  576. begin
  577. m[7-1-i]=a[k-i];
  578. n[7-1-i]=b[l-i];
  579. end
  580. end
  581. if (k<=num)
  582. begin
  583. m = a;
  584. end
  585. if (l<=num)
  586. begin
  587. n = b;
  588. end
  589. y=0;
  590. y=m*n;
  591. y=y<<sum; // appending of zeros at the end of multiplication
  592. end
  593. endmodule
  594. /* module tb_approx_multiplier_1();
  595. wire [127:0]y;
  596. reg [63:0]a;
  597. reg [63:0]b;
  598. parameter num=7;
  599. approx_multiplier_1 #(num) M1(a,b,y);
  600. initial begin
  601. #0 a=56783567; b=672323;
  602. #5 a=4012869; b=35343233;
  603. #5 a=5345664; b=655677;
  604. #5 a=1213313; b=987652;
  605. #5 a=94384884; b=3344333;
  606. end
  607. initial begin
  608. $display ("time a b y ");
  609. $monitor (" %0d %0d %0d %0d ",$time,a,b,y);
  610. end
  611. endmodule */