3 // Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
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.
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.
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.
19 // See the file ../license.txt
22 function cpr=c_pass2(bllst,connectmat,clkconnect,cor,corinv,flag)
23 // cor ; correspondance table with initial block ordering
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,
44 // 14- number of zero crossings
45 // 15- number of modes
46 // 16- empty list (equation for modelica blocks)
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
53 // clkconnect: same as connectmat but for clock scicopaths.
55 // define some constants
56 if argn(2) <6 then flag="verbose",end
58 if show_trace then mprintf("c_pass1:\t%f\n", timer()),end
64 messagebox(_("No block can be activated"),"modal","error")
70 //correction of clkconnect.. Must be done before
71 clkconnect(find(clkconnect(:,2)==0),2)=1;
76 if exists("%scicos_solver")==0 then %scicos_solver=0,end
78 clkptr=1,cliptr=1,typ_l=[],
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
90 [outoin,outoinptr]=conn_mat(inpptr,outptr,inplnk,outlnk)
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)
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)
108 mprintf("fin de paksazi")
112 if show_trace then mprintf("c_pass31:\t%f\n", timer()),end
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);
124 messagebox(_("Problem in port size or type."),"modal","error");
129 if show_trace then mprintf("c_pass41:\t%f\n", timer()),end
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)
135 if show_trace then mprintf("c_pass50:\t%f\n", timer()),end
137 [execlk_cons]=discard(clkptr,cliptr,clkconnect,exe_cons)
139 clkconnect=[];exe_cons=[]
141 if show_trace then mprintf("c_pass501:\t%f\n", timer()),end
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);
153 if show_trace then mprintf("c_pass51:\t%f\n", timer()),end
154 //form scicos arguments
161 zcptr(i+1)=zcptr(i)+typ_z(i)
162 modptr(i+1)=modptr(i)+sign(typ_z(i))*typ_mod(i);
165 ztyp=sign(typ_z0) //completement inutile pour simulation
166 // utiliser pour la generation de code
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");
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);
186 [tevts,evtspt,pointi]=init_agenda(initexe,clkptr)
187 if show_trace then mprintf("c_pass61:\t%f\n", timer()),end
189 //mod=0*ones(modptr($)-1,1)
192 outtb=buildouttb(lnksz,lnktyp);
196 if max(funtyp)>10000 &%scicos_solver==0 then
197 warning(_("Diagram contains implicit blocks, compiling for implicit Solver."))
200 if or(%scicos_solver==[100, 101, 102]) then xc0=[xc0;xcd0],end
212 cpr=scicos_cpr(state=state,sim=sim,cor=cor,corinv=corinv);
214 if show_trace then mprintf("c_pass71:\t%f\n", timer()),end
219 //donne les sources d'activation du sch�ma
220 function [vec_clk]=get_clocks(clkconnect,clkptr)
222 if (find(clkconnect(:,1)==0) ~=[]) then
223 //activation continue
224 vec_clk=[vec_clk;0 0];
226 for blk=1:size(clkptr,1)-1
228 for port=1:clkptr(blk+1)-clkptr(blk)
229 vec_clk=[vec_clk; blk port];
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)
241 n_vc0=find(vec_clk==i)
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)
250 for k=1:size(vec_clk1,1)
251 if find(vec_clk1(k)==vec_clk0)==[] then
252 vec_clk0($+1)=vec_clk1(k)
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)
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))
270 vec_clk0=[vec_clk0;primary(k,:)]
273 n_vc1=size(vec_clk1,1)
276 f=find(vec_clk1(k,1)==vec_clk0(:,1))
278 vec_clk0=[vec_clk0;vec_clk1(k,:)]
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)
293 ordclk=[ordclk;ordoclk0];
294 ordptr($+1)=ordptr($)+size(ordoclk0,1);
295 blocs_traites=[blocs_traites;k] //k
297 m=max(find(blocs_traites==k))
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:$)]
303 ind=find(blocs_traites <k)
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]
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:$)]
315 ordclk=[ordoclk0;ordclk]
316 ordptr=[1;ordptr+size(ordoclk0,1)]
317 blocs_traites=[k;blocs_traites]
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:$)]
328 ind=find(blocs_traites <k)
330 if m==size(blocs_traites,1) then
331 ordptr($+1)=ordptr($)+size(ordoclk0,1);
332 blocs_traites=[blocs_traites;k]
335 ordptr=[ordptr(1:m+1);ordptr(m+1:$)+size(ordoclk0,1)]
336 blocs_traites=[blocs_traites(1:m);k;blocs_traites(m+1:$)]
338 ordptr=[1;ordptr+size(ordoclk0,1)]
339 blocs_traites=[k;blocs_traites]
347 //donne les blocs activant blk
348 function [parents]=get_parents(parents,blk)
349 f=find(clkconnect(:,3)==blk)
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]
364 function [blks,blksptr]=depend_on(connectmat,dep_u,dep_uptr)
365 // returns the blocks on which depend directly a block
367 for i=1:size(dep_uptr,"*")-1
368 indport=find(dep_u(dep_uptr(i):dep_uptr(i+1)-1));
371 f=[f,find(connectmat(:,3)==i&connectmat(:,4)==j)]
373 blkis=unique(connectmat(f,1))
375 blksptr=[blksptr;blksptr($)+size(blkis,"*")]
379 function [eblks,eblksptr]=update_event_depend_on(eblks,eblksptr,clkconnect,cliptr,bls)
380 // updates the blocks activating a block
382 eblksptr($:nb1)=eblksptr($)
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
395 function [fblks,fblksptr]=update_event_activates(fblks,fblksptr,clkconnect,clkptr,bls)
396 // returns the blocks activated by a block (assumed typ_l)
398 fblksptr($:nb1)=fblksptr($)
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
411 function [eblks,eblksptr]=event_depend_on(clkconnect,cliptr)
412 // returns the blocks activating a block
414 for i=1:size(cliptr,"*")-1
415 f=find(clkconnect(:,3)==i)
416 blkis=unique(clkconnect(f,1))
418 eblksptr=[eblksptr;eblksptr($)+size(blkis,"*")]
423 function [fblks,fblksptr]=event_activates(clkconnect,clkptr)
424 // returns the blocks activated by a block of type typ_l
426 for i=1:size(typ_l,"*")
428 f=find(clkconnect(:,1)==i)
429 blkis=unique(clkconnect(f,3))
434 fblksptr=[fblksptr;fblksptr($)+size(blkis,"*")]
439 function uni=merge_mat(m1,m2)
440 // for m1 and m2 with two columns containing >=0 values
443 [j,ind]=unique(uni(:,1)*n+uni(:,2))
444 uni=uni(-gsort(-ind),:)
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)
451 nblock=size(typ_l,1)+1
452 g=find(clkconnect(:,1)==bl)
456 clkconnect=[clkconnect;xx]
459 g=find(connectmat(:,3)==bl)
461 xx(:,3)=nblock*ones(size(xx,1),1)
462 connectmat=[connectmat;xx]
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
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)
477 function [childs]=get_allchilds(primary,fblks,fblksptr,typ_l)
481 taille=size(childs,"*")
483 while oldtaille<>taille
485 for i=childs(typ_l(childs))
486 bb=fblks(fblksptr(i):fblksptr(i+1)-1)
487 childs=union(childs,bb)
489 taille=size(childs,"*")
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])
499 vec(lclkconnect(:,1))=1
504 oldvec=vec // not optimal to use large v
505 vec(lclkconnect(:,2))=vec(lclkconnect(:,1))+1
506 if and(vec==oldvec) then
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)
515 ordclk=[];ordptr=1;cord=[];
518 vbllst=1:length(bllst)
520 zz=get_clocks(clkconnect,clkptr)
521 //testing event algebraic loops
522 ok=is_alg_event_loop(typ_l,clkconnect)
524 disp(mprintf("%s: alg_event_loop failed", "c_pass2"));
525 messagebox(_("Algebraic loop on events."),"modal","error");
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)
538 nblock=size(typ_l,1); // number of blocks in diagram
543 blk=todo(1,1);port=todo(1,2);
544 if blk==0 |find(definedfields(lordclk)==clkptr(blk)+port-1)==[] then
546 f=find(clkconnect(:,1)==blk&clkconnect(:,2)==port);
548 primary0=clkconnect(f,3);prt0=clkconnect(f,4)
553 if show_comment then mprintf("Processing blk " + string(blk) + " port "+...
554 + string(port) + "\n"),end
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
561 primary=unique(primary0)
563 [balg,vec]=ini_ordo3(primary)
565 disp(mprintf("%s: ini_ordo (3) failed", "c_pass2"));
566 messagebox(_("Algebraic loop."),"modal","error"),
571 pvec=vec(primary)+.5*typ_l(primary) // same order typ_l to the right
574 lp=find(typ_l(primary))
580 for J=primary(i+1:$)'
581 if intersect(get_allchilds(primary(i)),get_allchilds(J))<>[] then
586 if bouclalg then break,end
589 if show_comment&bouclalg then mprintf("found intersect \n"),end
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)
597 if show_comment then mprintf("found non convergence\n"),pause,end
598 i=lp(1) // first typ_l
600 disp(mprintf("%s: ini_ordo (2) failed", "c_pass2"));
601 messagebox(_("Algebraic loop."),"modal","error")
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
619 clkconnect(f,3)=nblock
621 mprintf("duplicating pivot"+string(bl)+" to obtain "+string(nblock) + "\n"),
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)
628 blks_bl=blks(blksptr(bl):blksptr(bl+1)-1)
630 blksptr($+1)=blksptr($)+size(blks_bl,"*")
632 bls=[bl,nblock,fblks(fblksptr(bl):fblksptr(bl+1)-1)']
634 update_event_depend_on(eblks,eblksptr,clkconnect,cliptr,bls)
635 bl=nblock // use new block as pivot
641 f=find(clkconnect(:,1)==blk&clkconnect(:,2)==port&clkconnect(:,3)==bli)
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"),
652 clkconnect=[clkconnect;xx]
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)
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
670 if ~modif then messagebox(_("Algebraic loop."),"modal","error"),ok=%f,return,end
672 primary0=primary0(k);
674 [primary0,prt0]=aggregate(primary0,prt0)
675 [mi,In]=gsort(-Vec(primary0))
677 lordclk(clkptr(blk)+port-1)=[primary0(In),prt0(In)]
680 mprintf("for blk port "+string(blk)+" "+string(port)+..
681 " ordclk is :\n"), disp(lordclk(clkptr(blk)+port-1)),
684 cord=[primary0(In),prt0(In)]
690 C=[1:clkptr(Bi+1)-clkptr(Bi)]'
693 todo=merge_mat(todo,L)
700 mprintf("for blk port "+string(blk)+" "+string(port)+..
701 " ordclk is"), mprintf(lordclk(J) + "\n"),
716 ordclk=[ordclk;lordclk(J)]
717 ordptr=[ordptr;ordptr($)+size(lordclk(J),1)]
720 bllst=list(bllst(vbllst))
724 function [primary0,port0]=aggregate(primary0,port0)
725 primary=discardprimary([primary0,port0]) // must rewrite
726 primary0=primary(:,1);port0=primary(:,2)
730 function [parents]=get_parents2(parents,blk,port)
732 f=find(clkconnect(:,3)==blk)
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
750 //suppression des liens inutiles
751 function [clkconnect,amaj]=find_del_inutile(clkconnect,vec_plus,typ_l)
753 for i=1:size(vec_plus,1)
756 parents=get_parents2([],blk,port)
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
767 mprintf("find_del_inutile:")
768 mprintf("link between blocks "+string(par1)+" and "+string(blk)+..
772 [clkconnect]=del_inutile(clkconnect,par1,n_out,blk,port)
782 function [clkconnect]=del_inutile(clkconnect,blk0,n_out,blk,port)
783 f=find(clkconnect(:,1)==blk0)
786 if clkconnect(f(i),3)==blk & clkconnect(f(i),4)==port then
791 p=get_parents([],blk0)
794 clkconnect($+1,:)=[p(i,1),p(i,2),blk,port]
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
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
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,..
816 nblock=size(typ_l,1);
817 //initialisation de vec
818 //on initialise vec � -1
827 [wprim,wprimptr,g0prim,g0primptr,gprim,gprimptr]=prim_calc(primary)
830 vec=update_vec3(vec,wprim,wprimptr,g0prim,g0primptr,gprim,gprimptr)
832 if and(vec==oldvec2) then
838 if (counter2>n_p+1) then
840 // mprintf('Algebraic Loop detection.')
846 function [bouclalg,vec]=ini_ordo3(primary)
850 nblock=size(typ_l,1);
851 //initialisation de vec
852 //on initialise vec � -1
861 [wprim,wprimptr,g0prim,g0primptr,gprim,gprimptr]=prim_calc(primary)
864 vec=update_vec4(vec,wprim,wprimptr,g0prim,g0primptr,gprim,gprimptr)
866 if and(vec==oldvec2) then
872 if (counter2>n_p+1) then
874 // mprintf('Algebraic Loop detection.')
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,'*')])
890 vec(w(1))=max(vec(w(1)),max(vec(g0))+1)
892 //g=g(find(vec(g)>-1)) //not optimal test max(vec(g))
895 vec(w)=max(vec(w),max(vec(g)))+1
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
909 function [wprim,wprimptr,g0prim,g0primptr,gprim,gprimptr]=prim_calc(primary)
912 g0prim=[];g0primptr=1
915 for i=1:size(primary,"*")
918 w=get_allchilds(w0,fblks,fblksptr,typ_l)
920 w([1,j])=w([j,1]) // put w0 on top
925 wprimptr($+1)=wprimptr($)+size(w,"*")
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)
933 for i1=2:size(w,"*") // 1 is done already
934 px=blksptr(w(i1));py=blksptr(w(i1)+1)-1
938 gprimptr($+1)=gprimptr($)+size(g,"*")
942 function primary=discardprimary(primary)
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),:)=[]
952 // group calls to different ports of the same block.
953 primary=[primary(:,1),2^(primary(:,2)-ones(primary(:,2)))]
957 if size(con,"*")>=2 then
958 iini=[find(con(2:$)-con(1:$-1)<>0),size(primary,1)]
963 clkconnectjj=[clkconnectjj;[primary(ii,1),..
964 mysum(primary(find(primary(:,1)==primary(ii,1)),2))]]
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);
978 if execlk_cons<>[] then
980 no_tu_dep=execlk_cons(:,1)
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)
992 disp(mprintf("%s: scheduling failed", "c_pass2"));
993 messagebox(_("Algebraic loop."),"modal","error");
994 iord=[],oord=[],zord=[],critev=[]
1001 typp=zeros(typ_l);typp(typ_l)=1
1008 for i=[clkptr(ii):clkptr(ii+1)-1]
1009 ext_cord1=[ext_cord1;ordclk([ordptr(i):ordptr(i+1)-1],:)];
1013 if j>size(ext_cord1,1) then break;end
1015 // code to replace faulty unique which reorders
1017 [xx,kkn]=unique(yy);
1018 ext_cord=yy(-gsort(-kkn))
1019 //ext_cord=unique(ext_cord1(:,1)');
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
1026 [ext_cord_old,ok]=newc_tree3(vec,dep_u,dep_uptr,typp);
1028 if or(gsort(ext_cord_old)<>gsort(ext_cord)) then pause,end
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)
1035 if ~ok then mprintf("serious bug, report.");pause;end
1036 // ext_cord=ext_cord(n+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
1049 if typ_z(i)==0 then typ_z(i)=1;fin=0;end
1052 if ~typ_x(i) then typ_x(i)=%t;fin=0;end
1054 if typ_z(i)&typ_x(i) then break,end
1058 //supprimer les blocks a supprimer
1059 ind=find(typ_z(cord(:,1)));
1061 ind=find(typ_x(cord(:,1)));
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);
1070 // 1: important; 0:non
1071 //n=clkptr(nblk+1)-1 //nb d'evenement
1072 n=size(ordptr,"*")-1 //nb d'evenement
1074 //a priori tous les evenemets sont non-importants
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)
1087 function [ord,ok]=tree3(vec,dep_ut,typ_l)
1088 //compute blocks execution tree
1094 if vec(i)==j-1&typ_l(i)<>-1 then
1096 disp(mprintf("%s: tree (3) failed", "c_pass2"));
1097 messagebox(_("Algebraic loop."),"modal","error");ok=%f;ord=[];return;
1101 kk=bexe(boptr(i):boptr(i+1)-1)';
1104 for ii=blnk(blptr(i):blptr(i+1)-1)'
1105 if vec(ii)>-1 & (dep_ut(ii,1) | (typ_l(ii)==1)) then
1111 vec(kk)=j*ones(kk) ; //mprintf(vec)
1114 if fini then break;end
1116 [k,ord]=gsort(-vec);
1120 function [clkconnectj_cons]=discard(clkptr,cliptr,clkconnect,exe_cons)
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),:)=[]
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)
1137 if size(con,"*")>=2 then
1138 iini=[find(con(2:$)-con(1:$-1)<>0),size(clkconnectj,1)]
1143 clkconnectj_cons=[clkconnectj_cons;[clkconnectj(ii,1),..
1144 mysum(clkconnectj(find(clkconnectj(:,1)==clkconnectj(ii,1)),2))]]
1152 if b<>[] then a=sum(b), else a=[], end
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)
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;
1167 xc0=[];xcd0=[];xd0=[];
1173 typ_z=zeros(nbl,1);typ_x=fff;typ_m=fff;typ_mod=zeros(nbl,1);
1180 [ok,bllst]=adjust_inout(bllst,connectmat)
1182 [ok,bllst]=adjust_typ(bllst,connectmat)
1185 // placed here to make sure nzcross and nmode correctly updated
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=[]
1197 if type(ll.sim)==15 then
1199 funtyp(i,1)=ll.sim(2)
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")
1211 inpnum=ll.in;outnum=ll.out;cinpnum=ll.evtin;coutnum=ll.evtout;
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,"*")
1219 if funtyp(i,1)<10000 then
1222 xptr(i+1)=xptr(i)+size(ll.state,"*")
1224 xcd0=[xcd0;X0($/2+1:$)]
1226 xptr(i+1)=xptr(i)+size(ll.state,"*")/2
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
1236 zptr(i+1)=zptr(i)+size(xd0k,"*")
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;
1245 ozptr(i+1)=ozptr(i);
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);
1251 for j=1:ozsz, oxd0($+1)=ll.odstate(j), end;
1252 ozptr(i+1)=ozptr(i)+ozsz;
1254 ozptr(i+1)=ozptr(i);
1257 ozptr(i+1)=ozptr(i);
1260 //add an error message here please !
1261 ozptr(i+1)=ozptr(i);
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")
1274 rpptr(i+1)=rpptr(i)+size(rpark,"*")
1276 //integer parameters
1277 if type(ll.ipar)==1 then
1278 ipar=[ipar;ll.ipar(:)]
1279 ipptr(i+1)=ipptr(i)+size(ll.ipar,"*")
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
1289 opptr(i+1)=opptr(i)+1;
1291 opptr(i+1)=opptr(i);
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);
1297 for j=1:oparsz, opar($+1)=ll.opar(j), end;
1298 opptr(i+1)=opptr(i)+oparsz;
1300 opptr(i+1)=opptr(i);
1303 opptr(i+1)=opptr(i);
1306 //add an error message here please !
1307 opptr(i+1)=opptr(i);
1310 // typ_z(i)=ll.blocktype=='z'
1313 messagebox(msprintf(_("Number of zero crossings in block #%d cannot be determined."),i),"modal","error")
1316 typ_x(i)=ll.state<>[]|ll.blocktype=="x" // some blocks like delay
1317 // need to be in oord even
1320 typ_m(i)=ll.blocktype=="m"
1322 if ll.evtout<>[] then
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)]];
1329 if type(ll.label)==10 then
1330 labels=[labels;ll.label(1)]
1335 if type(ll.uid)==10 then
1336 uids=[uids;ll.uid(1)]
1342 clkconnect=clkconnect(find(clkconnect(:,1)<>0),:);
1344 con=clkptr(clkconnect(:,1))+clkconnect(:,2)-1;
1345 [junk,ind]=gsort(-con);con=-junk;
1346 clkconnect=clkconnect(ind,:);
1348 bclkconnect=clkconnect(:,[1 3]);
1352 r=bclkconnect(find(bclkconnect(:,1)==i),2);
1354 boptr=[boptr;boptr($)+size(r,1)];
1362 r=connectmat(find(connectmat(:,1)==i),3:4);
1364 blptr=[blptr;blptr($)+size(r,1)];
1368 nlnk=size(connectmat,1)
1369 inplnk=zeros(inpptr($)-1,1);outlnk=zeros(outptr($)-1,1);ptlnk=1;
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
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
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
1395 inplnk(inpptr(connectmat(jj,3))+connectmat(jj,4)-1)=ko
1397 outlnk(outptr(connectmat(jj,1))+connectmat(jj,2)-1)=ki
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));
1408 //store unconnected outputs, if any, at the end of outtb
1409 unco=find(outlnk==0);
1411 m=max(find(outptr<=j))
1415 under_connection(corinv(m),n,nm,-1,0,0,1),ok=%f,return,
1417 nm2=bllst(m).out2(n)
1419 under_connection(corinv(m),n,nm2,-1,0,0,1),ok=%f,return,
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
1427 //store unconnected inputs, if any, at the end of outtb
1428 unco=find(inplnk==0);
1430 m=max(find(inpptr<=j))
1434 under_connection(corinv(m),n,nm,-2,0,0,1),ok=%f,return,
1438 under_connection(corinv(m),n,nm2,-2,0,0,1),ok=%f,return,
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
1448 function [outoin,outoinptr]=conn_mat(inpptr,outptr,inplnk,outlnk)
1449 outoin=[];outoinptr=1
1450 nblk=size(inpptr,1)-1
1452 k=outptr(i):outptr(i+1)-1
1455 ii=[ii,find(inplnk==j)]
1459 m=max(find(inpptr<=j))
1461 outoini=[outoini;[m,n]]
1464 outoinptr=[outoinptr;outoinptr($)+jj]
1465 outoin=[outoin;outoini]
1469 function [ord,ok]=tree2(vec,outoin,outoinptr,dep_ut)
1470 //compute blocks execution tree
1479 disp(mprintf("%s: tree (2) failed", "c_pass2"));
1480 messagebox(_("Algebraic loop."),"modal","error");ok=%f;ord=[];return;
1482 for k=outoinptr(i):outoinptr(i+1)-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
1493 if fini then break;end
1496 [k,ord]=gsort(-vec);
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.
1510 //in parameters : bllst : list of blocks
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
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
1523 // bllst : modified list of blocks
1525 //18/05/06, Alan : improvement in order to take into
1526 // account two dimensional port size.
1528 //28/12/06, Alan : type for source port and target port must
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.
1535 //04/01/07, Fady : Can test the case of negatives equals target's dimensions.
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
1541 //10/05/07, Alan : - if-then-else event-select case
1543 function [ok,bllst]=adjust_inout(bllst,connectmat)
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
1551 nlnk=size(connectmat,1) //nlnk is the number of link
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
1558 for jj=1:nlnk //loop on number of link
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
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))
1572 //This Part is done in adjust_typ
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;
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))
1585 // bad_connection(corinv(connectmat(jj,1)),connectmat(jj,2),..
1587 // corinv(connectmat(jj,3)),connectmat(jj,4),..
1595 //loop on the two dimensions of source/target port
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))
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),..
1615 corinv(connectmat(jj,3)),connectmat(jj,4),..
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)
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)
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
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(:))
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(:))
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)
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)
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)
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)
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
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(:))
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(:))
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)
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)
1720 //fourth case : a dimension of source port is
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
1726 ww=bllst(connectmat(jj,1)).in(:)
1728 ww=bllst(connectmat(jj,1)).in2(:)
1731 //test if all size of the ndim dimension of input
1732 //port of the source block is positive
1734 //test if the dimension of the target port
1736 if nin(1,ndim)>0 then
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
1744 bllst(connectmat(jj,1)).out(connectmat(jj,2))=nin(1,ndim)
1746 bllst(connectmat(jj,1)).out2(connectmat(jj,2))=nin(1,ndim)
1748 //else call bad_connection, set flag ok to false and exit
1750 bad_connection(corinv(connectmat(jj,1)),0,0,1,-1,0,0,1)
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
1760 bllst(connectmat(jj,1)).out(connectmat(jj,2))=sum(ww)
1762 bllst(connectmat(jj,1)).out2(connectmat(jj,2))=sum(ww)
1768 //set nww to be the vector of all negative size of input ports
1769 //of the source block
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
1777 bllst(connectmat(jj,1)).out(connectmat(jj,2))=nin(1,ndim)
1779 bllst(connectmat(jj,1)).out2(connectmat(jj,2))=nin(1,ndim)
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,"*")
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
1793 bllst(connectmat(jj,1)).in(find(ww<0))=k
1795 bllst(connectmat(jj,1)).in2(find(ww<0))=k
1797 //else call bad_connection, set flag ok to false and exit
1799 bad_connection(corinv(connectmat(jj,1)),0,0,1,-1,0,0,1)
1803 //set flag ok to false
1810 //fifth case : a dimension of target port is
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
1816 ww=bllst(connectmat(jj,3)).out(:)
1818 ww=bllst(connectmat(jj,3)).out2(:)
1821 //test if all size of the ndim dimension of output
1822 //port of the target block is positive
1824 //test if the dimension of the source port
1826 if nout(1,ndim)>0 then
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
1834 bllst(connectmat(jj,3)).in(connectmat(jj,4))=nout(1,ndim)
1836 bllst(connectmat(jj,3)).in2(connectmat(jj,4))=nout(1,ndim)
1838 //else call bad_connection, set flag ok to false and exit
1840 bad_connection(corinv(connectmat(jj,3)),0,0,1,-1,0,0,1)
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
1850 bllst(connectmat(jj,3)).in(connectmat(jj,4))=sum(ww)
1852 bllst(connectmat(jj,3)).in2(connectmat(jj,4))=sum(ww)
1858 //set nww to be the vector of all negative size of output ports
1859 //of the target block
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
1867 bllst(connectmat(jj,3)).in(connectmat(jj,4))=nout(1,ndim)
1869 bllst(connectmat(jj,3)).in2(connectmat(jj,4))=nout(1,ndim)
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,"*")
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
1883 bllst(connectmat(jj,3)).out(find(ww<0))=k
1885 bllst(connectmat(jj,3)).out2(find(ww<0))=k
1887 //else call bad_connection, set flag ok to false and exit
1889 bad_connection(corinv(connectmat(jj,3)),0,0,1,-1,0,0,1)
1893 //set flag ok to false
1900 //sixth (& last) case : dimension of both source
1901 // and target port are negatives
1903 ok=%f //set flag ok to false
1907 if ok then return, end //if ok is set true then exit adjust_inout
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");
1913 //%%%%% pass 2 %%%%%//
1914 //Alan 19/01/07 : Warning : Behavior have changed, To Be more Tested
1915 findflag=%f //set findflag to false
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))
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
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)
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
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)
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)
1949 if bllst(connectmat(jj,1)).nzcross==nout(1,1) then
1950 bllst(connectmat(jj,1)).nzcross=ninnout(1,1)
1952 if bllst(connectmat(jj,1)).nzcross==nout(1,2) then
1953 bllst(connectmat(jj,1)).nzcross=ninnout(1,2)
1956 if bllst(connectmat(jj,1)).nmode==nout(1,1) then
1957 bllst(connectmat(jj,1)).nmode=ninnout(1,1)
1959 if bllst(connectmat(jj,1)).nmode==nout(1,2) then
1960 bllst(connectmat(jj,1)).nmode=ninnout(1,2)
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)
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)
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)
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)
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)
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)
1993 if bllst(connectmat(jj,3)).nzcross==nin(1,1) then
1994 bllst(connectmat(jj,3)).nzcross=ninnout(1,1)
1996 if bllst(connectmat(jj,3)).nzcross==nin(1,2) then
1997 bllst(connectmat(jj,3)).nzcross=ninnout(1,2)
1999 if bllst(connectmat(jj,3)).nmode==nin(1,1) then
2000 bllst(connectmat(jj,3)).nmode=ninnout(1,1)
2002 if bllst(connectmat(jj,3)).nmode==nin(1,2) then
2003 bllst(connectmat(jj,3)).nmode=ninnout(1,2)
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)
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)
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(:))
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(:))
2027 //if failed then display message
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");
2037 function id = getBlockIds(path)
2038 // Return a block id path from a block index path
2040 // path: the path in the index form
2041 // id: th path in the uid form
2043 scs_m; // check scs_m access
2049 if typeof(b) == "Block" & length(scs_m.objs(i).model.uid) >= 1 then
2050 id($ + 1) = scs_m.objs(i).model.uid;
2052 if typeof(b.model.rpar) == "diagram" then
2053 scs_m = b.model.rpar;
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
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);
2075 msg = "<html><body>";
2076 msg = msg + gettext("Block input has negative size:");
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);
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");
2093 msg = "<html><body>";
2095 msg = msg + gettext("<em>Please update the diagram to avoid this warning.</em><br />Block output port has a non-determined size:");
2097 msg = msg + "<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>";
2100 msg = msg + gettext("<em>Please update the diagram to avoid this warning.</em><br />Block output port has a non-determined type:");
2102 msg = msg + "<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>";
2105 msg = msg + "</ul>";
2106 msg = msg + "</body></html>";
2107 hilite_path(path_out, msg)
2109 if or(path_in<>path_out) then
2110 msg = "<html><body>";
2112 msg = msg + gettext("<em>Please update the diagram to avoid this warning.</em><br />Block input port has a non-determined size:");
2114 msg = msg + "<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>";
2117 msg = msg + gettext("<em>Please update the diagram to avoid this warning.</em><br />Block input port has a non-determined type:");
2119 msg = msg + "<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>";
2122 msg = msg + "</ul>";
2123 msg = msg + "</body></html>";
2124 hilite_path(path_in, msg)
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?"))
2130 ninnout=evstr(dialog(mess,"[1,1]"))
2132 ninnout=evstr(dialog(mess,"1"))
2136 function [clkconnect,exe_cons]=pak_ersi(connectmat,clkconnect,..
2137 typ_r,typ_l,outoin,outoinptr,tblock,typ_cons,clkptr)
2139 //add every event to the time event because time includes all events
2141 for k=1:size(clkptr,1)-1
2143 kk=[1:(clkptr(k+1)-clkptr(k))]'
2144 all_out=[all_out;[k*ones(kk),kk]]
2147 all_out=[all_out;[0,0]]
2149 //add time event if needed
2153 clkconnect=[clkconnect;[all_out,ones(all_out)*[k,0;0,0]]]
2155 if show_trace then mprintf("c_pass4444:\t%f\n", timer()),end
2158 exe_cons=[ind,zeros(ind)]
2162 [r,ok]=newc_tree4(vec,outoin,outoinptr,typ_r)
2164 exe_cons=[exe_cons;r]
2166 if show_trace then mprintf("c_pass4445:\t%f\n", timer()),end
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
2175 ii=find(cll(2:$)-cll(1:$-1)<>0)
2177 for k=1:size(ii,"*")-1
2178 oo=[ii(k):ii(k+1)-1]
2180 vec(clkconnect(oo,3))=0
2181 [r,ok]=newc_tree4(vec,outoin,outoinptr,typ_r)
2183 r=[clkconnect(ii(k),1)*ones(m,1),clkconnect(ii(k),2)*ones(m,1),r]
2184 clkconnect=[clkconnect;r]
2186 // temporary fix to take care of conditional blocks inherting from
2187 // constants: make these blocks always active
2189 ind=setdiff(find(typ_l),clkconnect(:,3))
2192 clkconnect=[clkconnect;[all_out,ones(all_out)*[k,0;0,0]]]
2194 // end of temoprary fix
2195 if show_trace then mprintf("c_pass4446:\t%f\n", timer()),end
2198 function [r,ok]=tree4(vec,outoin,outoinptr,typ_r)
2199 //compute blocks which inherit
2207 for k=outoinptr(i):outoinptr(i+1)-1
2209 if (vec(ii)>-1)|typ_r(ii) then
2219 if fini then break;end
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)
2228 clkptr=zeros(nbl+1,1);clkptr(1)=1
2229 cliptr=clkptr;inpptr=cliptr;outptr=inpptr;
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;
2235 //tblock=[] // specifies blocks that must be connected to time event.
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;
2243 // this block inherits
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
2256 tblock(i)=ll.dep_ut($);typ_r(i)=%f
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,"*")
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");
2277 dep_t(i)=ll.dep_ut($);
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;
2287 dep_uptr($+1)=dep_uptr($);
2290 dep_u_i=ll.dep_ut(1:$-1);
2291 dep_u=[dep_u;dep_u_i(:)];
2292 dep_uptr($+1)=dep_uptr($)+sizenin;
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;
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
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
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
2319 inplnk(inpptr(connectmat(jj,3))+connectmat(jj,4)-1)=ko
2321 outlnk(outptr(connectmat(jj,1))+connectmat(jj,2)-1)=ki
2323 outlnk(outptr(connectmat(jj,1))+connectmat(jj,2)-1)=ptlnk
2324 inplnk(inpptr(connectmat(jj,3))+connectmat(jj,4)-1)=ptlnk
2329 //store unconnected outputs, if any, at the end of outtb
2330 for unco=find(outlnk==0);
2331 outlnk(unco)=max(outlnk)+1
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
2340 function [evoutoin,evoutoinptr]=synch_clkconnect(typ_l,clkconnect)
2341 nblk=size(typ_l,"*")
2342 evoutoin=[];evoutoinptr=1
2345 dd=clkconnect(clkconnect(:,1)==i,3)
2349 evoutoin=[evoutoin;dd]
2350 evoutoinptr=[evoutoinptr;evoutoinptr($)+size(dd,"*")]
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,:)=[]
2364 //function mat=cleanup1(mat)
2367 // [cc1,ind]=gsort(-cc)
2369 // ind=find(cc1(2:$)-cc1(1:$-1)==0)
2373 function vec=intersection(vec1,vec2)
2375 for i=1:size(vec1,1)
2376 if find(vec1(i)==vec2)~=[] then
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)
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)
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)
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)
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)
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)
2424 function [critev]=critical_events(connectmat,clkconnect,dep_t,typ_r,..
2425 typ_l,typ_zx,outoin,outoinptr,clkptr)
2433 [clkr,clkc]=size(clkconnect);
2434 mm=max(clkconnect)+1;
2436 cll=clkconnect(:,1)*mm+clkconnect(:,2);
2437 [cll,ind]=gsort(-cll);
2438 clkconnect=clkconnect(ind,:);
2440 ii=find(cll(2:$)-cll(1:$-1)<>0)
2442 for k=1:size(ii,"*")-1
2443 oo=[ii(k):ii(k+1)-1]
2445 vec(clkconnect(oo,3))=0
2446 [r,ok]=newc_tree4(vec,outoin,outoinptr,typ_r)
2449 r=[clkconnect(ii(k),1)*ones(m,1),clkconnect(ii(k),2)*ones(m,1),r]
2450 clkconnect=[clkconnect;r]
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)),:)=[];
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
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
2482 function [ok,bllst]=adjust_typ(bllst,connectmat)
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);
2488 if size(bllst(i).out,"*")<>size(bllst(i).outtyp,"*") then
2489 bllst(i).outtyp=bllst(i).outtyp(1)*ones(bllst(i).out);
2492 nlnk=size(connectmat,1)
2493 for hhjj=1:length(bllst)+1
2494 for hh=1:length(bllst)+1
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))
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
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;
2517 bad_connection(corinv(connectmat(jj,1)),connectmat(jj,2),..
2519 corinv(connectmat(jj,3)),connectmat(jj,4),..
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
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
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
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
2559 //fourth (& last) case : type of both source
2560 // and target port are negatives
2562 ok=%f //set flag ok to false
2565 if ok then return, end //if ok is set true then exit adjust_typ
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");
2572 nouttyp=bllst(connectmat(jj,1)).outtyp(connectmat(jj,2))
2573 nintyp=bllst(connectmat(jj,3)).intyp(connectmat(jj,4))
2575 //loop on the two dimensions of source/target port
2576 //only case : target and source ports are both
2578 if nouttyp<=0 & nintyp<=0 then
2581 inouttyp=under_connection(corinv(connectmat(jj,1)),connectmat(jj,2),nouttyp,..
2582 corinv(connectmat(jj,3)),connectmat(jj,4),nintyp,2)
2584 if inouttyp<1|inouttyp>8 then ok=%f;return;end
2586 ww=find(bllst(connectmat(jj,1)).outtyp==nouttyp)
2587 bllst(connectmat(jj,1)).outtyp(ww)=inouttyp
2590 ww=find(bllst(connectmat(jj,1)).intyp==nouttyp)
2591 bllst(connectmat(jj,1)).intyp(ww)=inouttyp
2593 ww=find(bllst(connectmat(jj,3)).intyp==nintyp)
2594 bllst(connectmat(jj,3)).intyp(ww)=inouttyp
2596 ww=find(bllst(connectmat(jj,3)).outtyp==nintyp)
2597 bllst(connectmat(jj,3)).outtyp(ww)=inouttyp
2602 //if failed then display message
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");