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.8KB

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