32_scheme_1.v 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. `timescale 1ns/1ps
  2. /////////// 32*32 bit multiplier scheme 1///////////
  3. module approx_32 (input [31:0]a, input [31:0]b, output reg [63:0]y);
  4. integer i,j,k,l,sum1,sum2,sum;
  5. parameter num=6;
  6. reg [5:0]m=0; // m and n are the two partial numbers
  7. reg [5:0]n=0;
  8. always @(a or b)
  9. begin
  10. if(a[31]==1)
  11. begin
  12. k=31;
  13. end
  14. else if(a[30]==1)
  15. begin
  16. k=30;
  17. end
  18. else if(a[29]==1)
  19. begin
  20. k=29;
  21. end
  22. else if(a[28]==1)
  23. begin
  24. k=28;
  25. end
  26. else if(a[27]==1)
  27. begin
  28. k=27;
  29. end
  30. else if(a[26]==1)
  31. begin
  32. k=26;
  33. end
  34. else if (a[25]==1)
  35. begin
  36. k=25;
  37. end
  38. else if(a[24]==1)
  39. begin
  40. k=24;
  41. end
  42. else if(a[23]==1)
  43. begin
  44. k=23;
  45. end
  46. else if(a[22]==1)
  47. begin
  48. k=22;
  49. end
  50. else if(a[21]==1)
  51. begin
  52. k=21;
  53. end
  54. else if(a[20]==1)
  55. begin
  56. k=20;
  57. end
  58. else if(a[19]==1)
  59. begin
  60. k=19;
  61. end
  62. else if(a[18]==1)
  63. begin
  64. k=18;
  65. end
  66. else if(a[17]==1)
  67. begin
  68. k=17;
  69. end
  70. else if(a[16]==1)
  71. begin
  72. k=16;
  73. end
  74. else if(a[15]==1)
  75. begin
  76. k=15;
  77. end
  78. else if(a[14]==1)
  79. begin
  80. k=14;
  81. end
  82. else if(a[13]==1)
  83. begin
  84. k=13;
  85. end
  86. else if(a[12]==1)
  87. begin
  88. k=12;
  89. end
  90. else if(a[11]==1)
  91. begin
  92. k=11;
  93. end
  94. else if(a[10]==1)
  95. begin
  96. k=10;
  97. end
  98. else if(a[9]==1)
  99. begin
  100. k=9;
  101. end
  102. else if(a[8]==1)
  103. begin
  104. k=8;
  105. end
  106. else if(a[7]==1)
  107. begin
  108. k=7;
  109. end
  110. else if(a[6]==1)
  111. begin
  112. k=6;
  113. end
  114. else if(a[5]==1)
  115. begin
  116. k=5;
  117. end
  118. else if(a[4]==1)
  119. begin
  120. k=4;
  121. end
  122. else if(a[3]==1)
  123. begin
  124. k=3;
  125. end
  126. else if(a[2]==1)
  127. begin
  128. k=2;
  129. end
  130. else if(a[1]==1)
  131. begin
  132. k=1;
  133. end
  134. else
  135. begin
  136. k=0;
  137. end
  138. if(b[31]==1)
  139. begin
  140. l=31;
  141. end
  142. else if(b[30]==1)
  143. begin
  144. l=30;
  145. end
  146. else if(b[29]==1)
  147. begin
  148. l=29;
  149. end
  150. else if(b[28]==1)
  151. begin
  152. l=28;
  153. end
  154. else if(b[27]==1)
  155. begin
  156. l=27;
  157. end
  158. else if(b[26]==1)
  159. begin
  160. l=26;
  161. end
  162. else if (b[25]==1)
  163. begin
  164. l=25;
  165. end
  166. else if(b[24]==1)
  167. begin
  168. l=24;
  169. end
  170. else if(b[23]==1)
  171. begin
  172. l=23;
  173. end
  174. else if(b[22]==1)
  175. begin
  176. l=22;
  177. end
  178. else if(b[21]==1)
  179. begin
  180. l=21;
  181. end
  182. else if(b[20]==1)
  183. begin
  184. l=20;
  185. end
  186. else if(b[19]==1)
  187. begin
  188. l=19;
  189. end
  190. else if(b[18]==1)
  191. begin
  192. l=18;
  193. end
  194. else if(b[17]==1)
  195. begin
  196. l=17;
  197. end
  198. else if(b[16]==1)
  199. begin
  200. l=16;
  201. end
  202. else if(b[15]==1)
  203. begin
  204. l=15;
  205. end
  206. else if(b[14]==1)
  207. begin
  208. l=14;
  209. end
  210. else if(b[13]==1)
  211. begin
  212. l=13;
  213. end
  214. else if(b[12]==1)
  215. begin
  216. l=12;
  217. end
  218. else if(b[11]==1)
  219. begin
  220. l=11;
  221. end
  222. else if(b[10]==1)
  223. begin
  224. l=10;
  225. end
  226. else if(b[9]==1)
  227. begin
  228. l=9;
  229. end
  230. else if(b[8]==1)
  231. begin
  232. l=8;
  233. end
  234. else if(b[7]==1)
  235. begin
  236. l=7;
  237. end
  238. else if(b[6]==1)
  239. begin
  240. l=6;
  241. end
  242. else if(b[5]==1)
  243. begin
  244. l=5;
  245. end
  246. else if(b[4]==1)
  247. begin
  248. l=4;
  249. end
  250. else if(b[3]==1)
  251. begin
  252. l=3;
  253. end
  254. else if(b[2]==1)
  255. begin
  256. l=2;
  257. end
  258. else if(b[1]==1)
  259. begin
  260. l=1;
  261. end
  262. else
  263. begin
  264. l=0;
  265. end
  266. m=0;
  267. n=0;
  268. sum1 = k-num;
  269. sum2 = l-num;
  270. if (sum1<0)
  271. begin
  272. sum1 = -1;
  273. end
  274. if (sum2 <0)
  275. begin
  276. sum2 = -1;
  277. end
  278. sum = sum1+sum2+2;
  279. for (i=0;(i<num);i=i+1)
  280. begin
  281. m[num-1-i]=a[k-i];
  282. end
  283. for (j=0;(j<num);j=j+1)
  284. begin
  285. n[num-1-j]=b[l-j];
  286. end
  287. if (k<=num)
  288. begin
  289. m = a;
  290. end
  291. if (l<=num)
  292. begin
  293. n = b;
  294. end
  295. y=0;
  296. y=m*n;
  297. y=y<<sum;
  298. end
  299. endmodule
  300. /* module tb_approx_32();
  301. wire [63:0]y;
  302. reg [31:0]a;
  303. reg [31:0]b;
  304. parameter num=6;
  305. approx_32 #(num) M1(a,b,y);
  306. initial begin
  307. #0 a=56783567; b=672323;
  308. #5 a=4012869; b=35343233;
  309. #5 a=5345664; b=655677;
  310. #5 a=1213313; b=987652;
  311. #5 a=94384884; b=3344333;
  312. end
  313. initial begin
  314. $display ("time a b y ");
  315. $monitor (" %0d %0d %0d %0d ",$time,a,b,y);
  316. end
  317. endmodule */