ec907bccc9df1af7c5fe1ab0506ad91db913ceba
[scilab.git] / scilab / modules / scicos / macros / scicos_scicos / c_pass2.sci
1 //  Scicos
2 //
3 //  Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 //
19 // See the file ../license.txt
20 //
21
22 function cpr=c_pass2(bllst,connectmat,clkconnect,cor,corinv,flag)
23 // cor    ; correspondance table with initial block ordering
24 //
25 // bllst: list with nblk elts where nblk denotes number of blocks.
26 //        Each element must be a list with 16 elements:
27 //          1- a list containing function name and function type
28 //          2- vector of number of inputs
29 //          3- vector of number of ouputs
30 //          4- vector of number of clock inputs 
31 //          5- vector of number of clock outputs
32 //          6- vector (column) of continuous initial condition
33 //          7- vector (column) of discrete initial condition
34 //          8- vector (column) of real parameters
35 //          9- vector (column) of integer parameters
36 //          10- string: 'l' for synchro (ifthenelse,eselect) or 'm'
37 //          (memo) or 'x' for blocks that need to be called during
38 //          integration even in the absence of state (only if with workspace)
39 //          11- vector of size <number of clock outputs> including
40 //              preprogrammed event firing times (<0 if no firing) 
41 //          12- boolean vector (column): 1:nin entry for dependence on u,
42 //          last on t 
43 //          13- block label
44 //          14- number of zero crossings
45 //          15- number of modes
46 //          16- empty list (equation for modelica blocks)
47 //
48 // connectmat: nx4 matrix. Each row contains, in order, the block
49 //             number and the port number of an outgoing scicopath,
50 //             and the block number and the port number of the target
51 //             ingoing scicopath.
52 //
53 // clkconnect: same as connectmat but for clock scicopaths.
54 //
55 // define some constants
56   if argn(2) <6 then flag="verbose",end
57   show_trace=%f
58   if show_trace then mprintf('c_pass1:\t%f\n', timer()),end
59
60   show_pause=%f;
61   show_comment=%f;
62
63   if bllst==list() then
64     messagebox(_('No block can be activated'),"modal","error")
65     cpr=list()
66     ok=%f;
67     return
68   end
69   
70   //correction of clkconnect.. Must be done before
71   clkconnect(find(clkconnect(:,2)==0),2)=1;
72   
73
74
75
76   if exists('%scicos_solver')==0 then %scicos_solver=0,end
77
78   clkptr=1,cliptr=1,typ_l=[],
79   nblk=size(bllst)
80
81   //take care of the heritage
82   [bllst,inplnk,outlnk,clkptr,cliptr,inpptr,outptr,dep_u,dep_uptr,dep_t,..
83    typ_l,typ_r,typ_m,tblock,typ_cons,typ_zx,ok]=mini_extract_info(bllst,..
84                                                 connectmat,clkconnect)
85   if show_trace then mprintf('c_pass20:\t%f\n', timer()),end
86   if ~ok then 
87       cpr=list()
88       return
89   end
90   [outoin,outoinptr]=conn_mat(inpptr,outptr,inplnk,outlnk)
91   
92   [critev]=critical_events(connectmat,clkconnect,dep_t,typ_r,..
93                            typ_l,typ_zx,outoin,outoinptr,clkptr)
94   [clkconnect,exe_cons]=pak_ersi(connectmat,clkconnect,typ_r,..
95                                  typ_l,outoin,outoinptr,tblock,typ_cons,clkptr)
96
97
98   [ordclk,ordptr,cord,typ_l,clkconnect,connectmat,bllst,dep_t,dep_u,..
99    dep_uptr,corinv,clkptr,cliptr,critev,ok]=paksazi2(typ_l,clkconnect,..
100         connectmat,bllst,dep_t,dep_u,dep_uptr,corinv,clkptr,cliptr,critev)
101
102   if ~ok then 
103     cpr=list()
104     return
105   end
106
107   if show_pause then 
108     mprintf('fin de paksazi')
109     pause
110   end
111
112   if show_trace then mprintf('c_pass31:\t%f\n', timer()),end
113
114   //extract various info from bllst
115   [lnksz,lnktyp,inplnk,outlnk,clkptr,cliptr,inpptr,outptr,xptr,zptr,..
116    ozptr,typ_mod,rpptr,ipptr,opptr,xc0,xcd0,xd0,oxd0,rpar,..
117    ipar,opar,typ_z,typ_x,typ_m,funs,funtyp,initexe,labels,..
118    bexe,boptr,blnk,blptr,ok]=extract_info(bllst,connectmat,clkconnect,typ_l);
119   typ_z0=typ_z;
120  
121
122   
123   if ~ok then
124     messagebox(_('Problem in port size or type.'),"modal","error");
125     cpr=list()
126     return,
127   end
128
129   if show_trace then mprintf('c_pass41:\t%f\n', timer()),end
130  
131   //form a matrix which gives destinations of each block
132   [outoin,outoinptr]=conn_mat(inpptr,outptr,inplnk,outlnk)
133   [evoutoin,evoutoinptr]=synch_clkconnect(typ_l,clkconnect)
134   //
135   if show_trace then mprintf('c_pass50:\t%f\n', timer()),end
136   
137   [execlk_cons]=discard(clkptr,cliptr,clkconnect,exe_cons)
138
139   clkconnect=[];exe_cons=[]
140
141   if show_trace then mprintf('c_pass501:\t%f\n', timer()),end
142
143   // Set execution scheduling tables 
144   [ordclk,iord,oord,zord,typ_z,ok]=scheduler(inpptr,outptr,clkptr,execlk_cons,..
145    outoin,outoinptr,evoutoin,evoutoinptr,typ_z,typ_x,typ_l,bexe,boptr,blnk,blptr,..
146    ordclk,ordptr,cord,dep_t,dep_u,dep_uptr);
147
148   if ~ok then 
149     cpr=list()
150     return,
151   end
152
153   if show_trace then mprintf('c_pass51:\t%f\n', timer()),end
154   //form scicos arguments
155
156   nb=size(typ_z,'*');
157   zcptr=ones(nb+1,1);
158   modptr=ones(nb+1,1);
159
160   for i=1:nb
161     zcptr(i+1)=zcptr(i)+typ_z(i)
162     modptr(i+1)=modptr(i)+sign(typ_z(i))*typ_mod(i);
163   end
164
165   ztyp=sign(typ_z0)  //completement inutile pour simulation
166                      // utiliser pour la generation de code
167         
168   if xptr($)==1 & zcptr($)>1 then
169     mess=msprintf(_('No continuous-time state. Thresholds are ignored; this \n'+..
170                 'may be OK if you don''t generate external events with them.\n'+..
171                 'If you want to reactivate the thresholds, the you need\n\n'+..
172                 'to include a block with continuous-time state in your diagram.\n'+..
173                 'You can for example include DUMMY CLSS block (linear palette).'))
174     messagebox(mess,"modal","error");
175   end
176
177   subscr=[]
178   ncblk=0;nxblk=0;ndblk=0;ndcblk=0;
179   sim=scicos_sim(funs=funs,xptr=xptr,zptr=zptr,ozptr=ozptr,..
180                  zcptr=zcptr,inpptr=inpptr,outptr=outptr,..
181                  inplnk=inplnk,outlnk=outlnk,rpar=rpar,rpptr=rpptr,..
182                  ipar=ipar,ipptr=ipptr,opar=opar,opptr=opptr,..
183                  clkptr=clkptr,ordptr=ordptr,execlk=ordclk,..
184                  ordclk=ordclk,cord=cord,oord=oord,zord=zord,..
185                  critev=critev(:),nb=nb,ztyp=ztyp,nblk=nblk,..
186                  ndcblk=ndcblk,subscr=subscr,funtyp=funtyp,..
187                  iord=iord,labels=labels,modptr=modptr);
188
189   //initialize agenda
190   [tevts,evtspt,pointi]=init_agenda(initexe,clkptr)
191   if show_trace then mprintf('c_pass61:\t%f\n', timer()),end
192
193   //mod=0*ones(modptr($)-1,1)
194
195   outtb=list();
196   outtb=buildouttb(lnksz,lnktyp);
197
198   iz0=zeros(nb,1);
199
200   if max(funtyp)>10000 &%scicos_solver==0 then
201     warning(_('Diagram contains implicit blocks, compiling for implicit Solver.'))
202     %scicos_solver=100
203   end
204   if %scicos_solver==100 then xc0=[xc0;xcd0],end
205   state=scicos_state()
206   state.x=xc0
207   state.z=xd0
208   state.oz=oxd0
209   state.iz=iz0
210   state.tevts=tevts
211   state.evtspt=evtspt
212   state.pointi=pointi
213   state.outtb=outtb
214   //  state.mod=mod
215
216   cpr=scicos_cpr(state=state,sim=sim,cor=cor,corinv=corinv);
217
218   if show_trace then mprintf('c_pass71:\t%f\n', timer()),end
219
220 endfunction
221
222
223 //donne les sources d'activation du sch�ma
224 function [vec_clk]=get_clocks(clkconnect,clkptr)
225   vec_clk=[]
226   if (find(clkconnect(:,1)==0) ~=[]) then
227     //activation continue
228     vec_clk=[vec_clk;0 0];
229   end
230   for blk=1:size(clkptr,1)-1       
231     if ~typ_l(blk) then
232       for port=1:clkptr(blk+1)-clkptr(blk)
233         vec_clk=[vec_clk; blk port];           
234       end
235     end
236   end
237 endfunction
238
239
240
241 //insere le vecteur primary dans vec_clk apr�s la ligne comptenant le bloc i
242 function vec_clk0=set_primary_clk(vec_clk,primary,i)
243
244   if vec_clk~=[] then
245     n_vc0=find(vec_clk==i)
246     n_vc0=n_vc0($)
247     vec_clk0=vec_clk(1:n_vc0)
248     vec_clk1=vec_clk(n_vc0+1:size(vec_clk,1))
249     for k=1:size(primary,1)
250       if find(primary(k)==vec_clk0)==[] then
251         vec_clk0($+1)=primary(k)
252       end
253     end
254     for k=1:size(vec_clk1,1)
255       if find(vec_clk1(k)==vec_clk0)==[] then
256         vec_clk0($+1)=vec_clk1(k)
257       end
258     end
259   else
260     vec_clk0=primary
261   end
262
263 endfunction
264
265 //insere la sous-matrice primary dans vec_clk apr�s la ligne k
266 function vec_clk0=set_primary_clkport(vec_clk,primary,i)
267
268   if vec_clk~=[] then
269     vec_clk0=vec_clk(1:i,:)
270     vec_clk1=vec_clk(i+1:size(vec_clk,1),:)
271     for k=1:size(primary,1)
272       f=find(primary(k,1)==vec_clk0(:,1))
273       if f==[] then
274         vec_clk0=[vec_clk0;primary(k,:)]
275       end
276     end
277     n_vc1=size(vec_clk1,1)
278     if n_vc1>0 then
279       for k=1:n_vc1
280         f=find(vec_clk1(k,1)==vec_clk0(:,1))
281         if f==[] then
282           vec_clk0=[vec_clk0;vec_clk1(k,:)]
283         end
284       end
285     end
286   else
287     vec_clk0=primary
288   end
289   
290 endfunction
291
292 //insere la sous-matrice ordoclk0 dans ordclk apr�s le block k
293 function [ordptr,ordclk,blocs_traites]=set_ordclk(ordclk,..
294                                                   ordoclk0,k,j,ordptr,blocs_traites)
295   if ordoclk0~=[] then
296     if ordclk==[] then
297       ordclk=[ordclk;ordoclk0];
298       ordptr($+1)=ordptr($)+size(ordoclk0,1);
299       blocs_traites=[blocs_traites;k] //k
300     else
301       m=max(find(blocs_traites==k))
302       if j>1 & m~=[] then
303         ordclk=[ordclk(1:ordptr(m+1)-1,:);ordoclk0;ordclk(ordptr(m+1):$,:)]
304         ordptr=[ordptr(1:m+1);ordptr(m+1:$)+size(ordoclk0,1)]
305         blocs_traites=[blocs_traites(1:m);k;blocs_traites(m+1:$)]
306       else
307         ind=find(blocs_traites <k)
308         m=max(ind)
309         if m==size(blocs_traites,1) then
310           ordclk=[ordclk;ordoclk0]
311           ordptr($+1)=ordptr($)+size(ordoclk0,1);
312           blocs_traites=[blocs_traites;k]
313         else
314           if m~=[] then
315             ordclk=[ordclk(1:ordptr(m+1)-1,:);ordoclk0;ordclk(ordptr(m+1):$,:)]
316             ordptr=[ordptr(1:m+1);ordptr(m+1:$)+size(ordoclk0,1)]
317             blocs_traites=[blocs_traites(1:m);k;blocs_traites(m+1:$)]
318           else
319             ordclk=[ordoclk0;ordclk]
320             ordptr=[1;ordptr+size(ordoclk0,1)]
321             blocs_traites=[k;blocs_traites]
322           end
323         end
324       end
325     end
326   else
327     if j>1 & find((blocs_traites==k))~=[] then
328       m=max(find(blocs_traites==k))
329       ordptr=[ordptr(1:m+1);ordptr(m+1:$)+size(ordoclk0,1)]
330       blocs_traites=[blocs_traites(1:m);k;blocs_traites(m+1:$)]
331     else
332       ind=find(blocs_traites <k)
333       m=max(ind)
334       if m==size(blocs_traites,1) then
335         ordptr($+1)=ordptr($)+size(ordoclk0,1);
336         blocs_traites=[blocs_traites;k]
337       else
338         if m~=[] then
339           ordptr=[ordptr(1:m+1);ordptr(m+1:$)+size(ordoclk0,1)]
340           blocs_traites=[blocs_traites(1:m);k;blocs_traites(m+1:$)]
341         else
342           ordptr=[1;ordptr+size(ordoclk0,1)]
343           blocs_traites=[k;blocs_traites]
344         end
345       end
346     end
347   end
348 endfunction
349
350
351 //donne les blocs activant blk
352 function [parents]=get_parents(parents,blk)
353   f=find(clkconnect(:,3)==blk)
354   n_f=size(f,2)
355   if n_f>0 then
356     for i=1:n_f
357       clki1=clkconnect(f(i),1)
358       clki2=clkconnect(f(i),2)
359       g=find(clki1==parents(:,1))
360       if g==[] | (g~=[] & parents(g,2)~=clki2) then
361         parents=[parents;clki1,clki2]
362       end
363     end
364   end
365 endfunction
366
367
368 function [blks,blksptr]=depend_on(connectmat,dep_u,dep_uptr)
369 // returns the blocks on which depend directly  a block
370   blks=[];blksptr=1
371   for i=1:size(dep_uptr,'*')-1
372     indport=find(dep_u(dep_uptr(i):dep_uptr(i+1)-1));
373     f=[]
374     for j=indport
375       f=[f,find(connectmat(:,3)==i&connectmat(:,4)==j)]
376     end
377     blkis=unique(connectmat(f,1))
378     blks=[blks;blkis]
379     blksptr=[blksptr;blksptr($)+size(blkis,'*')]
380   end
381 endfunction
382
383 function [eblks,eblksptr]=update_event_depend_on(eblks,eblksptr,clkconnect,cliptr,bls)
384 // updates the blocks activating a block
385   nb1=size(cliptr,'*')
386   eblksptr($:nb1)=eblksptr($)
387   for i=bls
388     f=find(clkconnect(:,3)==i)
389     blkis=unique(clkconnect(f,1))
390     dblkis=size(blkis,'*')-(eblksptr(i+1)-eblksptr(i))
391     Ibef=eblksptr(1):eblksptr(i)-1;
392     Iaft=eblksptr(i+1):eblksptr($)-1;
393     eblks=[eblks(Ibef);blkis;eblks(Iaft)]
394     eblksptr(i+1:$)=eblksptr(i+1:$)+dblkis
395   end
396 endfunction
397
398
399 function [fblks,fblksptr]=update_event_activates(fblks,fblksptr,clkconnect,clkptr,bls)
400 // returns the blocks activated by a block (assumed typ_l)
401   nb1=size(cliptr,'*')
402   fblksptr($:nb1)=fblksptr($)
403   for i=bls
404     f=find(clkconnect(:,1)==i)
405     blkis=unique(clkconnect(f,3))
406     dblkis=size(blkis,'*')-(fblksptr(i+1)-fblksptr(i))
407     Ibef=fblksptr(1):fblksptr(i)-1;
408     Iaft=fblksptr(i+1):fblksptr($)-1;
409     fblks=[fblks(Ibef);blkis;fblks(Iaft)]
410     fblksptr(i+1:$)=fblksptr(i+1:$)+dblkis
411   end
412 endfunction
413
414
415 function [eblks,eblksptr]=event_depend_on(clkconnect,cliptr)
416 // returns the blocks activating a block
417   eblks=[];eblksptr=1
418   for i=1:size(cliptr,'*')-1
419     f=find(clkconnect(:,3)==i)
420     blkis=unique(clkconnect(f,1))
421     eblks=[eblks;blkis]
422     eblksptr=[eblksptr;eblksptr($)+size(blkis,'*')]
423   end
424 endfunction
425
426
427 function [fblks,fblksptr]=event_activates(clkconnect,clkptr)
428 // returns the blocks activated by a block of type typ_l
429   fblks=[];fblksptr=1
430   for i=1:size(typ_l,'*')
431     if typ_l(i) then
432       f=find(clkconnect(:,1)==i)
433       blkis=unique(clkconnect(f,3))
434       fblks=[fblks;blkis]
435     else
436       blkis=[]
437     end
438     fblksptr=[fblksptr;fblksptr($)+size(blkis,'*')]
439   end
440 endfunction
441
442
443 function uni=merge_mat(m1,m2)
444 // for  m1 and m2 with two columns containing >=0 values
445   uni=[m1;m2]
446   n=max(uni(:,2))+1;
447   [j,ind]=unique(uni(:,1)*n+uni(:,2))
448   uni=uni(-gsort(-ind),:)
449 endfunction
450
451 function [typ_l,clkconnect,connectmat,vbllst,dep_t,dep_u,dep_uptr,..
452           corinv,clkptr,cliptr,critev]=duplicate_block(bl,typ_l,clkconnect,..
453           connectmat,vbllst,dep_t,dep_u,dep_uptr,corinv,clkptr,cliptr,critev)
454
455   nblock=size(typ_l,1)+1
456   g=find(clkconnect(:,1)==bl)
457   if g<> [] then
458     xx=clkconnect(g,:)
459     xx(:,1)=nblock
460     clkconnect=[clkconnect;xx]
461   end
462   
463   g=find(connectmat(:,3)==bl)
464   xx=connectmat(g,:)
465   xx(:,3)=nblock*ones(size(xx,1),1)
466   connectmat=[connectmat;xx]
467   
468   typ_l($+1)=%t
469   critev=[critev;critev(clkptr(bl):clkptr(bl+1)-1)]
470   vbllst(nblock)=vbllst(bl)
471   dep_t(nblock)=dep_t(bl)
472   dep_u=[dep_u;dep_u(dep_uptr(bl))]
473   dep_uptr($+1)=dep_uptr($)+1
474   
475   corinv(nblock)=corinv(bl)
476   clkptr(nblock+1)=clkptr(nblock)+clkptr(bl+1)-clkptr(bl)
477   cliptr(nblock+1)=cliptr(nblock)+cliptr(bl+1)-cliptr(bl)
478   
479 endfunction
480
481 function [childs]=get_allchilds(primary,fblks,fblksptr,typ_l)
482
483   oldtaille=0
484   childs=primary(:)'
485   taille=size(childs,'*')
486
487   while oldtaille<>taille
488     oldtaille=taille
489     for i=childs(typ_l(childs))
490       bb=fblks(fblksptr(i):fblksptr(i+1)-1)
491       childs=union(childs,bb)
492     end
493     taille=size(childs,'*')
494   end
495 endfunction
496
497
498 function ok=is_alg_event_loop(typ_l,clkconnect)
499   clkconnect(find(clkconnect(:,1)==0),:)=[]
500   lclkconnect=clkconnect(typ_l(clkconnect(:,1))&typ_l(clkconnect(:,3)),[1,3])
501   n=size(typ_l,'*')
502   oldvec=zeros(n,1)
503   vec(lclkconnect(:,1))=1
504   i=0
505   ok=%f
506   while i<=n
507     i=i+1
508     oldvec=vec  // not optimal to use large v
509     vec(lclkconnect(:,2))=vec(lclkconnect(:,1))+1
510     if and(vec==oldvec) then
511       ok=%t
512       return
513     end
514   end  
515 endfunction
516
517 function [ordclk,ordptr,cord,typ_l,clkconnect,connectmat,bllst,dep_t,dep_u,dep_uptr,corinv,clkptr,cliptr,critev,ok]=paksazi2(typ_l,clkconnect,connectmat,bllst,dep_t,dep_u,dep_uptr,corinv,clkptr,cliptr,critev)
518
519   ordclk=[];ordptr=1;cord=[];
520   lordclk=list()
521
522   vbllst=1:length(bllst)
523
524   zz=get_clocks(clkconnect,clkptr)  
525   //testing event algebraic loops
526   ok=is_alg_event_loop(typ_l,clkconnect)
527   if ~ok then
528     disp(mprintf("%s: alg_event_loop failed", "c_pass2"));
529     messagebox(_('Algebraic loop on events.'),"modal","error");
530     return
531   end
532   
533
534   ok=%t
535   [fblks,fblksptr]=event_activates(clkconnect,clkptr)
536   [blks,blksptr]=depend_on(connectmat,dep_u,dep_uptr)
537   [eblks,eblksptr]=event_depend_on(clkconnect,cliptr)
538   
539
540   for i=1:size(zz,1)
541
542     nblock=size(typ_l,1);  // number of blocks in diagram 
543
544     todo=zz(i,:)
545     while todo<>[]
546
547       blk=todo(1,1);port=todo(1,2);
548       if blk==0 |find(definedfields(lordclk)==clkptr(blk)+port-1)==[] then
549
550         f=find(clkconnect(:,1)==blk&clkconnect(:,2)==port);
551         if f<>[] then
552           primary0=clkconnect(f,3);prt0=clkconnect(f,4)
553         else
554           primary0=[]
555         end
556
557         if  show_comment then mprintf("Processing blk " + string(blk) + " port "+...
558                                    + string(port) + "\n"),end
559           
560           if primary0<>[] then  // delete redundant links
561             [jj,k]=unique(primary0*(1+max(prt0))+prt0)
562             index=setdiff(1:size(primary0,1),k) 
563             clkconnect(f(index),:)=[]  // does not affect e(f)blks
564             
565             primary=unique(primary0)
566
567             [balg,vec]=ini_ordo3(primary)
568             if balg then 
569               disp(mprintf("%s: ini_ordo (3) failed", "c_pass2"));
570               messagebox(_('Algebraic loop.'),"modal","error"),
571               ok=%f
572               return
573             end  
574             
575             pvec=vec(primary)+.5*typ_l(primary) // same order typ_l to the right
576             [mi,In]=gsort(-pvec)
577             primary=primary(In)
578             lp=find(typ_l(primary))
579             lprimary=primary(lp)
580
581             bouclalg=%f
582             lp_backw=lp($:-1:1)
583             for i=lp_backw
584                for J=primary(i+1:$)'
585                  if intersect(get_allchilds(primary(i)),get_allchilds(J))<>[] then
586                       bouclalg=%t
587                       break
588                  end
589                end
590                if bouclalg then break,end
591             end
592    
593             if show_comment&bouclalg then mprintf('found intersect \n'),end
594
595             if ~bouclalg then
596               [bouclalg,Vec,typ_l,clkconnect,connectmat,bllst,dep_t,dep_u,..
597                 dep_uptr,corinv,clkptr,cliptr,critev]=ini_ordo2(primary,..
598                 clkconnect,connectmat,bllst,typ_l,dep_t,dep_u,dep_uptr,..
599                 corinv,clkptr,cliptr,critev)
600               if bouclalg then
601                 if show_comment then mprintf('found non convergence\n'),pause,end
602                 i=lp(1)  // first typ_l
603                 if i==[] then 
604                   disp(mprintf("%s: ini_ordo (2) failed", "c_pass2"));
605                   messagebox(_('Algebraic loop.'),"modal","error")
606                   ok=%f
607                   return
608                 end
609                 J=primary(i+1:$)
610               end
611             end
612
613             if bouclalg then
614               modif=%f
615               bl=primary(i)
616
617               nout=clkptr(bl+1)-clkptr(bl)
618               //duplicate bl if it receives other links
619               f=find(clkconnect(:,1)==blk&clkconnect(:,2)==port&clkconnect(:,3)==bl)
620               h=find(clkconnect(:,3)==bl)  // this is always one
621               if size(f,2)<>size(h,2) then
622                 nblock=nblock+1
623                 clkconnect(f,3)=nblock
624                 if  show_comment then   
625                     mprintf('duplicating pivot'+string(bl)+' to obtain '+string(nblock) + "\n"),
626                 end
627                 [typ_l,clkconnect,connectmat,vbllst,dep_t,dep_u,dep_uptr,..
628                  corinv,clkptr,cliptr,critev]=duplicate_block(bl,typ_l,clkconnect,..
629                         connectmat,vbllst,dep_t,dep_u,dep_uptr,corinv,clkptr,cliptr,critev)
630                 vec(nblock)=vec(bl)
631                 //update pointer
632                 blks_bl=blks(blksptr(bl):blksptr(bl+1)-1)
633                 blks=[blks;blks_bl]
634                 blksptr($+1)=blksptr($)+size(blks_bl,'*')
635                 //
636                 bls=[bl,nblock,fblks(fblksptr(bl):fblksptr(bl+1)-1)']
637                 [eblks,eblksptr]=..
638                    update_event_depend_on(eblks,eblksptr,clkconnect,cliptr,bls)
639                 bl=nblock  // use new block as pivot
640                 lprimary(i)=bl
641               end
642
643               for bli=J(:)'         
644                 modif=%t
645                 f=find(clkconnect(:,1)==blk&clkconnect(:,2)==port&clkconnect(:,3)==bli)
646                 clkconnect(f,1)=bl
647                 clkconnect(f,2)=1
648                 xx= clkconnect(f,:)
649                 for jj=2:nout
650                     if  show_comment then  
651                        mprintf('No block duplication but link between '+string(blk)+..
652                             ' and '+string(bli)+' replaced with links from '+..
653                                                  string(bl)+' to '+string(bli) + "\n"),
654                     end
655                     xx(:,2)=jj;
656                     clkconnect=[clkconnect;xx]
657                 end
658               end
659
660               [eblks,eblksptr]=update_event_depend_on(eblks,eblksptr,clkconnect,cliptr,J(:)')
661               bls=[blk,bl];bls(find(bls==0))=[];bls=bls(typ_l(bls)); //exclude non typ_l and 0
662               [fblks,fblksptr]=update_event_activates(fblks,fblksptr,clkconnect,clkptr,bls)
663                         
664               // can (must) be done outside the for loop
665             //  [fblks2,fblksptr2]=event_activates(clkconnect,clkptr)
666             //  [eblks2,eblksptr2]=event_depend_on(clkconnect,cliptr)
667               //but then it must be updated
668             //  if norm(eblksptr-eblksptr2)>0 then mprintf('roro2');pause,end   
669             //  if norm(fblksptr-fblksptr2)>0 then mprintf('soso2');pause,end   
670             //  if norm(eblks-eblks2)>0 then mprintf('roro');pause,end  
671             //  if norm(fblks-fblks2)>0 then mprintf('soso');pause,end  
672
673               
674               if ~modif then messagebox(_('Algebraic loop.'),"modal","error"),ok=%f,return,end
675             else
676               primary0=primary0(k);
677               prt0=prt0(k)
678               [primary0,prt0]=aggregate(primary0,prt0)
679               [mi,In]=gsort(-Vec(primary0))
680               if blk<>0 then
681                 lordclk(clkptr(blk)+port-1)=[primary0(In),prt0(In)]
682
683                 if  show_comment then  
684                       mprintf('for blk port '+string(blk)+' '+string(port)+..
685                                ' ordclk is :\n'), disp(lordclk(clkptr(blk)+port-1)),
686                 end
687               else
688                 cord=[primary0(In),prt0(In)]
689               end
690
691               todo(1,:)=[]
692               L=[];
693               for Bi=lprimary'
694                 C=[1:clkptr(Bi+1)-clkptr(Bi)]'
695                 L=[L;Bi*ones(C),C]
696               end
697               todo=merge_mat(todo,L)
698             end
699           else
700             if blk<>0 then
701               J=clkptr(blk)+port-1
702               lordclk(J)=[]
703               if  show_comment then   
704                   mprintf('for blk port '+string(blk)+' '+string(port)+..
705                                   ' ordclk is'), mprintf(lordclk(J) + "\n"),
706               end
707             else
708               cord=[]
709             end
710
711             todo(1,:)=[]
712           end
713         else
714           todo(1,:)=[]
715         end 
716       end
717     end
718
719     for J=1:clkptr($)-1
720       ordclk=[ordclk;lordclk(J)]
721       ordptr=[ordptr;ordptr($)+size(lordclk(J),1)]   
722     end
723
724     bllst=list(bllst(vbllst))
725
726 endfunction
727
728 function  [primary0,port0]=aggregate(primary0,port0)
729 primary=discardprimary([primary0,port0]) // must rewrite
730 primary0=primary(:,1);port0=primary(:,2)
731 endfunction
732
733
734 function [parents]=get_parents2(parents,blk,port)
735   k=size(parents,1)+1;
736   f=find(clkconnect(:,3)==blk)
737   n_f=size(f,2)
738   if n_f>0 then
739     for i=1:n_f
740       if clkconnect(f(i),4)==port then
741         clki1=clkconnect(f(i),1)
742         clki2=clkconnect(f(i),2)
743         g=find(clki1==parents(:,1))
744         if g==[] | (g~=[] & parents(g,2)~=clki2) then
745           parents(k,1)=clki1
746           parents(k,2)=clki2
747           k=k+1
748         end
749       end
750     end
751   end
752 endfunction
753
754 //suppression des liens inutiles
755 function [clkconnect,amaj]=find_del_inutile(clkconnect,vec_plus,typ_l)
756   amaj=[]
757   for i=1:size(vec_plus,1)
758     blk=vec_plus(i,1)
759     port=vec_plus(i,2)
760     parents=get_parents2([],blk,port)
761     j=1
762     done=%f
763     n_p=size(parents,1)
764     while j<=n_p & ~done
765       par1=parents(j,1)
766       if par1~=0 & typ_l(par1) then
767         n_out=clkptr(par1+1)-clkptr(par1)
768         f=find(par1==parents(:,1))
769         if size(f,2)==n_out then
770           if show_comment then
771             mprintf('find_del_inutile:')
772             mprintf('link between blocks '+string(par1)+' and '+string(blk)+..
773                  ' are deleted\n')
774             pause
775           end
776           [clkconnect]=del_inutile(clkconnect,par1,n_out,blk,port)
777           done=%t
778           amaj=[amaj;par1;blk]
779         end
780       end
781       j=j+1
782     end
783   end
784 endfunction
785
786 function [clkconnect]=del_inutile(clkconnect,blk0,n_out,blk,port)
787   f=find(clkconnect(:,1)==blk0)
788   del=[]
789   for i=1:size(f,2)
790     if clkconnect(f(i),3)==blk & clkconnect(f(i),4)==port then
791       del=[del;f(i)]
792     end
793   end
794   clkconnect(del,:)=[]
795   p=get_parents([],blk0)
796   n_p=size(p,1)
797   for i=1:n_p
798     clkconnect($+1,:)=[p(i,1),p(i,2),blk,port]
799   end
800 endfunction
801
802
803 function  blk0=get_father(blk1,vectmp)
804   gg=find(clkconnect(:,3)==blk1) // parents of blk1
805   del=find(clkconnect(gg,1)==0)
806   gg(del)=[]  // remove continuous-time source
807               // it is inactive
808   hh=find(vectmp(clkconnect(gg,1))>0) // keep active ones
809   blk0=unique(clkconnect(gg(hh),1)) // active parent(s)
810   blk0=blk0(typ_l(blk0)) // can be a vector
811 endfunction
812
813 function [bouclalg,vec,typ_l,clkconnect,connectmat,bllst,dep_t,dep_u,..
814           dep_uptr,corinv,clkptr,cliptr,critev]=ini_ordo2(primary,..
815         clkconnect,connectmat,bllst,typ_l,dep_t,dep_u,dep_uptr,corinv,clkptr,..
816           cliptr,critev)
817
818   n_p=size(primary,1)
819   
820   nblock=size(typ_l,1);
821   //initialisation de vec
822   //on initialise vec � -1
823   vec=-ones(nblock,1)
824   vec(primary(:,1))=1
825   
826   bouclalg=%f
827   fromfixe=%f
828   oldvec2=[]
829   counter2=0
830   
831   [wprim,wprimptr,g0prim,g0primptr,gprim,gprimptr]=prim_calc(primary)
832   
833   while ~fromfixe 
834     vec=update_vec3(vec,wprim,wprimptr,g0prim,g0primptr,gprim,gprimptr)
835     //
836     if and(vec==oldvec2) then
837       fromfixe=%t
838     else
839       oldvec2=vec
840     end
841     counter2=counter2+1
842     if (counter2>n_p+1) then
843       bouclalg=%t
844       //      mprintf('Algebraic Loop detection.')
845       break
846     end
847   end  
848 endfunction
849
850 function [bouclalg,vec]=ini_ordo3(primary)
851
852   n_p=size(primary,1)
853   
854   nblock=size(typ_l,1);
855   //initialisation de vec
856   //on initialise vec � -1
857   vec=-ones(nblock,1)
858   vec(primary(:,1))=0
859   
860   bouclalg=%f
861   fromfixe=%f
862   oldvec2=[]
863   counter2=0
864   
865   [wprim,wprimptr,g0prim,g0primptr,gprim,gprimptr]=prim_calc(primary)
866   
867   while ~fromfixe  
868     vec=update_vec4(vec,wprim,wprimptr,g0prim,g0primptr,gprim,gprimptr)
869     
870     if and(vec==oldvec2) then
871       fromfixe=%t
872     else
873       oldvec2=vec
874     end
875     counter2=counter2+1
876     if (counter2>n_p+1) then
877       bouclalg=%t
878       //      mprintf('Algebraic Loop detection.')
879       break
880     end
881   end  
882 endfunction
883
884
885 function  vec=update_vec3(vec,wprim,wprimptr,g0prim,g0primptr,gprim,gprimptr)
886   for i=1:size(wprimptr,'*')-1
887     w=wprim(wprimptr(i):wprimptr(i+1)-1)
888     g0=g0prim(g0primptr(i):g0primptr(i+1)-1)
889     g=gprim(gprimptr(i):gprimptr(i+1)-1)
890     // detecting algebraic loops within a single cluster
891     //mprintf([px,py,size(blks,'*')])
892     vec(w)=max(vec(w))
893     if (g0 ~= []) then
894       vec(w(1))=max(vec(w(1)),max(vec(g0))+1)
895     end
896     //g=g(find(vec(g)>-1))  //not optimal  test max(vec(g))
897     if g~=[] then
898       vec(w)=vec(w)-1
899       vec(w)=max(vec(w),max(vec(g)))+1
900     end
901   end
902 endfunction
903
904 function  vec=update_vec4(vec,wprim,wprimptr,g0prim,g0primptr,gprim,gprimptr)
905   for i=1:size(wprimptr,'*')-1
906     w=wprim(wprimptr(i):wprimptr(i+1)-1)
907     g0=g0prim(g0primptr(i):g0primptr(i+1)-1)
908     if g0<>[] then vec(w(1))=max(vec(w(1)),max(vec(g0))), end
909     vec(w(2:$))=vec(w(1))+1
910   end
911 endfunction
912
913 function [wprim,wprimptr,g0prim,g0primptr,gprim,gprimptr]=prim_calc(primary)
914   wprim=[];wprimptr=1
915   gprim=[];gprimptr=1
916   g0prim=[];g0primptr=1
917
918
919   for i=1:size(primary,'*')
920     w0=primary(i)
921     if typ_l(w0) then 
922       w=get_allchilds(w0,fblks,fblksptr,typ_l)
923       j=find(w==w0)
924       w([1,j])=w([j,1])  // put w0 on top
925     else
926       w=w0;
927     end
928     wprim=[wprim;w(:)]
929     wprimptr($+1)=wprimptr($)+size(w,'*')
930     
931     px=blksptr(w(1));py=blksptr(w(1)+1)-1
932     //mprintf([px,py,size(blks,'*')])
933     g0prim=[g0prim;blks(px:py)]
934     g0primptr($+1)=g0primptr($)+size(px:py,2)
935   
936     g=[]
937     for i1=2:size(w,'*') // 1 is done already
938       px=blksptr(w(i1));py=blksptr(w(i1)+1)-1
939       g=[g;blks(px:py)]
940     end
941     gprim=[gprim;g]
942     gprimptr($+1)=gprimptr($)+size(g,'*')
943   end       
944 endfunction
945
946 function primary=discardprimary(primary)
947 // discard
948   mma=max(primary(:,2))+1
949   con=mma*primary(:,1)+primary(:,2)
950   [junk,ind]=gsort(-con);con=-junk
951   primary=primary(ind,:)
952   // discard duplicate calls to the same block port
953   if size(con,'*')>=2 then
954     primary(find(con(2:$)-con(1:$-1)==0),:)=[]
955   end
956   // group calls to different ports of the same block.
957   primary=[primary(:,1),2^(primary(:,2)-ones(primary(:,2)))]
958   primary=int(primary)
959   con=primary(:,1) 
960   clkconnectjj=[]     
961   if size(con,'*')>=2 then 
962     iini=[find(con(2:$)-con(1:$-1)<>0),size(primary,1)]
963   else
964     iini=1
965   end
966   for ii=iini
967     clkconnectjj=[clkconnectjj;[primary(ii,1),..
968                     mysum(primary(find(primary(:,1)==primary(ii,1)),2))]]
969   end
970   primary=clkconnectjj
971 endfunction
972
973
974
975
976 function [ordclk,iord,oord,zord,typ_z,ok]=scheduler(inpptr,outptr,clkptr,execlk_cons,..
977           outoin,outoinptr,evoutoin,evoutoinptr,typ_z,typ_x,typ_l,bexe,boptr,..
978           blnk,blptr,ordclk,ordptr,cord,dep_t,dep_u,dep_uptr);
979
980   nblk=size(typ_x,1)
981   //compute iord
982   if execlk_cons<>[] then
983     vec=-ones(1,nblk)
984     no_tu_dep=execlk_cons(:,1)
985     wec=zeros(1,nblk)
986     wec(no_tu_dep')=execlk_cons(:,2)'
987     vec(no_tu_dep)=0*no_tu_dep'
988     [r,ok]=newc_tree2(vec,outoin,outoinptr,dep_u,dep_uptr)
989
990     iord=[r,wec(r)']
991   else
992     iord=[]
993   end
994   //
995   if ~ok then 
996     disp(mprintf("%s: scheduling failed", "c_pass2"));
997     messagebox(_('Algebraic loop.'),"modal","error");
998     iord=[],oord=[],zord=[],critev=[]
999     return,
1000   end
1001   
1002   n=size(cord,1)
1003   vec=-ones(1,nblk);
1004   vec(cord(:,1))=0;
1005   typp=zeros(typ_l);typp(typ_l)=1
1006
1007   ext_cord1=cord;
1008   j=1
1009   while %t
1010     ii=ext_cord1(j,1)
1011     if typ_l(ii)
1012       for i=[clkptr(ii):clkptr(ii+1)-1]
1013         ext_cord1=[ext_cord1;ordclk([ordptr(i):ordptr(i+1)-1],:)];
1014       end
1015     end
1016     j=j+1
1017     if j>size(ext_cord1,1) then break;end
1018   end
1019   // code to replace faulty unique which reorders
1020   yy=ext_cord1(:,1)'
1021   [xx,kkn]=unique(yy);
1022   ext_cord=yy(-gsort(-kkn))
1023   //ext_cord=unique(ext_cord1(:,1)');
1024   //for i=ext_cord
1025   //  if typ_l(i) then typ_z(i)=clkptr(i+1)-clkptr(i)-1;end
1026   //end  // adding zero crossing surfaces to cont. time synchros
1027   
1028   //a supprimer
1029
1030   [ext_cord_old,ok]=newc_tree3(vec,dep_u,dep_uptr,typp);
1031  
1032   if or(gsort(ext_cord_old)<>gsort(ext_cord)) then pause,end
1033   //
1034   //pour mettre a zero les typ_z qui ne sont pas dans ext_cord
1035   //noter que typ_z contient les tailles des nzcross (peut etre >1)
1036   typ_z(ext_cord)=-typ_z(ext_cord)
1037   typ_z=-min(typ_z,0)
1038   
1039   if ~ok then mprintf('serious bug, report.');pause;end
1040   // ext_cord=ext_cord(n+1:$);
1041
1042   typ_z_save=typ_z
1043   
1044   fin=0
1045   while ~fin
1046     fin=1
1047     for i=ext_cord($:-1:1)
1048       for ii=[outoin(outoinptr(i):outoinptr(i+1)-1,1)',..
1049               evoutoin(evoutoinptr(i):evoutoinptr(i+1)-1,1)']
1050         //ii est un block affecte par changement de sortie du 
1051         //i-eme block de oord
1052         if typ_z(ii) then 
1053           if typ_z(i)==0 then typ_z(i)=1;fin=0;end
1054         end
1055         if typ_x(ii) then 
1056           if ~typ_x(i) then typ_x(i)=%t;fin=0;end
1057         end
1058         if typ_z(i)&typ_x(i) then break,end
1059       end
1060     end
1061   end
1062   //supprimer les blocks a supprimer
1063   ind=find(typ_z(cord(:,1)));
1064   zord=cord(ind,:)
1065   ind=find(typ_x(cord(:,1)));
1066   oord=cord(ind,:)  
1067   typ_z=typ_z_save
1068   //critev: vecteur indiquant si evenement est important pour tcrit
1069   //Donc les blocks indiques sont des blocks susceptibles de produire
1070   //des discontinuites quand l'evenement se produit
1071   maX=max([ext_cord1(:,2);ordclk(:,2)])+1;
1072   cordX=ext_cord1(:,1)*maX+ext_cord1(:,2);
1073
1074   // 1: important; 0:non
1075   //n=clkptr(nblk+1)-1 //nb d'evenement
1076   n=size(ordptr,'*')-1 //nb d'evenement
1077   
1078   //a priori tous les evenemets sont non-importants
1079   //critev=zeros(n,1)
1080   for i=1:n
1081     for hh=ordptr(i):ordptr(i+1)-1
1082       jj= ordclk(hh,1) //block excite par evenement i
1083                        //Following line is not correct because of ever active synchros
1084                        if or(jj*maX+ordclk(hh,2)==cordX) then
1085                          ordclk(hh,2)=-ordclk(hh,2)
1086                        end
1087     end
1088   end
1089 endfunction
1090
1091 function [ord,ok]=tree3(vec,dep_ut,typ_l)
1092 //compute blocks execution tree
1093   ok=%t
1094   nb=size(vec,'*')
1095   for j=1:nb+2
1096     fini=%t
1097     for i=1:nb
1098       if vec(i)==j-1&typ_l(i)<>-1 then 
1099         if j==nb+2 then 
1100           disp(mprintf("%s: tree (3) failed", "c_pass2"));
1101           messagebox(_('Algebraic loop.'),"modal","error");ok=%f;ord=[];return;
1102         end
1103         if typ_l(i)==1 then
1104           fini=%f;
1105           kk=bexe(boptr(i):boptr(i+1)-1)';
1106         else
1107           kk=[];
1108           for ii=blnk(blptr(i):blptr(i+1)-1)'
1109             if vec(ii)>-1 & (dep_ut(ii,1) | (typ_l(ii)==1)) then
1110               fini=%f;
1111               kk=[kk ii];
1112             end
1113           end
1114         end
1115         vec(kk)=j*ones(kk) ;   //mprintf(vec)
1116       end
1117     end
1118     if fini then break;end
1119   end
1120   [k,ord]=gsort(-vec);
1121   ord(find(k==1))=[];
1122 endfunction
1123
1124 function [clkconnectj_cons]=discard(clkptr,cliptr,clkconnect,exe_cons)
1125
1126   if exe_cons<>[] then
1127     clkconnectj=exe_cons
1128     mma=max(clkconnectj(:,2))+1
1129     con=mma*(clkconnectj(:,1))+clkconnectj(:,2)
1130     [junk,ind]=gsort(-con);con=-junk
1131     clkconnectj=clkconnectj(ind,:)
1132     // discard duplicate calls to the same block port
1133     if size(con,'*')>=2 then
1134       clkconnectj(find(con(2:$)-con(1:$-1)==0),:)=[]
1135     end
1136     // group calls to different ports of the same block.
1137     clkconnectj=[clkconnectj(:,1),2^(clkconnectj(:,2)-ones(clkconnectj(:,2)))]
1138     clkconnectj=int(clkconnectj)
1139     con=clkconnectj(:,1)
1140     clkconnectj_cons=[]
1141     if size(con,'*')>=2 then 
1142       iini=[find(con(2:$)-con(1:$-1)<>0),size(clkconnectj,1)]
1143     else
1144       iini=1
1145     end
1146     for ii=iini
1147       clkconnectj_cons=[clkconnectj_cons;[clkconnectj(ii,1),..
1148                     mysum(clkconnectj(find(clkconnectj(:,1)==clkconnectj(ii,1)),2))]]
1149     end
1150   else
1151     clkconnectj_cons=[]
1152   end
1153 endfunction
1154
1155 function a=mysum(b)
1156   if b<>[] then a=sum(b), else a=[], end
1157 endfunction
1158
1159 function [lnksz,lnktyp,inplnk,outlnk,clkptr,cliptr,inpptr,outptr,xptr,zptr,..
1160           ozptr,typ_mod,rpptr,ipptr,opptr,xc0,xcd0,xd0,oxd0,rpar,..
1161           ipar,opar,typ_z,typ_x,typ_m,funs,funtyp,initexe,labels,..
1162           bexe,boptr,blnk,blptr,ok]=extract_info(bllst,connectmat,clkconnect,typ_l)
1163
1164   ok=%t
1165   nbl=length(bllst)
1166   clkptr=zeros(nbl+1,1);clkptr(1)=1
1167   cliptr=clkptr;inpptr=cliptr;outptr=inpptr;
1168   xptr=1;zptr=1;ozptr=1;
1169   rpptr=clkptr;ipptr=clkptr;opptr=clkptr;
1170   //
1171   xc0=[];xcd0=[];xd0=[];
1172   oxd0=list()
1173   rpar=[];ipar=[];
1174   opar=list();
1175
1176   fff=ones(nbl,1)==1
1177   typ_z=zeros(nbl,1);typ_x=fff;typ_m=fff;typ_mod=zeros(nbl,1);
1178
1179   initexe=[];
1180   funs=list();
1181   funtyp=zeros(typ_z)
1182   labels=[]
1183   [ok,bllst]=adjust_inout(bllst,connectmat)
1184   if ok then
1185     [ok,bllst]=adjust_typ(bllst,connectmat)
1186   end
1187
1188   // placed here to make sure nzcross and nmode correctly updated
1189   if ~ok then
1190     lnksz=[],lnktyp=[],inplnk=[],outlnk=[],clkptr=[],cliptr=[],inpptr=[],outptr=[],..
1191     xptr=[],zptr=[],ozptr=[],rpptr=[],ipptr=[],opptr=[],xc0=[],..
1192     xcd0=[],xd0=[],oxd0=list(),rpar=[],ipar=[],opar=list(),..
1193     typ_z=[],typ_x=[],typ_m=[],funs=[],funtyp=[],initexe=[],..
1194     labels=[],bexe=[],boptr=[],blnk=[],blptr=[]
1195     return;
1196   end
1197   for i=1:nbl
1198     ll=bllst(i)
1199
1200     if type(ll.sim)==15 then
1201       funs(i)=ll.sim(1)
1202       funtyp(i,1)=ll.sim(2)
1203     else
1204       funs(i)=ll.sim;
1205       funtyp(i,1)=0;
1206     end
1207     if funtyp(i,1)>999&funtyp(i,1)<10000 then
1208       if ~c_link(funs(i)) then
1209         messagebox(msprintf(_('A C or Fortran block is used but not linked.\n'+..
1210                    'You can save your compiled diagram and load it.\n'+..
1211                    'This will automatically link the C or Fortran function.')),"modal","error")
1212       end
1213     end
1214     inpnum=ll.in;outnum=ll.out;cinpnum=ll.evtin;coutnum=ll.evtout;
1215     //
1216     inpptr(i+1)=inpptr(i)+size(inpnum,'*')
1217     outptr(i+1)=outptr(i)+size(outnum,'*')
1218     cliptr(i+1)=cliptr(i)+size(cinpnum,'*')
1219     clkptr(i+1)=clkptr(i)+size(coutnum,'*')
1220     //
1221     X0=ll.state(:)
1222     if funtyp(i,1)<10000 then
1223       xcd0=[xcd0;0*X0]
1224       xc0=[xc0;X0]
1225       xptr(i+1)=xptr(i)+size(ll.state,'*')
1226     else
1227       xcd0=[xcd0;X0($/2+1:$)]
1228       xc0=[xc0;X0(1:$/2)]
1229       xptr(i+1)=xptr(i)+size(ll.state,'*')/2
1230     end
1231
1232     //dstate
1233     if (funtyp(i,1)==3 | funtyp(i,1)==5 | funtyp(i,1)==10005) then //sciblocks
1234       if ll.dstate==[] then xd0k=[]; else xd0k=var2vec(ll.dstate);end
1235     else
1236       xd0k=ll.dstate(:)
1237     end
1238     xd0=[xd0;xd0k]
1239     zptr(i+1)=zptr(i)+size(xd0k,'*')
1240
1241     //odstate
1242     if type(ll.odstate)==15 then
1243       if ((funtyp(i,1)==5) | (funtyp(i,1)==10005)) then //sciblocks : don't extract
1244         if lstsize(ll.odstate)>0 then
1245           oxd0($+1)=ll.odstate
1246           ozptr(i+1)=ozptr(i)+1;
1247         else
1248           ozptr(i+1)=ozptr(i);
1249         end
1250       elseif ((funtyp(i,1)==4)    | (funtyp(i,1)==10004) |...
1251               (funtyp(i,1)==2004) | (funtyp(i,1)==12004))  //C blocks : extract
1252         ozsz=lstsize(ll.odstate);
1253         if ozsz>0 then
1254           for j=1:ozsz, oxd0($+1)=ll.odstate(j), end;
1255           ozptr(i+1)=ozptr(i)+ozsz;
1256         else
1257           ozptr(i+1)=ozptr(i);
1258         end
1259       else
1260         ozptr(i+1)=ozptr(i);
1261       end
1262     else
1263       //add an error message here please !
1264       ozptr(i+1)=ozptr(i);
1265     end
1266
1267     //mod
1268     typ_mod(i)=ll.nmode;
1269     if typ_mod(i)<0 then
1270       messagebox(msprintf(_('Number of modes in block #%d cannot be determined.'),i),"modal","error")
1271       ok=%f
1272     end
1273
1274     //real paramaters
1275     if (funtyp(i,1)==3 | funtyp(i,1)==5 | funtyp(i,1)==10005) then //sciblocks
1276       if ll.rpar==[] then rpark=[]; else rpark=var2vec(ll.rpar);end
1277     else
1278       rpark=ll.rpar(:)
1279     end
1280     rpar=[rpar;rpark]
1281     rpptr(i+1)=rpptr(i)+size(rpark,'*')
1282
1283     //integer parameters
1284     if type(ll.ipar)==1 then
1285       ipar=[ipar;ll.ipar(:)]
1286       ipptr(i+1)=ipptr(i)+size(ll.ipar,'*')
1287     else
1288       ipptr(i+1)=ipptr(i)
1289     end
1290
1291     //object parameters
1292     if type(ll.opar)==15 then
1293      if ((funtyp(i,1)==5) | (funtyp(i,1)==10005)) then //sciblocks : don't extract
1294         if lstsize(ll.opar)>0 then
1295          opar($+1)=ll.opar
1296          opptr(i+1)=opptr(i)+1;
1297         else
1298          opptr(i+1)=opptr(i);
1299         end
1300      elseif ((funtyp(i,1)==4)    | (funtyp(i,1)==10004) |...
1301              (funtyp(i,1)==2004) | (funtyp(i,1)==12004)) then //C blocks : extract
1302        oparsz=lstsize(ll.opar);
1303        if oparsz>0 then
1304          for j=1:oparsz, opar($+1)=ll.opar(j), end;
1305          opptr(i+1)=opptr(i)+oparsz;
1306        else
1307          opptr(i+1)=opptr(i);
1308        end
1309      else
1310        opptr(i+1)=opptr(i);
1311      end
1312     else
1313       //add an error message here please !
1314       opptr(i+1)=opptr(i);
1315     end
1316
1317     //    typ_z(i)=ll.blocktype=='z'
1318     typ_z(i)=ll.nzcross
1319     if typ_z(i)<0 then
1320       messagebox(msprintf(_('Number of zero crossings in block #i cannot be determined.'),i),"modal","error")
1321       ok=%f
1322     end
1323     typ_x(i)=ll.state<>[]|ll.blocktype=='x' // some blocks like delay
1324                                             // need to be in oord even
1325                                             // without state
1326                                 
1327     typ_m(i)=ll.blocktype=='m'
1328     //
1329     if ll.evtout<>[] then  
1330       ll11=ll.firing
1331       prt=find(ll11>=zeros(ll11))
1332       nprt=prod(size(prt))
1333       initexe=[initexe;[i*ones(nprt,1),matrix(prt,nprt,1),matrix(ll11(prt),nprt,1)]];
1334     end
1335
1336     if type(ll.label)==10 then
1337       labels=[labels;ll.label(1)]
1338     else
1339       labels=[labels;' ']
1340     end
1341   end
1342   
1343   clkconnect=clkconnect(find(clkconnect(:,1)<>0),:);
1344
1345   con=clkptr(clkconnect(:,1))+clkconnect(:,2)-1;
1346   [junk,ind]=gsort(-con);con=-junk;
1347   clkconnect=clkconnect(ind,:);
1348   //
1349   bclkconnect=clkconnect(:,[1 3]);
1350   boptr=1;
1351   bexe=[];
1352   for i=1:nbl
1353     r=bclkconnect(find(bclkconnect(:,1)==i),2);
1354     bexe=[bexe;r];
1355     boptr=[boptr;boptr($)+size(r,1)];
1356   end
1357   //
1358  
1359   blptr=1;
1360   blnk=[];
1361
1362   for i=1:nbl
1363     r=connectmat(find(connectmat(:,1)==i),3:4);
1364     blnk=[blnk;r];
1365     blptr=[blptr;blptr($)+size(r,1)];
1366   end  
1367   //
1368
1369   nlnk=size(connectmat,1)
1370   inplnk=zeros(inpptr($)-1,1);outlnk=zeros(outptr($)-1,1);ptlnk=1;
1371   lnksz=[];lnktyp=[];
1372   for jj=1:nlnk
1373     ko=outlnk(outptr(connectmat(jj,1))+connectmat(jj,2)-1)
1374     ki=inplnk(inpptr(connectmat(jj,3))+connectmat(jj,4)-1)
1375     if ko<>0 & ki<>0 then
1376       if ko>ki then 
1377         outlnk(outlnk>ko)=outlnk(outlnk>ko)-1
1378         outlnk(outlnk==ko)=ki
1379         inplnk(inplnk>ko)=inplnk(inplnk>ko)-1
1380         inplnk(inplnk==ko)=ki
1381         ptlnk=-1+ptlnk
1382         lnksz(ko,1)=[];
1383         lnksz(ko,2)=[];
1384         lnktyp(ko) =[];
1385       elseif ki>ko
1386         outlnk(outlnk>ki)=outlnk(outlnk>ki)-1
1387         outlnk(outlnk==ki)=ko
1388         inplnk(inplnk>ki)=inplnk(inplnk>ki)-1
1389         inplnk(inplnk==ki)=ko
1390         ptlnk=-1+ptlnk
1391         lnksz(ki,1)=[];
1392         lnksz(ki,2)=[];
1393         lnktyp(ki) =[];
1394       end
1395     elseif ko<>0 then
1396       inplnk(inpptr(connectmat(jj,3))+connectmat(jj,4)-1)=ko
1397     elseif ki<>0 then
1398       outlnk(outptr(connectmat(jj,1))+connectmat(jj,2)-1)=ki
1399     else
1400       outlnk(outptr(connectmat(jj,1))+connectmat(jj,2)-1)=ptlnk
1401       inplnk(inpptr(connectmat(jj,3))+connectmat(jj,4)-1)=ptlnk
1402       lnksz(ptlnk,1)=bllst(connectmat(jj,1)).out(connectmat(jj,2));
1403       lnksz(ptlnk,2)=bllst(connectmat(jj,1)).out2(connectmat(jj,2));
1404       lnktyp(ptlnk)=bllst(connectmat(jj,1)).outtyp(connectmat(jj,2));
1405       ptlnk=1+ptlnk
1406     end
1407   end
1408
1409   //store unconnected outputs, if any, at the end of outtb
1410   unco=find(outlnk==0);
1411   for j=unco
1412     m=max(find(outptr<=j))
1413     n=j-outptr(m)+1
1414     nm=bllst(m).out(n)
1415     if nm<1 then
1416       under_connection(corinv(m),n,nm,-1,0,0,1),ok=%f,return,
1417     end
1418     nm2=bllst(m).out2(n)
1419     if nm2<1 then
1420       under_connection(corinv(m),n,nm2,-1,0,0,1),ok=%f,return,
1421     end
1422     lnksz($+1,1)=bllst(m).out(n);
1423     lnksz($,2)=bllst(m).out2(n);
1424     lnktyp($+1)=bllst(m).outtyp(n);
1425     outlnk(j)=max(outlnk)+1
1426   end
1427
1428   //store unconnected inputs, if any, at the end of outtb
1429   unco=find(inplnk==0);
1430   for j=unco
1431     m=max(find(inpptr<=j))
1432     n=j-inpptr(m)+1
1433     nm=bllst(m).in(n)
1434     if nm<1 then 
1435      under_connection(corinv(m),n,nm,-2,0,0,1),ok=%f,return,
1436     end
1437     nm2=bllst(m).in2(n)
1438     if nm2<1 then
1439      under_connection(corinv(m),n,nm2,-2,0,0,1),ok=%f,return,
1440     end
1441     lnksz($+1,1)=bllst(m).in(n);
1442     lnksz($,2)=bllst(m).in2(n);
1443     lnktyp($+1)=bllst(m).intyp(n);
1444     inplnk(j)=max([inplnk;max(outlnk)])+1
1445   end
1446
1447 endfunction
1448
1449 function [outoin,outoinptr]=conn_mat(inpptr,outptr,inplnk,outlnk)
1450   outoin=[];outoinptr=1
1451   nblk=size(inpptr,1)-1
1452   for i=1:nblk
1453     k=outptr(i):outptr(i+1)-1
1454     ii=[]
1455     for j=outlnk(k)'
1456       ii=[ii,find(inplnk==j)]
1457     end
1458     outoini=[];jj=0
1459     for j=ii
1460       m=max(find(inpptr<=j))
1461       n=j-inpptr(m)+1
1462       outoini=[outoini;[m,n]]
1463       jj=jj+1
1464     end
1465     outoinptr=[outoinptr;outoinptr($)+jj]
1466     outoin=[outoin;outoini]
1467   end
1468 endfunction
1469
1470 function [ord,ok]=tree2(vec,outoin,outoinptr,dep_ut)
1471 //compute blocks execution tree
1472   ok=%t;
1473
1474   nb=size(vec,'*');
1475   for j=1:nb+2
1476     fini=%t
1477     for i=1:nb
1478       if vec(i)==j-1 then 
1479         if j==nb+2 then 
1480           disp(mprintf("%s: tree (2) failed", "c_pass2"));
1481           messagebox(_('Algebraic loop.'),"modal","error");ok=%f;ord=[];return;
1482         end
1483         for k=outoinptr(i):outoinptr(i+1)-1
1484           ii=outoin(k,1);
1485           //indport=find(dep_u(dep_uptr(ii):dep_uptr(ii+1)-1)==1);
1486           //if (indport ~=[] &vec(ii)>-1) then
1487           if (dep_ut(ii) &vec(ii)>-1) then
1488             fini=%f;
1489             vec(ii)=j;
1490           end
1491         end
1492      end
1493     end
1494     if fini then break;end
1495   end
1496
1497   [k,ord]=gsort(-vec);
1498   ord(find(k==1))=[];
1499   ord=ord(:)
1500 endfunction
1501
1502
1503 //adjust_inout : it resolves positive, negative and null size
1504 //               of in/out port dimensions of connected block.
1505 //               If it's not done in a first pass, the second 
1506 //               pass try to resolve negative or null port 
1507 //               dimensions by asking user to informed dimensions 
1508 //               with underconnection function.
1509 //               It is a fixed point algorithm.
1510 //
1511 //in parameters  : bllst : list of blocks
1512 //
1513 //                 connectmat : matrix of connection
1514 //                              connectmat(lnk,1) : source block
1515 //                              connectmat(lnk,2) : source port
1516 //                              connectmat(lnk,3) : target block
1517 //                              connectmat(lnk,4) : target port
1518 //
1519 //out parameters : ok : a boolean flag to known if adjust_inout have
1520 //                      succeeded to resolve the in/out port size
1521 //                      - ok = %t : all size have been resolved in bllst
1522 //                      - ok = %f : problem in size adjustement
1523 //
1524 //                 bllst : modified list of blocks
1525 //
1526 //18/05/06, Alan  : improvement in order to take into
1527 //                  account two dimensional port size.
1528 //
1529 //28/12/06, Alan : type for source port and target port must
1530 //                 be the same.
1531 //
1532 //29/12/06, Fady : type for source and target can be different
1533 //                 in one condition that they are double and complex.
1534 //                 the result on the link will be complex.
1535 //
1536 //04/01/07, Fady : Can test the case of negatives equals target's dimensions.
1537 //
1538 //19/01/07, Alan : - Return correct information for user in editor
1539 //                   with preceding test of Fady in the first pass
1540 //                 - Second pass reviewed : under_connection returns two dimensions now
1541 //
1542 //10/05/07, Alan : - if-then-else event-select case
1543
1544 function [ok,bllst]=adjust_inout(bllst,connectmat)
1545
1546   //Adjust in2/out2, inttyp/outtyp
1547   //in accordance to in/out in bllst
1548   [ko,bllst]=adjust_in2out2(bllst);
1549   if ~ko then ok=%f,return, end //if adjust_in2out2 failed then exit
1550                                 //adjust_inout with flag ok=%f
1551
1552   nlnk=size(connectmat,1) //nlnk is the number of link
1553
1554   //loop on number of block (pass 1 and pass 2)
1555   for hhjj=1:length(bllst)+1
1556      //%%%%% pass 1 %%%%%//
1557      for hh=1:length(bllst)+1 //second loop on number of block
1558         ok=%t
1559         for jj=1:nlnk //loop on number of link
1560
1561            //intyp/outtyp are the type of the
1562            //target port and the source port of the observed link
1563            outtyp = bllst(connectmat(jj,1)).outtyp(connectmat(jj,2))
1564            intyp = bllst(connectmat(jj,3)).intyp(connectmat(jj,4))
1565            //nnin/nnout are the size (two dimensions) of the
1566            //target port and the source port of the observed link
1567            //before adjust
1568            nnout(1,1)=bllst(connectmat(jj,1)).out(connectmat(jj,2))
1569            nnout(1,2)=bllst(connectmat(jj,1)).out2(connectmat(jj,2))
1570            nnin(1,1)=bllst(connectmat(jj,3)).in(connectmat(jj,4))
1571            nnin(1,2)=bllst(connectmat(jj,3)).in2(connectmat(jj,4))
1572
1573            //This Part is done in adjust_typ
1574
1575            //check intyp/outtyp
1576 //            if intyp<>outtyp then
1577 //              if (intyp==1 & outtyp==2) then
1578 //                bllst(connectmat(jj,3)).intyp(connectmat(jj,4))=2;
1579 //              elseif (intyp==2 & outtyp==1) then
1580 //                bllst(connectmat(jj,1)).outtyp(connectmat(jj,2))=2;
1581 //              else
1582 //                if bllst(connectmat(jj,3)).sim(2)<0 //if-then-else/eselect case
1583 //                  bllst(connectmat(jj,3)).intyp(connectmat(jj,4))=...
1584 //                    bllst(connectmat(jj,1)).outtyp(connectmat(jj,2))
1585 //                else
1586 //                  bad_connection(corinv(connectmat(jj,1)),connectmat(jj,2),..
1587 //                                 nnout,outtyp,..
1588 //                                 corinv(connectmat(jj,3)),connectmat(jj,4),..
1589 //                                 nnin,intyp,1)
1590 //                  ok=%f;
1591 //                  return
1592 //                end
1593 //              end
1594 //            end
1595
1596            //loop on the two dimensions of source/target port 
1597            for ndim=1:2
1598               //check test source/target sizes
1599               //in case of negatif equal target dimensions
1600               //nin/nout are the size (two dimensions) of the
1601               //target port and the source port of the observed link
1602               nout(1,1)=bllst(connectmat(jj,1)).out(connectmat(jj,2))
1603               nout(1,2)=bllst(connectmat(jj,1)).out2(connectmat(jj,2))
1604               nin(1,1)=bllst(connectmat(jj,3)).in(connectmat(jj,4))
1605               nin(1,2)=bllst(connectmat(jj,3)).in2(connectmat(jj,4))
1606
1607               //first case : dimension of source and
1608               //             target ports are explicitly informed
1609               //             informed with positive size
1610               if(nout(1,ndim)>0&nin(1,ndim)>0) then
1611                  //if dimension of source and target port doesn't match
1612                  //then call bad_connection, set flag ok to false and exit
1613                  if nin(1,ndim)<>nout(1,ndim) then
1614                     bad_connection(corinv(connectmat(jj,1)),connectmat(jj,2),..
1615                                    nnout,outtyp,..
1616                                    corinv(connectmat(jj,3)),connectmat(jj,4),..
1617                                    nnin,intyp)
1618                     ok=%f;return
1619                  end
1620
1621               //second case : dimension of source port is
1622               //              positive and dimension of
1623               //              target port is negative
1624               elseif(nout(1,ndim)>0&nin(1,ndim)<0) then
1625                  //find vector of input ports of target block with
1626                  //first/second dimension equal to size nin(1,ndim)
1627                  //and assign it to nout(1,ndim)
1628                  ww=find(bllst(connectmat(jj,3)).in==nin(1,ndim))
1629                  bllst(connectmat(jj,3)).in(ww)=nout(1,ndim)
1630                  ww=find(bllst(connectmat(jj,3)).in2==nin(1,ndim))
1631                  bllst(connectmat(jj,3)).in2(ww)=nout(1,ndim)
1632
1633                  //find vector of output ports of target block with
1634                  //first/second dimension equal to size nin(1,ndim)
1635                  //and assign it to nout(1,ndim)
1636                  ww=find(bllst(connectmat(jj,3)).out==nin(1,ndim))
1637                  bllst(connectmat(jj,3)).out(ww)=nout(1,ndim)
1638                  ww=find(bllst(connectmat(jj,3)).out2==nin(1,ndim))
1639                  bllst(connectmat(jj,3)).out2(ww)=nout(1,ndim)
1640
1641                  //find vector of output ports of target block with
1642                  //ndim dimension equal to zero and sum the ndim
1643                  //dimension of all input ports of target block
1644                  //to be the new dimension of the ndim dimension
1645                  //of the output ports of the target block
1646                  if ndim==1 then
1647                    ww=find(bllst(connectmat(jj,3)).out==0)
1648                    if (ww<>[]&min(bllst(connectmat(jj,3)).in(:))>0) then
1649                       bllst(connectmat(jj,3)).out(ww)=sum(bllst(connectmat(jj,3)).in(:))
1650                    end
1651                  elseif ndim==2 then
1652                    ww=find(bllst(connectmat(jj,3)).out2==0)
1653                    if (ww<>[]&min(bllst(connectmat(jj,3)).in2(:))>0) then
1654                       bllst(connectmat(jj,3)).out2(ww)=sum(bllst(connectmat(jj,3)).in2(:))
1655                    end
1656                  end
1657
1658                  //if nzcross of the target block match with
1659                  //the negative dimension nin(1,ndim) then
1660                  //adjust it to nout(1,ndim)
1661                  if bllst(connectmat(jj,3)).nzcross==nin(1,ndim) then
1662                     bllst(connectmat(jj,3)).nzcross=nout(1,ndim)
1663                  end
1664                  //if nmode of the target block match with
1665                  //the negative dimension nin(1,ndim) then
1666                  //adjust it to nout(1,ndim)
1667                  if bllst(connectmat(jj,3)).nmode==nin(1,ndim) then
1668                     bllst(connectmat(jj,3)).nmode=nout(1,ndim)
1669                  end
1670
1671               //third case : dimension of source port is
1672               //             negative and dimension of
1673               //             target port is positive
1674               elseif(nout(1,ndim)<0&nin(1,ndim)>0) then
1675                  //find vector of output ports of source block with
1676                  //first/second dimension equal to size nout(1,ndim)
1677                  //and assign it to nin(1,ndim)
1678                  ww=find(bllst(connectmat(jj,1)).out==nout(1,ndim))
1679                  bllst(connectmat(jj,1)).out(ww)=nin(1,ndim)
1680                  ww=find(bllst(connectmat(jj,1)).out2==nout(1,ndim))
1681                  bllst(connectmat(jj,1)).out2(ww)=nin(1,ndim)
1682
1683                  //find vector of input ports of source block with
1684                  //first/second dimension equal to size nout(1,ndim)
1685                  //and assign it to nin(1,ndim)
1686                  ww=find(bllst(connectmat(jj,1)).in==nout(1,ndim))
1687                  bllst(connectmat(jj,1)).in(ww)=nin(1,ndim)
1688                  ww=find(bllst(connectmat(jj,1)).in2==nout(1,ndim))
1689                  bllst(connectmat(jj,1)).in2(ww)=nin(1,ndim)
1690
1691                  //find vector of input ports of source block with
1692                  //ndim dimension equal to zero and sum the ndim
1693                  //dimension of all output ports of source block
1694                  //to be the new dimension of the ndim dimension
1695                  //of the input ports of the source block
1696                  if ndim==1 then
1697                    ww=find(bllst(connectmat(jj,1)).in==0)
1698                    if (ww<>[]&min(bllst(connectmat(jj,1)).out(:))>0) then
1699                       bllst(connectmat(jj,1)).in(ww)=sum(bllst(connectmat(jj,1)).out(:))
1700                    end
1701                  elseif ndim==2 then
1702                    ww=find(bllst(connectmat(jj,1)).in2==0)
1703                    if (ww<>[]&min(bllst(connectmat(jj,1)).out2(:))>0) then
1704                       bllst(connectmat(jj,1)).in2(ww)=sum(bllst(connectmat(jj,1)).out2(:))
1705                    end
1706                  end
1707
1708                  //if nzcross of the source block match with
1709                  //the negative dimension nout(1,ndim) then
1710                  //adjust it to nin(1,ndim)
1711                  if bllst(connectmat(jj,1)).nzcross==nout(1,ndim) then
1712                     bllst(connectmat(jj,1)).nzcross=nin(1,ndim)
1713                  end
1714                  //if nmode of the source block match with
1715                  //the negative dimension nout(1,ndim) then
1716                  //adjust it to nin(1,ndim)
1717                  if bllst(connectmat(jj,1)).nmode==nout(1,ndim) then
1718                     bllst(connectmat(jj,1)).nmode=nin(1,ndim)
1719                  end
1720
1721               //fourth case : a dimension of source port is
1722               //              null
1723               elseif(nout(1,ndim)==0) then
1724                  //set ww to be the vector of size of the ndim
1725                  //dimension of input port of the source block
1726                  if ndim==1 then
1727                     ww=bllst(connectmat(jj,1)).in(:)
1728                  elseif ndim==2 then
1729                     ww=bllst(connectmat(jj,1)).in2(:)
1730                  end
1731
1732                  //test if all size of the ndim dimension of input
1733                  //port of the source block is positive
1734                  if min(ww)>0 then
1735                     //test if the dimension of the target port
1736                     //is positive
1737                     if nin(1,ndim)>0 then
1738
1739                        //if the sum of the size of the ndim dimension of the input 
1740                        //port of the source block is equal to the size of the ndim dimension
1741                        //of the target port, then the size of the ndim dimension of the source
1742                        //port is equal to nin(1,ndim)
1743                        if sum(ww)==nin(1,ndim) then
1744                           if ndim==1 then
1745                              bllst(connectmat(jj,1)).out(connectmat(jj,2))=nin(1,ndim)
1746                           elseif ndim==2 then
1747                              bllst(connectmat(jj,1)).out2(connectmat(jj,2))=nin(1,ndim)
1748                           end
1749                        //else call bad_connection, set flag ok to false and exit
1750                        else
1751                           bad_connection(corinv(connectmat(jj,1)),0,0,1,-1,0,0,1)
1752                           ok=%f;return
1753                        end
1754
1755                     //if the ndim dimension of the target port is negative
1756                     //then the size of the ndim dimension of the source port
1757                     //is equal to the sum of the size of the ndim dimension
1758                     //of input ports of source block, and flag ok is set to false
1759                     else
1760                        if ndim==1 then
1761                          bllst(connectmat(jj,1)).out(connectmat(jj,2))=sum(ww)
1762                        elseif ndim==2 then
1763                          bllst(connectmat(jj,1)).out2(connectmat(jj,2))=sum(ww)
1764                        end
1765                        ok=%f
1766                     end
1767
1768                  else
1769                     //set nww to be the vector of all negative size of input ports
1770                     //of the source block
1771                     nww=ww(find(ww<0))
1772
1773                     //if all negative size have same size and if size of the
1774                     //ndim dimension of the target port is positive then assign
1775                     //size of the ndim dimension of the source port to nin(1,ndim)
1776                     if norm(nww-nww(1),1)==0 & nin(1,ndim)>0 then
1777                        if ndim==1 then
1778                           bllst(connectmat(jj,1)).out(connectmat(jj,2))=nin(1,ndim)
1779                        elseif ndim==2 then
1780                           bllst(connectmat(jj,1)).out2(connectmat(jj,2))=nin(1,ndim)
1781                        end
1782
1783                        //compute a size to be the difference between the size
1784                        //of the ndim dimension of the target block and sum of positive 
1785                        //size of input ports of the source block divided by the number
1786                        //of input ports of source block with same negative size
1787                        k=(nin(1,ndim)-sum(ww(find(ww>0))))/size(nww,'*')
1788
1789                        //if this size is a positive integer then assign it
1790                        //to the size of the ndim dimension of input ports of the 
1791                        //source block which have negative size
1792                        if k==int(k)&k>0 then
1793                           if ndim==1 then
1794                              bllst(connectmat(jj,1)).in(find(ww<0))=k
1795                           elseif ndim==2 then
1796                              bllst(connectmat(jj,1)).in2(find(ww<0))=k
1797                           end
1798                        //else call bad_connection, set flag ok to false and exit
1799                        else
1800                           bad_connection(corinv(connectmat(jj,1)),0,0,1,-1,0,0,1)
1801                           ok=%f;return
1802                        end
1803
1804                     //set flag ok to false
1805                     else
1806                       ok=%f
1807                     end
1808
1809                  end
1810
1811               //fifth case : a dimension of target port is
1812               //             null
1813               elseif(nin(1,ndim)==0) then
1814                  //set ww to be the vector of size of the ndim
1815                  //dimension of output port of the target block
1816                  if ndim==1 then
1817                     ww=bllst(connectmat(jj,3)).out(:)
1818                  elseif ndim==2 then
1819                     ww=bllst(connectmat(jj,3)).out2(:)
1820                  end
1821
1822                  //test if all size of the ndim dimension of output
1823                  //port of the target block is positive
1824                  if min(ww)>0 then
1825                     //test if the dimension of the source port
1826                     //is positive
1827                     if nout(1,ndim)>0 then
1828
1829                        //if the sum of the size of the ndim dimension of the output 
1830                        //port of the target block is equal to the size of the ndim dimension
1831                        //of the source port, then the size of the ndim dimension of the target
1832                        //port is equal to nout(1,ndim)
1833                        if sum(ww)==nout(1,ndim) then
1834                           if ndim==1 then
1835                              bllst(connectmat(jj,3)).in(connectmat(jj,4))=nout(1,ndim)
1836                           elseif ndim==2 then
1837                              bllst(connectmat(jj,3)).in2(connectmat(jj,4))=nout(1,ndim)
1838                           end
1839                        //else call bad_connection, set flag ok to false and exit
1840                        else
1841                           bad_connection(corinv(connectmat(jj,3)),0,0,1,-1,0,0,1)
1842                           ok=%f;return
1843                        end
1844
1845                     //if the ndim dimension of the source port is negative
1846                     //then the size of the ndim dimension of the target port
1847                     //is equal to the sum of the size of the ndim dimension
1848                     //of output ports of target block, and flag ok is set to false
1849                     else
1850                        if ndim==1 then
1851                          bllst(connectmat(jj,3)).in(connectmat(jj,4))=sum(ww)
1852                        elseif ndim==2 then
1853                          bllst(connectmat(jj,3)).in2(connectmat(jj,4))=sum(ww)
1854                        end
1855                        ok=%f
1856                     end
1857
1858                  else
1859                     //set nww to be the vector of all negative size of output ports
1860                     //of the target block
1861                     nww=ww(find(ww<0))
1862
1863                     //if all negative size have same size and if size of the
1864                     //ndim dimension of the source port is positive then assign
1865                     //size of the ndim dimension of the target port to nout(1,ndim)
1866                     if norm(nww-nww(1),1)==0 & nout(1,ndim)>0 then
1867                        if ndim==1 then
1868                           bllst(connectmat(jj,3)).in(connectmat(jj,4))=nout(1,ndim)
1869                        elseif ndim==2 then
1870                           bllst(connectmat(jj,3)).in2(connectmat(jj,4))=nout(1,ndim)
1871                        end
1872
1873                        //compute a size to be the difference between the size
1874                        //of the ndim dimension of the source block and sum of positive 
1875                        //size of output ports of the target block divided by the number
1876                        //of output ports of target block with same negative size
1877                        k=(nout(1,ndim)-sum(ww(find(ww>0))))/size(nww,'*')
1878
1879                        //if this size is a positive integer then assign it
1880                        //to the size of the ndim dimension of output ports of the 
1881                        //target block which have negative size
1882                        if k==int(k)&k>0 then
1883                           if ndim==1 then
1884                              bllst(connectmat(jj,3)).out(find(ww<0))=k
1885                           elseif ndim==2 then
1886                              bllst(connectmat(jj,3)).out2(find(ww<0))=k
1887                           end
1888                        //else call bad_connection, set flag ok to false and exit
1889                        else
1890                           bad_connection(corinv(connectmat(jj,3)),0,0,1,-1,0,0,1)
1891                           ok=%f;return
1892                        end
1893
1894                     //set flag ok to false
1895                     else
1896                       ok=%f
1897                     end
1898
1899                  end
1900
1901               //sixth (& last) case : dimension of both source 
1902               //                      and target port are negatives
1903               else
1904                  ok=%f //set flag ok to false
1905               end
1906            end
1907         end
1908         if ok then return, end //if ok is set true then exit adjust_inout
1909      end
1910      //if failed then display message
1911      messagebox(msprintf(_('Not enough information to find port sizes.\n'+..
1912               'I try to find the problem.')),"modal","info");
1913
1914      //%%%%% pass 2 %%%%%//
1915      //Alan 19/01/07 : Warning  : Behavior have changed, To Be more Tested
1916      findflag=%f //set findflag to false
1917
1918      for jj=1:nlnk //loop on number of block
1919         //nin/nout are the size (two dimensions) of the
1920         //target port and the source port of the observed link
1921         nout(1,1)=bllst(connectmat(jj,1)).out(connectmat(jj,2))
1922         nout(1,2)=bllst(connectmat(jj,1)).out2(connectmat(jj,2))
1923         nin(1,1)=bllst(connectmat(jj,3)).in(connectmat(jj,4))
1924         nin(1,2)=bllst(connectmat(jj,3)).in2(connectmat(jj,4))
1925
1926         //loop on the two dimensions of source/target port
1927         //only case : target and source ports are both
1928         //            negatives or null
1929         if nout(1,1)<=0&nin(1,1)<=0 | nout(1,2)<=0&nin(1,2)<=0 then
1930             findflag=%t;
1931             //
1932             ninnout=under_connection(corinv(connectmat(jj,1)),connectmat(jj,2),nout(1,ndim),..
1933                                        corinv(connectmat(jj,3)),connectmat(jj,4),nin(1,ndim),1)
1934             //
1935             if size(ninnout,2) <> 2 then ok=%f;return;end
1936             if ninnout==[] then ok=%f;return;end
1937             if ninnout(1,1)<=0 | ninnout(1,2)<=0 then ok=%f;return;end
1938             //
1939             ww=find(bllst(connectmat(jj,1)).out==nout(1,1))
1940             bllst(connectmat(jj,1)).out(ww)=ninnout(1,1)
1941             ww=find(bllst(connectmat(jj,1)).out2==nout(1,1))
1942             bllst(connectmat(jj,1)).out2(ww)=ninnout(1,1)
1943
1944             ww=find(bllst(connectmat(jj,1)).out==nout(1,2))
1945             bllst(connectmat(jj,1)).out(ww)=ninnout(1,2)
1946             ww=find(bllst(connectmat(jj,1)).out2==nout(1,2))
1947             bllst(connectmat(jj,1)).out2(ww)=ninnout(1,2)
1948             //
1949
1950             if bllst(connectmat(jj,1)).nzcross==nout(1,1) then
1951                bllst(connectmat(jj,1)).nzcross=ninnout(1,1)
1952             end
1953             if bllst(connectmat(jj,1)).nzcross==nout(1,2) then
1954                bllst(connectmat(jj,1)).nzcross=ninnout(1,2)
1955             end
1956             //
1957             if bllst(connectmat(jj,1)).nmode==nout(1,1) then
1958                bllst(connectmat(jj,1)).nmode=ninnout(1,1)
1959             end
1960             if bllst(connectmat(jj,1)).nmode==nout(1,2) then
1961                bllst(connectmat(jj,1)).nmode=ninnout(1,2)
1962             end
1963             //
1964             ww=find(bllst(connectmat(jj,1)).in==nout(1,1))
1965             bllst(connectmat(jj,1)).in(ww)=ninnout(1,1)
1966             ww=find(bllst(connectmat(jj,1)).in2==nout(1,1))
1967             bllst(connectmat(jj,1)).in2(ww)=ninnout(1,1)
1968
1969             ww=find(bllst(connectmat(jj,1)).in==nout(1,2))
1970             bllst(connectmat(jj,1)).in(ww)=ninnout(1,2)
1971             ww=find(bllst(connectmat(jj,1)).in2==nout(1,2))
1972             bllst(connectmat(jj,1)).in2(ww)=ninnout(1,2)
1973             //
1974             ww=find(bllst(connectmat(jj,1)).in==0)
1975             if (ww<>[]&min(bllst(connectmat(jj,1)).out(:))>0) then 
1976                bllst(connectmat(jj,1)).in(ww)=sum(bllst(connectmat(jj,1)).out)
1977             end
1978
1979             ww=find(bllst(connectmat(jj,1)).in2==0)
1980             if (ww<>[]&min(bllst(connectmat(jj,1)).out2(:))>0) then 
1981                  bllst(connectmat(jj,1)).in2(ww)=sum(bllst(connectmat(jj,1)).out2)
1982             end
1983             //
1984             ww=find(bllst(connectmat(jj,3)).in==nin(1,1))
1985             bllst(connectmat(jj,3)).in(ww)=ninnout(1,1)
1986             ww=find(bllst(connectmat(jj,3)).in2==nin(1,1))
1987             bllst(connectmat(jj,3)).in2(ww)=ninnout(1,1)
1988
1989             ww=find(bllst(connectmat(jj,3)).in==nin(1,2))
1990             bllst(connectmat(jj,3)).in(ww)=ninnout(1,2)
1991             ww=find(bllst(connectmat(jj,3)).in2==nin(1,2))
1992             bllst(connectmat(jj,3)).in2(ww)=ninnout(1,2)
1993             //
1994             if bllst(connectmat(jj,3)).nzcross==nin(1,1) then
1995                bllst(connectmat(jj,3)).nzcross=ninnout(1,1)
1996             end
1997             if bllst(connectmat(jj,3)).nzcross==nin(1,2) then
1998                bllst(connectmat(jj,3)).nzcross=ninnout(1,2)
1999             end
2000             if bllst(connectmat(jj,3)).nmode==nin(1,1) then
2001                bllst(connectmat(jj,3)).nmode=ninnout(1,1)
2002             end
2003             if bllst(connectmat(jj,3)).nmode==nin(1,2) then
2004                bllst(connectmat(jj,3)).nmode=ninnout(1,2)
2005             end
2006             //
2007             ww=find(bllst(connectmat(jj,3)).out==nin(1,1))
2008             bllst(connectmat(jj,3)).out(ww)=ninnout(1,1)
2009             ww=find(bllst(connectmat(jj,3)).out2==nin(1,1))
2010             bllst(connectmat(jj,3)).out2(ww)=ninnout(1,1)
2011
2012             ww=find(bllst(connectmat(jj,3)).out==nin(1,2))
2013             bllst(connectmat(jj,3)).out(ww)=ninnout(1,2)
2014             ww=find(bllst(connectmat(jj,3)).out2==nin(1,2))
2015             bllst(connectmat(jj,3)).out2(ww)=ninnout(1,2)
2016             //
2017             ww=find(bllst(connectmat(jj,3)).out==0)
2018             if (ww<>[]&min(bllst(connectmat(jj,3)).in(:))>0) then
2019                bllst(connectmat(jj,3)).out(ww)=sum(bllst(connectmat(jj,3)).in(:))
2020             end
2021             ww=find(bllst(connectmat(jj,3)).out2==0)
2022             if (ww<>[]&min(bllst(connectmat(jj,3)).in2(:))>0) then
2023                bllst(connectmat(jj,3)).out2(ww)=sum(bllst(connectmat(jj,3)).in2(:))
2024             end
2025         end
2026      end
2027
2028      //if failed then display message
2029      if ~findflag then 
2030         messagebox(msprintf(_('I cannot find a link with undetermined size.\n'+..
2031                               'My guess is that you have a block with unconnected \n'+..
2032                               'undetermined output ports.')),"modal","error");
2033         ok=%f;return;
2034      end
2035   end
2036 endfunction
2037
2038 function id = getBlockIds(path)
2039     // Return a block id path from a block index path
2040     //
2041     // path: the path in the index form
2042     // id: th path in the uid form
2043     
2044     scs_m; // check scs_m access
2045     id=[];
2046     
2047     k = path(:);
2048     for i = k
2049         b = scs_m.objs(i);
2050         if typeof(b) == "Block" &  size(scs_m.objs(i).doc) >= 1 then
2051             id($ + 1) = scs_m.objs(i).doc(1);
2052         end
2053         if typeof(b.model.rpar) == "diagram" then
2054            scs_m = b.model.rpar; 
2055         end
2056     end
2057 endfunction
2058
2059 //19/01/07, Alan : under_connection show bad link and returns two dimensions now
2060 function ninnout=under_connection(path_out,prt_out,nout,path_in,prt_in,nin,flagg)
2061 // alert for badly connected blocks
2062 // path_out : Path of the "from block" in scs_m
2063 // path_in  : Path of the "to block" in scs_m
2064 //!
2065
2066   if path_in==-1 then
2067     msg = "<html><body>";
2068     msg = msg + gettext("One of this block output has negative size.<br />Please check.");
2069     msg = msg + "</body></html>";
2070     hilite_path(path_out, msg);
2071     ninnout=0
2072     return
2073   end
2074
2075   if path_in==-2 then
2076     msg = "<html><body>";
2077     msg = msg + gettext("Block input has negative size:");
2078     msg = msg + "<ul>";
2079     msg = msg + "<li>" + msprintf(gettext("Input port %s size is: %s"), string(prt_out), sci2exp(nout)) + "</li>";
2080     msg = msg + "</ul>";
2081     msg = msg + "</body></html>";
2082     hilite_path(path_out, msg);
2083     ninnout=0
2084     return
2085   end
2086
2087 // different use case (Unable to report on a non opened diagram)
2088   if isdef('Code_gene_run') then
2089     messagebox([gettext("Unable to report an error into a SuperBlock"); gettext("Please compile the diagram to report the error.")], "Compilation error", "error", "modal");
2090     ninnout=0
2091     return
2092   end
2093   
2094   msg = "<html><body>";
2095   if flagg==1 then
2096     msg = msg + gettext("<em>Please update the diagram to avoid this warning.</em><br />Block output port has a non-determined size:");
2097     msg = msg + "<ul>";
2098     msg = msg + "<li>" + msprintf(gettext("Output port %s size is: %s"), string(prt_out), sci2exp(nout)) + "</li>";
2099     msg = msg + "<li>" + msprintf(gettext("Input port %s size is: %s"), string(prt_in), sci2exp(nin)) + "</li>";
2100   else
2101     msg = msg + gettext("<em>Please update the diagram to avoid this warning.</em><br />Block output port has a non-determined type:");
2102     msg = msg + "<ul>";
2103     msg = msg + "<li>" + msprintf(gettext("Output port %s type."), string(prt_out)) + "</li>";
2104     msg = msg + "<li>" + msprintf(gettext("Input port %s type."), string(prt_in)) + "</li>";
2105   end
2106   msg = msg + "</ul>";
2107   msg = msg + "</body></html>";  
2108   hilite_path(path_out, msg)
2109   
2110   if or(path_in<>path_out) then
2111     msg = "<html><body>";
2112     if flagg==1 then
2113       msg = msg + gettext("<em>Please update the diagram to avoid this warning.</em><br />Block input port has a non-determined size:");
2114       msg = msg + "<ul>";
2115       msg = msg + "<li>" + msprintf(gettext("Output port %s size is: %s"), string(prt_out), sci2exp(nout)) + "</li>";
2116       msg = msg + "<li>" + msprintf(gettext("Input port %s size is: %s"), string(prt_in), sci2exp(nin)) + "</li>";
2117     else
2118       msg = msg + gettext("<em>Please update the diagram to avoid this warning.</em><br />Block input port has a non-determined type:");
2119       msg = msg + "<ul>";
2120       msg = msg + "<li>" + msprintf(gettext("Output port %s type."), string(prt_out)) + "</li>";
2121       msg = msg + "<li>" + msprintf(gettext("Input port %s type."), string(prt_in)) + "</li>";
2122     end
2123     msg = msg + "</ul>";
2124     msg = msg + "</body></html>";
2125     hilite_path(path_in, msg)
2126   end
2127   
2128   mess=msprintf(_('Highlighted block(s) have connected ports \n'+..
2129             'with  sizes that cannot be determined by the context.\n'+..
2130             'What is the size of this link?'))
2131         
2132   if flagg==1 then
2133     ninnout=evstr(dialog(mess,'[1,1]'))
2134   else 
2135     ninnout=evstr(dialog(mess,'1'))
2136   end
2137 endfunction
2138
2139 function [clkconnect,exe_cons]=pak_ersi(connectmat,clkconnect,..
2140                                         typ_r,typ_l,outoin,outoinptr,tblock,typ_cons,clkptr)
2141
2142   //add every event to the time event because time includes all events
2143   all_out=[]
2144   for k=1:size(clkptr,1)-1
2145     if ~typ_l(k) then
2146       kk=[1:(clkptr(k+1)-clkptr(k))]'
2147       all_out=[all_out;[k*ones(kk),kk]]
2148     end
2149   end
2150   all_out=[all_out;[0,0]]
2151
2152   //add time event if needed
2153   ind=find(tblock)
2154   ind=ind(:)
2155   for k=ind'
2156     clkconnect=[clkconnect;[all_out,ones(all_out)*[k,0;0,0]]]
2157   end
2158   if show_trace then mprintf('c_pass4444:\t%f\n', timer()),end
2159   ind1=find(typ_cons)
2160   ind=[ind;ind1(:)]
2161   exe_cons=[ind,zeros(ind)]
2162
2163   vec=-ones(1,nblk);
2164   vec(ind)=0
2165   [r,ok]=newc_tree4(vec,outoin,outoinptr,typ_r)
2166   
2167   exe_cons=[exe_cons;r]
2168
2169   if show_trace then mprintf('c_pass4445:\t%f\n', timer()),end
2170
2171   [clkr,clkc]=size(clkconnect);
2172   mm=max(clkconnect(:,2))+1;
2173   cll=clkconnect(:,1)*mm+clkconnect(:,2);
2174   [cll,ind]=gsort(-cll);
2175   clkconnect=clkconnect(ind,:);
2176   if cll<>[] then mcll=max(-cll)+1, else mcll=1;end
2177   cll=[-1;-cll;mcll];
2178   ii=find(cll(2:$)-cll(1:$-1)<>0)
2179
2180   for k=1:size(ii,'*')-1
2181     oo=[ii(k):ii(k+1)-1]
2182     vec=-ones(1,nblk);
2183     vec(clkconnect(oo,3))=0
2184     [r,ok]=newc_tree4(vec,outoin,outoinptr,typ_r)
2185     m=size(r,1)
2186     r=[clkconnect(ii(k),1)*ones(m,1),clkconnect(ii(k),2)*ones(m,1),r]
2187     clkconnect=[clkconnect;r]
2188   end
2189   // temporary fix to take care of conditional blocks inherting from
2190   // constants: make these blocks always active
2191
2192   ind=setdiff(find(typ_l),clkconnect(:,3))
2193   ind=ind(:)
2194   for k=ind'
2195     clkconnect=[clkconnect;[all_out,ones(all_out)*[k,0;0,0]]]
2196   end
2197   // end of  temoprary fix
2198   if show_trace then mprintf('c_pass4446:\t%f\n', timer()),end 
2199 endfunction
2200
2201 function [r,ok]=tree4(vec,outoin,outoinptr,typ_r)
2202 //compute blocks which inherit
2203   ok=%t;
2204   nb=size(vec,'*');
2205   r=[];
2206   for j=1:nb-1
2207     fini=%t
2208     for i=1:nb
2209       if vec(i)==j-1 then 
2210         for k=outoinptr(i):outoinptr(i+1)-1
2211           ii=outoin(k,1);
2212           if (vec(ii)>-1)|typ_r(ii) then
2213             fini=%f;
2214             vec(ii)=j;   
2215           end
2216           if typ_r(ii) then 
2217             r=[r;outoin(k,:)]
2218           end
2219         end
2220       end
2221     end
2222     if fini then break;end
2223   end
2224 endfunction
2225
2226 function [bllst,inplnk,outlnk,clkptr,cliptr,inpptr,outptr,dep_u,dep_uptr,dep_t,..
2227           typ_l,typ_r,typ_m,tblock,typ_cons,typ_zx,ok]=mini_extract_info(bllst,..
2228           connectmat,clkconnect)
2229   ok=%t
2230   nbl=length(bllst)
2231   clkptr=zeros(nbl+1,1);clkptr(1)=1
2232   cliptr=clkptr;inpptr=cliptr;outptr=inpptr;
2233   fff=ones(nbl,1)==1
2234   typ_l=fff;typ_r=fff;typ_cons=fff;typ_m=fff;typ_zx=fff;
2235   dep_t=ones(nbl,1)==1;
2236   dep_u=[];dep_uptr=1;
2237   tblock=fff
2238   //tblock=[]  // specifies blocks that must be connected to time event.
2239   //
2240   for i=1:nbl
2241     ll=bllst(i)
2242     if (ll.state==[]&ll.nzcross==0) then typ_zx(i)=%f;end
2243     inpnum=ll.in;outnum=ll.out;cinpnum=ll.evtin;coutnum=ll.evtout;
2244     //    
2245     if cinpnum==[] then
2246       // this block inherits
2247       //ok=%f
2248       
2249       typ_r(i)=~ll.dep_ut($)
2250       tblock(i)=ll.dep_ut($)
2251       //if block depends on time but has no event input port
2252       if ~ll.dep_ut($) then 
2253         //inherits from the inputs
2254         cinpnum=ones(inpnum)
2255         bllst(i).evtin=cinpnum  //XXXXXXXXXXXXXXXXXXXXX
2256       end
2257       //
2258     else
2259       tblock(i)=ll.dep_ut($);typ_r(i)=%f
2260     end
2261     inpptr(i+1)=inpptr(i)+size(inpnum,'*')
2262     outptr(i+1)=outptr(i)+size(outnum,'*')
2263     cliptr(i+1)=cliptr(i)+size(cinpnum,'*')
2264     clkptr(i+1)=clkptr(i)+size(coutnum,'*')
2265     //
2266
2267     typ_l(i)=ll.blocktype=='l';typ_m(i)=ll.blocktype=='m';
2268     typ_cons(i)=cinpnum==[]&inpnum==[]&~ll.dep_ut($)
2269 //test of the dep_ut size
2270     sizenin=size(ll.in,'*');
2271     if (size(ll.dep_ut,'*') <> 2) then
2272       if ( size(ll.dep_ut(1:$-1),'*') <> sizenin) then
2273         messagebox(msprintf(_('the dep_ut size of the %s block is not correct.\n'+..
2274                               'It should be a colon vector of size %d.'),..
2275                             ll.sim(1),sizenin+1),"modal","error");
2276         ok=%f;   
2277       end
2278     end
2279
2280     dep_t(i)=ll.dep_ut($);  
2281     
2282     if (size(ll.dep_ut,'*') == 2) then
2283       if (sizenin == 1) then
2284         dep_u($+1)=ll.dep_ut(1);
2285         dep_uptr($+1)=dep_uptr($)+1;
2286       elseif (sizenin > 1) then
2287         dep_u=[dep_u;ones(sizenin,1)==1*ll.dep_ut(1)];
2288         dep_uptr($+1)=dep_uptr($)+sizenin;
2289       else
2290         dep_uptr($+1)=dep_uptr($);
2291       end
2292     else  
2293       dep_u_i=ll.dep_ut(1:$-1);
2294       dep_u=[dep_u;dep_u_i(:)];
2295       dep_uptr($+1)=dep_uptr($)+sizenin;
2296     end
2297   
2298     //
2299   end
2300   if show_trace then mprintf('c_pass22222222:\t%f\n', timer()),end //'
2301   nlnk=size(connectmat,1)
2302   inplnk=zeros(inpptr($)-1,1);outlnk=zeros(outptr($)-1,1);ptlnk=1;
2303
2304   for jj=1:nlnk
2305     ko=outlnk(outptr(connectmat(jj,1))+connectmat(jj,2)-1)
2306     ki=inplnk(inpptr(connectmat(jj,3))+connectmat(jj,4)-1)
2307     if ko<>0 & ki<>0 then
2308       if ko>ki then 
2309         outlnk(outlnk>ko)=outlnk(outlnk>ko)-1
2310         outlnk(outlnk==ko)=ki
2311         inplnk(inplnk>ko)=inplnk(inplnk>ko)-1
2312         inplnk(inplnk==ko)=ki
2313         ptlnk=-1+ptlnk
2314       elseif ki>ko
2315         outlnk(outlnk>ki)=outlnk(outlnk>ki)-1
2316         outlnk(outlnk==ki)=ko
2317         inplnk(inplnk>ki)=inplnk(inplnk>ki)-1
2318         inplnk(inplnk==ki)=ko
2319         ptlnk=-1+ptlnk
2320       end
2321     elseif ko<>0 then
2322       inplnk(inpptr(connectmat(jj,3))+connectmat(jj,4)-1)=ko
2323     elseif ki<>0 then
2324       outlnk(outptr(connectmat(jj,1))+connectmat(jj,2)-1)=ki
2325     else
2326       outlnk(outptr(connectmat(jj,1))+connectmat(jj,2)-1)=ptlnk
2327       inplnk(inpptr(connectmat(jj,3))+connectmat(jj,4)-1)=ptlnk
2328       ptlnk=1+ptlnk
2329     end
2330   end
2331
2332   //store unconnected outputs, if any, at the end of outtb
2333   for unco=find(outlnk==0);
2334     outlnk(unco)=max(outlnk)+1
2335   end
2336
2337   //store unconnected inputs, if any, at the end of outtb
2338   for unco=find(inplnk==0);
2339     inplnk(unco)=max([inplnk;max(outlnk)])+1
2340   end
2341 endfunction
2342
2343 function [evoutoin,evoutoinptr]=synch_clkconnect(typ_l,clkconnect)
2344   nblk=size(typ_l,'*')
2345   evoutoin=[];evoutoinptr=1
2346   for i=1:nblk
2347     if typ_l(i) then
2348       dd=clkconnect(clkconnect(:,1)==i,3)
2349     else 
2350       dd=[]
2351     end
2352     evoutoin=[evoutoin;dd]
2353     evoutoinptr=[evoutoinptr;evoutoinptr($)+size(dd,'*')]
2354   end
2355 endfunction
2356
2357 function   clkconnect=cleanup(clkconnect)
2358   mm=max(clkconnect)+1
2359   cc=clkconnect(:,4)+mm*clkconnect(:,3)+clkconnect(:,2)*mm^2+..
2360      clkconnect(:,1)*mm^3
2361   [cc1,ind]=gsort(-cc)
2362   clkconnect=clkconnect(ind,:)
2363   ind=find(cc1(2:$)-cc1(1:$-1)==0)
2364   clkconnect(ind,:)=[]
2365 endfunction
2366
2367 //function mat=cleanup1(mat)
2368 //  mm=max(mat)+1
2369 //  cc=mat(:,1)*mm
2370 //  [cc1,ind]=gsort(-cc)
2371 //  mat=mat(ind,:)
2372 //  ind=find(cc1(2:$)-cc1(1:$-1)==0)
2373 //  mat(ind,:)=[]
2374 //endfunction
2375
2376 function vec=intersection(vec1,vec2)
2377   vec=[]
2378   for i=1:size(vec1,1)
2379     if find(vec1(i)==vec2)~=[] then
2380       vec=[vec;vec1(i)]
2381     end
2382   end
2383 endfunction
2384
2385 function  [r,ok]=newc_tree2(vec,outoin,outoinptr,dep_u,dep_uptr)
2386   dd=zeros(dep_u);dd(dep_u)=1;
2387   [r,ok2]=ctree2(vec,outoin,outoinptr,dd,dep_uptr)
2388   ok=ok2==1
2389 endfunction
2390
2391 function  [r,ok]=new_tree2(vec,outoin,outoinptr,dep_u,dep_uptr)
2392   dd=zeros(dep_u);dd(dep_u)=1;
2393   [r,ok2]=sci_tree2(vec,outoin,outoinptr,dd)
2394   ok=ok2==1
2395 endfunction
2396
2397 function  [r,ok]=new_tree3(vec,dep_ut,typ_l)
2398   dd=zeros(dep_ut);dd(dep_ut)=1;
2399   [r2,ok2]=sci_tree3(vec,dd,typ_l,bexe,boptr,blnk,blptr)
2400   r=r2'
2401   ok=ok2==1
2402 endfunction
2403
2404 function  [r,ok]=newc_tree3(vec,dep_u,dep_uptr,typ_l)
2405   dd=zeros(dep_u);dd(dep_u)=1;
2406   [r2,ok2]=ctree3(vec,dd,dep_uptr,typ_l,bexe,boptr,blnk,blptr)
2407   r=r2'
2408   ok=ok2==1
2409 endfunction
2410
2411 function  [r,ok]=new_tree4(vec,outoin,outoinptr,typ_r)
2412   nd=zeros(size(vec,'*'),(max(outoin(:,2))+1));
2413   ddd=zeros(typ_r);ddd(typ_r)=1; 
2414   [r1,r2]=sci_tree4(vec,outoin,outoinptr,nd,ddd)
2415   r=[r1',r2']
2416   ok=%t
2417 endfunction
2418
2419 function  [r,ok]=newc_tree4(vec,outoin,outoinptr,typ_r)
2420   nd=zeros(size(vec,'*'),(max(outoin(:,2))+1));
2421   ddd=zeros(typ_r);ddd(typ_r)=1; 
2422   [r1,r2]=ctree4(vec,outoin,outoinptr,nd,ddd)  
2423   r=[r1',r2']
2424   ok=%t
2425 endfunction
2426
2427 function [critev]=critical_events(connectmat,clkconnect,dep_t,typ_r,..
2428                                   typ_l,typ_zx,outoin,outoinptr,clkptr)
2429
2430   typ_c=typ_l<>typ_l;
2431   typ_r=typ_r|dep_t
2432   
2433   done1=%f
2434   while ~done1
2435     done1=%t
2436     [clkr,clkc]=size(clkconnect);
2437     mm=max(clkconnect)+1;
2438
2439     cll=clkconnect(:,1)*mm+clkconnect(:,2);
2440     [cll,ind]=gsort(-cll);
2441     clkconnect=clkconnect(ind,:);
2442     cll=[-1;-cll;mm];
2443     ii=find(cll(2:$)-cll(1:$-1)<>0)
2444     
2445     for k=1:size(ii,'*')-1
2446       oo=[ii(k):ii(k+1)-1]
2447       vec=-ones(1,nblk);
2448       vec(clkconnect(oo,3))=0
2449       [r,ok]=newc_tree4(vec,outoin,outoinptr,typ_r)
2450       
2451       m=size(r,1)
2452       r=[clkconnect(ii(k),1)*ones(m,1),clkconnect(ii(k),2)*ones(m,1),r]
2453       clkconnect=[clkconnect;r]
2454     end
2455     
2456     done=%f;
2457     while ~done
2458       done=%t;
2459       for jj=find(typ_l&(~typ_c));
2460         if ~or(jj==clkconnect(:,3)) then
2461           typ_r(clkconnect(find(jj==clkconnect(:,1)),3))=%t
2462           clkconnect(find(jj==clkconnect(:,1)),:)=[];
2463           typ_c(jj)=%t;
2464           done1=%f
2465           done=%f
2466         end
2467       end
2468     end
2469   end 
2470   critev=zeros(clkptr($)-1,1);
2471   for bb=1:size(clkptr,1)-1
2472     for i=clkptr(bb):clkptr(bb+1)-1
2473       if or(typ_zx(clkconnect(find((clkconnect(:,1)==bb)&..
2474                                    (clkconnect(:,2)==i-clkptr(bb)+1)),3))) then
2475         critev(i)=1
2476       end
2477     end
2478   end 
2479 endfunction
2480
2481 // adjust_typ: It resolves positives and negatives port types.
2482 //                 Its Algorithm is based on the algorithm of adjust_inout
2483 // Fady NASSIF: 14/06/2007
2484
2485 function [ok,bllst]=adjust_typ(bllst,connectmat)
2486
2487 for i=1:length(bllst)
2488   if size(bllst(i).in,1)<>size(bllst(i).intyp,2) then
2489     bllst(i).intyp=bllst(i).intyp(1)*ones(size(bllst(i).in,1),1);
2490   end
2491   if size(bllst(i).out,1)<>size(bllst(i).outtyp,2) then
2492     bllst(i).outtyp=bllst(i).outtyp(1)*ones(size(bllst(i).out,1),1);
2493   end
2494 end
2495 nlnk=size(connectmat,1) 
2496 for hhjj=1:length(bllst)+1
2497   for hh=1:length(bllst)+1 
2498     ok=%t
2499     for jj=1:nlnk 
2500       nnout(1,1)=bllst(connectmat(jj,1)).out(connectmat(jj,2))
2501       nnout(1,2)=bllst(connectmat(jj,1)).out2(connectmat(jj,2))
2502       nnin(1,1)=bllst(connectmat(jj,3)).in(connectmat(jj,4))
2503       nnin(1,2)=bllst(connectmat(jj,3)).in2(connectmat(jj,4))
2504       outtyp = bllst(connectmat(jj,1)).outtyp(connectmat(jj,2))
2505       intyp = bllst(connectmat(jj,3)).intyp(connectmat(jj,4))
2506       
2507       //first case : types of source and
2508       //             target ports are explicitly informed
2509       //             with positive types
2510       if (intyp>0 & outtyp>0) then
2511         //if types of source and target port doesn't match and aren't double and complex
2512         //then call bad_connection, set flag ok to false and exit
2513         
2514         if intyp<>outtyp then
2515           if (intyp==1 & outtyp==2) then
2516             bllst(connectmat(jj,3)).intyp(connectmat(jj,4))=2;
2517           elseif (intyp==2 & outtyp==1) then
2518             bllst(connectmat(jj,1)).outtyp(connectmat(jj,2))=2;
2519           else
2520             bad_connection(corinv(connectmat(jj,1)),connectmat(jj,2),..
2521                 nnout,outtyp,..
2522                 corinv(connectmat(jj,3)),connectmat(jj,4),..
2523                 nnin,intyp,1)
2524             ok=%f;
2525             return
2526           end
2527         end
2528         
2529         //second case : type of source port is
2530         //              positive and type of
2531         //              target port is negative
2532       elseif(outtyp>0&intyp<0) then
2533         //find vector of input ports of target block with
2534         //type equal to intyp
2535         //and assign it to outtyp
2536         ww=find(bllst(connectmat(jj,3)).intyp==intyp)
2537         bllst(connectmat(jj,3)).intyp(ww)=outtyp
2538         
2539         //find vector of output ports of target block with
2540         //type equal to intyp
2541         //and assign it to outtyp
2542         ww=find(bllst(connectmat(jj,3)).outtyp==intyp)
2543         bllst(connectmat(jj,3)).outtyp(ww)=outtyp
2544         
2545         //third case : type of source port is
2546         //             negative and type of
2547         //             target port is positive
2548       elseif(outtyp<0&intyp>0) then
2549         //find vector of output ports of source block with
2550         //type equal to outtyp
2551         //and assign it to intyp
2552         ww=find(bllst(connectmat(jj,1)).outtyp==outtyp)
2553         bllst(connectmat(jj,1)).outtyp(ww)=intyp
2554         
2555         //find vector of input ports of source block with
2556         //type equal to size outtyp
2557         //and assign it to intyp
2558         ww=find(bllst(connectmat(jj,1)).intyp==outtyp)
2559         bllst(connectmat(jj,1)).intyp(ww)=intyp
2560         
2561         
2562         //fourth (& last) case : type of both source 
2563         //                      and target port are negatives
2564       else
2565         ok=%f //set flag ok to false
2566       end
2567     end
2568     if ok then return, end //if ok is set true then exit adjust_typ
2569   end
2570   //if failed then display message
2571   messagebox(msprintf(_('Not enough information to find port type.\n'+..
2572                         'I will try to find the problem.')),"modal","info");
2573   findflag=%f 
2574   for jj=1:nlnk 
2575     nouttyp=bllst(connectmat(jj,1)).outtyp(connectmat(jj,2))
2576     nintyp=bllst(connectmat(jj,3)).intyp(connectmat(jj,4))
2577     
2578     //loop on the two dimensions of source/target port
2579     //only case : target and source ports are both
2580     //            negatives or null
2581     if nouttyp<=0 & nintyp<=0 then
2582       findflag=%t;
2583       //
2584       inouttyp=under_connection(corinv(connectmat(jj,1)),connectmat(jj,2),nouttyp,..
2585           corinv(connectmat(jj,3)),connectmat(jj,4),nintyp,2)                      
2586       //
2587       if inouttyp<1|inouttyp>8 then ok=%f;return;end
2588       //
2589       ww=find(bllst(connectmat(jj,1)).outtyp==nouttyp)
2590       bllst(connectmat(jj,1)).outtyp(ww)=inouttyp
2591       
2592       //
2593       ww=find(bllst(connectmat(jj,1)).intyp==nouttyp)
2594       bllst(connectmat(jj,1)).intyp(ww)=inouttyp
2595       
2596       ww=find(bllst(connectmat(jj,3)).intyp==nintyp)
2597       bllst(connectmat(jj,3)).intyp(ww)=inouttyp
2598       //
2599       ww=find(bllst(connectmat(jj,3)).outtyp==nintyp)
2600       bllst(connectmat(jj,3)).outtyp(ww)=inouttyp
2601       
2602       //
2603     end
2604   end
2605   //if failed then display message
2606   if ~findflag then 
2607     messagebox(msprintf(_('I cannot find a link with undetermined size.\n'+..
2608                           'My guess is that you have a block with unconnected \n'+..
2609                           'undetermined types.')),"modal","error");
2610     ok=%f;return;
2611   end
2612 end
2613 endfunction
2614