No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Etage4_Memoire_NS.vhd 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ----------------------------------------------------------------------------------
  2. -- Company: INSA-Toulouse
  3. -- Engineer: Paul Faure
  4. --
  5. -- Create Date: 18.04.2021 21:19:41
  6. -- Module Name: Etage4_Memoire_NS - Structural
  7. -- Project Name: Processeur sécurisé
  8. -- Target Devices: Basys 3 ARTIX7
  9. -- Tool Versions: Vivado 2016.4
  10. --
  11. -- Description: Etage 4 du processeur
  12. -- - Gestion de la mémoire
  13. -- - Gestion de la sauvegarde du contexte lors des appels de fonction
  14. --
  15. -- Dependencies:
  16. -- - MemoireDonnees
  17. -- - MemoireAdressesRetour
  18. -- - LC
  19. -- - MUX
  20. ----------------------------------------------------------------------------------
  21. library IEEE;
  22. use IEEE.STD_LOGIC_1164.ALL;
  23. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  24. entity Etage4_Memoire_NS is
  25. Generic ( Nb_bits : Natural; -- Taille d'un mot binaire
  26. Mem_size : Natural; -- Taille de la mémoire de donnees (nombre de mots binaires stockables)
  27. Adresse_mem_size : Natural; -- Nombre de bits pour adresser la mémoire de donnees
  28. Instruction_bus_size : Natural; -- Nombre de bits du bus d'instruction (Taille d'un code instruction)
  29. Bits_Controle_LC : STD_LOGIC_VECTOR; -- Vecteur de bit controlant le Link Controler (cf LC.vhd)
  30. Bits_Controle_MUX_IN : STD_LOGIC_VECTOR; -- Vecteur de bit controlant le multiplexer selectionnant A ou B comme adresse (cf MUX.vhd)
  31. Bits_Controle_MUX_IN_EBP : STD_LOGIC_VECTOR; -- Vecteur de bit controlant le multiplexer selectionnant si on doit ajouter ou non EBP à l'adresse (cf MUX.vhd)
  32. Bits_Controle_MUX_OUT : STD_LOGIC_VECTOR; -- Vecteur de bit controlant le multiplexer de sortie (cf MUX.vhd)
  33. Code_Instruction_CALL : STD_LOGIC_VECTOR; -- Numéro de l'instruction CALL
  34. Code_Instruction_RET : STD_LOGIC_VECTOR); -- Numéro de l'instruction RET
  35. Port ( CLK : in STD_LOGIC; -- Clock
  36. RST : in STD_LOGIC; -- Reset
  37. IN_A : in STD_LOGIC_VECTOR (Nb_bits - 1 downto 0); -- Entrée de l'opérande A
  38. IN_B : in STD_LOGIC_VECTOR (Nb_bits - 1 downto 0); -- Entrée de l'opérande B
  39. IN_Instruction : in STD_LOGIC_VECTOR (Instruction_bus_size - 1 downto 0); -- Entrée de l'instruction
  40. OUT_A : out STD_LOGIC_VECTOR (Nb_bits - 1 downto 0); -- Sortie de l'opérande A
  41. OUT_B : out STD_LOGIC_VECTOR (Nb_bits - 1 downto 0); -- Sortie de l'opérande B
  42. OUT_Instruction : out STD_LOGIC_VECTOR (Instruction_bus_size - 1 downto 0); -- Sortie de l'instruction
  43. OUT_AddrRetour : out STD_LOGIC_VECTOR (Nb_bits - 1 downto 0)); -- Sortie de l'adresse de retour vers l'étage 1
  44. end Etage4_Memoire_NS;
  45. architecture Structural of Etage4_Memoire_NS is
  46. component MemoireDonnees is
  47. Generic (Nb_bits : Natural;
  48. Addr_size : Natural;
  49. Mem_size : Natural);
  50. Port ( Addr : in STD_LOGIC_VECTOR (Addr_size-1 downto 0); -- L'adresse a laquelle il faut agir
  51. RW : in STD_LOGIC; -- Ce qu'il faut faire ('1' -> Read, '0' -> Write)
  52. D_IN : in STD_LOGIC_VECTOR (Nb_bits-1 downto 0); -- Data a ecrire (si RW = 0)
  53. CALL : in STD_LOGIC; -- '1' -> CALL en cours
  54. IN_EBP : in STD_LOGIC_VECTOR (Nb_bits-1 downto 0); -- Valeur d'EBP à stocker en cas de CALL
  55. IN_AddrRet : in STD_LOGIC_VECTOR (Nb_bits-1 downto 0); -- Valeur d'@ de retour à stocker en cas de CALL
  56. RET : in STD_LOGIC; -- '1' -> RET en cours
  57. OUT_EBP : out STD_LOGIC_VECTOR (Nb_bits-1 downto 0) := (others => '0'); -- Valeur d'EBP à renvoyer en cas de RET
  58. OUT_AddrRet : out STD_LOGIC_VECTOR (Nb_bits-1 downto 0) := (others => '0'); -- Valeur d'@ de retour à renvoyer en cas de RET
  59. RST : in STD_LOGIC; -- Reset
  60. CLK : in STD_LOGIC; -- Clock
  61. D_OUT : out STD_LOGIC_VECTOR (Nb_bits-1 downto 0) := (others => '0')); -- Sortie de la mémoire
  62. end component;
  63. component LC is
  64. Generic (Instruction_Vector_Size : Natural;
  65. Command_size : Natural;
  66. Bits_Controle : STD_LOGIC_VECTOR);
  67. Port ( Instruction : in STD_LOGIC_VECTOR (Instruction_Vector_Size - 1 downto 0);
  68. Commande : out STD_LOGIC_VECTOR (Command_size - 1 downto 0));
  69. end component;
  70. component MUX is
  71. Generic (Nb_bits : Natural;
  72. Instruction_Vector_Size : Natural;
  73. Bits_Controle : STD_LOGIC_VECTOR);
  74. Port ( Instruction : in STD_LOGIC_VECTOR (Instruction_Vector_Size - 1 downto 0);
  75. IN1 : in STD_LOGIC_VECTOR (Nb_bits - 1 downto 0);
  76. IN2 : in STD_LOGIC_VECTOR (Nb_bits - 1 downto 0);
  77. OUTPUT : out STD_LOGIC_VECTOR (Nb_bits - 1 downto 0));
  78. end component;
  79. signal EBP : STD_LOGIC_VECTOR (Adresse_mem_size - 1 downto 0) := (others => '0'); -- EBP (offset à ajouter à l'adresse)
  80. signal Last_EBP : STD_LOGIC_VECTOR (Nb_bits - 1 downto 0) := (others => '0'); -- Ancien EBP, valeur récupérée en mémoire lors d'un RET
  81. signal New_EBP : STD_LOGIC_VECTOR (Adresse_mem_size - 1 downto 0) := (others => '0'); -- Nouvelle valeur d'EBP, a stocker lors d'un CALL (Cf fonctionnement MemoireAdressesRetour.vhd)
  82. signal IN_EBP : STD_LOGIC_VECTOR (Nb_bits - 1 downto 0) := (others => '0'); -- EBP à stocker ne mémoire (ajout de 0)
  83. signal Addr_MemoireDonnees : STD_LOGIC_VECTOR (Adresse_mem_size - 1 downto 0) := (others => '0'); -- Adresse entrante dans le composant de mémoire de donnees
  84. signal IN_Addr_MemoireDonnees : STD_LOGIC_VECTOR (Adresse_mem_size - 1 downto 0) := (others => '0'); -- Sortie du mux de choix d'adresse entre A et B
  85. signal Addr_MemoireDonnees_EBP : STD_LOGIC_VECTOR (Adresse_mem_size - 1 downto 0) := (others => '0'); -- Adresse avec EBP ajouté (IN_Addr_MemoireDonnees + BP)
  86. signal Commande_MemoireDonnees : STD_LOGIC_VECTOR (0 downto 0) := "0"; -- Sortie du Link Controler, signal de commande de la mémoire
  87. signal Sortie_MemoireDonnees : STD_LOGIC_VECTOR (Nb_bits - 1 downto 0) := (others => '0'); -- Sortie de la mémoire (a multiplexer)
  88. signal intern_OUT_B : STD_LOGIC_VECTOR (Nb_bits - 1 downto 0) := (others => '0'); -- Signal interne
  89. -- Signaux de contrôle de la mémoire
  90. signal CALL_Aux : STD_LOGIC := '0';
  91. signal RET_Aux : STD_LOGIC := '0';
  92. begin
  93. instance_LC : LC -- Link controleur sur la mémoire de donnees
  94. generic map (Instruction_Vector_Size => Instruction_bus_size,
  95. Command_size => 1,
  96. Bits_Controle => Bits_Controle_LC)
  97. port map ( Instruction => IN_Instruction,
  98. Commande => Commande_MemoireDonnees);
  99. instance_MUX_IN : MUX -- Multiplexeur selectionnant A ou B pour adresse
  100. generic map (Nb_bits => Adresse_mem_size,
  101. Instruction_Vector_Size => Instruction_bus_size,
  102. Bits_Controle => Bits_Controle_MUX_IN)
  103. port map ( Instruction => IN_Instruction,
  104. IN1 => IN_A (Adresse_mem_size - 1 downto 0),
  105. IN2 => IN_B (Adresse_mem_size - 1 downto 0),
  106. OUTPUT => IN_Addr_MemoireDonnees);
  107. instance_MUX_IN_EBP : MUX -- Multiplexeur selectionnant l'adresse plus EBP ou l'adresse de base
  108. generic map (Nb_bits => Adresse_mem_size,
  109. Instruction_Vector_Size => Instruction_bus_size,
  110. Bits_Controle => Bits_Controle_MUX_IN_EBP)
  111. port map ( Instruction => IN_Instruction,
  112. IN1 => IN_Addr_MemoireDonnees,
  113. IN2 => Addr_MemoireDonnees_EBP,
  114. OUTPUT => Addr_MemoireDonnees);
  115. instance_MUX_OUT : MUX -- Multiplexeur selectionnant la sortie de l'étage (sur B)
  116. generic map (Nb_bits => Nb_bits,
  117. Instruction_Vector_Size => Instruction_bus_size,
  118. Bits_Controle => Bits_Controle_MUX_OUT)
  119. port map ( Instruction => IN_Instruction,
  120. IN1 => Sortie_MemoireDonnees,
  121. IN2 => IN_B,
  122. OUTPUT => intern_OUT_B);
  123. instance_MemoireDonnees : MemoireDonnees
  124. generic map (Nb_bits => Nb_bits,
  125. Addr_size => Adresse_mem_size,
  126. Mem_size => Mem_size)
  127. port map (Addr => Addr_MemoireDonnees,
  128. RW => Commande_MemoireDonnees(0),
  129. D_IN => IN_B,
  130. CALL => CALL_Aux,
  131. IN_EBP => IN_EBP,
  132. IN_AddrRet => IN_A,
  133. RET => RET_Aux,
  134. OUT_EBP => Last_EBP,
  135. OUT_AddrRet => OUT_AddrRetour,
  136. RST => RST,
  137. CLK => CLK,
  138. D_OUT => Sortie_MemoireDonnees);
  139. OUT_A <= (others => '0') when RST = '0' else
  140. IN_A;
  141. OUT_B <= (others => '0') when RST = '0' else
  142. intern_OUT_B;
  143. OUT_Instruction <= (others => '0') when RST = '0' else
  144. IN_Instruction;
  145. -- Controle de la gestion des appels de fonctions (ici aussi un LC aurait été disproportionné)
  146. RET_Aux <= '1' when IN_Instruction = Code_Instruction_RET else
  147. '0';
  148. CALL_Aux <= '1' when IN_Instruction = Code_Instruction_CALL else
  149. '0';
  150. process
  151. begin
  152. wait until CLK'event and CLK = '1';
  153. if (IN_Instruction = Code_Instruction_CALL) then
  154. EBP <= New_EBP;
  155. elsif (IN_Instruction = Code_Instruction_RET) then
  156. EBP <= Last_EBP (Adresse_mem_size - 1 downto 0);
  157. elsif (RST = '0') then
  158. EBP <= (others => '0');
  159. end if;
  160. end process;
  161. New_EBP <= EBP + IN_B (Adresse_mem_size - 1 downto 0) + 2;
  162. IN_EBP <= (Nb_bits - 1 downto Adresse_mem_size => '0') & EBP;
  163. Addr_MemoireDonnees_EBP <= IN_Addr_MemoireDonnees + EBP;
  164. end Structural;