source: PROJECT_CORE_MPI/MPI_HCL/BRANCHES/v2.1/CORE_MPI/EX4_FSM.vhd @ 146

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