Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. -------------------------------------------------------------
  2. -- authors: Tom Davidson and Peter Bertels
  3. -- date: 2009-10-07
  4. -------------------------------------------------------------
  5. -- Complex Systems Design Methodology
  6. -- Calculator - implementation of a stack
  7. -------------------------------------------------------------
  8.  
  9. library ieee;
  10. use ieee.std_logic_1164.all;
  11. use ieee.numeric_std.all;
  12.  
  13. entity stack is
  14. generic (
  15. width: integer :=8;
  16. depth: integer :=3
  17. );
  18. port (
  19. clk: in std_ulogic;
  20. rst: in std_ulogic;
  21. push: in std_ulogic;
  22. pop: in std_ulogic;
  23. error: out std_ulogic;
  24. data_valid: out std_ulogic;
  25. data_in: in std_ulogic_vector(width-1 downto 0);
  26. data_out: out std_ulogic_vector(width-1 downto 0)
  27. );
  28. end stack;
  29.  
  30.  
  31. architecture behaviour of stack is
  32. type STATE_TYPE is (STATE_WAIT, STATE_POP, STATE_PUSH);
  33. signal state: STATE_TYPE;
  34.  
  35. type mem is array(natural range <>) of std_ulogic_vector(width-1 downto 0);
  36. begin
  37.  
  38. stack: process (clk, rst) is
  39.  
  40. variable memory: mem(0 to depth-1);
  41. variable pointer: integer range 0 to 2**depth-1;
  42.  
  43. begin
  44. if rst = '1' then
  45. data_valid <= '1';
  46. state <= STATE_WAIT;
  47. pointer := 0;
  48. memory(0) := (others => '0');
  49. elsif rising_edge(clk) then
  50. if state = STATE_WAIT and push = '1' then
  51. data_valid <= '0';
  52. state <= STATE_PUSH;
  53. if pointer < 2**depth-1 then
  54. pointer := pointer+1;
  55. memory(pointer) := data_in;
  56. else
  57. error <= '1';
  58. end if;
  59. elsif state = STATE_PUSH and push = '0' then
  60. error <= '0';
  61. data_valid <= '1';
  62. state <= STATE_WAIT;
  63. elsif state = STATE_WAIT and pop = '1' then
  64. data_valid <= '0';
  65. state <= STATE_POP;
  66. if pointer > 0 then
  67. pointer := pointer-1;
  68. else
  69. error <= '1';
  70. end if;
  71. elsif state = STATE_POP and pop = '0' then
  72. error <= '0';
  73. data_valid <= '1';
  74. state <= STATE_WAIT;
  75. else
  76. state <= state;
  77. end if;
  78. data_out <= memory(pointer);
  79. end if;
  80. end process stack;
  81.  
  82. end architecture behaviour;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement