3 // Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
4 // - Alan Layec <alan.layec@inria.fr>
5 // - Ramine Nikoukhah <ramine.nikoukhah@inria.fr>
8 // - Scilab 5 update by Simone Mannori
10 // Copyright (C) DIGITEO - 2010 - Allan CORNET
11 // Copyright (C) Scilab Enterprises - 2012 - Bruno JOFRET
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation; either version 2 of the License, or
16 // (at your option) any later version.
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 // See the file ../license.txt
30 //**---------------------------------------------------------------------------------------------------------------------------------
32 //16/06/07 Author : ?, A. Layec
34 function [Code,actt,proto]=call_actuator(i)
35 nin=inpptr(i+1)-inpptr(i); //** number of input ports
36 nout=outptr(i+1)-outptr(i); //** number of output ports
44 //Code($+1)=+' args[0]=(double *)outtbptr[0]);';
46 uk = inplnk(inpptr(i));
47 nuk_1 = size(outtb(uk),1);
48 nuk_2 = size(outtb(uk),2);
49 uk_t = mat2scs_c_nb(outtb(uk));
50 //Code($+1)=' args[0]=('+mat2scs_c_ptr(outtb(uk))+' *)outtbptr['+string(uk-1)+'];';
53 //pour la fonction gui ont a : num de bloc l'adresse dans
54 //nouveau z et la taille du port
55 actt=[i uk nuk_1 nuk_2 uk_t bllst(i).ipar]
57 Code($+1)="block_"+rdnom+"["+string(i-1)+"].nevprt=nevprt;"
59 Code=["/* Call of actuator (blk nb "+string(i)+") */"
61 "nport = "+string(nbact)+";";
62 rdnom+"_actuator(&flag, &nport, &block_"+rdnom+"["+string(i-1)+"].nevprt, told, "+..
63 "("+mat2scs_c_ptr(outtb(uk))+" *)outtbptr["+string(uk-1)+"], &nrd_"+string(nuk_1)+", &nrd_"+..
64 string(nuk_2)+", &nrd_"+string(uk_t)+",bbb);"];
66 proto="void "+rdnom+"_actuator("+..
67 "int *, int *, int *, double *, void *, int *, int *,int *,int);"
68 proto=cformatline(proto,70);
71 //CallBlock : generate C calling sequence
74 //inputs : bk : bloc index
75 // pt : evt activation number
80 //16/06/07 Authors : Alan Layec
81 function txt=call_block42(bk,pt,flag)
84 if flag==2 & ((zptr(bk+1)-zptr(bk))+..
85 (ozptr(bk+1)-ozptr(bk))+..
86 (xptr(bk+1)-xptr(bk)+..
88 pt<=0) & ~(stalone & or(bk==actt(:,1))) then
89 return // block without state or continuously activated
91 if flag==0 & ((xptr(bk+1)-xptr(bk))==0) then
92 return // block without continuous state
94 if flag==9 & ((zcptr(bk+1)-zcptr(bk))==0) then
95 return // block without continuous state
97 if flag==3 & ((clkptr(bk+1)-clkptr(bk))==0) then
102 if ~(flag==3 & ((zcptr(bk+1)-zcptr(bk))<>0)) then
108 get_comment("call_blk",list(funs(bk),funtyp(bk),bk,labels(bk)));]
110 //** set nevprt and flag for called block
112 "block_"+rdnom+"["+string(bk-1)+"].nevprt = "+string(pt)+";"
113 "local_flag = "+string(flag)+";"]
115 //**see if its bidon, actuator or sensor
116 if funs(bk)=="bidon" then
119 elseif funs(bk)=="bidon2" then
122 elseif or(bk==actt(:,1)) then
123 ind=find(bk==actt(:,1))
129 "nport = "+string(ind)+";"]
131 rdnom+"_actuator(&local_flag, &nport, &block_"+rdnom+"["+string(bk-1)+"].nevprt, \"
132 get_blank(rdnom+"_actuator")+" &t, ("+mat2scs_c_ptr(outtb(uk))+" *)"+rdnom+"_block_outtbptr["+string(uk-1)+"], \"
133 get_blank(rdnom+"_actuator")+" &nrd_"+string(nuk_1)+", &nrd_"+string(nuk_2)+", &nrd_"+string(uk_t)+",bbb);"]
135 "if(local_flag < 0) return(5 - local_flag);"]
137 elseif or(bk==capt(:,1)) then
138 ind=find(bk==capt(:,1))
144 "nport = "+string(ind)+";"]
146 rdnom+"_sensor(&local_flag, &nport, &block_"+rdnom+"["+string(bk-1)+"].nevprt, \"
147 get_blank(rdnom+"_sensor")+" &t, ("+mat2scs_c_ptr(outtb(yk))+" *)"+rdnom+"_block_outtbptr["+string(yk-1)+"], \"
148 get_blank(rdnom+"_sensor")+" &nrd_"+string(nyk_1)+", &nrd_"+string(nyk_2)+", &nrd_"+string(yk_t)+",aaa);"]
150 "if(local_flag < 0) return(5 - local_flag);"]
155 nx=xptr(bk+1)-xptr(bk);
156 nz=zptr(bk+1)-zptr(bk);
157 nrpar=rpptr(bk+1)-rpptr(bk);
158 nipar=ipptr(bk+1)-ipptr(bk);
159 nin=inpptr(bk+1)-inpptr(bk); //* number of input ports */
160 nout=outptr(bk+1)-outptr(bk); //* number of output ports */
163 //l'adresse du pointeur de ipar
164 if nipar<>0 then ipar=ipptr(bk), else ipar=1;end
165 //l'adresse du pointeur de rpar
166 if nrpar<>0 then rpar=rpptr(bk), else rpar=1; end
167 //l'adresse du pointeur de z attention -1 pas sur
168 if nz<>0 then z=zptr(bk)-1, else z=0;end
169 //l'adresse du pointeur de x
170 if nx<>0 then x=xptr(bk)-1, else x=0;end
174 if ftyp>2000 then ftyp=ftyp-2000,end
175 if ftyp>1000 then ftyp=ftyp-1000,end
177 //** check function type
178 if ftyp < 0 then //** ifthenelse eselect blocks
182 if (ftyp<>0 & ftyp<>1 & ftyp<>2 & ftyp<>3 & ftyp<>4) then
183 disp("types other than 0,1,2,3 or 4 are not supported.")
192 //**** input/output addresses definition ****//
195 uk=inplnk(inpptr(bk)-1+k);
196 nuk=size(outtb(uk),"*");
198 "rdouttb["+string(k-1)+"]=(double *)"+rdnom+"_block_outtbptr["+string(uk-1)+"];"]
201 "args[0]=&(rdouttb[0]);"]
206 "args[0]=(double *)"+rdnom+"_block_outtbptr[0];"]
208 uk=inplnk(inpptr(bk));
209 nuk=size(outtb(uk),"*");
211 "args[0]=(double *)"+rdnom+"_block_outtbptr["+string(uk-1)+"];"]
216 yk=outlnk(outptr(bk)-1+k);
217 nyk=size(outtb(yk),"*");
219 "rdouttb["+string(k+nin-1)+"]=(double *)"+rdnom+"_block_outtbptr["+string(yk-1)+"];"];
222 "args[1]=&(rdouttb["+string(nin)+"]);"];
227 "args[1]=(double *)"+rdnom+"_block_outtbptr[0];"];
229 yk=outlnk(outptr(bk));
230 nyk=size(outtb(yk),"*"),;
232 "args[1]=(double *)"+rdnom+"_block_outtbptr["+string(yk-1)+"];"];
234 //*******************************************//
236 //*********** call seq definition ***********//
237 txtc=["(&local_flag,&block_"+rdnom+"["+string(bk-1)+"].nevprt,&t,block_"+rdnom+"["+string(bk-1)+"].xd, \";
238 "block_"+rdnom+"["+string(bk-1)+"].x,&block_"+rdnom+"["+string(bk-1)+"].nx, \";
239 "block_"+rdnom+"["+string(bk-1)+"].z,&block_"+rdnom+"["+string(bk-1)+"].nz,block_"+rdnom+"["+string(bk-1)+"].evout, \";
240 "&block_"+rdnom+"["+string(bk-1)+"].nevout,block_"+rdnom+"["+string(bk-1)+"].rpar,&block_"+rdnom+"["+string(bk-1)+"].nrpar, \";
241 "block_"+rdnom+"["+string(bk-1)+"].ipar,&block_"+rdnom+"["+string(bk-1)+"].nipar, \";
242 "(double *)args[0],&nrd_"+string(nuk)+",(double *)args[1],&nrd_"+string(nyk)+");"];
243 if (funtyp(bk)>2000 & funtyp(bk)<3000)
244 blank = get_blank(funs(bk)+"( ");
245 txtc(1) = funs(bk)+txtc(1);
246 elseif (funtyp(bk)<2000)
247 txtc(1) = "C2F("+funs(bk)+")"+txtc(1);
248 blank = get_blank("C2F("+funs(bk)+") ");
250 txtc(2:$) = blank + txtc(2:$);
252 //*******************************************//
257 //*********** call seq definition ***********//
258 txtc=["(&local_flag,&block_"+rdnom+"["+string(bk-1)+"].nevprt,&t,block_"+rdnom+"["+string(bk-1)+"].xd, \";
259 "block_"+rdnom+"["+string(bk-1)+"].x,&block_"+rdnom+"["+string(bk-1)+"].nx, \";
260 "block_"+rdnom+"["+string(bk-1)+"].z,&block_"+rdnom+"["+string(bk-1)+"].nz,block_"+rdnom+"["+string(bk-1)+"].evout, \";
261 "&block_"+rdnom+"["+string(bk-1)+"].nevout,block_"+rdnom+"["+string(bk-1)+"].rpar,&block_"+rdnom+"["+string(bk-1)+"].nrpar, \";
262 "block_"+rdnom+"["+string(bk-1)+"].ipar,&block_"+rdnom+"["+string(bk-1)+"].nipar"];
263 if (funtyp(bk)>2000 & funtyp(bk)<3000)
264 blank = get_blank(funs(bk)+"( ");
265 txtc(1) = funs(bk)+txtc(1);
266 elseif (funtyp(bk)<2000)
267 txtc(1) = "C2F("+funs(bk)+")"+txtc(1);
268 blank = get_blank("C2F("+funs(bk)+") ");
270 if nin>=1 | nout>=1 then
271 txtc($)=txtc($)+", \"
275 uk=inplnk(inpptr(bk)-1+k);
276 nuk=size(outtb(uk),"*");
277 txtc($)=txtc($)+"(double *)"+rdnom+"_block_outtbptr["+string(uk-1)+"],&nrd_"+string(nuk)+",";
279 txtc($)=part(txtc($),1:length(txtc($))-1); //remove last ,
283 txtc($)=txtc($)+", \"
287 yk=outlnk(outptr(bk)-1+k);
288 nyk=size(outtb(yk),"*");
289 txtc($)=txtc($)+"(double *)"+rdnom+"_block_outtbptr["+string(yk-1)+"],&nrd_"+string(nyk)+",";
291 txtc($)=part(txtc($),1:length(txtc($))-1); //remove last ,
296 txtc($)=txtc($)+", \"
298 "block_"+rdnom+"["+string(bk-1)+"].g,&block_"+rdnom+"["+string(bk-1)+"].ng);"]
300 txtc($)=txtc($)+");";
303 txtc(2:$) = blank + txtc(2:$);
305 //*******************************************//
310 //*********** call seq definition ***********//
311 txtc=[funs(bk)+"(&local_flag,&block_"+rdnom+"["+string(bk-1)+"].nevprt,&t,block_"+rdnom+"["+string(bk-1)+"].xd, \";
312 "block_"+rdnom+"["+string(bk-1)+"].x,&block_"+rdnom+"["+string(bk-1)+"].nx, \";
313 "block_"+rdnom+"["+string(bk-1)+"].z,&block_"+rdnom+"["+string(bk-1)+"].nz,block_"+rdnom+"["+string(bk-1)+"].evout, \";
314 "&block_"+rdnom+"["+string(bk-1)+"].nevout,block_"+rdnom+"["+string(bk-1)+"].rpar,&block_"+rdnom+"["+string(bk-1)+"].nrpar, \";
315 "block_"+rdnom+"["+string(bk-1)+"].ipar,&block_"+rdnom+"["+string(bk-1)+"].nipar, \";
316 "(double **)block_"+rdnom+"["+string(bk-1)+"].inptr,block_"+rdnom+"["+string(bk-1)+"].insz,&block_"+rdnom+"["+string(bk-1)+"].nin, \";
317 "(double **)block_"+rdnom+"["+string(bk-1)+"].outptr,block_"+rdnom+"["+string(bk-1)+"].outsz, &block_"+rdnom+"["+string(bk-1)+"].nout"];
319 txtc($)=txtc($)+");";
321 txtc($)=txtc($)+", \";
323 "block_"+rdnom+"["+string(bk-1)+"].g,&block_"+rdnom+"["+string(bk-1)+"].ng);"]
325 blank = get_blank(funs(bk)+"( ");
326 txtc(2:$) = blank + txtc(2:$);
328 //*******************************************//
333 funs(bk)+"(&block_"+rdnom+"["+string(bk-1)+"],local_flag);"];
337 txt =[txt;"if(local_flag < 0) return(5 - local_flag);"]
341 //CallBlock : generate C calling sequence
344 //inputs : bk : bloc index
346 // flag :block_'+rdnom+'[
350 //16/06/07 Authors : R.Nikoukhah, A.Layec
351 function txt=call_block4(bk)
352 // if flag==2 & ((zptr(bk+1)-zptr(bk))+(ozptr(bk+1)-ozptr(bk))==0 | pt<=0) then
353 // return // block without discrete state or continuously activated
354 // // If work allocated in the absence of z and oz, it does not work
358 nx=xptr(bk+1)-xptr(bk);
359 nz=zptr(bk+1)-zptr(bk);
360 nrpar=rpptr(bk+1)-rpptr(bk);
361 nipar=ipptr(bk+1)-ipptr(bk);
362 nin=inpptr(bk+1)-inpptr(bk); //* number of input ports */
363 nout=outptr(bk+1)-outptr(bk); //* number of output ports */
366 //l'adresse du pointeur de ipar
367 if nipar<>0 then ipar=ipptr(bk), else ipar=1;end
368 //l'adresse du pointeur de rpar
369 if nrpar<>0 then rpar=rpptr(bk), else rpar=1; end
370 //l'adresse du pointeur de z attention -1 pas sur
371 if nz<>0 then z=zptr(bk)-1, else z=0;end
372 //l'adresse du pointeur de x
373 if nx<>0 then x=xptr(bk)-1, else x=0;end
377 if ftyp>2000 then ftyp=ftyp-2000,end
378 if ftyp>1000 then ftyp=ftyp-1000,end
380 //** check function type
381 if ftyp < 0 then //** ifthenelse eselect blocks
385 if (ftyp<>0 & ftyp<>1 & ftyp<>2 & ftyp<>3 & ftyp<>4) then
386 disp("types other than 0,1,2,3 or 4 are not supported.")
393 txt=[get_comment("call_blk",list(funs(bk),funtyp(bk),bk,labels(bk)));]
395 //** write nevprt activation
398 "block_"+rdnom+"["+string(bk-1)+"].nevprt=nevprt;"]
403 //**** input/output addresses definition ****//
406 uk=inplnk(inpptr(bk)-1+k);
407 nuk=size(outtb(uk),"*");
408 TYPE=mat2scs_c_ptr(outtb(uk));//scilab index start from 1
410 "rdouttb["+string(k-1)+"]=("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(uk-1)+"];"]
413 "args[0]=&(rdouttb[0]);"]
418 "args[0]=(SCSREAL_COP *)"+rdnom+"_block_outtbptr[0];"]
420 uk=inplnk(inpptr(bk));
421 nuk=size(outtb(uk),"*");
422 TYPE=mat2scs_c_ptr(outtb(uk));//scilab index start from 1
424 "args[0]=("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(uk-1)+"];"]
429 yk=outlnk(outptr(bk)-1+k);
430 nyk=size(outtb(yk),"*");
431 TYPE=mat2scs_c_ptr(outtb(yk));//scilab index start from 1
433 "rdouttb["+string(k+nin-1)+"]=("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(yk-1)+"];"];
436 "args[1]=&(rdouttb["+string(nin)+"]);"];
441 "args[1]=(SCSREAL_COP *)"+rdnom+"_block_outtbptr[0];"];
443 yk=outlnk(outptr(bk));
444 nyk=size(outtb(yk),"*"),;
445 TYPE=mat2scs_c_ptr(outtb(yk));//scilab index start from 1
447 "args[1]=("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(yk-1)+"];"];
449 //*******************************************//
451 //*********** call seq definition ***********//
452 txtc=["(&flag,&block_"+rdnom+"["+string(bk-1)+"].nevprt,told,block_"+rdnom+"["+string(bk-1)+"].xd, \";
453 "block_"+rdnom+"["+string(bk-1)+"].x,&block_"+rdnom+"["+string(bk-1)+"].nx, \";
454 "block_"+rdnom+"["+string(bk-1)+"].z,&block_"+rdnom+"["+string(bk-1)+"].nz,block_"+rdnom+"["+string(bk-1)+"].evout, \";
455 "&block_"+rdnom+"["+string(bk-1)+"].nevout,block_"+rdnom+"["+string(bk-1)+"].rpar,&block_"+rdnom+"["+string(bk-1)+"].nrpar, \";
456 "block_"+rdnom+"["+string(bk-1)+"].ipar,&block_"+rdnom+"["+string(bk-1)+"].nipar, \";
457 "(double *)args[0],&nrd_"+string(nuk)+",(double *)args[1],&nrd_"+string(nyk)+");"];
458 if (funtyp(bk)>2000 & funtyp(bk)<3000)
459 blank = get_blank(funs(bk)+"( ");
460 txtc(1) = funs(bk)+txtc(1);
461 elseif (funtyp(bk)<2000)
462 txtc(1) = "C2F("+funs(bk)+")"+txtc(1);
463 blank = get_blank("C2F("+funs(bk)+") ");
465 txtc(2:$) = blank + txtc(2:$);
467 //*******************************************//
472 //**** input/output addresses definition ****//
475 // uk=inplnk(inpptr(i)-1+k);
476 // nuk=size(outtb(uk),'*');
481 // yk=outlnk(outptr(i)-1+k);
482 // nyk=size(outtb(yk),'*');
485 //*******************************************//
487 //*********** call seq definition ***********//
488 txtc=["(&flag,&block_"+rdnom+"["+string(bk-1)+"].nevprt,told,block_"+rdnom+"["+string(bk-1)+"].xd, \";
489 "block_"+rdnom+"["+string(bk-1)+"].x,&block_"+rdnom+"["+string(bk-1)+"].nx, \";
490 "block_"+rdnom+"["+string(bk-1)+"].z,&block_"+rdnom+"["+string(bk-1)+"].nz,block_"+rdnom+"["+string(bk-1)+"].evout, \";
491 "&block_"+rdnom+"["+string(bk-1)+"].nevout,block_"+rdnom+"["+string(bk-1)+"].rpar,&block_"+rdnom+"["+string(bk-1)+"].nrpar, \";
492 "block_"+rdnom+"["+string(bk-1)+"].ipar,&block_"+rdnom+"["+string(bk-1)+"].nipar"];
493 if (funtyp(bk)>2000 & funtyp(bk)<3000)
494 blank = get_blank(funs(bk)+"( ");
495 txtc(1) = funs(bk)+txtc(1);
496 elseif (funtyp(bk)<2000)
497 txtc(1) = "C2F("+funs(bk)+")"+txtc(1);
498 blank = get_blank("C2F("+funs(bk)+") ");
500 if nin>=1 | nout>=1 then
501 txtc($)=txtc($)+", \"
505 uk=inplnk(inpptr(bk)-1+k);
506 nuk=size(outtb(uk),"*");
507 txtc($)=txtc($)+"(SCSREAL_COP *)"+rdnom+"_block_outtbptr["+string(uk-1)+"],&nrd_"+string(nuk)+",";
509 txtc($)=part(txtc($),1:length(txtc($))-1); //remove last ,
513 txtc($)=txtc($)+", \"
517 yk=outlnk(outptr(bk)-1+k);
518 nyk=size(outtb(yk),"*");
519 txtc($)=txtc($)+"(SCSREAL_COP *)"+rdnom+"_block_outtbptr["+string(yk-1)+"],&nrd_"+string(nyk)+",";
521 txtc($)=part(txtc($),1:length(txtc($))-1); //remove last ,
526 txtc($)=txtc($)+", \"
527 txtc=[txtc;"w,&nrd_0);"];
529 txtc($)=txtc($)+");";
532 txtc(2:$) = blank + txtc(2:$);
534 //*******************************************//
539 //*********** call seq definition ***********//
540 txtc=[funs(bk)+"(&flag,&block_"+rdnom+"["+string(bk-1)+"].nevprt,told,block_"+rdnom+"["+string(bk-1)+"].xd, \";
541 "block_"+rdnom+"["+string(bk-1)+"].x,&block_"+rdnom+"["+string(bk-1)+"].nx, \";
542 "block_"+rdnom+"["+string(bk-1)+"].z,&block_"+rdnom+"["+string(bk-1)+"].nz,block_"+rdnom+"["+string(bk-1)+"].evout, \";
543 "&block_"+rdnom+"["+string(bk-1)+"].nevout,block_"+rdnom+"["+string(bk-1)+"].rpar,&block_"+rdnom+"["+string(bk-1)+"].nrpar, \";
544 "block_"+rdnom+"["+string(bk-1)+"].ipar,&block_"+rdnom+"["+string(bk-1)+"].nipar, \";
545 "(double **)block_"+rdnom+"["+string(bk-1)+"].inptr,block_"+rdnom+"["+string(bk-1)+"].insz,&block_"+rdnom+"["+string(bk-1)+"].nin, \";
546 "(double **)block_"+rdnom+"["+string(bk-1)+"].outptr,block_"+rdnom+"["+string(bk-1)+"].outsz, &block_"+rdnom+"["+string(bk-1)+"].nout"];
548 txtc($)=txtc($)+");";
550 txtc($)=txtc($)+", \";
552 "block_"+rdnom+"["+string(bk-1)+"].g,&block_"+rdnom+"["+string(bk-1)+"].ng);"]
554 blank = get_blank(funs(bk)+"( ");
555 txtc(2:$) = blank + txtc(2:$);
557 //*******************************************//
562 funs(bk)+"(&block_"+rdnom+"["+string(bk-1)+"],flag);"];
568 //16/06/07 Author : ?, A. Layec
570 function [Code,capt,proto]=call_sensor(i)
571 nin=inpptr(i+1)-inpptr(i); ///* number of input ports */
572 nout=outptr(i+1)-outptr(i); ///* number of output ports */
574 //declaration des I/O des blocs de type 1
581 //Code($+1)=+' args[1]=(double *)(outtbptr[0]);';
583 yk = outlnk(outptr(i));
584 nyk_1 = size(outtb(yk),1);
585 nyk_2 = size(outtb(yk),2);
586 yk_t = mat2scs_c_nb(outtb(yk));
587 //Code($+1)=' args[1]=('+mat2scs_c_ptr(outtb(yk))+' *)(outtbptr['+string(yk-1)+']);';
591 capt=[i yk nyk_1 nyk_2 yk_t bllst(i).ipar]
592 Code($+1)="block_"+rdnom+"["+string(i-1)+"].nevprt=nevprt;"
593 Code=["/* Call of sensor (blk nb "+string(i)+") */"
595 "nport = "+string(nbcap)+";";
596 rdnom+"_sensor(&flag, &nport, &block_"+rdnom+"["+string(i-1)+"].nevprt, "+..
597 "told, ("+mat2scs_c_ptr(outtb(yk))+" *)(outtbptr["+string(yk-1)+"]), &nrd_"+string(nyk_1)+..
598 ", &nrd_"+string(nyk_2)+", &nrd_"+string(yk_t)+",aaa);"];
599 proto="void "+rdnom+"_sensor("+..
600 "int *, int *, int *, double *,void *, int *, int *,int *, int);"
601 proto=cformatline(proto,70);
604 //Generates Code for dynamically linked Fortran and C Blocks
605 function [CCode,FCode]=gen_blocks()
609 kdyn=find(funtyp>1000) //dynamically linked blocs
610 //100X : Fortran blocks
613 if (size(kdyn,"*") >1)
615 //get the block data structure in the initial scs_m structure
616 if size(corinv(kdyn(1)),"*")==1 then
617 O=scs_m.objs(corinv(kdyn(1)));
620 for l=corinv(kdyn(1))(1:$-1)
626 path($+1)=corinv(kdyn(1))($);
629 if funtyp(kdyn(1))>2000 then
631 CCode=[CCode;O.graphics.exprs(2)]
633 FCode=[FCode;O.graphics.exprs(2)]
636 for i=2:size(kdyn,"*")
637 //get the block data structure in the initial scs_m structure
638 if size(corinv(kdyn(i)),"*")==1 then
639 O=scs_m.objs(corinv(kdyn(i)));
642 for l=corinv(kdyn(i))(1:$-1)
648 path($+1)=corinv(kdyn(i))($);
651 if (find(kfuns==funs(kdyn(i))) == [])
652 kfuns=[kfuns;funs(kdyn(i))];
653 if funtyp(kdyn(i))>2000 then
655 CCode=[CCode;O.graphics.exprs(2)]
657 FCode=[FCode;O.graphics.exprs(2)]
661 elseif (size(kdyn,"*")==1)
662 //get the block data structure in the initial scs_m structure
663 if size(corinv(kdyn),"*")==1 then
664 O=scs_m.objs(corinv(kdyn));
667 for l=corinv(kdyn)(1:$-1)
673 path($+1)=corinv(kdyn)($);
676 if funtyp(kdyn)>2000 then
678 CCode=[CCode;O.graphics.exprs(2)]
680 FCode=[FCode;O.graphics.exprs(2)]
684 CCode=["void no_ccode()"
691 //** Generates the C code for new block simulation
693 //12/07/07 Alan Layec
694 function ok=gen_ccode42();
696 //** Generate code for scicos block
698 ierr=execstr("make_computational42(rpat+''/''+rdnom+''.c'')","errcatch")
705 //** Generate files for dynamically linked scicos blocks
706 [CCode,FCode]=gen_blocks()
708 ierr=execstr("mputl(FCode,rpat+''/''+rdnom+''f.f'')","errcatch")
716 ierr=execstr("mputl(CCode,rpat+''/''+rdnom+''_Cblocks.c'')","errcatch")
724 //** Generate _void_io.c
726 fd = mopen(rpat+"/"+rdnom+"_void_io.c", "wt")
729 "** Copyright (c) 1989 - 2012 (INRIA)";
730 "** Copyright (c) 2011 - 2013 (Scilab Enterprises)";
732 "** Generated by Code_Generation toolbox of Xcos with "+getversion();
736 if (readGlobal <> [] | writeGlobal <> [])
738 "#include ""scicos_block4.h"""], fd);
742 "/*---------------------------------------- Actuators */"
743 "void "+rdnom+"_actuator(int *flag, int *nport, int *nevprt, double *t, void *u, int *nu1, int *nu2, int *ut, int *flag1)"
747 "/*---------------------------------------- Sensor */"
748 "void "+rdnom+"_sensor(int *flag, int *nport, int *nevprt, double *t, void *y, int *ny1, int *ny2, int *yt, int *flag1)"
753 // Generate global data read <=> FromWorkspace.
756 mputl(["/*---------------------------------------- Read variable "+i+" */"
757 "void readGlobal_"+i+"(scicos_block *block, int flag)"
759 " // Read a scilab Environment variable "+i
760 " fromws_c(block, flag);"
766 // Generate global data write <=> ToWorkspace.
769 mputl(["/*---------------------------------------- Write variable "+i+" */"
770 "void writeGlobal_"+i+"(scicos_block *block, int flag)"
772 " // Write a scilab environment variable "+i
773 " // to a C one : global"+i
774 " tows_c(block, flag);"
787 //** Generate _standalone.c
789 ierr=execstr("make_standalone42(rpat+''/''+rdnom+''_standalone.c'')","errcatch")
796 //** This copy is indispensable only for stand alone code generation
797 //** not supported in Scilab 5
799 //** copy source code of machine.h/scicos_block4.h
801 // txt=mgetl(SCI+'/modules/core/includes/machine.h');
802 // ierr=execstr('mputl(txt,rpat+''/machine.h'')','errcatch')
804 // message(lasterror())
809 // txt=mgetl(SCI+'/modules/scicos_blocks/includes/scicos_block4.h');
810 // ierr=execstr('mputl(txt,rpat+''/scicos_block4.h'')','errcatch')
812 // message(lasterror())
817 //** Generate _act_sens_events.c
819 "** Copyright (c) 1989 - 2012 (INRIA)"
820 "** Copyright (c) 2011 - 2012 (Scilab Enterprises)"
822 "** Generated by Code_Generation toolbox of Xcos with "+getversion()
827 "#include <stdlib.h>"
829 "#include <string.h>"
830 "extern void **"+rdnom+"_block_outtbptr;"
831 "extern char input[50],output[50];"
832 "FILE *fprr = NULL, *fprw = NULL;"
840 created=fileinfo(rpat+"/"+rdnom+"_act_sens_events.c")
842 reponse=messagebox(["File: """+rdnom+"_act_sens_events.c"" already exists,";
843 "do you want to replace it ?"],"modal","question",["Yes","No"]);
846 if reponse==1 | reponse==[] then
847 ierr=execstr("mputl(Code,rpat+''/''+rdnom+''_act_sens_events.c'')", ...
857 if (readGlobal <> [] | writeGlobal <> [])
858 fd = mopen(rpat+"/"+rdnom+"_read_write_global.c", "wt")
860 "** Copyright (c) 2012 - 2013 (Scilab Enterprises)"
862 "** Generated by Code_Generation toolbox of Xcos with "+getversion()
866 "// Uncomment this to log each To Workspace variable update"
872 "#include <stdlib.h>"
873 "#include <string.h>"
874 "#include ""scicos_block4.h"""
876 "double get_scicos_time();"
880 // Generate global C variables
881 // #define global_XXX_Size
882 // static double **global_XXX
883 // static int global_XXX_Index (to manage buffer like variables)
884 for i = 1:size(writeGlobal, "*")
885 mputl(["/*---------------------------------------- Global Variable "+writeGlobal(i)+" */"
886 "#define global_"+writeGlobal(i)+"_Size "+string(writeGlobalSize(i))
887 "static double **global_"+writeGlobal(i)+";"
888 "static int global_"+writeGlobal(i)+"_Index = 0;"
894 // Generate global C variables
895 // #define global_XXX_Size
896 // #define global_XXX_Time_Size
897 // static double **global_XXX
898 // static double global_XXX_Time (manage time <-> values association)
899 for i = 1:size(readGlobal, "*")
900 mputl(["/*---------------------------------------- Global Variable "+readGlobal(i)+" */"
901 "#define global_"+readGlobal(i)+"_Size "+string(readGlobalSize(i))
902 "#define global_"+readGlobal(i)+"_Time_Size "+string(readGlobalTimeSize(i))
903 "static double **global_"+readGlobal(i)+";"
904 "static double *global_"+readGlobal(i)+"_Time = 0;"
911 if (readGlobal <> [])
912 for i = 1:size(readGlobal, "*")
913 mputl(["/*---------------------------------------- Read variable "+readGlobal(i)+" */"
914 "void readGlobal_"+readGlobal(i)+"(scicos_block *block, int flag)"
918 " double *y_d = NULL;"
920 " /* Read a scilab Environment variable "+readGlobal(i)+" */"
925 // Init case : allocate data / timetable
926 // Feal data/timetable with values from Scilab.
928 mputl([" case 4 : /* init */"
930 " // Allocate time table"
931 " global_"+readGlobal(i)+"_Time = (double *) malloc(global_"+readGlobal(i)+"_Time_Size * sizeof(double));"
932 " memset(global_"+readGlobal(i)+"_Time, 0x0, global_"+readGlobal(i)+"_Time_Size * sizeof(double));"
935 " global_"+readGlobal(i)+" = (double **) malloc(global_"+readGlobal(i)+"_Time_Size * sizeof(double *));"
936 " for (i = 0 ; i < global_"+readGlobal(i)+"_Time_Size ; ++i)"
938 " global_"+readGlobal(i)+"[i] = (double *) malloc( global_"+readGlobal(i)+"_Size * sizeof(double));"
939 " memset(global_"+readGlobal(i)+"[i], 0x0, global_"+readGlobal(i)+"_Size * sizeof(double));"
944 oldFormat = format();
948 for (j = 1:readGlobalTimeSize(i))
949 mputl([" global_"+readGlobal(i)+"_Time["+string(j-1)+"] = " + ...
950 strsubst(string(evstr(readGlobal(i)+".time("+string(j)+")")), "D", "E")+";"
954 for (j = 1:readGlobalTimeSize(i))
955 for (k = 1:readGlobalSize(i))
956 mputl([" global_"+readGlobal(i)+"["+string(j-1)+"]["+string(k-1)+"] = " + ...
957 strsubst(string(evstr(readGlobal(i)+".values("+string(j)+", "+string(k)+")")), "D", "E")+";"
964 format(oldFormat(2), oldFormat(1));
971 // Output update case :
973 mputl([" case 1 : /* output update */"
976 " while(get_scicos_time() > global_"+readGlobal(i)+"_Time[j] && j < global_"+readGlobal(i)+"_Time_Size)"
980 " y_d = GetRealOutPortPtrs(block,1);"
981 " if (j == global_"+readGlobal(i)+"_Time_Size)"
983 " memset(y_d, 0x0, global_"+readGlobal(i)+"_Size * sizeof(double));"
987 " memcpy(y_d, global_"+readGlobal(i)+"[j], global_"+readGlobal(i)+"_Size * sizeof(double));"
995 mputl([" case 5 : /* END */"
997 " for (i = 0 ; i < global_"+readGlobal(i)+"_Time_Size ; ++i)"
999 " free(global_"+readGlobal(i)+"[i]);"
1001 " free(global_"+readGlobal(i)+");"
1005 // Close switch + function declaration
1012 if writeGlobal <> []
1013 for i = 1:size(writeGlobal, "*")
1014 mputl(["/*---------------------------------------- Write variable "+writeGlobal(i)+" */"
1015 "void writeGlobal_"+writeGlobal(i)+"(scicos_block *block, int flag)"
1019 " int nu = block->insz[0];"
1020 " int nu2 = block->insz[1];"
1022 " /* Write a scilab environment variable "+writeGlobal(i)+" */"
1023 " /* to a C one : global_"+writeGlobal(i)+" */"
1026 " case 4 : /* init */"
1028 " global_"+writeGlobal(i)+" = (double **) malloc(global_"+writeGlobal(i)+"_Size * sizeof(double *));"
1029 " for (i = 0 ; i < global_"+writeGlobal(i)+"_Size ; ++i)"
1031 " global_"+writeGlobal(i)+"[i] = (double *) malloc(nu * nu2 * sizeof(double));"
1032 " memset(global_"+writeGlobal(i)+"[i], 0x0, nu * nu2 * sizeof(double));"
1034 " global_"+writeGlobal(i)+"_Index = -1;"
1036 " printf(""C_"+writeGlobal(i)+".values = zeros(%d, %d, %d)\n"", global_"+writeGlobal(i)+"_Size, nu, nu2);"
1037 " printf(""C_"+writeGlobal(i)+".time = -%%inf * ones(%d, 1)\n"", global_"+writeGlobal(i)+"_Size);"
1041 " case 2 : /* state update */"
1043 " global_"+writeGlobal(i)+"_Index = (global_"+writeGlobal(i)+"_Index + 1) % global_"+writeGlobal(i)+"_Size;"
1044 " memcpy(global_"+writeGlobal(i)+"[global_"+writeGlobal(i)+"_Index], block->inptr[i], nu * nu2 * sizeof(double));"
1046 " printf(""C_"+writeGlobal(i)+".time(%d) = %.25E;\n"", global_"+writeGlobal(i)+"_Index + 1, get_scicos_time());"
1047 " for (j = 0 ; j < nu * nu2 ; ++j)"
1049 " printf(""C_"+writeGlobal(i)+".values(%d, %d) = %.25E;\n"", global_"+writeGlobal(i)+"_Index + 1, j + 1, global_"+writeGlobal(i)+"[global_"+writeGlobal(i)+"_Index][j]);"
1051 "#endif /* !VERBOSE */"
1054 " case 5 : /* end */"
1057 " //reshape to fit scilab behaviour"
1058 " printf(""C_"+writeGlobal(i)+".time = C_"+writeGlobal(i)+".time(C_"+writeGlobal(i)+".time <> -%%inf);\n"");"
1059 " printf(""C_"+writeGlobal(i)+".values = C_"+writeGlobal(i)+".values(1:size(C_"+writeGlobal(i)+".time, ''*''), :);\n"");"
1061 " for (i = 0 ; i < global_"+writeGlobal(i)+"_Size ; ++i)"
1063 " free(global_"+writeGlobal(i)+"[i]);"
1065 " free(global_"+writeGlobal(i)+");"
1077 message(lasterror())
1084 //creates the Scicos GUI function associated with the new block
1086 //Author : Rachid Djenidi, A.Layec
1088 //20/06/07, A.Layec : update with in2,out2,intyp,outtyp
1089 //27/06/07, A.Layec : update opar,oz
1091 function ok=gen_gui42();
1092 clkinput=ones(clkIN)';
1093 clkoutput=ones(clkOUT)';
1101 for i=1:lstsize(outtb)
1102 new_oz($+1) = outtb(i)
1105 //outtb($+1) = zeros(nblk,1);
1107 "// Copyright (c) 1989 - 2012 (INRIA)";
1108 "// Copyright (c) 2011 - 2013 (Scilab Enterprises)";
1110 "// Generated by Code_Generation toolbox of Xcos with "+getversion();
1111 "// Date : "+date();
1113 "function [x,y,typ]="+rdnom+"_c(job,arg1,arg2)";
1114 " x=[];y=[];typ=[];";
1116 " case ''plot'' then";
1117 " standard_draw(arg1)";
1118 " case ''getinputs'' then";
1119 " [x,y,typ]=standard_inputs(arg1)";
1120 " case ''getoutputs'' then";
1121 " [x,y,typ]=standard_outputs(arg1)";
1122 " case ''getorigin'' then";
1123 " [x,y]=standard_origin(arg1)";
1124 " case ''set'' then";
1126 " case ''define'' then"
1127 " "+sci2exp(capt(:,3),"in",70); //input ports sizes 1
1128 " "+sci2exp(capt(:,4),"in2",70); //input ports sizes 2
1129 " "+sci2exp(scs_c_nb2scs_nb(capt(:,5)),"intyp",70); //input ports type
1130 " "+sci2exp(actt(:,3),"out",70); //output ports sizes 1
1131 " "+sci2exp(actt(:,4),"out2",70); //output ports sizes 2
1132 " "+sci2exp(scs_c_nb2scs_nb(actt(:,5)),"outtyp",70); //output ports type
1133 " "+sci2exp(x,"x",70); //initial continuous state
1134 " "+sci2exp(z,"z",70); //initial discrete state
1135 " work=zeros("+string(nblk)+",1)";
1137 " "+sci2exp(new_oz,"odstate",70);
1138 " "+sci2exp(cpr.sim.rpar,"rpar",70); //real parameters
1139 " "+sci2exp(cpr.sim.ipar,"ipar",70); //integer parameters
1140 " "+sci2exp(cpr.sim.opar,"opar",70); //object parameters
1141 " "+sci2exp(clkinput,"clkinput",70);
1142 " "+sci2exp(clkoutput,"clkoutput",70);
1143 " "+sci2exp(FIRING,"firing",70);
1144 " nzcross="+string(sum(cpr.sim.zcptr(2:$)-cpr.sim.zcptr(1:$-1)))';
1145 " nmode="+string(sum(cpr.sim.modptr(2:$)-cpr.sim.modptr(1:$-1)))';]
1147 for i=1:length(bllst)
1149 if (bllst(i).dep_ut(2) == %t) then
1154 Code($+1)=" "+sci2exp(deput,"dep_ut",70);
1156 " model=scicos_model(sim=list(''"+rdnom+"'',4),.."
1163 " evtin=clkinput,.."
1164 " evtout=clkoutput,.."
1168 " odstate=odstate,.."
1172 " blocktype=''c'',.."
1174 " nzcross=nzcross,.."
1176 " gr_i=''xstringb(orig(1),orig(2),''''"+rdnom+"'''',sz(1),.."
1177 " sz(2),''''fill'''')''";
1178 " x=standard_define([2 2],model,[],gr_i)";
1182 ierr=execstr("mputl(Code,rpat+''/''+rdnom+''_c.sci'')","errcatch")
1184 message(lasterror())
1189 function [ok,XX,alreadyran,flgcdgen,szclkINTemp,freof] = do_compile_superblock42(XX,all_scs_m,numk,alreadyran)
1190 // Transforms a given Scicos discrete and continuous SuperBlock into a C defined Block
1195 //terminate current simulation
1199 hname=scs_m.props.title(1) //superblock name
1200 //***********************************************************
1201 //Check blocks properties and adapt them if necessary
1202 //***********************************************************
1211 writeGlobalSize = [];
1213 readGlobalTimeSize = [];
1214 readGlobalSize = [];
1216 for i=1:size(scs_m.objs)
1217 if typeof(scs_m.objs(i))=="Block" then
1218 if scs_m.objs(i).gui=="CLKOUT_f" then
1220 message("Superblock should not have any activation output port.")
1222 elseif scs_m.objs(i).gui=="IN_f" then
1223 //replace input ports by sensor blocks
1225 scs_m.objs(i).gui="INPUTPORTEVTS";
1226 scs_m.objs(i).model.evtin=1
1227 scs_m.objs(i).model.sim(1)="capteur"+string(numc)
1228 IN=[IN scs_m.objs(i).model.ipar]
1229 elseif scs_m.objs(i).gui=="OUT_f" then
1230 //replace output ports by actuator blocks
1232 scs_m.objs(i).gui="OUTPUTPORTEVTS";
1233 scs_m.objs(i).model.sim(1)="actionneur"+string(numa)
1234 OUT=[OUT scs_m.objs(i).model.ipar]
1235 elseif scs_m.objs(i).gui=="CLKINV_f" then
1236 //replace event input ports by fictious block
1237 scs_m.objs(i).gui="EVTGEN_f";
1238 scs_m.objs(i).model.sim(1)="bidon"
1239 clkIN=[clkIN scs_m.objs(i).model.ipar];
1240 //elseif scs_m.objs(i).model.dep_ut(2)==%t then
1241 //check for time dependency PAS IICI
1243 //message('a block have time dependence.')
1245 elseif scs_m.objs(i).gui=="CLKOUTV_f" then
1246 scs_m.objs(i).gui="EVTOUT_f";
1247 scs_m.objs(i).model.sim(1)="bidon2"
1248 clkOUT=[clkOUT scs_m.objs(i).model.ipar];
1253 //Check if input/output ports are numered properly
1255 if or(IN<>[1:size(IN,"*")]) then
1257 message("Input ports are not numbered properly.")
1261 if or(OUT<>[1:size(OUT,"*")]) then
1263 message("Output ports are not numbered properly.")
1266 clkIN=-gsort(-clkIN);
1267 if or(clkIN<>[1:size(clkIN,"*")]) then
1269 message("Event input ports are not numbered properly.")
1272 clkOUT=-gsort(-clkOUT);
1273 if or(clkOUT<>[1:size(clkOUT,"*")]) then
1275 message("Event output ports are not numbered properly.")
1279 //Check if there is more than one clock in the diagram
1280 szclkIN=size(clkIN,2);
1285 [bllst,connectmat,clkconnect,cor,corinv,ok,scs_m,flgcdgen,freof]=c_pass1(scs_m,flgcdgen);
1287 // Check for any To Workspace/FromWorkspace block
1288 for i = 1:size(bllst)
1289 if bllst(i).sim(1) == "tows_c" then
1290 // Explore scs_m to find original block
1291 towsObjIndex = corinv(i);
1292 towsObj = scs_m.objs(towsObjIndex(1));
1293 for j = 2:size(towsObjIndex, "*")
1294 towsObj = towsObj.model.rpar.objs(towsObjIndex(j));
1296 bllst(i).sim(1) = "writeGlobal_" + ...
1297 towsObj.graphics.exprs(2);
1298 // Force state to enable case 2 call
1299 // on generated code
1300 bllst(i).dstate = 0;
1301 writeGlobal = [writeGlobal towsObj.graphics.exprs(2)];
1302 writeGlobalSize = [writeGlobalSize bllst(i).ipar(1)];
1303 elseif bllst(i).sim(1) == "fromws_c" then
1304 fromwsObjIndex = corinv(i);
1305 fromwsObj = scs_m.objs(fromwsObjIndex(1));
1306 for j = 2:size(fromwsObjIndex, "*")
1307 fromwsObj = fromwsObj.model.rpar.objs(fromwsObjIndex(j));
1309 bllst(i).sim(1) = "readGlobal_" + fromwsObj.graphics.exprs(1);
1310 readGlobal = [readGlobal fromwsObj.graphics.exprs(1)];
1311 readGlobalTimeSize = [readGlobalTimeSize evstr("size(" + fromwsObj.graphics.exprs(1) + ".time, ""*"")")]
1312 readGlobalSize = [readGlobalSize evstr("size(" + fromwsObj.graphics.exprs(1) + ".values(1, :), ""*"")")]
1316 if flgcdgen<> szclkIN
1317 clkIN=[clkIN flgcdgen]
1319 szclkINTemp=szclkIN;
1321 // [bllst,connectmat,clkconnect,cor,corinv,ok]=c_pass1(scs_m);
1323 //Test for ALWAYS_ACTIVE sblock (RN -25/06/07)
1326 if blki.dep_ut($) then
1331 if ALWAYS_ACTIVE then
1333 for Ii=1:length(bllst)
1334 if part(bllst(Ii).sim(1),1:7)=="capteur" then
1335 bllst(Ii).dep_ut($)=%t
1336 // elseif part(bllst(Ii).sim(1),1:10)=='actionneur' then
1337 // bllst(Ii).dep_ut($)=%t
1343 message("Sorry: problem in the pre-compilation step.")
1355 ///**********************************
1357 // check for a scilab function block
1358 if type(bllst(i).sim(1)) == 13 then
1361 message(_("Superblock should not contains any Scilab function block."))
1366 if (bllst(i).sim(1)=="actionneur"+string(j)) then
1371 elseif (bllst(i).sim(1)=="capteur"+string(j)) then
1376 elseif (bllst(i).sim(1)=="bidon") then
1378 allhowclk=[allhowclk;i];
1381 elseif (bllst(i).sim(1)=="bidon2") then
1383 allhowclk2=[allhowclk2;i];
1389 ///**********************************
1391 //replace the N Event inputs by a fictious block with 2^N as many event
1393 output=ones((2^szclkIN)-1,1)
1394 bllst($+1)=scicos_model(sim=list("bidon",1),evtout=output,..
1396 firing=-output',dep_ut=[%f %f])
1397 corinv(size(bllst))=size(bllst)+1;
1399 // adjust the links accordingly
1400 for i=1:(2^szclkIN)-1
1401 vec=codebinaire(i,szclkIN)
1403 if vec(j)*allhowclk(j)>=1 then
1404 for k=1:size(clkconnect,1)
1405 if clkconnect(k,1)==allhowclk(j) then
1406 clkconnect=[clkconnect;[howclk i clkconnect(k,3:4)]]
1412 elseif szclkIN==[]&~ALWAYS_ACTIVE then
1413 //superblock has no Event input, add a fictious clock
1414 output=ones((2^(size(cap,"*")))-1,1)
1415 if (output == []) then
1418 bllst($+1)=scicos_model(sim=list("bidon",1),evtout=output,..
1419 firing=-output,blocktype="d",dep_ut=[%f %f])
1420 corinv(size(bllst))=size(bllst)+1;
1422 elseif szclkIN==1 then
1426 //mise en ordre de clkconnect
1428 newclkconnect=clkconnect;
1430 for i=1:size(newclkconnect,1)-1
1431 if or(newclkconnect(i,:)<>newclkconnect(i+1,:)) then
1432 clkconnect=[clkconnect;newclkconnect(i,:)]
1435 if or(newclkconnect($-1,:)<>newclkconnect($,:)) then
1436 clkconnect=[clkconnect;newclkconnect($,:)]
1439 //suppression des blocs bidons
1440 newclkconnect=clkconnect;nkt=[];
1442 for k=1:size(newclkconnect,1)
1443 if newclkconnect(k,1)~=allhowclk(i) then
1444 nkt=[nkt;newclkconnect(k,:)];
1450 clkconnect=newclkconnect;
1453 //**************************************************
1454 // nouveau clkconnect avec liaisons sur les capteurs
1455 //**************************************************
1458 // Generate block activation fo C code
1461 if ~(szclkIN==[]) then
1462 // Activate sensors on each clock pulse.
1463 generatedConnection = [];
1466 for j=1:(2^szclkIN)-1
1467 generatedConnection = [generatedConnection ; [howclk j cap(i) 1]];
1469 elseif szclkIN==1 then
1470 generatedConnection = [generatedConnection ; [howclk 1 cap(i) 1]];
1473 clkconnect = [clkconnect ; generatedConnection]
1474 elseif ~ALWAYS_ACTIVE then
1475 // Generate all possible activations for sensors, based on number of blocks.
1476 //codage de l'activation des capteurs dans le cas de l'heritage
1477 generatedConnection = [];
1479 vec=codebinaire(i,n);
1482 generatedConnection = [generatedConnection ; [howclk i cap(j) 1]];
1486 clkconnect = [clkconnect ; generatedConnection]
1490 for i=1:size(allhowclk2,1)
1491 j = find(clkconnect(:,3)==allhowclk2(i))
1493 FIRING=[FIRING;bllst(clkconnect(j,1)).firing(clkconnect(j,2))]
1500 //** %windo=xget('window')
1502 cpr=c_pass2(bllst,connectmat,clkconnect,cor,corinv)
1504 if cpr==list() then ok=%f,return, end
1506 //** Alan's patch 5/07/07: try to solve
1507 // which blocks use work
1508 funs_save=cpr.sim.funs;
1509 funtyp_save=cpr.sim.funtyp;
1510 with_work = zeros(cpr.sim.nblk,1)
1511 for i=1:lstsize(cpr.sim.funs)
1512 if part(cpr.sim.funs(i),1:10)=="actionneur" then
1513 cpr.sim.funs(i) ="bidon"
1514 cpr.sim.funtyp(i) = 1
1515 elseif part(cpr.sim.funs(i),1:7)=="capteur" then
1516 cpr.sim.funs(i) ="bidon"
1517 cpr.sim.funtyp(i) = 1
1522 //** retrieve all open Scilab windows with winsid()
1525 BeforeCG_WinList = winsid();
1527 ierr=execstr("[state,t]=scicosim(cpr.state,0,0,cpr.sim,"+..
1528 "''start'',scs_m.props.tol)","errcatch")
1530 for i=1:cpr.sim.nblk
1531 if state.iz(i)<>0 then
1535 ierr=execstr("[state,t]=scicosim(state,0,0,cpr.sim,"+..
1536 "''finish'',scs_m.props.tol)","errcatch")
1540 //** retrieve all open Scilab windows with winsid
1541 //** and close the additional windows open since first
1544 //** This code does not cover 100% of the possible situations because the user can
1545 //** destroy one or more Scicos wins manually during this intermediate phase
1546 //** This code is 100% functional if the user does not close manually any win.
1547 //** TO BE updated in Scilab 5.0
1549 AfterCG_WinList = winsid();
1551 AfterCG_size = size(AfterCG_WinList); //** matrix
1552 AfterCG_size = AfterCG_size(2) ; //** vector length
1554 BeforeCG_size = size(BeforeCG_WinList); //** matrix
1555 BeforeCG_size = BeforeCG_size(2) ; //** vector length
1557 if (or(AfterCG_WinList<>BeforeCG_WinList)) & (AfterCG_size>BeforeCG_size) then
1558 //** means that a scope or other scicos object has created some
1561 DiffCG_Winlist = AfterCG_WinList<>BeforeCG_WinList ; //** T/F mismatch
1562 DiffCG_Index = find(DiffCG_Winlist); //** recover the mismatched indexes only
1564 for win_idx = DiffCG_Index
1565 delete( scf( AfterCG_WinList(win_idx) ) ) ; //** clear the spurious windows
1569 //**------------- end of windows cleaning ----------------------------------------
1571 cpr.sim.funs=funs_save;
1572 cpr.sim.funtyp=funtyp_save;
1575 //** xset('window',%windo)
1578 //les pointeurs de cpr :
1581 outtb=cpr.state.outtb;
1584 zcptr=cpr.sim.zcptr;
1585 ozptr=cpr.sim.ozptr;
1586 rpptr=cpr.sim.rpptr;
1587 ipptr=cpr.sim.ipptr;
1588 opptr=cpr.sim.opptr;
1592 inpptr=cpr.sim.inpptr;
1593 inplnk=cpr.sim.inplnk;
1594 outptr=cpr.sim.outptr;
1595 outlnk=cpr.sim.outlnk;
1596 //@l@n lnkptr=cpr.sim.lnkptr;
1597 ordclk=cpr.sim.ordclk;
1598 funtyp=cpr.sim.funtyp;
1603 clkptr=cpr.sim.clkptr
1604 labels=cpr.sim.labels;
1605 //taille totale de z : nztotal
1608 //*******************************
1609 //Checking if superblock is valid
1610 //*******************************
1612 for i=1:length(funs)-1
1613 if funtyp(i)==3 then
1614 msg=[msg;"Type 3 block''s not allowed"]
1617 if msg<>[] then message(msg),ok=%f,return,end
1620 //********************************************************
1621 // Change logical units for readf and writef blocks if any ???
1622 //********************************************************
1624 for d=1:length(funs)
1625 if funs(d)=="readf" then
1628 elseif funs(d)=="writef"
1634 //***********************************
1635 // Get the name of the file
1636 //***********************************
1641 [x,ierr] = fileinfo(rpat);
1642 S_IWRITE = 128; // mask write permission
1643 if (ierr <> 0 | bitand(x(2), S_IWRITE) == 0) then
1648 label1=[hname;rpat+"/"+hname;""];
1651 ok=%t // to avoid infinite loop
1656 label1]=scicos_getvalue("Set code generator parameters :",..
1657 ["New block''s name :";
1658 "Created files Path :";
1659 "Other object files to link with (if any)"],..
1660 list("str",1,"str",1,"str",1),label1);
1665 rpat=stripblanks(rpat);
1667 //** 1/07/06 Alan trying to solve multiple libraries during week-end
1668 if strindex(libs,"''")<>[] | strindex(libs,"""")<>[] then
1669 ierr=execstr("libs=evstr(libs)","errcatch")
1671 message(["Can''t solve other files to link"])
1677 //** Alan, I put a warning here in order to inform the user
1678 //** that the name of the superblock will change
1679 //** because the space char in name isn't allowed.
1680 if grep(rdnom," ")<>[] then
1681 message(["Superblock name cannot contains space characters.";
1682 "space chars will be automatically substituted by ""_"" "])
1684 rdnom = strsubst(rdnom," ","_");
1686 //** Put a warning here in order to inform the user
1687 //** that the name of the superblock will change
1688 //** because the "-" char could generate GCC problems
1689 //** (the C functions contains the name of the superblock).
1690 if grep(rdnom,"-")<>[] then
1691 message(["For full C compiler compatibility ";
1692 "Superblock name cannot contains ""-"" characters";
1693 """-"" chars will be automatically substituted by ""_"" "])
1695 rdnom = strsubst(rdnom,"-","_");
1697 dirinfo=fileinfo(rpat)
1700 [pathrp,fnamerp,extensionrp]=fileparts(rpat)
1701 ok=mkdir(pathrp,fnamerp+extensionrp)
1703 messagebox("Directory "+rpat+" cannot be created","modal","info");
1705 elseif filetype(dirinfo(2))<>"Directory" then
1707 messagebox(rpat+" is not a directory","modal","error");
1710 if stripblanks(rdnom)==emptystr() then
1712 messagebox("sorry C file name not defined","modal","error");
1714 if ok then break,end
1717 //////////////////////////////////////////////////
1718 maxnrpar=max(rpptr(2:$)-rpptr(1:$-1))
1719 maxnipar=max(ipptr(2:$)-ipptr(1:$-1))
1720 maxnx=max(xptr(2:$)-xptr(1:$-1))
1721 maxnz=max(zptr(2:$)-zptr(1:$-1))
1722 maxnin=max(inpptr(2:$)-inpptr(1:$-1))
1723 maxnout=max(outptr(2:$)-outptr(1:$-1))
1725 for i=1:lstsize(cpr.state.outtb)
1726 maxdim=max(size(cpr.state.outtb(i)))
1728 maxtotal=max([maxnrpar;maxnipar;maxnx;maxnz;maxnin;maxnout;maxdim]);
1730 //************************************************************************
1731 //generate the call to the blocks and blocs simulation function prototypes
1732 //************************************************************************
1734 nbcap=0;nbact=0;capt=[];actt=[];Protostalone=[];Protos=[];
1736 for i=1:length(funs)
1737 if or(i==act) then //block is an actuator
1739 [Code,actti,protoi]=call_actuator(i)
1740 wfunclist($+1)=[Code;"if(flag < 0 ) return(5 - flag);"]
1741 if nbact==1 then Protostalone=[Protostalone;"";protoi],end
1743 elseif or(i==cap) then //block is a sensor
1745 [Code,capti,protoi]=call_sensor(i)
1746 wfunclist($+1)=[Code;"if(flag < 0 ) return(5 - flag);"]
1747 if nbcap==1 then Protostalone=[Protostalone;"";protoi] ,end
1749 elseif funs(i)=="bidon"
1751 elseif funs(i)=="bidon2"
1754 ki=find(funs(i)==dfuns)
1755 dfuns=[dfuns;funs(i)]
1756 //[Code,protoi]=call_block(i)
1757 [Code]=call_block4(i)
1759 wfunclist($+1)=[Code;"if(flag < 0 ) return(5 - flag);"]
1764 Protos=[Protos;"";BlockProto(i)];
1765 Protostalone=[Protostalone;"";BlockProto(i)];
1770 //**************************************
1771 //on prend en compte l'ordre des numerotation graphique des
1772 //capteur et des actionneur
1773 [junk,index]=gsort(-actt(:,$));
1774 actt=actt(index,1:$) ;
1775 [junk,index]=gsort(-capt(:,$));
1776 capt=capt(index,1:$) ;
1778 //***************************************************
1779 //Compute the initial state and outtb (links) values
1780 //***************************************************
1781 //petite modification pour uniformiser les fichiers capteurs
1782 //et actionneurs ???
1784 for r=1:length(cap),rdcpr(cap(r))="bidon";end
1785 for r=1:length(act),rdcpr(act(r))="bidon";end
1786 Total_rdcpr=cpr.sim;Total_rdcpr.funs=rdcpr;
1790 tolerances=scs_m.props.tol;
1791 //[state,t]=scicosim(cpr.state,tcur,tf,Total_rdcpr,'start',tolerances);
1794 outtb=cpr.state.outtb;
1795 //[junk_state,t]=scicosim(cpr.state,tcur,tf,Total_rdcpr,'finish',tolerances);
1797 //***********************************
1798 // Scilab and C files generation
1799 //***********************************
1801 //** generate scilab interfacing function
1802 // of the generated scicos block
1806 //** generate C files
1807 // of the generated scicos block
1812 //** Generates Makefile, loader
1813 // and compile and link C files
1815 //** def files to build
1816 files=[rdnom rdnom+"_void_io" rdnom+"_Cblocks"]
1818 //** def files to build for standalone
1819 filestan=[rdnom+"_standalone" rdnom+"_act_sens_events" rdnom+"_Cblocks"]
1822 ok = buildnewblock(rdnom,files,filestan,rpat,libs,"","")
1826 //if ok then ok=gen_loader(),end
1829 //load the gui function
1830 exec(rpat+"/"+rdnom+"_c.sci");
1831 //Change diagram superblock to new generated block
1832 XX=update_block(XX);
1833 execstr(rdnom+"_c=resume("+rdnom+"_c)")
1838 //BlockProto : generate prototype
1839 // of a scicos block
1841 //inputs : bk : bloc index
1845 //16/06/07 Author : A.Layec
1846 function [txt]=BlockProto(bk)
1848 nin=inpptr(bk+1)-inpptr(bk); //* number of input ports */
1849 nout=outptr(bk+1)-outptr(bk); //* number of output ports */
1853 if ftyp>2000 then ftyp=ftyp-2000,end
1854 if ftyp>1000 then ftyp=ftyp-1000,end
1856 //** check function type
1857 if ftyp < 0 then //** ifthenelse eselect blocks
1861 if (ftyp<>0 & ftyp<>1 & ftyp<>2 & ftyp<>3 & ftyp<>4) then
1862 disp("types other than 0,1,2,3 or 4 are not yet supported.")
1869 txt=[get_comment("proto_blk",list(funs(bk),funtyp(bk),bk,labels(bk)));]
1875 //*********** prototype definition ***********//
1876 txtp=["(int *, int *, double *, double *, double *, int *, double *, \";
1877 " int *, double *, int *, double *, int *,int *, int *, \";
1878 " double *, int *, double *, int *);"];
1879 if (funtyp(bk)>2000 & funtyp(bk)<3000)
1880 blank = get_blank("void "+funs(bk)+"(");
1881 txtp(1) = "void "+funs(bk)+txtp(1);
1882 elseif (funtyp(bk)<2000)
1883 txtp(1) = "void C2F("+funs(bk)+")"+txtp(1);
1884 blank = get_blank("void C2F("+funs(bk)+")");
1886 txtp(2:$) = blank + txtp(2:$);
1888 //*******************************************//
1894 //*********** prototype definition ***********//
1895 txtp=["(int *, int *, double *, double *, double *, int *, double *, \";
1896 " int *, double *, int *, double *, int *,int *, int *";]
1897 if (funtyp(bk)>2000 & funtyp(bk)<3000)
1898 blank = get_blank("void "+funs(bk)+"(");
1899 txtp(1) = "void "+funs(bk)+txtp(1);
1900 elseif (funtyp(bk)<2000)
1901 txtp(1) = "void C2F("+funs(bk)+")"+txtp(1);
1902 blank = get_blank("void C2F("+funs(bk)+")");
1904 if nin>=1 | nout>=1 then
1905 txtp($)=txtp($)+", \"
1909 txtp($)=txtp($)+" double *, int * ,"
1911 txtp($)=part(txtp($),1:length(txtp($))-1); //remove last ,
1915 txtp($)=txtp($)+", \"
1919 txtp($)=txtp($)+" double *, int * ,"
1921 txtp($)=part(txtp($),1:length(txtp($))-1); //remove last ,
1926 txtp($)=txtp($)+", \"
1927 txtp=[txtp;" double *,int *);"];
1929 txtp($)=txtp($)+");";
1932 txtp(2:$) = blank + txtp(2:$);
1934 //*******************************************//
1939 //*********** prototype definition ***********//
1941 txtp=["void "+funs(bk)+...
1942 "(int *, int *, double *, double *, double *, int *, double *, \";
1943 " int *, double *, int *, double *, int *, int *, int *, \"
1944 " double **, int *, int *, double **,int *, int *"];
1946 txtp($)=txtp($)+");";
1948 txtp($)=txtp($)+", \";
1950 " double *,int *);"]
1952 blank = get_blank("void "+funs(bk));
1953 txtp(2:$) = blank + txtp(2:$);
1955 //********************************************//
1960 "void "+funs(bk)+"(scicos_block *, int );"];
1965 //Generating the routine for actuators interfacing
1968 //Authors : R. Djenid, R. Nikoukhah, A. Layec
1970 //actt=[i uk nuk_1 nuk_2 uk_t bllst(i).ipar]
1971 function Code=make_actuator(standalone)
1973 "** Copyright (c) 1989 - 2012 (INRIA)"
1974 "** Copyright (c) 2011 - 2013 (Scilab Enterprises)"
1976 "** Generated by Code_Generation toolbox of Xcos with "+getversion()
1980 "/*"+part("-",ones(1,40))+" Actuators */";
1981 "void "+rdnom+"_actuator(int *flag, int *nport, int *nevprt, double *t, void *u, int *nu1, int *nu2, int *ut, int flag1)"]
1984 " * To be customized for standalone execution";
1985 " * flag : specifies the action to be done"
1986 " * nport : specifies the index of the Super Bloc"
1987 " * regular input (The input ports are numbered"
1988 " * from the top to the bottom )"
1989 " * nevprt: indicates if an activation had been received"
1990 " * 0 = no activation"
1992 " * t : the current time value"
1993 " * u : the vector inputs value"
1994 " * nu1 : the input size 1"
1995 " * nu2 : the input size 2"
1996 " * ut : the input type"
1997 " * flag1 : learn mode (0 from terminal,1 from input file"
2004 a_actuator=[" /* skeleton to be customized */"
2006 " /* OutputUpdate (*flag==1) will be called on each iteration */"
2008 " /*if(*nevprt>0) { get the input value */"
2011 " for (l=0;l<*nu2;l++) {"
2012 " for (k=0;k<*nu1;k++) {"
2013 " printf(""Actuator: time=%f, "+...
2014 "u(%d,%d) of actuator %d is %f "+...
2016 " *t, k, l, *nport,"+...
2017 "*((double *) u+(k+l*(*nu1))));"
2023 " for (l=0;l<*nu2;l++) {"
2024 " for (k=0;k<*nu1;k++) {"
2025 " printf(""Actuator: time=%f, "+...
2026 "u(%d,%d) of actuator %d is %f,%f "+...
2028 " *t, k, l, *nport,"+...
2029 "*((double *) u+(k+l*(*nu1))),"+...
2030 "*((double *) u+((*nu1)*(*nu2)+k+l*(*nu1))));"
2036 " for (l=0;l<*nu2;l++) {"
2037 " for (k=0;k<*nu1;k++) {"
2038 " printf(""Actuator: time=%f, "+...
2039 "u(%d,%d) of actuator %d is %i "+...
2041 " *t, k, l, *nport,"+...
2042 "*((char *) u+(k+l*(*nu1))));"
2048 " for (l=0;l<*nu2;l++) {"
2049 " for (k=0;k<*nu1;k++) {"
2050 " printf(""Actuator: time=%f, "+...
2051 "u(%d,%d) of actuator %d is %hd "+...
2053 " *t, k, l, *nport,"+...
2054 "*((short *) u+(k+l*(*nu1))));"
2060 " for (l=0;l<*nu2;l++) {"
2061 " for (k=0;k<*nu1;k++) {"
2062 " printf(""Actuator: time=%f, "+...
2063 "u(%d,%d) of actuator %d is %ld "+...
2065 " *t, k, l, *nport,"+...
2066 "*((long *) u+(k+l*(*nu1))));"
2072 " for (l=0;l<*nu2;l++) {"
2073 " for (k=0;k<*nu1;k++) {"
2074 " printf(""Actuator: time=%f, "+...
2075 "u(%d,%d) of actuator %d is %d "+...
2077 " *t, k, l, *nport,"+...
2078 "*((unsigned char *) u+(k+l*(*nu1))));"
2084 " for (l=0;l<*nu2;l++) {"
2085 " for (k=0;k<*nu1;k++) {"
2086 " printf(""Actuator: time=%f, "+...
2087 "u(%d,%d) of actuator %d is %hu "+...
2089 " *t, k, l, *nport,"+...
2090 "*((unsigned short *) u+(k+l*(*nu1))));"
2096 " for (l=0;l<*nu2;l++) {"
2097 " for (k=0;k<*nu1;k++) {"
2098 " printf(""Actuator: time=%f, "+...
2099 "u(%d,%d) of actuator %d is %lu "+...
2101 " *t, k, l, *nport,"+...
2102 "*((unsigned long *) u+(k+l*(*nu1))));"
2109 " case 4 : /* actuator initialisation */"
2110 " /* do whatever you want to initialize the actuator */"
2112 " case 5 : /* actuator ending */"
2113 " /* do whatever you want to end the actuator */"
2121 nc=size(act,"*") // Alan : d'o? viens act ?????
2122 // reponse : de do_compile_superblock!
2123 typ=["""%f "]; //time
2125 typ($)=typ($)+""" \"
2127 for j=1:sum(actt(i,3)*actt(i,4))
2129 typ($)=typ($)+scs_c_n2c_fmt(actt(i,5))+" ";
2132 typ($)=typ($)+"\n"", \"
2133 typ(1)=" fprintf(fprw,"+typ(1);
2135 if size(typ,1) <> 1 then
2136 typ(2:$) = bl+typ(2:$);
2138 //Code1=' fprintf(fprw, '"'+typ+' \n'",*t'
2139 Code1=[typ;bl+"*t"];
2140 //actt=[i uk nuk_1 nuk_2 uk_t bllst(i).ipar]
2141 for i=1:size(actt,1)
2142 ni=actt(i,3)*actt(i,4) // dimension of ith output
2143 Code1($)=Code1($)+","
2146 if actt(i,5)<>11 then
2147 Code1($)=Code1($)+...
2148 "*((("+scs_c_n2c_typ(actt(i,5))+" *)("+...
2149 rdnom+"_block_outtbptr+"+string(actt(i,2)-1)+"))"+...
2150 "+"+string(j-1)+")";
2152 Code1($)=Code1($)+...
2153 "*((("+scs_c_n2c_typ(actt(i,5))+" *)("+...
2154 rdnom+"_block_outtbptr+"+string(actt(i,2)-1)+"))"+...
2155 "+"+string((j-1))+"), "+...
2156 "*((("+scs_c_n2c_typ(actt(i,5))+" *)("+...
2157 rdnom+"_block_outtbptr+"+string(actt(i,2)-1)+"))"+...
2158 "+"+string(ni+(j-1))+")";
2161 Code1($)=Code1($)+", ";
2165 Code1($)=Code1($)+");"
2169 if nc==1|~standalone then
2173 " if (flag1 == 0) {"
2176 " else if (flag1 == 1) {"
2177 " if (*flag == 4 && *nport == 1) {"
2178 " fprw=fopen(output,'"wt'");"
2179 " if( fprw == NULL )"
2181 " printf('"Error opening file: %s\n'", output);"
2184 " }else if (*flag == 2 /* && *nevprt>0 */) {"
2186 " }else if (*flag == 5 && *nport == 1) {"
2192 S=" switch (*nport) {"
2195 " case "+string(k)+" :/* Port number "+string(k)+" ----------*/"
2208 " else if (flag1 == 1) {"
2209 " if (*flag == 4 && *nport == 1) {"
2210 " fprw=fopen(output,'"wt'");"
2211 " if( fprw == NULL ) {"
2212 " printf('"Error opening file: %s\n'", output);"
2216 " else if (*flag == 2 /*&& *nevprt>0*/ ) {"
2219 " else if (*flag == 5 && *nport == 1) {"
2227 //** Generates the scicos computational function
2228 // associated with the block
2229 //12/07/07 Alan Layec
2230 function make_computational42(filename)
2233 outtb=cpr.state.outtb;
2234 tevts=cpr.state.tevts;
2235 evtspt=cpr.state.evtspt;
2236 outptr=cpr.sim.outptr;
2237 funtyp=cpr.sim.funtyp;
2238 clkptr=cpr.sim.clkptr;
2239 ordptr=cpr.sim.ordptr;
2240 pointi=cpr.state.pointi;
2242 zcptr=cpr.sim.zcptr;
2244 ozptr=cpr.sim.ozptr;
2245 opptr=cpr.sim.opptr;
2247 rpptr=cpr.sim.rpptr;
2248 ipptr=cpr.sim.ipptr;
2249 inpptr=cpr.sim.inpptr;
2252 modptr=cpr.sim.modptr;
2253 inplnk=cpr.sim.inplnk;
2255 outlnk=cpr.sim.outlnk;
2259 noord=size(cpr.sim.oord,1);
2260 nzord=size(cpr.sim.zord,1);
2261 niord=size(cpr.sim.iord,1);
2264 Indent2=Indent+Indent;
2267 nZ=size(z,"*"); //** index of work in z
2268 nO=lstsize(oz); //** index of outtb in oz
2272 // Open file to write in
2273 fd = mopen(filename, "wt");
2276 "** SCILAB Computational function"
2277 "** Copyright (c) 1989 - 2012 (INRIA)"
2278 "** Copyright (c) 2011 - 2013 (Scilab Enterprises)"
2280 "** Generated by Code_Generation toolbox of Xcos with "+getversion()
2284 "/* ---- Headers ---- */"
2285 "#include <stdio.h>"
2286 "#include <stdlib.h>"
2287 "#include <memory.h>"
2288 "#include <string.h>"
2289 "#include ""machine.h"" "
2290 "#include ""scicos.h"" "
2291 "#include ""scicos_malloc.h"" "
2292 "#include ""scicos_free.h"" "
2295 if getos() == "Windows" then
2298 "#define max(a,b) ((a) >= (b) ? (a) : (b))"
2299 "#define min(a,b) ((a) <= (b) ? (a) : (b))"
2304 mputl([Protos ; ""], fd);
2306 mputl(["/* Table of constant values */"
2307 "static int nrd_"+string(0:maxtotal)'+" = "+string(0:maxtotal)'+";"], fd);
2310 mputl("static int nrd_10 = 10;", fd);
2313 mputl("static int nrd_11 = 11;", fd);
2317 mputl("static int nrd_81 = 81;", fd);
2320 mputl("static int nrd_82 = 82;", fd);
2323 mputl("static int nrd_84 = 84;", fd);
2325 if maxtotal<811 then
2326 mputl("static int nrd_811 = 811;", fd);
2328 if maxtotal<812 then
2329 mputl("static int nrd_812 = 812;", fd);
2331 if maxtotal<814 then
2332 mputl("static int nrd_814 = 814;", fd);
2336 "/* Some general static variables */"
2337 "static double zero=0;"
2338 "static double w[1];"
2339 "static int aaa=0, bbb=0;"], fd);
2342 "/*"+part("-",ones(1,40))+" Block Computational function */ ";
2343 "int "+rdnom+"(scicos_block *block, int flag)"
2345 " /* declaration of local variables for that block struct */"
2346 " double* z = block->z;"
2347 " void **ozptr = block->ozptr;"
2348 " void **oparptr = block->oparptr;"
2349 " int nopar = block->nopar;"
2350 " double* x = block->x;"
2351 " double* xd = block->xd;"
2352 " double* res = block->res;"
2353 " void** u = block->inptr;"
2354 " void** y = block->outptr;"
2355 " int nevprt = block->nevprt;"
2356 " double* rpar = block->rpar;"
2357 " int nrpar = block->nrpar;"
2358 " int* ipar = block->ipar;"
2359 " int nipar = block->nipar;"
2360 " double* g = block->g;"
2361 " int* mode = block->mode;"
2362 " void **work = block->work;"
2364 " double t = get_scicos_time();"
2365 " int phase = get_phase_simulation();"
2369 " int* reentryflag = NULL;"
2371 " double *args[100];"
2374 " void **"+rdnom+"_block_outtbptr;"
2375 " scicos_block *block_"+rdnom+";"
2377 " /* work of blocks are catenated at the end of z */"
2378 " work = (void **)(z+"+string(nZ)+");"
2380 " /* "+rdnom+"_block_outtbptr is catenated at the end of oz */"
2381 " "+rdnom+"_block_outtbptr = (void **)(ozptr+"+string(nO)+");"
2383 " /* struct of all blocks are stored in work of that block struct */"
2384 " block_"+rdnom+"=(scicos_block*) *block->work;"
2386 " /* Copy inputs in the block outtb */"], fd);
2389 for i=1:size(capt,1)
2390 ni=capt(i,3)*capt(i,4); //** dimension of ith input
2391 if capt(i,5)<>11 then
2392 mputl([" memcpy(*("+rdnom+"_block_outtbptr+"+string(capt(i,2)-1)+"),"+...
2393 "*(u+"+string(capt(i,6)-1)+"),"+...
2394 string(ni)+"*sizeof("+mat2c_typ(capt(i,5))+"));"], fd);
2396 mputl([" memcpy(*("+rdnom+"_block_outtbptr+"+string(capt(i,2)-1)+"),"+...
2397 "*(u+"+string(capt(i,6)-1)+"),"+...
2398 string(2*ni)+"*sizeof("+mat2c_typ(capt(i,5))+"));"], fd);
2403 " if (flag != 4 && flag != 6 && flag != 5){"
2404 " reentryflag=(int*) ((scicos_block *)(*block->work)+"+string(nblk)+");"
2405 " if (*reentryflag==0){"
2406 " *reentryflag=1;"], fd);
2409 nin=inpptr(kf+1)-inpptr(kf); //** number of input ports
2410 nout=outptr(kf+1)-outptr(kf); //** number of output ports
2411 nx=xptr(kf+1)-xptr(kf);
2412 ng=zcptr(kf+1)-zcptr(kf);
2413 nmode=modptr(kf+1)-modptr(kf);
2417 " "+get_comment("set_blk",list(funs(kf),funtyp(kf),kf,labels(kf)));], fd);
2420 lprt=inplnk(inpptr(kf)-1+k);
2421 mputl([" block_"+rdnom+"["+string(kf-1)+"].inptr["+string(k-1)+"] = "+...
2422 rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2425 lprt=outlnk(outptr(kf)-1+k);
2426 mputl([" block_"+rdnom+"["+string(kf-1)+"].outptr["+string(k-1)+"] = "+...
2427 rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2429 mputl([" block_"+rdnom+"["+string(kf-1)+"].z = &(z["+...
2430 string(zptr(kf)-1)+"]);"], fd);
2432 mputl([" block_"+rdnom+"["+string(kf-1)+"].x = &(x["+...
2433 string(xptr(kf)-1)+"]);"], fd);
2435 mputl([" block_"+rdnom+"["+string(kf-1)+"].x = &(zero);"
2436 " block_"+rdnom+"["+string(kf-1)+"].xd = w;"], fd);
2439 mputl([" block_"+rdnom+"["+string(kf-1)+"].g = &(g["+...
2440 string(zcptr(kf)-1)+"]);"], fd);
2442 mputl([" block_"+rdnom+"["+string(kf-1)+"].g = &(zero);";], fd);
2445 mputl([" block_"+rdnom+"["+string(kf-1)+"].mode = &(mode["+...
2446 string(modptr(kf)-1)+"]);"], fd);
2448 if (part(funs(kf),1:7) ~= "capteur" &...
2449 part(funs(kf),1:10) ~= "actionneur" &...
2450 funs(kf) ~= "bidon" &...
2451 funs(kf) ~= "bidon2") then
2453 if (rpptr(kf+1)-rpptr(kf)>0) then
2454 mputl([" block_"+rdnom+"["+string(kf-1)+"].rpar = &(rpar["+...
2455 string(rpptr(kf)-1)+"]);"], fd);
2458 if (ipptr(kf+1)-ipptr(kf)>0) then
2459 mputl([" block_"+rdnom+"["+string(kf-1)+"].ipar = &(ipar["+...
2460 string(ipptr(kf)-1)+"]);"], fd);
2463 if (opptr(kf+1)-opptr(kf)>0) then
2464 nopar = opptr(kf+1)-opptr(kf);
2466 mputl([" block_"+rdnom+"["+string(kf-1)+"].oparptr["+string(k-1)+...
2467 "] = oparptr["+string(opptr(kf)-1+k-1)+"];"], fd);
2471 if (ozptr(kf+1)-ozptr(kf)>0) then
2472 noz = ozptr(kf+1)-ozptr(kf);
2474 mputl([" block_"+rdnom+"["+string(kf-1)+"].ozptr["+string(k-1)+...
2475 "] = ozptr["+string(ozptr(kf)-1+k-1)+"];"], fd);
2479 mputl([" block_"+rdnom+"["+string(kf-1)+"].work ="+...
2480 " (void **)(((double *)work)+"+string(kf-1)+");"], fd);
2488 /////////////////////////////////////////////
2491 //** find activation number
2492 blks=find(funtyp>-1);
2496 for ev=clkptr(blk):clkptr(blk+1)-1
2497 if funs(blk)=="bidon" then
2498 if ev > clkptr(howclk) -1
2513 nx=xptr(kf+1)-xptr(kf);
2516 " block_"+rdnom+"["+string(kf-1)+"].xd=&(xd["+...
2517 string(xptr(kf)-1)+"]);"]
2519 if part(funs(kf),1:10) == "actionneur" then
2525 " "+get_comment("update_xd",list())
2531 write_code_odoit(1) //** first pass
2532 write_code_odoit(0) //** second pass
2536 mputl([" if (flag == 0) { "+get_comment("flag",list(flag))
2546 //** continuous time blocks must be activated
2549 txt = write_code_cdoit(flag);
2553 Indent+" switch (nevprt) {"
2556 Indent2+" case "+string(0)+" : "+...
2557 get_comment("ev",list(0))
2560 txt3=[txt3;" break;";""]
2566 //** blocks with input discrete event must be activated
2567 //** for flag 1, 2 and 3
2568 if size(evs,2)>=1 then
2572 txt2=write_code_doit(ev,flag);
2574 //** adjust event number because of bidon block
2575 new_ev=ev-(clkptr(howclk)-1)
2578 Indent2+[" case "+string(new_ev)+" : "+...
2579 get_comment("ev",list(new_ev))
2589 Indent+" switch (nevprt) {"
2607 if flag==1 & txt22==[] then
2608 mputl([" if (flag == "+string(flag)+") { "+...
2609 get_comment("flag",list(flag))
2613 mputl([" else if (flag == "+string(flag)+") { "+...
2614 get_comment("flag",list(flag))
2625 mputl([" else if (flag == "+string(flag)+") { "+...
2626 get_comment("flag",list(flag))], fd);
2630 if zcptr(kf+1)-zcptr(kf) <> 0 then
2632 " block_"+rdnom+"["+string(kf-1)+"].g="+...
2633 "&(g["+string(zcptr(kf)-1)+"]);"]
2637 mputl([" "+get_comment("update_g",list())
2647 mputl([" else if (flag == 4) { "+get_comment("flag",list(4))
2648 " if ((*block->work=scicos_malloc(sizeof(scicos_block)*"+...
2649 string(nblk)+"+sizeof(int)))== NULL) return 0;";
2650 " reentryflag=(int*) ((scicos_block *)(*block->work)+"+string(nblk)+");"
2652 " block_"+rdnom+"=(scicos_block*) *block->work;"], fd);
2655 nin=inpptr(kf+1)-inpptr(kf); //* number of input ports */
2656 nout=outptr(kf+1)-outptr(kf); //* number of output ports */
2657 nx=xptr(kf+1)-xptr(kf);
2658 ng=zcptr(kf+1)-zcptr(kf);
2659 nmode=modptr(kf+1)-modptr(kf);
2663 " "+get_comment("set_blk",list(funs(kf),funtyp(kf),kf,labels(kf)));], fd);
2665 mputl([" block_"+rdnom+"["+string(kf-1)+"].type = "+...
2666 string(funtyp(kf))+";";
2667 " block_"+rdnom+"["+string(kf-1)+"].ztyp = "+...
2668 string(ztyp(kf))+";";
2669 " block_"+rdnom+"["+string(kf-1)+"].ng = "+...
2670 string(zcptr(kf+1)-zcptr(kf))+";"], fd);
2673 mputl([" block_"+rdnom+"["+string(kf-1)+"].nx = "+...
2675 " block_"+rdnom+"["+string(kf-1)+"].x = &(x["+...
2676 string(xptr(kf)-1)+"]);"], fd);
2678 mputl([" block_"+rdnom+"["+string(kf-1)+"].nx = 1;";
2679 " block_"+rdnom+"["+string(kf-1)+"].x = &(zero);"
2680 " block_"+rdnom+"["+string(kf-1)+"].xd = w;"], fd);
2684 mputl([" block_"+rdnom+"["+string(kf-1)+"].g = &(g["+...
2685 string(zcptr(kf)-1)+"]);"], fd);
2687 mputl([" block_"+rdnom+"["+string(kf-1)+"].g = &(zero);";], fd);
2690 mputl([" block_"+rdnom+"["+string(kf-1)+"].mode = &(mode["+...
2691 string(modptr(kf)-1)+"]);"], fd);
2693 mputl([" block_"+rdnom+"["+string(kf-1)+"].nz = "+...
2694 string(zptr(kf+1)-zptr(kf))+";";
2695 " block_"+rdnom+"["+string(kf-1)+"].noz = "+...
2696 string(ozptr(kf+1)-ozptr(kf))+";";
2697 " block_"+rdnom+"["+string(kf-1)+"].nrpar = "+...
2698 string(rpptr(kf+1)-rpptr(kf))+";";
2699 " block_"+rdnom+"["+string(kf-1)+"].nopar = "+...
2700 string(opptr(kf+1)-opptr(kf))+";";
2701 " block_"+rdnom+"["+string(kf-1)+"].nipar = "+...
2702 string(ipptr(kf+1)-ipptr(kf))+";"
2703 " block_"+rdnom+"["+string(kf-1)+"].nin = "+...
2704 string(inpptr(kf+1)-inpptr(kf))+";";
2705 " block_"+rdnom+"["+string(kf-1)+"].nout = "+...
2706 string(outptr(kf+1)-outptr(kf))+";";
2707 " block_"+rdnom+"["+string(kf-1)+"].nevout = "+...
2708 string(clkptr(kf+1)-clkptr(kf))+";";
2709 " block_"+rdnom+"["+string(kf-1)+"].nmode = "+...
2710 string(modptr(kf+1)-modptr(kf))+";"], fd);
2712 mputl([" if ((block_"+rdnom+"["+string(kf-1)+"].evout = "+...
2713 "calloc(block_"+rdnom+"["+string(kf-1)+"].nevout,sizeof(double)))== NULL) return 0;"], fd);
2715 //***************************** input port *****************************//
2716 //** alloc insz/inptr **//
2717 mputl([" if ((block_"+rdnom+"["+string(kf-1)+"].insz = "+...
2718 "malloc(3*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nin))== NULL) return 0;";
2719 " if ((block_"+rdnom+"["+string(kf-1)+"].inptr = "+...
2720 "malloc(sizeof(void *)*block_"+rdnom+"["+string(kf-1)+"].nin))== NULL) return 0;"], fd);
2724 lprt=inplnk(inpptr(kf)-1+k);
2725 mputl([" block_"+rdnom+"["+string(kf-1)+"].inptr["+string(k-1)+"] = "+...
2726 rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2731 lprt=inplnk(inpptr(kf)-1+k);
2732 mputl([" block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1))+"] = "+...
2733 string(size(outtb(lprt),1))+";"], fd);
2738 lprt=inplnk(inpptr(kf)-1+k);
2739 mputl([" block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1)+nin)+"] = "+...
2740 string(size(outtb(lprt),2))+";"], fd);
2745 lprt=inplnk(inpptr(kf)-1+k);
2746 mputl([" block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1)+2*nin)+"] = "+...
2747 mat2scs_c_typ(outtb(lprt))+";"], fd);
2749 //**********************************************************************//
2751 //***************************** output port *****************************//
2752 //** alloc outsz/outptr **//
2753 mputl([" if ((block_"+rdnom+"["+string(kf-1)+"].outsz = "+...
2754 "malloc(3*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nout))== NULL) return 0;";
2755 " if ((block_"+rdnom+"["+string(kf-1)+"].outptr = "+...
2756 "malloc(sizeof(void*)*block_"+rdnom+"["+string(kf-1)+"].nout))== NULL) return 0;"], fd);
2760 lprt=outlnk(outptr(kf)-1+k);
2761 mputl([" block_"+rdnom+"["+string(kf-1)+"].outptr["+string(k-1)+"] = "+...
2762 rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2767 lprt=outlnk(outptr(kf)-1+k);
2768 mputl([" block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1))+...
2769 "] = "+string(size(outtb(lprt),1))+";"], fd);
2774 lprt=outlnk(outptr(kf)-1+k);
2775 mputl([" block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1)+nout)+...
2776 "] = "+string(size(outtb(lprt),2))+";"], fd)
2781 lprt=outlnk(outptr(kf)-1+k);
2782 mputl([" block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1)+2*nout)+...
2783 "] = "+mat2scs_c_typ(outtb(lprt))+";"], fd);
2785 //**********************************************************************//
2787 mputl([" block_"+rdnom+"["+string(kf-1)+"].z = &(z["+...
2788 string(zptr(kf)-1)+"]);"], fd);
2790 //***************************** object state *****************************//
2791 if (ozptr(kf+1)-ozptr(kf)>0) then
2792 noz = ozptr(kf+1)-ozptr(kf);
2793 mputl([" if ((block_"+rdnom+"["+string(kf-1)+"].ozptr = "+...
2794 "malloc(sizeof(void *)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";
2795 " if ((block_"+rdnom+"["+string(kf-1)+"].ozsz = "+...
2796 "malloc(2*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";
2797 " if ((block_"+rdnom+"["+string(kf-1)+"].oztyp = "+...
2798 "malloc(sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";], fd);
2802 mputl([" block_"+rdnom+"["+string(kf-1)+"].ozptr["+string(k-1)+...
2803 "] = ozptr["+string(ozptr(kf)-1+k-1)+"];"], fd);
2808 mputl([" block_"+rdnom+"["+string(kf-1)+"].ozsz["+string(k-1)+...
2809 "] = "+string(size(oz(ozptr(kf)-1+k),1))+";"], fd);
2814 mputl([" block_"+rdnom+"["+string(kf-1)+"].ozsz["+string(k-1)+...
2815 "] = "+string(size(oz(ozptr(kf)-1+k),2))+";"], fd);
2820 mputl([" block_"+rdnom+"["+string(kf-1)+"].oztyp["+string(k-1)+...
2821 "] = "+mat2scs_c_typ(oz(ozptr(kf)-1+k))+";"], fd);
2824 //************************************************************************//
2826 if (part(funs(kf),1:7) ~= "capteur" &...
2827 part(funs(kf),1:10) ~= "actionneur" &...
2828 funs(kf) ~= "bidon" &...
2829 funs(kf) ~= "bidon2") then
2830 if (rpptr(kf+1)-rpptr(kf)>0) then
2831 mputl([" block_"+rdnom+"["+string(kf-1)+...
2832 "].rpar = &(rpar["+string(rpptr(kf)-1)+"]);"], fd);
2834 if (ipptr(kf+1)-ipptr(kf)>0) then
2835 mputl([" block_"+rdnom+"["+string(kf-1)+...
2836 "].ipar = &(ipar["+string(ipptr(kf)-1)+"]);"], fd);
2839 if (opptr(kf+1)-opptr(kf)>0) then
2840 mputl([" if ((block_"+rdnom+"["+string(kf-1)+"].oparptr = "+...
2841 "malloc(sizeof(void *)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL) return 0;";
2842 " if ((block_"+rdnom+"["+string(kf-1)+"].oparsz = "+...
2843 "malloc(2*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL) return 0;";
2844 " if ((block_"+rdnom+"["+string(kf-1)+"].opartyp = "+...
2845 "malloc(sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL) return 0;";
2847 nopar = opptr(kf+1)-opptr(kf);
2850 mputl([" block_"+rdnom+"["+string(kf-1)+"].oparptr["+string(k-1)+...
2851 "] = oparptr["+string(opptr(kf)-1+k-1)+"];"], fd);
2855 mputl([" block_"+rdnom+"["+string(kf-1)+"].oparsz["+string(k-1)+...
2856 "] = "+string(size(opar(opptr(kf)-1+k),1))+";"], fd);
2860 mputl([" block_"+rdnom+"["+string(kf-1)+"].oparsz["+string(nopar+(k-1))+...
2861 "] = "+string(size(opar(opptr(kf)-1+k),2))+";"], fd)
2865 mputl([" block_"+rdnom+"["+string(kf-1)+"].opartyp["+string(k-1)+...
2866 "] = "+mat2scs_c_typ(opar(opptr(kf)-1+k))+";"], fd)
2871 mputl([" block_"+rdnom+"["+string(kf-1)+...
2872 "].work = (void **)(((double *)work)+"+string(kf-1)+");"
2873 " block_"+rdnom+"["+string(kf-1)+...
2874 "].nevprt = nevprt;"], fd);
2880 // if or(kf==act) | or(kf==cap) then
2882 // txt = call_block42(kf,0,4);
2883 // if txt <> [] then
2890 txt = call_block42(kf,0,4);
2898 //** cst blocks and it's dep
2899 txt=write_code_idoit()
2903 " /* initial blocks must be called with flag 1 */"
2906 // for kf=iord(:,1)'
2907 // // if or(kf==act) then
2908 // // if stalone then
2909 // // txt = call_block42(kf,0,1);
2910 // // if txt <> [] then
2917 // txt = call_block42(kf,0,1);
2918 // if txt <> [] then
2929 mputl([" else if (flag == 5) { "+get_comment("flag",list(5))
2930 " block_"+rdnom+"=*block->work;"], fd);
2933 nin=inpptr(kf+1)-inpptr(kf); //* number of input ports */
2934 nout=outptr(kf+1)-outptr(kf); //* number of output ports */
2938 " "+get_comment("set_blk",list(funs(kf),funtyp(kf),kf,labels(kf)));], fd);
2941 lprt=inplnk(inpptr(kf)-1+k);
2942 mputl([" block_"+rdnom+"["+string(kf-1)+"].inptr["+string(k-1)+...
2943 "] = "+rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2946 lprt=outlnk(outptr(kf)-1+k);
2947 mputl([" block_"+rdnom+"["+string(kf-1)+"].outptr["+string(k-1)+...
2948 "] = "+rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2950 mputl([" block_"+rdnom+"["+string(kf-1)+"].z=&(z["+...
2951 string(zptr(kf)-1)+"]);"], fd);
2952 if (part(funs(kf),1:7) ~= "capteur" &...
2953 part(funs(kf),1:10) ~= "actionneur" &...
2954 funs(kf) ~= "bidon" &...
2955 funs(kf) ~= "bidon2") then
2957 if (rpptr(kf+1)-rpptr(kf)>0) then
2958 mputl([" block_"+rdnom+"["+string(kf-1)+...
2959 "].rpar=&(rpar["+string(rpptr(kf)-1)+"]);"], fd);
2962 if (ipptr(kf+1)-ipptr(kf)>0) then
2963 mputl([" block_"+rdnom+"["+string(kf-1)+...
2964 "].ipar=&(ipar["+string(ipptr(kf)-1)+"]);"], fd);
2967 if (opptr(kf+1)-opptr(kf)>0) then
2968 nopar = opptr(kf+1)-opptr(kf);
2970 mputl([" block_"+rdnom+"["+string(kf-1)+"].oparptr["+string(k-1)+...
2971 "] = oparptr["+string(opptr(kf)-1+k-1)+"];"], fd);
2975 if (ozptr(kf+1)-ozptr(kf)>0) then
2976 noz = ozptr(kf+1)-ozptr(kf);
2978 mputl([" block_"+rdnom+"["+string(kf-1)+"].ozptr["+string(k-1)+...
2979 "] = ozptr["+string(ozptr(kf)-1+k-1)+"];"], fd);
2984 mputl([" block_"+rdnom+"["+string(kf-1)+...
2985 "].work=(void **)(((double *)work)+"+string(kf-1)+");"], fd)
2989 // if or(kf==act) | or(kf==cap) then
2991 // txt = call_block42(kf,0,5);
2992 // if txt <> [] then
2999 txt = call_block42(kf,0,5);
3008 " for (kf = 0; kf < "+string(nblk)+"; ++kf) {"
3009 " if (block_"+rdnom+"[kf].insz!=NULL) {"
3010 " free(block_"+rdnom+"[kf].insz);"
3014 " if (block_"+rdnom+"[kf].outsz!=NULL){"
3015 " free(block_"+rdnom+"[kf].outsz);"
3019 " if (block_"+rdnom+"[kf].nopar!=0){"
3020 " if (block_"+rdnom+"[kf].oparptr!=NULL){"
3021 " free(block_"+rdnom+"[kf].oparptr);"
3025 " if (block_"+rdnom+"[kf].oparsz!=NULL){"
3026 " free(block_"+rdnom+"[kf].oparsz);"
3030 " if (block_"+rdnom+"[kf].opartyp!=NULL){"
3031 " free(block_"+rdnom+"[kf].opartyp);"
3036 " if (block_"+rdnom+"[kf].noz!=0){"
3037 " if (block_"+rdnom+"[kf].ozptr!=NULL){"
3038 " free(block_"+rdnom+"[kf].ozptr);"
3042 " if (block_"+rdnom+"[kf].ozsz!=NULL){"
3043 " free(block_"+rdnom+"[kf].ozsz);"
3047 " if (block_"+rdnom+"[kf].oztyp!=NULL){"
3048 " free(block_"+rdnom+"[kf].oztyp);"
3053 " if (block_"+rdnom+"[kf].evout!=NULL){"
3054 " free(block_"+rdnom+"[kf].evout);"
3059 " scicos_free(block_"+rdnom+");"
3063 for i=1:size(actt,1)
3064 ni=actt(i,3)*actt(i,4); // dimension of ith input
3065 if actt(i,5)<>11 then
3066 mputl([" memcpy(*(y+"+string(actt(i,6)-1)+"),"+...
3067 "*("+rdnom+"_block_outtbptr+"+string(actt(i,2)-1)+"),"+...
3068 string(ni)+"*sizeof("+mat2c_typ(actt(i,5))+"));"], fd);
3070 mputl([" memcpy(*(y+"+string(actt(i,6)-1)+"),"+...
3071 "*("+rdnom+"_block_outtbptr+"+string(actt(i,2)-1)+"),"+...
3072 string(2*ni)+"*sizeof("+mat2c_typ(actt(i,5))+"));"], fd);
3079 "} /* "+rdnom+" */"], fd);
3085 //generates skeleton of external world events handling function
3086 function Code=make_outevents()
3094 Code=[ "/*"+part("-",ones(1,40))+" External events handling function */";
3095 "void "+rdnom+"_events(int *nevprt,double *t)";
3097 "/* set next event time and associated events ports"
3098 " * nevprt has binary expression b1..b"+string(newszclkIN)+" where bi is a bit"
3099 " * bi is set to 1 if an activation is received by port i. Note that"
3100 " * more than one activation can be received simultaneously"
3101 " * Caution: at least one bi should be equal to one */"
3104 if (newszclkIN <> 0) then
3106 " int i,p,b[]={"+strcat(z(ones(1,newszclkIN)),",")+"};"
3108 "/* this is an example for the activation of events ports */"
3111 if newszclkIN>1 then
3113 Code($+1)=" b["+string(bb-1)+"]=1;"
3119 "/* definition of the step time */"
3122 "/* External events handling process */"
3124 " for (i=0;i<"+string(newszclkIN)+";i++) {"
3125 " *nevprt=*nevprt+b[i]*p;"
3132 "/* definition of the step time */"
3138 //Generating the routine for sensors interfacing
3141 //Author : R. Djenidi, R. Nikoukhah, A. Layec
3143 function Code=make_sensor(standalone)
3144 Call=["/*"+part("-",ones(1,40))+" Sensor */";
3145 "void "+rdnom+"_sensor(int *flag, int *nport, int *nevprt, double *t, void *y, int *ny1, int *ny2, int *yt, int *flag1)"]
3148 " * To be customized for standalone execution";
3149 " * flag : specifies the action to be done"
3150 " * nport : specifies the index of the Super Bloc"
3151 " * regular input (The input ports are numbered"
3152 " * from the top to the bottom )"
3153 " * nevprt: indicates if an activation had been received"
3154 " * 0 = no activation"
3156 " * t : the current time value"
3157 " * y : the vector outputs value"
3158 " * ny1 : the output size 1"
3159 " * ny2 : the output size 2"
3160 " * yt : the output type"
3161 " * flag1 : learn mode (0 from terminal,1 from input file"
3170 a_sensor=[" switch (*flag) {"
3171 " case 1 : /* set the output value */"
3172 " printf(""Require outputs of sensor number %d\n"", *nport);"
3173 " printf(""time is: %f\n"", *t);"
3174 " printf(""sizes of the sensor output is: %d,%d\n"", *ny1,*ny2);"
3177 " printf(""type of the sensor output is: %d (double) \n"", *yt);"
3178 " puts(""Please set the sensor output values"");"
3179 " for (l=0;l<*ny2;l++) {"
3180 " for (k=0;k<*ny1;k++) {"
3181 " printf(""y(%d,%d) : "",k,l);"
3182 " scanf(""%lf"", (double *) y+(k+l*(*ny1)));"
3188 " printf(""type of the sensor output is: %d (complex) \n"", *yt);"
3189 " puts(""Please set the sensor output values"");"
3190 " for (l=0;l<*ny2;l++) {"
3191 " for (k=0;k<*ny1;k++) {"
3192 " printf(""y(%d,%d) real part : "",k,l);"
3193 " scanf(""%lf"", (double *) y+(k+l*(*ny1)));"
3194 " printf(""y(%d,%d) imag part : "",k,l);"
3195 " scanf(""%lf"", (double *) y+((*ny1)*(*ny2)+k+l*(*ny1)));"
3201 " printf(""type of the sensor output is: %d (char) \n"", *yt);"
3202 " puts(""Please set the sensor output values"");"
3203 " for (l=0;l<*ny2;l++) {"
3204 " for (k=0;k<*ny1;k++) {"
3205 " printf(""y(%d,%d) : "",k,l);"
3206 " scanf(""%i"", (char *) y+(k+l*(*ny1)));"
3212 " printf(""type of the sensor output is: %d (char) \n"", *yt);"
3213 " puts(""Please set the sensor output values"");"
3214 " for (l=0;l<*ny2;l++) {"
3215 " for (k=0;k<*ny1;k++) {"
3216 " printf(""y(%d,%d) : "",k,l);"
3217 " scanf(""%hd"", (short *) y+(k+l*(*ny1)));"
3223 " printf(""type of the sensor output is: %d (long) \n"", *yt);"
3224 " puts(""Please set the sensor output values"");"
3225 " for (l=0;l<*ny2;l++) {"
3226 " for (k=0;k<*ny1;k++) {"
3227 " printf(""y(%d,%d) : "",k,l);"
3228 " scanf(""%ld"", (long *) y+(k+l*(*ny1)));"
3234 " printf(""type of the sensor output is: %d (unsigned char) \n"", *yt);"
3235 " puts(""Please set the sensor output values"");"
3236 " for (l=0;l<*ny2;l++) {"
3237 " for (k=0;k<*ny1;k++) {"
3238 " printf(""y(%d,%d) : "",k,l);"
3239 " scanf(""%d"", (unsigned char *) y+(k+l*(*ny1)));"
3245 " printf(""type of the sensor output is: %d (unsigned short) \n"", *yt);"
3246 " puts(""Please set the sensor output values"");"
3247 " for (l=0;l<*ny2;l++) {"
3248 " for (k=0;k<*ny1;k++) {"
3249 " printf(""y(%d,%d) : "",k,l);"
3250 " scanf(""%hu"", (unsigned short *) y+(k+l*(*ny1)));"
3256 " printf(""type of the sensor output is: %d (unsigned long) \n"", *yt);"
3257 " puts(""Please set the sensor output values"");"
3258 " for (l=0;l<*ny2;l++) {"
3259 " for (k=0;k<*ny1;k++) {"
3260 " printf(""y(%d,%d) : "",k,l);"
3261 " scanf(""%lu"", (unsigned long *) y+(k+l*(*ny1)));"
3268 " case 4 : /* sensor initialisation */"
3269 " /* do whatever you want to initialize the sensor */"
3271 " case 5 : /* sensor ending */"
3272 " /* do whatever you want to end the sensor */"
3282 typ=["""%lf "]; //temps
3284 typ($)=typ($)+""" \"
3286 for j=1:sum(capt(i,3)*capt(i,4))
3288 typ($)=typ($)+scs_c_n2c_fmt(capt(i,5))+" ";
3291 typ($)=typ($)+"\n"", \"
3292 typ=strsubst(typ,"%f","%lf");
3293 typ(1)=" fscanf(fprr,"+typ(1);
3295 if size(typ,1) <> 1 then
3296 typ(2:$) = bl+typ(2:$);
3298 //Code1=[' fscanf( fprr, '"'+typ+' \n'",&temps']
3299 Code1=[typ;bl+"&temps"];
3300 for i=1:size(capt,1)
3301 ni=capt(i,3)*capt(i,4); // dimension of ith input
3302 Code1($)=Code1($)+",";
3305 if capt(i,5)<>11 then
3306 Code1($)=Code1($)+...
3307 "("+scs_c_n2c_typ(capt(i,5))+" *)("+...
3308 rdnom+"_block_outtbptr+"+string(capt(i,2)-1)+")"+...
3311 Code1($)=Code1($)+...
3312 "("+scs_c_n2c_typ(capt(i,5))+" *)("+...
3313 rdnom+"_block_outtbptr+"+string(capt(i,2)-1)+")"+...
3314 "+"+string((j-1))+", "+...
3315 "("+scs_c_n2c_typ(capt(i,5))+" *)("+...
3316 rdnom+"_block_outtbptr+"+string(capt(i,2)-1)+")"+...
3317 "+"+string(ni+(j-1))+"";
3320 Code1($)=Code1($)+", ";
3324 Code1($)=Code1($)+");"
3327 if nc==1|~standalone then
3332 " if (flag1 == 0) {"
3335 " else if (flag1 == 1) {"
3336 " if (*flag == 4 && *nport == 1) {"
3337 " fprr=fopen(input,'"r'");"
3338 " if( fprr == NULL ) {"
3339 " printf('"Error opening file: %s\n'", input);"
3343 " else if (*flag == 1) {"
3346 " else if (*flag == 5 && *nport == 1) {"
3353 S=" switch (*nport) {"
3356 " case "+string(k)+" : /* Port number "+string(k)+" ----------*/"
3366 " if (flag1 == 0) {"
3369 " else if (flag1 == 1){"
3370 " if (*flag == 4 && *nport == 1) {"
3371 " fprr=fopen(input,'"r'");"
3372 " if( fprr == NULL ) {"
3373 " printf('"Error opening file: %s\n'", input);"
3377 " else if (*flag == 1) {"
3380 " else if (*flag == 5 && *nport == 1) {"
3388 //generates code of the standalone simulation procedure
3391 // rmq : La fonction zdoit n'est pas utilis?e pour le moment
3392 function make_standalone42(filename)
3395 modptr=cpr.sim.modptr;
3396 rpptr=cpr.sim.rpptr;
3397 ipptr=cpr.sim.ipptr;
3398 opptr=cpr.sim.opptr;
3403 ordptr=cpr.sim.ordptr;
3407 tevts=cpr.state.tevts;
3408 evtspt=cpr.state.evtspt;
3410 clkptr=cpr.sim.clkptr;
3411 ordptr=cpr.sim.ordptr;
3412 pointi=cpr.state.pointi;
3414 noord=size(cpr.sim.oord,1);
3415 nzord=size(cpr.sim.zord,1);
3416 niord=size(cpr.sim.iord,1);
3419 Indent2=Indent+Indent;
3423 Z=[z;zeros(lstsize(outtb),1);work]';
3429 fd = mopen(filename, "wt");
3432 "** Code prototype for standalone use"
3434 "** Copyright (c) 1989 - 2012 (INRIA)"
3435 "** Copyright (c) 2011 - 2013 (Scilab Enterprises)"
3437 "** Generated by Code_Generation toolbox of Xcos with "+getversion()
3441 "/* To learn how to use the standalone code, type '"./standalone -h'" */"
3443 "/* ---- Headers ---- */"
3444 "#include <stdio.h>"
3445 "#include <stdlib.h>"
3447 "#include <string.h>"
3448 "#include <memory.h>"
3449 "#include '"scicos_block4.h'""
3450 "#include '"machine.h'""
3452 "/* ---- Internals functions declaration ---- */"
3453 "int "+rdnom+"_sim(double, double, double, int);"
3457 if getos() == "Windows" then
3459 "#define max(a,b) ((a) >= (b) ? (a) : (b))"
3460 "#define min(a,b) ((a) <= (b) ? (a) : (b))"
3467 mputl(["/* Code prototype for standalone use */"
3468 "int C2F("+rdnom+"simblk)(double , double *, double *);"
3469 "int dset(int *n, double *dx, double *dy, int *incy);"
3476 mputl(["/* Table of constant values */"
3477 "static int nrd_"+string(0:maxtotal)'+" = "+string(0:maxtotal)'+";"], fd);
3480 mputl(["static int nrd_10 = 10;"], fd);
3483 mputl(["static int nrd_11 = 11;"], fd)
3487 mputl(["static int nrd_81 = 81;"], fd);
3490 mputl(["static int nrd_82 = 82;"], fd);
3493 mputl(["static int nrd_84 = 84;"], fd);
3495 if maxtotal<811 then
3496 mputl(["static int nrd_811 = 811;"], fd);
3498 if maxtotal<812 then
3499 mputl(["static int nrd_812 = 812;"], fd);
3501 if maxtotal<814 then
3502 mputl(["static int nrd_814 = 814;"], fd);
3506 "/* Some general static variables */"
3507 "static double zero=0;"
3508 "static double w[1];"
3509 "static int aaa=0, bbb=0;"
3510 "void **"+rdnom+"_block_outtbptr;"], fd); //** !!
3512 mputl([make_static_standalone42()], fd);
3514 mputl(["static void usage();"
3516 "/* Main program */"
3517 "int main(int argc, char *argv[])"
3519 " double tf=30;" // FIXME : Add current simulation tf.
3520 " double dt=0.1;" // FIXME : Try to figure out what is the given Simulation step.
3523 " char * progname = argv[0];"
3525 " strcpy(input,'"'");"
3526 " strcpy(output,'"'");"
3527 " while ((c = getopt(argc , argv, '"i:o:d:t:e:s:hv'")) != -1)"
3530 " strcpy(input,argv[optind-1]);"
3533 " strcpy(output,argv[optind-1]);"
3536 " strcpy(s,argv[optind-1]);"
3540 " strcpy(sf,argv[optind-1]);"
3544 " strcpy(se,argv[optind-1]);"
3548 " strcpy(ss,argv[optind-1]);"
3549 " solver=(int) strtod(ss,p);"
3553 " printf(""Options : \n'");"
3554 " printf("" -h for the help \n"");"
3555 " printf("" -v for printing the Scilab Version \n"");"
3556 " printf("" -i for input file name, by default is Terminal \n"");"
3557 " printf("" -o for output file name, by default is Terminal \n"");"
3558 " printf("" -d for the clock period, by default is 0.1 \n"");"
3559 " printf("" -t for the final time, by default is 30 \n"");"
3560 " printf("" -e for the solvers step size, by default is 0.001 \n"");"
3561 " printf("" -s integer parameter for select the numerical solver : \n"");"
3562 " printf("" 1 for Euler''s method \n"");"
3563 " printf("" 2 for Heun''s method \n"");"
3564 " printf("" 3 (default value) for the Fourth-Order Runge-Kutta"+...
3565 " (RK4) Formula \n"");"
3569 " printf(""Generated by Code_Generation toolbox of Xcos """
3570 " ""with "+getversion()+" version \n"");"
3582 " "+rdnom+"_sim(tf,dt,h,solver);"
3586 "static void usage(prog)"
3589 " fprintf(stderr, ""Usage: %s [-h] [-v] [-i arg] [-o arg] """
3590 " ""[-d arg] [-t arg] [-e arg] [-s arg]\n"", prog);"
3594 mputl(["/*"+part("-",ones(1,40))+" External simulation function */"
3595 "int "+rdnom+"_sim(tf,dt,h,solver)"
3605 " double *args[100];"
3609 mputl([" double tout;"
3614 mputl([" /* Initial values */"
3616 " /* Note that z[]=[z_initial_condition;outtbptr;work]"
3617 cformatline(" z_initial_condition={"+...
3618 strcat(string(z),",")+"};",70)
3619 cformatline(" outtbptr={"+...
3620 strcat(string(zeros(lstsize(outtb),1)),"," )+"};",70)
3621 cformatline(" work= {"+...
3622 strcat(string(work),"," )+"};",70)
3625 cformatline(" double z[]={"+strcat(string(Z),",")+"};",70)], fd);
3627 if size(z,1) <> 0 then
3628 for i=1:(length(zptr)-1)
3629 if zptr(i+1)-zptr(i)>0 then
3630 if size(corinv(i),"*")==1 then
3631 OO=scs_m.objs(corinv(i))
3634 for l=cpr.corinv(i)(1:$-1)
3635 path($+1)=l;path($+1)="model"
3639 path($+1)=cpr.corinv(i)($)
3644 if and(aaa+bbb~=["INPUTPORTEVTS";"OUTPUTPORTEVTS";"EVTGEN_f"]) then
3646 " /* Routine name of block: "+strcat(string(cpr.sim.funs(i)));
3647 " Gui name of block: "+strcat(string(OO.gui));
3648 //'/* Name block: '+strcat(string(cpr.sim.funs(i)));
3649 //'Object number in diagram: '+strcat(string(cpr.corinv(i)));
3650 " Compiled structure index: "+strcat(string(i));], fd);
3651 if stripblanks(OO.model.label)~=emptystr() then
3652 mputl([cformatline(" Label: "+strcat(string(OO.model.label)),70)], fd);
3654 if stripblanks(OO.graphics.exprs(1))~=emptystr() then
3655 mputl([cformatline(" Exprs: "+strcat(OO.graphics.exprs(1),","),70)], fd);
3657 if stripblanks(OO.graphics.id)~=emptystr() then
3658 mputl([cformatline(" Identification: "+..
3659 strcat(string(OO.graphics.id)),70)], fd);
3661 mputl([cformatline(" z={"+...
3662 strcat(string(z(zptr(i):zptr(i+1)-1)),",")+"};",70)
3669 //** declaration of oz
3672 if mat2scs_c_nb(oz(i)) <> 11 then
3674 cformatline(" "+mat2c_typ(oz(i))+...
3675 " oz_"+string(i)+"[]={"+...
3676 strcat(string(oz(i)(:)),",")+"};",70)]
3677 else //** cmplx test
3679 cformatline(" "+mat2c_typ(oz(i))+...
3680 " oz_"+string(i)+"[]={"+...
3681 strcat(string([real(oz(i)(:));
3682 imag(oz(i)(:))]),",")+"};",70)]
3686 if Code_oz <> [] then
3687 mputl([" /* oz declaration */"
3691 //** declaration of outtb
3693 for i=1:lstsize(outtb)
3694 if mat2scs_c_nb(outtb(i)) <> 11 then
3695 Code_outtb=[Code_outtb;
3696 cformatline(" "+mat2c_typ(outtb(i))+...
3697 " outtb_"+string(i)+"[]={"+...
3698 strcat(string(outtb(i)(:)),",")+"};",70)]
3699 else //** cmplx test
3700 Code_outtb=[Code_outtb;
3701 cformatline(" "+mat2c_typ(outtb(i))+...
3702 " outtb_"+string(i)+"[]={"+...
3703 strcat(string([real(outtb(i)(:));
3704 imag(outtb(i)(:))]),",")+"};",70)]
3708 if Code_outtb<>[] then
3710 " /* outtb declaration */"
3716 for i=1:lstsize(outtb)
3717 Code_outtbptr=[Code_outtbptr;
3718 " "+rdnom+"_block_outtbptr["+...
3719 string(i-1)+"] = (void *) outtb_"+string(i)+";"];
3722 //** declaration of work
3723 mputl([" /* work ptr declaration */"
3726 //## affectation of work
3727 " /* Get work ptr of blocks */"
3728 " work = (void **)(z+"+string(size(z,"*")+lstsize(outtb))+");"
3731 //## affection of outtbptr
3732 if Code_outtb<>[] then
3733 mputl([" /* Get outtbptr ptr of blocks */"
3734 " "+rdnom+"_block_outtbptr = (void **)(z+"+string(nztotal)+");"
3737 if Code_outtbptr<>[] then
3738 mputl([Code_outtbptr
3743 nx=xptr(kf+1)-xptr(kf); //** number of continuous state
3744 nin=inpptr(kf+1)-inpptr(kf); //** number of input ports
3745 nout=outptr(kf+1)-outptr(kf); //** number of output ports
3749 " "+get_comment("set_blk",list(funs(kf),funtyp(kf),kf,labels(kf)));], fd);
3751 mputl([" block_"+rdnom+"["+string(kf-1)+"].type = "+string(funtyp(kf))+";";
3752 " block_"+rdnom+"["+string(kf-1)+"].ztyp = "+string(ztyp(kf))+";";
3753 " block_"+rdnom+"["+string(kf-1)+"].ng = "+string(zcptr(kf+1)-zcptr(kf))+";";
3754 " block_"+rdnom+"["+string(kf-1)+"].nz = "+string(zptr(kf+1)-zptr(kf))+";";
3755 " block_"+rdnom+"["+string(kf-1)+"].noz = "+string(ozptr(kf+1)-ozptr(kf))+";";
3756 " block_"+rdnom+"["+string(kf-1)+"].nrpar = "+string(rpptr(kf+1)-rpptr(kf))+";";
3757 " block_"+rdnom+"["+string(kf-1)+"].nopar = "+string(opptr(kf+1)-opptr(kf))+";";
3758 " block_"+rdnom+"["+string(kf-1)+"].nipar = "+string(ipptr(kf+1)-ipptr(kf))+";"
3759 " block_"+rdnom+"["+string(kf-1)+"].nin = "+string(inpptr(kf+1)-inpptr(kf))+";";
3760 " block_"+rdnom+"["+string(kf-1)+"].nout = "+string(outptr(kf+1)-outptr(kf))+";";
3761 " block_"+rdnom+"["+string(kf-1)+"].nevout = "+string(clkptr(kf+1)-clkptr(kf))+";";
3762 " block_"+rdnom+"["+string(kf-1)+"].nmode = "+string(modptr(kf+1)-modptr(kf))+";";], fd);
3765 mputl([" block_"+rdnom+"["+string(kf-1)+"].nx = "+string(nx)+";";
3766 " block_"+rdnom+"["+string(kf-1)+"].x = &(x["+string(xptr(kf)-1)+"]);"
3767 " block_"+rdnom+"["+string(kf-1)+"].xd = &(xd["+string(xptr(kf)-1)+"]);"], fd);
3770 mputl([" if ((block_"+rdnom+"["+string(kf-1)+"].evout = "+...
3771 "calloc(block_"+rdnom+"["+string(kf-1)+"].nevout,sizeof(double)))== NULL) return 0;"], fd);
3773 //***************************** input port *****************************//
3774 //** alloc insz/inptr **//
3775 mputl([" if ((block_"+rdnom+"["+string(kf-1)+"].inptr = "+...
3776 "malloc(sizeof(double*)*block_"+rdnom+"["+string(kf-1)+"].nin))== NULL) return 0;";
3777 " if ((block_"+rdnom+"["+string(kf-1)+"].insz = "+...
3778 "malloc(3*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nin))== NULL) return 0;"], fd);
3782 lprt=inplnk(inpptr(kf)-1+k);
3783 mputl([" block_"+rdnom+"["+string(kf-1)+"].inptr["+string(k-1)+...
3784 "] = "+rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
3789 lprt=inplnk(inpptr(kf)-1+k);
3790 mputl([" block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1))+...
3791 "] = "+string(size(outtb(lprt),1))+";"], fd);
3796 lprt=inplnk(inpptr(kf)-1+k);
3797 mputl([" block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1)+nin)+...
3798 "] = "+string(size(outtb(lprt),2))+";"], fd);
3803 lprt=inplnk(inpptr(kf)-1+k);
3804 mputl([" block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1)+2*nin)+...
3805 "] = "+mat2scs_c_typ(outtb(lprt))+";"], fd);
3807 //**********************************************************************//
3809 //***************************** output port *****************************//
3810 //** alloc outsz/outptr **//
3811 mputl([" if ((block_"+rdnom+"["+string(kf-1)+"].outsz = "+...
3812 "malloc(3*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nout))== NULL) return 0;";
3813 " if ((block_"+rdnom+"["+string(kf-1)+"].outptr = "+...
3814 "malloc(sizeof(double*)*block_"+rdnom+"["+string(kf-1)+"].nout))== NULL) return 0;"], fd);
3818 lprt=outlnk(outptr(kf)-1+k);
3819 mputl([" block_"+rdnom+"["+string(kf-1)+"].outptr["+string(k-1)+...
3820 "] = "+rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
3825 lprt=outlnk(outptr(kf)-1+k);
3826 mputl([" block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1))+...
3827 "] = "+string(size(outtb(lprt),1))+";"], fd);
3832 lprt=outlnk(outptr(kf)-1+k);
3833 mputl([" block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1)+nout)+...
3834 "] = "+string(size(outtb(lprt),2))+";"], fd);
3839 lprt=outlnk(outptr(kf)-1+k);
3840 mputl([" block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1)+2*nout)+...
3841 "] = "+mat2scs_c_typ(outtb(lprt))+";"], fd);
3843 //**********************************************************************//
3845 mputl([" block_"+rdnom+"["+string(kf-1)+...
3846 "].z = &(z["+string(zptr(kf)-1)+"]);"], fd);
3848 if (part(funs(kf),1:7) ~= "capteur" &...
3849 part(funs(kf),1:10) ~= "actionneur" &...
3850 funs(kf) ~= "bidon") then
3852 if (rpptr(kf+1)-rpptr(kf)>0) then
3853 mputl([ " block_"+rdnom+"["+string(kf-1)+...
3854 "].rpar=&(RPAR1["+string(rpptr(kf)-1)+"]);"], fd);
3857 if (ipptr(kf+1)-ipptr(kf)>0) then
3858 mputl([" block_"+rdnom+"["+string(kf-1)+...
3859 "].ipar=&(IPAR1["+string(ipptr(kf)-1)+"]);"], fd);
3862 if (opptr(kf+1)-opptr(kf)>0) then
3863 mputl([" if ((block_"+rdnom+"["+string(kf-1)+"].oparptr = "+...
3864 "malloc(sizeof(void *)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL ) return 0;";
3865 " if ((block_"+rdnom+"["+string(kf-1)+"].oparsz = "+...
3866 "malloc(2*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL ) return 0;";
3867 " if ((block_"+rdnom+"["+string(kf-1)+"].opartyp = "+...
3868 "malloc(sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL ) return 0;";
3870 nopar = opptr(kf+1)-opptr(kf);
3873 mputl([" block_"+rdnom+"["+string(kf-1)+"].oparptr["+string(k-1)+...
3874 "] = (void *) OPAR_"+string(opptr(kf)-1+k)+";"], fd);
3878 mputl([" block_"+rdnom+"["+string(kf-1)+"].oparsz["+string(k-1)+...
3879 "] = "+string(size(opar(opptr(kf)-1+k),1))+";"], fd);
3883 mputl([" block_"+rdnom+"["+string(kf-1)+"].oparsz["+string(nopar+(k-1))+...
3884 "] = "+string(size(opar(opptr(kf)-1+k),2))+";"], fd);
3888 mputl([" block_"+rdnom+"["+string(kf-1)+"].opartyp["+string(k-1)+...
3889 "] = "+mat2scs_c_typ(opar(opptr(kf)-1+k))+";"], fd);
3893 if (ozptr(kf+1)-ozptr(kf)>0) then
3894 noz = ozptr(kf+1)-ozptr(kf);
3895 mputl([" if ((block_"+rdnom+"["+string(kf-1)+"].ozptr = "+...
3896 "malloc(sizeof(void *)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";
3897 " if ((block_"+rdnom+"["+string(kf-1)+"].ozsz = "+...
3898 "malloc(2*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";
3899 " if ((block_"+rdnom+"["+string(kf-1)+"].oztyp = "+...
3900 "malloc(sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";], fd);
3903 mputl([" block_"+rdnom+"["+string(kf-1)+"].ozptr["+string(k-1)+...
3904 "] = (void *) oz_"+string(ozptr(kf)-1+k)+";"], fd);
3908 mputl([" block_"+rdnom+"["+string(kf-1)+"].ozsz["+string(k-1)+...
3909 "] = "+string(size(oz(ozptr(kf)-1+k),1))+";"], fd);
3913 mputl([" block_"+rdnom+"["+string(kf-1)+"].ozsz["+string(noz+(k-1))+...
3914 "] = "+string(size(oz(ozptr(kf)-1+k),2))+";"], fd);
3918 mputl([" block_"+rdnom+"["+string(kf-1)+"].oztyp["+string(k-1)+...
3919 "] = "+mat2scs_c_typ(oz(ozptr(kf)-1+k))+";"], fd);
3923 mputl([" block_"+rdnom+"["+string(kf-1)+"].work = "+...
3924 "(void **)(((double *)work)+"+string(kf-1)+");"], fd);
3927 mputl([" if (strlen(input) > 0)"
3929 " if (strlen(output)> 0)"
3932 " /* set initial time */"
3937 mputl([" "+get_comment("flag",list(4))], fd);
3940 if or(kf==act) | or(kf==cap) then
3941 txt = call_block42(kf,0,4);
3947 txt = call_block42(kf,0,4);
3955 //** cst blocks and it's dep
3956 txt=write_code_idoit()
3960 " /* Initial blocks must be called with flag 1 */"
3964 //** begin input main loop on time
3966 " while (t < tf) {";
3971 //** find source activation number
3972 blks=find(funtyp>-1);
3976 for ev=clkptr(blk):clkptr(blk+1)-1
3977 if funs(blk)=="bidon" then
3978 if ev > clkptr(howclk) -1
3990 //** continuous time blocks must be activated
3993 txt = write_code_cdoit(flag);
3997 " "+get_comment("ev",list(0))
4003 //** blocks with input discrete event must be activated
4004 //** for flag 1, 2 and 3
4005 if size(evs,2)>=1 then
4009 txt2=write_code_doit(ev,flag);
4011 //** adjust event number because of bidon block
4012 new_ev=ev-(clkptr(howclk)-1)
4015 Indent2+[" case "+string(new_ev)+" : "+...
4016 get_comment("ev",list(new_ev))
4025 Indent+" /* Discrete activations */"
4026 Indent+" switch (nevprt) {"
4034 mputl([" "+get_comment("flag",list(flag))
4043 " /* integrate until the cumulative add of the integration"
4044 " * time step doesn''t cross the sample time step"
4046 " while (tout+h<t+dt){"
4047 " switch (solver) {"
4049 " ode1(C2F("+rdnom+"simblk),tout,h);"
4052 " ode2(C2F("+rdnom+"simblk),tout,h);"
4055 " ode4(C2F("+rdnom+"simblk),tout,h);"
4058 " ode4(C2F("+rdnom+"simblk),tout,h);"
4064 " /* integration for the remainder piece of time */"
4066 " switch (solver) {"
4068 " ode1(C2F("+rdnom+"simblk),tout,he);"
4071 " ode2(C2F("+rdnom+"simblk),tout,he);"
4074 " ode4(C2F("+rdnom+"simblk),tout,he);"
4077 " ode4(C2F("+rdnom+"simblk),tout,he);"
4082 //** fix bug provided by Roberto Bucher
4086 " /* update ptrs of continuous array */"], fd);
4088 nx=xptr(kf+1)-xptr(kf); //** number of continuous state
4090 mputl([" block_"+rdnom+"["+string(kf-1)+"].nx = "+...
4092 " block_"+rdnom+"["+string(kf-1)+"].x = "+...
4093 "&(x["+string(xptr(kf)-1)+"]);"
4094 " block_"+rdnom+"["+string(kf-1)+"].xd = "+...
4095 "&(xd["+string(xptr(kf)-1)+"]);"], fd);
4101 " /* update current time */"
4108 " "+get_comment("flag",list(5))], fd);
4111 if or(kf==act) | or(kf==cap) then
4112 txt = call_block42(kf,0,5);
4118 txt = call_block42(kf,0,5);
4130 "/*"+part("-",ones(1,40))+" Lapack messag function */";
4131 "void C2F(xerbla)(char *SRNAME,int *INFO,long int L)"
4133 " printf(""** On entry to %s, parameter number %d"""
4134 " "" had an illegal value\n"",SRNAME,*INFO);"
4137 "void set_block_error(int err)"
4142 "int get_phase_simulation()"
4147 "void * scicos_malloc(size_t size)"
4149 " return malloc(size);"
4152 "void scicos_free(void *p)"
4157 "double get_scicos_time()"
4162 "void do_cold_restart()"
4167 "void sciprint (char *fmt)"
4172 "int getopt (int argc, char *argv[], char *optstring)"
4174 " char *group, option, *sopt;"
4178 " while ( optind < argc )"
4180 " group = argv[optind];"
4181 " if ( *group != ''-'' )"
4188 " len = strlen (group);"
4189 " group = group + offset;"
4190 " if ( *group == ''-'' )"
4197 " option = *group ;"
4198 " sopt = strchr ( optstring, option ) ;"
4199 " if ( sopt != NULL )"
4202 " if ( *sopt == '':'' )"
4204 " optarg = group + 1;"
4205 " if ( *optarg == ''\0'' )"
4206 " optarg = argv[++optind];"
4207 " if ( *optarg == ''-'' )"
4209 " fprintf ( stderr, '"\n%s: illegal option -- %c'","
4210 " argv[0], option );"
4221 " if ( offset >= len )"
4230 " fprintf ( stderr, '"\n%s: illegal option -- %c'", argv[0], option );"
4235 " return ( option );"
4240 mputl(["int C2F("+rdnom+"simblk)(t, x, xd)"
4242 " double t, *x, *xd;"
4246 " * compute state derivative of the continuous part"
4247 " * !calling sequence"
4248 " * neq : integer the size of the continuous state"
4249 " * t : current time"
4250 " * x : double precision vector whose contains the continuous state"
4251 " * xd : double precision vector, contain the computed derivative"
4259 " double *args[100];"
4260 " dset(&neq, &c_b14,xd , &c__1);"
4262 " "+get_comment("update_xd",list())], fd);
4265 if (xptr(kf+1)-xptr(kf)) > 0 then
4266 mputl([" block_"+rdnom+"["+string(kf-1)+"].x="+...
4267 "&(x["+string(xptr(kf)-1)+"]);"
4268 " block_"+rdnom+"["+string(kf-1)+"].xd="+...
4269 "&(xd["+string(xptr(kf)-1)+"]);"], fd);
4280 "int dset(int *n, double *dx, double *dy, int *incy)"
4283 " int i = 0, iy = 0;"
4285 " if (*n <= 0) return 0;"
4289 " iy = (-(*n) + 1) * *incy + 1;"
4292 " for (i = 1; i <= iMax; ++i)"
4300 "/* Euler''s Method */"
4310 " for (i=0;i<neq;i++) {"
4311 " x[i]=x[i]+h*xd[i];"
4317 "/* Heun''s Method */"
4323 " double y["+string(nX)+"],yh["+string(nX)+"],temp,f0["+string(nX)+"],th;"
4326 " memcpy(y,x,neq*sizeof(double));"
4327 " memcpy(f0,xd,neq*sizeof(double));"
4333 " for (i=0;i<neq;i++) {"
4334 " x[i]=y[i]+h*f0[i];"
4337 " for (i=0;i<neq;i++) {"
4338 " yh[i]=y[i]+h*f0[i];"
4344 " for (i=0;i<neq;i++) {"
4345 " x[i]=y[i]+temp*(f0[i]+xd[i]);"
4351 "/* Fourth-Order Runge-Kutta (RK4) Formula */"
4357 " double y["+string(nX)+"],yh["+string(nX)+"],"+...
4358 "temp,f0["+string(nX)+"],th,th2,"+...
4359 "f1["+string(nX)+"],f2["+string(nX)+"];"
4362 " memcpy(y,x,neq*sizeof(double));"
4363 " memcpy(f0,xd,neq*sizeof(double));"
4369 " for (i=0;i<neq;i++) {"
4370 " x[i]=y[i]+h*f0[i];"
4373 " for (i=0;i<neq;i++) {"
4374 " yh[i]=y[i]+(h/2)*f0[i];"
4376 " (*f)(th2,yh, f1);"
4380 " for (i=0;i<neq;i++) {"
4381 " x[i]=y[i]+temp*f1[i];"
4383 " for (i=0;i<neq;i++) {"
4384 " yh[i]=y[i]+(h/2)*f1[i];"
4386 " (*f)(th2,yh, f2);"
4389 " for (i=0;i<neq;i++) {"
4390 " x[i]=y[i]+h*f2[i];"
4393 " for (i=0;i<neq;i++) {"
4394 " yh[i]=y[i]+h*f2[i];"
4396 " (*f)(th2,yh, xd);"
4400 " for (i=0;i<neq;i++) {"
4401 " x[i]=y[i]+temp*(f0[i]+2.0*f1[i]+2.0*f2[i]+xd[i]);"
4411 //generates static table definitions
4413 //Author : Rachid Djenidi, Alan Layec
4414 function txt=make_static_standalone42()
4415 oldFormat = format();
4419 //*** Continuous state ***//
4422 "/* def continuous state */"
4423 cformatline("double x[]={"+strcat(string(x),",")+"};",70)
4424 cformatline("double xd[]={"+strcat(string(x),",")+"};",70)
4425 "static int c__1 = 1;"
4426 "static double c_b14 = 0.;"
4427 "static int neq="+string(nX)+";"
4430 //************************//
4433 "char input[50], output[50],s[1],sf[1],se[1],ss[1],**p;"
4434 "static double sci_time;"
4435 "static int errflg = 0;"
4436 "static char *optarg = NULL ;"
4437 "static int optind = 1, offset = 0 ;"
4438 "scicos_block block_"+rdnom+"["+string(nblk)+"];"
4441 //*** Real parameters ***//
4442 if size(rpar,1) <> 0 then
4444 "/* def real parameters */"
4445 "static double RPAR1[ ] = {"];
4447 for i=1:(length(rpptr)-1)
4448 if rpptr(i+1)-rpptr(i)>0 then
4450 if size(corinv(i),"*")==1 then
4451 OO=scs_m.objs(corinv(i));
4454 for l=cpr.corinv(i)(1:$-1)
4460 path($+1)=cpr.corinv(i)($);
4464 //** Add comments **//
4465 txt($+1)="/* Routine name of block: "+strcat(string(cpr.sim.funs(i)));
4466 txt($+1)=" * Gui name of block: "+strcat(string(OO.gui));
4467 txt($+1)=" * Compiled structure index: "+strcat(string(i));
4469 if stripblanks(OO.model.label)~=emptystr() then
4470 txt=[txt;cformatline(" * Label: "+strcat(string(OO.model.label)),70)];
4472 if stripblanks(OO.graphics.exprs(1))~=emptystr() then
4473 txt=[txt;cformatline(" * Exprs: "+strcat(OO.graphics.exprs(1),","),70)];
4475 if stripblanks(OO.graphics.id)~=emptystr() then
4477 cformatline(" * Identification: "+strcat(string(OO.graphics.id)),70)];
4479 txt=[txt;" * rpar="];
4481 //******************//
4484 cformatline(strcat(msprintf("%.25E,\n",rpar(rpptr(i):rpptr(i+1)-1))),70);
4494 txt($+1)="static double RPAR1[1];";
4496 //***********************//
4498 //*** Integer parameters ***//
4499 if size(ipar,1) <> 0 then
4501 "/* def integer parameters */"
4502 "static int IPAR1[ ] = {"];
4504 for i=1:(length(ipptr)-1)
4505 if ipptr(i+1)-ipptr(i)>0 then
4506 if size(corinv(i),"*")==1 then
4507 OO=scs_m.objs(corinv(i));
4510 for l=cpr.corinv(i)(1:$-1)
4516 path($+1)=cpr.corinv(i)($);
4520 //** Add comments **//
4521 txt($+1)="/* Routine name of block: "+strcat(string(cpr.sim.funs(i)));
4522 txt($+1)=" * Gui name of block: "+strcat(string(OO.gui));
4523 txt($+1)=" * Compiled structure index: "+strcat(string(i));
4524 if stripblanks(OO.model.label)~=emptystr() then
4525 txt=[txt;cformatline(" * Label: "+strcat(string(OO.model.label)),70)];
4528 if stripblanks(OO.graphics.exprs(1))~=emptystr() then
4530 cformatline(" * Exprs: "+strcat(OO.graphics.exprs(1),","),70)];
4532 if stripblanks(OO.graphics.id)~=emptystr() then
4534 cformatline(" * Identification: "+strcat(string(OO.graphics.id)),70)];
4537 cformatline(" * ipar= {"+strcat(string(ipar(ipptr(i):ipptr(i+1)-1)),",")+"};",70)];
4539 //******************//
4541 txt=[txt;cformatline(strcat(string(ipar(ipptr(i):ipptr(i+1)-1))+","),70)];
4549 txt($+1)="static int IPAR1[1];";
4551 //**************************//
4553 //Alan added opar (27/06/07)
4554 //*** Object parameters ***//
4555 if lstsize(opar)<>0 then
4557 "/* def object parameters */"]
4558 for i=1:(length(opptr)-1)
4559 if opptr(i+1)-opptr(i)>0 then
4561 if size(corinv(i),"*")==1 then
4562 OO=scs_m.objs(corinv(i));
4565 for l=cpr.corinv(i)(1:$-1)
4571 path($+1)=cpr.corinv(i)($);
4575 //** Add comments **//
4577 txt($+1)="/* Routine name of block: "+strcat(string(cpr.sim.funs(i)));
4578 txt($+1)=" * Gui name of block: "+strcat(string(OO.gui));
4579 txt($+1)=" * Compiled structure index: "+strcat(string(i));
4580 if stripblanks(OO.model.label)~=emptystr() then
4581 txt=[txt;cformatline(" * Label: "+strcat(string(OO.model.label)),70)];
4583 if stripblanks(OO.graphics.id)~=emptystr() then
4585 cformatline(" * Identification: "+strcat(string(OO.graphics.id)),70)];
4588 //******************//
4591 for j=1:opptr(i+1)-opptr(i)
4593 cformatline("static "+mat2c_typ(opar(opptr(i)+j-1)) +...
4594 " OPAR_"+string(opptr(i)+j-1) + "[] = {"+...
4595 strcat(string(opar(opptr(i)+j-1)),",")+"};",70)]
4600 //*************************//
4604 format(oldFormat(2), oldFormat(1));
4607 //utilitary fonction used to format long C instruction
4608 //t : a string containing a C instruction
4609 //l : max line length allowed
4611 //Author : Rachid Djenidi
4612 function t1=cformatline(t ,l)
4613 // Force format to avoid precision loss
4614 oldFormat = format();
4623 if kw(1)==1 then // there is leading blanks
4624 k1=find(kw(2:$)-kw(1:$-1)<>1)
4625 if k1==[] then // there is a single blank
4632 t=part(t,nw+1:length(t));
4633 bl=part(" ",ones(1,nw))
4636 if length(t)<=l then
4645 k($+1)=length(t)+1 // positions of the commas
4646 i=find(k(1:$-1)<=l&k(2:$)>l) //nearest left comma (reltively to l)
4650 t1=[t1;bl+part(t,1:k(i))]
4651 t=part(t,k(i)+1:length(t))
4658 format(oldFormat(2), oldFormat(1));
4662 // Generate Matrix of all binary code from
4665 function vec = binaryTable(n)
4671 accu = [accu ; zeros(2^(i-1),1) ; ones(2^(i-1),1)]
4675 vec=vec(2:$, :); // Remove first line [ 0 --- 0 ]
4678 //used in do_compile_superblock
4679 function vec=codebinaire(v,szclkIN)
4680 vec=zeros(1,szclkIN)
4688 function t=filetype(m)
4690 filetypes=["Directory","Character device","Block device",...
4691 "Regular file","FIFO","Symbolic link","Socket"]
4692 bits=[16384,8192,24576,32768,4096,40960,49152]
4693 m=int32(m)&int32(61440)
4694 t=filetypes(find(m==int32(bits)))
4697 //get_blank : return blanks with a length
4698 // of the given input string
4700 //input : str : a string
4702 //output : txt : blanks
4704 //16/06/07 Author : A.Layec
4705 function [txt] = get_blank(str)
4712 // get_comment : return a C comment
4713 // for generated code
4715 //input : typ : a string
4718 //output : a C comment
4720 //16/06/07 Author : A.Layec
4721 function [txt]=get_comment(typ,param)
4728 txt = "/* Continuous state computation */"
4730 txt = "/* Output computation */"
4732 txt = "/* Discrete state computation */"
4734 txt = "/* Output Event computation */"
4736 txt = "/* Initialization */"
4738 txt = "/* Ending */"
4740 txt = "/* Update zero crossing surfaces */"
4742 //** blocks activated on event number
4744 txt = "/* Blocks activated on the event number "+string(param(1))+" */"
4746 //** blk calling sequence
4747 case "call_blk" then
4748 txt = ["/* Call of ''"+param(1) + ...
4749 "'' (type "+string(param(2))+" - blk nb "+...
4750 string(param(3))+" - uid "+param(4)];
4751 if ztyp(param(3)) then
4752 txt=txt+" - with zcross) */";
4756 //** proto calling sequence
4757 case "proto_blk" then
4758 txt = ["/* prototype of ''"+param(1) + ...
4759 "'' (type "+string(param(2))];
4760 if ztyp(param(3)) then
4761 txt=txt+" - with zcross) */";
4765 //** ifthenelse calling sequence
4766 case "ifthenelse_blk" then
4767 txt = ["/* Call of ''if-then-else'' blk (blk nb "+...
4768 string(param(1))+") */"]
4769 //** eventselect calling sequence
4770 case "evtselect_blk" then
4771 txt = ["/* Call of ''event-select'' blk (blk nb "+...
4772 string(param(1))+") */"]
4773 //** set block structure
4775 txt = ["/* set blk struc. of ''"+param(1) + ...
4776 "'' (type "+string(param(2))+" - blk nb "+...
4777 string(param(3))++" - uid "+param(4)+") */"];
4778 //** Update xd vector ptr
4779 case "update_xd" then
4780 txt = ["/* Update xd vector ptr */"];
4781 //** Update g vector ptr
4782 case "update_g" then
4783 txt = ["/* Update g vector ptr */"];
4789 //mat2c_typ : matrix to C type
4790 //sci2c_ttyp : get the C string of a scicos type
4792 //input : outtb : a matrix
4794 //output : txt : the string of the C scicos type
4795 // of the data of outtb
4797 //16/06/07 Author : A.Layec
4798 function [txt]=mat2c_typ(outtb)
4802 if isreal(outtb) then
4809 select typeof(outtb)
4817 txt = "unsigned long"
4819 txt = "unsigned short"
4821 txt = "unsigned char"
4828 //mat2scs_c_nb matrix to scicos C number (sci2sci_n)
4830 //input : outtb : a matrix
4832 //output : c_nb : the scicos C number
4834 //16/06/07 Author : A.Layec
4835 function [c_nb]=mat2scs_c_nb(outtb)
4839 if isreal(outtb) then
4846 select typeof(outtb)
4865 //mat2scs_c_ptr matrix to scicos C ptr (sci2c_typ)
4867 //input : outtb : a matrix
4869 //output : txt : the string of the C scicos type
4870 // of the data of outtb
4872 //16/06/07 Author : A.Layec
4873 function [txt]=mat2scs_c_ptr(outtb)
4877 if isreal(outtb) then
4880 txt = "SCSCOMPLEX_COP"
4884 select typeof(outtb)
4886 txt = "SCSINT32_COP"
4888 txt = "SCSINT16_COP"
4892 txt = "SCSUINT32_COP"
4894 txt = "SCSUINT16_COP"
4896 txt = "SCSUINT8_COP"
4903 //mat2scs_c_typ matrix to scicos C type
4905 //input : outtb : a matrix
4907 //output : txt : the string of the C scicos type
4908 // of the data of outtb
4910 //16/06/07 Author : A.Layec
4911 function [txt]=mat2scs_c_typ(outtb)
4915 if isreal(outtb) then
4918 txt = "SCSCOMPLEX_N"
4922 select typeof(outtb)
4941 //scs_c_n2c_fmt : scicos C number to C format
4943 //input : c_nb : a C scicos type
4945 //output : txt : the string of the C format string
4946 // of the data of outtb
4948 //16/06/07 Author : A.Layec
4949 function [txt]=scs_c_n2c_fmt(c_nb)
4981 //scs_c_n2c_typ scicos C number to C type
4983 //input : c_nb : a C scicos number
4985 //output : txt : the string of the C format string
4986 // of the data of outtb
4988 //16/06/07 Author : A.Layec
4989 function [txt]=scs_c_n2c_typ(c_nb)
5008 txt = "unsigned char";
5011 txt = "unsigned short";
5014 txt = "unsigned long";
5021 //scs_c_nb2scs_nb : scicos C number to scicos number
5023 //input : c_nb : the scicos C number type
5025 //output : scs_nb : the scilab number type
5027 //16/06/07 Author : A.Layec
5028 function [scs_nb]=scs_c_nb2scs_nb(c_nb)
5029 scs_nb=zeros(size(c_nb,1),size(c_nb,2));
5030 for i=1:size(c_nb,1)
5031 for j=1:size(c_nb,2)
5056 //used in do_compile_superblock
5057 function XX=update_block(XX)
5058 execstr("o="+rdnom+"_c(''define'')")
5061 XX.graphics.gr_i=o.graphics.gr_i
5064 //write_code_cdoit : generate body of the code for
5065 // for all time dependant blocks
5067 //input : flag : flag number for block's call
5069 //output : txt for cord blocks
5071 //12/07/07 Alan Layec
5072 function [txt]=write_code_cdoit(flag)
5079 if funtyp(bk)>-1 then
5080 if or(bk==act) | or(bk==cap) then
5082 txt2=call_block42(bk,pt,flag);
5090 txt2=call_block42(bk,pt,flag);
5098 elseif funtyp(bk)==-1 then
5099 ix=-1+inplnk(inpptr(bk));
5100 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5101 thentxt=write_code_doit(clkptr(bk),flag);
5102 elsetxt=write_code_doit(clkptr(bk)+1,flag);
5103 if thentxt<>[] | elsetxt<>[] then
5105 " "+get_comment("ifthenelse_blk",list(bk));]
5107 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5109 " if("+tmp_+">0) {"]
5127 //** eventselect blk
5128 elseif funtyp(bk)==-2 then
5129 Noutport=clkptr(bk+1)-clkptr(bk);
5130 ix=-1+inplnk(inpptr(bk));
5131 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5135 switchtxt(i)=write_code_doit(clkptr(bk)+i-1,flag);
5136 if switchtxt(i)<>[] then II=[II i];end
5140 " "+get_comment("evtselect_blk",list(bk));]
5142 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5144 " i=max(min((int) "+...
5145 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"
5152 " case "+string(i)+" :";]
5155 BigIndent+write_code_doit(clkptr(bk)+i-1,flag);]
5168 error("Unknown block type "+string(bk));
5174 //write_code_doit : generate body of the code for
5175 // ordering calls of blocks during
5176 // flag 1,2 & flag 3
5178 //input : ev : evt number for block's call
5179 // flag : flag number for block's call
5181 //output : txt for flag 1 or 2, or flag 3
5183 //12/07/07 Alan Layec
5184 function [txt]=write_code_doit(ev,flag)
5187 for j=ordptr(ev):ordptr(ev+1)-1
5191 if funtyp(bk)>-1 then
5192 if or(bk==act) | or(bk==cap) then
5194 txt2=call_block42(bk,pt,flag);
5202 txt2=call_block42(bk,pt,flag);
5210 elseif funtyp(bk)==-1 then
5211 ix=-1+inplnk(inpptr(bk));
5212 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5213 thentxt=write_code_doit(clkptr(bk),flag);
5214 elsetxt=write_code_doit(clkptr(bk)+1,flag);
5215 if thentxt<>[] | elsetxt<>[] then
5217 " "+get_comment("ifthenelse_blk",list(bk));]
5219 tmp_ = "*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5221 " if("+tmp_+">0) {"]
5239 //** eventselect blk
5240 elseif funtyp(bk)==-2 then
5241 Noutport=clkptr(bk+1)-clkptr(bk);
5242 ix=-1+inplnk(inpptr(bk));
5243 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5247 switchtxt(i)=write_code_doit(clkptr(bk)+i-1,flag);
5248 if switchtxt(i)<>[] then II=[II i];end
5252 " "+get_comment("evtselect_blk",list(bk));]
5253 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5256 " i=max(min((int) "+...
5257 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"
5264 " case "+string(i)+" :";]
5267 BigIndent+write_code_doit(clkptr(bk)+i-1,flag);]
5280 error("Unknown block type "+string(bk));
5286 //write_code_idoit : generate body of the code for
5287 // ordering calls of initial
5290 //input : nothing (blocks are called with flag 1)
5292 //output : txt for iord
5294 //15/07/07 Alan Layec
5295 function [txt]=write_code_idoit()
5302 if funtyp(bk)>-1 then
5305 txt2=call_block42(bk,pt,1);
5313 txt2=call_block42(bk,pt,1);
5321 elseif funtyp(bk)==-1 then
5322 ix=-1+inplnk(inpptr(bk));
5323 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5324 thentxt=write_code_doit(clkptr(bk),1);
5325 elsetxt=write_code_doit(clkptr(bk)+1,1);
5326 if thentxt<>[] | elsetxt<>[] then
5328 " "+get_comment("ifthenelse_blk",list(bk));]
5330 tmp_ = "*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5332 " if("+tmp_+">0) {"]
5350 //** eventselect blk
5351 elseif funtyp(bk)==-2 then
5352 Noutport=clkptr(bk+1)-clkptr(bk);
5353 ix=-1+inplnk(inpptr(bk));
5354 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5358 switchtxt(i)=write_code_doit(clkptr(bk)+i-1,1);
5359 if switchtxt(i)<>[] then II=[II i];end
5363 " "+get_comment("evtselect_blk",list(bk));]
5365 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5367 " i=max(min((int) "+...
5368 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"]
5376 " case "+string(i)+" :";]
5379 BigIndent+write_code_doit(clkptr(bk)+i-1,1);]
5392 error("Unknown block type "+string(bk));
5398 //write_code_odoit : generate body of the code for
5399 // ordering calls of blocks before
5400 // continuous time integration
5402 //input : flag : flag number for block's call
5404 //output : txt for flag 0
5406 //12/07/07 Alan Layec
5407 function [txt]=write_code_odoit(flag)
5414 if funtyp(bk)>-1 then
5415 txt2=call_block42(bk,pt,flag);
5422 elseif funtyp(bk)==-1 then
5423 ix=-1+inplnk(inpptr(bk));
5424 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5425 thentxt=write_code_ozdoit(clkptr(bk),flag);
5426 elsetxt=write_code_ozdoit(clkptr(bk)+1,flag);
5427 if thentxt<>[] | elsetxt<>[] then
5429 " "+get_comment("ifthenelse_blk",list(bk));]
5431 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5433 " if ((block_"+rdnom+"["+string(bk-1)+"].nmode<0"+...
5434 " && "+tmp_+">0)"+...
5436 " (block_"+rdnom+"["+string(bk-1)+"].nmode>0"+...
5437 " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==1)) {"]
5448 " else if ((block_"+rdnom+"["+string(bk-1)+"].nmode<0"+...
5449 " && "+tmp_+"<=0)"+...
5451 " (block_"+rdnom+"["+string(bk-1)+"].nmode>0"+...
5452 " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==2)) {";]
5462 //** eventselect blk
5463 elseif funtyp(bk)==-2 then
5464 Noutport=clkptr(bk+1)-clkptr(bk);
5465 ix=-1+inplnk(inpptr(bk));
5466 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5470 switchtxt(i)=write_code_ozdoit(clkptr(bk)+i-1,flag);
5471 if switchtxt(i)<>[] then II=[II i];end
5475 " "+get_comment("evtselect_blk",list(bk));]
5477 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5479 " if (block_"+rdnom+"["+string(bk-1)+"].nmode<0) {";
5480 " i=max(min((int) "+...
5481 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"
5484 " i=block_"+rdnom+"["+string(bk-1)+"].mode[0];"
5493 " case "+string(i)+" :";]
5496 BigIndent+write_code_ozdoit(clkptr(bk)+i-1,flag);]
5509 error("Unknown block type "+string(bk));
5515 //write_code_ozdoit : generate body of the code for both
5518 //input: ev : evt number for block's call
5519 // flag : flag number for block's call
5521 //output : txt for flag 0 or flag 9
5523 //12/07/07 Alan Layec
5524 function [txt]=write_code_ozdoit(ev,flag)
5527 for j=ordptr(ev):ordptr(ev+1)-1
5531 if funtyp(bk)>-1 then
5532 if (or(bk==act) | or(bk==cap)) & (flag==1) then
5535 " "+call_block42(bk,pt,flag)
5539 txt2=call_block42(bk,pt,flag);
5547 elseif funtyp(bk)==-1 then
5548 ix=-1+inplnk(inpptr(bk));
5549 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5550 thentxt=write_code_ozdoit(clkptr(bk),flag);
5551 elsetxt=write_code_ozdoit(clkptr(bk)+1,flag);
5552 if thentxt<>[] | elsetxt<>[] then
5554 " "+get_comment("ifthenelse_blk",list(bk));]
5556 tmp_ = "*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5558 " if (((phase==1"+...
5559 " || block_"+rdnom+"["+string(bk-1)+"].nmode==0)"+...
5560 " && "+tmp_+">0)"+...
5563 " && block_"+rdnom+"["+string(bk-1)+"].nmode!=0)"+...
5564 " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==1)) {"]
5575 " else if (((phase==1"+...
5576 " || block_"+rdnom+"["+string(bk-1)+"].nmode==0)"+...
5577 " && "+tmp_+"<=0)"+...
5580 " && block_"+rdnom+"["+string(bk-1)+"].nmode!=0)"+...
5581 " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==2)) {";]
5591 //** eventselect blk
5592 elseif funtyp(bk)==-2 then
5593 Noutport=clkptr(bk+1)-clkptr(bk);
5594 ix=-1+inplnk(inpptr(bk));
5595 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5599 switchtxt(i)=write_code_ozdoit(clkptr(bk)+i-1,flag);
5600 if switchtxt(i)<>[] then II=[II i];end
5604 " "+get_comment("evtselect_blk",list(bk));]
5606 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5608 " if (phase==1 || block_"+rdnom+"["+string(bk-1)+"].nmode==0) {";
5609 " i=max(min((int) "+...
5610 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"
5613 " i=block_"+rdnom+"["+string(bk-1)+"].mode[0];"
5622 " case "+string(i)+" :";]
5625 BigIndent+write_code_ozdoit(clkptr(bk)+i-1,flag);]
5638 error("Unknown block type "+string(bk));
5644 //write_code_zdoit : generate body of the code for
5645 // ordering calls of blocks before
5646 // continuous time zero crossing
5651 //output : txt for flag 9
5653 //12/07/07 Alan Layec
5654 function [txt]=write_code_zdoit()
5657 //** first pass (flag 1)
5662 if funtyp(bk)>-1 then
5663 if or(bk==act) | or(bk==cap) then
5666 " "+call_block42(bk,pt,1)
5670 txt2=call_block42(bk,pt,1);
5678 elseif funtyp(bk)==-1 then
5679 ix=-1+inplnk(inpptr(bk));
5680 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5681 thentxt=write_code_ozdoit(clkptr(bk),1);
5682 elsetxt=write_code_ozdoit(clkptr(bk)+1,1);
5683 if thentxt<>[] | elsetxt<>[] then
5685 " "+get_comment("ifthenelse_blk",list(bk));]
5687 tmp_ = "*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5689 " if (((phase==1"+...
5690 " || block_"+rdnom+"["+string(bk-1)+"].nmode==0)"+...
5691 " && "+tmp_+">0)"+...
5694 " && block_"+rdnom+"["+string(bk-1)+"].nmode!=0)"+...
5695 " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==1)) {"]
5706 " else if (((phase==1"+...
5707 " || block_"+rdnom+"["+string(bk-1)+"].nmode==0)"+...
5708 " && "+tmp_+"<=0)"+...
5711 " && block_"+rdnom+"["+string(bk-1)+"].nmode!=0)"+...
5712 " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==2)) {";]
5722 //** eventselect blk
5723 elseif funtyp(bk)==-2 then
5724 Noutport=clkptr(bk+1)-clkptr(bk);
5725 ix=-1+inplnk(inpptr(bk));
5726 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5730 switchtxt(i)=write_code_ozdoit(clkptr(bk)+i-1,1);
5731 if switchtxt(i)<>[] then II=[II i];end
5735 " "+get_comment("evtselect_blk",list(bk));]
5737 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5739 " if (phase==1 || block_"+rdnom+"["+string(bk-1)+"].nmode==0){";
5740 " i=max(min((int) "+...
5741 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"
5743 " i=block_"+rdnom+"["+string(bk-1)+"].mode[0];"
5752 " case "+string(i)+" :";]
5755 BigIndent+write_code_ozdoit(clkptr(bk)+i-1,1);]
5768 error("Unknown block type "+string(bk));
5772 //** second pass (flag 9)
5777 if funtyp(bk)>-1 then
5778 if or(bk==act) | or(bk==cap) then
5781 " "+call_block42(bk,pt,9)
5785 txt2=call_block42(bk,pt,9);
5794 elseif funtyp(bk)==-1 then
5795 ix=-1+inplnk(inpptr(bk));
5796 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5798 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5800 thentxt=write_code_zzdoit(clkptr(bk),9);
5801 elsetxt=write_code_zzdoit(clkptr(bk)+1,9);
5803 " "+get_comment("ifthenelse_blk",list(bk));]
5806 " g["+string(zcptr(bk)-1)+"]=(double)"+tmp_+";"]
5808 if thentxt<>[] | elsetxt<>[] then
5811 " if (g["+string(zcptr(bk)-1)+"] > 0.){"]
5819 if elsetxt <> [] then
5834 " if(phase==1 && block_"+rdnom+"["+string(bk-1)+"].nmode > 0){"
5835 " if (g["+string(zcptr(bk)-1)+"] > 0.){"
5836 " block_"+rdnom+"["+string(bk-1)+"].mode[0] = 1;"
5839 " block_"+rdnom+"["+string(bk-1)+"].mode[0] = 2;"
5843 //** eventselect blk
5844 elseif funtyp(bk)==-2 then
5845 Noutport=clkptr(bk+1)-clkptr(bk);
5846 ix=-1+inplnk(inpptr(bk));
5847 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5849 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5854 switchtxt(i)=write_code_zzdoit(clkptr(bk)+i-1,9);
5855 if switchtxt(i)<>[] then II=[II i];end
5858 " "+get_comment("evtselect_blk",list(bk));]
5862 " j=max(min((int) "+...
5863 tmp_+",block_"+rdnom+"["+string(bk-1)+"].nevout),1);"]
5871 " case "+string(j)+" :";]
5874 BigIndent+write_code_zzdoit(clkptr(bk)+i-1,9);]
5887 " for (jj=0;jj<block_"+rdnom+"["+string(fun-1)+"].nevout-1;++jj) {"
5888 " g["+string(zcptr(bk)-1)+"+jj]=(double)"+tmp_+"-(double)(jj+2);"
5890 " if(phase==1 && block_"+rdnom+"["+string(bk-1)+"].nmode>0){"
5891 " j=max(min((int) "+tmp_+","
5892 " block_"+rdnom+"["+string(bk-1)+"].nevout),1);"
5893 " block_"+rdnom+"["+string(bk-1)+"].mode[0]= j;"
5898 error("Unknown block type "+string(bk));
5904 //write_code_zzdoit : generate body of the code for
5907 //input: ev : evt number for block's call
5908 // flag : flag number for block's call
5910 //output : txt for flag 9
5912 //12/07/07 Alan Layec
5913 function [txt]=write_code_zzdoit(ev,flag)
5916 for j=ordptr(ev):ordptr(ev+1)-1
5920 if funtyp(bk)>-1 then
5921 if or(bk==act) | or(bk==cap) then
5924 " "+call_block42(bk,pt,flag)
5928 txt2=call_block42(bk,pt,flag);
5936 elseif funtyp(bk)==-1 then
5937 ix=-1+inplnk(inpptr(bk));
5938 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5940 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5942 thentxt=write_code_zzdoit(clkptr(bk),9);
5943 elsetxt=write_code_zzdoit(clkptr(bk)+1,9);
5945 " "+get_comment("ifthenelse_blk",list(bk));]
5948 " g["+string(zcptr(bk)-1)+"]=(double)"+tmp_+";"]
5950 if thentxt<>[] | elsetxt<>[] then
5953 " if (g["+string(zcptr(bk)-1)+"] > 0.){"]
5961 if elsetxt <> [] then
5976 " if(phase==1 && block_"+rdnom+"["+string(bk-1)+"].nmode > 0){"
5977 " if (g["+string(zcptr(bk)-1)+"] > 0.){"
5978 " block_"+rdnom+"["+string(bk-1)+"].mode[0] = 1;"
5981 " block_"+rdnom+"["+string(bk-1)+"].mode[0] = 2;"
5986 //** eventselect blk
5987 elseif funtyp(bk)==-2 then
5988 Noutport=clkptr(bk+1)-clkptr(bk);
5989 ix=-1+inplnk(inpptr(bk));
5990 TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5992 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5997 switchtxt(i)=write_code_zzdoit(clkptr(bk)+i-1,9);
5998 if switchtxt(i)<>[] then II=[II i];end
6001 " "+get_comment("evtselect_blk",list(bk));]
6005 " j=max(min((int) "+...
6006 tmp_+",block_"+rdnom+"["+string(bk-1)+"].nevout),1);"]
6014 " case "+string(j)+" :";]