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