16_scheme_1.v 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. `timescale 1ns/1ps
  2. /////////// 16*16 bit multiplier scheme 1///////////
  3. module approx_multiplier_1 (input [15:0]a, input [15:0]b, output reg [31:0]y);
  4. integer i,j,k,l,sum1,sum2,sum,c;
  5. parameter num=6;
  6. reg [num-1:0]m=0; // m and n are the two partial numbers
  7. reg [num-1:0]n=0;
  8. always @(a or b)
  9. begin
  10. if (a[15]==1)
  11. begin
  12. k=15;
  13. end
  14. else if(a[14]==1)
  15. begin
  16. k=14;
  17. end
  18. else if(a[13]==1)
  19. begin
  20. k=13;
  21. end
  22. else if(a[12]==1)
  23. begin
  24. k=12;
  25. end
  26. else if(a[11]==1)
  27. begin
  28. k=11;
  29. end
  30. else if(a[10]==1)
  31. begin
  32. k=10;
  33. end
  34. else if(a[9]==1)
  35. begin
  36. k=9;
  37. end
  38. else if(a[8]==1)
  39. begin
  40. k=8;
  41. end
  42. else if(a[7]==1)
  43. begin
  44. k=7;
  45. end
  46. else if(a[6]==1)
  47. begin
  48. k=6;
  49. end
  50. else if(a[5]==1)
  51. begin
  52. k=5;
  53. end
  54. else if(a[4]==1)
  55. begin
  56. k=4;
  57. end
  58. else if(a[3]==1)
  59. begin
  60. k=3;
  61. end
  62. else if(a[2]==1)
  63. begin
  64. k=2;
  65. end
  66. else if(a[1]==1)
  67. begin
  68. k=1;
  69. end
  70. else
  71. begin
  72. k=0;
  73. end
  74. if (b[15]==1)
  75. begin
  76. l=15;
  77. end
  78. else if(b[14]==1)
  79. begin
  80. l=14;
  81. end
  82. else if(b[13]==1)
  83. begin
  84. l=13;
  85. end
  86. else if(b[12]==1)
  87. begin
  88. l=12;
  89. end
  90. else if(b[11]==1)
  91. begin
  92. l=11;
  93. end
  94. else if(b[10]==1)
  95. begin
  96. l=10;
  97. end
  98. else if(b[9]==1)
  99. begin
  100. l=9;
  101. end
  102. else if(b[8]==1)
  103. begin
  104. l=8;
  105. end
  106. else if(b[7]==1)
  107. begin
  108. l=7;
  109. end
  110. else if(a[6]==1)
  111. begin
  112. l=6;
  113. end
  114. else if(b[5]==1)
  115. begin
  116. l=5;
  117. end
  118. else if(b[4]==1)
  119. begin
  120. l=4;
  121. end
  122. else if(b[3]==1)
  123. begin
  124. l=3;
  125. end
  126. else if(b[2]==1)
  127. begin
  128. l=2;
  129. end
  130. else if(b[1]==1)
  131. begin
  132. l=1;
  133. end
  134. else
  135. begin
  136. l=0;
  137. end
  138. m=0;
  139. n=0;
  140. sum1 = k-num;
  141. sum2 = l-num;
  142. if (sum1<0)
  143. begin
  144. sum1 = -1;
  145. end
  146. if (sum2 <0)
  147. begin
  148. sum2 = -1;
  149. end
  150. sum = sum1+sum2+2;
  151. for (i=0;(i<num);i=i+1)
  152. begin
  153. m[num-1-i]=a[k-i];
  154. end
  155. for (j=0;(j<num);j=j+1)
  156. begin
  157. n[num-1-j]=b[l-j];
  158. end
  159. if (k<=num)
  160. begin
  161. m = a;
  162. end
  163. if (l<=num)
  164. begin
  165. n = b;
  166. end
  167. y=0;
  168. y=m*n;
  169. y=y<<sum;
  170. end
  171. endmodule
  172. /* module tb_approx_multiplier_1_5();
  173. wire [31:0]y;
  174. reg [15:0]a;
  175. reg [15:0]b;
  176. parameter num=6;
  177. approx_multiplier_1 #(num) M1(a,b,y);
  178. initial begin
  179. #0 a=56783; b=6723;
  180. #5 a=16'hFFAA; b=16'h08FA;
  181. #5 a=16'h0892; b=16'h081A;
  182. #5 a=16'h0A12; b=16'h01FB;
  183. #5 a=16'h0392; b=16'hC8CA;
  184. end
  185. initial begin
  186. $display ("time a b y ");
  187. $monitor (" %0d %0d %0d %0d ",$time,a,b,y);
  188. end
  189. endmodule
  190. */