Advertisement
Mercedes

Untitled

Oct 28th, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. package body Utils is
  2. procedure Clock(signal C: out Bit; HT,LT: Time) is
  3. begin
  4. loop
  5. C <='1' after LT, '0' after LT + HT;
  6. wait for LT + HT;
  7. end loop;
  8. end;
  9.  
  10. function Convert(N,L:Natural) return Bit_Vector is
  11. variable Temp: Bit_Vector(L - 1 downto 0);
  12. variable Value:Natural:= N;
  13. begin
  14. for i in Temp'Right to Temp'Left loop
  15. Temp(i):=Bit'Val(Value mod 2);
  16. Value:=Value / 2;
  17. end loop;
  18. return Temp;
  19. end;
  20.  
  21. function Convert(B:Bit_Vector) return Natural is
  22. variable Temp: Bit_Vector(B'Length-1 downto 0):=B;
  23. variable Value:Natural:= 0;
  24. begin
  25. for i in Temp'Right to Temp'Left loop
  26. if Temp(i) = '1' then
  27. Value:=Value + (2**i);
  28. end if;
  29. end loop;
  30. return Value;
  31. end;
  32.  
  33. end Utils;
  34.  
  35. entity Multiplicador is
  36.  
  37. port (
  38. CLK: in Bit;
  39. STB: in Bit;
  40. Z: in Bit_vector(3 downto 0);
  41. B: in Bit_vector(3 downto 0);
  42. DONE: out Bit;
  43. RESULT: out Bit_vector(7 downto 0));
  44. begin
  45. assert ((Z'Length <= 4)or(B'Length <= 4))
  46. report "La Entrada A o la B no debe ser mas ancha que 4"
  47. severity Failure;
  48. end Multiplicador;
  49.  
  50. architecture coso of Multiplicador is
  51.  
  52.  
  53. component Latch8
  54. port(D: in Bit_Vector(7 downto 0); Clk: in Bit; Pre: in Bit; Clr: in Bit; Q:out Bit_Vector(7 downto 0));
  55. end component;
  56. component Controller
  57. port (STB, CLK, LSB, Stop: in Bit;
  58. Init, Shift, Add, Done: out Bit);
  59. end component;
  60. component Adder8
  61. port (A, B: in Bit_Vector(7 downto 0); Cin: in Bit; Cout: out Bit; Sum: out Bit_Vector(7 downto 0)); --S1 subtotal que entra al sumador
  62. end component;
  63. component ShiftN
  64. port (CLK: in Bit; CLR: in Bit; LD: in Bit;
  65. SH: in Bit; DIR: in Bit;
  66. D: in Bit_Vector; Q: out Bit_Vector);
  67. end component;
  68.  
  69. signal CLR, LD, SH, DIR, INIT,ADD,INITneg: Bit:='0';
  70. signal D: Bit_Vector(1 to 4);
  71. signal Q: Bit_Vector(7 downto 0);
  72. signal QA: Bit_Vector(7 downto 0);
  73. signal K: bit_vector(7 downto 0);
  74. signal S1: bit_vector(7 downto 0);
  75. signal cout:bit;
  76. signal RES_SUMA: bit_vector(7 downto 0);
  77. signal Stop: bit;
  78. signal STB2: bit;
  79. signal Result2: bit_vector(7 downto 0);
  80. signal CLK2: bit;
  81. begin
  82.  
  83. INITneg <=not INIT;
  84. Stop <= not (Q(7) or Q(6) or Q(5) or Q(4) or Q(3) or Q(2) or Q(1) or Q(0));
  85. SRAA: ShiftN port map (CLK,'0',INIT,SH,'0',Z,Q);
  86. SRB: ShiftN port map (CLK, '0',INIT,SH,'1',B,QA);
  87. CONTROLADOR: Controller port map(STB,CLK,Q(0),Stop,INIT,SH,ADD,DONE);
  88. ACUMULADOR1: Latch8 port map (RES_SUMA,ADD,'1',INITneg,S1);
  89. SUMADOR: Adder8 port map(S1,QA,'0',cout,RES_SUMA);
  90. ACUMULADOR2: Latch8 port map (S1,CLK,'1',INITneg,Result2);
  91. RESULT<=Result2 when Stop = '1';
  92.  
  93. end;
  94.  
  95. -----Testbench------
  96.  
  97. entity Test_Mult is end;
  98. use work.Utils.all;
  99. architecture tuvieja of Test_Mult is
  100. component Multiplicador port (CLK: in bit; STB: in bit; Z: in bit_vector(3 downto 0); b: in bit_vector(3 downto 0); DONE: out bit; RESULT: out bit_vector (7 downto 0));
  101. end component;
  102. signal A, B: Bit_Vector(3 downto 0);
  103. signal Clk, STB, Done: bit;
  104. signal resultau: bit_vector (7 downto 0);
  105.  
  106. begin
  107. Mul: Multiplicador port map (Clk,STB,A,B,Done,resultau);
  108. --Clock: Clk <= not Clk after 20 ns;
  109. Clock: CLK <= not (CLK) after 10 ns;
  110. Roto: process
  111.  
  112. --variable op1,op2: bit_vector(3 downto 0);
  113.  
  114. begin
  115.  
  116. --STB <= '1', '0' after 400000 ns;
  117.  
  118. --A<="0110" ;
  119.  
  120. --B<="1110" ;
  121. --wait for 240 ns;
  122.  
  123.  
  124. --A<="1000";
  125.  
  126. --B<="0001";
  127. --wait for 240 ns;
  128.  
  129. --A<="0101";
  130.  
  131. --B<="1010";wait for 10 ns;
  132.  
  133. for i in 0 to 15 loop
  134.  
  135. A<=Convert (i, A'Length) after 10 ns;
  136.  
  137. --wait for 10 ns;
  138. for j in 0 to 15 loop
  139. B<=Convert (j, B'Length); --after 200ns;
  140.  
  141. -- sacar promedio := tiempoTotal/15*15
  142.  
  143. end loop;
  144. --wait until (DONE='1');
  145. end loop;
  146.  
  147. wait;
  148.  
  149. end process;
  150.  
  151.  
  152. end;
  153.  
  154.  
  155. entity Test_Mult2 is end;
  156. USE std.textio.all;
  157. use work.Utils.all;
  158.  
  159. architecture nisman of Test_Mult2 is
  160.  
  161. component Multiplicador port (CLK: in bit; STB: in bit; Z: in bit_vector(3 downto 0); b: in bit_vector(3 downto 0); DONE: out bit; RESULT: out bit_vector (7 downto 0));
  162. end component;
  163.  
  164. signal A, B: Bit_Vector(3 downto 0);
  165. signal Clk, STB, Done: bit;
  166. signal resultau: bit_vector (7 downto 0);
  167. CONSTANT mensaje1: String:="Ingrese el primer numero";
  168. CONSTANT mensaje2: String:="Ingrese el segundo numero";
  169. CONSTANT mensaje3: String:="Quiere realizar otra operacion, '1' si, '0' no ?";
  170.  
  171. begin
  172. Mul: Multiplicador port map (Clk,STB,A,B,Done,resultau);
  173.  
  174. Clock: CLK <= not (CLK) after 10 ns;
  175.  
  176.  
  177.  
  178.  
  179. pro:process
  180.  
  181. variable linea1: line;
  182. variable linea2:line;
  183. variable linea3:line;
  184. variable lineaA:line;
  185. variable lineaB:line;
  186. variable lineaD:line;
  187. variable numA: integer;
  188. variable numB: integer;
  189.  
  190. variable decision:integer;
  191. variable droopy: side;
  192.  
  193. begin
  194. write (linea1,mensaje1,droopy,mensaje1'length);
  195. write (linea2,mensaje2,droopy,mensaje2'length);
  196. write (linea3,mensaje3,droopy,mensaje3'length);
  197. decision:=1;
  198. wait for 30ns;
  199. while (decision=1) loop
  200.  
  201. writeline(output,linea1);
  202. readline(input,lineaA);
  203. read(lineaA,numA);
  204. A<=Convert (numA, A'Length);
  205. writeline(output,linea2);
  206. readline(input,lineaB);
  207. read(lineaB,numB);
  208. B<=Convert (numB, B'Length);
  209. STB<='1', '0' after 30ns;
  210. wait until (DONE='1');
  211. writeline(output,linea3);
  212. readline(input,lineaD);
  213. read(lineaD, decision);
  214. --agregar write para imprimer enconsola de resultau
  215. end loop;
  216. wait;
  217.  
  218. end process;
  219.  
  220. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement