source: PROJECT_CORE_MPI/MPI_HCL/TRUNK/EX4_FSM.vhd.bak @ 96

Last change on this file since 96 was 96, checked in by rolagamo, 10 years ago
File size: 58.9 KB
Line 
1----------------------------------------------------------------------------------
2-- Company:
3-- Engineer: GAMOM
4--
5-- Create Date:    14:44:36 03/07/2012
6-- Design Name:
7-- Module Name:    EX4_FSM - Behavioral
8-- Project Name: MPI_CORE_COMPONENTS
9-- Target Devices: SPARTAN 3E xc3s1200e
10-- Tool versions:
11-- Description: Ce module renferme les fonctions permettant d'initialiser la
12-- bibliothèque matériel MPI
13-- Dependencies:
14--
15-- Revision: 25/juin/2012 au 24/Octobre/2012
16-- Revision 0.03 - File updated
17-- Additional Comments:
18--
19----------------------------------------------------------------------------------
20library IEEE;
21
22use IEEE.STD_LOGIC_1164.ALL;
23
24-- Uncomment the following library declaration if using
25-- arithmetic functions with Signed or Unsigned values
26use IEEE.NUMERIC_STD.ALL;
27Library NocLib;
28use NoCLib.CoreTypes.all;
29use work.packet_type.all;
30
31-- Uncomment the following library declaration if instantiating
32-- any Xilinx primitives in this code.
33--library UNISIM;
34--use UNISIM.VComponents.all;
35
36entity EX4_FSM is
37    Port ( Instruction : in  STD_LOGIC_VECTOR (Word-1 downto 0);
38           Instruction_En : in  STD_LOGIC;
39           clk : in  STD_LOGIC;
40           reset : in  STD_LOGIC;
41           ResultOut : out  STD_LOGIC_VECTOR (Word-1 downto 0):=(others=>'0');
42           Result_En : out  STD_LOGIC:='0';
43           Ready : out STD_LOGIC; --indique que le module est disponible
44           NocSize : out  STD_LOGIC_VECTOR(3 downto 0);
45                             PortId : out  STD_LOGIC_VECTOR(3 downto 0):=(others=>'0');
46           AppRank : out  STD_LOGIC_VECTOR(3 downto 0):=(others=>'0');
47           AppSize : out  STD_LOGIC_VECTOR(3 downto 0):=(others=>'0');
48           IsMain : out  STD_LOGIC;
49           I_fifo_full :in STD_LOGIC; --instruction fifo
50                       I_fifo_wr_en: out STD_LOGIC;--instruction fifo Write enable
51                       I_fifo_data_in :out STD_LOGIC_vector(Word-1 downto 0); --instruction fifo data
52                             Initialized : out STD_LOGIC;
53                          Snd_Ack : IN std_logic;
54                Rec_Rdy : IN std_logic;
55                Rec_Data : IN Typ_PortIO(0 to 3);
56                Snd_data : OUT Typ_PortIO(0 to 3);
57                Snd_Start : buffer std_logic;
58                Rec_Ack : OUT std_logic;       
59                                   dma_wr_grant : in  STD_LOGIC;
60                                   dma_wr_request : out  STD_LOGIC;
61                                dma_rd_grant : in  STD_LOGIC;
62                                dma_rd_request : out  STD_LOGIC;
63                                ram_rd : out std_logic;
64                                ram_wr : out std_logic;
65                                ram_address : out std_logic_vector(ADRLEN-1 downto 0);--accès au stockage
66                                Ram_data_in : out STD_LOGIC_VECTOR (Word-1 downto 0);
67                                Ram_data_out : in STD_LOGIC_VECTOR (Word-1 downto 0);
68                       
69                          AppAck : in  STD_LOGIC;
70           AppReq : in  STD_LOGIC;
71                          port_in_cmd_en : out  STD_LOGIC:='0';
72                          port_in_wr_en : out  STD_LOGIC:='0';
73                          port_in_empty : in  STD_LOGIC;
74           port_in_full : in  STD_LOGIC;
75           port_in_data : out  STD_LOGIC_VECTOR (Word-1 downto 0):=(others=>'-');
76                          port_out_data : in  STD_LOGIC_VECTOR (Word-1 downto 0);
77           port_out_rd_en : out  STD_LOGIC:='0';
78           port_out_data_available : in  STD_LOGIC);
79end EX4_FSM;
80
81architecture Behavioral of EX4_FSM is
82
83COMPONENT SetBit
84        PORT(
85                clk : IN std_logic;
86                reset : IN std_logic;
87                BitNum : IN std_logic_vector(0 to 2);
88                BitVal : IN std_logic;
89                dma_wr_grant : IN std_logic;
90                dma_rd_grant : IN std_logic;
91                Ram_data_in : IN std_logic_vector(7 downto 0);
92                   
93                dma_wr_request : OUT std_logic;
94                dma_rd_request : OUT std_logic;
95                ram_rd : OUT std_logic;
96                ram_wr : OUT std_logic;
97                ram_address : OUT std_logic_vector(15 downto 0);
98                Ram_data_out : OUT std_logic_vector(7 downto 0)
99                );
100        END COMPONENT;
101Type Ar_MPIPort_In is array (positive range <>) of Typ_MPIPort_in;
102Type Ar_MPIPort_out is array (positive range <>) of Typ_MPIPort_out;
103
104        --MAE pour emission de init
105   --type init1_type is (init,NocSize,InitToNoc,NextPort,EndBCast);
106        --      MAE pour affectation des rangs aux différentes librairies MPI
107        type init2_type is (init,GetPortNum,DecodeData,IsPortZero,SeekMain1,SeekMain2,StoreMain,SetMainFlag,ReadNoc,GetMainReq,StoreRank,NewRank,SendRank,RegRank,SendPeerStat,AskPeerStat,GetPeerStat,SendApp,SpawnApp,SpawnLoad,ErrSpawn,EndInit);
108   -- MAE pour réception de init depuis le réseau;
109        type init3_type is  (init,GetNocSize,ReadInitHead,StorePort,EndInit);
110        type typ_send is (s_init,s_head,s_len,s_len2,s_data,s_end);
111        type typ_receiv is (r_wait,r_dlen,r_drop,r_glen,r_data,r_pulse,r_end);
112        type typ_cmd is (cmdstart,cmdpost,cmdpostidle,cmdread,cmdlen,cmdglen,cmddata,cmdend,cmdtimeout);
113  type typ_mem16 is array(natural range <>) of std_logic_vector(15 downto 0);--
114                signal etsnd : typ_send;
115                signal etrec:typ_receiv;   --pour la machine à état de réception
116                signal etcmd :typ_cmd;     --pour la machine à état de commande
117signal IS_state,next_Is_state : natural range 0 to 3:=0;
118
119--      signal stInit1, next_stInit1 : init1_type;
120        signal stInit2, next_stInit2 : init2_type;
121        --signal stInit3, next_stInit3 : init3_type;
122        --signaux pour l'interface avec les ports
123        signal cport_in_wr_en,sport_in_wr_en:std_logic;
124        signal cport_out_rd_en,rport_out_rd_en:std_logic;
125        signal rdy:std_logic:='0'; --signal busy/ready
126        signal tosend,tosend4 :std_logic_vector(Word-1 downto 0);
127        signal PeerStat,PeerStat_i : std_logic_vector(15 downto 0):=(others=>'0'); --état des autres Lib initialisées
128  signal SpawnReq,SpawnReq_Q : std_logic_vector(15 downto 0):=(others=>'0'); --état des autres HT qui ont appelés le Spawn     
129        signal Spawn_grp,Spawn_grp_Q :typ_mem16(0 to 3):=((others=>'0'),(others=>'0'),(others=>'0'),(others=>'0')); --un max de quatre groupes de fils est prévu
130        signal Grp_id,grp_id_i :natural range 0 to 3:=0; --id du groupe des HT fils
131        signal selector : std_logic_vector (2 downto 0); -- pour le MUX des signaux vers le port
132        --signaux pour les états des MAE
133        signal  NocSizeOk,PortCountFlag : std_logic;
134        signal PortNumFlag,PortNumFlag_i,EquFlag,EquFlag_i,MainResp: std_logic:='0';
135        signal StatAsked,RankAsked,RankSent:std_logic;
136        signal  nextr,nextr_i : natural :=0;
137   signal RTS_cmd,RTS_dat,RTS_I,CTR,BCast,Send_Ack,DS_Ack,CM_Ack:std_logic:='0';
138        signal I_Send_Ack,I_Send_Rdy:std_logic:='0';
139        signal Rcv_On,Snd_On,Cmd_On : std_logic:='0'; --status des MAE d'envoie et de réception
140        signal SpawnOn,SpawnInit :std_logic:='0'; --indique que le Spawn est activé
141        signal SpawnNbReq,SpawnNbAck,SpawnNBAck_i : natural range 0 to 15 :=0; --compte le nombre de requêtes et le nombre d'acquitement
142        signal SpawnNbReq_Q : natural range 0 to 15 :=0; --compte le nombre de requêtes et le nombre d'acquitement
143        signal SpawnCmd0,SpawnCmd1, SpawnCmd2 :std_logic_vector(Word-1 downto 0):=(others=>'0');
144        signal SpawnCmd0_Q,SpawnCmd1_Q, SpawnCmd2_Q :std_logic_vector(Word-1 downto 0):=(others=>'0');
145        signal SpawnDest :std_logic_vector(3 downto 0);
146        Signal Ht_Start,Ht_Start_i : natural range 0 to 15:=0;
147        Signal vPeerStat,vPeerStat_i : std_logic_vector(15 downto 0):=(others=>'0');
148        signal IsMain_i,IsMain1,Initialized_i,HCL_init,HCL_Init_i: std_logic:='0';
149        signal DS_RDY ,BCast_Rdy,Send_RDY,CM_RDY:std_logic:='0';
150        signal ExecTime_out:std_logic;
151        --signaux de gestion de la RAM
152        signal Ram_Next_Address : std_logic_vector(ADRLEN-1 downto 0):=std_logic_vector(to_unsigned(Core_BASE_ADR+CORE_Rank2port_BASE,16));
153        signal Ram_NExt_Address_i :std_logic_vector(ADRLEN-1 downto 0):=std_logic_vector(to_unsigned(Core_BASE_ADR+CORE_Rank2port_BASE,16));
154        signal n,n_i:natural range 0 to 15 :=0;
155        signal n0,n0_i:natural range 0 to 7:=0;
156        signal DataToRam : std_logic_vector(Word-1 downto 0) ;
157        signal dma_rd,dma_wr,rd_ok ,wr_ok:std_logic:='0';
158        --Signaux des résultats et de l'état
159  signal PortNum_i,PortNum,MyPort : std_logic_vector(3 downto 0);  --
160        signal NocMax ,NocMax_i: std_logic_vector(3 downto 0):=(others=>'0'); -- Nombre de ports du réseau -1 ?
161        signal MyRank : std_logic_vector(3 downto 0) ; --rang du PE
162        signal AppSize_i,AppSize1:std_logic_vector(3 downto 0) ;--taille de l'application
163        signal Spawn_done,Spawn_done_i:std_logic:='0'; --indique que le Spawn a été effectué
164        signal MainPort,MainPort_i : std_logic_vector(3 downto 0) ; --Port de la lib main
165        signal NextRank,NextRank_i : std_logic_vector(3 downto 0):=(others=>'0'); --utiliser pour gérer les affectations de rangs MPI
166   signal Data_To_Send,Data_to_send_i : Typ_PortIo(0 to 3):=((others=>'0'),(others=>'0'),(others=>'0'),(others=>'0')); -- permet d'empiler les données à envoyer
167        signal RankToPort :Typ_PortIo(0 to 15);--permet d'associer un port réseau à chaque  rang
168        signal CmdReceived : Typ_PortIO(0 to 3):=((others=>'0'),(others=>'0'),(others=>'0'),(others=>'0'));
169        signal DataReceived : Typ_PortIO(0 to 3):=((others=>'0'),(others=>'0'),(others=>'0'),(others=>'0'));
170        signal Datalen,DataLen_i : natural range 0 to 7:=0; --indique la longueur des données
171        --other outputs
172        signal StatAsked_i,RankAsked_i,MainResp_i,RankSent_i,Result_i:std_logic:='0';
173        signal snd_start_i:std_logic:='0';--version interne des signaux de sortie
174   --signal BCast_Rdy_i : std_logic;
175        signal cdlen,dcount : natural range 0 to 255:=0; --longueur du paquet reçu par le process pcmd
176        signal timeout,timeout_i : natural range 0 to 4095:=0;
177        signal PortStat,PortStat_i : std_logic_vector(3 downto 0):=(others=>'0');
178        signal PeerPort,PeerPort_i : natural range 0 to 15:=0; -- désigne le port qui emet une requête
179        signal PeerRTS,PeerRTS_i,PeerCTR:std_logic:='0';
180        signal cpt,cpt_peer : natural range 0 to 31:=0; --compteur pour le nombre de HT initialisés
181        signal clk_cyl,clk_cyl_i : natural range 0 to 15:=0;
182begin
183--=======================================================================================
184--fonctions du module
185--=======================================================================================
186--=======================================================================================
187 
188-- Inst_SetBit: SetBit PORT MAP(
189--              clk => clk,
190--              reset => reset ,
191--              ce => cmd,
192--              BitNum => ,
193--              BitVal => '1',
194--              dma_wr_grant => dma_wr_grant ,
195--              dma_wr_request => dma_wr_request,
196--              dma_rd_grant => dma_rd_grant,
197--              dma_rd_request => dma_rd_request,
198--              ram_rd => ram_rd,
199--              ram_wr => ram_wr,
200--              ram_address => ,
201--              Ram_data_in => Ram_data_in,
202--              Ram_data_out => Ram_data_out
203--      );
204
205 
206   -- Gestion de l'initialisation du Core MPI
207  sync_Init2 : process (clk,reset)
208  begin
209        if reset='1' then
210                stinit2<=init;
211                StatAsked<='0';
212                MyPort<=(others=>'0');
213                NextRank<=(others =>'0');
214        elsif rising_edge(clk) then
215                stinit2<=next_stinit2;
216                IS_State<=Next_IS_State;
217                n0<=n0_i;
218                --ajout des autres affectations ici
219                Ram_Next_Address<=Ram_Next_Address_i;
220                PortNumFlag<=PortNumFlag_i;
221                RankAsked<=RankAsked_i;
222                RankSent<=RankSent_i;
223                AppRank<=MyRank;
224                timeout<=timeout_i;
225                cpt<=cpt_peer;
226                clk_cyl<=clk_cyl_i;
227                NextRank<=NextRank_i;
228                StatAsked<=StatAsked_i;
229                MainResp<=MainResp_i;
230                AppSize1<=AppSize_i;
231                n<=n_i;
232                Ht_Start<=Ht_Start_i;
233                Grp_Id<=Grp_Id_i;
234                vPeerStat<=vPeerStat_i;
235                SpawnNbAck<=SpawnNbAck_i;
236                PortStat<=PortStat_i;
237                EquFlag<=EquFlag_i;
238                --IsMain1<=IsMain_i;
239                if ismain_i='1' then
240                        IsMain1<='1';
241                       
242                else
243                        IsMain1<=IsMain1;
244                end if;
245                if stInit2=DecodeData then
246                        MyPort<=Cmdreceived(2)(3 downto 0);
247                end if;
248               
249                if stInit2=StoreRank and ExecTime_Out='1' then
250                        MyRank<=MyPort;
251                elsif stInit2=StoreRank then
252                        MyRank<=DataReceived(2)(3 downto 0);
253                elsif stInit2=SetMainFlag then
254                  MyRank<=MyPort;
255                end if;
256                Ready<=Rdy;
257                snd_start<=snd_start_i;
258                SpawnNbReq_q<=SpawnNbReq;
259                SpawnReq_Q<=SpawnReq;
260                SpawnCmd0_Q<=SpawnCmd0;
261                SpawnCmd1_Q<=SpawnCmd1;
262                SpawnCmd2_Q<=SpawnCmd2;
263                Spawn_grp_q<=Spawn_grp;
264                PeerRTS<=PeerRTS_i;
265                PortNum<=PortNum_i;
266                Spawn_done<=Spawn_done_i;
267                HCL_init<=Hcl_init_i;
268                PeerStat<=PeerStat_i;
269                DataLen<=DataLen_i;
270                MainPort<=MainPort_i;
271                Nextr<=NextR_i;
272                NocMax<=NoCMax_i;
273                PeerPort<=PeerPort_i;
274                grp_id<=grp_id_i;
275                for i in 0 to 3 loop
276                        data_to_send(i)<=data_to_send_i(i);
277                end loop;
278        end if;
279end process;
280
281AppSize<=AppSize1;
282
283  Init2_DECODE: process (stinit2,n, CmdReceived, EquFlag, MyPort, DS_RDY, MainResp, DataReceived,
284  SpawnOn,Ram_Next_Address,PeerRTS,PortSTAT,dma_rd,dma_wr,Send_rdy,I_Send_rdy,rd_ok,wr_ok,Instruction,
285  MainPort,PeerStat,SpawnCmd0,SpawnCmd1,SpawnCmd2,SpawnInit,BCast_rdy)
286   variable nextadr : natural :=0;--to_integer(unsigned(Ram_Next_Address));
287        variable nport : natural range 0 to 15:=0;
288        begin
289      for i in 0 to 3 loop
290                        data_to_send_i(i)<=data_to_send(i);
291                end loop;
292                PortNum_i<=PortNum;
293                PortNumFlag_i<=PortNumFlag;
294                PortID<=MyPort;
295                PeerPort_i<=PeerPort;
296                NocMax_i<=NoCMax;
297                NextRank_i<=NextRank;
298                MainPort_i<=MainPort;
299                PeerStat_i<=PeerStat;
300                EquFlag_i<=EquFlag;
301                RankAsked_i<=RankAsked;
302                NextR_i<=NextR;
303                Ram_Next_Address_i<=Ram_Next_Address;
304                Ram_address<=(others=>'1');
305                Ram_data_in<=(others=>'1');
306                dma_rd_request<='0';
307                dma_wr_request<='0';
308                Initialized_i<='0';
309                rdy<='0';
310                ram_rd<='0';
311                ram_wr<='0';
312                I_Send_ack<='0';
313                IsMain_i<='0';
314                send_ack<='0';
315                DataLen_i<=0;
316      Case stInit2 is
317                When Init =>
318                        RTS_cmd<='0';
319                        --RTS_dat<='0';
320                        RTS_I<='0';
321                        BCast<='0';
322                        Result_En<='0';
323                        Initialized_i<='0';
324                        CM_Ack <='0';
325                        CTR<='0' ;
326                        DS_ACK<='0';
327                        Send_Ack<='0';
328                        ram_wr<='0';
329                        rdy<='1';--le module est disponible
330                        ResultOut<=(others=>'0');
331                        if PortNumFlag='1'  and IsMain1='1' then
332                                PortNum_i<=Instruction(3 downto 0);
333                        else
334                                PortNum_i<=(others=>'-');
335                        end if;
336                        EquFlag_i<='0';
337                        RankAsked_i<='0';
338                        dma_wr_request<='0';
339                        dma_rd_request<='0';
340                When GetPortNum =>
341                        RTS_cmd<='1';
342                        RTS_I<='0';
343                       
344                        Result_En<='0';
345                        Initialized_i<='0';
346                        BCast<='0';   --réalise un envoie non collectif
347                        CM_Ack<='0';   -- les données sont maintenant attendues
348                        CTR<='0' ;
349                        data_to_send_i(0)<=X"0" & GETPORTID;
350                        DS_ACK<='0';
351                        Send_Ack<='0';
352                        ram_wr<='0';
353                        rdy<='0';
354                        IsMain_i<='0';
355                        ResultOut<=(others=>'0');
356                        PortNum_i<=(others=>'-');
357                        RankAsked_i<='0';
358                        dma_wr_request<='0';
359                When DecodeData =>
360                        EquFlag_i<=(All_zeros(Cmdreceived(2)(3 downto 0)));
361                        RTS_cmd<='0';
362                        RTS_I<='0';
363                        BCast<='0';
364                       
365                        Result_En<='0';
366                       
367                        CM_Ack <='1'; --les données ont été reçues
368                        --MyPort<=Cmdreceived(2)(3 downto 0);
369                        PortNum_i<=Cmdreceived(2)(3 downto 0); --récupère les valeurs de port
370                        PeerPort_i<=To_integer(unsigned(Cmdreceived(2)(3 downto 0)));
371                        NocMax_i<=Cmdreceived(2)(7 downto 4);  -- et la taille du réseau
372                        PortNumFlag_i<='1';  -- le numéro du port est maintenant connu
373                        CTR<='0' ;
374                        DS_ACK<='0';
375                        Send_Ack<='0';
376                        ram_wr<='0';
377                        rdy<='0';
378                        RankAsked_i<='0';
379                        ResultOut<=(others=>'0');
380                        dma_wr_request<='0';
381                When IsPortZero => --Ce port est-il le n° 0 du NoC ?
382                        BCast<='0';
383                        Result_En<='0';
384                        RTS_cmd<='0';
385                        RTS_I<='0';
386                        NextRank_i<="0001"; -- le prochain rang à affecter sera le n° 1
387                        nextr_i<=1;
388                        IsMain_i<=EquFlag;
389                        --MyRank<="0000";
390                        MainPort_i<="0000";
391                        --MainAdr<="0000";   -- le port 0 est le main Lib par défaut;
392                        CTR<='0' ;
393                        DS_ACK<='0';
394                        ram_wr<='0';
395                                                rdy<='0';
396                        RankAsked_i<='0';
397                        ResultOut<=(others=>'0');
398                        dma_wr_request<='0';
399                When SeekMain1 =>    -- recherche de la lib main
400                                RTS_cmd<='0';
401                                --RTS_DAT<='1';
402                                RTS_I<='1';
403                                CTR<='1' ; -- prêt à recevoir les données Clear To Receive
404                                DS_ACK<='0'; --les données sont attendus
405                                Send_Ack<=BCast_rdy;
406                                I_Send_Ack<=BCAst_rdy;
407                                data_to_send_i(0)<=MPI_INIT & MyPort ;
408                                data_to_send_i(1)<="00000100"; -- la longueur du packet =4
409                                data_to_send_i(2)<="0000" & MyPort; --propose sa propre adresse
410                                data_to_send_i(3)<=INIT_SEEKMAIN & MyPort ;
411                                DataLen_i<=4;
412                                BCast<='1';
413                               
414                                Result_En<='0';
415                                Initialized_i<='0';
416                                ram_wr<='0';
417                                rdy<='0';
418                                ResultOut<=(others=>'0');
419                                RankAsked_i<='0';
420                                dma_wr_request<='0';
421                When SeekMain2 =>    -- recherche de la lib main
422                                RTS_cmd<='0';
423                                RTS_I<='0';
424                                CTR<='1' ; -- prêt à recevoir les données Clear To Receive
425                                DS_ACK<= DS_RDY; --les données sont acquittées aussitôt reçues
426                                Send_Ack<=BCast_rdy;
427--                              data_to_send_i(0)<=INIT_SEEKMAIN & MyPort ;
428--                              data_to_send_i(1)<="00000011"; -- la longueur du packet =3
429--                              data_to_send_i(2)<="0000" & MyPort; --propose sa propre adresse
430--                              DataLen<=3;
431                                BCast<='0';                             
432                                Result_En<='0';
433                                Initialized_i<='0';
434                                ram_wr<='0';
435                                rdy<='0';
436                                ResultOut<=(others=>'0');
437                                EquFlag_i<='0';
438                                RankAsked_i<='0';
439                                dma_wr_request<='0';
440                When StoreMain =>     -- la Main Lib est une autre
441                                BCAST<='0';
442                                Result_En<='0';
443                                IsMain_i<='0';
444                                Initialized_i<='0';
445                                DS_ACK<=DS_RDY;
446                                Send_Ack<='0';
447                                RTS_cmd<='0';
448                                RTS_I<='0';
449                                CTR<=not(MainResp);  -- essayer de recevoir tant que le main n'a pas répondu
450                                ram_wr<='1';
451                                rdy<='0';
452                                ResultOut<=(others=>'0');
453                                RankAsked_i<='0';
454                                EquFlag_i<='0';
455                                MainPort_i<=DataReceived(0)(3 downto 0);
456                                ram_address<= STD_logic_vector(to_unsigned(Core_init_adr+1,16));
457                                ram_data_in<=DataReceived(0)(3 downto 0)& DataReceived(2)(3 downto 0);  --MainPort & MyRank
458                                dma_wr_request<= '0';  ---pas très sûr
459               
460                When AskPeerStat =>    -- Interroge les données d'initialisation
461                                RTS_cmd<='0';
462                                if n=0 then
463                                --RTS_DAT<='0'; --on peut envoyer les données
464                                RTS_I<='1';
465                                else
466                                  --RTS_DAT<='0'; --on désactive l'envoie des données
467                                  RTS_I<='0';
468                                 end if;
469                                CTR<='0' ; -- prêt à recevoir les données Clear To Receive
470                                DS_ACK<='0'; --les données sont attendus
471                                I_Send_Ack<=I_Send_rdy;
472                                Send_Ack<=Send_rdy;
473                                data_to_send_i(0)<=MPI_INIT & MainPort ;
474                                data_to_send_i(1)<="00000100"; -- la longueur du packet =4
475                                data_to_send_i(2)<="0000" & MyPort; --indique sa propre adresse
476                                data_to_send_i(3)<=INIT_STAT & MyPort ;
477                                DataLen_i<=4;
478                                BCast<='0';
479                                Result_En<='0';
480                                ram_wr<='0';
481                                rdy<='0';
482                                ResultOut<=(others=>'0');
483                                EquFlag_i<='0';
484                                RankAsked_i<='0';
485                                dma_wr_request<='0';
486                When SetMainFlag =>   -- Cette Lib est la Main Lib
487                                if (Datareceived(3)(7 downto 4) = INIT_SEEKMAIN) then
488                                        RankAsked_i<='1';
489                                else
490                                        RankAsked_i<='0';
491                                end if;
492                                RTS_cmd<='0';
493                                Initialized_i<='0';
494                      RTS_I<='0';
495                                BCast<='0';
496                                IsMain_i<='1';
497                                Result_En<='0';
498                                --MyRank<="0000";
499                                MainPort_i<="0000";
500                                PeerStat_i(0)<='1'; --la main lib est initialisée
501                                CTR<='1';
502                                DS_ACK<='0';
503                                ram_wr<='1';
504                                rdy<='0';
505                                ResultOut<=(others=>'0');
506                                EquFlag_i<='0';
507                                ram_address<= STD_logic_vector(to_unsigned(Core_init_adr+1,16));
508                                ram_data_in<=(others =>'0');  -- le port vaut 0 et la mainlib est aussi à 0
509                                dma_wr_request<='1';
510                When ReadNoc =>
511                                if (Datareceived(3)(7 downto 4) = INIT_SEEKMAIN) then
512                                        RankAsked_i<='1' ;
513                                else
514                                        RankAsked_i<='0';
515                                end if;
516                                ram_wr<='0';  -- pas d'écriture en RAM
517                                rdy<='0';
518                                BCast<='0';
519                                DS_ACK<='0';
520                                --IsMain<='0';
521                                Result_En<='0';                 
522                                RTS_cmd<='0';
523                                RTS_I<='0';
524                                CTR<='1';
525                                ResultOut<=(others=>'0');
526                                PortNum_i<=DataReceived(2)(3 downto 0); -- le port qui demande un rang;
527                                PeerPort_i<= To_integer(unsigned(DataReceived(2)(3 downto 0)));
528                                --EquFlag<='1';
529                                dma_wr_request<='0';
530                When GetMainReq  =>
531                                --RankAsked_i<='1';
532                                ram_wr<='0';  -- pas
533                                BCast<='0';
534                                DS_ACK<='1';                           
535                                Result_En<='0';                         
536                                RTS_cmd<='0';
537                                RTS_I<='0';
538                                CTR<='1';
539                                ResultOut<=(others=>'0');
540                                PortNum_i<=DataReceived(0)(3 downto 0); -- le port qui demande un rang;
541                                EquFlag_i<='1';
542                                dma_wr_request<='0';
543                When GetPeerStat  =>
544                                --RankAsked_i<='1';
545                                ram_wr<='0';  -- pas
546                                BCast<='0';
547                                DS_ACK<=DS_RDY;
548                                Result_En<='0';
549                                RTS_cmd<='0';
550                                RTS_I<='0';
551                                CTR<=not MainResp;
552                                ResultOut<=(others=>'0');
553                                PortNum_i<=DataReceived(0)(3 downto 0); --adresse du Main Port
554                                PeerStat_i(7 downto 0)<=DataReceived(2); -- état des initialisations
555                                EquFlag_i<='0';
556                                dma_wr_request<=dma_wr;
557                                ram_wr<=wr_ok;
558                                rdy<='0';
559                                ram_rd<=rd_ok;
560                                Ram_Next_Address_i<= STD_logic_vector(to_unsigned(CORE_Init_Adr+2,ADRLEN)); --incr_vec(Ram_Next_Address,'1');
561                                ram_address<=Ram_Next_Address;  --
562                                ram_data_in<=DataReceived(2); --enregistrer les ports actifs
563                               
564                When NewRank =>
565                                nport:=to_integer(Unsigned(PortNum)); --port ayant sollicité le rang
566                                nextr_i<=to_integer(unsigned(nextrank));
567                                if PeerStat(nport)='0' then -- si le rang n'a pas encore été affecté à ce port
568                                nextrank_i<=incr_vec(nextrank,'1');
569                                RankToPort(to_integer(unsigned(nextrank)))<="0000" & PortNum;
570                                NextAdr:=NextAdr+1;
571                                end if;
572                                data_to_send_i(0)<=MPI_INIT & PortNum ;
573                                data_to_send_i(1)<="00000100";
574                                data_to_send_i(2)<="0000" & NextRank;  --ici c'est la valeur avant incrémentation !
575                                data_to_send_i(3)<=INIT_SETRANK & PortNum ;
576                                PeerStat_i(nport)<='1'; -- mise à jour du status
577                                DataLen_i<=4;
578                                BCast<='0';
579                                DS_ACK<='0';
580                                Result_En<='0';
581                                EquFlag_i<='1';
582                                RTS_cmd<='0';
583                                RTS_I<='1';
584                                CTR<='0' ;
585                                ram_wr<='0';
586                                rdy<='0';
587                                RankAsked_i<='1';
588                                ResultOut<=(others=>'0');
589                                dma_wr_request<='0';
590                               
591                When SendRank =>
592                                BCast<='0';
593                                Result_En<='0';
594                                EquFlag_i<='1';
595
596                                RTS_I<='1';
597                                DataLen_i<=4;
598                                RTS_cmd<='0';
599                                DS_ACK<='0';
600                                Send_Ack<='0';
601                                CTR<='0' ;
602                                ram_wr<='0';
603                                Ram_Next_Address_i<= STD_logic_vector(to_unsigned(CORE_RANK_ADR+Nextr-1,16));
604                                ResultOut<=(others=>'0');
605                                RankAsked_i<='1';
606                                dma_wr_request<='0';
607                When RegRank =>
608                                        RankAsked_i<='0';
609
610                                CTR<='1' ; -- continuer à recevoir les données du NoC
611                                Result_En<='0';
612                                RTS_cmd<='0';
613                                DS_ACK<='0';
614                                Send_Ack<=Send_RDY;
615                                I_Send_ack<=i_send_rdy;
616                                Rts_I<='0';
617                                BCast<='0';
618                                EquFlag_i<='1';
619                                ram_wr<='1';
620                               
621                                Ram_Next_Address_i<= STD_logic_vector(to_unsigned(CORE_RANK_ADR+Nextr-1,16)); --incr_vec(Ram_Next_Address,'1');
622                                ram_address<=Ram_Next_Address;  -- le rang qui a été envoyé;
623                                -- "le motif 0001 indique juste que le port est bien activé
624                                ram_data_in<="0001" & PortNum; --enregistrer le port qui a fait la demande en RAM
625                                ResultOut<=(others=>'0');
626                                dma_wr_request<='1';
627                When SendPeerStat =>
628                                BCast<='0';
629                                Result_En<='0';
630                                EquFlag_i<='0';
631                                RTS_I<=PeerRTS;
632                                RTS_cmd<='0';
633                                DS_ACK<='0';
634                                data_to_send_i(0)<=MPI_INIT & PortStat ;
635                                data_to_send_i(1)<="00000100";
636                                data_to_send_i(2)<=PeerStat(7 downto 0);
637                                data_to_send_i(3)<=INIT_REGISTER & MyPort ;
638                                Send_Ack<=Send_Rdy;
639                                I_Send_Ack<=I_Send_Rdy;
640                                CTR<='0' ;
641                                ram_wr<='0';
642                                rdy<='0';
643                                ResultOut<=(others=>'0');
644                                RankAsked_i<='1';
645                                dma_wr_request<='0';
646                                Initialized_i<='1';
647                When StoreRank => --le processus qui écoute le Port et qui
648                                                                --stoke les adresses des autres bib va gérer le stockage du rang
649                                BCAST<='0';
650                                DS_ACK<='1';
651                                Send_Ack<='0';
652                                RTS_cmd<='0';
653                                EquFlag_i<='0';
654                                RTS_I<='0';
655                                CTR<='0';
656                                Result_En<='0';
657                               
658                                if ExecTime_out='1' then
659                                MainPort_i<="0000";
660                                --MyRank<=MyPort;
661                                initialized_i<='0';
662                                else
663                                MainPort_i<=DataReceived(0)(3 downto 0);
664                                --MyRank<=DataReceived(2)(3 downto 0);
665                                Initialized_i<='1';
666                                end if;
667                                ram_wr<='1';
668                                --ram_address<=DataReceived(2);  -- le rang qui a été envoyé;
669                                ram_address<= STD_logic_vector(to_unsigned(Core_init_adr+1,16));
670                                ram_data_in<=DataReceived(0)(3 downto 0)& DataReceived(2)(3 downto 0);  --MainPort & MyRank
671                                ResultOut<=(others=>'0');
672                                RankAsked_i<='0';
673                                dma_wr_request<='1';
674                When SendApp =>  --ajouter du code pour recevoir l'adr du reg status
675                                Initialized_i<='1';
676                                ResultOut<="00000001"; -- Init Ok
677                                Result_En<='1';
678                                PortId<=MyPort;
679                                BCAST<='0';
680                                DS_ACK<='1';
681                                Send_Ack<='0';
682                                RTS_cmd<='0';
683                                RTS_I<='0';
684                                CTR<='0';
685                                ram_wr<='0';
686                                rdy<='0';
687                                RankAsked_i<='0';
688                                dma_wr_request<='0';
689                When SpawnApp =>
690                                RTS_cmd<='0';
691                                RTS_I<='0';
692                                Result_En<='0';
693                                DS_ACK<=DS_RDY;
694                                BCAST<='0';
695                                CTR<='0';  --cette mise à 1 permet de ctrler la fin de réception
696                                rdy<='0';
697                                ram_wr<=wr_ok;
698                                ram_rd<=rd_ok;
699                                Ram_Next_Address_i<= STD_logic_vector(to_unsigned(CORE_BASE_ADR,ADRLEN)); --incr_vec(Ram_Next_Address,'1');
700                                ram_address<=Ram_Next_Address;  -- le rang qui a été envoyé;
701                                -- "le motif 0001 indique juste que le port est bien activé
702                                ram_data_in<=DataToRam; --enregistrer le port qui a fait la demande en RAM
703                                dma_rd_request <= dma_rd;
704                                dma_wr_request <=dma_wr;
705                                RankAsked_i<='0';
706                                if n>=3 then
707                                        ResultOut<=("00000010");--spawn completed
708                                else
709                                        ResultOut<=("00000000");--spawn message received
710                                end if;
711                               
712                When SpawnLoad =>
713                    RTS_cmd<='0';
714                                --RTS_DAT<='1'; --à tester
715                                RTS_I<=PeerRTS; -- pour le timing
716                                Result_En<='0';
717                                DS_ACK<='0';
718                                BCAST<='0';
719                                CTR<='0';
720                                rdy<='0';
721                                ram_wr<=wr_ok;
722                                ram_rd<=rd_ok;
723                      data_to_send_i(0)<=SpawnCmd0;
724          data_to_send_i(1)<="00000100";
725          data_to_send_i(2)<=SpawnCmd1;
726          data_to_send_i(3)<=SpawnCmd2; -- SPAWN_LOAD & MyPort ;
727          Send_Ack<=Send_Rdy;
728                                I_Send_Ack<=I_Send_rdy;
729          RankAsked_i<='0';
730                                if SpawnInit='1' then
731                                        ResultOut<=("10000000"); --spawn completed
732                                else
733                                        ResultOut<=("01000000");--spawn in progress
734                                end if;
735                When ErrSpawn =>
736                                RTS_cmd<='0';
737                                RTS_I<='0';
738                                Result_En<='1';
739                                DS_ACK<='0';
740                                Send_Ack<='0';
741                                BCAST<='0';
742                                CTR<='0';
743                                ram_wr<='0';
744                                rdy<='0';
745                                RankAsked_i<='0';
746                               
747                                ResultOut<=(others=>'0');
748                                dma_wr_request<='0';
749                                dma_rd_request<='0';
750                When EndInit =>
751                                RTS_cmd<='0';
752                                RTS_I<='0';                             
753                                Result_En<='0';
754                                DS_ACK<='0';
755                                Send_Ack<='0';
756                                BCAST<='0';
757                                CTR<='0';
758                                ram_wr<='0';
759                                rdy<='0';
760                                RankAsked_i<='0';
761                                ResultOut<=(others=>'0');
762                                dma_wr_request<='0';
763                                dma_rd_request<='0';
764                end case;
765   end process;
766       
767result_proc:process (ISMain_i,Initialized_i,stInit2,reset)
768begin
769if reset='1' then
770        --IsMain1<='0';
771        Initialized<='0';
772        HCL_Init_i<='0';
773       
774Else
775        HCL_init_i<=HCl_init;
776        Initialized<=Hcl_init;
777--      if ismain_i='1' then
778--              IsMain1<='1';
779--      end if;
780
781        if Initialized_i='1' and stInit2=EndInit then
782                Initialized<='1';
783                HCL_Init_i<='1';
784        elsif stInit2=SendApp then
785          HCL_Init_i<='1';
786        end if;
787end if;
788end process result_proc;
789 --================
790 --Traitement du Spawn
791 --===================
792 
793 --==================
794   NEXT_STInit2_DECODE: process (stInit2,AppReq,Instruction_en, AppAck,PortNumFlag,EquFlag,MainResp,StatAsked,RankAsked,RankSent,CM_RDY, CmdReceived,
795        BCast_Rdy, DS_RDY, DataReceived,Ht_Start,vPeerStat, Send_RDY,   I_Send_RDY,TimeOut,Dma_rd_grant,Dma_wr_grant,cpt,n,clk_cyl,IsMain1,SpawnOn,
796        Instruction,Hcl_Init,Spawn_done,mainport,PeerStat,PeerRTS,Rec_Data,SpawnInit,grp_id,SpawnNbReq,Ram_data_out,Spawn_grp,MyPort)
797   variable InitTimeOut :natural:=0;
798        variable LastPort,Ht_hole : std_logic:='0';
799        variable i,nbSpawn,htloc,Ht_Id : natural range 0 to 15:=0;
800        --variable vPeerStat : std_logic_vector(15 downto 0);
801   variable vPortStat,nulvect : std_logic_vector(3 downto 0):="0000";   
802        variable tempval : std_logic_vector(Word-1 downto 0);
803  begin
804      --declare default state for next_state to avoid latches
805      next_stInit2 <= stInit2;  --default is to stay in current state
806      --insert statements to decode next_state
807      --below is a simple example
808                vPeerStat_i<=vPeerStat;
809                SpawnNbReq<=SpawnNbReq_q;
810                StatAsked_i<=StatAsked;
811                MainResp_i<=MainResp;
812                AppSize_i<=AppSize1;
813                PortStat_i<=PortStat;
814                grp_id_i<=grp_id;
815                PeerRTS_i<=PeerRTS;
816                --PortNumFlag_i<=PortNumFlag;
817                SpawnReq<=SpawnReq_q;
818                Spawn_done_i<=Spawn_done;
819                Ht_Start_i<=Ht_Start;
820                SpawnCmd0<=SpawnCmd0_Q;
821                SpawnCmd1<=SpawnCmd1_Q;
822                SpawnCmd2<=SpawnCmd2_Q;
823                Timeout_i<=Timeout;
824                clk_cyl_i<=clk_cyl;
825                n_i<=n;
826                Cpt_peer<=cpt;
827                DataToRam<=(others=>'0');--A revoir
828                dma_rd<='0';
829                dma_wr<='0';
830                rd_ok<='0';
831                wr_ok<='0';
832                for i in 0 to 3 loop
833                Spawn_grp(i)<=Spawn_grp_q(i);
834                end loop;
835      case (stInit2) is
836         
837                       
838                When Init =>If Instruction_en='1' and AppReq='1'  then
839                                         If PortNumFlag='1'  and IsMain1='1' then
840                                                if instruction(7 downto 4)=INIT_SEEKMAIN and (instruction(3 downto 0)/=MyRank) then
841                                                        Next_stInit2 <=NewRank;--affecter un rang au demandeur.
842                                                elsif instruction(7 downto 4)=INIT_STAT then
843                                                  StatASked_i<='1';
844                                                        Next_stInit2 <=SendPeerStat; --envoyer des stat au demandeur
845                                                elsif instruction(7 downto 4)=SPAWN_LOAD then
846                                                        SpawnInit<='0'; -- indique le debut du Spawn
847                                                        Next_stInit2 <=SpawnLoad;
848                                                elsif instruction(7 downto 4)=INIT_SPAWN then
849                                                        SpawnInit<='1'; -- indique la fin du
850                                                        Next_stInit2 <=SpawnLoad; --dans ce cas il s'agit de terminer le spawn
851                                                else --ignorer ce message
852                                                        Next_stInit2 <=SendApp;
853                                                end if;
854                                         elsif HCL_Init='1' and IsMain1='0' then
855                                                Next_stInit2 <=SendApp; --ignorer ce message simplement
856                                        else
857                                                Next_stInit2 <=GetPortNum; -- initialiser la bibliothèque normalement
858                                        end if;
859                                        TimeOut_i<=0;PeerRTS_i<='0';
860                                       
861                                 elsif SpawnOn='1' and Spawn_done='0' then
862                                        Next_stInit2 <=SpawnApp;
863                                        TimeOut_i<=0;
864                                 end if;
865                                cpt_peer<=0;LastPort:='0';n_i<=0;
866                When GetPortNum =>  -- récupérer le numéro du port
867                                                                                               
868                                                if CM_RDY='1' then
869                                                        Next_stInit2 <=DecodeData;
870                                                end if;
871                                               
872                When DecodeData =>  -- cet état permet de lire les données reçues du port
873                                                        --if CM_RDY='1' then
874                                                                if ((Cmdreceived(2)(3 downto 0) or nulvect)=nulvect) then -- teste si le n° du port est zero
875                                                                  --EquFlag<='1';
876                                                                 else
877                                                                  --EquFlag<='0';
878                                                                end if;
879                                                               
880                                                                Next_stInit2 <=IsPortZero;
881                                                        --end if;
882                When IsPortZero =>
883                                                if PortNumFlag='1' then
884                                               
885                                                End if;
886                                                if EquFlag='0'  then  --tester le numéro de port obtenu
887                                                        Next_stInit2 <=SeekMain1; -- chercher le numéro de port sur le réseau
888                                                else
889                                                        Next_stInit2<=SetMainFlag; -- enregistrer le numéro de port
890                                                End If;
891                                                TimeOut_i<=0;
892               
893                When SeekMain1 =>If BCast_RDY='1' then  -- si tous les envois ont été postés
894                                                                        Next_stInit2<=SeekMain2;
895                                                        End if;                         
896                When SeekMain2 => 
897                                                        If DS_RDY='1' then  -- Si un jeu de données a été reçu
898                                                                                if (Datareceived(3)(7 downto 4) = INIT_SETRANK) then
899                                                                                                MainResp_i<='1';
900                                                                                                Next_stInit2 <=StoreMain; -- de la librairie pricipale
901                                                                         else
902                                                                                                MainResp_i<='0';
903                                                                                                Timeout_i<=0;
904                                                                         end if;       
905                                                                               
906                                                        elsif (ExecTime_Out='1') then
907                                                                       
908                                                                                                -- le Noc ne répond pas
909                                                                                                -- affecter un numéro en raport avec le port
910                                                                                Next_stInit2 <=SeekMain2;
911                                                        elsif TimeOut=800 then
912                                                                                                --essayer de redemander l'initialisation
913                                                                        assert true report "Ex4:seekmain ->redemander l'initialisation"
914                      severity failure;
915                                                                        Timeout_i<=0;                   
916                                                                        Next_stInit2 <=SeekMain1;
917                                                        else
918                                                                                Timeout_i<=TimeOut+1;
919                                                        End if;
920                                                       
921                                                         
922                                                         
923                When StoreMain =>
924                                if DataReceived(3)(7 downto 4)= INIT_SETRANK then
925                                -- enregistrer le Main Adresse et les adresses des autres processus
926                                -- dans la variable prévue à cet effet
927                                Next_stInit2 <=StoreRank;
928                               
929                                elsif Datareceived(3)(7 downto 4) = INIT_SEEKMAIN then
930                                -- ignorer ce message
931                                Next_stInit2 <=StoreMain; --essayer de recevoir le rang
932                               
933                               
934                                end if;
935                                       
936                When StoreRank =>
937                                        --il faut prévoir du code pour s'assurer que la RAM a bien enregistrer
938                                        -- la donnée ...
939                                        if dma_wr_grant = '1' then
940                                                if clk_cyl=2 then --prévoir deux cycle d'horloge pour lire en RAM
941                                                        Next_stInit2 <=AskPeerStat;
942                                                        TimeOut_i<=0;
943                                                        clk_cyl_i<=0;
944                                                else
945                                                        clk_cyl_i<=clk_cyl+1;
946                                                end if;
947                                        else
948                                                        Next_stInit2 <=StoreRank;
949                                                        TimeOut_i<=0;
950                                                        clk_cyl_i<=0;
951                                        end if;
952                When AskPeerStat =>if n=0 then
953                        If I_SEND_RDY='1' then  -- si tous les envois ont été postés
954                                                                        n_i<=1;
955                                                                        StatAsked_i<='0';
956                                                        End if;         
957                                                elsif n=1 then
958                                                If I_SEND_RDY='0' then  -- si tous les envois ont été postés
959                                                                        Next_stInit2<=GetPeerStat;
960                                                                        StatAsked_i<='1';
961                                                                        MainResp_i<='0';
962                                                           n_i<=0;
963                                                        End if;         
964                                                end if;         
965                When GetPeerStat => 
966                                                        if n=0 then
967                                                        If DS_RDY='1' then  -- Si un jeu de données a été reçu
968                                                                         if (Datareceived(3) = INIT_REGISTER & MainPort ) then
969                                                                                                MainResp_i<='1';
970                                                                                                n_i<=n+1;
971                                                                                               
972                                                                         else
973                                                                                                MainResp_i<='0';
974                                                                                                Timeout_i<=0;
975                                                                                                n_i<=n;
976                                                                         end if;       
977                                                                               
978                                                        elsif TimeOut=1800 then
979                                                                                                --essayer de redemander les statistiques
980                                                                        assert true report "Ex4:GetPeerStat timeout  ->Les statistiques n'ont pas été reçu"
981                                                                        severity failure;
982                                                                        Timeout_i<=0;   
983                                                                        If StatAsked='1' then
984                                                                        Next_stInit2 <=AskPeerStat; -- ceci n'est pas sûr
985                                                                        else
986                                                                        Next_stInit2 <=GetPeerStat;
987                                                                        end if;
988                                                        else
989                                                                                Timeout_i<=TimeOut+1;
990                                                        End if; 
991                                                        elsif n=1 then  --écrire le résultat de l'initialisation
992                                                        dma_wr<='1';
993                                                        if dma_wr_grant='1' then
994                                                                n_i<=n+1;
995                                                                wr_ok<='1';
996                                                        end if;
997                                                        elsif n=2 then
998                                                                n_i<=n+1;   --écriture dans la RAM
999                                                        elsif n=3 then
1000                                                                n_i<=0;
1001                                                                dma_wr<='0';
1002                                                                Next_stInit2 <=SendApp; -- de la librairie pricipale
1003                                                        end if;
1004                When SetMainFlag =>
1005                                if dma_wr_grant='1' then   
1006                                                TimeOut_i<=0;
1007                                                Next_stInit2 <=ReadNoc;
1008                                        elsif Timeout=800 then
1009                                                assert true report "Ex4:SetMainFlag timeout  ->La mémoire n'est pas dispo dma_wr_grant=0"
1010                                                severity failure;
1011                                                Next_stInit2 <=SendApp;
1012                                        else
1013                                                TimeOut_i<=TimeOut+1;
1014--                                      end if; -- il est envisagée de terminer le programme à ce point
1015                                end if;
1016                when ReadNoc => if DS_RDY='1' then
1017                                                                        if (Datareceived(3)(7 downto 4) = INIT_STAT) then
1018                                                                                        StatAsked_i<='1' ;
1019                                                                                        Next_stInit2 <=SendPeerStat;
1020                                                                                        LastPort:='0';
1021                                                                        else
1022                                                                                StatAsked_i<='0';
1023                                                                                Next_stInit2 <=GetMainReq;
1024                                                                        end if;
1025                                                                       
1026                                                                        Timeout_i<=0;
1027                                                                elsif Timeout=400 then
1028                                                                        assert true report "Ex4:ReadNoc timeout pas de données lues sur le NoC après 200 cycles"       
1029                                                                        severity failure;                                                         
1030                                                                        Timeout_i<=0;
1031                                                                        --if StatAsked = '1' then
1032                                                                                --Next_stInit2 <=ErrSpawn; --il y a eu une erreur
1033                                                                                Next_stInit2 <=SendApp;
1034                                                                --      else
1035                                                                        --      Next_stInit2 <=SendPeerStat;
1036                                                                --      end if;
1037                                                                else
1038                                                                        TimeOut_i<=TimeOut+1;
1039                                                                end if;
1040                               
1041                When GetMainReq => if RankAsked='1' and PeerStat(PeerPort)='0' then
1042                                                                        Next_stInit2 <=NewRank; -- un nouveau rang est demandé
1043                                                                else
1044                                                                        Next_stInit2 <=ReadNoC; -- sinon essayer encore de lire un jeu de données.
1045                                                                end if;
1046                When NewRank =>
1047                                Next_stInit2 <=SendRank;
1048                When SendRank =>
1049                                        if I_Send_RDY='1' then  --RankSent
1050                                                Next_stInit2 <=RegRank;
1051                                                RankSent_i<='1';  -- le rang a été envoyé
1052                                                TimeOut_i<=0;   -- prépare le traitement de la prochaine requête
1053                                                                                                -- d'attente des ports
1054                                  elsif Timeout=800 then
1055                                    assert true report "Impossible d'envoyer le rang"
1056                                    severity failure;
1057                                    timeout_i<=0;
1058                                  else
1059                                    TimeOut_i<=TimeOut+1;
1060                                        end if;
1061                When RegRank =>
1062                                        --il faut prévoir du code pour s'assurer que la RAM a bien enregistrer
1063                                        -- la donnée ...
1064                                        if dma_wr_grant = '1' then
1065--                                                      if DS_RDY='1' then
1066                                                                RankSent_i<='0';
1067                                                                if DS_RDY='0' then
1068                                                                 
1069                                                                        Next_stInit2 <=ReadNoc;
1070                                                                end if;
1071                                                        elsif Timeout=800 then
1072                                                                Next_stInit2 <=SendApp;
1073                                                                assert true report "Ex4:RegRank Timeout dma_wr_grant=0 "
1074                                    severity failure;
1075                                                        else
1076                                                                TimeOut_i<=TimeOut+1;
1077--                                                      end if; -- il est envisagée
1078                                        end if;
1079                                        --calcul de la taille de l'appli
1080                                        HtLoc:=0;
1081                                        for i in 0 to 7 loop
1082                                                if peerStat(i)='1' then --trouve le prochai rang libre
1083                                                        HtLoc:=HtLoc+1;
1084                                                end if;
1085                                        end loop;
1086                                        AppSize_i<=std_logic_vector(to_unsigned(HtLoc,4));
1087               
1088                When SendPeerStat =>
1089                                       
1090                                        if cpt<=unsigned(NoCMax)and LastPort='0' then
1091                                                if peerRts='0' then -- pas encore envoyer les données alors les préparer
1092                                                                if StatAsked='1' then
1093                                                                       
1094                                                                        PortStat_i<=PortNum;
1095                                                                        vPortStat:=std_logic_vector(to_unsigned(PeerPort,4));
1096                                                                else
1097                                                                        PortStat_i<=std_logic_vector(to_unsigned(cpt,4));--Port à contacter
1098                                                                        vPortStat:=std_logic_vector(to_unsigned(cpt,4));
1099                                                                end if;
1100                                                               
1101                                                                if StatAsked='1' then
1102                                                                        LastPort:='1';  -- on traite u seul port celui qui en a fait la demande
1103                                                                        if PeerStat(PeerPort)='1' then
1104                                                                        PeerRts_i<='1';
1105                                                                        end if;
1106                                                                else
1107                                                                        if (PeerStat(cpt)='1') and not(vPortStat=MyPort) then
1108                                                                                PeerRts_i<='1';
1109                                                                        end if;
1110                                                                        if cpt<unsigned(NoCMax) then
1111                                                                                cpt_peer<=cpt+1;
1112                                                                                LastPort:='0';
1113                                                                        else
1114                                                                                if LastPort='0' then
1115                                                                                                LastPort:='1';
1116                                                                                                cpt_peer<=cpt+1;--ceci permet de déclencher l'évaluation du process
1117                                                                                end if;
1118                                                                        end if;
1119                                                                end if;
1120                                                                TimeOut_i<=0;
1121                                        else
1122                                                        if I_Send_RDY='1' then 
1123                                                                               
1124                                                                                RankSent_i<='1';        -- les stat ont été envoyé
1125                                                                                TimeOut_i<=0;           -- prépare le traitement de la prochaine requête
1126                                                                                PeerRts_i<='0';                                                                         -- d'attente des ports
1127                                                          elsif TimeOut=800 then
1128                                                                        Next_stInit2 <=EndInit ;-- impossible d'envoyer sur le NoC
1129                                                           assert true report "Ex4:SendPeerStat Timeout -> Impossible d'envoyer sur le NoC"
1130                                        severity failure;
1131                                                          else
1132                                                                        TimeOut_i<=TimeOut+1;
1133                                                        end if;
1134                                        end if;
1135                                        elsif PeerRTS='1' then -- le dernier port est il activé ?
1136                                                if I_Send_RDY='1' then 
1137                                                                RankSent_i<='1';        -- les stat ont été envoyé
1138                                                                                TimeOut_i<=0;           -- prépare le traitement de la prochaine requête
1139                                                                                PeerRts_i<='0';
1140                                                        if statAsked='1' then
1141                                                         -- Next_stInit2<=ReadNoc;
1142                                                       
1143                                                        Next_stInit2<=SendApp; --terminer l'application
1144                                                  end if;
1145                                               
1146                                                elsif TimeOut=100 then
1147                                                        Next_stInit2 <=EndInit;
1148                                                        assert true report "Ex4:SendPeerStat Timeout -> Impossible d'envoyer sur le NoC"
1149                                        severity failure;
1150                                                else
1151                                                        TimeOut_i<=TimeOut+1;
1152                                                end if;
1153                                        else
1154                                                Next_stInit2<=SendApp;
1155                                        end if;
1156                When SendApp =>  -- envoyer au programme un signal
1157                                        -- et attendre un acquittement et l'adresse de base
1158                                        If AppAck='1' then
1159                                                Next_stInit2 <=EndInit;
1160                                        end if;
1161               
1162                When SpawnLoad =>
1163               
1164                                if n=0 then
1165                                        ht_id:=to_integer(unsigned(rec_data(0)(3 downto 0)));
1166                                        vPeerStat_i<=PeerStat; --récupérer la situation actuelle des tâches créées
1167                                        if IsMain1='1' and SpawnInit='0' then
1168                                         SpawnNbReq<=to_integer(unsigned(Instruction(3 downto 0))); -- sauver le nombre de HT à créer
1169 
1170                                        n_i<=1;
1171                                        SpawnReq(ht_id)<='1'; --Note l'id de HT qui a appelé le Spawn
1172                                        elsif IsMain1='1' and SpawnInit='1'  then -- il faut envoyer les acquittements
1173                                        ht_id:=to_integer(unsigned(rec_data(0)(3 downto 0)));
1174                                        Spawn_grp(grp_id)(ht_id)<='1'; --noter le port du Fils Spawné dans ce groupe
1175                                        Ht_start_i<=0; --compte le nombre de tâches chargées
1176                                                        n_i<=6;
1177                                       
1178                                        else
1179                                           if SpawnOn='1' then --if IsMain_i='0' then  -- il faut juste charger puis activer la tâche
1180                                             n_i<=7;
1181                                            end if;
1182                                            assert true
1183                                           Report "SpawnLoad : Cas non prévu !"
1184                                          severity failure;
1185                                        end if;
1186                                elsif n=1 then
1187                                if PeerStat=SpawnReq then --tous les Hts ont appelés Spawn ?
1188                                ht_hole:='0';           
1189        L1:                     for i in 0 to 15 loop
1190                                                if vPeerStat(i)='0' then
1191                                                        SpawnDest<=std_logic_vector(to_unsigned(i,4)); -- le port à activer
1192                                                        SpawnCmd0<=MPI_SPAWN & std_logic_vector(to_unsigned(i,4));
1193                                                        vPeerStat_i(i)<='1';
1194                                                        Ht_Start_i<=Ht_Start+1;
1195                                                        Ht_Hole:='1'; -- une place a été trouvé
1196                                                else
1197                                                        Ht_hole:='0'; -- pas de place trouvé
1198                                                end if;
1199                                                exit L1 when ht_hole='1';
1200                                        end loop L1;
1201                                        if Ht_hole='0' then -- toutes les tâches n'ont pas été créés
1202                                                -- aller à la fin ! en signalant une erreur!
1203                                                SpawnCMd2<=Spawn_Err & "0001"; --erreur pas assez de ports sur le NoC
1204                                                n_i<=9;
1205                                        else
1206                                                n_i<=n+1;
1207                                                SpawnCmd1<=PeerStat(7 downto 0);
1208                                                SpawnCmd2<=SPAWN_START & MyPort;
1209                                        end if;
1210                                        else --Tous les Hts n'ont pas appelé Spawn encore
1211                                          Next_stInit2<=SendApp; --on termine normalement
1212                                          n_i<=0;
1213                                        end if;
1214                                elsif n=2 then
1215                                PeerRTS_i<='1'; -- envoyer les données du Spawn
1216                                n_i<=n+1;
1217                                elsif n=3 then
1218                                        if I_Send_RDy='1' then
1219                                                n_i<=n+1;
1220                                                PeerRts_i<='0';
1221                                        end if;
1222                                elsif n=4 then
1223                                        if SpawnInit='0' then
1224                                        if Ht_Start>=SpawnNbReq then
1225                                                n_i<=n+1;
1226                                        else
1227                                                n_i<=1; -- passer à la création du HT suivant
1228                                        end if;
1229                                        else
1230                                                n_i<=7; -- passer au contact du HT suivant
1231                                        end if;
1232                                elsif n=5 then
1233                                                                -- Aller à la fin le résultat du Spawn
1234                               
1235                                n_i<=10;
1236                                elsif n=6 then
1237                                  nbSpawn:=0;
1238                                  for i in 0 to 15 loop
1239                                      if Spawn_grp(grp_id)(i)='1' then
1240                                         nbSpawn:=nbSpawn+1;
1241                                       end if;
1242                                   end loop;
1243                                  if nbSpawn=SpawnNbReq then
1244                                  n_i<=n+1; --aller renvoyer les réponses
1245                                  else
1246                                    n_i<=10; --fin du Spawn
1247                                   
1248                                   end if;
1249                                elsif n=7 then
1250                                Ht_hole:='0';
1251L2:                     for i in 0 to 7 loop --for i in Ht_start to 7 loop
1252                                                if i>=Ht_start then
1253                                                if vPeerStat(i)='1' and Spawn_grp(grp_id)(i)='0' then  --on avertit les membres du groupe parent
1254                                                  --mais pas ceux du groupe Fils !
1255                                                        SpawnDest<=std_logic_vector(to_unsigned(i,4)); -- le port à avertir
1256                                                        SpawnCmd0<=MPI_ACK & std_logic_vector(to_unsigned(i,4));
1257                                                        vPeerStat_i(i)<='0';
1258                                                        Ht_Start_i<=i+1; -- le prochain Ht à prévenir
1259                                                        Ht_Hole:='1'; -- Une tâche doit être avertie
1260                                                else
1261                                                        Ht_hole:='0'; --
1262                                                end if;
1263                                                end if;
1264                                                exit L2 when ht_hole='1';
1265                                        end loop L2;
1266                                        n_i<=n+1;
1267                        elsif n=8 then
1268                                 if Ht_hole='1' then --
1269                                                -- un HT existant à prévenir !
1270                                                SpawnCmd1<=Spawn_grp(grp_id)(7 downto 0);
1271                                                SpawnCMd2<=MPI_SPAWN & MyRank; -- fin du Spawn
1272                                                n_i<=2; --envoyer la  donnée sur le réseau
1273                                        elsif vpeerstat /=spawn_grp(grp_id) then --il ne reste plus de membres à prévenir ?
1274              n_i<=7; --on est sorti mais ce n'est pas la fin
1275
1276                                        else -- fin du Spawn car tous ont été averti
1277                                                n_i<=10;
1278                                                SpawnNbReq<=0;
1279                                                SpawnReq<=(others=>'0');
1280                                                vPeerStat_i<=(others=>'0');
1281                                                grp_id_i<=grp_id+1; --incrementer le compteur de groupes************
1282                                        end if;
1283                                elsif n=9 then
1284                                  assert true report "error in Spawn command"
1285                                   severity failure;
1286                                elsif n=10 then
1287                                  n_i<=0;
1288                                  Next_stInit2<=SendApp;
1289                                 end if;
1290               
1291        When SpawnApp=>
1292                --Mise à jour du bit 6 du registre status du COre.
1293                if n>=0 and n <4 then     
1294                       
1295                        dma_wr<='1';  --demander un accès exclusif au bus
1296                        dma_rd<='1'; -- pour éviter une mauvaise mise à jour des données
1297                else
1298                        dma_wr<='0';                                                                           
1299                        dma_rd<='0';
1300                end if;
1301               
1302                if n=0 then
1303                  if DS_RDY='1' then ---
1304                      if Datareceived(3)(7 downto 4)=SPAWN_START then
1305                        n_i<=n+1;
1306                       else
1307                         Next_stInit2<=ErrSpawn;
1308                         n_i<=0; --erreur instruction incorrecte
1309                        end if;
1310                           
1311                           rd_ok<='1';
1312                           wr_ok<='0';
1313                        end if;
1314                elsif n=1 or n=2 then
1315                        if dma_rd_grant='1' then
1316                                n_i<=n+1;
1317                                tempval:=Ram_data_out;
1318                        end if;
1319                                rd_ok<='1';
1320                                wr_ok<='0';
1321                         
1322                elsif n=3 then
1323                                if dma_rd_grant='1' and dma_wr_grant='1' then
1324                                        n_i<=n+1;
1325                                        tempval:=Ram_data_out;
1326                                        tempval(6):='1';       
1327                                                       
1328                                        dataToram<=tempval;
1329                                        rd_ok<='0';
1330                                        wr_ok<='1';
1331                                else
1332                                        rd_ok<='1';
1333                                        wr_ok<='0';
1334                                end if;
1335                elsif n=4 then
1336                if spawninit='0' then
1337                  Spawn_done_i<='1';
1338                end if;
1339                rd_ok<='0';
1340                wr_ok<='0';
1341                n_i<=0;
1342                Next_stInit2<=EndInit;
1343                end if;
1344                       
1345                When ErrSpawn =>
1346                  assert true
1347                  report "Ex4/stInit2/SpawnApp:Erreur lors de Spawn"
1348                  severity failure;
1349                        Next_stInit2<=Init;
1350                When EndInit=>
1351                 
1352                        StatAsked_i<='0'; --reset de ces variables
1353                        Next_stInit2<=Init;
1354      end case;     
1355   end process;
1356       
1357--================================================
1358--sauvegarde du rang et du main port à l'adresse de retour de la fonction Init
1359--==================================================
1360
1361--====================================================
1362--envoie des données sur le port
1363--===================================================
1364selector<=(Rcv_on,Cmd_on,Snd_on);
1365
1366process (selector,Rport_out_rd_en,cport_out_rd_en,sPort_in_wr_en ,cPort_in_wr_en,
1367                                tosend ,tosend4)
1368begin
1369   case selector is
1370       
1371      when "000" => port_in_data<=(others=>'-');
1372                                                Port_in_wr_en<='0';
1373                                                Port_out_rd_en<='0';
1374     
1375                when "001" => port_in_data<=tosend; --envoie de données
1376                                                Port_in_wr_en<=sPort_in_wr_en ;
1377                                                Port_out_rd_en<='0';
1378      when "101" => port_in_data<=tosend; --envoie de données
1379                                                Port_in_wr_en<=sPort_in_wr_en ;
1380                                                Port_out_rd_en<=Rport_out_rd_en ;
1381                when "010" | "110" | "011" | "111" =>
1382                                                port_in_data<=tosend4; --envoie de commande GetPort
1383                                                Port_in_wr_en<=cPort_in_wr_en;
1384                                                Port_out_rd_en<=cport_out_rd_en;
1385     
1386          when "100" => port_in_data<=(others=>'-'); -- réception de données
1387                                                Port_in_wr_en<='0';
1388                                                Port_out_rd_en<=Rport_out_rd_en ;
1389     
1390                when others => port_in_data<=(others=>'-');
1391                                                        Port_in_wr_en<='0';                                                     
1392                                                        Port_out_rd_en<='0';
1393   end case;
1394end process;
1395 
1396--==================================================   
1397       
1398        --===============================================================
1399        --processus d'accès au réseau
1400        --===============================================================
1401
1402
1403
1404
1405
1406        preceiv:process(clk,reset)
1407       
1408        variable origport,destport : natural range 0 to 15;
1409        variable dcount,dlen ,rtimeout:natural range 0 to 1023;
1410        variable ptype : std_logic_vector(3 downto 0);
1411        variable bad_paquet :std_logic:='0';
1412        begin
1413--                             
1414       
1415        if reset='1' then
1416                etrec<=r_wait;
1417                destport:=1;
1418                rtimeout:=0;
1419                bad_paquet:='0';
1420        else
1421        if  rising_edge(clk) then
1422                        case  etrec is
1423                        when r_wait =>
1424                               
1425                                rtimeout:=0;
1426                                bad_paquet:='0';       
1427                                --modifier le 01-08-pour gérer le Spawn
1428                        if Port_out_data_available='1' and Cmd_on='0' then
1429                                ptype:=port_out_data(7 downto 4);
1430                                origport:=to_integer(unsigned(port_out_data(3 downto 0)));
1431                               
1432                                if ptype=MPI_SPAWN and not(portnumflag='1') then
1433                                                etrec<=r_dlen; --identification de la signature d'en tête valide
1434                                                Datareceived(0)<=Port_out_data; --stocker l'entête
1435                                                bad_paquet:='0';
1436                                                SpawnOn<='1'; --le spawn est actif
1437                                elsif Instruction_en='0' and CTR='0' and Hcl_init= '0' then
1438                                  --il faut éliminer ce paquet qui est inoportun !
1439                                  Bad_paquet:='1';
1440                                  etrec<=r_dlen; --il faut ignorer cette donnée et quitter
1441                                        dcount:=1;
1442                                elsif CTR='1' then
1443                                if ptype=MPI_INIT or ptype=INIT_SETRANK or ptype=INIT_SEEKMAIN or ptype=INIT_REGISTER then
1444                                                etrec<=r_dlen; --identification de la signature d'en tête valide
1445                                                Datareceived(0)<=Port_out_data; --stocker l'entête
1446                                                bad_paquet:='0';
1447                                else
1448                                        --une donnée non attendue est présente sur le port
1449                                        etrec<=r_dlen; --il faut ignorer cette donnée et quitter
1450                                        dcount:=1;
1451                                        bad_paquet:='1';
1452                                        Datareceived(0)<=(others=>'0');
1453                                end if;
1454                                end if;
1455                        --elsif Port_out_data_available='1' and CTR='0' then
1456                        --              etrec<=r_drop;
1457                        else
1458                               
1459                                etrec<=r_wait;
1460                        end if;
1461                        DS_RDY<='0';
1462                        When r_drop => -- ignorer les messages qui arrivent à ce noeud tant que
1463                                                                --l'application n'a pas été initialisée
1464                                                if port_out_data_available='0' then
1465                                                        etrec<=r_end;
1466                                                        dcount:=0;
1467                                                       
1468                                                end if;
1469                                               
1470                                               
1471                                               
1472                                                --Rport_out_rd_en<='1'; --lire les données qui sont dans le tampon de sortie           
1473                                                DS_RDY<='0';
1474                        when r_Dlen =>   --positionnement du mot de longueur des données
1475                        if Port_out_data_available ='1' then
1476                       
1477                        --Rport_out_rd_en<='1';
1478                        etrec<=r_glen;
1479                        rtimeout:=0;
1480                        else
1481                                rtimeout:=rtimeout+1;
1482                                        if rtimeout>=30 then
1483                                          assert true report "Ex4:etrec r_glen Timeout -> Impossible de recevoir du NoC"
1484                                        severity failure;
1485                                        Exectime_out<='0';  --read Noc time out
1486                                                etrec<=r_end;             -- données pas prêtes
1487                                        end if;
1488                       
1489                       
1490                        end if;
1491                        DS_RDY<='0';
1492                        -- 
1493                        when r_glen =>   --lecture effective de la longueur des données
1494                        if port_out_data_available='1' then
1495                        dlen:=to_integer(unsigned(port_out_data(Word-1 downto 0)));
1496                        Datareceived(1)<=Port_out_data;
1497                        --RPort_out_rd_en<='0';
1498                        etrec<=r_data;
1499                        dcount:=2;  -- initialisation du compteur de reception (il y a
1500                                                        -- déjà deux mots reçues
1501                        else
1502                                rtimeout:=rtimeout+1;
1503                                        if rtimeout>=30 then
1504                                         assert true report "Ex4:etrec Timeout -> Impossible de recevoir sur le NoC"
1505                                        severity failure;
1506                                        ExecTime_out<='0';
1507                                                etrec<=r_end;             -- données pas prêtes
1508                                        end if;
1509                       
1510                        end if;
1511                        DS_RDY<='0';
1512                        when r_data =>                                          -- lecture des données
1513                        if port_out_data_available='1'  then
1514                                       
1515                                        --Rport_out_rd_en<='1';   --autoriser la lecture
1516                                        if bad_paquet='0' then
1517                                                DataReceived(dcount)<=Port_out_data; --récupération des données
1518                                        else
1519                                                DataReceived(2)<=(others=>'0');
1520                                          --ignorer ces données
1521                                        end if;
1522                                        --assert true report "Donnée lue :"; --& string(unsigned(port4_out(Word-1 downto 0)))
1523                                        DS_RDY<='0';
1524                                        dcount:=dcount+1;
1525                                        --severity note;
1526                                        if dlen <=dcount then -- ce doit être égale ici et non <= ???
1527                                                etrec<=r_end;
1528                                                DS_RDY<= not bad_paquet; -- jeu de données disponibles
1529                                        elsif dcount=255 then   --dépassement de la capacité
1530                                                DS_RDY<='0';
1531                                                etrec<=r_end;
1532                                        end if;
1533                        else
1534                                        rtimeout:=rtimeout+1;
1535                                        if rtimeout>=30 then
1536                                           ExecTime_out<='0';
1537                                                etrec<=r_end;             -- données pas prêtes
1538                                        end if;
1539                                        DS_RDY<='0';
1540                                       
1541                        end if;
1542                        when r_pulse =>
1543                                        etrec<=r_end;
1544                                        DS_RDY<=not bad_paquet;
1545                        when r_end =>
1546                                               
1547                        --rPort_out_rd_en <='0';
1548                       
1549                        if DS_ACK='1' or rtimeout> 30 or (CTR='0' and SpawnOn='0') or bad_paquet='1' then
1550                                DS_RDY<='0';
1551                                SpawnOn<='0';
1552                                etrec<=r_wait;
1553                        else
1554                                DS_RDY<=DS_RDY;
1555                        end if;                 
1556                        end case;
1557                 end if;--reset='1'
1558                end if;   
1559  end process preceiv;
1560 
1561 val_preceiv: process (etrec)
1562  begin
1563  case etrec is
1564                                        when r_wait  =>
1565--                                              DS_RDY<='0';
1566                                                rcv_on<='0';
1567                                                Rport_out_rd_en <='0';
1568                                        when r_dlen  =>
1569--                                                      DS_RDY<='0';
1570                                                        Rport_out_rd_en <='1';
1571                                                        rcv_On<='1';
1572                                        when r_drop  =>
1573--                                                      DS_RDY<='0';
1574                                                        Rport_out_rd_en <='1';
1575                                                        rcv_On<='1';
1576                                        when r_glen =>
1577                                                        Rport_out_rd_en <='1';
1578--                                                      DS_RDY<='0';
1579                                                        rcv_On<='1';
1580                                        when r_data =>
1581                                                        Rport_out_rd_en <='1';
1582--                                                      DS_RDY<='0';
1583                                                        rcv_On<='1';
1584                                        when r_pulse =>
1585                                                                Rport_out_rd_en <='0';
1586                                                                rcv_On<='1';
1587--                                                              DS_RDY<='1';
1588                                        when r_end =>
1589                                                        Rport_out_rd_en <='0';
1590--                                                      DS_RDY<='0';
1591                                                        rcv_On<='0';
1592                                        when others =>
1593                                                        Rport_out_rd_en <='0';
1594--                                                      DS_RDY<='0';
1595                                                        rcv_On<='0';
1596                                end case;
1597        end process;
1598 
1599  psend:process(clk,reset)
1600                --génération des paquets à partir du  port courant
1601                variable pactype :natural range 0 to 15;
1602                variable destport : natural range 0 to 15:=0;
1603                variable realdlen, i,i_pair : natural range 0 to 255;
1604                variable MaxPort : natural :=4; -- en fait ne doit pas être 0
1605               
1606               
1607                                begin
1608                                if rising_edge(clk) then
1609                                                 if reset='1' then
1610                                                                etsnd<=s_init;
1611                                 
1612                                   
1613                                                 
1614                                                else -- le process s'exécute sur chaque front
1615                                                                                                                        -- montant de l'horloge
1616                                                case etsnd is
1617                                                when s_init => tosend<=(others=>'-');
1618                                                                                        MaxPort :=to_integer(unsigned(NOCMAX));
1619                                                                sPort_in_wr_en<='0';
1620                                                        if port_in_full='0' and Rts_dat='1' then   --on peut aussi tester si le port est vide
1621                                                                BCast_RDY<='0';  -- pas la fin de du Broadcasting
1622                                                                          Send_RDY<='0';
1623                                                                          Snd_on<='1';
1624                                                                          if Bcast='1' then -- envoyer à tous les ports le même message ?
1625                                                                          DestPort :=0;
1626                                                                          else
1627                                                                                DestPort:=to_integer(unsigned(data_to_send(0)(3 downto 0)));
1628                                                                          end if;
1629                                                                          etsnd<=s_head;
1630                                                        end if;
1631                                                when s_head  =>    -- construction et envoie de l'en-tête
1632                                                Send_RDY<='0'; --l'envoi commence
1633                                                BCast_RDY<='0';
1634                                                Snd_on<='1';
1635                                                --pactype:=to_integer(MPI_INIT);--
1636                                                pactype:=to_integer(unsigned(data_to_send(0)(7 downto 4)));
1637                                                --realdlen:=to_integer(unsigned(Datalen));
1638                                                --? destport:=MAXPORT; -- le port de destination
1639                                               
1640                                                tosend <=STD_LOGIC_VECTOR(to_unsigned(pactype,4)) & STD_LOGIC_VECTOR(to_unsigned(destport,4));
1641                                                 sPort_in_wr_en<='1';   --
1642                                                 
1643                                                 i:=1;
1644                                                 etsnd<=s_len2;  -- passer à l'état suivant
1645                                                 
1646                                                when s_len =>
1647                                                        BCast_RDY<='0';
1648                                                        Send_RDY<='0';
1649                                                        Snd_on<='1';
1650                                                        --tosend<=(STD_LOGIC_VECTOR(to_unsigned(Realdlen,8)));
1651                                                        tosend<=data_to_send(1);
1652                                                        sPort_in_wr_en <='1';
1653                                                       
1654
1655                                                        --port1_in<=tosend1; --copie directe sur le port
1656                                                        etsnd<=s_len2;
1657                                                when s_len2 =>
1658                                                        --tosend<=(STD_LOGIC_VECTOR(to_unsigned(realdlen,8)));
1659                                                        sPort_in_wr_en<='1';
1660                                                        tosend<=data_to_send(1);
1661
1662                                                        --port1_in<=tosend1; --copie directe sur le port
1663                                                        etsnd<=s_data; 
1664                                                        BCast_RDY<='0';
1665                                                        Send_RDY<='0';
1666                                                        Snd_on<='1';
1667                                                        i:=i+1;
1668                                                when s_data =>
1669                                                        Snd_on<='1';
1670                                                        if Port_in_full='0' and RTS_dat='1' then
1671                                                                sPort_in_wr_en<='1';
1672                                                               
1673                                                                     --envoie des données sur le port   
1674                                                                        tosend<=data_to_send(i);
1675                                                               
1676                                                               
1677                                                                if i+1>=datalen then
1678                                                                        etsnd<=s_end;
1679                                                                        Send_RDY<='1'; --l'envoi est terminé
1680                                                                        if BCast='1' and destport=MAXPort then
1681                                                                                BCAST_RDY<='1'; --l'envoi collectif aussi
1682                                                                        else
1683                                                                                BCast_RDY<='0';
1684                                                       
1685                                                                        end if;
1686                                                                else
1687                                                                        BCast_RDY<='0';
1688                                                                        Send_RDY<='0';
1689                                                                        i:=i+1;
1690                                                                end if;
1691                                                        elsif port_in_full='1' then
1692                                                                sPort_in_wr_en<='0';
1693                                                        else
1694                                                                sPort_in_wr_en<='0';
1695                                                                BCast_RDY<='0';
1696                                                                Send_RDY<='0';
1697                                                        end if;
1698                                                when s_end  =>
1699                                                 tosend<=(others=>'-');
1700                                                       
1701                                                        sPort_in_wr_en<='0';
1702                                                        if Bcast='1' then
1703                                                        if destPort<Maxport then
1704                                                                DestPort:=Destport+1;
1705                                                                etsnd<=s_head;
1706                                                               
1707                                                                else
1708                                                                BCast_RDY<='1' ; -- BroadCast End=Ok
1709                                                                        if Send_ack='1' then
1710                                                                                etsnd<=s_init;
1711                                                                                Send_RDY<='0';
1712                                                                                Snd_on<='0';
1713                                                                        end if;
1714                                                                end if;
1715                                                        else
1716                                                                BCast_RDY<='0';
1717                                                                if Send_ack='1' then
1718                                                                        etsnd<=s_init;
1719                                                                        Send_RDY<='0';
1720                                                                        Snd_on<='0';
1721                                                                end if;
1722                                                               
1723                                                        end if;
1724                                                end case;       
1725                                               
1726                                                                               
1727                                                end if;  --reset ='1'
1728                                        end if; --rising_edge...
1729                end process psend;     
1730
1731-- envoi des commandes         
1732pcmd:process(clk,reset)
1733       
1734        variable origport,destport,pid,mport : natural range 0 to 15;
1735        variable ctimeout:natural range 0 to 255;
1736        begin
1737       
1738       
1739                if reset='1' then
1740                        etcmd<=cmdstart;
1741                       
1742        --              sorigport<=origport;
1743                else
1744                if  rising_edge(clk) then
1745                                        case  etcmd is
1746                                        when cmdstart =>
1747                                                if Port_in_empty='1' and RTS_Cmd='1' then
1748                                                                        etcmd<=cmdpost;
1749                                               
1750                                                end if;
1751                                                destport:=0;
1752                                                ctimeout:=0;
1753                                                origport:=1;
1754                                 when cmdpost =>
1755                                        if Port_in_empty='1' then
1756                                                etcmd<=cmdread;
1757                                        end if;
1758                                       
1759                                when cmdpostidle  =>   --permet juste la prise en compte de la commande
1760                                                etcmd<=cmdread;
1761                                                dcount<=0;-- initialisation du compteur de reception
1762                                 when cmdread =>
1763                                               
1764                                               
1765                                                ctimeout:=0;
1766                                               
1767                                        if Port_out_data_available='1' then
1768                                                mport:=to_integer(unsigned(port_out_data(7 downto 4)));
1769                                                pid:=to_integer(unsigned(port_out_data(3 downto 0)))+1;
1770                                                CmdReceived(dcount)<=port_out_data;
1771                                                --cdata_out_en(origport)<='1';
1772--                                              if pid=origport then --le port a été bien identifié
1773                                                                etcmd<=cmdglen; --
1774                                                                dcount<=dcount+1;
1775--                                                      else
1776--                                                      etcmd<=cmdtimeout;
1777--                                              end if;
1778                                        else
1779
1780                                                etcmd<=cmdread;
1781                                        end if;
1782                                       
1783                                       
1784                                        when cmdlen =>   --positionnement du mot de longueur des données
1785                                        if Port_out_data_available='1' then
1786                                       
1787                                        etcmd<=cmdglen;
1788                                        ctimeout:=0;
1789                                        else
1790                                                ctimeout:=ctimeout+1;
1791                                                        if ctimeout>=30 then
1792                                                          assert true report "Ex4:etcmd Timeout -> impossible de recevoir du le NoC"
1793                                        severity failure;
1794                                                                etcmd<=cmdtimeout;             -- données pas prêtes
1795                                                        end if;
1796                                       
1797                                       
1798                                        end if;
1799                                        when cmdglen =>   --lecture effective de la longueur des données
1800                                        if Port_out_data_available='1' then
1801                                                cdlen<=to_integer(unsigned(port_out_data(Word-1 downto 0)));
1802                                                CmdReceived(dcount)<=port_out_data;
1803                                                etcmd<=cmddata;
1804                                          dcount<=dcount+1;
1805                                        else
1806                                                ctimeout:=ctimeout+1;
1807                                                        if ctimeout>=30 then
1808                                                        --time_out(destport)<='1';
1809                                                         assert true report "Ex4:etcmd Timeout -> impossible de recevoir du le NoC"
1810                                        severity failure;
1811                                                                etcmd<=cmdtimeout;             -- données pas prêtes
1812                                                        end if;
1813                                       
1814                                        end if;
1815                                       
1816                                        when cmddata =>
1817                                        if (port_out_data_available='1' and ctimeout<30) then
1818                                                       
1819                                                        --cdata_out_en(origport)<='1';
1820                                                        mport:=to_integer(unsigned(port_out_data(7 downto 4)));
1821                                                        --attention les ports sont numérotés à partir de 0
1822                                                        pid:=to_integer(unsigned(port_out_data(3 downto 0)))+1;
1823                                                        CmdReceived(dcount)<=port_out_data;
1824                                                        if (dcount>=cdlen-1 ) then--attention le compteur de données commence à 0
1825                                                                etcmd<=cmdend;
1826                                                                --CM_RDY<='1';
1827                                                        else
1828                                                                dcount<=dcount+1;
1829                                                        end if;                 
1830                                        Else
1831                                                       
1832                                                        if ctimeout>=30 then
1833                                                                --time_out(destport)<='1';
1834                                                                etcmd<=cmdtimeout;             -- données pas prêtes
1835                                                         assert true report "Ex4:etcmd. cmddata Timeout -> impossible de recevoir du le NoC"
1836                                        severity failure;
1837                                                        else
1838                                                        ctimeout:=ctimeout+1;
1839                                                        etcmd<=cmdend;
1840                                                        end if;
1841                                        end if;
1842                                        when cmdend =>
1843                               
1844                                                        etcmd<=cmdstart;
1845
1846                                        when cmdtimeout =>
1847
1848                                          etcmd<=cmdstart;
1849                                        end case;
1850                --sorigport<=origport; 
1851                end if;   --reset='1'
1852        end if;
1853  end process pcmd;     
1854
1855majetcmd: process (etcmd,data_to_send, port_out_data_available, dcount,port_out_data)
1856variable origport : natural;
1857variable i:natural:=0;
1858        begin
1859                        case  etcmd is
1860                when cmdstart =>
1861                                i:=0;
1862                                Port_in_cmd_en<='0';
1863                                tosend4<=(others=>'-');
1864                                cport_out_rd_en<='0';
1865                                cport_in_wr_en<='0';
1866                                CM_RDY<='0';
1867                                Cmd_on<='0';
1868                 when cmdpost | cmdpostidle =>
1869                                cport_in_wr_en<='1';
1870                                tosend4<=data_to_send(0); ---code pour getportid
1871                                Port_in_cmd_en<='1';
1872                                cport_out_rd_en<='0';
1873                                CM_RDY<='0';
1874                                Cmd_on<='1';
1875                 when cmdread =>
1876                                tosend4<=(others=>'-');
1877                                cPort_in_wr_en<='0';
1878                                cport_out_rd_en<=port_out_data_available;
1879                                --CmdReceived(dcount)<=port_out_data;  --mettre les données dans le tampon
1880                                Port_in_cmd_en<='1';
1881                                CM_RDY<='0';
1882                                Cmd_on<='1';
1883                when cmdlen |cmdglen =>   --positionnement du mot de longueur des données
1884                                        tosend4<=(others=>'-');
1885                                        cport_in_wr_en<='0';
1886                                        cport_out_rd_en<=port_out_data_available;
1887                                        Port_in_cmd_en<='1';
1888                                        --CmdReceived(dcount)<= port_out_data;
1889                                        CM_RDY<='0';
1890                                        Cmd_on<='1';
1891                when cmddata =>
1892                                        tosend4<=(others=>'-');
1893                                        cport_in_wr_en<='0';
1894                                        cport_out_rd_en<=Port_out_data_available;
1895                                        Port_in_cmd_en<='1';
1896                                        --CmdReceived(dcount)<=port_out_data;  --mettre les données dans le tampon
1897                                        i:=i+1;
1898                                        CM_RDY<='0';
1899                                        Cmd_on<='1';
1900                        when cmdend =>
1901                                        tosend4<=(others=>'-');
1902                                cport_in_wr_en<='0';
1903                                cport_out_rd_en<='0';
1904                                --CmdReceived(dcount)<=port_out_data;
1905                                Port_in_cmd_en<='0';
1906                            CM_RDY<='1';
1907                                 Cmd_on<='0';
1908                        when cmdtimeout =>
1909                                        tosend4<=(others=>'-');
1910                                  cport_in_wr_en<='0';
1911                                  cport_out_rd_en<='0';
1912                                  Port_in_cmd_en<='0';
1913                                        Cmd_on<='1';
1914                                        cm_rdy<='0';
1915                        end case;
1916               
1917end process majetcmd ;
1918
1919i_send_val:process(HCL_Init,Send_RDY,RTS_I,Snd_Ack,I_send_ack,data_to_send,Bcast_rdy,Bcast)
1920begin
1921snd_start_i<=snd_start;
1922RTS_DAT<='0';
1923
1924if HCL_Init='1' then
1925 if RTS_I='1' and snd_ack='0' then
1926  snd_start_i<='1';
1927 elsif i_send_ack='1' then
1928 
1929 snd_start_i<='0';
1930 end if;
1931  i_send_rdy<=snd_ack;
1932
1933  Snd_data<=data_to_send;
1934else
1935  if RTS_I='1' and i_send_ack='0' then
1936  RTS_DAT<='1';
1937 elsif i_send_ack='1' then
1938 
1939 RTS_DAT<='0';
1940 end if;
1941 i_send_rdy<=send_rdy;
1942 for i in 0 to 3 loop
1943 Snd_data(i)<=(others=>'0');
1944 end loop;
1945end if;
1946end process;
1947end Behavioral;
1948
Note: See TracBrowser for help on using the repository browser.