91d45b35acda1b1fffcf3d2dd749bfcddc981bcd
[scilab.git] / scilab / modules / scicos / macros / scicos_scicos / do_compile_superblock42.sci
1 //  Scicos
2 //
3 //  Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
4 //                      - Alan Layec <alan.layec@inria.fr>
5 //                      - Ramine Nikoukhah <ramine.nikoukhah@inria.fr>
6 //                      - Rachid Djenidi
7 //
8 //                      - Scilab 5 update by Simone Mannori
9 //
10 //  Copyright (C) DIGITEO - 2010 - Allan CORNET
11 //  Copyright (C) Scilab Enterprises - 2012 - Bruno JOFRET
12 //
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.
17 //
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.
22 //
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.
26 //
27 // See the file ../license.txt
28 //
29
30 //**---------------------------------------------------------------------------------------------------------------------------------
31 //
32 //16/06/07 Author : ?, A. Layec
33 //
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
37
38     if funtyp(i)==0 then
39         if nin==0 then
40             uk    = 0;
41             nuk_1 = 0;
42             nuk_2 = 0;
43             uk_t  = 1;
44             //Code($+1)=+'  args[0]=(double *)outtbptr[0]);';
45         else
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)+'];';
51         end
52     end
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]
56
57     Code($+1)="block_"+rdnom+"["+string(i-1)+"].nevprt=nevprt;"
58
59     Code=["/* Call of actuator (blk nb "+string(i)+") */"
60     Code;
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);"];
65
66     proto="void "+rdnom+"_actuator("+..
67     "int *, int *, int *, double *, void *, int *, int *,int *,int);"
68     proto=cformatline(proto,70);
69 endfunction
70
71 //CallBlock : generate C syntax
72 //            of a scicos block
73 //
74 //inputs : bk   : bloc index
75 //         pt   : evt activation number
76 //         flag : flag
77 //
78 //output : txt  :
79 //
80 //16/06/07 Authors : Alan Layec
81 function txt=call_block42(bk,pt,flag)
82     txt=[]
83     //**
84     if flag==2 & ((zptr(bk+1)-zptr(bk))+..
85         (ozptr(bk+1)-ozptr(bk))+..
86         (xptr(bk+1)-xptr(bk)+..
87         with_work(bk))==0 |..
88         pt<=0) & ~(stalone & or(bk==actt(:,1))) then
89         return // block without state or continuously activated
90     end
91     if flag==0 & ((xptr(bk+1)-xptr(bk))==0) then
92         return // block without continuous state
93     end
94     if flag==9 & ((zcptr(bk+1)-zcptr(bk))==0) then
95         return // block without continuous state
96     end
97     if flag==3 & ((clkptr(bk+1)-clkptr(bk))==0) then
98         return
99     end
100
101     //** adjust pt
102     if ~(flag==3 & ((zcptr(bk+1)-zcptr(bk))<>0)) then
103         pt=abs(pt)
104     end
105
106     //** add comment
107     txt=[txt;
108     get_comment("call_blk",list(funs(bk),funtyp(bk),bk,labels(bk)));]
109
110     //** set nevprt and flag for called block
111     txt=[txt;
112     "block_"+rdnom+"["+string(bk-1)+"].nevprt = "+string(pt)+";"
113     "local_flag = "+string(flag)+";"]
114
115     //**see if its bidon, actuator or sensor
116     if funs(bk)=="bidon" then
117         txt=[];
118         return
119     elseif funs(bk)=="bidon2" then
120         txt=[];
121         return
122     elseif or(bk==actt(:,1)) then
123         ind=find(bk==actt(:,1))
124         uk=actt(ind,2)
125         nuk_1=actt(ind,3)
126         nuk_2=actt(ind,4)
127         uk_t=actt(ind,5)
128         txt = [txt;
129         "nport = "+string(ind)+";"]
130         txt = [txt;
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);"]
134         txt = [txt;
135         "if(local_flag < 0) return(5 - local_flag);"]
136         return
137     elseif or(bk==capt(:,1)) then
138         ind=find(bk==capt(:,1))
139         yk=capt(ind,2);
140         nyk_1=capt(ind,3);
141         nyk_2=capt(ind,4);
142         yk_t=capt(ind,5);
143         txt = [txt;
144         "nport = "+string(ind)+";"]
145         txt = [txt;
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);"]
149         txt = [txt;
150         "if(local_flag < 0) return(5 - local_flag);"]
151         return
152     end
153
154     //**
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 */
161
162     //**
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
171
172     //**
173     ftyp=funtyp(bk)
174     if ftyp>2000 then ftyp=ftyp-2000,end
175     if ftyp>1000 then ftyp=ftyp-1000,end
176
177     //** check function type
178     if ftyp < 0 then //** ifthenelse eselect blocks
179         txt = [];
180         return;
181     else
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.")
184             txt = [];
185             return;
186         end
187     end
188
189     select ftyp
190
191     case 0 then
192         //**** input/output addresses definition ****//
193         if nin>1 then
194             for k=1:nin
195                 uk=inplnk(inpptr(bk)-1+k);
196                 nuk=size(outtb(uk),"*");
197                 txt=[txt;
198                 "rdouttb["+string(k-1)+"]=(double *)"+rdnom+"_block_outtbptr["+string(uk-1)+"];"]
199             end
200             txt=[txt;
201             "args[0]=&(rdouttb[0]);"]
202         elseif nin==0
203             uk=0;
204             nuk=0;
205             txt=[txt;
206             "args[0]=(double *)"+rdnom+"_block_outtbptr[0];"]
207         else
208             uk=inplnk(inpptr(bk));
209             nuk=size(outtb(uk),"*");
210             txt=[txt;
211             "args[0]=(double *)"+rdnom+"_block_outtbptr["+string(uk-1)+"];"]
212         end
213
214         if nout>1 then
215             for k=1:nout
216                 yk=outlnk(outptr(bk)-1+k);
217                 nyk=size(outtb(yk),"*");
218                 txt=[txt;
219                 "rdouttb["+string(k+nin-1)+"]=(double *)"+rdnom+"_block_outtbptr["+string(yk-1)+"];"];
220             end
221             txt=[txt;
222             "args[1]=&(rdouttb["+string(nin)+"]);"];
223         elseif nout==0
224             yk=0;
225             nyk=0;
226             txt=[txt;
227             "args[1]=(double *)"+rdnom+"_block_outtbptr[0];"];
228         else
229             yk=outlnk(outptr(bk));
230             nyk=size(outtb(yk),"*"),;
231             txt=[txt;
232             "args[1]=(double *)"+rdnom+"_block_outtbptr["+string(yk-1)+"];"];
233         end
234         //*******************************************//
235
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)+") ");
249         end
250         txtc(2:$) = blank + txtc(2:$);
251         txt = [txt;txtc];
252         //*******************************************//
253
254
255         //**
256     case 1 then
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)+") ");
269         end
270         if nin>=1 | nout>=1 then
271             txtc($)=txtc($)+", \"
272             txtc=[txtc;""]
273             if nin>=1 then
274                 for k=1:nin
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)+",";
278                 end
279                 txtc($)=part(txtc($),1:length(txtc($))-1); //remove last ,
280             end
281             if nout>=1 then
282                 if nin>=1 then
283                     txtc($)=txtc($)+", \"
284                     txtc=[txtc;""]
285                 end
286                 for k=1:nout
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)+",";
290                 end
291                 txtc($)=part(txtc($),1:length(txtc($))-1); //remove last ,
292             end
293         end
294
295         if ztyp(bk) then
296             txtc($)=txtc($)+", \"
297             txtc=[txtc;
298             "block_"+rdnom+"["+string(bk-1)+"].g,&block_"+rdnom+"["+string(bk-1)+"].ng);"]
299         else
300             txtc($)=txtc($)+");";
301         end
302
303         txtc(2:$) = blank + txtc(2:$);
304         txt = [txt;txtc];
305         //*******************************************//
306
307         //**
308     case 2 then
309
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"];
318         if ~ztyp(bk) then
319             txtc($)=txtc($)+");";
320         else
321             txtc($)=txtc($)+", \";
322             txtc=[txtc;
323             "block_"+rdnom+"["+string(bk-1)+"].g,&block_"+rdnom+"["+string(bk-1)+"].ng);"]
324         end
325         blank = get_blank(funs(bk)+"( ");
326         txtc(2:$) = blank + txtc(2:$);
327         txt = [txt;txtc];
328         //*******************************************//
329
330         //**
331     case 4 then
332         txt=[txt;
333         funs(bk)+"(&block_"+rdnom+"["+string(bk-1)+"],local_flag);"];
334
335     end
336
337     txt =[txt;"if(local_flag < 0) return(5 - local_flag);"]
338
339 endfunction
340
341 //CallBlock : generate C syntax
342 //            of a scicos block
343 //
344 //inputs : bk   : bloc index
345 //         pt   :
346 //         flag :block_'+rdnom+'[
347 //
348 //output : txt  :
349 //
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
355     //   end
356
357     //**
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 */
364
365     //**
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
374
375     //**
376     ftyp=funtyp(bk)
377     if ftyp>2000 then ftyp=ftyp-2000,end
378     if ftyp>1000 then ftyp=ftyp-1000,end
379
380     //** check function type
381     if ftyp < 0 then //** ifthenelse eselect blocks
382         txt = [];
383         return;
384     else
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.")
387             txt = [];
388             return;
389         end
390     end
391
392     //** add comment
393     txt=[get_comment("call_blk",list(funs(bk),funtyp(bk),bk,labels(bk)));]
394
395     //** write nevprt activation
396     //   nclock=abs(pt);
397     txt=[txt;
398     "block_"+rdnom+"["+string(bk-1)+"].nevprt=nevprt;"]
399
400     select ftyp
401         //** zero funtyp
402     case 0 then
403         //**** input/output addresses definition ****//
404         if nin>1 then
405             for k=1:nin
406                 uk=inplnk(inpptr(bk)-1+k);
407                 nuk=size(outtb(uk),"*");
408                 TYPE=mat2scs_c_ptr(outtb(uk));//scilab index start from 1
409                 txt=[txt;
410                 "rdouttb["+string(k-1)+"]=("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(uk-1)+"];"]
411             end
412             txt=[txt;
413             "args[0]=&(rdouttb[0]);"]
414         elseif nin==0
415             uk=0;
416             nuk=0;
417             txt=[txt;
418             "args[0]=(SCSREAL_COP *)"+rdnom+"_block_outtbptr[0];"]
419         else
420             uk=inplnk(inpptr(bk));
421             nuk=size(outtb(uk),"*");
422             TYPE=mat2scs_c_ptr(outtb(uk));//scilab index start from 1
423             txt=[txt;
424             "args[0]=("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(uk-1)+"];"]
425         end
426
427         if nout>1 then
428             for k=1:nout
429                 yk=outlnk(outptr(bk)-1+k);
430                 nyk=size(outtb(yk),"*");
431                 TYPE=mat2scs_c_ptr(outtb(yk));//scilab index start from 1
432                 txt=[txt;
433                 "rdouttb["+string(k+nin-1)+"]=("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(yk-1)+"];"];
434             end
435             txt=[txt;
436             "args[1]=&(rdouttb["+string(nin)+"]);"];
437         elseif nout==0
438             yk=0;
439             nyk=0;
440             txt=[txt;
441             "args[1]=(SCSREAL_COP *)"+rdnom+"_block_outtbptr[0];"];
442         else
443             yk=outlnk(outptr(bk));
444             nyk=size(outtb(yk),"*"),;
445             TYPE=mat2scs_c_ptr(outtb(yk));//scilab index start from 1
446             txt=[txt;
447             "args[1]=("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(yk-1)+"];"];
448         end
449         //*******************************************//
450
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)+") ");
464         end
465         txtc(2:$) = blank + txtc(2:$);
466         txt = [txt;txtc];
467         //*******************************************//
468
469
470         //**
471     case 1 then
472         //**** input/output addresses definition ****//
473         //       if nin>=1 then
474         //         for k=1:nin
475         //           uk=inplnk(inpptr(i)-1+k);
476         //           nuk=size(outtb(uk),'*');
477         //         end
478         //       end
479         //       if nout>=1 then
480         //         for k=1:nout
481         //           yk=outlnk(outptr(i)-1+k);
482         //           nyk=size(outtb(yk),'*');
483         //         end
484         //       end
485         //*******************************************//
486
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)+") ");
499         end
500         if nin>=1 | nout>=1 then
501             txtc($)=txtc($)+", \"
502             txtc=[txtc;""]
503             if nin>=1 then
504                 for k=1:nin
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)+",";
508                 end
509                 txtc($)=part(txtc($),1:length(txtc($))-1); //remove last ,
510             end
511             if nout>=1 then
512                 if nin>=1 then
513                     txtc($)=txtc($)+", \"
514                     txtc=[txtc;""]
515                 end
516                 for k=1:nout
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)+",";
520                 end
521                 txtc($)=part(txtc($),1:length(txtc($))-1); //remove last ,
522             end
523         end
524
525         if ztyp(bk) then
526             txtc($)=txtc($)+", \"
527             txtc=[txtc;"w,&nrd_0);"];
528         else
529             txtc($)=txtc($)+");";
530         end
531
532         txtc(2:$) = blank + txtc(2:$);
533         txt = [txt;txtc];
534         //*******************************************//
535
536         //**
537     case 2 then
538
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"];
547         if ~ztyp(bk) then
548             txtc($)=txtc($)+");";
549         else
550             txtc($)=txtc($)+", \";
551             txtc=[txtc;
552             "block_"+rdnom+"["+string(bk-1)+"].g,&block_"+rdnom+"["+string(bk-1)+"].ng);"]
553         end
554         blank = get_blank(funs(bk)+"( ");
555         txtc(2:$) = blank + txtc(2:$);
556         txt = [txt;txtc];
557         //*******************************************//
558
559         //**
560     case 4 then
561         txt=[txt;
562         funs(bk)+"(&block_"+rdnom+"["+string(bk-1)+"],flag);"];
563
564     end
565 endfunction
566
567 //
568 //16/06/07 Author : ?, A. Layec
569 //
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 */
573
574     //declaration des I/O des blocs de type 1
575     if funtyp(i)==0 then
576         if nout==0 then
577             yk    = 0;
578             nyk_1 = 0;
579             nyk_2 = 0;
580             yk_t  = 1;
581             //Code($+1)=+'  args[1]=(double *)(outtbptr[0]);';
582         else
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)+']);';
588         end
589
590     end
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)+") */"
594     Code;
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);
602 endfunction
603
604 //Generates Code for dynamically linked Fortran and C Blocks
605 function [CCode,FCode]=gen_blocks()
606     CCode=[]
607     FCode=[]
608
609     kdyn=find(funtyp>1000) //dynamically linked blocs
610     //100X : Fortran blocks
611     //200X : C blocks
612
613     if (size(kdyn,"*") >1)
614         kfuns=[];
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)));
618         else
619             path=list("objs");
620             for l=corinv(kdyn(1))(1:$-1)
621                 path($+1)=l;
622                 path($+1)="model";
623                 path($+1)="rpar";
624                 path($+1)="objs";
625             end
626             path($+1)=corinv(kdyn(1))($);
627             O=scs_m(path);
628         end
629         if funtyp(kdyn(1))>2000 then
630             //C block
631             CCode=[CCode;O.graphics.exprs(2)]
632         else
633             FCode=[FCode;O.graphics.exprs(2)]
634         end
635         kfuns=funs(kdyn(1));
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)));
640             else
641                 path=list("objs");
642                 for l=corinv(kdyn(i))(1:$-1)
643                     path($+1)=l;
644                     path($+1)="model";
645                     path($+1)="rpar";
646                     path($+1)="objs";
647                 end
648                 path($+1)=corinv(kdyn(i))($);
649                 O=scs_m(path);
650             end
651             if (find(kfuns==funs(kdyn(i))) == [])
652                 kfuns=[kfuns;funs(kdyn(i))];
653                 if funtyp(kdyn(i))>2000  then
654                     //C block
655                     CCode=[CCode;O.graphics.exprs(2)]
656                 else
657                     FCode=[FCode;O.graphics.exprs(2)]
658                 end
659             end
660         end
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));
665         else
666             path=list("objs");
667             for l=corinv(kdyn)(1:$-1)
668                 path($+1)=l;
669                 path($+1)="model";
670                 path($+1)="rpar";
671                 path($+1)="objs";
672             end
673             path($+1)=corinv(kdyn)($);
674             O=scs_m(path);
675         end
676         if funtyp(kdyn)>2000 then
677             //C block
678             CCode=[CCode;O.graphics.exprs(2)]
679         else
680             FCode=[FCode;O.graphics.exprs(2)]
681         end
682     end
683     if CCode==[]
684         CCode=["void no_ccode()"
685         "{"
686         "  return;"
687         "}"]
688     end
689 endfunction
690
691 //** Generates the C code for new block simulation
692 //
693 //12/07/07 Alan Layec
694 function ok=gen_ccode42();
695
696     //** Generate code for scicos block
697
698     ierr=execstr("make_computational42(rpat+''/''+rdnom+''.c'')","errcatch")
699     if ierr<>0 then
700         message(lasterror())
701         ok=%f
702         return
703     end
704
705     //** Generate files for dynamically linked scicos blocks
706     [CCode,FCode]=gen_blocks()
707     if FCode<>[] then
708         ierr=execstr("mputl(FCode,rpat+''/''+rdnom+''f.f'')","errcatch")
709         if ierr<>0 then
710             message(lasterror())
711             ok=%f
712             return
713         end
714     end
715     if CCode<>[] then
716         ierr=execstr("mputl(CCode,rpat+''/''+rdnom+''_Cblocks.c'')","errcatch")
717         if ierr<>0 then
718             message(lasterror())
719             ok=%f
720             return
721         end
722     end
723
724     //** Generate _void_io.c
725     try
726         fd = mopen(rpat+"/"+rdnom+"_void_io.c", "wt")
727
728         mputl(["/*";
729         "**    Generated by Code_Generation toolbox of Xcos with "+getversion();
730         "**    Date : "+date();
731         "*/"], fd);
732
733         if (readGlobal <> [] | writeGlobal <> [])
734             mputl([""
735             "#include ""scicos_block4.h"""], fd);
736         end
737
738         mputl([""
739         "/*---------------------------------------- Actuators */"
740         "void "+rdnom+"_actuator(int *flag, int *nport, int *nevprt, double *t, void *u, int *nu1, int *nu2, int *ut, int *flag1)"
741         "{"
742         "  int k = 0,l = 0;"
743         "}"
744         "/*---------------------------------------- Sensor */"
745         "void "+rdnom+"_sensor(int *flag, int *nport, int *nevprt, double *t, void *y, int *ny1, int *ny2, int *yt, int *flag1)"
746         "{"
747         "  int k = 0,l = 0;"
748         "}"], fd)
749
750         // Generate global data read <=> FromWorkspace.
751         if readGlobal <> []
752             for i = readGlobal
753                 mputl(["/*---------------------------------------- Read variable "+i+" */"
754                 "void readGlobal_"+i+"(scicos_block *block, int flag)"
755                 "{"
756                 "    // Read a scilab Environment variable "+i
757                 "    fromws_c(block, flag);"
758                 "}"
759                 ], fd);
760             end
761         end
762
763         // Generate global data write <=> ToWorkspace.
764         if writeGlobal <> []
765             for i = writeGlobal
766                 mputl(["/*---------------------------------------- Write variable "+i+" */"
767                 "void writeGlobal_"+i+"(scicos_block *block, int flag)"
768                 "{"
769                 "    // Write a scilab environment variable "+i
770                 "    // to a C one : global"+i
771                 "    tows_c(block, flag);"
772                 "}"
773                 ], fd);
774             end
775         end
776
777         mclose(fd);
778     catch
779         message(lasterror())
780         ok=%f
781         return
782     end
783
784     //** Generate _standalone.c
785
786     ierr=execstr("make_standalone42(rpat+''/''+rdnom+''_standalone.c'')","errcatch")
787     if ierr<>0 then
788         message(lasterror())
789         ok=%f
790         return
791     end
792
793     //** This copy is indispensable only for stand alone code generation
794     //** not supported in Scilab 5
795
796     //** copy source code of machine.h/scicos_block4.h
797     //   in target path
798     //   txt=mgetl(SCI+'/modules/core/includes/machine.h');
799     //   ierr=execstr('mputl(txt,rpat+''/machine.h'')','errcatch')
800     //   if ierr<>0 then
801     //     message(lasterror())
802     //     ok=%f
803     //     return
804     //   end
805     //
806     //   txt=mgetl(SCI+'/modules/scicos_blocks/includes/scicos_block4.h');
807     //   ierr=execstr('mputl(txt,rpat+''/scicos_block4.h'')','errcatch')
808     //   if ierr<>0 then
809     //     message(lasterror())
810     //     ok=%f
811     //     return
812     //   end
813
814     //** Generate _act_sens_events.c
815     Code=["/*"
816     "**    Generated by Code_Generation toolbox of Xcos with "+getversion()
817     "**    Date :"+date()
818     "*/"
819     ""
820     "#include <stdio.h>"
821     "#include <stdlib.h>"
822     "#include <math.h>"
823     "#include <string.h>"
824     "extern void **"+rdnom+"_block_outtbptr;"
825     "extern char input[50],output[50];"
826     "FILE *fprr = NULL, *fprw = NULL;"
827     make_outevents()
828     make_actuator(%t)
829     make_sensor(%t)]
830
831     created=[];
832     reponse=[];
833
834     created=fileinfo(rpat+"/"+rdnom+"_act_sens_events.c")
835     if created~=[] then
836         reponse=messagebox(["File: """+rdnom+"_act_sens_events.c"" already exists,";
837         "do you want to replace it ?"],"modal","question",["Yes","No"]);
838     end
839
840     if reponse==1 |  reponse==[] then
841         ierr=execstr("mputl(Code,rpat+''/''+rdnom+''_act_sens_events.c'')", ...
842         "errcatch")
843         if ierr<>0 then
844             message(lasterror())
845             ok=%f
846             return
847         end
848     end
849
850     try
851         if (readGlobal <> [] | writeGlobal <> [])
852             fd = mopen(rpat+"/"+rdnom+"_read_write_global.c", "wt")
853             mputl(["/*"
854             "**    Generated by Code_Generation toolbox of Xcos with "+getversion()
855             "**    Date :"+date()
856             "*/"
857             ""
858             "// Uncomment this to log each To Workspace variable update"
859             "//#define VERBOSE"
860             ""
861             "#ifdef VERBOSE"
862             "#include <stdio.h>"
863             "#endif"
864             "#include <stdlib.h>"
865             "#include <string.h>"
866             "#include ""scicos_block4.h"""
867             ""
868             "double get_scicos_time();"
869             ""
870             ], fd);
871             if writeGlobal <> []
872                 // Generate global C variables
873                 // #define global_XXX_Size
874                 // static double **global_XXX
875                 // static int global_XXX_Index (to manage buffer like variables)
876                 for i = 1:size(writeGlobal, "*")
877                     mputl(["/*---------------------------------------- Global Variable "+writeGlobal(i)+" */"
878                     "#define global_"+writeGlobal(i)+"_Size    "+string(writeGlobalSize(i))
879                     "static double **global_"+writeGlobal(i)+";"
880                     "static int global_"+writeGlobal(i)+"_Index = 0;"
881                     ], fd);
882                 end
883             end
884
885             if readGlobal <> []
886                 // Generate global C variables
887                 // #define global_XXX_Size
888                 // #define global_XXX_Time_Size
889                 // static double **global_XXX
890                 // static double global_XXX_Time (manage time <-> values association)
891                 for i = 1:size(readGlobal, "*")
892                     mputl(["/*---------------------------------------- Global Variable "+readGlobal(i)+" */"
893                     "#define global_"+readGlobal(i)+"_Size    "+string(readGlobalSize(i))
894                     "#define global_"+readGlobal(i)+"_Time_Size    "+string(readGlobalTimeSize(i))
895                     "static double **global_"+readGlobal(i)+";"
896                     "static double *global_"+readGlobal(i)+"_Time = 0;"
897                     ], fd);
898                 end
899             end
900
901
902
903             if (readGlobal <> [])
904                 for i = 1:size(readGlobal, "*")
905                     mputl(["/*---------------------------------------- Read variable "+readGlobal(i)+" */"
906                     "void readGlobal_"+readGlobal(i)+"(scicos_block *block, int flag)"
907                     "{"
908                     "    int i = 0;"
909                     "    int j = 0;"
910                     "    double *y_d = NULL;"
911                     ""
912                     "    /* Read a scilab Environment variable "+readGlobal(i)+" */"
913                     "    switch(flag)"
914                     "    {"
915                     ], fd)
916                     //
917                     // Init case : allocate data / timetable
918                     // Feal data/timetable with values from Scilab.
919                     //
920                     mputl(["    case 4 : /* init */"
921                     "    {"
922                     "        // Allocate time table"
923                     "        global_"+readGlobal(i)+"_Time = (double *) malloc(global_"+readGlobal(i)+"_Time_Size * sizeof(double));"
924                     "        memset(global_"+readGlobal(i)+"_Time, 0x0, global_"+readGlobal(i)+"_Time_Size * sizeof(double));"
925                     ""
926                     "        // Allocate data"
927                     "        global_"+readGlobal(i)+" = (double **) malloc(global_"+readGlobal(i)+"_Time_Size * sizeof(double *));"
928                     "        for (i = 0 ; i < global_"+readGlobal(i)+"_Time_Size ; ++i)"
929                     "        {"
930                     "            global_"+readGlobal(i)+"[i] = (double *) malloc( global_"+readGlobal(i)+"_Size * sizeof(double));"
931                     "            memset(global_"+readGlobal(i)+"[i], 0x0, global_"+readGlobal(i)+"_Size * sizeof(double));"
932                     "        }"
933                     ""
934                     ], fd);
935                     // Save format
936                     oldFormat = format();
937                     format(25);
938
939                     // Fead timetable
940                     for (j = 1:readGlobalTimeSize(i))
941                         mputl(["        global_"+readGlobal(i)+"_Time["+string(j-1)+"] = " + ...
942                         strsubst(string(evstr(readGlobal(i)+".time("+string(j)+")")), "D", "E")+";"
943                         ], fd)
944                     end
945                     // Fead data
946                     for (j = 1:readGlobalTimeSize(i))
947                         for (k = 1:readGlobalSize(i))
948                             mputl(["        global_"+readGlobal(i)+"["+string(j-1)+"]["+string(k-1)+"] = " + ...
949                             strsubst(string(evstr(readGlobal(i)+".values("+string(j)+", "+string(k)+")")), "D", "E")+";"
950                             ], fd)
951
952                         end
953                     end
954
955                     // Restore format
956                     format(oldFormat(2), oldFormat(1));
957
958                     mputl([""
959                     "        break;"
960                     "    }"
961                     ],fd);
962                     //
963                     // Output update case :
964                     //
965                     mputl(["    case 1 : /* output update */"
966                     "    {"
967                     "        j = 0;"
968                     "        while(get_scicos_time() >  global_"+readGlobal(i)+"_Time[j] && j < global_"+readGlobal(i)+"_Time_Size)"
969                     "        {"
970                     "            j++;"
971                     "        }"
972                     "        y_d = GetRealOutPortPtrs(block,1);"
973                     "        if (j == global_"+readGlobal(i)+"_Time_Size)"
974                     "        {"
975                     "            memset(y_d, 0x0, global_"+readGlobal(i)+"_Size * sizeof(double));"
976                     "        }"
977                     "        else"
978                     "        {"
979                     "            memcpy(y_d, global_"+readGlobal(i)+"[j], global_"+readGlobal(i)+"_Size * sizeof(double));"
980                     "        }"
981                     "        break;"
982                     "    }"
983                     ],fd);
984                     //
985                     // End, free memory.
986                     //
987                     mputl(["    case 5 : /* END */"
988                     "    {"
989                     "        for (i = 0 ; i < global_"+readGlobal(i)+"_Time_Size ; ++i)"
990                     "        {"
991                     "            free(global_"+readGlobal(i)+"[i]);"
992                     "        }"
993                     "        free(global_"+readGlobal(i)+");"
994                     "        break;"
995                     "    }"
996                     ],fd);
997                     // Close switch + function declaration
998                     mputl(["    }"
999                     "}"
1000                     ], fd);
1001                 end
1002             end
1003
1004             if writeGlobal <> []
1005                 for i = 1:size(writeGlobal, "*")
1006                     mputl(["/*---------------------------------------- Write variable "+writeGlobal(i)+" */"
1007                     "void writeGlobal_"+writeGlobal(i)+"(scicos_block *block, int flag)"
1008                     "{"
1009                     "    int i = 0;"
1010                     "    int j = 0;"
1011                     "    int nu = block->insz[0];"
1012                     "    int nu2 = block->insz[1];"
1013                     ""
1014                     "    /* Write a scilab environment variable "+writeGlobal(i)+" */"
1015                     "    /* to a C one : global_"+writeGlobal(i)+" */"
1016                     "    switch(flag)"
1017                     "    {"
1018                     "    case 4 : /* init */"
1019                     "    {"
1020                     "        global_"+writeGlobal(i)+" = (double **) malloc(global_"+writeGlobal(i)+"_Size * sizeof(double *));"
1021                     "        for (i = 0 ; i < global_"+writeGlobal(i)+"_Size ; ++i)"
1022                     "        {"
1023                     "            global_"+writeGlobal(i)+"[i] = (double *) malloc(nu * nu2 * sizeof(double));"
1024                     "            memset(global_"+writeGlobal(i)+"[i], 0x0, nu * nu2 * sizeof(double));"
1025                     "        }"
1026                     "        global_"+writeGlobal(i)+"_Index = -1;"
1027                     "#ifdef VERBOSE"
1028                     "        printf(""C_"+writeGlobal(i)+".values = zeros(%d, %d, %d)\n"", global_"+writeGlobal(i)+"_Size, nu, nu2);"
1029                     "        printf(""C_"+writeGlobal(i)+".time = -%%inf * ones(%d, 1)\n"", global_"+writeGlobal(i)+"_Size);"
1030                     "#endif"
1031                     "        break;"
1032                     "    }"
1033                     "    case 2 : /* state update */"
1034                     "    {"
1035                     "        global_"+writeGlobal(i)+"_Index = (global_"+writeGlobal(i)+"_Index + 1) % global_"+writeGlobal(i)+"_Size;"
1036                     "        memcpy(global_"+writeGlobal(i)+"[global_"+writeGlobal(i)+"_Index], block->inptr[i], nu * nu2 * sizeof(double));"
1037                     "#ifdef VERBOSE"
1038                     "        printf(""C_"+writeGlobal(i)+".time(%d) = %.25E;\n"", global_"+writeGlobal(i)+"_Index + 1, get_scicos_time());"
1039                     "        for (j = 0 ; j < nu * nu2 ; ++j)"
1040                     "        {"
1041                     "            printf(""C_"+writeGlobal(i)+".values(%d, %d) = %.25E;\n"", global_"+writeGlobal(i)+"_Index + 1, j + 1, global_"+writeGlobal(i)+"[global_"+writeGlobal(i)+"_Index][j]);"
1042                     "        }"
1043                     "#endif /* !VERBOSE */"
1044                     "        break;"
1045                     "    }"
1046                     "    case 5 : /* end */"
1047                     "    {"
1048                     "#ifdef VERBOSE"
1049                     "        //reshape to fit scilab behaviour"
1050                     "        printf(""C_"+writeGlobal(i)+".time = C_"+writeGlobal(i)+".time(C_"+writeGlobal(i)+".time <> -%%inf);\n"");"
1051                     "        printf(""C_"+writeGlobal(i)+".values = C_"+writeGlobal(i)+".values(1:size(C_"+writeGlobal(i)+".time, ''*''), :);\n"");"
1052                     "#endif"
1053                     "        for (i = 0 ; i < global_"+writeGlobal(i)+"_Size ; ++i)"
1054                     "        {"
1055                     "            free(global_"+writeGlobal(i)+"[i]);"
1056                     "        }"
1057                     "        free(global_"+writeGlobal(i)+");"
1058                     "        break;"
1059                     "    }"
1060                     "    }"
1061                     "}"
1062                     ], fd);
1063                 end
1064             end
1065
1066             mclose(fd);
1067         end
1068     catch
1069         message(lasterror())
1070         ok=%f
1071         return
1072     end
1073
1074 endfunction
1075
1076 //creates the Scicos GUI function associated with the new block
1077 //
1078 //Author : Rachid Djenidi, A.Layec
1079 //
1080 //20/06/07, A.Layec : update with in2,out2,intyp,outtyp
1081 //27/06/07, A.Layec : update opar,oz
1082 //
1083 function ok=gen_gui42();
1084     clkinput=ones(clkIN)';
1085     clkoutput=ones(clkOUT)';
1086     for i=1:length(bllst)
1087         deput=[%t,%f]
1088         if (bllst(i).dep_ut(2) == %t) then
1089             deput(1,2)=%t;
1090             break;
1091         end
1092     end
1093     Code=["function [sciblk, s1, s2] = "+rdnom+"_c(job,sciblk,e1)"
1094     "// Interfacing function  of generated Xcos block "+rdnom
1095     "//    Generated by the Xcos Code_Generation tool with "+getversion();
1096     "//    Date : "+date();
1097     "//"
1098     "  s1= []; s2 = [];"
1099     "  select job";
1100     "  case ""set"" then";
1101     "  case ""define"" then"
1102     "    "+sci2exp(capt(:,3),"in",70); //input ports sizes 1
1103     "    "+sci2exp(capt(:,4),"in2",70); //input ports sizes 2
1104     "    "+sci2exp(scs_c_nb2scs_nb(capt(:,5)),"intyp",70); //input ports type
1105     "    "+sci2exp(actt(:,3),"out",70); //output ports sizes 1
1106     "    "+sci2exp(actt(:,4),"out2",70); //output ports sizes 2
1107     "    "+sci2exp(scs_c_nb2scs_nb(actt(:,5)),"outtyp",70); //output ports type
1108     "    "+sci2exp(cpr.state.x,"x",70); //initial continuous state
1109     "    "+sci2exp(cpr.state.z,"z",70); //initial discrete state
1110     "    work=zeros("+string(nblk)+",1)";
1111     "    Z=[z;work]";
1112     "    "+sci2exp(lstcat(cpr.state.oz,cpr.state.outtb),"odstate",70);
1113     "    "+sci2exp(cpr.sim.rpar,"rpar",70); //real parameters
1114     "    "+sci2exp(cpr.sim.ipar,"ipar",70); //integer parameters
1115     "    "+sci2exp(cpr.sim.opar,"opar",70); //object parameters
1116     "    "+sci2exp(clkinput,"clkinput",70);
1117     "    "+sci2exp(clkoutput,"clkoutput",70);
1118     "    "+sci2exp(FIRING,"firing",70);
1119     "    nzcross="+string(sum(cpr.sim.zcptr(2:$)-cpr.sim.zcptr(1:$-1)))';
1120     "    nmode="+string(sum(cpr.sim.modptr(2:$)-cpr.sim.modptr(1:$-1)))';
1121     "    "+sci2exp(deput,"dep_ut",70);
1122     "    model=scicos_model(sim=list(''"+rdnom+"'',4),...";
1123     "                       in=in,..."
1124     "                       in2=in2,..."
1125     "                       intyp=intyp,..."
1126     "                       out=out,..."
1127     "                       out2=out2,..."
1128     "                       outtyp=outtyp,..."
1129     "                       evtin=clkinput,..."
1130     "                       evtout=clkoutput,..."
1131     "                       firing=firing,..."
1132     "                       state=x,..."
1133     "                       dstate=Z,..."
1134     "                       odstate=odstate,..."
1135     "                       rpar=rpar,..."
1136     "                       ipar=ipar,..."
1137     "                       opar=opar,..."
1138     "                       blocktype=''c'',..."
1139     "                       dep_ut=dep_ut,..."
1140     "                       nzcross=nzcross,..."
1141     "                       nmode=nmode)";
1142     "    gr_i=""xstringb(orig(1),orig(2),"""""+rdnom+""""",sz(1),sz(2),""""fill"""")""";
1143     "    sciblk = standard_define([2 2],model,[],gr_i);";
1144     "    style_properties = [""blockWithLabel"";";
1145     "                        ""verticalLabelPosition=middle"";";
1146     "                        ""verticalAlign=middle"";";
1147     "                        ""displayedLabel="+rdnom+"""];";
1148     "    sciblk.graphics.style = strcat(style_properties,"";"");";
1149     "  end"
1150     "endfunction"];
1151     //Create file
1152     ierr=execstr("mputl(Code,rpat+""/""+rdnom+""_c.sci"")","errcatch")
1153     if ierr<>0 then
1154         message(lasterror())
1155         ok=%f
1156     end
1157 endfunction
1158
1159 function  [ok,XX,alreadyran,flgcdgen,szclkINTemp,freof] = do_compile_superblock42(XX,all_scs_m,numk,alreadyran)
1160     // Transforms a given Scicos discrete and continuous SuperBlock into a C defined Block
1161
1162     scs_m=XX.model.rpar
1163     par=scs_m.props;
1164     if alreadyran then
1165         //terminate current simulation
1166         do_terminate()
1167         alreadyran=%f
1168     end
1169     hname=scs_m.props.title(1) //superblock name
1170     //***********************************************************
1171     //Check blocks properties and adapt them if necessary
1172     //***********************************************************
1173
1174     IN=[];
1175     OUT=[];
1176     clkIN=[];
1177     clkOUT=[];
1178     numa=0;
1179     numc=0;
1180     writeGlobal = [];
1181     writeGlobalSize = [];
1182     readGlobal = [];
1183     readGlobalTimeSize = [];
1184     readGlobalSize = [];
1185
1186     for i=1:size(scs_m.objs)
1187         if typeof(scs_m.objs(i))=="Block" then
1188             if scs_m.objs(i).gui=="CLKOUT_f" then
1189                 ok=%f;%cpr=list();
1190                 message("Superblock should not have any activation output port.")
1191                 return
1192             elseif scs_m.objs(i).gui=="IN_f" then
1193                 //replace input ports by sensor blocks
1194                 numc=numc+1
1195                 scs_m.objs(i).gui="INPUTPORTEVTS";
1196                 scs_m.objs(i).model.evtin=1
1197                 scs_m.objs(i).model.sim(1)="capteur"+string(numc)
1198                 IN=[IN scs_m.objs(i).model.ipar]
1199             elseif scs_m.objs(i).gui=="OUT_f" then
1200                 //replace output ports by actuator blocks
1201                 numa=numa+1
1202                 scs_m.objs(i).gui="OUTPUTPORTEVTS";
1203                 scs_m.objs(i).model.sim(1)="actionneur"+string(numa)
1204                 OUT=[OUT  scs_m.objs(i).model.ipar]
1205             elseif scs_m.objs(i).gui=="CLKINV_f" then
1206                 //replace event input ports by  fictious block
1207                 scs_m.objs(i).gui="EVTGEN_f";
1208                 scs_m.objs(i).model.sim(1)="bidon"
1209                 clkIN=[clkIN scs_m.objs(i).model.ipar];
1210                 //elseif scs_m.objs(i).model.dep_ut(2)==%t then
1211                 //check for time dependency PAS IICI
1212                 //ok=%f;%cpr=list()
1213                 //message('a block have time dependence.')
1214                 //return
1215             elseif scs_m.objs(i).gui=="CLKOUTV_f" then
1216                 scs_m.objs(i).gui="EVTOUT_f";
1217                 scs_m.objs(i).model.sim(1)="bidon2"
1218                 clkOUT=[clkOUT scs_m.objs(i).model.ipar];
1219             end
1220         end
1221     end
1222
1223     //Check if input/output ports are numered properly
1224     IN=-gsort(-IN);
1225     if or(IN<>[1:size(IN,"*")]) then
1226         ok=%f;%cpr=list()
1227         message("Input ports are not numbered properly.")
1228         return
1229     end
1230     OUT=-gsort(-OUT);
1231     if or(OUT<>[1:size(OUT,"*")]) then
1232         ok=%f;%cpr=list()
1233         message("Output ports are not numbered properly.")
1234         return
1235     end
1236     clkIN=-gsort(-clkIN);
1237     if or(clkIN<>[1:size(clkIN,"*")]) then
1238         ok=%f;%cpr=list()
1239         message("Event input ports are not numbered properly.")
1240         return
1241     end
1242     clkOUT=-gsort(-clkOUT);
1243     if or(clkOUT<>[1:size(clkOUT,"*")]) then
1244         ok=%f;%cpr=list()
1245         message("Event output ports are not numbered properly.")
1246         return
1247     end
1248
1249     //Check if there is more than one clock in the diagram
1250     szclkIN=size(clkIN,2);
1251     if szclkIN==0 then
1252         szclkIN=[]
1253     end
1254     flgcdgen=szclkIN;
1255     [bllst,connectmat,clkconnect,cor,corinv,ok,scs_m,flgcdgen,freof]=c_pass1(scs_m,flgcdgen);
1256
1257     // Check for any To Workspace/FromWorkspace block
1258     for i = 1:size(bllst)
1259         if bllst(i).sim(1) == "tows_c" then
1260             // Explore scs_m to find original block
1261             towsObjIndex = corinv(i);
1262             towsObj = scs_m.objs(towsObjIndex(1));
1263             for j = 2:size(towsObjIndex, "*")
1264                 towsObj = towsObj.model.rpar.objs(towsObjIndex(j));
1265             end
1266             bllst(i).sim(1) = "writeGlobal_" + ...
1267             towsObj.graphics.exprs(2);
1268             // Force state to enable case 2 call
1269             // on generated code
1270             bllst(i).dstate = 0;
1271             writeGlobal = [writeGlobal towsObj.graphics.exprs(2)];
1272             writeGlobalSize = [writeGlobalSize bllst(i).ipar(1)];
1273         elseif bllst(i).sim(1) == "fromws_c" then
1274             fromwsObjIndex = corinv(i);
1275             fromwsObj = scs_m.objs(fromwsObjIndex(1));
1276             for j = 2:size(fromwsObjIndex, "*")
1277                 fromwsObj = fromwsObj.model.rpar.objs(fromwsObjIndex(j));
1278             end
1279             bllst(i).sim(1) = "readGlobal_" + fromwsObj.graphics.exprs(1);
1280             readGlobal = [readGlobal fromwsObj.graphics.exprs(1)];
1281             readGlobalTimeSize = [readGlobalTimeSize evstr("size(" + fromwsObj.graphics.exprs(1) + ".time, ""*"")")]
1282             readGlobalSize = [readGlobalSize evstr("size(" + fromwsObj.graphics.exprs(1) + ".values(1, :), ""*"")")]
1283         end
1284     end
1285
1286     if flgcdgen<> szclkIN
1287         clkIN=[clkIN flgcdgen]
1288     end
1289     szclkINTemp=szclkIN;
1290     szclkIN=flgcdgen;
1291     // [bllst,connectmat,clkconnect,cor,corinv,ok]=c_pass1(scs_m);
1292
1293     //Test for  ALWAYS_ACTIVE sblock (RN -25/06/07)
1294     ALWAYS_ACTIVE=%f;
1295     for blki=bllst
1296         if blki.dep_ut($) then
1297             ALWAYS_ACTIVE=%t;
1298             break;
1299         end
1300     end
1301     if ALWAYS_ACTIVE then
1302         CAPTEURS=[];
1303         for Ii=1:length(bllst)
1304             if part(bllst(Ii).sim(1),1:7)=="capteur" then
1305                 bllst(Ii).dep_ut($)=%t
1306                 //       elseif part(bllst(Ii).sim(1),1:10)=='actionneur' then
1307                 //         bllst(Ii).dep_ut($)=%t
1308             end
1309         end
1310     end
1311
1312     if ~ok then
1313         message("Sorry: problem in the pre-compilation step.")
1314         return
1315     end
1316     a=[];
1317     b=[];
1318     tt=[];
1319     howclk=[];
1320     allhowclk=[];
1321     allhowclk2=[];
1322     cap=[];
1323     act=[];
1324
1325     ///**********************************
1326     for i=1:size(bllst)
1327         // check for a scilab function block
1328         if type(bllst(i).sim(1)) == 13 then
1329             ok=%f;
1330             %cpr=list();
1331             message(_("Superblock should not contains any Scilab function block."))
1332             return
1333         end
1334
1335         for j=1:size(bllst)
1336             if (bllst(i).sim(1)=="actionneur"+string(j)) then
1337                 if tt<>i then
1338                     act=[act;i];
1339                     tt=i;
1340                 end
1341             elseif (bllst(i).sim(1)=="capteur"+string(j)) then
1342                 if tt<>i then
1343                     cap=[cap;i];
1344                     tt=i;
1345                 end
1346             elseif (bllst(i).sim(1)=="bidon") then
1347                 if tt<>i then
1348                     allhowclk=[allhowclk;i];
1349                     tt=i;
1350                 end
1351             elseif (bllst(i).sim(1)=="bidon2") then
1352                 if tt<>i then
1353                     allhowclk2=[allhowclk2;i];
1354                     tt=i;
1355                 end
1356             end
1357         end
1358     end
1359     ///**********************************
1360     if szclkIN>1 then
1361         //replace the N Event inputs by a fictious block with 2^N as many event
1362         //outputs
1363         output=ones((2^szclkIN)-1,1)
1364         bllst($+1)=scicos_model(sim=list("bidon",1),evtout=output,..
1365         blocktype="d",..
1366         firing=-output',dep_ut=[%f %f])
1367         corinv(size(bllst))=size(bllst)+1;
1368         howclk=size(bllst)
1369         // adjust the links accordingly
1370         for i=1:(2^szclkIN)-1
1371             vec=codebinaire(i,szclkIN)
1372             for j=1:szclkIN
1373                 if vec(j)*allhowclk(j)>=1 then
1374                     for k=1:size(clkconnect,1)
1375                         if clkconnect(k,1)==allhowclk(j) then
1376                             clkconnect=[clkconnect;[howclk i clkconnect(k,3:4)]]
1377                         end
1378                     end
1379                 end
1380             end
1381         end
1382     elseif szclkIN==[]&~ALWAYS_ACTIVE then
1383         //superblock has no Event input, add a fictious clock
1384         output=ones((2^(size(cap,"*")))-1,1)
1385         if (output == []) then
1386             output=0;
1387         end
1388         bllst($+1)=scicos_model(sim=list("bidon",1),evtout=output,..
1389         firing=-output,blocktype="d",dep_ut=[%f %f])
1390         corinv(size(bllst))=size(bllst)+1;
1391         howclk=size(bllst);
1392     elseif szclkIN==1  then
1393         howclk=allhowclk;
1394     end
1395
1396     //mise en ordre de clkconnect
1397     if szclkIN>1 then
1398         newclkconnect=clkconnect;
1399         clkconnect=[];
1400         for i=1:size(newclkconnect,1)-1
1401             if or(newclkconnect(i,:)<>newclkconnect(i+1,:)) then
1402                 clkconnect=[clkconnect;newclkconnect(i,:)]
1403             end
1404         end
1405         if or(newclkconnect($-1,:)<>newclkconnect($,:)) then
1406             clkconnect=[clkconnect;newclkconnect($,:)]
1407         end
1408
1409         //suppression des blocs bidons
1410         newclkconnect=clkconnect;nkt=[];
1411         for i=1:szclkIN
1412             for k=1:size(newclkconnect,1)
1413                 if newclkconnect(k,1)~=allhowclk(i) then
1414                     nkt=[nkt;newclkconnect(k,:)];
1415                 end
1416             end
1417             newclkconnect=nkt;
1418             nkt=[];
1419         end
1420         clkconnect=newclkconnect;
1421     end
1422
1423     //**************************************************
1424     // nouveau clkconnect avec liaisons sur les capteurs
1425     //**************************************************
1426
1427     //
1428     // Generate block activation fo C code
1429     //
1430     n=size(cap,1)
1431     if ~(szclkIN==[]) then
1432         // Activate sensors on each clock pulse.
1433         generatedConnection = [];
1434         for i=1:n
1435             if szclkIN>1 then
1436                 for j=1:(2^szclkIN)-1
1437                     generatedConnection = [generatedConnection ; [howclk j cap(i) 1]];
1438                 end
1439             elseif szclkIN==1 then
1440                 generatedConnection = [generatedConnection ; [howclk 1 cap(i) 1]];
1441             end
1442         end
1443         clkconnect = [clkconnect ; generatedConnection]
1444     elseif ~ALWAYS_ACTIVE then
1445         // Generate all possible activations for sensors, based on number of blocks.
1446         //codage de l'activation des capteurs dans le cas de l'heritage
1447         generatedConnection = [];
1448         for i=1:2^n-1
1449             vec=codebinaire(i,n);
1450             for j=1:n
1451                 if (vec(j)==1) then
1452                     generatedConnection = [generatedConnection ; [howclk i cap(j) 1]];
1453                 end
1454             end
1455         end
1456         clkconnect = [clkconnect ; generatedConnection]
1457     end
1458
1459     FIRING=[]
1460     for i=1:size(allhowclk2,1)
1461         j = find(clkconnect(:,3)==allhowclk2(i))
1462         if j<>[] then
1463             FIRING=[FIRING;bllst(clkconnect(j,1)).firing(clkconnect(j,2))]
1464         end
1465     end
1466
1467     Code_gene_run=[];
1468
1469     //** OLD GRAPHICS
1470     //** %windo = gcf().figure_id
1471
1472     cpr=c_pass2(bllst,connectmat,clkconnect,cor,corinv)
1473
1474     if cpr==list() then ok=%f,return, end
1475
1476     //** Alan's patch 5/07/07: try to solve
1477     //   which blocks use work
1478     funs_save=cpr.sim.funs;
1479     funtyp_save=cpr.sim.funtyp;
1480     with_work = zeros(cpr.sim.nblk,1)
1481     for i=1:size(cpr.sim.funs)
1482         if part(cpr.sim.funs(i),1:10)=="actionneur" then
1483             cpr.sim.funs(i) ="bidon"
1484             cpr.sim.funtyp(i) = 1
1485         elseif part(cpr.sim.funs(i),1:7)=="capteur" then
1486             cpr.sim.funs(i) ="bidon"
1487             cpr.sim.funtyp(i) = 1
1488         end
1489     end
1490
1491     //**
1492     //** retrieve all open Scilab windows with winsid()
1493     //**
1494
1495     BeforeCG_WinList = winsid();
1496
1497     ierr=execstr("[state,t]=scicosim(cpr.state,0,0,cpr.sim,"+..
1498     "''start'',scs_m.props.tol)","errcatch")
1499     if ierr==0 then
1500         for i=1:cpr.sim.nblk
1501             if state.iz(i)<>0 then
1502                 with_work(i)=%t
1503             end
1504         end
1505         ierr=execstr("[state,t]=scicosim(state,0,0,cpr.sim,"+..
1506         "''finish'',scs_m.props.tol)","errcatch")
1507     end
1508
1509     //**
1510     //** retrieve all open Scilab windows with winsid
1511     //** and close the additional windows open since first
1512     //**
1513
1514     //** This code does not cover 100% of the possible situations because the user can
1515     //** destroy one or more Scicos wins manually during this intermediate phase
1516     //** This code is 100% functional if the user does not close manually any win.
1517     //** TO BE updated in Scilab 5.0
1518
1519     AfterCG_WinList = winsid();
1520
1521     AfterCG_size = size(AfterCG_WinList); //** matrix
1522     AfterCG_size = AfterCG_size(2) ; //** vector length
1523
1524     BeforeCG_size = size(BeforeCG_WinList); //** matrix
1525     BeforeCG_size = BeforeCG_size(2) ; //** vector length
1526
1527     if (or(AfterCG_WinList<>BeforeCG_WinList)) & (AfterCG_size>BeforeCG_size) then
1528         //** means that a scope or other scicos object has created some
1529         //** output window
1530
1531         DiffCG_Winlist = AfterCG_WinList<>BeforeCG_WinList ; //** T/F mismatch
1532         DiffCG_Index = find(DiffCG_Winlist); //** recover the mismatched indexes only
1533
1534         for win_idx = DiffCG_Index
1535             delete( scf( AfterCG_WinList(win_idx) ) ) ; //** clear the spurious windows
1536         end
1537
1538     end
1539     //**------------- end of windows cleaning ----------------------------------------
1540
1541     cpr.sim.funs=funs_save;
1542     cpr.sim.funtyp=funtyp_save;
1543
1544     //** OLD GRAPHICS
1545     //** scf(%windo)
1546
1547     ///////////////////
1548     //les pointeurs de cpr :
1549     x=cpr.state.x;
1550     z=cpr.state.z;
1551     outtb=cpr.state.outtb;
1552
1553     //RN
1554     zcptr=cpr.sim.zcptr;
1555     ozptr=cpr.sim.ozptr;
1556     rpptr=cpr.sim.rpptr;
1557     ipptr=cpr.sim.ipptr;
1558     opptr=cpr.sim.opptr;
1559     funs=cpr.sim.funs;
1560     xptr=cpr.sim.xptr;
1561     zptr=cpr.sim.zptr;
1562     inpptr=cpr.sim.inpptr;
1563     inplnk=cpr.sim.inplnk;
1564     outptr=cpr.sim.outptr;
1565     outlnk=cpr.sim.outlnk;
1566     //@l@n lnkptr=cpr.sim.lnkptr;
1567     ordclk=cpr.sim.ordclk;
1568     funtyp=cpr.sim.funtyp;
1569     cord=cpr.sim.cord;
1570     ncord=size(cord,1);
1571     nblk=cpr.sim.nb;
1572     ztyp=cpr.sim.ztyp;
1573     clkptr=cpr.sim.clkptr
1574     labels=cpr.sim.labels;
1575     //taille totale de z : nztotal
1576     nztotal=size(z,1);
1577
1578     //*******************************
1579     //Checking if superblock is valid
1580     //*******************************
1581     msg=[]
1582     for i=1:length(funs)-1
1583         if funtyp(i)==3 then
1584             msg=[msg;"Type 3 block''s not allowed"]
1585             break;
1586         end
1587         if msg<>[] then message(msg),ok=%f,return,end
1588     end
1589
1590     //********************************************************
1591     // Change logical units for readf and writef blocks if any ???
1592     //********************************************************
1593     lunit=0
1594     for d=1:length(funs)
1595         if funs(d)=="readf"  then
1596             z(zptr(d)+2)=lunit
1597             lunit=lunit+1;
1598         elseif funs(d)=="writef"
1599             z(zptr(d)+1)=lunit
1600             lunit=lunit+1;
1601         end
1602     end
1603
1604     //***********************************
1605     // Get the name of the file
1606     //***********************************
1607     foo=3;
1608     okk=%f;
1609     rdnom="foo";
1610     rpat=pwd();
1611     [x,ierr] = fileinfo(rpat);
1612     S_IWRITE = 128; // mask write permission
1613     if (ierr <> 0 | bitand(x(2), S_IWRITE) == 0) then
1614         rpat = TMPDIR;
1615     end
1616
1617     libs="";
1618     label1=[hname;rpat+"/"+hname;""];
1619
1620     while %t do
1621         ok=%t  // to avoid infinite loop
1622         [okk,..
1623         rdnom,..
1624         rpat,..
1625         libs,..
1626         label1]=scicos_getvalue("Set code generator parameters :",..
1627         ["New block''s name :";
1628         "Created files Path :";
1629         "Other object files to link with (if any)"],..
1630         list("str",1,"str",1,"str",1),label1);
1631         if okk==%f then
1632             ok=%f
1633             return
1634         end
1635         rpat=stripblanks(rpat);
1636
1637         //** 1/07/06 Alan trying to solve multiple libraries during week-end
1638         if strindex(libs,"''")<>[] | strindex(libs,"""")<>[] then
1639             ierr=execstr("libs=evstr(libs)","errcatch")
1640             if ierr<>0  then
1641                 message(["Can''t solve other files to link"])
1642                 ok=%f;
1643                 return
1644             end
1645         end
1646
1647         //** Alan, I put a warning here in order to inform the user
1648         //** that the name of the superblock will change
1649         //** because the space char in name isn't allowed.
1650         if grep(rdnom," ")<>[] then
1651             message(["Superblock name cannot contains space characters.";
1652             "space chars will be automatically substituted by ""_"" "])
1653         end
1654         rdnom = strsubst(rdnom," ","_");
1655
1656         //** Put a warning here in order to inform the user
1657         //** that the name of the superblock will change
1658         //** because the "-" char could generate GCC problems
1659         //** (the C functions contains the name of the superblock).
1660         if grep(rdnom,"-")<>[] then
1661             message(["For full C compiler compatibility ";
1662             "Superblock name cannot contains ""-"" characters";
1663             """-"" chars will be automatically substituted by ""_"" "])
1664         end
1665         rdnom = strsubst(rdnom,"-","_");
1666
1667         dirinfo=fileinfo(rpat)
1668
1669         if dirinfo==[] then
1670             [pathrp,fnamerp,extensionrp]=fileparts(rpat)
1671             ok=mkdir(pathrp,fnamerp+extensionrp)
1672             if ~ok then
1673                 messagebox("Directory "+rpat+" cannot be created","modal","info");
1674             end
1675         elseif filetype(dirinfo(2))<>"Directory" then
1676             ok=%f;
1677             messagebox(rpat+" is not a directory","modal","error");
1678         end
1679
1680         if stripblanks(rdnom)==emptystr() then
1681             ok=%f;
1682             messagebox("sorry C file name not defined","modal","error");
1683         end
1684         if ok then break,end
1685     end
1686
1687     //////////////////////////////////////////////////
1688     maxnrpar=max(rpptr(2:$)-rpptr(1:$-1))
1689     maxnipar=max(ipptr(2:$)-ipptr(1:$-1))
1690     maxnx=max(xptr(2:$)-xptr(1:$-1))
1691     maxnz=max(zptr(2:$)-zptr(1:$-1))
1692     maxnin=max(inpptr(2:$)-inpptr(1:$-1))
1693     maxnout=max(outptr(2:$)-outptr(1:$-1))
1694     maxdim=[];
1695     for i=1:size(cpr.state.outtb)
1696         maxdim=max(size(cpr.state.outtb(i)))
1697     end
1698     maxtotal=max([maxnrpar;maxnipar;maxnx;maxnz;maxnin;maxnout;maxdim]);
1699
1700     //************************************************************************
1701     //generate the call to the blocks and blocs simulation function prototypes
1702     //************************************************************************
1703     wfunclist=list();
1704     nbcap=0;nbact=0;capt=[];actt=[];Protostalone=[];Protos=[];
1705     dfuns=[]
1706     for i=1:length(funs)
1707         if or(i==act) then //block is an actuator
1708             nbact=nbact+1;
1709             [Code,actti,protoi]=call_actuator(i)
1710             wfunclist($+1)=[Code;"if(flag < 0 ) return(5 - flag);"]
1711             if nbact==1 then Protostalone=[Protostalone;"";protoi],end
1712             actt=[actt;actti]
1713         elseif or(i==cap) then //block is a sensor
1714             nbcap=nbcap+1;
1715             [Code,capti,protoi]=call_sensor(i)
1716             wfunclist($+1)=[Code;"if(flag < 0 ) return(5 - flag);"]
1717             if nbcap==1 then Protostalone=[Protostalone;"";protoi] ,end
1718             capt=[capt;capti]
1719         elseif funs(i)=="bidon"
1720             wfunclist($+1)=" "
1721         elseif funs(i)=="bidon2"
1722             wfunclist($+1)=" "
1723         else
1724             ki=find(funs(i)==dfuns)
1725             dfuns=[dfuns;funs(i)]
1726             //[Code,protoi]=call_block(i)
1727             [Code]=call_block4(i)
1728             if Code<>[] then
1729                 wfunclist($+1)=[Code;"if(flag < 0 ) return(5 - flag);"]
1730             else
1731                 wfunclist($+1)=" ";
1732             end
1733             if ki==[] then
1734                 Protos=[Protos;"";BlockProto(i)];
1735                 Protostalone=[Protostalone;"";BlockProto(i)];
1736             end
1737         end
1738     end
1739
1740     //**************************************
1741     //on prend en compte l'ordre des numerotation graphique des
1742     //capteur et des actionneur
1743     [junk,index]=gsort(-actt(:,$));
1744     actt=actt(index,1:$) ;
1745     [junk,index]=gsort(-capt(:,$));
1746     capt=capt(index,1:$) ;
1747
1748     //***************************************************
1749     //Compute the initial state and outtb (links)  values
1750     //***************************************************
1751     //petite modification pour uniformiser les fichiers capteurs
1752     //et actionneurs ???
1753     rdcpr=cpr.sim.funs;
1754     for r=1:length(cap),rdcpr(cap(r))="bidon";end
1755     for r=1:length(act),rdcpr(act(r))="bidon";end
1756     Total_rdcpr=cpr.sim;Total_rdcpr.funs=rdcpr;
1757     //
1758     tcur=0;
1759     tf=scs_m.props.tf;
1760     tolerances=scs_m.props.tol;
1761     //[state,t]=scicosim(cpr.state,tcur,tf,Total_rdcpr,'start',tolerances);
1762     //cpr.state=state;
1763     z=cpr.state.z;
1764     outtb=cpr.state.outtb;
1765     //[junk_state,t]=scicosim(cpr.state,tcur,tf,Total_rdcpr,'finish',tolerances);
1766
1767     //***********************************
1768     // Scilab and C files generation
1769     //***********************************
1770
1771     //** generate scilab interfacing function
1772     //   of the generated scicos block
1773     ok=gen_gui42();
1774
1775
1776     //** generate C files
1777     //   of the generated scicos block
1778     if ok then
1779         ok=gen_ccode42()
1780     end
1781
1782     //** Generates Makefile, loader
1783     //   and compile and link C files
1784
1785     //** def files to build
1786     files=[rdnom rdnom+"_void_io" rdnom+"_Cblocks"]
1787
1788     //** def files to build for standalone
1789     filestan=[rdnom+"_standalone" rdnom+"_act_sens_events" rdnom+"_Cblocks"]
1790
1791     if ok then
1792         ok = buildnewblock(rdnom,files,filestan,rpat,libs,"","")
1793     end
1794
1795     //**
1796     //if ok then ok=gen_loader(),end
1797
1798     if ok then
1799         //load the gui function
1800         exec(rpat+"/"+rdnom+"_c.sci");
1801         //Change diagram superblock to new generated block
1802         XX=update_block(XX);
1803         execstr(rdnom+"_c=resume("+rdnom+"_c)")
1804     else
1805         message([_("Generated block cannot be linked with Scilab.")])
1806     end
1807
1808 endfunction
1809
1810 //BlockProto : generate prototype
1811 //            of a scicos block
1812 //
1813 //inputs : bk   : bloc index
1814 //
1815 //output : txt  :
1816 //
1817 //16/06/07 Author : A.Layec
1818 function [txt]=BlockProto(bk)
1819
1820     nin=inpptr(bk+1)-inpptr(bk);  //* number of input ports */
1821     nout=outptr(bk+1)-outptr(bk); //* number of output ports */
1822
1823     //**
1824     ftyp=funtyp(bk)
1825     if ftyp>2000 then ftyp=ftyp-2000,end
1826     if ftyp>1000 then ftyp=ftyp-1000,end
1827
1828     //** check function type
1829     if ftyp < 0 then //** ifthenelse eselect blocks
1830         txt = [];
1831         return;
1832     else
1833         if (ftyp<>0 & ftyp<>1 & ftyp<>2 & ftyp<>3 & ftyp<>4) then
1834             disp("types other than 0,1,2,3 or 4 are not yet supported.")
1835             txt = [];
1836             return;
1837         end
1838     end
1839
1840     //** add comment
1841     txt=[get_comment("proto_blk",list(funs(bk),funtyp(bk),bk,labels(bk)));]
1842
1843     select ftyp
1844         //** zero funtyp
1845     case 0 then
1846
1847         //*********** prototype definition ***********//
1848         txtp=["(int *, int *, double *, double *, double *, int *, double *, \";
1849         " int *, double *, int *, double *, int *,int *, int *, \";
1850         " double *, int *, double *, int *);"];
1851         if (funtyp(bk)>2000 & funtyp(bk)<3000)
1852             blank = get_blank("void "+funs(bk)+"(");
1853             txtp(1) = "void "+funs(bk)+txtp(1);
1854         elseif (funtyp(bk)<2000)
1855             txtp(1) = "void C2F("+funs(bk)+")"+txtp(1);
1856             blank = get_blank("void C2F("+funs(bk)+")");
1857         end
1858         txtp(2:$) = blank + txtp(2:$);
1859         txt = [txt;txtp];
1860         //*******************************************//
1861
1862
1863         //**
1864     case 1 then
1865
1866         //*********** prototype definition ***********//
1867         txtp=["(int *, int *, double *, double *, double *, int *, double *, \";
1868         " int *, double *, int *, double *, int *,int *, int *";]
1869         if (funtyp(bk)>2000 & funtyp(bk)<3000)
1870             blank = get_blank("void "+funs(bk)+"(");
1871             txtp(1) = "void "+funs(bk)+txtp(1);
1872         elseif (funtyp(bk)<2000)
1873             txtp(1) = "void C2F("+funs(bk)+")"+txtp(1);
1874             blank = get_blank("void C2F("+funs(bk)+")");
1875         end
1876         if nin>=1 | nout>=1 then
1877             txtp($)=txtp($)+", \"
1878             txtp=[txtp;""]
1879             if nin>=1 then
1880                 for k=1:nin
1881                     txtp($)=txtp($)+" double *, int * ,"
1882                 end
1883                 txtp($)=part(txtp($),1:length(txtp($))-1); //remove last ,
1884             end
1885             if nout>=1 then
1886                 if nin>=1 then
1887                     txtp($)=txtp($)+", \"
1888                     txtp=[txtp;""]
1889                 end
1890                 for k=1:nout
1891                     txtp($)=txtp($)+" double *, int * ,"
1892                 end
1893                 txtp($)=part(txtp($),1:length(txtp($))-1); //remove last ,
1894             end
1895         end
1896
1897         if ztyp(bk) then
1898             txtp($)=txtp($)+", \"
1899             txtp=[txtp;" double *,int *);"];
1900         else
1901             txtp($)=txtp($)+");";
1902         end
1903
1904         txtp(2:$) = blank + txtp(2:$);
1905         txt = [txt;txtp];
1906         //*******************************************//
1907
1908         //**
1909     case 2 then
1910
1911         //*********** prototype definition ***********//
1912
1913         txtp=["void "+funs(bk)+...
1914         "(int *, int *, double *, double *, double *, int *, double *, \";
1915         " int *, double *, int *, double *, int *, int *, int *, \"
1916         " double **, int *, int *, double **,int *, int *"];
1917         if ~ztyp(bk) then
1918             txtp($)=txtp($)+");";
1919         else
1920             txtp($)=txtp($)+", \";
1921             txtp=[txtp;
1922             " double *,int *);"]
1923         end
1924         blank = get_blank("void "+funs(bk));
1925         txtp(2:$) = blank + txtp(2:$);
1926         txt = [txt;txtp];
1927         //********************************************//
1928
1929         //**
1930     case 4 then
1931         txt=[txt;
1932         "void "+funs(bk)+"(scicos_block *, int );"];
1933
1934     end
1935 endfunction
1936
1937 //Generating the routine for actuators interfacing
1938 //
1939 //
1940 //Authors : R. Djenid, R. Nikoukhah, A. Layec
1941 //
1942 //actt=[i uk nuk_1 nuk_2 uk_t bllst(i).ipar]
1943 function Code=make_actuator(standalone)
1944     Call=["/*"
1945     "**    Generated by Code_Generation toolbox of Xcos with "+getversion()
1946     "**    Date : "+date()
1947     "*/"
1948     ""
1949     "/*"+part("-",ones(1,40))+" Actuators */";
1950     "void "+rdnom+"_actuator(int *flag, int *nport, int *nevprt, double *t, void *u, int *nu1, int *nu2, int *ut, int flag1)"]
1951
1952     comments=["     /*"
1953     "      * To be customized for standalone execution";
1954     "      * flag  : specifies the action to be done"
1955     "      * nport : specifies the  index of the Super Bloc"
1956     "      *         regular input (The input ports are numbered"
1957     "      *         from the top to the bottom )"
1958     "      * nevprt: indicates if an activation had been received"
1959     "      *         0 = no activation"
1960     "      *         1 = activation"
1961     "      * t     : the current time value"
1962     "      * u     : the vector inputs value"
1963     "      * nu1   : the input size 1"
1964     "      * nu2   : the input size 2"
1965     "      * ut    : the input type"
1966     "      * flag1 : learn mode (0 from terminal,1 from input file"
1967     "      */"]
1968
1969     dcl=["{"
1970     "  int j,k,l;"];
1971
1972     if standalone then
1973         a_actuator=["  /* skeleton to be customized */"
1974         "    switch (*flag) {"
1975         "    /* OutputUpdate (*flag==1) will be called on each iteration */"
1976         "    case 1 :"
1977         "      /*if(*nevprt>0) { get the input value */"
1978         "        switch (*ut) {"
1979         "        case 10 :"
1980         "          for (l=0;l<*nu2;l++) {"
1981         "            for (k=0;k<*nu1;k++) {"
1982         "              printf(""Actuator: time=%f, "+...
1983         "u(%d,%d) of actuator %d is %f "+...
1984         "\n"", \"
1985         "                     *t, k, l, *nport,"+...
1986         "*((double *) u+(k+l*(*nu1))));"
1987         "            }"
1988         "          }"
1989         "          break;"
1990         ""
1991         "        case 11 :"
1992         "          for (l=0;l<*nu2;l++) {"
1993         "            for (k=0;k<*nu1;k++) {"
1994         "              printf(""Actuator: time=%f, "+...
1995         "u(%d,%d) of actuator %d is %f,%f "+...
1996         "\n"", \"
1997         "                     *t, k, l, *nport,"+...
1998         "*((double *) u+(k+l*(*nu1))),"+...
1999         "*((double *) u+((*nu1)*(*nu2)+k+l*(*nu1))));"
2000         "            }"
2001         "          }"
2002         "          break;"
2003         ""
2004         "        case 81 :"
2005         "          for (l=0;l<*nu2;l++) {"
2006         "            for (k=0;k<*nu1;k++) {"
2007         "              printf(""Actuator: time=%f, "+...
2008         "u(%d,%d) of actuator %d is %i "+...
2009         "\n"", \"
2010         "                     *t, k, l, *nport,"+...
2011         "*((char *) u+(k+l*(*nu1))));"
2012         "            }"
2013         "          }"
2014         "          break;"
2015         ""
2016         "        case 82 :"
2017         "          for (l=0;l<*nu2;l++) {"
2018         "            for (k=0;k<*nu1;k++) {"
2019         "              printf(""Actuator: time=%f, "+...
2020         "u(%d,%d) of actuator %d is %hd "+...
2021         "\n"", \"
2022         "                     *t, k, l, *nport,"+...
2023         "*((short *) u+(k+l*(*nu1))));"
2024         "            }"
2025         "          }"
2026         "          break;"
2027         ""
2028         "        case 84 :"
2029         "          for (l=0;l<*nu2;l++) {"
2030         "            for (k=0;k<*nu1;k++) {"
2031         "              printf(""Actuator: time=%f, "+...
2032         "u(%d,%d) of actuator %d is %ld "+...
2033         "\n"", \"
2034         "                     *t, k, l, *nport,"+...
2035         "*((long *) u+(k+l*(*nu1))));"
2036         "            }"
2037         "          }"
2038         "          break;"
2039         ""
2040         "        case 811 :"
2041         "          for (l=0;l<*nu2;l++) {"
2042         "            for (k=0;k<*nu1;k++) {"
2043         "              printf(""Actuator: time=%f, "+...
2044         "u(%d,%d) of actuator %d is %d "+...
2045         "\n"", \"
2046         "                     *t, k, l, *nport,"+...
2047         "*((unsigned char *) u+(k+l*(*nu1))));"
2048         "            }"
2049         "          }"
2050         "          break;"
2051         ""
2052         "        case 812 :"
2053         "          for (l=0;l<*nu2;l++) {"
2054         "            for (k=0;k<*nu1;k++) {"
2055         "              printf(""Actuator: time=%f, "+...
2056         "u(%d,%d) of actuator %d is %hu "+...
2057         "\n"", \"
2058         "                     *t, k, l, *nport,"+...
2059         "*((unsigned short *) u+(k+l*(*nu1))));"
2060         "            }"
2061         "          }"
2062         "          break;"
2063         ""
2064         "        case 814 :"
2065         "          for (l=0;l<*nu2;l++) {"
2066         "            for (k=0;k<*nu1;k++) {"
2067         "              printf(""Actuator: time=%f, "+...
2068         "u(%d,%d) of actuator %d is %lu "+...
2069         "\n"", \"
2070         "                     *t, k, l, *nport,"+...
2071         "*((unsigned long *) u+(k+l*(*nu1))));"
2072         "            }"
2073         "          }"
2074         "          break;"
2075         "        }"
2076         "      /*} */"
2077         "      break;"
2078         "    case 4 : /* actuator initialisation */"
2079         "      /* do whatever you want to initialize the actuator */"
2080         "      break;"
2081         "    case 5 : /* actuator ending */"
2082         "      /* do whatever you want to end the actuator */"
2083         "      break;"
2084         "    }"]
2085     else
2086         a_actuator=[]
2087     end
2088
2089     // pour fprintf
2090     nc=size(act,"*") // Alan : d'o? viens act ?????
2091     // reponse : de do_compile_superblock!
2092     typ=["""%f "]; //time
2093     for i=1:nc
2094         typ($)=typ($)+""" \"
2095         typ=[typ;""""];
2096         for j=1:sum(actt(i,3)*actt(i,4))
2097             //typ=typ+'%f ';
2098             typ($)=typ($)+scs_c_n2c_fmt(actt(i,5))+" ";
2099         end
2100     end
2101     typ($)=typ($)+"\n"", \"
2102     typ(1)="    fprintf(fprw,"+typ(1);
2103     bl    ="                 ";
2104     if size(typ,1) <> 1 then
2105         typ(2:$) = bl+typ(2:$);
2106     end
2107     //Code1='    fprintf(fprw, '"'+typ+' \n'",*t'
2108     Code1=[typ;bl+"*t"];
2109     //actt=[i uk nuk_1 nuk_2 uk_t bllst(i).ipar]
2110     for i=1:size(actt,1)
2111         ni=actt(i,3)*actt(i,4) // dimension of ith output
2112         Code1($)=Code1($)+","
2113         Code1=[Code1;bl];
2114         for j=1:ni
2115             if actt(i,5)<>11 then
2116                 Code1($)=Code1($)+...
2117                 "*((("+scs_c_n2c_typ(actt(i,5))+" *)("+...
2118                 rdnom+"_block_outtbptr+"+string(actt(i,2)-1)+"))"+...
2119                 "+"+string(j-1)+")";
2120             else //CAS CMPLX
2121                 Code1($)=Code1($)+...
2122                 "*((("+scs_c_n2c_typ(actt(i,5))+" *)("+...
2123                 rdnom+"_block_outtbptr+"+string(actt(i,2)-1)+"))"+...
2124                 "+"+string((j-1))+"), "+...
2125                 "*((("+scs_c_n2c_typ(actt(i,5))+" *)("+...
2126                 rdnom+"_block_outtbptr+"+string(actt(i,2)-1)+"))"+...
2127                 "+"+string(ni+(j-1))+")";
2128             end
2129             if j<>ni then
2130                 Code1($)=Code1($)+", ";
2131             end
2132         end
2133     end
2134     Code1($)=Code1($)+");"
2135
2136     Code=[]
2137
2138     if nc==1|~standalone then
2139         Code=[Call
2140         comments
2141         dcl
2142         "  if (flag1 == 0) {"
2143         a_actuator
2144         "  }"
2145         "  else if (flag1 == 1) {"
2146         "    if (*flag == 4 && *nport == 1) {"
2147         "      fprw=fopen(output,'"wt'");"
2148         "      if( fprw == NULL )"
2149         "        {"
2150         "          printf('"Error opening file: %s\n'", output);"
2151         "          return;"
2152         "        }"
2153         "    }else if (*flag == 2 /* && *nevprt>0 */) {"
2154         Code1
2155         "    }else if (*flag == 5 && *nport == 1) {"
2156         "      fclose(fprw);"
2157         "    }"
2158         "  }"
2159         "}"]
2160     elseif nc>1 then
2161         S="  switch (*nport) {"
2162         for k=1:nc
2163             S=[S;
2164             "  case "+string(k)+" :/* Port number "+string(k)+" ----------*/"
2165             "  "+a_actuator
2166             "  break;"]
2167         end
2168         S=[S;"  }"]
2169
2170         Code=[Code
2171         Call
2172         comments
2173         dcl
2174         "  if (flag1 == 0){"
2175         S
2176         "  }"
2177         "  else if (flag1 == 1) {"
2178         "    if (*flag == 4 && *nport == 1) {"
2179         "      fprw=fopen(output,'"wt'");"
2180         "      if( fprw == NULL ) {"
2181         "        printf('"Error opening file: %s\n'", output);"
2182         "        return;"
2183         "        }"
2184         "    }"
2185         "    else if (*flag == 2 /*&& *nevprt>0*/ ) {"
2186         Code1
2187         "    }"
2188         "    else if (*flag == 5 && *nport == 1) {"
2189         "      fclose(fprw);"
2190         "    }"
2191         "  }"
2192         "}"]
2193     end
2194 endfunction
2195
2196 //** Generates the scicos computational function
2197 //   associated with the block
2198 //12/07/07 Alan Layec
2199 function make_computational42(filename)
2200     z=cpr.state.z;
2201     oz=cpr.state.oz;
2202     outtb=cpr.state.outtb;
2203     tevts=cpr.state.tevts;
2204     evtspt=cpr.state.evtspt;
2205     outptr=cpr.sim.outptr;
2206     funtyp=cpr.sim.funtyp;
2207     clkptr=cpr.sim.clkptr;
2208     ordptr=cpr.sim.ordptr;
2209     pointi=cpr.state.pointi;
2210     ztyp=cpr.sim.ztyp;
2211     zcptr=cpr.sim.zcptr;
2212     zptr=cpr.sim.zptr;
2213     ozptr=cpr.sim.ozptr;
2214     opptr=cpr.sim.opptr;
2215     opar=cpr.sim.opar;
2216     rpptr=cpr.sim.rpptr;
2217     ipptr=cpr.sim.ipptr;
2218     inpptr=cpr.sim.inpptr;
2219     funs=cpr.sim.funs;
2220     xptr=cpr.sim.xptr;
2221     modptr=cpr.sim.modptr;
2222     inplnk=cpr.sim.inplnk;
2223     nblk=cpr.sim.nb;
2224     outlnk=cpr.sim.outlnk;
2225     oord=cpr.sim.oord;
2226     zord=cpr.sim.zord;
2227     iord=cpr.sim.iord;
2228     noord=size(cpr.sim.oord,1);
2229     nzord=size(cpr.sim.zord,1);
2230     niord=size(cpr.sim.iord,1);
2231
2232     Indent="  ";
2233     Indent2=Indent+Indent;
2234     BigIndent="          ";
2235
2236     nZ = size(z,"*"); //** index of work in z
2237     nO = size(oz); //** index of outtb in oz
2238
2239     stalone=%f
2240
2241     // Open file to write in
2242     fd = mopen(filename, "wt");
2243
2244     mputl(["/*"
2245     "**    SCILAB Computational function"
2246     "**    Generated by Code_Generation toolbox of Xcos with "+getversion()
2247     "**    Date : "+date()
2248     "*/"
2249     ""
2250     "/* ---- Headers ---- */"
2251     "#include <stdio.h>"
2252     "#include <stdlib.h>"
2253     "#include <memory.h>"
2254     "#include <string.h>"
2255     "#include ""machine.h"" "
2256     "#include ""scicos.h"" "
2257     "#include ""scicos_malloc.h"" "
2258     "#include ""scicos_free.h"" "
2259     "#ifndef max"
2260     "#define max(a,b) ((a) >= (b) ? (a) : (b))"
2261     "#endif"
2262     "#ifndef min"
2263     "#define min(a,b) ((a) <= (b) ? (a) : (b))"
2264     "#endif"], fd);
2265
2266     mputl([Protos ; ""], fd);
2267
2268     mputl(["/* Table of constant values */"
2269     "static int nrd_"+string(0:maxtotal)'+" = "+string(0:maxtotal)'+";"], fd);
2270
2271     if maxtotal<10 then
2272         mputl("static int nrd_10 = 10;", fd);
2273     end
2274     if maxtotal<11 then
2275         mputl("static int nrd_11 = 11;", fd);
2276     end
2277
2278     if maxtotal<81 then
2279         mputl("static int nrd_81 = 81;", fd);
2280     end
2281     if maxtotal<82 then
2282         mputl("static int nrd_82 = 82;", fd);
2283     end
2284     if maxtotal<84 then
2285         mputl("static int nrd_84 = 84;", fd);
2286     end
2287     if maxtotal<811 then
2288         mputl("static int nrd_811 = 811;", fd);
2289     end
2290     if maxtotal<812 then
2291         mputl("static int nrd_812 = 812;", fd);
2292     end
2293     if maxtotal<814 then
2294         mputl("static int nrd_814 = 814;", fd);
2295     end
2296
2297     mputl([""
2298     "/* Some general static variables */"
2299     "static double zero=0;"
2300     "static double w[1];"
2301     "static int aaa=0, bbb=0;"], fd);
2302
2303     mputl([""
2304     "/*"+part("-",ones(1,40))+" Block Computational function */ ";
2305     "int  "+rdnom+"(scicos_block *block, int flag)"
2306     "{"
2307     "  /* declaration of local variables for that block struct */"
2308     "  double* z      = block->z;"
2309     "  void **ozptr   = block->ozptr;"
2310     "  void **oparptr = block->oparptr;"
2311     "  int nopar      = block->nopar;"
2312     "  double* x      = block->x;"
2313     "  double* xd     = block->xd;"
2314     "  double* res    = block->res;"
2315     "  void** u       = block->inptr;"
2316     "  void** y       = block->outptr;"
2317     "  int nevprt     = block->nevprt;"
2318     "  double* rpar   = block->rpar;"
2319     "  int nrpar      = block->nrpar;"
2320     "  int* ipar      = block->ipar;"
2321     "  int nipar      = block->nipar;"
2322     "  double* g      = block->g;"
2323     "  int* mode      = block->mode;"
2324     "  void **work    = block->work;"
2325     ""
2326     "  double t     = get_scicos_time();"
2327     "  int    phase = get_phase_simulation();"
2328     ""
2329     "  int kf = 0;"
2330     "  int i = 0;"
2331     "  int* reentryflag = NULL;"
2332     ""
2333     "  double *args[100];"
2334     "  int local_flag;"
2335     "  int nport;"
2336     "  void **"+rdnom+"_block_outtbptr;"
2337     "  scicos_block *block_"+rdnom+";"
2338     ""
2339     "  /*  work of blocks are catenated at the end of z */"
2340     "  work = (void **)(z+"+string(nZ)+");"
2341     ""
2342     "  /*  "+rdnom+"_block_outtbptr is catenated at the end of oz */"
2343     "  "+rdnom+"_block_outtbptr = (void **)(ozptr+"+string(nO)+");"
2344     ""
2345     "  /* struct of all blocks are stored in work of that block struct */"
2346     "  block_"+rdnom+"=(scicos_block*) *block->work;"
2347     ""
2348     "  /* Copy inputs in the block outtb */"], fd);
2349
2350
2351     for i=1:size(capt,1)
2352         ni=capt(i,3)*capt(i,4); //** dimension of ith input
2353         if capt(i,5)<>11 then
2354             mputl(["  memcpy(*("+rdnom+"_block_outtbptr+"+string(capt(i,2)-1)+"),"+...
2355             "*(u+"+string(capt(i,6)-1)+"),"+...
2356             string(ni)+"*sizeof("+mat2c_typ(capt(i,5))+"));"], fd);
2357         else //** Cas cmplx
2358             mputl(["  memcpy(*("+rdnom+"_block_outtbptr+"+string(capt(i,2)-1)+"),"+...
2359             "*(u+"+string(capt(i,6)-1)+"),"+...
2360             string(2*ni)+"*sizeof("+mat2c_typ(capt(i,5))+"));"], fd);
2361         end
2362     end
2363
2364     mputl([""
2365     "  if (flag != 4 && flag != 6 && flag != 5){"
2366     "    reentryflag=(int*) ((scicos_block *)(*block->work)+"+string(nblk)+");"
2367     "    if (*reentryflag==0){"
2368     "      *reentryflag=1;"], fd);
2369
2370     for kf=1:nblk
2371         nin=inpptr(kf+1)-inpptr(kf);  //** number of input ports
2372         nout=outptr(kf+1)-outptr(kf); //** number of output ports
2373         nx=xptr(kf+1)-xptr(kf);
2374         ng=zcptr(kf+1)-zcptr(kf);
2375         nmode=modptr(kf+1)-modptr(kf);
2376
2377         //** add comment
2378         mputl([""
2379         "      "+get_comment("set_blk",list(funs(kf),funtyp(kf),kf,labels(kf)));], fd);
2380
2381         for k=1:nin
2382             lprt=inplnk(inpptr(kf)-1+k);
2383             mputl(["      block_"+rdnom+"["+string(kf-1)+"].inptr["+string(k-1)+"]  = "+...
2384             rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2385         end
2386         for k=1:nout
2387             lprt=outlnk(outptr(kf)-1+k);
2388             mputl(["      block_"+rdnom+"["+string(kf-1)+"].outptr["+string(k-1)+"] = "+...
2389             rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2390         end
2391         mputl(["      block_"+rdnom+"["+string(kf-1)+"].z         = &(z["+...
2392         string(zptr(kf)-1)+"]);"], fd);
2393         if nx <> 0 then
2394             mputl(["      block_"+rdnom+"["+string(kf-1)+"].x         = &(x["+...
2395             string(xptr(kf)-1)+"]);"], fd);
2396         else
2397             mputl(["      block_"+rdnom+"["+string(kf-1)+"].x         = &(zero);"
2398             "      block_"+rdnom+"["+string(kf-1)+"].xd        = w;"], fd);
2399         end
2400         if ng <> 0 then
2401             mputl(["      block_"+rdnom+"["+string(kf-1)+"].g         = &(g["+...
2402             string(zcptr(kf)-1)+"]);"], fd);
2403         else
2404             mputl(["      block_"+rdnom+"["+string(kf-1)+"].g         = &(zero);";], fd);
2405         end
2406         if nmode <> 0 then
2407             mputl(["      block_"+rdnom+"["+string(kf-1)+"].mode      = &(mode["+...
2408             string(modptr(kf)-1)+"]);"], fd);
2409         end
2410         if (part(funs(kf),1:7) ~= "capteur" &...
2411             part(funs(kf),1:10) ~= "actionneur" &...
2412             funs(kf) ~= "bidon" &...
2413             funs(kf) ~= "bidon2") then
2414             //** rpar **//
2415             if (rpptr(kf+1)-rpptr(kf)>0) then
2416                 mputl(["      block_"+rdnom+"["+string(kf-1)+"].rpar      = &(rpar["+...
2417                 string(rpptr(kf)-1)+"]);"], fd);
2418             end
2419             //** ipar **//
2420             if (ipptr(kf+1)-ipptr(kf)>0) then
2421                 mputl(["      block_"+rdnom+"["+string(kf-1)+"].ipar      = &(ipar["+...
2422                 string(ipptr(kf)-1)+"]);"], fd);
2423             end
2424             //** opar **//
2425             if (opptr(kf+1)-opptr(kf)>0) then
2426                 nopar = opptr(kf+1)-opptr(kf);
2427                 for k=1:nopar
2428                     mputl(["      block_"+rdnom+"["+string(kf-1)+"].oparptr["+string(k-1)+...
2429                     "] = oparptr["+string(opptr(kf)-1+k-1)+"];"], fd);
2430                 end
2431             end
2432             //** oz **//
2433             if (ozptr(kf+1)-ozptr(kf)>0) then
2434                 noz = ozptr(kf+1)-ozptr(kf);
2435                 for k=1:noz
2436                     mputl(["      block_"+rdnom+"["+string(kf-1)+"].ozptr["+string(k-1)+...
2437                     "]  = ozptr["+string(ozptr(kf)-1+k-1)+"];"], fd);
2438                 end
2439             end
2440         end
2441         mputl(["      block_"+rdnom+"["+string(kf-1)+"].work      ="+...
2442         " (void **)(((double *)work)+"+string(kf-1)+");"], fd);
2443     end
2444
2445     mputl(["    }"
2446     "  }"
2447     ""
2448     ], fd);
2449
2450     /////////////////////////////////////////////
2451
2452
2453     //** find activation number
2454     blks=find(funtyp>-1);
2455     evs=[];
2456
2457     for blk=blks
2458         for ev=clkptr(blk):clkptr(blk+1)-1
2459             if funs(blk)=="bidon" then
2460                 if ev > clkptr(howclk) -1
2461                     evs=[evs,ev];
2462                 end
2463             end
2464         end
2465     end
2466
2467     //** flag 0
2468     flag = 0;
2469
2470     block_has_output=%f
2471     txt   = [];
2472     txt22 = [];
2473
2474     for kf=1:nblk
2475         nx=xptr(kf+1)-xptr(kf);
2476         if nx <> 0 then
2477             txt=[txt;
2478             "    block_"+rdnom+"["+string(kf-1)+"].xd=&(xd["+...
2479             string(xptr(kf)-1)+"]);"]
2480         end
2481         if part(funs(kf),1:10) == "actionneur" then
2482             block_has_output=%t
2483         end
2484     end
2485     if txt<>[] then
2486         txt22=[txt22;
2487         "    "+get_comment("update_xd",list())
2488         txt
2489         ""
2490         ]
2491     end
2492     txt22=[txt22;
2493     write_code_odoit(1) //** first pass
2494     write_code_odoit(0) //** second pass
2495     ]
2496
2497     if txt22<>[] then
2498         mputl(["  if (flag == 0) { "+get_comment("flag",list(flag))
2499         txt22
2500         "  }"], fd);
2501     end
2502
2503     //** flag 1,2,3
2504     for flag=[1,2,3]
2505
2506         txt3=[]
2507
2508         //** continuous time blocks must be activated
2509         //** for flag 1
2510         if flag==1 then
2511             txt = write_code_cdoit(flag);
2512
2513             if txt <> [] then
2514                 txt3=[txt3;
2515                 Indent+"  switch (nevprt) {"
2516                 ];
2517                 txt3=[txt3;
2518                 Indent2+"  case "+string(0)+" : "+...
2519                 get_comment("ev",list(0))
2520                 "    "+txt;
2521                 ];
2522                 txt3=[txt3;"      break;";""]
2523             end
2524         else
2525             txt=[];
2526         end
2527
2528         //** blocks with input discrete event must be activated
2529         //** for flag 1, 2 and 3
2530         if size(evs,2)>=1 then
2531             txt4=[]
2532             //**
2533             for ev=evs
2534                 txt2=write_code_doit(ev,flag);
2535                 if txt2<>[] then
2536                     //** adjust event number because of bidon block
2537                     new_ev=ev-(clkptr(howclk)-1)
2538                     //**
2539                     txt4=[txt4;
2540                     Indent2+["  case "+string(new_ev)+" : "+...
2541                     get_comment("ev",list(new_ev))
2542                     txt2];
2543                     "      break;";""]
2544                 end
2545             end
2546
2547             //**
2548             if txt == [] then
2549                 if txt4 <> [] then
2550                     txt3=[txt3;
2551                     Indent+"  switch (nevprt) {"
2552                     txt4
2553                     "    }"];
2554                 end
2555             else
2556                 txt3=[txt3;
2557                 txt4]
2558             end
2559         end
2560
2561         //**
2562         if txt <> [] then
2563             txt3=[txt3;
2564             "    }"];
2565         end
2566
2567         //**
2568         if txt3<>[] then
2569             if flag==1 & txt22==[] then
2570                 mputl(["  if (flag == "+string(flag)+") { "+...
2571                 get_comment("flag",list(flag))
2572                 txt3
2573                 "  }"], fd);
2574             else
2575                 mputl(["  else if (flag == "+string(flag)+") { "+...
2576                 get_comment("flag",list(flag))
2577                 txt3
2578                 "  }"], fd);
2579             end
2580         end
2581     end
2582
2583     //** flag 9
2584     ng=zcptr($)-1;
2585     if (ng ~= 0) then
2586         flag = 9;
2587         mputl(["  else if (flag == "+string(flag)+") { "+...
2588         get_comment("flag",list(flag))], fd);
2589
2590         txt=[]
2591         for kf=1:nblk
2592             if zcptr(kf+1)-zcptr(kf) <> 0 then
2593                 txt=[txt;
2594                 "    block_"+rdnom+"["+string(kf-1)+"].g="+...
2595                 "&(g["+string(zcptr(kf)-1)+"]);"]
2596             end
2597         end
2598
2599         mputl(["    "+get_comment("update_g",list())
2600         txt
2601         ""
2602         write_code_zdoit()
2603         ], fd);
2604
2605         mputl(["  }"], fd);
2606     end
2607
2608     //** flag 4
2609     mputl(["  else if (flag == 4) { "+get_comment("flag",list(4))
2610     "    if ((*block->work=scicos_malloc(sizeof(scicos_block)*"+...
2611     string(nblk)+"+sizeof(int)))== NULL) return 0;";
2612     "    reentryflag=(int*) ((scicos_block *)(*block->work)+"+string(nblk)+");"
2613     "    *reentryflag=0;"
2614     "    block_"+rdnom+"=(scicos_block*) *block->work;"], fd);
2615
2616     for kf=1:nblk
2617         nin=inpptr(kf+1)-inpptr(kf);  //* number of input ports */
2618         nout=outptr(kf+1)-outptr(kf); //* number of output ports */
2619         nx=xptr(kf+1)-xptr(kf);
2620         ng=zcptr(kf+1)-zcptr(kf);
2621         nmode=modptr(kf+1)-modptr(kf);
2622
2623         //** add comment
2624         mputl([""
2625         "    "+get_comment("set_blk",list(funs(kf),funtyp(kf),kf,labels(kf)));], fd);
2626
2627         mputl(["    block_"+rdnom+"["+string(kf-1)+"].type   = "+...
2628         string(funtyp(kf))+";";
2629         "    block_"+rdnom+"["+string(kf-1)+"].ztyp   = "+...
2630         string(ztyp(kf))+";";
2631         "    block_"+rdnom+"["+string(kf-1)+"].ng     = "+...
2632         string(zcptr(kf+1)-zcptr(kf))+";"], fd);
2633
2634         if nx <> 0 then
2635             mputl(["    block_"+rdnom+"["+string(kf-1)+"].nx     = "+...
2636             string(nx)+";";
2637             "    block_"+rdnom+"["+string(kf-1)+"].x      = &(x["+...
2638             string(xptr(kf)-1)+"]);"], fd);
2639         else
2640             mputl(["    block_"+rdnom+"["+string(kf-1)+"].nx     = 0;";
2641             "    block_"+rdnom+"["+string(kf-1)+"].x      = &(zero);"
2642             "    block_"+rdnom+"["+string(kf-1)+"].xd     = w;"], fd);
2643         end
2644
2645         if ng <> 0 then
2646             mputl(["    block_"+rdnom+"["+string(kf-1)+"].g      = &(g["+...
2647             string(zcptr(kf)-1)+"]);"], fd);
2648         else
2649             mputl(["    block_"+rdnom+"["+string(kf-1)+"].g      = &(zero);";], fd);
2650         end
2651         if nmode <> 0 then
2652             mputl(["    block_"+rdnom+"["+string(kf-1)+"].mode   = &(mode["+...
2653             string(modptr(kf)-1)+"]);"], fd);
2654         end
2655         mputl(["    block_"+rdnom+"["+string(kf-1)+"].nz     = "+...
2656         string(zptr(kf+1)-zptr(kf))+";";
2657         "    block_"+rdnom+"["+string(kf-1)+"].noz    = "+...
2658         string(ozptr(kf+1)-ozptr(kf))+";";
2659         "    block_"+rdnom+"["+string(kf-1)+"].nrpar  = "+...
2660         string(rpptr(kf+1)-rpptr(kf))+";";
2661         "    block_"+rdnom+"["+string(kf-1)+"].nopar  = "+...
2662         string(opptr(kf+1)-opptr(kf))+";";
2663         "    block_"+rdnom+"["+string(kf-1)+"].nipar  = "+...
2664         string(ipptr(kf+1)-ipptr(kf))+";"
2665         "    block_"+rdnom+"["+string(kf-1)+"].nin    = "+...
2666         string(inpptr(kf+1)-inpptr(kf))+";";
2667         "    block_"+rdnom+"["+string(kf-1)+"].nout   = "+...
2668         string(outptr(kf+1)-outptr(kf))+";";
2669         "    block_"+rdnom+"["+string(kf-1)+"].nevout = "+...
2670         string(clkptr(kf+1)-clkptr(kf))+";";
2671         "    block_"+rdnom+"["+string(kf-1)+"].nmode  = "+...
2672         string(modptr(kf+1)-modptr(kf))+";"], fd);
2673
2674         mputl(["    if ((block_"+rdnom+"["+string(kf-1)+"].evout  = "+...
2675         "calloc(block_"+rdnom+"["+string(kf-1)+"].nevout,sizeof(double)))== NULL) return 0;"], fd);
2676
2677         //***************************** input port *****************************//
2678         //** alloc insz/inptr **//
2679         mputl(["    if ((block_"+rdnom+"["+string(kf-1)+"].insz   = "+...
2680         "malloc(3*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nin))== NULL) return 0;";
2681         "    if ((block_"+rdnom+"["+string(kf-1)+"].inptr  = "+...
2682         "malloc(sizeof(void *)*block_"+rdnom+"["+string(kf-1)+"].nin))== NULL) return 0;"], fd);
2683
2684         //** inptr **//
2685         for k=1:nin
2686             lprt=inplnk(inpptr(kf)-1+k);
2687             mputl(["    block_"+rdnom+"["+string(kf-1)+"].inptr["+string(k-1)+"]  = "+...
2688             rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2689         end
2690
2691         //** 1st dim **//
2692         for k=1:nin
2693             lprt=inplnk(inpptr(kf)-1+k);
2694             mputl(["    block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1))+"]   = "+...
2695             string(size(outtb(lprt),1))+";"], fd);
2696         end
2697
2698         //** 2dn dim **//
2699         for k=1:nin
2700             lprt=inplnk(inpptr(kf)-1+k);
2701             mputl(["    block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1)+nin)+"]   = "+...
2702             string(size(outtb(lprt),2))+";"], fd);
2703         end
2704
2705         //** typ **//
2706         for k=1:nin
2707             lprt=inplnk(inpptr(kf)-1+k);
2708             mputl(["    block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1)+2*nin)+"]   = "+...
2709             mat2scs_c_typ(outtb(lprt))+";"], fd);
2710         end
2711         //**********************************************************************//
2712
2713         //***************************** output port *****************************//
2714         //** alloc outsz/outptr **//
2715         mputl(["    if ((block_"+rdnom+"["+string(kf-1)+"].outsz  = "+...
2716         "malloc(3*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nout))== NULL) return 0;";
2717         "    if ((block_"+rdnom+"["+string(kf-1)+"].outptr = "+...
2718         "malloc(sizeof(void*)*block_"+rdnom+"["+string(kf-1)+"].nout))== NULL) return 0;"], fd);
2719
2720         //** outptr **//
2721         for k=1:nout
2722             lprt=outlnk(outptr(kf)-1+k);
2723             mputl(["    block_"+rdnom+"["+string(kf-1)+"].outptr["+string(k-1)+"] = "+...
2724             rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2725         end
2726
2727         //** 1st dim **//
2728         for k=1:nout
2729             lprt=outlnk(outptr(kf)-1+k);
2730             mputl(["    block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1))+...
2731             "]  = "+string(size(outtb(lprt),1))+";"], fd);
2732         end
2733
2734         //** 2dn dim **//
2735         for k=1:nout
2736             lprt=outlnk(outptr(kf)-1+k);
2737             mputl(["    block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1)+nout)+...
2738             "]  = "+string(size(outtb(lprt),2))+";"], fd)
2739         end
2740
2741         //** typ **//
2742         for k=1:nout
2743             lprt=outlnk(outptr(kf)-1+k);
2744             mputl(["    block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1)+2*nout)+...
2745             "]  = "+mat2scs_c_typ(outtb(lprt))+";"], fd);
2746         end
2747         //**********************************************************************//
2748
2749         mputl(["    block_"+rdnom+"["+string(kf-1)+"].z         = &(z["+...
2750         string(zptr(kf)-1)+"]);"], fd);
2751
2752         //***************************** object state *****************************//
2753         if (ozptr(kf+1)-ozptr(kf)>0) then
2754             noz = ozptr(kf+1)-ozptr(kf);
2755             mputl(["    if ((block_"+rdnom+"["+string(kf-1)+"].ozptr = "+...
2756             "malloc(sizeof(void *)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";
2757             "    if ((block_"+rdnom+"["+string(kf-1)+"].ozsz  = "+...
2758             "malloc(2*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";
2759             "    if ((block_"+rdnom+"["+string(kf-1)+"].oztyp = "+...
2760             "malloc(sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";], fd);
2761
2762             //** ozptr **//
2763             for k=1:noz
2764                 mputl(["    block_"+rdnom+"["+string(kf-1)+"].ozptr["+string(k-1)+...
2765                 "]  = ozptr["+string(ozptr(kf)-1+k-1)+"];"], fd);
2766             end
2767
2768             //** 1st dim **//
2769             for k=1:noz
2770                 mputl(["    block_"+rdnom+"["+string(kf-1)+"].ozsz["+string(k-1)+...
2771                 "]   = "+string(size(oz(ozptr(kf)-1+k),1))+";"], fd);
2772             end
2773
2774             //** 2nd dim **//
2775             for k=1:noz
2776                 mputl(["    block_"+rdnom+"["+string(kf-1)+"].ozsz["+string(k-1)+...
2777                 "]   = "+string(size(oz(ozptr(kf)-1+k),2))+";"], fd);
2778             end
2779
2780             //** typ **//
2781             for k=1:noz
2782                 mputl(["    block_"+rdnom+"["+string(kf-1)+"].oztyp["+string(k-1)+...
2783                 "]  = "+mat2scs_c_typ(oz(ozptr(kf)-1+k))+";"], fd);
2784             end
2785         end
2786         //************************************************************************//
2787
2788         if (part(funs(kf),1:7) ~= "capteur" &...
2789             part(funs(kf),1:10) ~= "actionneur" &...
2790             funs(kf) ~= "bidon" &...
2791             funs(kf) ~= "bidon2") then
2792             if (rpptr(kf+1)-rpptr(kf)>0) then
2793                 mputl(["    block_"+rdnom+"["+string(kf-1)+...
2794                 "].rpar      = &(rpar["+string(rpptr(kf)-1)+"]);"], fd);
2795             end
2796             if (ipptr(kf+1)-ipptr(kf)>0) then
2797                 mputl(["    block_"+rdnom+"["+string(kf-1)+...
2798                 "].ipar      = &(ipar["+string(ipptr(kf)-1)+"]);"], fd);
2799             end
2800             //** opar
2801             if (opptr(kf+1)-opptr(kf)>0) then
2802                 mputl(["    if ((block_"+rdnom+"["+string(kf-1)+"].oparptr = "+...
2803                 "malloc(sizeof(void *)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL) return 0;";
2804                 "    if ((block_"+rdnom+"["+string(kf-1)+"].oparsz  = "+...
2805                 "malloc(2*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL) return 0;";
2806                 "    if ((block_"+rdnom+"["+string(kf-1)+"].opartyp = "+...
2807                 "malloc(sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL) return 0;";
2808                 ], fd);
2809                 nopar = opptr(kf+1)-opptr(kf);
2810                 //** oparptr **//
2811                 for k=1:nopar
2812                     mputl(["    block_"+rdnom+"["+string(kf-1)+"].oparptr["+string(k-1)+...
2813                     "]  = oparptr["+string(opptr(kf)-1+k-1)+"];"], fd);
2814                 end
2815                 //** 1st dim **//
2816                 for k=1:nopar
2817                     mputl(["    block_"+rdnom+"["+string(kf-1)+"].oparsz["+string(k-1)+...
2818                     "]   = "+string(size(opar(opptr(kf)-1+k),1))+";"], fd);
2819                 end
2820                 //** 2dn dim **//
2821                 for k=1:nopar
2822                     mputl(["    block_"+rdnom+"["+string(kf-1)+"].oparsz["+string(nopar+(k-1))+...
2823                     "]   = "+string(size(opar(opptr(kf)-1+k),2))+";"], fd)
2824                 end
2825                 //** typ **//
2826                 for k=1:nopar
2827                     mputl(["    block_"+rdnom+"["+string(kf-1)+"].opartyp["+string(k-1)+...
2828                     "]  = "+mat2scs_c_typ(opar(opptr(kf)-1+k))+";"], fd)
2829                 end
2830             end
2831         end
2832
2833         mputl(["    block_"+rdnom+"["+string(kf-1)+...
2834         "].work      = (void **)(((double *)work)+"+string(kf-1)+");"
2835         "    block_"+rdnom+"["+string(kf-1)+...
2836         "].nevprt    = nevprt;"], fd);
2837
2838     end //for kf=1:nblk
2839
2840     //** init
2841     for kf=1:nblk
2842         //     if or(kf==act) | or(kf==cap) then
2843         //       if stalone then
2844         //         txt = call_block42(kf,0,4);
2845         //         if txt <> [] then
2846         //           Code=[Code;
2847         //                 '';
2848         //                 '    '+txt];
2849         //         end
2850         //       end
2851         //     else
2852         txt = call_block42(kf,0,4);
2853         if txt <> [] then
2854             mputl(["";
2855             "    "+txt], fd);
2856         end
2857         //     end
2858     end
2859
2860     //** cst blocks and it's dep
2861     txt=write_code_idoit()
2862
2863     if txt<>[] then
2864         mputl([""
2865         "    /* initial blocks must be called with flag 1 */"
2866         txt], fd);
2867     end
2868     //   for kf=iord(:,1)'
2869     // //     if or(kf==act) then
2870     // //       if stalone then
2871     // //         txt = call_block42(kf,0,1);
2872     // //         if txt <> [] then
2873     // //           Code=[Code;
2874     // //                 '';
2875     // //                 '    '+txt];
2876     // //         end
2877     // //       end
2878     // //     else
2879     //       txt = call_block42(kf,0,1);
2880     //       if txt <> [] then
2881     //         Code=[Code;
2882     //               '';
2883     //               '    '+txt];
2884     //       end
2885     // //     end
2886     //   end
2887
2888     mputl(["  }"], fd);
2889
2890     //** flag 5
2891     mputl(["  else if (flag == 5) { "+get_comment("flag",list(5))
2892     "    block_"+rdnom+"=*block->work;"], fd);
2893
2894     for kf=1:nblk
2895         nin=inpptr(kf+1)-inpptr(kf);  //* number of input ports */
2896         nout=outptr(kf+1)-outptr(kf); //* number of output ports */
2897
2898         //** add comment
2899         txt=mputl([""
2900         "    "+get_comment("set_blk",list(funs(kf),funtyp(kf),kf,labels(kf)));], fd);
2901
2902         for k=1:nin
2903             lprt=inplnk(inpptr(kf)-1+k);
2904             mputl(["    block_"+rdnom+"["+string(kf-1)+"].inptr["+string(k-1)+...
2905             "]  = "+rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2906         end
2907         for k=1:nout
2908             lprt=outlnk(outptr(kf)-1+k);
2909             mputl(["    block_"+rdnom+"["+string(kf-1)+"].outptr["+string(k-1)+...
2910             "] = "+rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2911         end
2912         mputl(["    block_"+rdnom+"["+string(kf-1)+"].z=&(z["+...
2913         string(zptr(kf)-1)+"]);"], fd);
2914         if (part(funs(kf),1:7) ~= "capteur" &...
2915             part(funs(kf),1:10) ~= "actionneur" &...
2916             funs(kf) ~= "bidon" &...
2917             funs(kf) ~= "bidon2") then
2918             //** rpar **//
2919             if (rpptr(kf+1)-rpptr(kf)>0) then
2920                 mputl(["    block_"+rdnom+"["+string(kf-1)+...
2921                 "].rpar=&(rpar["+string(rpptr(kf)-1)+"]);"], fd);
2922             end
2923             //** ipar **//
2924             if (ipptr(kf+1)-ipptr(kf)>0) then
2925                 mputl(["    block_"+rdnom+"["+string(kf-1)+...
2926                 "].ipar=&(ipar["+string(ipptr(kf)-1)+"]);"], fd);
2927             end
2928             //** opar **//
2929             if (opptr(kf+1)-opptr(kf)>0) then
2930                 nopar = opptr(kf+1)-opptr(kf);
2931                 for k=1:nopar
2932                     mputl(["    block_"+rdnom+"["+string(kf-1)+"].oparptr["+string(k-1)+...
2933                     "]  = oparptr["+string(opptr(kf)-1+k-1)+"];"], fd);
2934                 end
2935             end
2936             //** oz **//
2937             if (ozptr(kf+1)-ozptr(kf)>0) then
2938                 noz = ozptr(kf+1)-ozptr(kf);
2939                 for k=1:noz
2940                     mputl(["    block_"+rdnom+"["+string(kf-1)+"].ozptr["+string(k-1)+...
2941                     "]  = ozptr["+string(ozptr(kf)-1+k-1)+"];"], fd);
2942                 end
2943             end
2944         end
2945
2946         mputl(["    block_"+rdnom+"["+string(kf-1)+...
2947         "].work=(void **)(((double *)work)+"+string(kf-1)+");"], fd)
2948     end
2949
2950     for kf=1:nblk
2951         //     if or(kf==act) | or(kf==cap) then
2952         //       if stalone then
2953         //         txt = call_block42(kf,0,5);
2954         //         if txt <> [] then
2955         //           Code=[Code;
2956         //                 '';
2957         //                 '    '+txt];
2958         //         end
2959         //       end
2960         //     else
2961         txt = call_block42(kf,0,5);
2962         if txt <> [] then
2963             mputl(["";
2964             "    "+txt], fd);
2965         end
2966         //     end
2967     end
2968
2969     mputl([""
2970     "    for (kf = 0; kf < "+string(nblk)+"; ++kf) {"
2971     "      if (block_"+rdnom+"[kf].insz!=NULL) {"
2972     "        free(block_"+rdnom+"[kf].insz);"
2973     "      }else {"
2974     "        break;"
2975     "      }"
2976     "      if (block_"+rdnom+"[kf].outsz!=NULL){"
2977     "        free(block_"+rdnom+"[kf].outsz);"
2978     "      }else {"
2979     "        break;"
2980     "      }"
2981     "      if (block_"+rdnom+"[kf].nopar!=0){"
2982     "        if (block_"+rdnom+"[kf].oparptr!=NULL){"
2983     "          free(block_"+rdnom+"[kf].oparptr);"
2984     "        }else {"
2985     "          break;"
2986     "        }"
2987     "        if (block_"+rdnom+"[kf].oparsz!=NULL){"
2988     "          free(block_"+rdnom+"[kf].oparsz);"
2989     "        }else {"
2990     "          break;"
2991     "        }"
2992     "        if (block_"+rdnom+"[kf].opartyp!=NULL){"
2993     "          free(block_"+rdnom+"[kf].opartyp);"
2994     "        }else {"
2995     "          break;"
2996     "        }"
2997     "      }"
2998     "      if (block_"+rdnom+"[kf].noz!=0){"
2999     "        if (block_"+rdnom+"[kf].ozptr!=NULL){"
3000     "          free(block_"+rdnom+"[kf].ozptr);"
3001     "        }else {"
3002     "          break;"
3003     "        }"
3004     "        if (block_"+rdnom+"[kf].ozsz!=NULL){"
3005     "          free(block_"+rdnom+"[kf].ozsz);"
3006     "        }else {"
3007     "          break;"
3008     "        }"
3009     "        if (block_"+rdnom+"[kf].oztyp!=NULL){"
3010     "          free(block_"+rdnom+"[kf].oztyp);"
3011     "        }else {"
3012     "          break;"
3013     "        }"
3014     "      }"
3015     "      if (block_"+rdnom+"[kf].evout!=NULL){"
3016     "        free(block_"+rdnom+"[kf].evout);"
3017     "      }else {"
3018     "        break;"
3019     "      }"
3020     "    }"
3021     "    scicos_free(block_"+rdnom+");"
3022     "  }"
3023     ""], fd);
3024
3025     for i=1:size(actt,1)
3026         ni=actt(i,3)*actt(i,4); // dimension of ith input
3027         if actt(i,5)<>11 then
3028             mputl(["  memcpy(*(y+"+string(actt(i,6)-1)+"),"+...
3029             "*("+rdnom+"_block_outtbptr+"+string(actt(i,2)-1)+"),"+...
3030             string(ni)+"*sizeof("+mat2c_typ(actt(i,5))+"));"], fd);
3031         else //** Cas cmplx
3032             mputl(["  memcpy(*(y+"+string(actt(i,6)-1)+"),"+...
3033             "*("+rdnom+"_block_outtbptr+"+string(actt(i,2)-1)+"),"+...
3034             string(2*ni)+"*sizeof("+mat2c_typ(actt(i,5))+"));"], fd);
3035         end
3036     end
3037
3038     //**
3039     mputl(["  return 0;"
3040     ""
3041     "} /* "+rdnom+" */"], fd);
3042
3043     mclose(fd);
3044
3045 endfunction
3046
3047 //generates skeleton of external world events handling function
3048 function Code=make_outevents()
3049     z="0"
3050     if szclkIN==[] then
3051         newszclkIN=0;
3052     else
3053         newszclkIN=szclkIN;
3054     end
3055
3056     Code=[ "/*"+part("-",ones(1,40))+"  External events handling function */";
3057     "void "+rdnom+"_events(int *nevprt,double *t)";
3058     "{"
3059     "/*  set next event time and associated events ports"
3060     " *  nevprt has binary expression b1..b"+string(newszclkIN)+" where bi is a bit"
3061     " *  bi is set to 1 if an activation is received by port i. Note that"
3062     " *  more than one activation can be received simultaneously"
3063     " *  Caution: at least one bi should be equal to one */"
3064     ""]
3065
3066     if (newszclkIN <> 0) then
3067         Code=[Code;
3068         "    int i,p,b[]={"+strcat(z(ones(1,newszclkIN)),",")+"};"
3069         ""
3070         "/* this is an example for the activation of events ports */"
3071         "    b[0]=1;"]
3072
3073         if newszclkIN>1 then
3074             for bb=2:newszclkIN
3075                 Code($+1)="    b["+string(bb-1)+"]=1;"
3076             end
3077         end
3078
3079         Code=[Code;
3080         ""
3081         "/* definition of the step time  */"
3082         "    *t = *t + 0.1;"
3083         ""
3084         "/* External events handling process */"
3085         "    *nevprt=0;p=1;"
3086         "    for (i=0;i<"+string(newszclkIN)+";i++) {"
3087         "      *nevprt=*nevprt+b[i]*p;"
3088         "      p=p*2;"
3089         "    }"
3090         "}"]
3091     else
3092         Code=[Code;
3093         "";
3094         "/* definition of the step time  */"
3095         "    *t = *t + 0.1;"
3096         "}"]
3097     end
3098 endfunction
3099
3100 //Generating the routine for sensors interfacing
3101 //
3102 //
3103 //Author : R. Djenidi, R. Nikoukhah, A. Layec
3104 //
3105 function Code=make_sensor(standalone)
3106     Call=["/*"+part("-",ones(1,40))+" Sensor */";
3107     "void "+rdnom+"_sensor(int *flag, int *nport, int *nevprt, double *t, void *y, int *ny1, int *ny2, int *yt, int *flag1)"]
3108
3109     comments=["     /*"
3110     "      * To be customized for standalone execution";
3111     "      * flag  : specifies the action to be done"
3112     "      * nport : specifies the  index of the Super Bloc"
3113     "      *         regular input (The input ports are numbered"
3114     "      *         from the top to the bottom )"
3115     "      * nevprt: indicates if an activation had been received"
3116     "      *         0 = no activation"
3117     "      *         1 = activation"
3118     "      * t     : the current time value"
3119     "      * y     : the vector outputs value"
3120     "      * ny1   : the output size 1"
3121     "      * ny2   : the output size 2"
3122     "      * yt    : the output type"
3123     "      * flag1 : learn mode (0 from terminal,1 from input file"
3124     "      */"]
3125
3126     dcl=["{"
3127     "  int j,k,l;"
3128     "  double temps;"]
3129
3130     if standalone then
3131
3132         a_sensor=["    switch (*flag) {"
3133         "    case 1 : /* set the output value */"
3134         "      printf(""Require outputs of sensor number %d\n"", *nport);"
3135         "      printf(""time is: %f\n"", *t);"
3136         "      printf(""sizes of the sensor output is: %d,%d\n"", *ny1,*ny2);"
3137         "      switch (*yt) {"
3138         "      case 10 :"
3139         "        printf(""type of the sensor output is: %d (double) \n"", *yt);"
3140         "        puts(""Please set the sensor output values"");"
3141         "        for (l=0;l<*ny2;l++) {"
3142         "          for (k=0;k<*ny1;k++) {"
3143         "            printf(""y(%d,%d) : "",k,l);"
3144         "            scanf(""%lf"", (double *) y+(k+l*(*ny1)));"
3145         "          }"
3146         "        }"
3147         "        break;"
3148         ""
3149         "      case 11 :"
3150         "        printf(""type of the sensor output is: %d (complex) \n"", *yt);"
3151         "        puts(""Please set the sensor output values"");"
3152         "        for (l=0;l<*ny2;l++) {"
3153         "          for (k=0;k<*ny1;k++) {"
3154         "            printf(""y(%d,%d) real part : "",k,l);"
3155         "            scanf(""%lf"", (double *) y+(k+l*(*ny1)));"
3156         "            printf(""y(%d,%d) imag part : "",k,l);"
3157         "            scanf(""%lf"", (double *) y+((*ny1)*(*ny2)+k+l*(*ny1)));"
3158         "          }"
3159         "        }"
3160         "        break;"
3161         ""
3162         "      case 81 :"
3163         "        printf(""type of the sensor output is: %d (char) \n"", *yt);"
3164         "        puts(""Please set the sensor output values"");"
3165         "        for (l=0;l<*ny2;l++) {"
3166         "          for (k=0;k<*ny1;k++) {"
3167         "            printf(""y(%d,%d) : "",k,l);"
3168         "            scanf(""%i"", (char *) y+(k+l*(*ny1)));"
3169         "          }"
3170         "        }"
3171         "        break;"
3172         ""
3173         "      case 82 :"
3174         "        printf(""type of the sensor output is: %d (char) \n"", *yt);"
3175         "        puts(""Please set the sensor output values"");"
3176         "        for (l=0;l<*ny2;l++) {"
3177         "          for (k=0;k<*ny1;k++) {"
3178         "            printf(""y(%d,%d) : "",k,l);"
3179         "            scanf(""%hd"", (short *) y+(k+l*(*ny1)));"
3180         "          }"
3181         "        }"
3182         "        break;"
3183         ""
3184         "      case 84 :"
3185         "        printf(""type of the sensor output is: %d (long) \n"", *yt);"
3186         "        puts(""Please set the sensor output values"");"
3187         "        for (l=0;l<*ny2;l++) {"
3188         "          for (k=0;k<*ny1;k++) {"
3189         "            printf(""y(%d,%d) : "",k,l);"
3190         "            scanf(""%ld"", (long *) y+(k+l*(*ny1)));"
3191         "          }"
3192         "        }"
3193         "        break;"
3194         ""
3195         "      case 811 :"
3196         "        printf(""type of the sensor output is: %d (unsigned char) \n"", *yt);"
3197         "        puts(""Please set the sensor output values"");"
3198         "        for (l=0;l<*ny2;l++) {"
3199         "          for (k=0;k<*ny1;k++) {"
3200         "            printf(""y(%d,%d) : "",k,l);"
3201         "            scanf(""%d"", (unsigned char *) y+(k+l*(*ny1)));"
3202         "          }"
3203         "        }"
3204         "        break;"
3205         ""
3206         "      case 812 :"
3207         "        printf(""type of the sensor output is: %d (unsigned short) \n"", *yt);"
3208         "        puts(""Please set the sensor output values"");"
3209         "        for (l=0;l<*ny2;l++) {"
3210         "          for (k=0;k<*ny1;k++) {"
3211         "            printf(""y(%d,%d) : "",k,l);"
3212         "            scanf(""%hu"", (unsigned short *) y+(k+l*(*ny1)));"
3213         "          }"
3214         "        }"
3215         "        break;"
3216         ""
3217         "      case 814 :"
3218         "        printf(""type of the sensor output is: %d (unsigned long) \n"", *yt);"
3219         "        puts(""Please set the sensor output values"");"
3220         "        for (l=0;l<*ny2;l++) {"
3221         "          for (k=0;k<*ny1;k++) {"
3222         "            printf(""y(%d,%d) : "",k,l);"
3223         "            scanf(""%lu"", (unsigned long *) y+(k+l*(*ny1)));"
3224         "          }"
3225         "        }"
3226         "        break;"
3227         ""
3228         "      }"
3229         "      break;"
3230         "    case 4 : /* sensor initialisation */"
3231         "      /* do whatever you want to initialize the sensor */"
3232         "      break;"
3233         "    case 5 : /* sensor ending */"
3234         "      /* do whatever you want to end the sensor */"
3235         "      break;"
3236         "    }"]
3237     else
3238         a_sensor=[]
3239     end
3240
3241     nc=size(cap,"*")
3242
3243     // pour fscanf
3244     typ=["""%lf "]; //temps
3245     for i=1:nc
3246         typ($)=typ($)+""" \"
3247         typ=[typ;""""];
3248         for j=1:sum(capt(i,3)*capt(i,4))
3249             //typ=typ+'%f ';
3250             typ($)=typ($)+scs_c_n2c_fmt(capt(i,5))+" ";
3251         end
3252     end
3253     typ($)=typ($)+"\n"", \"
3254     typ=strsubst(typ,"%f","%lf");
3255     typ(1)="    fscanf(fprr,"+typ(1);
3256     bl    ="                ";
3257     if size(typ,1) <> 1 then
3258         typ(2:$) = bl+typ(2:$);
3259     end
3260     //Code1=['      fscanf( fprr, '"'+typ+' \n'",&temps']
3261     Code1=[typ;bl+"&temps"];
3262     for i=1:size(capt,1)
3263         ni=capt(i,3)*capt(i,4); // dimension of ith input
3264         Code1($)=Code1($)+",";
3265         Code1=[Code1;bl];
3266         for j=1:ni
3267             if capt(i,5)<>11 then
3268                 Code1($)=Code1($)+...
3269                 "("+scs_c_n2c_typ(capt(i,5))+" *)("+...
3270                 rdnom+"_block_outtbptr+"+string(capt(i,2)-1)+")"+...
3271                 "+"+string(j-1)+"";
3272             else //CAS CMPLX
3273                 Code1($)=Code1($)+...
3274                 "("+scs_c_n2c_typ(capt(i,5))+" *)("+...
3275                 rdnom+"_block_outtbptr+"+string(capt(i,2)-1)+")"+...
3276                 "+"+string((j-1))+", "+...
3277                 "("+scs_c_n2c_typ(capt(i,5))+" *)("+...
3278                 rdnom+"_block_outtbptr+"+string(capt(i,2)-1)+")"+...
3279                 "+"+string(ni+(j-1))+"";
3280             end
3281             if j<>ni then
3282                 Code1($)=Code1($)+", ";
3283             end
3284         end
3285     end
3286     Code1($)=Code1($)+");"
3287
3288     Code=[]
3289     if nc==1|~standalone then
3290         Code=[Code;
3291         Call
3292         comments
3293         dcl
3294         "  if (flag1 == 0) {"
3295         a_sensor;
3296         "  } "
3297         "  else if (flag1 == 1) {"
3298         "    if (*flag == 4 && *nport == 1) {"
3299         "      fprr=fopen(input,'"r'");"
3300         "      if( fprr == NULL ) {"
3301         "        printf('"Error opening file: %s\n'", input);"
3302         "        return;"
3303         "      }"
3304         "    }"
3305         "    else if (*flag == 1) {"
3306         Code1
3307         "    }"
3308         "    else if (*flag == 5 && *nport == 1) {"
3309         "      fclose(fprr);"
3310         "    }"
3311         "  }"
3312         "}"];
3313
3314     elseif nc>1 then
3315         S="  switch (*nport) {"
3316         for k=1:nc
3317             S=[S;
3318             "  case "+string(k)+" : /* Port number "+string(k)+" ----------*/"
3319             "  "+a_sensor
3320             "  break;"]
3321         end
3322         S=[S;"  }"]
3323
3324         Code=[Code
3325         Call
3326         comments
3327         dcl
3328         "  if (flag1 == 0) {"
3329         S
3330         "  }"
3331         "  else if (flag1 == 1){"
3332         "    if (*flag == 4 && *nport == 1) {"
3333         "      fprr=fopen(input,'"r'");"
3334         "      if( fprr == NULL ) {"
3335         "        printf('"Error opening file: %s\n'", input);"
3336         "        return ;"
3337         "      }"
3338         "    }"
3339         "    else if (*flag == 1) {"
3340         Code1
3341         "    }"
3342         "    else if (*flag == 5 && *nport == 1) {"
3343         "      fclose(fprr);"
3344         "    }"
3345         "  }"
3346         "}"]
3347     end
3348 endfunction
3349
3350 //generates code of the standalone simulation procedure
3351 //
3352 //
3353 // rmq : La fonction zdoit n'est pas utilis?e pour le moment
3354 function make_standalone42(filename)
3355
3356     x=cpr.state.x;
3357     modptr=cpr.sim.modptr;
3358     rpptr=cpr.sim.rpptr;
3359     ipptr=cpr.sim.ipptr;
3360     opptr=cpr.sim.opptr;
3361     rpar=cpr.sim.rpar;
3362     ipar=cpr.sim.ipar;
3363     opar=cpr.sim.opar;
3364     oz=cpr.state.oz;
3365     ordptr=cpr.sim.ordptr;
3366     oord=cpr.sim.oord;
3367     zord=cpr.sim.zord;
3368     iord=cpr.sim.iord;
3369     tevts=cpr.state.tevts;
3370     evtspt=cpr.state.evtspt;
3371     zptr=cpr.sim.zptr;
3372     clkptr=cpr.sim.clkptr;
3373     ordptr=cpr.sim.ordptr;
3374     pointi=cpr.state.pointi;
3375     funs=cpr.sim.funs;
3376     noord=size(cpr.sim.oord,1);
3377     nzord=size(cpr.sim.zord,1);
3378     niord=size(cpr.sim.iord,1);
3379
3380     Indent="  ";
3381     Indent2=Indent+Indent;
3382     BigIndent="          ";
3383
3384     work=zeros(nblk,1)
3385     Z=[z;zeros(size(outtb),1);work]';
3386     nX=size(x,"*");
3387     nztotal=size(z,1);
3388
3389     stalone = %t;
3390
3391     fd = mopen(filename, "wt");
3392
3393     mputl(["/*"
3394     "**    Code prototype for standalone use"
3395     "**    Generated by Code_Generation toolbox of Xcos with "+getversion()
3396     "**    Date : "+date()
3397     "*/"
3398     ""
3399     "/* To learn how to use the standalone code, type '"./standalone -h'" */"
3400     ""
3401     "/* ---- Headers ---- */"
3402     "#include <stdio.h>"
3403     "#include <stdlib.h>"
3404     "#include <math.h>"
3405     "#include <string.h>"
3406     "#include <memory.h>"
3407     "#include '"scicos_block4.h'""
3408     "#include '"machine.h'""
3409     "#ifndef max"
3410     "#define max(a,b) ((a) >= (b) ? (a) : (b))"
3411     "#endif"
3412     "#ifndef min"
3413     "#define min(a,b) ((a) <= (b) ? (a) : (b))"
3414     "#endif"
3415     ""
3416     "/* ---- Internals functions declaration ---- */"
3417     "int "+rdnom+"_sim(double, double, double, int);"
3418     Protostalone
3419     ""], fd);
3420
3421
3422
3423     if x<>[] then
3424         mputl(["/* Code prototype for standalone use  */"
3425         "int C2F("+rdnom+"simblk)(double , double *, double *);"
3426         "int dset(int *n, double *dx, double *dy, int *incy);"
3427         "int ode1();"
3428         "int ode2();"
3429         "int ode4();"
3430         ""], fd);
3431     end
3432
3433     mputl(["/* Table of constant values */"
3434     "static int nrd_"+string(0:maxtotal)'+" = "+string(0:maxtotal)'+";"], fd);
3435
3436     if maxtotal<10 then
3437         mputl(["static int nrd_10 = 10;"], fd);
3438     end
3439     if maxtotal<11 then
3440         mputl(["static int nrd_11 = 11;"], fd)
3441     end
3442
3443     if maxtotal<81 then
3444         mputl(["static int nrd_81 = 81;"], fd);
3445     end
3446     if maxtotal<82 then
3447         mputl(["static int nrd_82 = 82;"], fd);
3448     end
3449     if maxtotal<84 then
3450         mputl(["static int nrd_84 = 84;"], fd);
3451     end
3452     if maxtotal<811 then
3453         mputl(["static int nrd_811 = 811;"], fd);
3454     end
3455     if maxtotal<812 then
3456         mputl(["static int nrd_812 = 812;"], fd);
3457     end
3458     if maxtotal<814 then
3459         mputl(["static int nrd_814 = 814;"], fd);
3460     end
3461
3462     mputl([""
3463     "/* Some general static variables */"
3464     "static double zero=0;"
3465     "static double w[1];"
3466     "static int aaa=0, bbb=0;"
3467     "void **"+rdnom+"_block_outtbptr;"], fd); //** !!
3468
3469     mputl([make_static_standalone42()], fd);
3470
3471     mputl(["static void usage();"
3472     ""
3473     "/* Main program */"
3474     "int main(int argc, char *argv[])"
3475     "{"
3476     "  double tf=30;"  // FIXME : Add current simulation tf.
3477     "  double dt=0.1;" // FIXME : Try to figure out what is the given Simulation step.
3478     "  double h=0.001;"
3479     "  int solver=3;"
3480     "  char * progname = argv[0];"
3481     "  int c;"
3482     "  strcpy(input,'"'");"
3483     "  strcpy(output,'"'");"
3484     "  while ((c = getopt(argc , argv, '"i:o:d:t:e:s:hv'")) != -1)"
3485     "    switch (c) {"
3486     "    case ''i'':"
3487     "      strcpy(input,argv[optind-1]);"
3488     "      break;"
3489     "    case ''o'':"
3490     "      strcpy(output,argv[optind-1]);"
3491     "      break;"
3492     "    case ''d'':"
3493     "      strcpy(s,argv[optind-1]);"
3494     "      dt=strtod(s,p);"
3495     "      break;"
3496     "    case ''t'':"
3497     "      strcpy(sf,argv[optind-1]);"
3498     "      tf=strtod(sf,p);"
3499     "      break;"
3500     "    case ''e'':"
3501     "      strcpy(se,argv[optind-1]);"
3502     "      h=strtod(se,p);"
3503     "      break;"
3504     "    case ''s'':"
3505     "      strcpy(ss,argv[optind-1]);"
3506     "      solver=(int) strtod(ss,p);"
3507     "      break;"
3508     "    case ''h'':"
3509     "      usage(progname);"
3510     "      printf(""Options : \n'");"
3511     "      printf(""     -h for the help  \n"");"
3512     "      printf(""     -v for printing the Scilab Version \n"");"
3513     "      printf(""     -i for input file name, by default is Terminal \n"");"
3514     "      printf(""     -o for output file name, by default is Terminal \n"");"
3515     "      printf(""     -d for the clock period, by default is 0.1 \n"");"
3516     "      printf(""     -t for the final time, by default is 30 \n"");"
3517     "      printf(""     -e for the solvers step size, by default is 0.001 \n"");"
3518     "      printf(""     -s integer parameter for select the numerical solver : \n"");"
3519     "      printf(""        1 for Euler''s method \n"");"
3520     "      printf(""        2 for Heun''s method \n"");"
3521     "      printf(""        3 (default value) for the Fourth-Order Runge-Kutta"+...
3522     " (RK4) Formula \n"");"
3523     "      return 0;"
3524     "      break;"
3525     "    case ''v'':"
3526     "      printf(""Generated by Code_Generation toolbox of Xcos """
3527     "             ""with "+getversion()+" version \n"");"
3528     "      return 0;"
3529     "      break;"
3530     "    case ''?'':"
3531     "      errflg++;"
3532     "      break;"
3533     "    }"
3534     "    if (errflg){"
3535     "      usage(progname);"
3536     "      return 0;"
3537     "    }"
3538     " "
3539     "  "+rdnom+"_sim(tf,dt,h,solver);"
3540     "  return 0;"
3541     "}"
3542     ""
3543     "static void usage(prog)"
3544     "       char *prog;"
3545     "{"
3546     "  fprintf(stderr, ""Usage: %s [-h] [-v] [-i arg] [-o arg] """
3547     "                  ""[-d arg] [-t arg] [-e arg] [-s arg]\n"", prog);"
3548     "}"
3549     ""], fd);
3550
3551     mputl(["/*"+part("-",ones(1,40))+"  External simulation function */"
3552     "int "+rdnom+"_sim(tf,dt,h,solver)"
3553     ""
3554     "   double tf,dt,h;"
3555     "   int solver;"
3556     "{"
3557     "  double t;"
3558     "  int nevprt=1;"
3559     "  int local_flag;"
3560     "  int nport;"
3561     "  int i;"
3562     "  double *args[100];"
3563     ""], fd);
3564
3565     if (x <> []) then
3566         mputl(["  double tout;"
3567         "  double he=0.1;"
3568         ""], fd);
3569     end
3570
3571     if isempty(z) then
3572         Cz = "";
3573     else
3574         Cz = strcat(string(z),",");
3575     end
3576     if isempty(work) then
3577         Cwork = "";
3578     else
3579         Cwork = strcat(string(work),",");
3580     end
3581     if isempty(Z) then
3582         CZ = "";
3583     else
3584         CZ = strcat(string(Z),",");
3585     end
3586
3587     mputl(["  /* Initial values */"
3588     ""
3589     "  /* Note that z[]=[z_initial_condition;outtbptr;work]"
3590     cformatline("     z_initial_condition={"+Cz+"};",70)
3591     cformatline("     outtbptr={"+strcat(string(zeros(size(outtb),1)),"," )+"};",70)
3592     cformatline("     work={"+Cwork+"};",70)
3593     "  */"
3594     ""
3595     cformatline("  double z[]={"+CZ+"};",70)], fd);
3596
3597     if size(z,1) <> 0 then
3598         for i=1:(length(zptr)-1)
3599             if zptr(i+1)-zptr(i)>0 then
3600                 if size(corinv(i),"*")==1 then
3601                     OO=scs_m.objs(corinv(i))
3602                 else
3603                     path=list("objs")
3604                     for l=cpr.corinv(i)(1:$-1)
3605                         path($+1)=l;path($+1)="model"
3606                         path($+1)="rpar"
3607                         path($+1)="objs"
3608                     end
3609                     path($+1)=cpr.corinv(i)($)
3610                     OO=scs_m(path)
3611                 end
3612                 aaa=OO.gui
3613                 bbb=emptystr(3,1);
3614                 if and(aaa+bbb~=["INPUTPORTEVTS";"OUTPUTPORTEVTS";"EVTGEN_f"]) then
3615                     mputl(["";
3616                     "  /* Routine name of block: "+strcat(string(cpr.sim.funs(i)));
3617                     "     Gui name of block: "+strcat(string(OO.gui));
3618                     //'/* Name block: '+strcat(string(cpr.sim.funs(i)));
3619                     //'Object number in diagram: '+strcat(string(cpr.corinv(i)));
3620                     "     Compiled structure index: "+strcat(string(i));], fd);
3621                     if stripblanks(OO.model.label)~=emptystr() then
3622                         mputl([cformatline("     Label: "+strcat(string(OO.model.label)),70)], fd);
3623                     end
3624                     if stripblanks(OO.graphics.exprs(1))~=emptystr() then
3625                         mputl([cformatline("     Exprs: "+strcat(OO.graphics.exprs(1),","),70)], fd);
3626                     end
3627                     if stripblanks(OO.graphics.id)~=emptystr() then
3628                         mputl([cformatline("     Identification: "+..
3629                         strcat(string(OO.graphics.id)),70)], fd);
3630                     end
3631                     mputl([cformatline("     z={"+...
3632                     strcat(string(z(zptr(i):zptr(i+1)-1)),",")+"};",70)
3633                     " */"], fd);
3634                 end
3635             end
3636         end
3637     end
3638
3639     //** declaration of oz
3640     Code_oz = [];
3641     for i=1:size(oz)
3642         if mat2scs_c_nb(oz(i)) <> 11 then
3643             Code_oz=[Code_oz;
3644             cformatline("  "+mat2c_typ(oz(i))+...
3645             " oz_"+string(i)+"[]={"+...
3646             strcat(string(oz(i)(:)),",")+"};",70)]
3647         else //** cmplx test
3648             Code_oz=[Code_oz;
3649             cformatline("  "+mat2c_typ(oz(i))+...
3650             " oz_"+string(i)+"[]={"+...
3651             strcat(string([real(oz(i)(:));
3652             imag(oz(i)(:))]),",")+"};",70)]
3653         end
3654     end
3655
3656     if Code_oz <> [] then
3657         mputl(["  /* oz declaration */"
3658         Code_oz], fd);
3659     end
3660
3661     //** declaration of outtb
3662     Code_outtb = [];
3663     for i=1:size(outtb)
3664         if mat2scs_c_nb(outtb(i)) <> 11 then
3665             Code_outtb=[Code_outtb;
3666             cformatline("  "+mat2c_typ(outtb(i))+...
3667             " outtb_"+string(i)+"[]={"+...
3668             strcat(string(outtb(i)(:)),",")+"};",70)]
3669         else //** cmplx test
3670             Code_outtb=[Code_outtb;
3671             cformatline("  "+mat2c_typ(outtb(i))+...
3672             " outtb_"+string(i)+"[]={"+...
3673             strcat(string([real(outtb(i)(:));
3674             imag(outtb(i)(:))]),",")+"};",70)]
3675         end
3676     end
3677
3678     if Code_outtb<>[] then
3679         mputl([""
3680         "  /* outtb declaration */"
3681         Code_outtb
3682         ""], fd);
3683     end
3684
3685     Code_outtbptr=[];
3686     for i=1:size(outtb)
3687         Code_outtbptr=[Code_outtbptr;
3688         "  "+rdnom+"_block_outtbptr["+...
3689         string(i-1)+"] = (void *) outtb_"+string(i)+";"];
3690     end
3691
3692     //** declaration of work
3693     mputl(["  /* work ptr declaration */"
3694     "  void **work;"
3695     ""
3696     //## affectation of work
3697     "  /* Get work ptr of blocks */"
3698     "  work = (void **)(z+"+string(size(z,"*")+size(outtb))+");"
3699     ""], fd);
3700
3701     //## affection of outtbptr
3702     if Code_outtb<>[] then
3703         mputl(["  /* Get outtbptr ptr of blocks */"
3704         "  "+rdnom+"_block_outtbptr = (void **)(z+"+string(nztotal)+");"
3705         ""], fd);
3706     end
3707     if Code_outtbptr<>[] then
3708         mputl([Code_outtbptr
3709         ""], fd);
3710     end
3711
3712     for kf=1:nblk
3713         nx=xptr(kf+1)-xptr(kf);       //** number of continuous state
3714         nin=inpptr(kf+1)-inpptr(kf);  //** number of input ports
3715         nout=outptr(kf+1)-outptr(kf); //** number of output ports
3716
3717         //** add comment
3718         mputl([""
3719         "  "+get_comment("set_blk",list(funs(kf),funtyp(kf),kf,labels(kf)));], fd);
3720
3721         mputl(["  block_"+rdnom+"["+string(kf-1)+"].type   = "+string(funtyp(kf))+";";
3722         "  block_"+rdnom+"["+string(kf-1)+"].ztyp   = "+string(ztyp(kf))+";";
3723         "  block_"+rdnom+"["+string(kf-1)+"].ng     = "+string(zcptr(kf+1)-zcptr(kf))+";";
3724         "  block_"+rdnom+"["+string(kf-1)+"].nz     = "+string(zptr(kf+1)-zptr(kf))+";";
3725         "  block_"+rdnom+"["+string(kf-1)+"].noz    = "+string(ozptr(kf+1)-ozptr(kf))+";";
3726         "  block_"+rdnom+"["+string(kf-1)+"].nrpar  = "+string(rpptr(kf+1)-rpptr(kf))+";";
3727         "  block_"+rdnom+"["+string(kf-1)+"].nopar  = "+string(opptr(kf+1)-opptr(kf))+";";
3728         "  block_"+rdnom+"["+string(kf-1)+"].nipar  = "+string(ipptr(kf+1)-ipptr(kf))+";"
3729         "  block_"+rdnom+"["+string(kf-1)+"].nin    = "+string(inpptr(kf+1)-inpptr(kf))+";";
3730         "  block_"+rdnom+"["+string(kf-1)+"].nout   = "+string(outptr(kf+1)-outptr(kf))+";";
3731         "  block_"+rdnom+"["+string(kf-1)+"].nevout = "+string(clkptr(kf+1)-clkptr(kf))+";";
3732         "  block_"+rdnom+"["+string(kf-1)+"].nmode  = "+string(modptr(kf+1)-modptr(kf))+";";], fd);
3733
3734         if nx <> 0 then
3735             mputl(["  block_"+rdnom+"["+string(kf-1)+"].nx = "+string(nx)+";";
3736             "  block_"+rdnom+"["+string(kf-1)+"].x  = &(x["+string(xptr(kf)-1)+"]);"
3737             "  block_"+rdnom+"["+string(kf-1)+"].xd = &(xd["+string(xptr(kf)-1)+"]);"], fd);
3738         end
3739
3740         mputl(["  if ((block_"+rdnom+"["+string(kf-1)+"].evout  = "+...
3741         "calloc(block_"+rdnom+"["+string(kf-1)+"].nevout,sizeof(double)))== NULL) return 0;"], fd);
3742
3743         //***************************** input port *****************************//
3744         //** alloc insz/inptr **//
3745         mputl(["  if ((block_"+rdnom+"["+string(kf-1)+"].inptr  = "+...
3746         "malloc(sizeof(double*)*block_"+rdnom+"["+string(kf-1)+"].nin))== NULL) return 0;";
3747         "  if ((block_"+rdnom+"["+string(kf-1)+"].insz   = "+...
3748         "malloc(3*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nin))== NULL) return 0;"], fd);
3749
3750         //** inptr **//
3751         for k=1:nin
3752             lprt=inplnk(inpptr(kf)-1+k);
3753             mputl(["  block_"+rdnom+"["+string(kf-1)+"].inptr["+string(k-1)+...
3754             "]  = "+rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
3755         end
3756
3757         //** 1st dim **//
3758         for k=1:nin
3759             lprt=inplnk(inpptr(kf)-1+k);
3760             mputl(["  block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1))+...
3761             "]   = "+string(size(outtb(lprt),1))+";"], fd);
3762         end
3763
3764         //** 2dn dim **//
3765         for k=1:nin
3766             lprt=inplnk(inpptr(kf)-1+k);
3767             mputl(["  block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1)+nin)+...
3768             "]   = "+string(size(outtb(lprt),2))+";"], fd);
3769         end
3770
3771         //** typ **//
3772         for k=1:nin
3773             lprt=inplnk(inpptr(kf)-1+k);
3774             mputl(["  block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1)+2*nin)+...
3775             "]   = "+mat2scs_c_typ(outtb(lprt))+";"], fd);
3776         end
3777         //**********************************************************************//
3778
3779         //***************************** output port *****************************//
3780         //** alloc outsz/outptr **//
3781         mputl(["  if ((block_"+rdnom+"["+string(kf-1)+"].outsz  = "+...
3782         "malloc(3*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nout))== NULL) return 0;";
3783         "  if ((block_"+rdnom+"["+string(kf-1)+"].outptr = "+...
3784         "malloc(sizeof(double*)*block_"+rdnom+"["+string(kf-1)+"].nout))== NULL) return 0;"], fd);
3785
3786         //** outptr **//
3787         for k=1:nout
3788             lprt=outlnk(outptr(kf)-1+k);
3789             mputl(["  block_"+rdnom+"["+string(kf-1)+"].outptr["+string(k-1)+...
3790             "] = "+rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
3791         end
3792
3793         //** 1st dim **//
3794         for k=1:nout
3795             lprt=outlnk(outptr(kf)-1+k);
3796             mputl(["  block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1))+...
3797             "]  = "+string(size(outtb(lprt),1))+";"], fd);
3798         end
3799
3800         //** 2dn dim **//
3801         for k=1:nout
3802             lprt=outlnk(outptr(kf)-1+k);
3803             mputl(["  block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1)+nout)+...
3804             "]  = "+string(size(outtb(lprt),2))+";"], fd);
3805         end
3806
3807         //** typ **//
3808         for k=1:nout
3809             lprt=outlnk(outptr(kf)-1+k);
3810             mputl(["  block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1)+2*nout)+...
3811             "]  = "+mat2scs_c_typ(outtb(lprt))+";"], fd);
3812         end
3813         //**********************************************************************//
3814
3815         mputl(["  block_"+rdnom+"["+string(kf-1)+...
3816         "].z = &(z["+string(zptr(kf)-1)+"]);"], fd);
3817
3818         if (part(funs(kf),1:7) ~= "capteur" &...
3819             part(funs(kf),1:10) ~= "actionneur" &...
3820             funs(kf) ~= "bidon") then
3821             //** rpar **//
3822             if (rpptr(kf+1)-rpptr(kf)>0) then
3823                 mputl([ "  block_"+rdnom+"["+string(kf-1)+...
3824                 "].rpar=&(RPAR1["+string(rpptr(kf)-1)+"]);"], fd);
3825             end
3826             //** ipar **//
3827             if (ipptr(kf+1)-ipptr(kf)>0) then
3828                 mputl(["  block_"+rdnom+"["+string(kf-1)+...
3829                 "].ipar=&(IPAR1["+string(ipptr(kf)-1)+"]);"], fd);
3830             end
3831             //** opar **//
3832             if (opptr(kf+1)-opptr(kf)>0) then
3833                 mputl(["  if ((block_"+rdnom+"["+string(kf-1)+"].oparptr = "+...
3834                 "malloc(sizeof(void *)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL ) return 0;";
3835                 "  if ((block_"+rdnom+"["+string(kf-1)+"].oparsz  = "+...
3836                 "malloc(2*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL ) return 0;";
3837                 "  if ((block_"+rdnom+"["+string(kf-1)+"].opartyp = "+...
3838                 "malloc(sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL ) return 0;";
3839                 ], fd);
3840                 nopar = opptr(kf+1)-opptr(kf);
3841                 //** oparptr **//
3842                 for k=1:nopar
3843                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].oparptr["+string(k-1)+...
3844                     "]   = (void *) OPAR_"+string(opptr(kf)-1+k)+";"], fd);
3845                 end
3846                 //** 1st dim **//
3847                 for k=1:nopar
3848                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].oparsz["+string(k-1)+...
3849                     "]    = "+string(size(opar(opptr(kf)-1+k),1))+";"], fd);
3850                 end
3851                 //** 2dn dim **//
3852                 for k=1:nopar
3853                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].oparsz["+string(nopar+(k-1))+...
3854                     "]    = "+string(size(opar(opptr(kf)-1+k),2))+";"], fd);
3855                 end
3856                 //** typ **//
3857                 for k=1:nopar
3858                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].opartyp["+string(k-1)+...
3859                     "]   = "+mat2scs_c_typ(opar(opptr(kf)-1+k))+";"], fd);
3860                 end
3861             end
3862             //** oz **//
3863             if (ozptr(kf+1)-ozptr(kf)>0) then
3864                 noz = ozptr(kf+1)-ozptr(kf);
3865                 mputl(["  if ((block_"+rdnom+"["+string(kf-1)+"].ozptr = "+...
3866                 "malloc(sizeof(void *)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";
3867                 "  if ((block_"+rdnom+"["+string(kf-1)+"].ozsz  = "+...
3868                 "malloc(2*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";
3869                 "  if ((block_"+rdnom+"["+string(kf-1)+"].oztyp = "+...
3870                 "malloc(sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";], fd);
3871                 //** ozptr **//
3872                 for k=1:noz
3873                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].ozptr["+string(k-1)+...
3874                     "]  = (void *) oz_"+string(ozptr(kf)-1+k)+";"], fd);
3875                 end
3876                 //** 1st dim **//
3877                 for k=1:noz
3878                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].ozsz["+string(k-1)+...
3879                     "]  = "+string(size(oz(ozptr(kf)-1+k),1))+";"], fd);
3880                 end
3881                 //** 2nd dim **//
3882                 for k=1:noz
3883                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].ozsz["+string(noz+(k-1))+...
3884                     "]  = "+string(size(oz(ozptr(kf)-1+k),2))+";"], fd);
3885                 end
3886                 //** typ **//
3887                 for k=1:noz
3888                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].oztyp["+string(k-1)+...
3889                     "]  = "+mat2scs_c_typ(oz(ozptr(kf)-1+k))+";"], fd);
3890                 end
3891             end
3892         end
3893         mputl(["  block_"+rdnom+"["+string(kf-1)+"].work = "+...
3894         "(void **)(((double *)work)+"+string(kf-1)+");"], fd);
3895     end
3896
3897     mputl(["  if (strlen(input) > 0)"
3898     "    aaa=1;"
3899     "  if (strlen(output)> 0)"
3900     "    bbb=1;"
3901     ""
3902     "  /* set initial time */"
3903     "  t=0.0;"
3904     ""], fd);
3905
3906     //** init
3907     mputl(["  "+get_comment("flag",list(4))], fd);
3908
3909     for kf=1:nblk
3910         if or(kf==act) | or(kf==cap) then
3911             txt = call_block42(kf,0,4);
3912             if txt <> [] then
3913                 mputl(["";
3914                 "  "+txt], fd);
3915             end
3916         else
3917             txt = call_block42(kf,0,4);
3918             if txt <> [] then
3919                 mputl(["";
3920                 "  "+txt], fd);
3921             end
3922         end
3923     end
3924
3925     //** cst blocks and it's dep
3926     txt=write_code_idoit()
3927
3928     if txt<>[] then
3929         mputl([""
3930         "    /* Initial blocks must be called with flag 1 */"
3931         txt], fd);
3932     end
3933
3934     //** begin input main loop on time
3935     mputl([""
3936     "  while (t < tf) {";
3937     "    /* */"
3938     "    sci_time=t;"
3939     ""], fd);
3940
3941     //** find source activation number
3942     blks=find(funtyp>-1);
3943     evs=[];
3944
3945     for blk=blks
3946         for ev=clkptr(blk):clkptr(blk+1)-1
3947             if funs(blk)=="bidon" then
3948                 if ev > clkptr(howclk) -1
3949                     evs=[evs,ev];
3950                 end
3951             end
3952         end
3953     end
3954
3955     //** flag 1,2,3
3956     for flag=[1,2,3]
3957
3958         txt3=[]
3959
3960         //** continuous time blocks must be activated
3961         //** for flag 1
3962         if flag==1 then
3963             txt = write_code_cdoit(flag);
3964
3965             if txt <> [] then
3966                 txt3=[""
3967                 "    "+get_comment("ev",list(0))
3968                 txt;
3969                 ];
3970             end
3971         end
3972
3973         //** blocks with input discrete event must be activated
3974         //** for flag 1, 2 and 3
3975         if size(evs,2)>=1 then
3976             txt4=[]
3977             //**
3978             for ev=evs
3979                 txt2=write_code_doit(ev,flag);
3980                 if txt2<>[] then
3981                     //** adjust event number because of bidon block
3982                     new_ev=ev-(clkptr(howclk)-1)
3983                     //**
3984                     txt4=[txt4;
3985                     Indent2+["  case "+string(new_ev)+" : "+...
3986                     get_comment("ev",list(new_ev))
3987                     txt2];
3988                     "      break;";""]
3989                 end
3990             end
3991
3992             //**
3993             if txt4 <> [] then
3994                 txt3=[txt3;
3995                 Indent+"  /* Discrete activations */"
3996                 Indent+"  switch (nevprt) {"
3997                 txt4
3998                 "    }"];
3999             end
4000         end
4001
4002         //**
4003         if txt3<>[] then
4004             mputl(["    "+get_comment("flag",list(flag))
4005             txt3], fd);
4006         end
4007     end
4008
4009     if x<>[] then
4010         mputl([""
4011         "    tout=t;"
4012         ""
4013         "   /* integrate until the cumulative add of the integration"
4014         "    * time step doesn''t cross the sample time step"
4015         "    */"
4016         "    while (tout+h<t+dt){"
4017         "      switch (solver) {"
4018         "      case 1:"
4019         "        ode1(C2F("+rdnom+"simblk),tout,h);"
4020         "        break;"
4021         "      case 2:"
4022         "        ode2(C2F("+rdnom+"simblk),tout,h);"
4023         "        break;"
4024         "      case 3:"
4025         "        ode4(C2F("+rdnom+"simblk),tout,h);"
4026         "        break;"
4027         "      default :"
4028         "        ode4(C2F("+rdnom+"simblk),tout,h);"
4029         "        break;"
4030         "        }"
4031         "       tout=tout+h;"
4032         "    }"
4033         ""
4034         "    /* integration for the remainder piece of time */"
4035         "    he=t+dt-tout;"
4036         "    switch (solver) {"
4037         "    case 1:"
4038         "      ode1(C2F("+rdnom+"simblk),tout,he);"
4039         "      break;"
4040         "    case 2:"
4041         "      ode2(C2F("+rdnom+"simblk),tout,he);"
4042         "      break;"
4043         "    case 3:"
4044         "      ode4(C2F("+rdnom+"simblk),tout,he);"
4045         "      break;"
4046         "    default :"
4047         "      ode4(C2F("+rdnom+"simblk),tout,he);"
4048         "      break;"
4049         "    }"], fd);
4050     end
4051
4052     //** fix bug provided by Roberto Bucher
4053     //** Alan, 13/10/07
4054     if nX <> 0 then
4055         mputl([""
4056         "    /* update ptrs of continuous array */"], fd);
4057         for kf=1:nblk
4058             nx=xptr(kf+1)-xptr(kf);  //** number of continuous state
4059             if nx<>0 then
4060                 mputl(["    block_"+rdnom+"["+string(kf-1)+"].nx = "+...
4061                 string(nx)+";";
4062                 "    block_"+rdnom+"["+string(kf-1)+"].x  = "+...
4063                 "&(x["+string(xptr(kf)-1)+"]);"
4064                 "    block_"+rdnom+"["+string(kf-1)+"].xd = "+...
4065                 "&(xd["+string(xptr(kf)-1)+"]);"], fd);
4066             end
4067         end
4068     end
4069
4070     mputl([""
4071     "    /* update current time */"
4072     "    t=t+dt;"
4073     "  }"
4074     //** flag 5
4075     ""
4076     " sci_time = tf;"
4077     ""
4078     "  "+get_comment("flag",list(5))], fd);
4079
4080     for kf=1:nblk
4081         if or(kf==act) | or(kf==cap) then
4082             txt = call_block42(kf,0,5);
4083             if txt <> [] then
4084                 mputl(["";
4085                 "  "+txt], fd);
4086             end
4087         else
4088             txt = call_block42(kf,0,5);
4089             if txt <> [] then
4090                 mputl(["";
4091                 "  "+txt], fd);
4092             end
4093         end
4094     end
4095
4096     mputl([""
4097     "  return 0;"
4098     "}"
4099     ""
4100     "/*"+part("-",ones(1,40))+"  Lapack messag function */";
4101     "void C2F(xerbla)(char *SRNAME,int *INFO,long int L)"
4102     "{"
4103     "  printf(""** On entry to %s, parameter number %d"""
4104     "         ""  had an illegal value\n"",SRNAME,*INFO);"
4105     "}"
4106     ""
4107     "void set_block_error(int err)"
4108     "{"
4109     "  return;"
4110     "}"
4111     ""
4112     "int get_phase_simulation()"
4113     "{"
4114     "  return 1;"
4115     "}"
4116     ""
4117     "void * scicos_malloc(size_t size)"
4118     "{"
4119     "  return malloc(size);"
4120     "}"
4121     ""
4122     "void scicos_free(void *p)"
4123     "{"
4124     "  free(p);"
4125     "}"
4126     ""
4127     "double get_scicos_time()"
4128     "{"
4129     "  return sci_time;"
4130     "}"
4131     ""
4132     "void do_cold_restart()"
4133     "{"
4134     "  return;"
4135     "}"
4136     ""
4137     "void sciprint (char *fmt)"
4138     "{"
4139     "  return;"
4140     "}"
4141     ""
4142     "int getopt (int argc, char *argv[], char *optstring)"
4143     "{"
4144     "  char *group, option, *sopt;"
4145     "  int len;"
4146     "  option = -1;"
4147     "  optarg = NULL;"
4148     "  while ( optind < argc )"
4149     "    { "
4150     "      group = argv[optind];"
4151     "      if ( *group != ''-'' )"
4152     "        {"
4153     "         option = -1;"
4154     "         optarg = group;"
4155     "         optind++;"
4156     "         break;"
4157     "        }"
4158     "      len = strlen (group);"
4159     "      group = group + offset;"
4160     "      if ( *group == ''-'' )"
4161     "        {"
4162     "         group++;"
4163     "         offset += 2;"
4164     "        }"
4165     "      else"
4166     "        offset++ ;"
4167     "      option = *group ;"
4168     "      sopt = strchr ( optstring, option ) ;"
4169     "      if ( sopt != NULL )"
4170     "        {"
4171     "         sopt++ ;"
4172     "         if ( *sopt == '':'' )"
4173     "           {"
4174     "             optarg = group + 1;"
4175     "             if ( *optarg == ''\0'' )"
4176     "                optarg = argv[++optind];"
4177     "             if ( *optarg == ''-'' )"
4178     "                {"
4179     "                 fprintf ( stderr, '"\n%s: illegal option -- %c'","
4180     "                           argv[0], option );"
4181     "                 option = ''?'';"
4182     "                 break;"
4183     "                }"
4184     "             else"
4185     "                {"
4186     "                 optind++;"
4187     "                 offset = 0;"
4188     "                 break;"
4189     "                }"
4190     "           }"
4191     "         if ( offset >= len )"
4192     "           {"
4193     "             optind++;"
4194     "             offset = 0;"
4195     "           }"
4196     "         break;"
4197     "        }"
4198     "      else"
4199     "        {"
4200     "         fprintf ( stderr, '"\n%s: illegal option -- %c'", argv[0], option );"
4201     "         option = ''?'';"
4202     "         break;"
4203     "        }"
4204     "    }"
4205     "  return ( option );"
4206     "}"
4207     ""], fd);
4208
4209     if (x <> []) then
4210         mputl(["int C2F("+rdnom+"simblk)(t, x, xd)"
4211         ""
4212         "   double t, *x, *xd;"
4213         ""
4214         "     /*"
4215         "      *  !purpose"
4216         "      *  compute state derivative of the continuous part"
4217         "      *  !syntax"
4218         "      *  neq   : integer the size of the  continuous state"
4219         "      *  t     : current time"
4220         "      *  x     : double precision vector whose contains the continuous state"
4221         "      *  xd    : double precision vector, contain the computed derivative"
4222         "      *  of the state"
4223         "      */"
4224         "{"
4225         "  int phase=2;"
4226         "  int local_flag;"
4227         "  int nport;"
4228         "  int nevprt=1;"
4229         "  double *args[100];"
4230         "  dset(&neq, &c_b14,xd , &c__1);"
4231         ""
4232         "    "+get_comment("update_xd",list())], fd);
4233
4234         for kf=1:nblk
4235             if (xptr(kf+1)-xptr(kf)) > 0 then
4236                 mputl(["    block_"+rdnom+"["+string(kf-1)+"].x="+...
4237                 "&(x["+string(xptr(kf)-1)+"]);"
4238                 "    block_"+rdnom+"["+string(kf-1)+"].xd="+...
4239                 "&(xd["+string(xptr(kf)-1)+"]);"], fd);
4240             end
4241         end
4242
4243         mputl([""
4244         write_code_odoit(1)
4245         write_code_odoit(0)
4246         ""
4247         "  return 0;"
4248         "}"
4249         ""
4250         "int dset(int *n, double *dx, double *dy, int *incy)"
4251         "{"
4252         "    int iMax = 0;"
4253         "    int i = 0, iy = 0;"
4254         "    --dy;"
4255         "    if (*n <= 0) return 0;"
4256         "    iy = 1;"
4257         "    if (*incy < 0)"
4258         "    {"
4259         "      iy = (-(*n) + 1) * *incy + 1;"
4260         "    }"
4261         "    iMax = *n;"
4262         "    for (i = 1; i <= iMax; ++i)"
4263         "    {"
4264         "      dy[iy] = *dx;"
4265         "      iy += *incy;"
4266         "    }"
4267         "    return 0;"
4268         "}"
4269         ""
4270         "/* Euler''s Method */"
4271         "int ode1(f,t,h)"
4272         "  int (*f) ();"
4273         "  double t, h;"
4274         "{"
4275         "  int i;"
4276         ""
4277         "  /**/"
4278         "  (*f)(t,x, xd);"
4279         ""
4280         "  for (i=0;i<neq;i++) {"
4281         "   x[i]=x[i]+h*xd[i];"
4282         "  }"
4283         ""
4284         "  return 0;"
4285         "}"
4286         ""
4287         "/* Heun''s Method */"
4288         "int ode2(f,t,h)"
4289         "  int (*f) ();"
4290         "  double t, h;"
4291         "{"
4292         "  int i;"
4293         "  double y["+string(nX)+"],yh["+string(nX)+"],temp,f0["+string(nX)+"],th;"
4294         ""
4295         "  /**/"
4296         "  memcpy(y,x,neq*sizeof(double));"
4297         "  memcpy(f0,xd,neq*sizeof(double));"
4298         ""
4299         "  /**/"
4300         "  (*f)(t,y, f0);"
4301         ""
4302         "  /**/"
4303         "  for (i=0;i<neq;i++) {"
4304         "    x[i]=y[i]+h*f0[i];"
4305         "  }"
4306         "  th=t+h;"
4307         "  for (i=0;i<neq;i++) {"
4308         "    yh[i]=y[i]+h*f0[i];"
4309         "  }"
4310         "  (*f)(th,yh, xd);"
4311         ""
4312         "  /**/"
4313         "  temp=0.5*h;"
4314         "  for (i=0;i<neq;i++) {"
4315         "    x[i]=y[i]+temp*(f0[i]+xd[i]);"
4316         "  }"
4317         ""
4318         "  return 0;"
4319         "}"
4320         ""
4321         "/* Fourth-Order Runge-Kutta (RK4) Formula */"
4322         "int ode4(f,t,h)"
4323         "  int (*f) ();"
4324         "  double t, h;"
4325         "{"
4326         "  int i;"
4327         "  double y["+string(nX)+"],yh["+string(nX)+"],"+...
4328         "temp,f0["+string(nX)+"],th,th2,"+...
4329         "f1["+string(nX)+"],f2["+string(nX)+"];"
4330         ""
4331         "  /**/"
4332         "  memcpy(y,x,neq*sizeof(double));"
4333         "  memcpy(f0,xd,neq*sizeof(double));"
4334         ""
4335         "  /**/"
4336         "  (*f)(t,y, f0);"
4337         ""
4338         "  /**/"
4339         "  for (i=0;i<neq;i++) {"
4340         "    x[i]=y[i]+h*f0[i];"
4341         "  }"
4342         "  th2=t+h/2;"
4343         "  for (i=0;i<neq;i++) {"
4344         "    yh[i]=y[i]+(h/2)*f0[i];"
4345         "  }"
4346         "  (*f)(th2,yh, f1);"
4347         ""
4348         "  /**/"
4349         "  temp=0.5*h;"
4350         "  for (i=0;i<neq;i++) {"
4351         "    x[i]=y[i]+temp*f1[i];"
4352         "  }"
4353         "  for (i=0;i<neq;i++) {"
4354         "    yh[i]=y[i]+(h/2)*f1[i];"
4355         "  }"
4356         "  (*f)(th2,yh, f2);"
4357         ""
4358         "  /**/"
4359         "  for (i=0;i<neq;i++) {"
4360         "    x[i]=y[i]+h*f2[i];"
4361         "  }"
4362         "  th=t+h;"
4363         "  for (i=0;i<neq;i++) {"
4364         "    yh[i]=y[i]+h*f2[i];"
4365         "  }"
4366         "  (*f)(th2,yh, xd);"
4367         ""
4368         "  /**/"
4369         "  temp=h/6;"
4370         "  for (i=0;i<neq;i++) {"
4371         "    x[i]=y[i]+temp*(f0[i]+2.0*f1[i]+2.0*f2[i]+xd[i]);"
4372         "  }"
4373         ""
4374         "return 0;"
4375         "}"], fd);
4376     end
4377
4378     mclose(fd);
4379 endfunction
4380
4381 //generates  static table definitions
4382 //
4383 //Author : Rachid Djenidi, Alan Layec
4384 function txt=make_static_standalone42()
4385     oldFormat = format();
4386     format(25);
4387     txt=[];
4388
4389     //*** Continuous state ***//
4390     if x <> [] then
4391         txt=[txt;
4392         "/* def continuous state */"
4393         cformatline("double x[]={"+strcat(string(x),",")+"};",70)
4394         cformatline("double xd[]={"+strcat(string(x),",")+"};",70)
4395         "static int c__1 = 1;"
4396         "static double c_b14 = 0.;"
4397         "static int neq="+string(nX)+";"
4398         ""]
4399     end
4400     //************************//
4401
4402     txt=[txt;
4403     "char input[50], output[50],s[1],sf[1],se[1],ss[1],**p;"
4404     "static double sci_time;"
4405     "static int errflg = 0;"
4406     "static char *optarg = NULL ;"
4407     "static int optind = 1, offset = 0 ;"
4408     "scicos_block block_"+rdnom+"["+string(nblk)+"];"
4409     ""];
4410
4411     //*** Real parameters ***//
4412     if size(rpar,1) <> 0 then
4413         txt=[txt;
4414         "/* def real parameters */"
4415         "static double RPAR1[ ] = {"];
4416
4417         for i=1:(length(rpptr)-1)
4418             if rpptr(i+1)-rpptr(i)>0  then
4419
4420                 if size(corinv(i),"*")==1 then
4421                     OO=scs_m.objs(corinv(i));
4422                 else
4423                     path=list("objs");
4424                     for l=cpr.corinv(i)(1:$-1)
4425                         path($+1)=l;
4426                         path($+1)="model";
4427                         path($+1)="rpar";
4428                         path($+1)="objs";
4429                     end
4430                     path($+1)=cpr.corinv(i)($);
4431                     OO=scs_m(path);
4432                 end
4433
4434                 //** Add comments **//
4435                 txt($+1)="/* Routine name of block: "+strcat(string(cpr.sim.funs(i)));
4436                 txt($+1)=" * Gui name of block: "+strcat(string(OO.gui));
4437                 txt($+1)=" * Compiled structure index: "+strcat(string(i));
4438
4439                 if stripblanks(OO.model.label)~=emptystr() then
4440                     txt=[txt;cformatline(" * Label: "+strcat(string(OO.model.label)),70)];
4441                 end
4442                 if stripblanks(OO.graphics.exprs(1))~=emptystr() then
4443                     txt=[txt;cformatline(" * Exprs: "+strcat(OO.graphics.exprs(1),","),70)];
4444                 end
4445                 if stripblanks(OO.graphics.id)~=emptystr() then
4446                     txt=[txt;
4447                     cformatline(" * Identification: "+strcat(string(OO.graphics.id)),70)];
4448                 end
4449                 txt=[txt;" * rpar="];
4450                 txt($+1)=" */";
4451                 //******************//
4452
4453                 txt=[txt;
4454                 cformatline(strcat(msprintf("%.25E,\n",rpar(rpptr(i):rpptr(i+1)-1))),70);
4455                 ""]
4456
4457             end
4458         end
4459         txt=[txt;
4460         "};"
4461         ""]
4462
4463     else
4464         txt($+1)="static double RPAR1[1];";
4465     end
4466     //***********************//
4467
4468     //*** Integer parameters ***//
4469     if size(ipar,1) <> 0 then
4470         txt=[txt;
4471         "/* def integer parameters */"
4472         "static int IPAR1[ ] = {"];
4473
4474         for i=1:(length(ipptr)-1)
4475             if ipptr(i+1)-ipptr(i)>0  then
4476                 if size(corinv(i),"*")==1 then
4477                     OO=scs_m.objs(corinv(i));
4478                 else
4479                     path=list("objs");
4480                     for l=cpr.corinv(i)(1:$-1)
4481                         path($+1)=l
4482                         path($+1)="model"
4483                         path($+1)="rpar"
4484                         path($+1)="objs"
4485                     end
4486                     path($+1)=cpr.corinv(i)($);
4487                     OO=scs_m(path);
4488                 end
4489
4490                 //** Add comments **//
4491                 txt($+1)="/* Routine name of block: "+strcat(string(cpr.sim.funs(i)));
4492                 txt($+1)=" * Gui name of block: "+strcat(string(OO.gui));
4493                 txt($+1)=" * Compiled structure index: "+strcat(string(i));
4494                 if stripblanks(OO.model.label)~=emptystr() then
4495                     txt=[txt;cformatline(" * Label: "+strcat(string(OO.model.label)),70)];
4496                 end
4497
4498                 if stripblanks(OO.graphics.exprs(1))~=emptystr() then
4499                     txt=[txt;
4500                     cformatline(" * Exprs: "+strcat(OO.graphics.exprs(1),","),70)];
4501                 end
4502                 if stripblanks(OO.graphics.id)~=emptystr() then
4503                     txt=[txt;
4504                     cformatline(" * Identification: "+strcat(string(OO.graphics.id)),70)];
4505                 end
4506                 txt=[txt;
4507                 cformatline(" * ipar= {"+strcat(string(ipar(ipptr(i):ipptr(i+1)-1)),",")+"};",70)];
4508                 txt($+1)=" */";
4509                 //******************//
4510
4511                 txt=[txt;cformatline(strcat(string(ipar(ipptr(i):ipptr(i+1)-1))+","),70)];
4512                 txt($+1)="";
4513             end
4514         end
4515         txt=[txt;
4516         "};"
4517         ""]
4518     else
4519         txt($+1)="static int IPAR1[1];";
4520     end
4521     //**************************//
4522
4523     //Alan added opar (27/06/07)
4524     //*** Object parameters ***//
4525     if size(opar)<>0 then
4526         txt=[txt;
4527         "/* def object parameters */"]
4528         for i=1:(length(opptr)-1)
4529             if opptr(i+1)-opptr(i)>0  then
4530
4531                 if size(corinv(i),"*")==1 then
4532                     OO=scs_m.objs(corinv(i));
4533                 else
4534                     path=list("objs");
4535                     for l=cpr.corinv(i)(1:$-1)
4536                         path($+1)=l;
4537                         path($+1)="model";
4538                         path($+1)="rpar";
4539                         path($+1)="objs";
4540                     end
4541                     path($+1)=cpr.corinv(i)($);
4542                     OO=scs_m(path);
4543                 end
4544
4545                 //** Add comments **//
4546                 txt($+1)="";
4547                 txt($+1)="/* Routine name of block: "+strcat(string(cpr.sim.funs(i)));
4548                 txt($+1)=" * Gui name of block: "+strcat(string(OO.gui));
4549                 txt($+1)=" * Compiled structure index: "+strcat(string(i));
4550                 if stripblanks(OO.model.label)~=emptystr() then
4551                     txt=[txt;cformatline(" * Label: "+strcat(string(OO.model.label)),70)];
4552                 end
4553                 if stripblanks(OO.graphics.id)~=emptystr() then
4554                     txt=[txt;
4555                     cformatline(" * Identification: "+strcat(string(OO.graphics.id)),70)];
4556                 end
4557                 txt($+1)=" */";
4558                 //******************//
4559
4560
4561                 for j=1:opptr(i+1)-opptr(i)
4562                     txt =[txt;
4563                     cformatline("static "+mat2c_typ(opar(opptr(i)+j-1)) +...
4564                     " OPAR_"+string(opptr(i)+j-1) + "[] = {"+...
4565                     strcat(string(opar(opptr(i)+j-1)),",")+"};",70)]
4566                 end
4567             end
4568         end
4569     end
4570     //*************************//
4571
4572     txt=[txt;
4573     ""]
4574     format(oldFormat(2), oldFormat(1));
4575 endfunction
4576
4577 //utilitary fonction used to format long C instruction
4578 //t : a string containing a C instruction
4579 //l : max line length allowed
4580
4581 //Author : Rachid Djenidi
4582 function t1=cformatline(t ,l)
4583     // Force format to avoid precision loss
4584     oldFormat = format();
4585     format(25);
4586
4587     sep=[",","+"]
4588     l1=l-2
4589     t1=[]
4590     kw=strindex(t," ")
4591     nw=0
4592     if kw<>[] then
4593         if kw(1)==1 then // there is leading blanks
4594             k1=find(kw(2:$)-kw(1:$-1)<>1)
4595             if k1==[] then // there is a single blank
4596                 nw=1
4597             else
4598                 nw=kw(k1(1))
4599             end
4600         end
4601     end
4602     t=part(t,nw+1:length(t));
4603     bl=part(" ",ones(1,nw))
4604     l1=l-nw;first=%t
4605     while %t
4606         if length(t)<=l then
4607             t1=[t1;bl+t]
4608             break
4609         end
4610         k=strindex(t,sep);
4611         if k==[] then
4612             t1=[t1;bl+t]
4613             break
4614         end
4615         k($+1)=length(t)+1 // positions of the commas
4616         i=find(k(1:$-1)<=l&k(2:$)>l) //nearest left comma (reltively to l)
4617         if i==[] then
4618             i=1
4619         end
4620         t1=[t1;bl+part(t,1:k(i))]
4621         t=part(t,k(i)+1:length(t))
4622         if first then
4623             l1=l1-2;bl=bl+"  ";
4624             first=%f;
4625         end
4626     end
4627
4628     format(oldFormat(2), oldFormat(1));
4629 endfunction
4630
4631 //
4632 // Generate Matrix of all binary code from
4633 // 1 to 2^n-1
4634 //
4635 function vec = binaryTable(n)
4636     n = int(n);
4637     vec = [];
4638     for i = 1:n
4639         accu = [];
4640         for j = 1:2^(n-i-1)
4641             accu = [accu ; zeros(2^(i-1),1) ; ones(2^(i-1),1)]
4642         end
4643         vec = [accu, vec]
4644     end
4645     vec=vec(2:$, :); // Remove first line [ 0 --- 0 ]
4646 endfunction
4647
4648 //used in do_compile_superblock
4649 function vec=codebinaire(v,szclkIN)
4650     vec=zeros(1,szclkIN)
4651     for i=1:szclkIN
4652         w=v/2;
4653         vec(i)=v-2*int(w);
4654         v=int(w);
4655     end
4656 endfunction
4657
4658 function t=filetype(m)
4659     m=int32(m)
4660     filetypes=["Directory","Character device","Block device",...
4661     "Regular file","FIFO","Symbolic link","Socket"]
4662     bits=[16384,8192,24576,32768,4096,40960,49152]
4663     m=int32(m)&int32(61440)
4664     t=filetypes(find(m==int32(bits)))
4665 endfunction
4666
4667 //get_blank : return blanks with a length
4668 //            of the given input string
4669 //
4670 //input : str : a string
4671 //
4672 //output : txt : blanks
4673 //
4674 //16/06/07 Author : A.Layec
4675 function [txt] = get_blank(str)
4676     txt="";
4677     for i=1:length(str)
4678         txt=txt+" ";
4679     end
4680 endfunction
4681
4682 // get_comment : return a C comment
4683 //               for generated code
4684 //
4685 //input : typ : a string
4686 //        param : a list
4687 //
4688 //output : a C comment
4689 //
4690 //16/06/07 Author : A.Layec
4691 function [txt]=get_comment(typ,param)
4692     txt = [];
4693     select typ
4694         //** main flag
4695     case "flag" then
4696         select param(1)
4697         case 0 then
4698             txt = "/* Continuous state computation */"
4699         case 1 then
4700             txt = "/* Output computation */"
4701         case 2 then
4702             txt = "/* Discrete state computation */"
4703         case 3 then
4704             txt = "/* Output Event computation */"
4705         case 4 then
4706             txt = "/* Initialization */"
4707         case 5 then
4708             txt = "/* Ending */"
4709         case 9 then
4710             txt = "/* Update zero crossing surfaces */"
4711         end
4712         //** blocks activated on event number
4713     case "ev" then
4714         txt = "/* Blocks activated on the event number "+string(param(1))+" */"
4715
4716         //** blk syntax
4717     case "call_blk" then
4718         txt = ["/* Call of ''"+param(1) + ...
4719         "'' (type "+string(param(2))+" - blk nb "+...
4720         string(param(3))+" - uid "+param(4)];
4721         if ztyp(param(3)) then
4722             txt=txt+" - with zcross) */";
4723         else
4724             txt=txt+") */";
4725         end
4726         //** proto syntax
4727     case "proto_blk" then
4728         txt = ["/* prototype of ''"+param(1) + ...
4729         "'' (type "+string(param(2))];
4730         if ztyp(param(3)) then
4731             txt=txt+" - with zcross) */";
4732         else
4733             txt=txt+") */";
4734         end
4735         //** ifthenelse syntax
4736     case "ifthenelse_blk" then
4737         txt = ["/* Call of ''if-then-else'' blk (blk nb "+...
4738         string(param(1))+") */"]
4739         //** eventselect syntax
4740     case "evtselect_blk" then
4741         txt = ["/* Call of ''event-select'' blk (blk nb "+...
4742         string(param(1))+") */"]
4743         //** set block structure
4744     case "set_blk" then
4745         txt = ["/* set blk struc. of ''"+param(1) + ...
4746         "'' (type "+string(param(2))+" - blk nb "+...
4747         string(param(3))++" - uid "+param(4)+") */"];
4748         //** Update xd vector ptr
4749     case "update_xd" then
4750         txt = ["/* Update xd vector ptr */"];
4751         //** Update g vector ptr
4752     case "update_g" then
4753         txt = ["/* Update g vector ptr */"];
4754     else
4755         break;
4756     end
4757 endfunction
4758
4759 //mat2c_typ : matrix to C type
4760 //sci2c_ttyp : get the C string of a scicos type
4761 //
4762 //input : outtb : a matrix
4763 //
4764 //output : txt : the string of the C scicos type
4765 //               of the data of outtb
4766 //
4767 //16/06/07 Author : A.Layec
4768 function [txt]=mat2c_typ(outtb)
4769     select type(outtb)
4770         //real matrix
4771     case 1 then
4772         if isreal(outtb) then
4773             txt = "double"
4774         else
4775             txt = "double"
4776         end
4777         //integer matrix
4778     case 8 then
4779         select typeof(outtb)
4780         case "int32" then
4781             txt = "long"
4782         case "int16" then
4783             txt = "short"
4784         case "int8" then
4785             txt = "char"
4786         case "uint32" then
4787             txt = "unsigned long"
4788         case "uint16" then
4789             txt = "unsigned short"
4790         case "uint8" then
4791             txt = "unsigned char"
4792         end
4793     else
4794         break;
4795     end
4796 endfunction
4797
4798 //mat2scs_c_nb  matrix to scicos C number (sci2sci_n)
4799 //
4800 //input : outtb : a matrix
4801 //
4802 //output : c_nb : the scicos C number
4803 //
4804 //16/06/07 Author : A.Layec
4805 function [c_nb]=mat2scs_c_nb(outtb)
4806     select type(outtb)
4807         //real matrix
4808     case 1 then
4809         if isreal(outtb) then
4810             c_nb = 10
4811         else
4812             c_nb = 11
4813         end
4814         //integer matrix
4815     case 8 then
4816         select typeof(outtb)
4817         case "int32" then
4818             c_nb = 84
4819         case "int16" then
4820             c_nb = 82
4821         case "int8" then
4822             c_nb = 81
4823         case "uint32" then
4824             c_nb = 814
4825         case "uint16" then
4826             c_nb = 812
4827         case "uint8" then
4828             c_nb = 811
4829         end
4830     else
4831         break;
4832     end
4833 endfunction
4834
4835 //mat2scs_c_ptr matrix to scicos C ptr (sci2c_typ)
4836 //
4837 //input : outtb : a matrix
4838 //
4839 //output : txt : the string of the C scicos type
4840 //               of the data of outtb
4841 //
4842 //16/06/07 Author : A.Layec
4843 function [txt]=mat2scs_c_ptr(outtb)
4844     select type(outtb)
4845         //real matrix
4846     case 1 then
4847         if isreal(outtb) then
4848             txt = "SCSREAL_COP"
4849         else
4850             txt = "SCSCOMPLEX_COP"
4851         end
4852         //integer matrix
4853     case 8 then
4854         select typeof(outtb)
4855         case "int32" then
4856             txt = "SCSINT32_COP"
4857         case "int16" then
4858             txt = "SCSINT16_COP"
4859         case "int8" then
4860             txt = "SCSINT8_COP"
4861         case "uint32" then
4862             txt = "SCSUINT32_COP"
4863         case "uint16" then
4864             txt = "SCSUINT16_COP"
4865         case "uint8" then
4866             txt = "SCSUINT8_COP"
4867         end
4868     else
4869         break;
4870     end
4871 endfunction
4872
4873 //mat2scs_c_typ matrix to scicos C type
4874 //
4875 //input : outtb : a matrix
4876 //
4877 //output : txt : the string of the C scicos type
4878 //               of the data of outtb
4879 //
4880 //16/06/07 Author : A.Layec
4881 function [txt]=mat2scs_c_typ(outtb)
4882     select type(outtb)
4883         //real matrix
4884     case 1 then
4885         if isreal(outtb) then
4886             txt = "SCSREAL_N"
4887         else
4888             txt = "SCSCOMPLEX_N"
4889         end
4890         //integer matrix
4891     case 8 then
4892         select typeof(outtb)
4893         case "int32" then
4894             txt = "SCSINT32_N"
4895         case "int16" then
4896             txt = "SCSINT16_N"
4897         case "int8" then
4898             txt = "SCSINT8_N"
4899         case "uint32" then
4900             txt = "SCSUINT32_N"
4901         case "uint16" then
4902             txt = "SCSUINT16_N"
4903         case "uint8" then
4904             txt = "SCSUINT8_N"
4905         end
4906     else
4907         break;
4908     end
4909 endfunction
4910
4911 //scs_c_n2c_fmt : scicos C number to C format
4912 //
4913 //input : c_nb : a C scicos type
4914 //
4915 //output : txt : the string of the C format string
4916 //               of the data of outtb
4917 //
4918 //16/06/07 Author : A.Layec
4919 function [txt]=scs_c_n2c_fmt(c_nb)
4920     select c_nb
4921         //real matrix
4922     case 10 then
4923         txt = "%f";
4924         //complex matrix
4925     case 11 then
4926         txt = "%f,%f";
4927         //int8 matrix
4928     case 81 then
4929         txt = "%d";
4930         //int16 matrix
4931     case 82 then
4932         txt = "%d";
4933         //int32 matrix
4934     case 84 then
4935         txt = "%d";
4936         //uint8 matrix
4937     case 811 then
4938         txt = "%d";
4939         //uint16 matrix
4940     case 812 then
4941         txt = "%d";
4942         //uint32 matrix
4943     case 814 then
4944         txt = "%d";
4945     else
4946         txt="%f"
4947         break;
4948     end
4949 endfunction
4950
4951 //scs_c_n2c_typ scicos C number to C type
4952 //
4953 //input : c_nb : a C scicos number
4954 //
4955 //output : txt : the string of the C format string
4956 //               of the data of outtb
4957 //
4958 //16/06/07 Author : A.Layec
4959 function [txt]=scs_c_n2c_typ(c_nb)
4960     select c_nb
4961         //real matrix
4962     case 10 then
4963         txt = "double";
4964         //complex matrix
4965     case 11 then
4966         txt = "double";
4967         //int8 matrix
4968     case 81 then
4969         txt = "char";
4970         //int16 matrix
4971     case 82 then
4972         txt = "short";
4973         //int32 matrix
4974     case 84 then
4975         txt = "long";
4976         //uint8 matrix
4977     case 811 then
4978         txt = "unsigned char";
4979         //uint16 matrix
4980     case 812 then
4981         txt = "unsigned short";
4982         //uint32 matrix
4983     case 814 then
4984         txt = "unsigned long";
4985     else
4986         txt="double"
4987         break;
4988     end
4989 endfunction
4990
4991 //scs_c_nb2scs_nb : scicos C number to scicos number
4992 //
4993 //input : c_nb  : the scicos C number type
4994 //
4995 //output : scs_nb : the scilab number type
4996 //
4997 //16/06/07 Author : A.Layec
4998 function [scs_nb]=scs_c_nb2scs_nb(c_nb)
4999     scs_nb=zeros(size(c_nb,1),size(c_nb,2));
5000     for i=1:size(c_nb,1)
5001         for j=1:size(c_nb,2)
5002             select (c_nb(i,j))
5003             case 10 then
5004                 scs_nb(i,j) = 1
5005             case 11 then
5006                 scs_nb(i,j) = 2
5007             case 81 then
5008                 scs_nb(i,j) = 5
5009             case 82 then
5010                 scs_nb(i,j) = 4
5011             case 84 then
5012                 scs_nb(i,j) = 3
5013             case 811 then
5014                 scs_nb(i,j) = 8
5015             case 812 then
5016                 scs_nb(i,j) = 7
5017             case 814 then
5018                 scs_nb(i,j) = 6
5019             else
5020                 scs_nb(i,j) = 1
5021             end
5022         end
5023     end
5024 endfunction
5025
5026 //used in do_compile_superblock
5027 function XX=update_block(XX)
5028     execstr("o="+rdnom+"_c(''define'')")
5029     XX.model=o.model
5030     XX.gui=rdnom+"_c";
5031     XX.graphics.gr_i=o.graphics.gr_i
5032 endfunction
5033
5034 //write_code_cdoit : generate body of the code for
5035 //                   for all time dependant blocks
5036 //
5037 //input : flag : flag number for block's call
5038 //
5039 //output : txt for cord blocks
5040 //
5041 //12/07/07 Alan Layec
5042 function [txt]=write_code_cdoit(flag)
5043     txt=[];
5044
5045     for j=1:ncord
5046         bk=cord(j,1);
5047         pt=cord(j,2);
5048         //** blk
5049         if funtyp(bk)>-1 then
5050             if or(bk==act) | or(bk==cap) then
5051                 if stalone then
5052                     txt2=call_block42(bk,pt,flag);
5053                     if txt2<>[] then
5054                         txt=[txt;
5055                         "    "+txt2
5056                         ""];
5057                     end
5058                 end
5059             else
5060                 txt2=call_block42(bk,pt,flag);
5061                 if txt2<>[] then
5062                     txt=[txt;
5063                     "    "+txt2
5064                     ""];
5065                 end
5066             end
5067             //** ifthenelse blk
5068         elseif funtyp(bk)==-1 then
5069             ix=-1+inplnk(inpptr(bk));
5070             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5071             thentxt=write_code_doit(clkptr(bk),flag);
5072             elsetxt=write_code_doit(clkptr(bk)+1,flag);
5073             if thentxt<>[] | elsetxt<>[] then
5074                 txt=[txt;
5075                 "    "+get_comment("ifthenelse_blk",list(bk));]
5076                 //** C **//
5077                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5078                 txt=[txt;
5079                 "    if("+tmp_+">0) {"]
5080                 //*******//
5081                 txt=[txt;
5082                 Indent+thentxt];
5083                 if elsetxt<>[] then
5084                     //** C **//
5085                     txt=[txt;
5086                     "    }";
5087                     "    else {";]
5088                     //*******//
5089                     txt=[txt;
5090                     Indent+elsetxt];
5091                 end
5092                 //** C **//
5093                 txt=[txt;
5094                 "    }"]
5095                 //*******//
5096             end
5097             //** eventselect blk
5098         elseif funtyp(bk)==-2 then
5099             Noutport=clkptr(bk+1)-clkptr(bk);
5100             ix=-1+inplnk(inpptr(bk));
5101             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5102             II=[];
5103             switchtxt=list()
5104             for i=1: Noutport
5105                 switchtxt(i)=write_code_doit(clkptr(bk)+i-1,flag);
5106                 if switchtxt(i)<>[] then II=[II i];end
5107             end
5108             if II<>[] then
5109                 txt=[txt;
5110                 "    "+get_comment("evtselect_blk",list(bk));]
5111                 //** C **//
5112                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5113                 txt=[txt;
5114                 "    i=max(min((int) "+...
5115                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"
5116                 "    switch(i)"
5117                 "    {"]
5118                 //*******//
5119                 for i=II
5120                     //** C **//
5121                     txt=[txt;
5122                     "     case "+string(i)+" :";]
5123                     //*******//
5124                     txt=[txt;
5125                     BigIndent+write_code_doit(clkptr(bk)+i-1,flag);]
5126                     //** C **//
5127                     txt=[txt;
5128                     BigIndent+"break;"]
5129                     //*******//
5130                 end
5131                 //** C **//
5132                 txt=[txt;
5133                 "    }"];
5134                 //*******//
5135             end
5136             //** Unknown block
5137         else
5138             error("Unknown block type "+string(bk));
5139         end
5140     end
5141
5142 endfunction
5143
5144 //write_code_doit : generate body of the code for
5145 //                  ordering calls of blocks during
5146 //                  flag 1,2 & flag 3
5147 //
5148 //input : ev  : evt number for block's call
5149 //       flag : flag number for block's call
5150 //
5151 //output : txt for flag 1 or 2, or flag 3
5152 //
5153 //12/07/07 Alan Layec
5154 function [txt]=write_code_doit(ev,flag)
5155     txt=[];
5156
5157     for j=ordptr(ev):ordptr(ev+1)-1
5158         bk=ordclk(j,1);
5159         pt=ordclk(j,2);
5160         //** blk
5161         if funtyp(bk)>-1 then
5162             if or(bk==act) | or(bk==cap) then
5163                 if stalone then
5164                     txt2=call_block42(bk,pt,flag);
5165                     if txt2<>[] then
5166                         txt=[txt;
5167                         "    "+txt2
5168                         ""];
5169                     end
5170                 end
5171             else
5172                 txt2=call_block42(bk,pt,flag);
5173                 if txt2<>[] then
5174                     txt=[txt;
5175                     "    "+txt2
5176                     ""];
5177                 end
5178             end
5179             //** ifthenelse blk
5180         elseif funtyp(bk)==-1 then
5181             ix=-1+inplnk(inpptr(bk));
5182             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5183             thentxt=write_code_doit(clkptr(bk),flag);
5184             elsetxt=write_code_doit(clkptr(bk)+1,flag);
5185             if thentxt<>[] | elsetxt<>[] then
5186                 txt=[txt;
5187                 "    "+get_comment("ifthenelse_blk",list(bk));]
5188                 //** C **//
5189                 tmp_ = "*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5190                 txt=[txt;
5191                 "    if("+tmp_+">0) {"]
5192                 //*******//
5193                 txt=[txt;
5194                 Indent+thentxt]
5195                 if elsetxt<>[] then
5196                     //** C **//
5197                     txt=[txt;
5198                     "    }";
5199                     "    else {";]
5200                     //*******//
5201                     txt=[txt;
5202                     Indent+elsetxt];
5203                 end
5204                 //** C **//
5205                 txt=[txt;
5206                 "    }"]
5207                 //*******//
5208             end
5209             //** eventselect blk
5210         elseif funtyp(bk)==-2 then
5211             Noutport=clkptr(bk+1)-clkptr(bk);
5212             ix=-1+inplnk(inpptr(bk));
5213             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5214             II=[];
5215             switchtxt=list()
5216             for i=1: Noutport
5217                 switchtxt(i)=write_code_doit(clkptr(bk)+i-1,flag);
5218                 if switchtxt(i)<>[] then II=[II i];end
5219             end
5220             if II<>[] then
5221                 txt=[txt;
5222                 "    "+get_comment("evtselect_blk",list(bk));]
5223                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5224                 //** C **//
5225                 txt=[txt;
5226                 "    i=max(min((int) "+...
5227                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"
5228                 "    switch(i)"
5229                 "    {"]
5230                 //*******//
5231                 for i=II
5232                     //** C **//
5233                     txt=[txt;
5234                     "     case "+string(i)+" :";]
5235                     //*******//
5236                     txt=[txt;
5237                     BigIndent+write_code_doit(clkptr(bk)+i-1,flag);]
5238                     //** C **//
5239                     txt=[txt;
5240                     BigIndent+"break;"]
5241                     //*******//
5242                 end
5243                 //** C **//
5244                 txt=[txt;
5245                 "    }"]
5246                 //*******//
5247             end
5248             //** Unknown block
5249         else
5250             error("Unknown block type "+string(bk));
5251         end
5252     end
5253
5254 endfunction
5255
5256 //write_code_idoit : generate body of the code for
5257 //                   ordering calls of initial
5258 //                   called blocks
5259 //
5260 //input : nothing (blocks are called with flag 1)
5261 //
5262 //output : txt for iord
5263 //
5264 //15/07/07 Alan Layec
5265 function [txt]=write_code_idoit()
5266     txt=[];
5267
5268     for j=1:niord
5269         bk=iord(j,1);
5270         pt=iord(j,2);
5271         //** blk
5272         if funtyp(bk)>-1 then
5273             if or(bk==act) then
5274                 if stalone then
5275                     txt2=call_block42(bk,pt,1);
5276                     if txt2<>[] then
5277                         txt=[txt;
5278                         "    "+txt2
5279                         ""];
5280                     end
5281                 end
5282             else
5283                 txt2=call_block42(bk,pt,1);
5284                 if txt2<>[] then
5285                     txt=[txt;
5286                     "    "+txt2
5287                     ""];
5288                 end
5289             end
5290             //** ifthenelse blk
5291         elseif funtyp(bk)==-1 then
5292             ix=-1+inplnk(inpptr(bk));
5293             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5294             thentxt=write_code_doit(clkptr(bk),1);
5295             elsetxt=write_code_doit(clkptr(bk)+1,1);
5296             if thentxt<>[] | elsetxt<>[] then
5297                 txt=[txt;
5298                 "    "+get_comment("ifthenelse_blk",list(bk));]
5299                 //** C **//
5300                 tmp_ = "*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5301                 txt=[txt;
5302                 "    if("+tmp_+">0) {"]
5303                 //*******//
5304                 txt=[txt;
5305                 Indent+thentxt];
5306                 if elsetxt<>[] then
5307                     //** C **//
5308                     txt=[txt;
5309                     "    }";
5310                     "    else {";]
5311                     //*******//
5312                     txt=[txt;
5313                     Indent+elsetxt];
5314                 end
5315                 //** C **//
5316                 txt=[txt;
5317                 "    }"]
5318                 //*******//
5319             end
5320             //** eventselect blk
5321         elseif funtyp(bk)==-2 then
5322             Noutport=clkptr(bk+1)-clkptr(bk);
5323             ix=-1+inplnk(inpptr(bk));
5324             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5325             II=[];
5326             switchtxt=list()
5327             for i=1: Noutport
5328                 switchtxt(i)=write_code_doit(clkptr(bk)+i-1,1);
5329                 if switchtxt(i)<>[] then II=[II i];end
5330             end
5331             if II<>[] then
5332                 txt=[txt;
5333                 "    "+get_comment("evtselect_blk",list(bk));]
5334                 //** C **//
5335                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5336                 txt=[txt;
5337                 "    i=max(min((int) "+...
5338                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"]
5339                 txt=[txt;
5340                 "    switch(i)"
5341                 "    {"]
5342                 //*******//
5343                 for i=II
5344                     //** C **//
5345                     txt=[txt;
5346                     "     case "+string(i)+" :";]
5347                     //*******//
5348                     txt=[txt;
5349                     BigIndent+write_code_doit(clkptr(bk)+i-1,1);]
5350                     //** C **//
5351                     txt=[txt;
5352                     BigIndent+"break;"]
5353                     //*******//
5354                 end
5355                 //** C **//
5356                 txt=[txt;
5357                 "    }"];
5358                 //*******//
5359             end
5360             //** Unknown block
5361         else
5362             error("Unknown block type "+string(bk));
5363         end
5364     end
5365
5366 endfunction
5367
5368 //write_code_odoit : generate body of the code for
5369 //                   ordering calls of blocks before
5370 //                   continuous time integration
5371 //
5372 //input : flag : flag number for block's call
5373 //
5374 //output : txt for flag 0
5375 //
5376 //12/07/07 Alan Layec
5377 function [txt]=write_code_odoit(flag)
5378     txt=[];
5379
5380     for j=1:noord
5381         bk=oord(j,1);
5382         pt=oord(j,2);
5383         //** blk
5384         if funtyp(bk)>-1 then
5385             txt2=call_block42(bk,pt,flag);
5386             if txt2<>[] then
5387                 txt=[txt;
5388                 "    "+txt2
5389                 ""];
5390             end
5391             //** ifthenelse blk
5392         elseif funtyp(bk)==-1 then
5393             ix=-1+inplnk(inpptr(bk));
5394             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5395             thentxt=write_code_ozdoit(clkptr(bk),flag);
5396             elsetxt=write_code_ozdoit(clkptr(bk)+1,flag);
5397             if thentxt<>[] | elsetxt<>[] then
5398                 txt=[txt;
5399                 "    "+get_comment("ifthenelse_blk",list(bk));]
5400                 //** C **//
5401                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5402                 txt=[txt;
5403                 "    if ((block_"+rdnom+"["+string(bk-1)+"].nmode<0"+...
5404                 " && "+tmp_+">0)"+...
5405                 " || \"
5406                 "        (block_"+rdnom+"["+string(bk-1)+"].nmode>0"+...
5407                 " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==1)) {"]
5408                 //*******//
5409                 txt=[txt;
5410                 Indent+thentxt]
5411                 //** C **//
5412                 txt=[txt;
5413                 "    }"];
5414                 //*******//
5415                 if elsetxt<>[] then
5416                     //** C **//
5417                     txt=[txt;
5418                     "    else if  ((block_"+rdnom+"["+string(bk-1)+"].nmode<0"+...
5419                     " && "+tmp_+"<=0)"+...
5420                     " || \"
5421                     "              (block_"+rdnom+"["+string(bk-1)+"].nmode>0"+...
5422                     " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==2)) {";]
5423                     //*******//
5424                     txt=[txt;
5425                     Indent+elsetxt]
5426                     //** C **//
5427                     txt=[txt;
5428                     "    }"];
5429                     //*******//
5430                 end
5431             end
5432             //** eventselect blk
5433         elseif funtyp(bk)==-2 then
5434             Noutport=clkptr(bk+1)-clkptr(bk);
5435             ix=-1+inplnk(inpptr(bk));
5436             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5437             II=[];
5438             switchtxt=list()
5439             for i=1: Noutport
5440                 switchtxt(i)=write_code_ozdoit(clkptr(bk)+i-1,flag);
5441                 if switchtxt(i)<>[] then II=[II i];end
5442             end
5443             if II<>[] then
5444                 txt=[txt;
5445                 "    "+get_comment("evtselect_blk",list(bk));]
5446                 //** C **//
5447                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5448                 txt=[txt;
5449                 "    if (block_"+rdnom+"["+string(bk-1)+"].nmode<0) {";
5450                 "      i=max(min((int) "+...
5451                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"
5452                 "    }"
5453                 "    else {"
5454                 "      i=block_"+rdnom+"["+string(bk-1)+"].mode[0];"
5455                 "    }"]
5456                 txt=[txt;
5457                 "    switch(i)"
5458                 "    {"];
5459                 //*******//
5460                 for i=II
5461                     //** C **//
5462                     txt=[txt;
5463                     "     case "+string(i)+" :";]
5464                     //*******//
5465                     txt=[txt;
5466                     BigIndent+write_code_ozdoit(clkptr(bk)+i-1,flag);]
5467                     //** C **//
5468                     txt=[txt;
5469                     BigIndent+"break;"]
5470                     //*******//
5471                 end
5472                 //** C **//
5473                 txt=[txt;
5474                 "    }"];
5475                 //*******//
5476             end
5477             //** Unknown block
5478         else
5479             error("Unknown block type "+string(bk));
5480         end
5481     end
5482
5483 endfunction
5484
5485 //write_code_ozdoit : generate body of the code for both
5486 //                    flag 0 & flag 9
5487 //
5488 //input: ev  : evt number for block's call
5489 //      flag : flag number for block's call
5490 //
5491 //output : txt for flag 0 or flag 9
5492 //
5493 //12/07/07 Alan Layec
5494 function [txt]=write_code_ozdoit(ev,flag)
5495     txt=[];
5496
5497     for j=ordptr(ev):ordptr(ev+1)-1
5498         bk=ordclk(j,1);
5499         pt=ordclk(j,2);
5500         //** blk
5501         if funtyp(bk)>-1 then
5502             if (or(bk==act) | or(bk==cap)) & (flag==1) then
5503                 if stalone then
5504                     txt=[txt;
5505                     "    "+call_block42(bk,pt,flag)
5506                     ""];
5507                 end
5508             else
5509                 txt2=call_block42(bk,pt,flag);
5510                 if txt2<>[] then
5511                     txt=[txt;
5512                     "    "+txt2
5513                     ""];
5514                 end
5515             end
5516             //** ifthenelse blk
5517         elseif funtyp(bk)==-1 then
5518             ix=-1+inplnk(inpptr(bk));
5519             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5520             thentxt=write_code_ozdoit(clkptr(bk),flag);
5521             elsetxt=write_code_ozdoit(clkptr(bk)+1,flag);
5522             if thentxt<>[] | elsetxt<>[] then
5523                 txt=[txt;
5524                 "    "+get_comment("ifthenelse_blk",list(bk));]
5525                 //** C **//
5526                 tmp_ = "*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5527                 txt=[txt;
5528                 "    if (((phase==1"+...
5529                 " || block_"+rdnom+"["+string(bk-1)+"].nmode==0)"+...
5530                 " && "+tmp_+">0)"+...
5531                 " || \"
5532                 "        ((phase!=1"+...
5533                 " && block_"+rdnom+"["+string(bk-1)+"].nmode!=0)"+...
5534                 " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==1)) {"]
5535                 //*******//
5536                 txt=[txt;
5537                 Indent+thentxt]
5538                 //** C **//
5539                 txt=[txt;
5540                 "    }"];
5541                 //*******//
5542                 if elsetxt<>[] then
5543                     //** C **//
5544                     txt=[txt;
5545                     "      else if (((phase==1"+...
5546                     " || block_"+rdnom+"["+string(bk-1)+"].nmode==0)"+...
5547                     " && "+tmp_+"<=0)"+...
5548                     " || \"
5549                     "               ((phase!=1"+...
5550                     " && block_"+rdnom+"["+string(bk-1)+"].nmode!=0)"+...
5551                     " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==2)) {";]
5552                     //*******//
5553                     txt=[txt;
5554                     Indent+elsetxt]
5555                     //** C **//
5556                     txt=[txt;
5557                     "    }"];
5558                     //*******//
5559                 end
5560             end
5561             //** eventselect blk
5562         elseif funtyp(bk)==-2 then
5563             Noutport=clkptr(bk+1)-clkptr(bk);
5564             ix=-1+inplnk(inpptr(bk));
5565             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5566             II=[];
5567             switchtxt=list()
5568             for i=1: Noutport
5569                 switchtxt(i)=write_code_ozdoit(clkptr(bk)+i-1,flag);
5570                 if switchtxt(i)<>[] then II=[II i];end
5571             end
5572             if II<>[] then
5573                 txt=[txt;
5574                 "    "+get_comment("evtselect_blk",list(bk));]
5575                 //** C **//
5576                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5577                 txt=[txt;
5578                 "    if (phase==1 || block_"+rdnom+"["+string(bk-1)+"].nmode==0) {";
5579                 "      i=max(min((int) "+...
5580                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"
5581                 "    }"
5582                 "    else {"
5583                 "      i=block_"+rdnom+"["+string(bk-1)+"].mode[0];"
5584                 "    }"]
5585                 txt=[txt;
5586                 "    switch(i)"
5587                 "    {"];
5588                 //*******//
5589                 for i=II
5590                     //** C **//
5591                     txt=[txt;
5592                     "     case "+string(i)+" :";]
5593                     //*******//
5594                     txt=[txt;
5595                     BigIndent+write_code_ozdoit(clkptr(bk)+i-1,flag);]
5596                     //** C **//
5597                     txt=[txt;
5598                     BigIndent+"break;"]
5599                     //*******//
5600                 end
5601                 //** C **//
5602                 txt=[txt;
5603                 "    }"];
5604                 //*******//
5605             end
5606             //** Unknown block
5607         else
5608             error("Unknown block type "+string(bk));
5609         end
5610     end
5611
5612 endfunction
5613
5614 //write_code_zdoit : generate body of the code for
5615 //                   ordering calls of blocks before
5616 //                   continuous time zero crossing
5617 //                   detection
5618 //
5619 //input : noting
5620 //
5621 //output : txt for flag 9
5622 //
5623 //12/07/07 Alan Layec
5624 function [txt]=write_code_zdoit()
5625     txt=[];
5626
5627     //** first pass (flag 1)
5628     for j=1:nzord
5629         bk=zord(j,1);
5630         pt=zord(j,2);
5631         //** blk
5632         if funtyp(bk)>-1 then
5633             if or(bk==act) | or(bk==cap) then
5634                 if stalone then
5635                     txt=[txt;
5636                     "    "+call_block42(bk,pt,1)
5637                     ""];
5638                 end
5639             else
5640                 txt2=call_block42(bk,pt,1);
5641                 if txt2<>[] then
5642                     txt=[txt;
5643                     "    "+txt2
5644                     ""];
5645                 end
5646             end
5647             //** ifthenelse blk
5648         elseif funtyp(bk)==-1 then
5649             ix=-1+inplnk(inpptr(bk));
5650             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5651             thentxt=write_code_ozdoit(clkptr(bk),1);
5652             elsetxt=write_code_ozdoit(clkptr(bk)+1,1);
5653             if thentxt<>[] | elsetxt<>[] then
5654                 txt=[txt;
5655                 "    "+get_comment("ifthenelse_blk",list(bk));]
5656                 //** C **//
5657                 tmp_ = "*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5658                 txt=[txt;
5659                 "    if (((phase==1"+...
5660                 " || block_"+rdnom+"["+string(bk-1)+"].nmode==0)"+...
5661                 " && "+tmp_+">0)"+...
5662                 " || \"
5663                 "        ((phase!=1"+...
5664                 " && block_"+rdnom+"["+string(bk-1)+"].nmode!=0)"+...
5665                 " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==1)) {"]
5666                 //*******//
5667                 txt=[txt;
5668                 Indent+thentxt]
5669                 //** C **//
5670                 txt=[txt;
5671                 "    }"];
5672                 //*******//
5673                 if elsetxt<>[] then
5674                     //** C **//
5675                     txt=[txt;
5676                     "      else if (((phase==1"+...
5677                     " || block_"+rdnom+"["+string(bk-1)+"].nmode==0)"+...
5678                     " && "+tmp_+"<=0)"+...
5679                     " || \"
5680                     "               ((phase!=1"+...
5681                     " && block_"+rdnom+"["+string(bk-1)+"].nmode!=0)"+...
5682                     " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==2)) {";]
5683                     //*******//
5684                     txt=[txt;
5685                     Indent+elsetxt]
5686                     //** C **//
5687                     txt=[txt;
5688                     "    }"];
5689                     //*******//
5690                 end
5691             end
5692             //** eventselect blk
5693         elseif funtyp(bk)==-2 then
5694             Noutport=clkptr(bk+1)-clkptr(bk);
5695             ix=-1+inplnk(inpptr(bk));
5696             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5697             II=[];
5698             switchtxt=list()
5699             for i=1: Noutport
5700                 switchtxt(i)=write_code_ozdoit(clkptr(bk)+i-1,1);
5701                 if switchtxt(i)<>[] then II=[II i];end
5702             end
5703             if II<>[] then
5704                 txt=[txt;
5705                 "    "+get_comment("evtselect_blk",list(bk));]
5706                 //** C **//
5707                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5708                 txt=[txt;
5709                 "    if (phase==1 || block_"+rdnom+"["+string(bk-1)+"].nmode==0){";
5710                 "      i=max(min((int) "+...
5711                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"
5712                 "    else {"
5713                 "      i=block_"+rdnom+"["+string(bk-1)+"].mode[0];"
5714                 "    }"]
5715                 txt=[txt;
5716                 "      switch(i)"
5717                 "      {"];
5718                 //*******//
5719                 for i=II
5720                     //** C **//
5721                     txt=[txt;
5722                     "       case "+string(i)+" :";]
5723                     //*******//
5724                     txt=[txt;
5725                     BigIndent+write_code_ozdoit(clkptr(bk)+i-1,1);]
5726                     //** C **//
5727                     txt=[txt;
5728                     BigIndent+"break;"]
5729                     //*******//
5730                 end
5731                 //** C **//
5732                 txt=[txt;
5733                 "      }"];
5734                 //*******//
5735             end
5736             //** Unknown block
5737         else
5738             error("Unknown block type "+string(bk));
5739         end
5740     end
5741
5742     //** second pass (flag 9)
5743     for j=1:nzord
5744         bk=zord(j,1);
5745         pt=zord(j,2);
5746         //** blk
5747         if funtyp(bk)>-1 then
5748             if or(bk==act) | or(bk==cap) then
5749                 if stalone then
5750                     txt=[txt;
5751                     "    "+call_block42(bk,pt,9)
5752                     ""];
5753                 end
5754             else
5755                 txt2=call_block42(bk,pt,9);
5756                 if txt2<>[] then
5757                     txt=[txt;
5758                     "    "+txt2
5759                     ""];
5760                 end
5761             end
5762
5763             //** ifthenelse blk
5764         elseif funtyp(bk)==-1 then
5765             ix=-1+inplnk(inpptr(bk));
5766             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5767             //** C **//
5768             tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5769             //*******//
5770             thentxt=write_code_zzdoit(clkptr(bk),9);
5771             elsetxt=write_code_zzdoit(clkptr(bk)+1,9);
5772             txt=[txt;
5773             "    "+get_comment("ifthenelse_blk",list(bk));]
5774             //** C **//
5775             txt=[txt;
5776             "    g["+string(zcptr(bk)-1)+"]=(double)"+tmp_+";"]
5777             //*******//
5778             if thentxt<>[] | elsetxt<>[] then
5779                 //** C **//
5780                 txt=[txt;
5781                 "    if (g["+string(zcptr(bk)-1)+"] > 0.){"]
5782                 //*******//
5783                 txt=[txt;
5784                 Indent+thentxt]
5785                 //** C **//
5786                 txt=[txt;
5787                 "      }"]
5788                 //*******//
5789                 if elsetxt <> [] then
5790                     //** C **//
5791                     txt=[txt;
5792                     "      else {"]
5793                     //*******//
5794                     txt=[txt;
5795                     Indent+elsetxt]
5796                     //** C **//
5797                     txt=[txt;
5798                     "      }"]
5799                     //*******//
5800                 end
5801             end
5802             //** C **//
5803             txt=[txt;
5804             "    if(phase==1 && block_"+rdnom+"["+string(bk-1)+"].nmode > 0){"
5805             "      if (g["+string(zcptr(bk)-1)+"] > 0.){"
5806             "        block_"+rdnom+"["+string(bk-1)+"].mode[0] = 1;"
5807             "      }"
5808             "      else {"
5809             "        block_"+rdnom+"["+string(bk-1)+"].mode[0] = 2;"
5810             "      }"
5811             "    }"]
5812             //*******//
5813             //** eventselect blk
5814         elseif funtyp(bk)==-2 then
5815             Noutport=clkptr(bk+1)-clkptr(bk);
5816             ix=-1+inplnk(inpptr(bk));
5817             TYPE=mat2c_typ(outtb(ix+1));  //** scilab index start from 1
5818             //** C **//
5819             tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5820             //*******//
5821             II=[];
5822             switchtxt=list()
5823             for i=1:Noutport
5824                 switchtxt(i)=write_code_zzdoit(clkptr(bk)+i-1,9);
5825                 if switchtxt(i)<>[] then II=[II i];end
5826             end
5827             txt=[txt;
5828             "    "+get_comment("evtselect_blk",list(bk));]
5829             if II<>[] then
5830                 //** C **//
5831                 txt=[txt;
5832                 "    j=max(min((int) "+...
5833                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].nevout),1);"]
5834                 txt=[txt;
5835                 "    switch(j)"
5836                 "    {"];
5837                 //*******//
5838                 for i=II
5839                     //** C **//
5840                     txt=[txt;
5841                     "     case "+string(j)+" :";]
5842                     //*******//
5843                     txt=[txt;
5844                     BigIndent+write_code_zzdoit(clkptr(bk)+i-1,9);]
5845                     //** C **//
5846                     txt=[txt;
5847                     BigIndent+"break;"]
5848                     //*******//
5849                 end
5850                 //** C **//
5851                 txt=[txt;
5852                 "    }"];
5853                 //*******//
5854             end
5855             //** C **//
5856             txt=[txt;
5857             "  for (jj=0;jj<block_"+rdnom+"["+string(fun-1)+"].nevout-1;++jj) {"
5858             "    g["+string(zcptr(bk)-1)+"+jj]=(double)"+tmp_+"-(double)(jj+2);"
5859             "  }"
5860             "  if(phase==1 && block_"+rdnom+"["+string(bk-1)+"].nmode>0){"
5861             "    j=max(min((int) "+tmp_+","
5862             "              block_"+rdnom+"["+string(bk-1)+"].nevout),1);"
5863             "    block_"+rdnom+"["+string(bk-1)+"].mode[0]= j;"
5864             "  }"]
5865             //*******//
5866             //** Unknown block
5867         else
5868             error("Unknown block type "+string(bk));
5869         end
5870     end
5871
5872 endfunction
5873
5874 //write_code_zzdoit : generate body of the code for
5875 //                    flag 9
5876 //
5877 //input: ev  : evt number for block's call
5878 //      flag : flag number for block's call
5879 //
5880 //output : txt for flag 9
5881 //
5882 //12/07/07 Alan Layec
5883 function [txt]=write_code_zzdoit(ev,flag)
5884     txt=[];
5885
5886     for j=ordptr(ev):ordptr(ev+1)-1
5887         bk=ordclk(j,1);
5888         pt=ordclk(j,2);
5889         //** blk
5890         if funtyp(bk)>-1 then
5891             if or(bk==act) | or(bk==cap) then
5892                 if stalone then
5893                     txt=[txt;
5894                     "    "+call_block42(bk,pt,flag)
5895                     ""];
5896                 end
5897             else
5898                 txt2=call_block42(bk,pt,flag);
5899                 if txt2<>[] then
5900                     txt=[txt;
5901                     "    "+txt2
5902                     ""];
5903                 end
5904             end
5905             //** ifthenelse blk
5906         elseif funtyp(bk)==-1 then
5907             ix=-1+inplnk(inpptr(bk));
5908             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5909             //** C **//
5910             tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5911             //*******//
5912             thentxt=write_code_zzdoit(clkptr(bk),9);
5913             elsetxt=write_code_zzdoit(clkptr(bk)+1,9);
5914             txt=[txt;
5915             "    "+get_comment("ifthenelse_blk",list(bk));]
5916             //** C **//
5917             txt=[txt;
5918             "    g["+string(zcptr(bk)-1)+"]=(double)"+tmp_+";"]
5919             //*******//
5920             if thentxt<>[] | elsetxt<>[] then
5921                 //** C **//
5922                 txt=[txt;
5923                 "    if (g["+string(zcptr(bk)-1)+"] > 0.){"]
5924                 //*******//
5925                 txt=[txt;
5926                 Indent+thentxt]
5927                 //** C **//
5928                 txt=[txt;
5929                 "      }"]
5930                 //*******//
5931                 if elsetxt <> [] then
5932                     //** C **//
5933                     txt=[txt;
5934                     "      else {"]
5935                     //*******//
5936                     txt=[txt;
5937                     Indent+elsetxt]
5938                     //** C **//
5939                     txt=[txt;
5940                     "      }"]
5941                     //*******//
5942                 end
5943             end
5944             //** C **//
5945             txt=[txt;
5946             "    if(phase==1 && block_"+rdnom+"["+string(bk-1)+"].nmode > 0){"
5947             "      if (g["+string(zcptr(bk)-1)+"] > 0.){"
5948             "        block_"+rdnom+"["+string(bk-1)+"].mode[0] = 1;"
5949             "      }"
5950             "      else {"
5951             "        block_"+rdnom+"["+string(bk-1)+"].mode[0] = 2;"
5952             "      }"
5953             "    }"]
5954             //*******//
5955
5956             //** eventselect blk
5957         elseif funtyp(bk)==-2 then
5958             Noutport=clkptr(bk+1)-clkptr(bk);
5959             ix=-1+inplnk(inpptr(bk));
5960             TYPE=mat2c_typ(outtb(ix+1));  //** scilab index start from 1
5961             //** C **//
5962             tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5963             //*******//
5964             II=[];
5965             switchtxt=list()
5966             for i=1:Noutport
5967                 switchtxt(i)=write_code_zzdoit(clkptr(bk)+i-1,9);
5968                 if switchtxt(i)<>[] then II=[II i];end
5969             end
5970             txt=[txt;
5971             "    "+get_comment("evtselect_blk",list(bk));]
5972             if II<>[] then
5973                 //** C **//
5974                 txt=[txt;
5975                 "    j=max(min((int) "+...
5976                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].nevout),1);"]
5977                 txt=[txt;
5978                 "    switch(j)"
5979                 "    {"];
5980                 //*******//
5981                 for i=II
5982                     //** C **//
5983                     txt=[txt;
5984                     "     case "+string(j)+" :";]
5985                     //*******//
5986                     txt=[txt;
5987                     BigIndent+write_code_zzdoit(clkptr(bk)+i-1,9);]
5988                     //** C **//
5989                     txt=[txt;
5990                     BigIndent+"break;"]
5991                     //*******//
5992                 end
5993                 //** C **//
5994                 txt=[txt;
5995                 "    }"];
5996                 //*******//
5997             end
5998             //** C **//
5999             txt=[txt;
6000             "  for (jj=0;jj<block_"+rdnom+"["+string(fun-1)+"].nevout-1;++jj) {"
6001             "    g["+string(zcptr(bk)-1)+"+jj]=(double)"+tmp_+"-(double)(jj+2);"
6002             "  }"
6003             "  if(phase==1 && block_"+rdnom+"["+string(bk-1)+"].nmode>0){"
6004             "    j=max(min((int) "+tmp_+","
6005             "              block_"+rdnom+"["+string(bk-1)+"].nevout),1);"
6006             "    block_"+rdnom+"["+string(bk-1)+"].mode[0]= j;"
6007             "  }"]
6008             //*******//
6009
6010             //** Unknown block
6011      &nb