cb35d28ae87653db7b3b747e75796b09a14a3eea
[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 calling sequence
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 calling sequence
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=[];
1179     numc=[];
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=xget('window')
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:lstsize(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     //** xset('window',%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:lstsize(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     end
1805
1806 endfunction
1807
1808 //BlockProto : generate prototype
1809 //            of a scicos block
1810 //
1811 //inputs : bk   : bloc index
1812 //
1813 //output : txt  :
1814 //
1815 //16/06/07 Author : A.Layec
1816 function [txt]=BlockProto(bk)
1817
1818     nin=inpptr(bk+1)-inpptr(bk);  //* number of input ports */
1819     nout=outptr(bk+1)-outptr(bk); //* number of output ports */
1820
1821     //**
1822     ftyp=funtyp(bk)
1823     if ftyp>2000 then ftyp=ftyp-2000,end
1824     if ftyp>1000 then ftyp=ftyp-1000,end
1825
1826     //** check function type
1827     if ftyp < 0 then //** ifthenelse eselect blocks
1828         txt = [];
1829         return;
1830     else
1831         if (ftyp<>0 & ftyp<>1 & ftyp<>2 & ftyp<>3 & ftyp<>4) then
1832             disp("types other than 0,1,2,3 or 4 are not yet supported.")
1833             txt = [];
1834             return;
1835         end
1836     end
1837
1838     //** add comment
1839     txt=[get_comment("proto_blk",list(funs(bk),funtyp(bk),bk,labels(bk)));]
1840
1841     select ftyp
1842         //** zero funtyp
1843     case 0 then
1844
1845         //*********** prototype definition ***********//
1846         txtp=["(int *, int *, double *, double *, double *, int *, double *, \";
1847         " int *, double *, int *, double *, int *,int *, int *, \";
1848         " double *, int *, double *, int *);"];
1849         if (funtyp(bk)>2000 & funtyp(bk)<3000)
1850             blank = get_blank("void "+funs(bk)+"(");
1851             txtp(1) = "void "+funs(bk)+txtp(1);
1852         elseif (funtyp(bk)<2000)
1853             txtp(1) = "void C2F("+funs(bk)+")"+txtp(1);
1854             blank = get_blank("void C2F("+funs(bk)+")");
1855         end
1856         txtp(2:$) = blank + txtp(2:$);
1857         txt = [txt;txtp];
1858         //*******************************************//
1859
1860
1861         //**
1862     case 1 then
1863
1864         //*********** prototype definition ***********//
1865         txtp=["(int *, int *, double *, double *, double *, int *, double *, \";
1866         " int *, double *, int *, double *, int *,int *, int *";]
1867         if (funtyp(bk)>2000 & funtyp(bk)<3000)
1868             blank = get_blank("void "+funs(bk)+"(");
1869             txtp(1) = "void "+funs(bk)+txtp(1);
1870         elseif (funtyp(bk)<2000)
1871             txtp(1) = "void C2F("+funs(bk)+")"+txtp(1);
1872             blank = get_blank("void C2F("+funs(bk)+")");
1873         end
1874         if nin>=1 | nout>=1 then
1875             txtp($)=txtp($)+", \"
1876             txtp=[txtp;""]
1877             if nin>=1 then
1878                 for k=1:nin
1879                     txtp($)=txtp($)+" double *, int * ,"
1880                 end
1881                 txtp($)=part(txtp($),1:length(txtp($))-1); //remove last ,
1882             end
1883             if nout>=1 then
1884                 if nin>=1 then
1885                     txtp($)=txtp($)+", \"
1886                     txtp=[txtp;""]
1887                 end
1888                 for k=1:nout
1889                     txtp($)=txtp($)+" double *, int * ,"
1890                 end
1891                 txtp($)=part(txtp($),1:length(txtp($))-1); //remove last ,
1892             end
1893         end
1894
1895         if ztyp(bk) then
1896             txtp($)=txtp($)+", \"
1897             txtp=[txtp;" double *,int *);"];
1898         else
1899             txtp($)=txtp($)+");";
1900         end
1901
1902         txtp(2:$) = blank + txtp(2:$);
1903         txt = [txt;txtp];
1904         //*******************************************//
1905
1906         //**
1907     case 2 then
1908
1909         //*********** prototype definition ***********//
1910
1911         txtp=["void "+funs(bk)+...
1912         "(int *, int *, double *, double *, double *, int *, double *, \";
1913         " int *, double *, int *, double *, int *, int *, int *, \"
1914         " double **, int *, int *, double **,int *, int *"];
1915         if ~ztyp(bk) then
1916             txtp($)=txtp($)+");";
1917         else
1918             txtp($)=txtp($)+", \";
1919             txtp=[txtp;
1920             " double *,int *);"]
1921         end
1922         blank = get_blank("void "+funs(bk));
1923         txtp(2:$) = blank + txtp(2:$);
1924         txt = [txt;txtp];
1925         //********************************************//
1926
1927         //**
1928     case 4 then
1929         txt=[txt;
1930         "void "+funs(bk)+"(scicos_block *, int );"];
1931
1932     end
1933 endfunction
1934
1935 //Generating the routine for actuators interfacing
1936 //
1937 //
1938 //Authors : R. Djenid, R. Nikoukhah, A. Layec
1939 //
1940 //actt=[i uk nuk_1 nuk_2 uk_t bllst(i).ipar]
1941 function Code=make_actuator(standalone)
1942     Call=["/*"
1943     "**    Generated by Code_Generation toolbox of Xcos with "+getversion()
1944     "**    Date : "+date()
1945     "*/"
1946     ""
1947     "/*"+part("-",ones(1,40))+" Actuators */";
1948     "void "+rdnom+"_actuator(int *flag, int *nport, int *nevprt, double *t, void *u, int *nu1, int *nu2, int *ut, int flag1)"]
1949
1950     comments=["     /*"
1951     "      * To be customized for standalone execution";
1952     "      * flag  : specifies the action to be done"
1953     "      * nport : specifies the  index of the Super Bloc"
1954     "      *         regular input (The input ports are numbered"
1955     "      *         from the top to the bottom )"
1956     "      * nevprt: indicates if an activation had been received"
1957     "      *         0 = no activation"
1958     "      *         1 = activation"
1959     "      * t     : the current time value"
1960     "      * u     : the vector inputs value"
1961     "      * nu1   : the input size 1"
1962     "      * nu2   : the input size 2"
1963     "      * ut    : the input type"
1964     "      * flag1 : learn mode (0 from terminal,1 from input file"
1965     "      */"]
1966
1967     dcl=["{"
1968     "  int j,k,l;"];
1969
1970     if standalone then
1971         a_actuator=["  /* skeleton to be customized */"
1972         "    switch (*flag) {"
1973         "    /* OutputUpdate (*flag==1) will be called on each iteration */"
1974         "    case 1 :"
1975         "      /*if(*nevprt>0) { get the input value */"
1976         "        switch (*ut) {"
1977         "        case 10 :"
1978         "          for (l=0;l<*nu2;l++) {"
1979         "            for (k=0;k<*nu1;k++) {"
1980         "              printf(""Actuator: time=%f, "+...
1981         "u(%d,%d) of actuator %d is %f "+...
1982         "\n"", \"
1983         "                     *t, k, l, *nport,"+...
1984         "*((double *) u+(k+l*(*nu1))));"
1985         "            }"
1986         "          }"
1987         "          break;"
1988         ""
1989         "        case 11 :"
1990         "          for (l=0;l<*nu2;l++) {"
1991         "            for (k=0;k<*nu1;k++) {"
1992         "              printf(""Actuator: time=%f, "+...
1993         "u(%d,%d) of actuator %d is %f,%f "+...
1994         "\n"", \"
1995         "                     *t, k, l, *nport,"+...
1996         "*((double *) u+(k+l*(*nu1))),"+...
1997         "*((double *) u+((*nu1)*(*nu2)+k+l*(*nu1))));"
1998         "            }"
1999         "          }"
2000         "          break;"
2001         ""
2002         "        case 81 :"
2003         "          for (l=0;l<*nu2;l++) {"
2004         "            for (k=0;k<*nu1;k++) {"
2005         "              printf(""Actuator: time=%f, "+...
2006         "u(%d,%d) of actuator %d is %i "+...
2007         "\n"", \"
2008         "                     *t, k, l, *nport,"+...
2009         "*((char *) u+(k+l*(*nu1))));"
2010         "            }"
2011         "          }"
2012         "          break;"
2013         ""
2014         "        case 82 :"
2015         "          for (l=0;l<*nu2;l++) {"
2016         "            for (k=0;k<*nu1;k++) {"
2017         "              printf(""Actuator: time=%f, "+...
2018         "u(%d,%d) of actuator %d is %hd "+...
2019         "\n"", \"
2020         "                     *t, k, l, *nport,"+...
2021         "*((short *) u+(k+l*(*nu1))));"
2022         "            }"
2023         "          }"
2024         "          break;"
2025         ""
2026         "        case 84 :"
2027         "          for (l=0;l<*nu2;l++) {"
2028         "            for (k=0;k<*nu1;k++) {"
2029         "              printf(""Actuator: time=%f, "+...
2030         "u(%d,%d) of actuator %d is %ld "+...
2031         "\n"", \"
2032         "                     *t, k, l, *nport,"+...
2033         "*((long *) u+(k+l*(*nu1))));"
2034         "            }"
2035         "          }"
2036         "          break;"
2037         ""
2038         "        case 811 :"
2039         "          for (l=0;l<*nu2;l++) {"
2040         "            for (k=0;k<*nu1;k++) {"
2041         "              printf(""Actuator: time=%f, "+...
2042         "u(%d,%d) of actuator %d is %d "+...
2043         "\n"", \"
2044         "                     *t, k, l, *nport,"+...
2045         "*((unsigned char *) u+(k+l*(*nu1))));"
2046         "            }"
2047         "          }"
2048         "          break;"
2049         ""
2050         "        case 812 :"
2051         "          for (l=0;l<*nu2;l++) {"
2052         "            for (k=0;k<*nu1;k++) {"
2053         "              printf(""Actuator: time=%f, "+...
2054         "u(%d,%d) of actuator %d is %hu "+...
2055         "\n"", \"
2056         "                     *t, k, l, *nport,"+...
2057         "*((unsigned short *) u+(k+l*(*nu1))));"
2058         "            }"
2059         "          }"
2060         "          break;"
2061         ""
2062         "        case 814 :"
2063         "          for (l=0;l<*nu2;l++) {"
2064         "            for (k=0;k<*nu1;k++) {"
2065         "              printf(""Actuator: time=%f, "+...
2066         "u(%d,%d) of actuator %d is %lu "+...
2067         "\n"", \"
2068         "                     *t, k, l, *nport,"+...
2069         "*((unsigned long *) u+(k+l*(*nu1))));"
2070         "            }"
2071         "          }"
2072         "          break;"
2073         "        }"
2074         "      /*} */"
2075         "      break;"
2076         "    case 4 : /* actuator initialisation */"
2077         "      /* do whatever you want to initialize the actuator */"
2078         "      break;"
2079         "    case 5 : /* actuator ending */"
2080         "      /* do whatever you want to end the actuator */"
2081         "      break;"
2082         "    }"]
2083     else
2084         a_actuator=[]
2085     end
2086
2087     // pour fprintf
2088     nc=size(act,"*") // Alan : d'o? viens act ?????
2089     // reponse : de do_compile_superblock!
2090     typ=["""%f "]; //time
2091     for i=1:nc
2092         typ($)=typ($)+""" \"
2093         typ=[typ;""""];
2094         for j=1:sum(actt(i,3)*actt(i,4))
2095             //typ=typ+'%f ';
2096             typ($)=typ($)+scs_c_n2c_fmt(actt(i,5))+" ";
2097         end
2098     end
2099     typ($)=typ($)+"\n"", \"
2100     typ(1)="    fprintf(fprw,"+typ(1);
2101     bl    ="                 ";
2102     if size(typ,1) <> 1 then
2103         typ(2:$) = bl+typ(2:$);
2104     end
2105     //Code1='    fprintf(fprw, '"'+typ+' \n'",*t'
2106     Code1=[typ;bl+"*t"];
2107     //actt=[i uk nuk_1 nuk_2 uk_t bllst(i).ipar]
2108     for i=1:size(actt,1)
2109         ni=actt(i,3)*actt(i,4) // dimension of ith output
2110         Code1($)=Code1($)+","
2111         Code1=[Code1;bl];
2112         for j=1:ni
2113             if actt(i,5)<>11 then
2114                 Code1($)=Code1($)+...
2115                 "*((("+scs_c_n2c_typ(actt(i,5))+" *)("+...
2116                 rdnom+"_block_outtbptr+"+string(actt(i,2)-1)+"))"+...
2117                 "+"+string(j-1)+")";
2118             else //CAS CMPLX
2119                 Code1($)=Code1($)+...
2120                 "*((("+scs_c_n2c_typ(actt(i,5))+" *)("+...
2121                 rdnom+"_block_outtbptr+"+string(actt(i,2)-1)+"))"+...
2122                 "+"+string((j-1))+"), "+...
2123                 "*((("+scs_c_n2c_typ(actt(i,5))+" *)("+...
2124                 rdnom+"_block_outtbptr+"+string(actt(i,2)-1)+"))"+...
2125                 "+"+string(ni+(j-1))+")";
2126             end
2127             if j<>ni then
2128                 Code1($)=Code1($)+", ";
2129             end
2130         end
2131     end
2132     Code1($)=Code1($)+");"
2133
2134     Code=[]
2135
2136     if nc==1|~standalone then
2137         Code=[Call
2138         comments
2139         dcl
2140         "  if (flag1 == 0) {"
2141         a_actuator
2142         "  }"
2143         "  else if (flag1 == 1) {"
2144         "    if (*flag == 4 && *nport == 1) {"
2145         "      fprw=fopen(output,'"wt'");"
2146         "      if( fprw == NULL )"
2147         "        {"
2148         "          printf('"Error opening file: %s\n'", output);"
2149         "          return;"
2150         "        }"
2151         "    }else if (*flag == 2 /* && *nevprt>0 */) {"
2152         Code1
2153         "    }else if (*flag == 5 && *nport == 1) {"
2154         "      fclose(fprw);"
2155         "    }"
2156         "  }"
2157         "}"]
2158     elseif nc>1 then
2159         S="  switch (*nport) {"
2160         for k=1:nc
2161             S=[S;
2162             "  case "+string(k)+" :/* Port number "+string(k)+" ----------*/"
2163             "  "+a_actuator
2164             "  break;"]
2165         end
2166         S=[S;"  }"]
2167
2168         Code=[Code
2169         Call
2170         comments
2171         dcl
2172         "  if (flag1 == 0){"
2173         S
2174         "  }"
2175         "  else if (flag1 == 1) {"
2176         "    if (*flag == 4 && *nport == 1) {"
2177         "      fprw=fopen(output,'"wt'");"
2178         "      if( fprw == NULL ) {"
2179         "        printf('"Error opening file: %s\n'", output);"
2180         "        return;"
2181         "        }"
2182         "    }"
2183         "    else if (*flag == 2 /*&& *nevprt>0*/ ) {"
2184         Code1
2185         "    }"
2186         "    else if (*flag == 5 && *nport == 1) {"
2187         "      fclose(fprw);"
2188         "    }"
2189         "  }"
2190         "}"]
2191     end
2192 endfunction
2193
2194 //** Generates the scicos computational function
2195 //   associated with the block
2196 //12/07/07 Alan Layec
2197 function make_computational42(filename)
2198     z=cpr.state.z;
2199     oz=cpr.state.oz;
2200     outtb=cpr.state.outtb;
2201     tevts=cpr.state.tevts;
2202     evtspt=cpr.state.evtspt;
2203     outptr=cpr.sim.outptr;
2204     funtyp=cpr.sim.funtyp;
2205     clkptr=cpr.sim.clkptr;
2206     ordptr=cpr.sim.ordptr;
2207     pointi=cpr.state.pointi;
2208     ztyp=cpr.sim.ztyp;
2209     zcptr=cpr.sim.zcptr;
2210     zptr=cpr.sim.zptr;
2211     ozptr=cpr.sim.ozptr;
2212     opptr=cpr.sim.opptr;
2213     opar=cpr.sim.opar;
2214     rpptr=cpr.sim.rpptr;
2215     ipptr=cpr.sim.ipptr;
2216     inpptr=cpr.sim.inpptr;
2217     funs=cpr.sim.funs;
2218     xptr=cpr.sim.xptr;
2219     modptr=cpr.sim.modptr;
2220     inplnk=cpr.sim.inplnk;
2221     nblk=cpr.sim.nb;
2222     outlnk=cpr.sim.outlnk;
2223     oord=cpr.sim.oord;
2224     zord=cpr.sim.zord;
2225     iord=cpr.sim.iord;
2226     noord=size(cpr.sim.oord,1);
2227     nzord=size(cpr.sim.zord,1);
2228     niord=size(cpr.sim.iord,1);
2229
2230     Indent="  ";
2231     Indent2=Indent+Indent;
2232     BigIndent="          ";
2233
2234     nZ=size(z,"*"); //** index of work in z
2235     nO=lstsize(oz); //** index of outtb in oz
2236
2237     stalone=%f
2238
2239     // Open file to write in
2240     fd = mopen(filename, "wt");
2241
2242     mputl(["/*"
2243     "**    SCILAB Computational function"
2244     "**    Generated by Code_Generation toolbox of Xcos with "+getversion()
2245     "**    Date : "+date()
2246     "*/"
2247     ""
2248     "/* ---- Headers ---- */"
2249     "#include <stdio.h>"
2250     "#include <stdlib.h>"
2251     "#include <memory.h>"
2252     "#include <string.h>"
2253     "#include ""machine.h"" "
2254     "#include ""scicos.h"" "
2255     "#include ""scicos_malloc.h"" "
2256     "#include ""scicos_free.h"" "
2257     ""], fd);
2258
2259     if getos() == "Windows" then
2260
2261         mputl([" "
2262         "#define max(a,b) ((a) >= (b) ? (a) : (b))"
2263         "#define min(a,b) ((a) <= (b) ? (a) : (b))"
2264         " "
2265         ], fd);
2266     end
2267
2268     mputl([Protos ; ""], fd);
2269
2270     mputl(["/* Table of constant values */"
2271     "static int nrd_"+string(0:maxtotal)'+" = "+string(0:maxtotal)'+";"], fd);
2272
2273     if maxtotal<10 then
2274         mputl("static int nrd_10 = 10;", fd);
2275     end
2276     if maxtotal<11 then
2277         mputl("static int nrd_11 = 11;", fd);
2278     end
2279
2280     if maxtotal<81 then
2281         mputl("static int nrd_81 = 81;", fd);
2282     end
2283     if maxtotal<82 then
2284         mputl("static int nrd_82 = 82;", fd);
2285     end
2286     if maxtotal<84 then
2287         mputl("static int nrd_84 = 84;", fd);
2288     end
2289     if maxtotal<811 then
2290         mputl("static int nrd_811 = 811;", fd);
2291     end
2292     if maxtotal<812 then
2293         mputl("static int nrd_812 = 812;", fd);
2294     end
2295     if maxtotal<814 then
2296         mputl("static int nrd_814 = 814;", fd);
2297     end
2298
2299     mputl([""
2300     "/* Some general static variables */"
2301     "static double zero=0;"
2302     "static double w[1];"
2303     "static int aaa=0, bbb=0;"], fd);
2304
2305     mputl([""
2306     "/*"+part("-",ones(1,40))+" Block Computational function */ ";
2307     "int  "+rdnom+"(scicos_block *block, int flag)"
2308     "{"
2309     "  /* declaration of local variables for that block struct */"
2310     "  double* z      = block->z;"
2311     "  void **ozptr   = block->ozptr;"
2312     "  void **oparptr = block->oparptr;"
2313     "  int nopar      = block->nopar;"
2314     "  double* x      = block->x;"
2315     "  double* xd     = block->xd;"
2316     "  double* res    = block->res;"
2317     "  void** u       = block->inptr;"
2318     "  void** y       = block->outptr;"
2319     "  int nevprt     = block->nevprt;"
2320     "  double* rpar   = block->rpar;"
2321     "  int nrpar      = block->nrpar;"
2322     "  int* ipar      = block->ipar;"
2323     "  int nipar      = block->nipar;"
2324     "  double* g      = block->g;"
2325     "  int* mode      = block->mode;"
2326     "  void **work    = block->work;"
2327     ""
2328     "  double t     = get_scicos_time();"
2329     "  int    phase = get_phase_simulation();"
2330     ""
2331     "  int kf = 0;"
2332     "  int i = 0;"
2333     "  int* reentryflag = NULL;"
2334     ""
2335     "  double *args[100];"
2336     "  int local_flag;"
2337     "  int nport;"
2338     "  void **"+rdnom+"_block_outtbptr;"
2339     "  scicos_block *block_"+rdnom+";"
2340     ""
2341     "  /*  work of blocks are catenated at the end of z */"
2342     "  work = (void **)(z+"+string(nZ)+");"
2343     ""
2344     "  /*  "+rdnom+"_block_outtbptr is catenated at the end of oz */"
2345     "  "+rdnom+"_block_outtbptr = (void **)(ozptr+"+string(nO)+");"
2346     ""
2347     "  /* struct of all blocks are stored in work of that block struct */"
2348     "  block_"+rdnom+"=(scicos_block*) *block->work;"
2349     ""
2350     "  /* Copy inputs in the block outtb */"], fd);
2351
2352
2353     for i=1:size(capt,1)
2354         ni=capt(i,3)*capt(i,4); //** dimension of ith input
2355         if capt(i,5)<>11 then
2356             mputl(["  memcpy(*("+rdnom+"_block_outtbptr+"+string(capt(i,2)-1)+"),"+...
2357             "*(u+"+string(capt(i,6)-1)+"),"+...
2358             string(ni)+"*sizeof("+mat2c_typ(capt(i,5))+"));"], fd);
2359         else //** Cas cmplx
2360             mputl(["  memcpy(*("+rdnom+"_block_outtbptr+"+string(capt(i,2)-1)+"),"+...
2361             "*(u+"+string(capt(i,6)-1)+"),"+...
2362             string(2*ni)+"*sizeof("+mat2c_typ(capt(i,5))+"));"], fd);
2363         end
2364     end
2365
2366     mputl([""
2367     "  if (flag != 4 && flag != 6 && flag != 5){"
2368     "    reentryflag=(int*) ((scicos_block *)(*block->work)+"+string(nblk)+");"
2369     "    if (*reentryflag==0){"
2370     "      *reentryflag=1;"], fd);
2371
2372     for kf=1:nblk
2373         nin=inpptr(kf+1)-inpptr(kf);  //** number of input ports
2374         nout=outptr(kf+1)-outptr(kf); //** number of output ports
2375         nx=xptr(kf+1)-xptr(kf);
2376         ng=zcptr(kf+1)-zcptr(kf);
2377         nmode=modptr(kf+1)-modptr(kf);
2378
2379         //** add comment
2380         mputl([""
2381         "      "+get_comment("set_blk",list(funs(kf),funtyp(kf),kf,labels(kf)));], fd);
2382
2383         for k=1:nin
2384             lprt=inplnk(inpptr(kf)-1+k);
2385             mputl(["      block_"+rdnom+"["+string(kf-1)+"].inptr["+string(k-1)+"]  = "+...
2386             rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2387         end
2388         for k=1:nout
2389             lprt=outlnk(outptr(kf)-1+k);
2390             mputl(["      block_"+rdnom+"["+string(kf-1)+"].outptr["+string(k-1)+"] = "+...
2391             rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2392         end
2393         mputl(["      block_"+rdnom+"["+string(kf-1)+"].z         = &(z["+...
2394         string(zptr(kf)-1)+"]);"], fd);
2395         if nx <> 0 then
2396             mputl(["      block_"+rdnom+"["+string(kf-1)+"].x         = &(x["+...
2397             string(xptr(kf)-1)+"]);"], fd);
2398         else
2399             mputl(["      block_"+rdnom+"["+string(kf-1)+"].x         = &(zero);"
2400             "      block_"+rdnom+"["+string(kf-1)+"].xd        = w;"], fd);
2401         end
2402         if ng <> 0 then
2403             mputl(["      block_"+rdnom+"["+string(kf-1)+"].g         = &(g["+...
2404             string(zcptr(kf)-1)+"]);"], fd);
2405         else
2406             mputl(["      block_"+rdnom+"["+string(kf-1)+"].g         = &(zero);";], fd);
2407         end
2408         if nmode <> 0 then
2409             mputl(["      block_"+rdnom+"["+string(kf-1)+"].mode      = &(mode["+...
2410             string(modptr(kf)-1)+"]);"], fd);
2411         end
2412         if (part(funs(kf),1:7) ~= "capteur" &...
2413             part(funs(kf),1:10) ~= "actionneur" &...
2414             funs(kf) ~= "bidon" &...
2415             funs(kf) ~= "bidon2") then
2416             //** rpar **//
2417             if (rpptr(kf+1)-rpptr(kf)>0) then
2418                 mputl(["      block_"+rdnom+"["+string(kf-1)+"].rpar      = &(rpar["+...
2419                 string(rpptr(kf)-1)+"]);"], fd);
2420             end
2421             //** ipar **//
2422             if (ipptr(kf+1)-ipptr(kf)>0) then
2423                 mputl(["      block_"+rdnom+"["+string(kf-1)+"].ipar      = &(ipar["+...
2424                 string(ipptr(kf)-1)+"]);"], fd);
2425             end
2426             //** opar **//
2427             if (opptr(kf+1)-opptr(kf)>0) then
2428                 nopar = opptr(kf+1)-opptr(kf);
2429                 for k=1:nopar
2430                     mputl(["      block_"+rdnom+"["+string(kf-1)+"].oparptr["+string(k-1)+...
2431                     "] = oparptr["+string(opptr(kf)-1+k-1)+"];"], fd);
2432                 end
2433             end
2434             //** oz **//
2435             if (ozptr(kf+1)-ozptr(kf)>0) then
2436                 noz = ozptr(kf+1)-ozptr(kf);
2437                 for k=1:noz
2438                     mputl(["      block_"+rdnom+"["+string(kf-1)+"].ozptr["+string(k-1)+...
2439                     "]  = ozptr["+string(ozptr(kf)-1+k-1)+"];"], fd);
2440                 end
2441             end
2442         end
2443         mputl(["      block_"+rdnom+"["+string(kf-1)+"].work      ="+...
2444         " (void **)(((double *)work)+"+string(kf-1)+");"], fd);
2445     end
2446
2447     mputl(["    }"
2448     "  }"
2449     ""
2450     ], fd);
2451
2452     /////////////////////////////////////////////
2453
2454
2455     //** find activation number
2456     blks=find(funtyp>-1);
2457     evs=[];
2458
2459     for blk=blks
2460         for ev=clkptr(blk):clkptr(blk+1)-1
2461             if funs(blk)=="bidon" then
2462                 if ev > clkptr(howclk) -1
2463                     evs=[evs,ev];
2464                 end
2465             end
2466         end
2467     end
2468
2469     //** flag 0
2470     flag = 0;
2471
2472     block_has_output=%f
2473     txt   = [];
2474     txt22 = [];
2475
2476     for kf=1:nblk
2477         nx=xptr(kf+1)-xptr(kf);
2478         if nx <> 0 then
2479             txt=[txt;
2480             "    block_"+rdnom+"["+string(kf-1)+"].xd=&(xd["+...
2481             string(xptr(kf)-1)+"]);"]
2482         end
2483         if part(funs(kf),1:10) == "actionneur" then
2484             block_has_output=%t
2485         end
2486     end
2487     if txt<>[] then
2488         txt22=[txt22;
2489         "    "+get_comment("update_xd",list())
2490         txt
2491         ""
2492         ]
2493     end
2494     txt22=[txt22;
2495     write_code_odoit(1) //** first pass
2496     write_code_odoit(0) //** second pass
2497     ]
2498
2499     if txt22<>[] then
2500         mputl(["  if (flag == 0) { "+get_comment("flag",list(flag))
2501         txt22
2502         "  }"], fd);
2503     end
2504
2505     //** flag 1,2,3
2506     for flag=[1,2,3]
2507
2508         txt3=[]
2509
2510         //** continuous time blocks must be activated
2511         //** for flag 1
2512         if flag==1 then
2513             txt = write_code_cdoit(flag);
2514
2515             if txt <> [] then
2516                 txt3=[txt3;
2517                 Indent+"  switch (nevprt) {"
2518                 ];
2519                 txt3=[txt3;
2520                 Indent2+"  case "+string(0)+" : "+...
2521                 get_comment("ev",list(0))
2522                 "    "+txt;
2523                 ];
2524                 txt3=[txt3;"      break;";""]
2525             end
2526         else
2527             txt=[];
2528         end
2529
2530         //** blocks with input discrete event must be activated
2531         //** for flag 1, 2 and 3
2532         if size(evs,2)>=1 then
2533             txt4=[]
2534             //**
2535             for ev=evs
2536                 txt2=write_code_doit(ev,flag);
2537                 if txt2<>[] then
2538                     //** adjust event number because of bidon block
2539                     new_ev=ev-(clkptr(howclk)-1)
2540                     //**
2541                     txt4=[txt4;
2542                     Indent2+["  case "+string(new_ev)+" : "+...
2543                     get_comment("ev",list(new_ev))
2544                     txt2];
2545                     "      break;";""]
2546                 end
2547             end
2548
2549             //**
2550             if txt == [] then
2551                 if txt4 <> [] then
2552                     txt3=[txt3;
2553                     Indent+"  switch (nevprt) {"
2554                     txt4
2555                     "    }"];
2556                 end
2557             else
2558                 txt3=[txt3;
2559                 txt4]
2560             end
2561         end
2562
2563         //**
2564         if txt <> [] then
2565             txt3=[txt3;
2566             "    }"];
2567         end
2568
2569         //**
2570         if txt3<>[] then
2571             if flag==1 & txt22==[] then
2572                 mputl(["  if (flag == "+string(flag)+") { "+...
2573                 get_comment("flag",list(flag))
2574                 txt3
2575                 "  }"], fd);
2576             else
2577                 mputl(["  else if (flag == "+string(flag)+") { "+...
2578                 get_comment("flag",list(flag))
2579                 txt3
2580                 "  }"], fd);
2581             end
2582         end
2583     end
2584
2585     //** flag 9
2586     ng=zcptr($)-1;
2587     if (ng ~= 0) then
2588         flag = 9;
2589         mputl(["  else if (flag == "+string(flag)+") { "+...
2590         get_comment("flag",list(flag))], fd);
2591
2592         txt=[]
2593         for kf=1:nblk
2594             if zcptr(kf+1)-zcptr(kf) <> 0 then
2595                 txt=[txt;
2596                 "    block_"+rdnom+"["+string(kf-1)+"].g="+...
2597                 "&(g["+string(zcptr(kf)-1)+"]);"]
2598             end
2599         end
2600
2601         mputl(["    "+get_comment("update_g",list())
2602         txt
2603         ""
2604         write_code_zdoit()
2605         ], fd);
2606
2607         mputl(["  }"], fd);
2608     end
2609
2610     //** flag 4
2611     mputl(["  else if (flag == 4) { "+get_comment("flag",list(4))
2612     "    if ((*block->work=scicos_malloc(sizeof(scicos_block)*"+...
2613     string(nblk)+"+sizeof(int)))== NULL) return 0;";
2614     "    reentryflag=(int*) ((scicos_block *)(*block->work)+"+string(nblk)+");"
2615     "    *reentryflag=0;"
2616     "    block_"+rdnom+"=(scicos_block*) *block->work;"], fd);
2617
2618     for kf=1:nblk
2619         nin=inpptr(kf+1)-inpptr(kf);  //* number of input ports */
2620         nout=outptr(kf+1)-outptr(kf); //* number of output ports */
2621         nx=xptr(kf+1)-xptr(kf);
2622         ng=zcptr(kf+1)-zcptr(kf);
2623         nmode=modptr(kf+1)-modptr(kf);
2624
2625         //** add comment
2626         mputl([""
2627         "    "+get_comment("set_blk",list(funs(kf),funtyp(kf),kf,labels(kf)));], fd);
2628
2629         mputl(["    block_"+rdnom+"["+string(kf-1)+"].type   = "+...
2630         string(funtyp(kf))+";";
2631         "    block_"+rdnom+"["+string(kf-1)+"].ztyp   = "+...
2632         string(ztyp(kf))+";";
2633         "    block_"+rdnom+"["+string(kf-1)+"].ng     = "+...
2634         string(zcptr(kf+1)-zcptr(kf))+";"], fd);
2635
2636         if nx <> 0 then
2637             mputl(["    block_"+rdnom+"["+string(kf-1)+"].nx     = "+...
2638             string(nx)+";";
2639             "    block_"+rdnom+"["+string(kf-1)+"].x      = &(x["+...
2640             string(xptr(kf)-1)+"]);"], fd);
2641         else
2642             mputl(["    block_"+rdnom+"["+string(kf-1)+"].nx     = 0;";
2643             "    block_"+rdnom+"["+string(kf-1)+"].x      = &(zero);"
2644             "    block_"+rdnom+"["+string(kf-1)+"].xd     = w;"], fd);
2645         end
2646
2647         if ng <> 0 then
2648             mputl(["    block_"+rdnom+"["+string(kf-1)+"].g      = &(g["+...
2649             string(zcptr(kf)-1)+"]);"], fd);
2650         else
2651             mputl(["    block_"+rdnom+"["+string(kf-1)+"].g      = &(zero);";], fd);
2652         end
2653         if nmode <> 0 then
2654             mputl(["    block_"+rdnom+"["+string(kf-1)+"].mode   = &(mode["+...
2655             string(modptr(kf)-1)+"]);"], fd);
2656         end
2657         mputl(["    block_"+rdnom+"["+string(kf-1)+"].nz     = "+...
2658         string(zptr(kf+1)-zptr(kf))+";";
2659         "    block_"+rdnom+"["+string(kf-1)+"].noz    = "+...
2660         string(ozptr(kf+1)-ozptr(kf))+";";
2661         "    block_"+rdnom+"["+string(kf-1)+"].nrpar  = "+...
2662         string(rpptr(kf+1)-rpptr(kf))+";";
2663         "    block_"+rdnom+"["+string(kf-1)+"].nopar  = "+...
2664         string(opptr(kf+1)-opptr(kf))+";";
2665         "    block_"+rdnom+"["+string(kf-1)+"].nipar  = "+...
2666         string(ipptr(kf+1)-ipptr(kf))+";"
2667         "    block_"+rdnom+"["+string(kf-1)+"].nin    = "+...
2668         string(inpptr(kf+1)-inpptr(kf))+";";
2669         "    block_"+rdnom+"["+string(kf-1)+"].nout   = "+...
2670         string(outptr(kf+1)-outptr(kf))+";";
2671         "    block_"+rdnom+"["+string(kf-1)+"].nevout = "+...
2672         string(clkptr(kf+1)-clkptr(kf))+";";
2673         "    block_"+rdnom+"["+string(kf-1)+"].nmode  = "+...
2674         string(modptr(kf+1)-modptr(kf))+";"], fd);
2675
2676         mputl(["    if ((block_"+rdnom+"["+string(kf-1)+"].evout  = "+...
2677         "calloc(block_"+rdnom+"["+string(kf-1)+"].nevout,sizeof(double)))== NULL) return 0;"], fd);
2678
2679         //***************************** input port *****************************//
2680         //** alloc insz/inptr **//
2681         mputl(["    if ((block_"+rdnom+"["+string(kf-1)+"].insz   = "+...
2682         "malloc(3*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nin))== NULL) return 0;";
2683         "    if ((block_"+rdnom+"["+string(kf-1)+"].inptr  = "+...
2684         "malloc(sizeof(void *)*block_"+rdnom+"["+string(kf-1)+"].nin))== NULL) return 0;"], fd);
2685
2686         //** inptr **//
2687         for k=1:nin
2688             lprt=inplnk(inpptr(kf)-1+k);
2689             mputl(["    block_"+rdnom+"["+string(kf-1)+"].inptr["+string(k-1)+"]  = "+...
2690             rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2691         end
2692
2693         //** 1st dim **//
2694         for k=1:nin
2695             lprt=inplnk(inpptr(kf)-1+k);
2696             mputl(["    block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1))+"]   = "+...
2697             string(size(outtb(lprt),1))+";"], fd);
2698         end
2699
2700         //** 2dn dim **//
2701         for k=1:nin
2702             lprt=inplnk(inpptr(kf)-1+k);
2703             mputl(["    block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1)+nin)+"]   = "+...
2704             string(size(outtb(lprt),2))+";"], fd);
2705         end
2706
2707         //** typ **//
2708         for k=1:nin
2709             lprt=inplnk(inpptr(kf)-1+k);
2710             mputl(["    block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1)+2*nin)+"]   = "+...
2711             mat2scs_c_typ(outtb(lprt))+";"], fd);
2712         end
2713         //**********************************************************************//
2714
2715         //***************************** output port *****************************//
2716         //** alloc outsz/outptr **//
2717         mputl(["    if ((block_"+rdnom+"["+string(kf-1)+"].outsz  = "+...
2718         "malloc(3*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nout))== NULL) return 0;";
2719         "    if ((block_"+rdnom+"["+string(kf-1)+"].outptr = "+...
2720         "malloc(sizeof(void*)*block_"+rdnom+"["+string(kf-1)+"].nout))== NULL) return 0;"], fd);
2721
2722         //** outptr **//
2723         for k=1:nout
2724             lprt=outlnk(outptr(kf)-1+k);
2725             mputl(["    block_"+rdnom+"["+string(kf-1)+"].outptr["+string(k-1)+"] = "+...
2726             rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2727         end
2728
2729         //** 1st dim **//
2730         for k=1:nout
2731             lprt=outlnk(outptr(kf)-1+k);
2732             mputl(["    block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1))+...
2733             "]  = "+string(size(outtb(lprt),1))+";"], fd);
2734         end
2735
2736         //** 2dn dim **//
2737         for k=1:nout
2738             lprt=outlnk(outptr(kf)-1+k);
2739             mputl(["    block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1)+nout)+...
2740             "]  = "+string(size(outtb(lprt),2))+";"], fd)
2741         end
2742
2743         //** typ **//
2744         for k=1:nout
2745             lprt=outlnk(outptr(kf)-1+k);
2746             mputl(["    block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1)+2*nout)+...
2747             "]  = "+mat2scs_c_typ(outtb(lprt))+";"], fd);
2748         end
2749         //**********************************************************************//
2750
2751         mputl(["    block_"+rdnom+"["+string(kf-1)+"].z         = &(z["+...
2752         string(zptr(kf)-1)+"]);"], fd);
2753
2754         //***************************** object state *****************************//
2755         if (ozptr(kf+1)-ozptr(kf)>0) then
2756             noz = ozptr(kf+1)-ozptr(kf);
2757             mputl(["    if ((block_"+rdnom+"["+string(kf-1)+"].ozptr = "+...
2758             "malloc(sizeof(void *)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";
2759             "    if ((block_"+rdnom+"["+string(kf-1)+"].ozsz  = "+...
2760             "malloc(2*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";
2761             "    if ((block_"+rdnom+"["+string(kf-1)+"].oztyp = "+...
2762             "malloc(sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";], fd);
2763
2764             //** ozptr **//
2765             for k=1:noz
2766                 mputl(["    block_"+rdnom+"["+string(kf-1)+"].ozptr["+string(k-1)+...
2767                 "]  = ozptr["+string(ozptr(kf)-1+k-1)+"];"], fd);
2768             end
2769
2770             //** 1st dim **//
2771             for k=1:noz
2772                 mputl(["    block_"+rdnom+"["+string(kf-1)+"].ozsz["+string(k-1)+...
2773                 "]   = "+string(size(oz(ozptr(kf)-1+k),1))+";"], fd);
2774             end
2775
2776             //** 2nd dim **//
2777             for k=1:noz
2778                 mputl(["    block_"+rdnom+"["+string(kf-1)+"].ozsz["+string(k-1)+...
2779                 "]   = "+string(size(oz(ozptr(kf)-1+k),2))+";"], fd);
2780             end
2781
2782             //** typ **//
2783             for k=1:noz
2784                 mputl(["    block_"+rdnom+"["+string(kf-1)+"].oztyp["+string(k-1)+...
2785                 "]  = "+mat2scs_c_typ(oz(ozptr(kf)-1+k))+";"], fd);
2786             end
2787         end
2788         //************************************************************************//
2789
2790         if (part(funs(kf),1:7) ~= "capteur" &...
2791             part(funs(kf),1:10) ~= "actionneur" &...
2792             funs(kf) ~= "bidon" &...
2793             funs(kf) ~= "bidon2") then
2794             if (rpptr(kf+1)-rpptr(kf)>0) then
2795                 mputl(["    block_"+rdnom+"["+string(kf-1)+...
2796                 "].rpar      = &(rpar["+string(rpptr(kf)-1)+"]);"], fd);
2797             end
2798             if (ipptr(kf+1)-ipptr(kf)>0) then
2799                 mputl(["    block_"+rdnom+"["+string(kf-1)+...
2800                 "].ipar      = &(ipar["+string(ipptr(kf)-1)+"]);"], fd);
2801             end
2802             //** opar
2803             if (opptr(kf+1)-opptr(kf)>0) then
2804                 mputl(["    if ((block_"+rdnom+"["+string(kf-1)+"].oparptr = "+...
2805                 "malloc(sizeof(void *)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL) return 0;";
2806                 "    if ((block_"+rdnom+"["+string(kf-1)+"].oparsz  = "+...
2807                 "malloc(2*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL) return 0;";
2808                 "    if ((block_"+rdnom+"["+string(kf-1)+"].opartyp = "+...
2809                 "malloc(sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL) return 0;";
2810                 ], fd);
2811                 nopar = opptr(kf+1)-opptr(kf);
2812                 //** oparptr **//
2813                 for k=1:nopar
2814                     mputl(["    block_"+rdnom+"["+string(kf-1)+"].oparptr["+string(k-1)+...
2815                     "]  = oparptr["+string(opptr(kf)-1+k-1)+"];"], fd);
2816                 end
2817                 //** 1st dim **//
2818                 for k=1:nopar
2819                     mputl(["    block_"+rdnom+"["+string(kf-1)+"].oparsz["+string(k-1)+...
2820                     "]   = "+string(size(opar(opptr(kf)-1+k),1))+";"], fd);
2821                 end
2822                 //** 2dn dim **//
2823                 for k=1:nopar
2824                     mputl(["    block_"+rdnom+"["+string(kf-1)+"].oparsz["+string(nopar+(k-1))+...
2825                     "]   = "+string(size(opar(opptr(kf)-1+k),2))+";"], fd)
2826                 end
2827                 //** typ **//
2828                 for k=1:nopar
2829                     mputl(["    block_"+rdnom+"["+string(kf-1)+"].opartyp["+string(k-1)+...
2830                     "]  = "+mat2scs_c_typ(opar(opptr(kf)-1+k))+";"], fd)
2831                 end
2832             end
2833         end
2834
2835         mputl(["    block_"+rdnom+"["+string(kf-1)+...
2836         "].work      = (void **)(((double *)work)+"+string(kf-1)+");"
2837         "    block_"+rdnom+"["+string(kf-1)+...
2838         "].nevprt    = nevprt;"], fd);
2839
2840     end //for kf=1:nblk
2841
2842     //** init
2843     for kf=1:nblk
2844         //     if or(kf==act) | or(kf==cap) then
2845         //       if stalone then
2846         //         txt = call_block42(kf,0,4);
2847         //         if txt <> [] then
2848         //           Code=[Code;
2849         //                 '';
2850         //                 '    '+txt];
2851         //         end
2852         //       end
2853         //     else
2854         txt = call_block42(kf,0,4);
2855         if txt <> [] then
2856             mputl(["";
2857             "    "+txt], fd);
2858         end
2859         //     end
2860     end
2861
2862     //** cst blocks and it's dep
2863     txt=write_code_idoit()
2864
2865     if txt<>[] then
2866         mputl([""
2867         "    /* initial blocks must be called with flag 1 */"
2868         txt], fd);
2869     end
2870     //   for kf=iord(:,1)'
2871     // //     if or(kf==act) then
2872     // //       if stalone then
2873     // //         txt = call_block42(kf,0,1);
2874     // //         if txt <> [] then
2875     // //           Code=[Code;
2876     // //                 '';
2877     // //                 '    '+txt];
2878     // //         end
2879     // //       end
2880     // //     else
2881     //       txt = call_block42(kf,0,1);
2882     //       if txt <> [] then
2883     //         Code=[Code;
2884     //               '';
2885     //               '    '+txt];
2886     //       end
2887     // //     end
2888     //   end
2889
2890     mputl(["  }"], fd);
2891
2892     //** flag 5
2893     mputl(["  else if (flag == 5) { "+get_comment("flag",list(5))
2894     "    block_"+rdnom+"=*block->work;"], fd);
2895
2896     for kf=1:nblk
2897         nin=inpptr(kf+1)-inpptr(kf);  //* number of input ports */
2898         nout=outptr(kf+1)-outptr(kf); //* number of output ports */
2899
2900         //** add comment
2901         txt=mputl([""
2902         "    "+get_comment("set_blk",list(funs(kf),funtyp(kf),kf,labels(kf)));], fd);
2903
2904         for k=1:nin
2905             lprt=inplnk(inpptr(kf)-1+k);
2906             mputl(["    block_"+rdnom+"["+string(kf-1)+"].inptr["+string(k-1)+...
2907             "]  = "+rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2908         end
2909         for k=1:nout
2910             lprt=outlnk(outptr(kf)-1+k);
2911             mputl(["    block_"+rdnom+"["+string(kf-1)+"].outptr["+string(k-1)+...
2912             "] = "+rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
2913         end
2914         mputl(["    block_"+rdnom+"["+string(kf-1)+"].z=&(z["+...
2915         string(zptr(kf)-1)+"]);"], fd);
2916         if (part(funs(kf),1:7) ~= "capteur" &...
2917             part(funs(kf),1:10) ~= "actionneur" &...
2918             funs(kf) ~= "bidon" &...
2919             funs(kf) ~= "bidon2") then
2920             //** rpar **//
2921             if (rpptr(kf+1)-rpptr(kf)>0) then
2922                 mputl(["    block_"+rdnom+"["+string(kf-1)+...
2923                 "].rpar=&(rpar["+string(rpptr(kf)-1)+"]);"], fd);
2924             end
2925             //** ipar **//
2926             if (ipptr(kf+1)-ipptr(kf)>0) then
2927                 mputl(["    block_"+rdnom+"["+string(kf-1)+...
2928                 "].ipar=&(ipar["+string(ipptr(kf)-1)+"]);"], fd);
2929             end
2930             //** opar **//
2931             if (opptr(kf+1)-opptr(kf)>0) then
2932                 nopar = opptr(kf+1)-opptr(kf);
2933                 for k=1:nopar
2934                     mputl(["    block_"+rdnom+"["+string(kf-1)+"].oparptr["+string(k-1)+...
2935                     "]  = oparptr["+string(opptr(kf)-1+k-1)+"];"], fd);
2936                 end
2937             end
2938             //** oz **//
2939             if (ozptr(kf+1)-ozptr(kf)>0) then
2940                 noz = ozptr(kf+1)-ozptr(kf);
2941                 for k=1:noz
2942                     mputl(["    block_"+rdnom+"["+string(kf-1)+"].ozptr["+string(k-1)+...
2943                     "]  = ozptr["+string(ozptr(kf)-1+k-1)+"];"], fd);
2944                 end
2945             end
2946         end
2947
2948         mputl(["    block_"+rdnom+"["+string(kf-1)+...
2949         "].work=(void **)(((double *)work)+"+string(kf-1)+");"], fd)
2950     end
2951
2952     for kf=1:nblk
2953         //     if or(kf==act) | or(kf==cap) then
2954         //       if stalone then
2955         //         txt = call_block42(kf,0,5);
2956         //         if txt <> [] then
2957         //           Code=[Code;
2958         //                 '';
2959         //                 '    '+txt];
2960         //         end
2961         //       end
2962         //     else
2963         txt = call_block42(kf,0,5);
2964         if txt <> [] then
2965             mputl(["";
2966             "    "+txt], fd);
2967         end
2968         //     end
2969     end
2970
2971     mputl([""
2972     "    for (kf = 0; kf < "+string(nblk)+"; ++kf) {"
2973     "      if (block_"+rdnom+"[kf].insz!=NULL) {"
2974     "        free(block_"+rdnom+"[kf].insz);"
2975     "      }else {"
2976     "        break;"
2977     "      }"
2978     "      if (block_"+rdnom+"[kf].outsz!=NULL){"
2979     "        free(block_"+rdnom+"[kf].outsz);"
2980     "      }else {"
2981     "        break;"
2982     "      }"
2983     "      if (block_"+rdnom+"[kf].nopar!=0){"
2984     "        if (block_"+rdnom+"[kf].oparptr!=NULL){"
2985     "          free(block_"+rdnom+"[kf].oparptr);"
2986     "        }else {"
2987     "          break;"
2988     "        }"
2989     "        if (block_"+rdnom+"[kf].oparsz!=NULL){"
2990     "          free(block_"+rdnom+"[kf].oparsz);"
2991     "        }else {"
2992     "          break;"
2993     "        }"
2994     "        if (block_"+rdnom+"[kf].opartyp!=NULL){"
2995     "          free(block_"+rdnom+"[kf].opartyp);"
2996     "        }else {"
2997     "          break;"
2998     "        }"
2999     "      }"
3000     "      if (block_"+rdnom+"[kf].noz!=0){"
3001     "        if (block_"+rdnom+"[kf].ozptr!=NULL){"
3002     "          free(block_"+rdnom+"[kf].ozptr);"
3003     "        }else {"
3004     "          break;"
3005     "        }"
3006     "        if (block_"+rdnom+"[kf].ozsz!=NULL){"
3007     "          free(block_"+rdnom+"[kf].ozsz);"
3008     "        }else {"
3009     "          break;"
3010     "        }"
3011     "        if (block_"+rdnom+"[kf].oztyp!=NULL){"
3012     "          free(block_"+rdnom+"[kf].oztyp);"
3013     "        }else {"
3014     "          break;"
3015     "        }"
3016     "      }"
3017     "      if (block_"+rdnom+"[kf].evout!=NULL){"
3018     "        free(block_"+rdnom+"[kf].evout);"
3019     "      }else {"
3020     "        break;"
3021     "      }"
3022     "    }"
3023     "    scicos_free(block_"+rdnom+");"
3024     "  }"
3025     ""], fd);
3026
3027     for i=1:size(actt,1)
3028         ni=actt(i,3)*actt(i,4); // dimension of ith input
3029         if actt(i,5)<>11 then
3030             mputl(["  memcpy(*(y+"+string(actt(i,6)-1)+"),"+...
3031             "*("+rdnom+"_block_outtbptr+"+string(actt(i,2)-1)+"),"+...
3032             string(ni)+"*sizeof("+mat2c_typ(actt(i,5))+"));"], fd);
3033         else //** Cas cmplx
3034             mputl(["  memcpy(*(y+"+string(actt(i,6)-1)+"),"+...
3035             "*("+rdnom+"_block_outtbptr+"+string(actt(i,2)-1)+"),"+...
3036             string(2*ni)+"*sizeof("+mat2c_typ(actt(i,5))+"));"], fd);
3037         end
3038     end
3039
3040     //**
3041     mputl(["  return 0;"
3042     ""
3043     "} /* "+rdnom+" */"], fd);
3044
3045     mclose(fd);
3046
3047 endfunction
3048
3049 //generates skeleton of external world events handling function
3050 function Code=make_outevents()
3051     z="0"
3052     if szclkIN==[] then
3053         newszclkIN=0;
3054     else
3055         newszclkIN=szclkIN;
3056     end
3057
3058     Code=[ "/*"+part("-",ones(1,40))+"  External events handling function */";
3059     "void "+rdnom+"_events(int *nevprt,double *t)";
3060     "{"
3061     "/*  set next event time and associated events ports"
3062     " *  nevprt has binary expression b1..b"+string(newszclkIN)+" where bi is a bit"
3063     " *  bi is set to 1 if an activation is received by port i. Note that"
3064     " *  more than one activation can be received simultaneously"
3065     " *  Caution: at least one bi should be equal to one */"
3066     ""]
3067
3068     if (newszclkIN <> 0) then
3069         Code=[Code;
3070         "    int i,p,b[]={"+strcat(z(ones(1,newszclkIN)),",")+"};"
3071         ""
3072         "/* this is an example for the activation of events ports */"
3073         "    b[0]=1;"]
3074
3075         if newszclkIN>1 then
3076             for bb=2:newszclkIN
3077                 Code($+1)="    b["+string(bb-1)+"]=1;"
3078             end
3079         end
3080
3081         Code=[Code;
3082         ""
3083         "/* definition of the step time  */"
3084         "    *t = *t + 0.1;"
3085         ""
3086         "/* External events handling process */"
3087         "    *nevprt=0;p=1;"
3088         "    for (i=0;i<"+string(newszclkIN)+";i++) {"
3089         "      *nevprt=*nevprt+b[i]*p;"
3090         "      p=p*2;"
3091         "    }"
3092         "}"]
3093     else
3094         Code=[Code;
3095         "";
3096         "/* definition of the step time  */"
3097         "    *t = *t + 0.1;"
3098         "}"]
3099     end
3100 endfunction
3101
3102 //Generating the routine for sensors interfacing
3103 //
3104 //
3105 //Author : R. Djenidi, R. Nikoukhah, A. Layec
3106 //
3107 function Code=make_sensor(standalone)
3108     Call=["/*"+part("-",ones(1,40))+" Sensor */";
3109     "void "+rdnom+"_sensor(int *flag, int *nport, int *nevprt, double *t, void *y, int *ny1, int *ny2, int *yt, int *flag1)"]
3110
3111     comments=["     /*"
3112     "      * To be customized for standalone execution";
3113     "      * flag  : specifies the action to be done"
3114     "      * nport : specifies the  index of the Super Bloc"
3115     "      *         regular input (The input ports are numbered"
3116     "      *         from the top to the bottom )"
3117     "      * nevprt: indicates if an activation had been received"
3118     "      *         0 = no activation"
3119     "      *         1 = activation"
3120     "      * t     : the current time value"
3121     "      * y     : the vector outputs value"
3122     "      * ny1   : the output size 1"
3123     "      * ny2   : the output size 2"
3124     "      * yt    : the output type"
3125     "      * flag1 : learn mode (0 from terminal,1 from input file"
3126     "      */"]
3127
3128     dcl=["{"
3129     "  int j,k,l;"
3130     "  double temps;"]
3131
3132     if standalone then
3133
3134         a_sensor=["    switch (*flag) {"
3135         "    case 1 : /* set the output value */"
3136         "      printf(""Require outputs of sensor number %d\n"", *nport);"
3137         "      printf(""time is: %f\n"", *t);"
3138         "      printf(""sizes of the sensor output is: %d,%d\n"", *ny1,*ny2);"
3139         "      switch (*yt) {"
3140         "      case 10 :"
3141         "        printf(""type of the sensor output is: %d (double) \n"", *yt);"
3142         "        puts(""Please set the sensor output values"");"
3143         "        for (l=0;l<*ny2;l++) {"
3144         "          for (k=0;k<*ny1;k++) {"
3145         "            printf(""y(%d,%d) : "",k,l);"
3146         "            scanf(""%lf"", (double *) y+(k+l*(*ny1)));"
3147         "          }"
3148         "        }"
3149         "        break;"
3150         ""
3151         "      case 11 :"
3152         "        printf(""type of the sensor output is: %d (complex) \n"", *yt);"
3153         "        puts(""Please set the sensor output values"");"
3154         "        for (l=0;l<*ny2;l++) {"
3155         "          for (k=0;k<*ny1;k++) {"
3156         "            printf(""y(%d,%d) real part : "",k,l);"
3157         "            scanf(""%lf"", (double *) y+(k+l*(*ny1)));"
3158         "            printf(""y(%d,%d) imag part : "",k,l);"
3159         "            scanf(""%lf"", (double *) y+((*ny1)*(*ny2)+k+l*(*ny1)));"
3160         "          }"
3161         "        }"
3162         "        break;"
3163         ""
3164         "      case 81 :"
3165         "        printf(""type of the sensor output is: %d (char) \n"", *yt);"
3166         "        puts(""Please set the sensor output values"");"
3167         "        for (l=0;l<*ny2;l++) {"
3168         "          for (k=0;k<*ny1;k++) {"
3169         "            printf(""y(%d,%d) : "",k,l);"
3170         "            scanf(""%i"", (char *) y+(k+l*(*ny1)));"
3171         "          }"
3172         "        }"
3173         "        break;"
3174         ""
3175         "      case 82 :"
3176         "        printf(""type of the sensor output is: %d (char) \n"", *yt);"
3177         "        puts(""Please set the sensor output values"");"
3178         "        for (l=0;l<*ny2;l++) {"
3179         "          for (k=0;k<*ny1;k++) {"
3180         "            printf(""y(%d,%d) : "",k,l);"
3181         "            scanf(""%hd"", (short *) y+(k+l*(*ny1)));"
3182         "          }"
3183         "        }"
3184         "        break;"
3185         ""
3186         "      case 84 :"
3187         "        printf(""type of the sensor output is: %d (long) \n"", *yt);"
3188         "        puts(""Please set the sensor output values"");"
3189         "        for (l=0;l<*ny2;l++) {"
3190         "          for (k=0;k<*ny1;k++) {"
3191         "            printf(""y(%d,%d) : "",k,l);"
3192         "            scanf(""%ld"", (long *) y+(k+l*(*ny1)));"
3193         "          }"
3194         "        }"
3195         "        break;"
3196         ""
3197         "      case 811 :"
3198         "        printf(""type of the sensor output is: %d (unsigned char) \n"", *yt);"
3199         "        puts(""Please set the sensor output values"");"
3200         "        for (l=0;l<*ny2;l++) {"
3201         "          for (k=0;k<*ny1;k++) {"
3202         "            printf(""y(%d,%d) : "",k,l);"
3203         "            scanf(""%d"", (unsigned char *) y+(k+l*(*ny1)));"
3204         "          }"
3205         "        }"
3206         "        break;"
3207         ""
3208         "      case 812 :"
3209         "        printf(""type of the sensor output is: %d (unsigned short) \n"", *yt);"
3210         "        puts(""Please set the sensor output values"");"
3211         "        for (l=0;l<*ny2;l++) {"
3212         "          for (k=0;k<*ny1;k++) {"
3213         "            printf(""y(%d,%d) : "",k,l);"
3214         "            scanf(""%hu"", (unsigned short *) y+(k+l*(*ny1)));"
3215         "          }"
3216         "        }"
3217         "        break;"
3218         ""
3219         "      case 814 :"
3220         "        printf(""type of the sensor output is: %d (unsigned long) \n"", *yt);"
3221         "        puts(""Please set the sensor output values"");"
3222         "        for (l=0;l<*ny2;l++) {"
3223         "          for (k=0;k<*ny1;k++) {"
3224         "            printf(""y(%d,%d) : "",k,l);"
3225         "            scanf(""%lu"", (unsigned long *) y+(k+l*(*ny1)));"
3226         "          }"
3227         "        }"
3228         "        break;"
3229         ""
3230         "      }"
3231         "      break;"
3232         "    case 4 : /* sensor initialisation */"
3233         "      /* do whatever you want to initialize the sensor */"
3234         "      break;"
3235         "    case 5 : /* sensor ending */"
3236         "      /* do whatever you want to end the sensor */"
3237         "      break;"
3238         "    }"]
3239     else
3240         a_sensor=[]
3241     end
3242
3243     nc=size(cap,"*")
3244
3245     // pour fscanf
3246     typ=["""%lf "]; //temps
3247     for i=1:nc
3248         typ($)=typ($)+""" \"
3249         typ=[typ;""""];
3250         for j=1:sum(capt(i,3)*capt(i,4))
3251             //typ=typ+'%f ';
3252             typ($)=typ($)+scs_c_n2c_fmt(capt(i,5))+" ";
3253         end
3254     end
3255     typ($)=typ($)+"\n"", \"
3256     typ=strsubst(typ,"%f","%lf");
3257     typ(1)="    fscanf(fprr,"+typ(1);
3258     bl    ="                ";
3259     if size(typ,1) <> 1 then
3260         typ(2:$) = bl+typ(2:$);
3261     end
3262     //Code1=['      fscanf( fprr, '"'+typ+' \n'",&temps']
3263     Code1=[typ;bl+"&temps"];
3264     for i=1:size(capt,1)
3265         ni=capt(i,3)*capt(i,4); // dimension of ith input
3266         Code1($)=Code1($)+",";
3267         Code1=[Code1;bl];
3268         for j=1:ni
3269             if capt(i,5)<>11 then
3270                 Code1($)=Code1($)+...
3271                 "("+scs_c_n2c_typ(capt(i,5))+" *)("+...
3272                 rdnom+"_block_outtbptr+"+string(capt(i,2)-1)+")"+...
3273                 "+"+string(j-1)+"";
3274             else //CAS CMPLX
3275                 Code1($)=Code1($)+...
3276                 "("+scs_c_n2c_typ(capt(i,5))+" *)("+...
3277                 rdnom+"_block_outtbptr+"+string(capt(i,2)-1)+")"+...
3278                 "+"+string((j-1))+", "+...
3279                 "("+scs_c_n2c_typ(capt(i,5))+" *)("+...
3280                 rdnom+"_block_outtbptr+"+string(capt(i,2)-1)+")"+...
3281                 "+"+string(ni+(j-1))+"";
3282             end
3283             if j<>ni then
3284                 Code1($)=Code1($)+", ";
3285             end
3286         end
3287     end
3288     Code1($)=Code1($)+");"
3289
3290     Code=[]
3291     if nc==1|~standalone then
3292         Code=[Code;
3293         Call
3294         comments
3295         dcl
3296         "  if (flag1 == 0) {"
3297         a_sensor;
3298         "  } "
3299         "  else if (flag1 == 1) {"
3300         "    if (*flag == 4 && *nport == 1) {"
3301         "      fprr=fopen(input,'"r'");"
3302         "      if( fprr == NULL ) {"
3303         "        printf('"Error opening file: %s\n'", input);"
3304         "        return;"
3305         "      }"
3306         "    }"
3307         "    else if (*flag == 1) {"
3308         Code1
3309         "    }"
3310         "    else if (*flag == 5 && *nport == 1) {"
3311         "      fclose(fprr);"
3312         "    }"
3313         "  }"
3314         "}"];
3315
3316     elseif nc>1 then
3317         S="  switch (*nport) {"
3318         for k=1:nc
3319             S=[S;
3320             "  case "+string(k)+" : /* Port number "+string(k)+" ----------*/"
3321             "  "+a_sensor
3322             "  break;"]
3323         end
3324         S=[S;"  }"]
3325
3326         Code=[Code
3327         Call
3328         comments
3329         dcl
3330         "  if (flag1 == 0) {"
3331         S
3332         "  }"
3333         "  else if (flag1 == 1){"
3334         "    if (*flag == 4 && *nport == 1) {"
3335         "      fprr=fopen(input,'"r'");"
3336         "      if( fprr == NULL ) {"
3337         "        printf('"Error opening file: %s\n'", input);"
3338         "        return ;"
3339         "      }"
3340         "    }"
3341         "    else if (*flag == 1) {"
3342         Code1
3343         "    }"
3344         "    else if (*flag == 5 && *nport == 1) {"
3345         "      fclose(fprr);"
3346         "    }"
3347         "  }"
3348         "}"]
3349     end
3350 endfunction
3351
3352 //generates code of the standalone simulation procedure
3353 //
3354 //
3355 // rmq : La fonction zdoit n'est pas utilis?e pour le moment
3356 function make_standalone42(filename)
3357
3358     x=cpr.state.x;
3359     modptr=cpr.sim.modptr;
3360     rpptr=cpr.sim.rpptr;
3361     ipptr=cpr.sim.ipptr;
3362     opptr=cpr.sim.opptr;
3363     rpar=cpr.sim.rpar;
3364     ipar=cpr.sim.ipar;
3365     opar=cpr.sim.opar;
3366     oz=cpr.state.oz;
3367     ordptr=cpr.sim.ordptr;
3368     oord=cpr.sim.oord;
3369     zord=cpr.sim.zord;
3370     iord=cpr.sim.iord;
3371     tevts=cpr.state.tevts;
3372     evtspt=cpr.state.evtspt;
3373     zptr=cpr.sim.zptr;
3374     clkptr=cpr.sim.clkptr;
3375     ordptr=cpr.sim.ordptr;
3376     pointi=cpr.state.pointi;
3377     funs=cpr.sim.funs;
3378     noord=size(cpr.sim.oord,1);
3379     nzord=size(cpr.sim.zord,1);
3380     niord=size(cpr.sim.iord,1);
3381
3382     Indent="  ";
3383     Indent2=Indent+Indent;
3384     BigIndent="          ";
3385
3386     work=zeros(nblk,1)
3387     Z=[z;zeros(lstsize(outtb),1);work]';
3388     nX=size(x,"*");
3389     nztotal=size(z,1);
3390
3391     stalone = %t;
3392
3393     fd = mopen(filename, "wt");
3394
3395     mputl(["/*"
3396     "**    Code prototype for standalone use"
3397     "**    Generated by Code_Generation toolbox of Xcos with "+getversion()
3398     "**    Date : "+date()
3399     "*/"
3400     ""
3401     "/* To learn how to use the standalone code, type '"./standalone -h'" */"
3402     ""
3403     "/* ---- Headers ---- */"
3404     "#include <stdio.h>"
3405     "#include <stdlib.h>"
3406     "#include <math.h>"
3407     "#include <string.h>"
3408     "#include <memory.h>"
3409     "#include '"scicos_block4.h'""
3410     "#include '"machine.h'""
3411     ""
3412     "/* ---- Internals functions declaration ---- */"
3413     "int "+rdnom+"_sim(double, double, double, int);"
3414     Protostalone
3415     ""], fd);
3416
3417     if getos() == "Windows" then
3418         mputl([" "
3419         "#define max(a,b) ((a) >= (b) ? (a) : (b))"
3420         "#define min(a,b) ((a) <= (b) ? (a) : (b))"
3421         " "
3422         ], fd);
3423     end
3424
3425
3426     if x<>[] then
3427         mputl(["/* Code prototype for standalone use  */"
3428         "int C2F("+rdnom+"simblk)(double , double *, double *);"
3429         "int dset(int *n, double *dx, double *dy, int *incy);"
3430         "int ode1();"
3431         "int ode2();"
3432         "int ode4();"
3433         ""], fd);
3434     end
3435
3436     mputl(["/* Table of constant values */"
3437     "static int nrd_"+string(0:maxtotal)'+" = "+string(0:maxtotal)'+";"], fd);
3438
3439     if maxtotal<10 then
3440         mputl(["static int nrd_10 = 10;"], fd);
3441     end
3442     if maxtotal<11 then
3443         mputl(["static int nrd_11 = 11;"], fd)
3444     end
3445
3446     if maxtotal<81 then
3447         mputl(["static int nrd_81 = 81;"], fd);
3448     end
3449     if maxtotal<82 then
3450         mputl(["static int nrd_82 = 82;"], fd);
3451     end
3452     if maxtotal<84 then
3453         mputl(["static int nrd_84 = 84;"], fd);
3454     end
3455     if maxtotal<811 then
3456         mputl(["static int nrd_811 = 811;"], fd);
3457     end
3458     if maxtotal<812 then
3459         mputl(["static int nrd_812 = 812;"], fd);
3460     end
3461     if maxtotal<814 then
3462         mputl(["static int nrd_814 = 814;"], fd);
3463     end
3464
3465     mputl([""
3466     "/* Some general static variables */"
3467     "static double zero=0;"
3468     "static double w[1];"
3469     "static int aaa=0, bbb=0;"
3470     "void **"+rdnom+"_block_outtbptr;"], fd); //** !!
3471
3472     mputl([make_static_standalone42()], fd);
3473
3474     mputl(["static void usage();"
3475     ""
3476     "/* Main program */"
3477     "int main(int argc, char *argv[])"
3478     "{"
3479     "  double tf=30;"  // FIXME : Add current simulation tf.
3480     "  double dt=0.1;" // FIXME : Try to figure out what is the given Simulation step.
3481     "  double h=0.001;"
3482     "  int solver=3;"
3483     "  char * progname = argv[0];"
3484     "  int c;"
3485     "  strcpy(input,'"'");"
3486     "  strcpy(output,'"'");"
3487     "  while ((c = getopt(argc , argv, '"i:o:d:t:e:s:hv'")) != -1)"
3488     "    switch (c) {"
3489     "    case ''i'':"
3490     "      strcpy(input,argv[optind-1]);"
3491     "      break;"
3492     "    case ''o'':"
3493     "      strcpy(output,argv[optind-1]);"
3494     "      break;"
3495     "    case ''d'':"
3496     "      strcpy(s,argv[optind-1]);"
3497     "      dt=strtod(s,p);"
3498     "      break;"
3499     "    case ''t'':"
3500     "      strcpy(sf,argv[optind-1]);"
3501     "      tf=strtod(sf,p);"
3502     "      break;"
3503     "    case ''e'':"
3504     "      strcpy(se,argv[optind-1]);"
3505     "      h=strtod(se,p);"
3506     "      break;"
3507     "    case ''s'':"
3508     "      strcpy(ss,argv[optind-1]);"
3509     "      solver=(int) strtod(ss,p);"
3510     "      break;"
3511     "    case ''h'':"
3512     "      usage(progname);"
3513     "      printf(""Options : \n'");"
3514     "      printf(""     -h for the help  \n"");"
3515     "      printf(""     -v for printing the Scilab Version \n"");"
3516     "      printf(""     -i for input file name, by default is Terminal \n"");"
3517     "      printf(""     -o for output file name, by default is Terminal \n"");"
3518     "      printf(""     -d for the clock period, by default is 0.1 \n"");"
3519     "      printf(""     -t for the final time, by default is 30 \n"");"
3520     "      printf(""     -e for the solvers step size, by default is 0.001 \n"");"
3521     "      printf(""     -s integer parameter for select the numerical solver : \n"");"
3522     "      printf(""        1 for Euler''s method \n"");"
3523     "      printf(""        2 for Heun''s method \n"");"
3524     "      printf(""        3 (default value) for the Fourth-Order Runge-Kutta"+...
3525     " (RK4) Formula \n"");"
3526     "      return 0;"
3527     "      break;"
3528     "    case ''v'':"
3529     "      printf(""Generated by Code_Generation toolbox of Xcos """
3530     "             ""with "+getversion()+" version \n"");"
3531     "      return 0;"
3532     "      break;"
3533     "    case ''?'':"
3534     "      errflg++;"
3535     "      break;"
3536     "    }"
3537     "    if (errflg){"
3538     "      usage(progname);"
3539     "      return 0;"
3540     "    }"
3541     " "
3542     "  "+rdnom+"_sim(tf,dt,h,solver);"
3543     "  return 0;"
3544     "}"
3545     ""
3546     "static void usage(prog)"
3547     "       char *prog;"
3548     "{"
3549     "  fprintf(stderr, ""Usage: %s [-h] [-v] [-i arg] [-o arg] """
3550     "                  ""[-d arg] [-t arg] [-e arg] [-s arg]\n"", prog);"
3551     "}"
3552     ""], fd);
3553
3554     mputl(["/*"+part("-",ones(1,40))+"  External simulation function */"
3555     "int "+rdnom+"_sim(tf,dt,h,solver)"
3556     ""
3557     "   double tf,dt,h;"
3558     "   int solver;"
3559     "{"
3560     "  double t;"
3561     "  int nevprt=1;"
3562     "  int local_flag;"
3563     "  int nport;"
3564     "  int i;"
3565     "  double *args[100];"
3566     ""], fd);
3567
3568     if (x <> []) then
3569         mputl(["  double tout;"
3570         "  double he=0.1;"
3571         ""], fd);
3572     end
3573
3574     mputl(["  /* Initial values */"
3575     ""
3576     "  /* Note that z[]=[z_initial_condition;outtbptr;work]"
3577     cformatline("     z_initial_condition={"+...
3578     strcat(string(z),",")+"};",70)
3579     cformatline("     outtbptr={"+...
3580     strcat(string(zeros(lstsize(outtb),1)),"," )+"};",70)
3581     cformatline("     work= {"+...
3582     strcat(string(work),"," )+"};",70)
3583     "  */"
3584     ""
3585     cformatline("  double z[]={"+strcat(string(Z),",")+"};",70)], fd);
3586
3587     if size(z,1) <> 0 then
3588         for i=1:(length(zptr)-1)
3589             if zptr(i+1)-zptr(i)>0 then
3590                 if size(corinv(i),"*")==1 then
3591                     OO=scs_m.objs(corinv(i))
3592                 else
3593                     path=list("objs")
3594                     for l=cpr.corinv(i)(1:$-1)
3595                         path($+1)=l;path($+1)="model"
3596                         path($+1)="rpar"
3597                         path($+1)="objs"
3598                     end
3599                     path($+1)=cpr.corinv(i)($)
3600                     OO=scs_m(path)
3601                 end
3602                 aaa=OO.gui
3603                 bbb=emptystr(3,1);
3604                 if and(aaa+bbb~=["INPUTPORTEVTS";"OUTPUTPORTEVTS";"EVTGEN_f"]) then
3605                     mputl(["";
3606                     "  /* Routine name of block: "+strcat(string(cpr.sim.funs(i)));
3607                     "     Gui name of block: "+strcat(string(OO.gui));
3608                     //'/* Name block: '+strcat(string(cpr.sim.funs(i)));
3609                     //'Object number in diagram: '+strcat(string(cpr.corinv(i)));
3610                     "     Compiled structure index: "+strcat(string(i));], fd);
3611                     if stripblanks(OO.model.label)~=emptystr() then
3612                         mputl([cformatline("     Label: "+strcat(string(OO.model.label)),70)], fd);
3613                     end
3614                     if stripblanks(OO.graphics.exprs(1))~=emptystr() then
3615                         mputl([cformatline("     Exprs: "+strcat(OO.graphics.exprs(1),","),70)], fd);
3616                     end
3617                     if stripblanks(OO.graphics.id)~=emptystr() then
3618                         mputl([cformatline("     Identification: "+..
3619                         strcat(string(OO.graphics.id)),70)], fd);
3620                     end
3621                     mputl([cformatline("     z={"+...
3622                     strcat(string(z(zptr(i):zptr(i+1)-1)),",")+"};",70)
3623                     " */"], fd);
3624                 end
3625             end
3626         end
3627     end
3628
3629     //** declaration of oz
3630     Code_oz = [];
3631     for i=1:lstsize(oz)
3632         if mat2scs_c_nb(oz(i)) <> 11 then
3633             Code_oz=[Code_oz;
3634             cformatline("  "+mat2c_typ(oz(i))+...
3635             " oz_"+string(i)+"[]={"+...
3636             strcat(string(oz(i)(:)),",")+"};",70)]
3637         else //** cmplx test
3638             Code_oz=[Code_oz;
3639             cformatline("  "+mat2c_typ(oz(i))+...
3640             " oz_"+string(i)+"[]={"+...
3641             strcat(string([real(oz(i)(:));
3642             imag(oz(i)(:))]),",")+"};",70)]
3643         end
3644     end
3645
3646     if Code_oz <> [] then
3647         mputl(["  /* oz declaration */"
3648         Code_oz], fd);
3649     end
3650
3651     //** declaration of outtb
3652     Code_outtb = [];
3653     for i=1:lstsize(outtb)
3654         if mat2scs_c_nb(outtb(i)) <> 11 then
3655             Code_outtb=[Code_outtb;
3656             cformatline("  "+mat2c_typ(outtb(i))+...
3657             " outtb_"+string(i)+"[]={"+...
3658             strcat(string(outtb(i)(:)),",")+"};",70)]
3659         else //** cmplx test
3660             Code_outtb=[Code_outtb;
3661             cformatline("  "+mat2c_typ(outtb(i))+...
3662             " outtb_"+string(i)+"[]={"+...
3663             strcat(string([real(outtb(i)(:));
3664             imag(outtb(i)(:))]),",")+"};",70)]
3665         end
3666     end
3667
3668     if Code_outtb<>[] then
3669         mputl([""
3670         "  /* outtb declaration */"
3671         Code_outtb
3672         ""], fd);
3673     end
3674
3675     Code_outtbptr=[];
3676     for i=1:lstsize(outtb)
3677         Code_outtbptr=[Code_outtbptr;
3678         "  "+rdnom+"_block_outtbptr["+...
3679         string(i-1)+"] = (void *) outtb_"+string(i)+";"];
3680     end
3681
3682     //** declaration of work
3683     mputl(["  /* work ptr declaration */"
3684     "  void **work;"
3685     ""
3686     //## affectation of work
3687     "  /* Get work ptr of blocks */"
3688     "  work = (void **)(z+"+string(size(z,"*")+lstsize(outtb))+");"
3689     ""], fd);
3690
3691     //## affection of outtbptr
3692     if Code_outtb<>[] then
3693         mputl(["  /* Get outtbptr ptr of blocks */"
3694         "  "+rdnom+"_block_outtbptr = (void **)(z+"+string(nztotal)+");"
3695         ""], fd);
3696     end
3697     if Code_outtbptr<>[] then
3698         mputl([Code_outtbptr
3699         ""], fd);
3700     end
3701
3702     for kf=1:nblk
3703         nx=xptr(kf+1)-xptr(kf);       //** number of continuous state
3704         nin=inpptr(kf+1)-inpptr(kf);  //** number of input ports
3705         nout=outptr(kf+1)-outptr(kf); //** number of output ports
3706
3707         //** add comment
3708         mputl([""
3709         "  "+get_comment("set_blk",list(funs(kf),funtyp(kf),kf,labels(kf)));], fd);
3710
3711         mputl(["  block_"+rdnom+"["+string(kf-1)+"].type   = "+string(funtyp(kf))+";";
3712         "  block_"+rdnom+"["+string(kf-1)+"].ztyp   = "+string(ztyp(kf))+";";
3713         "  block_"+rdnom+"["+string(kf-1)+"].ng     = "+string(zcptr(kf+1)-zcptr(kf))+";";
3714         "  block_"+rdnom+"["+string(kf-1)+"].nz     = "+string(zptr(kf+1)-zptr(kf))+";";
3715         "  block_"+rdnom+"["+string(kf-1)+"].noz    = "+string(ozptr(kf+1)-ozptr(kf))+";";
3716         "  block_"+rdnom+"["+string(kf-1)+"].nrpar  = "+string(rpptr(kf+1)-rpptr(kf))+";";
3717         "  block_"+rdnom+"["+string(kf-1)+"].nopar  = "+string(opptr(kf+1)-opptr(kf))+";";
3718         "  block_"+rdnom+"["+string(kf-1)+"].nipar  = "+string(ipptr(kf+1)-ipptr(kf))+";"
3719         "  block_"+rdnom+"["+string(kf-1)+"].nin    = "+string(inpptr(kf+1)-inpptr(kf))+";";
3720         "  block_"+rdnom+"["+string(kf-1)+"].nout   = "+string(outptr(kf+1)-outptr(kf))+";";
3721         "  block_"+rdnom+"["+string(kf-1)+"].nevout = "+string(clkptr(kf+1)-clkptr(kf))+";";
3722         "  block_"+rdnom+"["+string(kf-1)+"].nmode  = "+string(modptr(kf+1)-modptr(kf))+";";], fd);
3723
3724         if nx <> 0 then
3725             mputl(["  block_"+rdnom+"["+string(kf-1)+"].nx = "+string(nx)+";";
3726             "  block_"+rdnom+"["+string(kf-1)+"].x  = &(x["+string(xptr(kf)-1)+"]);"
3727             "  block_"+rdnom+"["+string(kf-1)+"].xd = &(xd["+string(xptr(kf)-1)+"]);"], fd);
3728         end
3729
3730         mputl(["  if ((block_"+rdnom+"["+string(kf-1)+"].evout  = "+...
3731         "calloc(block_"+rdnom+"["+string(kf-1)+"].nevout,sizeof(double)))== NULL) return 0;"], fd);
3732
3733         //***************************** input port *****************************//
3734         //** alloc insz/inptr **//
3735         mputl(["  if ((block_"+rdnom+"["+string(kf-1)+"].inptr  = "+...
3736         "malloc(sizeof(double*)*block_"+rdnom+"["+string(kf-1)+"].nin))== NULL) return 0;";
3737         "  if ((block_"+rdnom+"["+string(kf-1)+"].insz   = "+...
3738         "malloc(3*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nin))== NULL) return 0;"], fd);
3739
3740         //** inptr **//
3741         for k=1:nin
3742             lprt=inplnk(inpptr(kf)-1+k);
3743             mputl(["  block_"+rdnom+"["+string(kf-1)+"].inptr["+string(k-1)+...
3744             "]  = "+rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
3745         end
3746
3747         //** 1st dim **//
3748         for k=1:nin
3749             lprt=inplnk(inpptr(kf)-1+k);
3750             mputl(["  block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1))+...
3751             "]   = "+string(size(outtb(lprt),1))+";"], fd);
3752         end
3753
3754         //** 2dn dim **//
3755         for k=1:nin
3756             lprt=inplnk(inpptr(kf)-1+k);
3757             mputl(["  block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1)+nin)+...
3758             "]   = "+string(size(outtb(lprt),2))+";"], fd);
3759         end
3760
3761         //** typ **//
3762         for k=1:nin
3763             lprt=inplnk(inpptr(kf)-1+k);
3764             mputl(["  block_"+rdnom+"["+string(kf-1)+"].insz["+string((k-1)+2*nin)+...
3765             "]   = "+mat2scs_c_typ(outtb(lprt))+";"], fd);
3766         end
3767         //**********************************************************************//
3768
3769         //***************************** output port *****************************//
3770         //** alloc outsz/outptr **//
3771         mputl(["  if ((block_"+rdnom+"["+string(kf-1)+"].outsz  = "+...
3772         "malloc(3*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nout))== NULL) return 0;";
3773         "  if ((block_"+rdnom+"["+string(kf-1)+"].outptr = "+...
3774         "malloc(sizeof(double*)*block_"+rdnom+"["+string(kf-1)+"].nout))== NULL) return 0;"], fd);
3775
3776         //** outptr **//
3777         for k=1:nout
3778             lprt=outlnk(outptr(kf)-1+k);
3779             mputl(["  block_"+rdnom+"["+string(kf-1)+"].outptr["+string(k-1)+...
3780             "] = "+rdnom+"_block_outtbptr["+string(lprt-1)+"];"], fd);
3781         end
3782
3783         //** 1st dim **//
3784         for k=1:nout
3785             lprt=outlnk(outptr(kf)-1+k);
3786             mputl(["  block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1))+...
3787             "]  = "+string(size(outtb(lprt),1))+";"], fd);
3788         end
3789
3790         //** 2dn dim **//
3791         for k=1:nout
3792             lprt=outlnk(outptr(kf)-1+k);
3793             mputl(["  block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1)+nout)+...
3794             "]  = "+string(size(outtb(lprt),2))+";"], fd);
3795         end
3796
3797         //** typ **//
3798         for k=1:nout
3799             lprt=outlnk(outptr(kf)-1+k);
3800             mputl(["  block_"+rdnom+"["+string(kf-1)+"].outsz["+string((k-1)+2*nout)+...
3801             "]  = "+mat2scs_c_typ(outtb(lprt))+";"], fd);
3802         end
3803         //**********************************************************************//
3804
3805         mputl(["  block_"+rdnom+"["+string(kf-1)+...
3806         "].z = &(z["+string(zptr(kf)-1)+"]);"], fd);
3807
3808         if (part(funs(kf),1:7) ~= "capteur" &...
3809             part(funs(kf),1:10) ~= "actionneur" &...
3810             funs(kf) ~= "bidon") then
3811             //** rpar **//
3812             if (rpptr(kf+1)-rpptr(kf)>0) then
3813                 mputl([ "  block_"+rdnom+"["+string(kf-1)+...
3814                 "].rpar=&(RPAR1["+string(rpptr(kf)-1)+"]);"], fd);
3815             end
3816             //** ipar **//
3817             if (ipptr(kf+1)-ipptr(kf)>0) then
3818                 mputl(["  block_"+rdnom+"["+string(kf-1)+...
3819                 "].ipar=&(IPAR1["+string(ipptr(kf)-1)+"]);"], fd);
3820             end
3821             //** opar **//
3822             if (opptr(kf+1)-opptr(kf)>0) then
3823                 mputl(["  if ((block_"+rdnom+"["+string(kf-1)+"].oparptr = "+...
3824                 "malloc(sizeof(void *)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL ) return 0;";
3825                 "  if ((block_"+rdnom+"["+string(kf-1)+"].oparsz  = "+...
3826                 "malloc(2*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL ) return 0;";
3827                 "  if ((block_"+rdnom+"["+string(kf-1)+"].opartyp = "+...
3828                 "malloc(sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].nopar))== NULL ) return 0;";
3829                 ], fd);
3830                 nopar = opptr(kf+1)-opptr(kf);
3831                 //** oparptr **//
3832                 for k=1:nopar
3833                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].oparptr["+string(k-1)+...
3834                     "]   = (void *) OPAR_"+string(opptr(kf)-1+k)+";"], fd);
3835                 end
3836                 //** 1st dim **//
3837                 for k=1:nopar
3838                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].oparsz["+string(k-1)+...
3839                     "]    = "+string(size(opar(opptr(kf)-1+k),1))+";"], fd);
3840                 end
3841                 //** 2dn dim **//
3842                 for k=1:nopar
3843                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].oparsz["+string(nopar+(k-1))+...
3844                     "]    = "+string(size(opar(opptr(kf)-1+k),2))+";"], fd);
3845                 end
3846                 //** typ **//
3847                 for k=1:nopar
3848                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].opartyp["+string(k-1)+...
3849                     "]   = "+mat2scs_c_typ(opar(opptr(kf)-1+k))+";"], fd);
3850                 end
3851             end
3852             //** oz **//
3853             if (ozptr(kf+1)-ozptr(kf)>0) then
3854                 noz = ozptr(kf+1)-ozptr(kf);
3855                 mputl(["  if ((block_"+rdnom+"["+string(kf-1)+"].ozptr = "+...
3856                 "malloc(sizeof(void *)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";
3857                 "  if ((block_"+rdnom+"["+string(kf-1)+"].ozsz  = "+...
3858                 "malloc(2*sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";
3859                 "  if ((block_"+rdnom+"["+string(kf-1)+"].oztyp = "+...
3860                 "malloc(sizeof(int)*block_"+rdnom+"["+string(kf-1)+"].noz))== NULL) return 0;";], fd);
3861                 //** ozptr **//
3862                 for k=1:noz
3863                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].ozptr["+string(k-1)+...
3864                     "]  = (void *) oz_"+string(ozptr(kf)-1+k)+";"], fd);
3865                 end
3866                 //** 1st dim **//
3867                 for k=1:noz
3868                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].ozsz["+string(k-1)+...
3869                     "]  = "+string(size(oz(ozptr(kf)-1+k),1))+";"], fd);
3870                 end
3871                 //** 2nd dim **//
3872                 for k=1:noz
3873                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].ozsz["+string(noz+(k-1))+...
3874                     "]  = "+string(size(oz(ozptr(kf)-1+k),2))+";"], fd);
3875                 end
3876                 //** typ **//
3877                 for k=1:noz
3878                     mputl(["  block_"+rdnom+"["+string(kf-1)+"].oztyp["+string(k-1)+...
3879                     "]  = "+mat2scs_c_typ(oz(ozptr(kf)-1+k))+";"], fd);
3880                 end
3881             end
3882         end
3883         mputl(["  block_"+rdnom+"["+string(kf-1)+"].work = "+...
3884         "(void **)(((double *)work)+"+string(kf-1)+");"], fd);
3885     end
3886
3887     mputl(["  if (strlen(input) > 0)"
3888     "    aaa=1;"
3889     "  if (strlen(output)> 0)"
3890     "    bbb=1;"
3891     ""
3892     "  /* set initial time */"
3893     "  t=0.0;"
3894     ""], fd);
3895
3896     //** init
3897     mputl(["  "+get_comment("flag",list(4))], fd);
3898
3899     for kf=1:nblk
3900         if or(kf==act) | or(kf==cap) then
3901             txt = call_block42(kf,0,4);
3902             if txt <> [] then
3903                 mputl(["";
3904                 "  "+txt], fd);
3905             end
3906         else
3907             txt = call_block42(kf,0,4);
3908             if txt <> [] then
3909                 mputl(["";
3910                 "  "+txt], fd);
3911             end
3912         end
3913     end
3914
3915     //** cst blocks and it's dep
3916     txt=write_code_idoit()
3917
3918     if txt<>[] then
3919         mputl([""
3920         "    /* Initial blocks must be called with flag 1 */"
3921         txt], fd);
3922     end
3923
3924     //** begin input main loop on time
3925     mputl([""
3926     "  while (t < tf) {";
3927     "    /* */"
3928     "    sci_time=t;"
3929     ""], fd);
3930
3931     //** find source activation number
3932     blks=find(funtyp>-1);
3933     evs=[];
3934
3935     for blk=blks
3936         for ev=clkptr(blk):clkptr(blk+1)-1
3937             if funs(blk)=="bidon" then
3938                 if ev > clkptr(howclk) -1
3939                     evs=[evs,ev];
3940                 end
3941             end
3942         end
3943     end
3944
3945     //** flag 1,2,3
3946     for flag=[1,2,3]
3947
3948         txt3=[]
3949
3950         //** continuous time blocks must be activated
3951         //** for flag 1
3952         if flag==1 then
3953             txt = write_code_cdoit(flag);
3954
3955             if txt <> [] then
3956                 txt3=[""
3957                 "    "+get_comment("ev",list(0))
3958                 txt;
3959                 ];
3960             end
3961         end
3962
3963         //** blocks with input discrete event must be activated
3964         //** for flag 1, 2 and 3
3965         if size(evs,2)>=1 then
3966             txt4=[]
3967             //**
3968             for ev=evs
3969                 txt2=write_code_doit(ev,flag);
3970                 if txt2<>[] then
3971                     //** adjust event number because of bidon block
3972                     new_ev=ev-(clkptr(howclk)-1)
3973                     //**
3974                     txt4=[txt4;
3975                     Indent2+["  case "+string(new_ev)+" : "+...
3976                     get_comment("ev",list(new_ev))
3977                     txt2];
3978                     "      break;";""]
3979                 end
3980             end
3981
3982             //**
3983             if txt4 <> [] then
3984                 txt3=[txt3;
3985                 Indent+"  /* Discrete activations */"
3986                 Indent+"  switch (nevprt) {"
3987                 txt4
3988                 "    }"];
3989             end
3990         end
3991
3992         //**
3993         if txt3<>[] then
3994             mputl(["    "+get_comment("flag",list(flag))
3995             txt3], fd);
3996         end
3997     end
3998
3999     if x<>[] then
4000         mputl([""
4001         "    tout=t;"
4002         ""
4003         "   /* integrate until the cumulative add of the integration"
4004         "    * time step doesn''t cross the sample time step"
4005         "    */"
4006         "    while (tout+h<t+dt){"
4007         "      switch (solver) {"
4008         "      case 1:"
4009         "        ode1(C2F("+rdnom+"simblk),tout,h);"
4010         "        break;"
4011         "      case 2:"
4012         "        ode2(C2F("+rdnom+"simblk),tout,h);"
4013         "        break;"
4014         "      case 3:"
4015         "        ode4(C2F("+rdnom+"simblk),tout,h);"
4016         "        break;"
4017         "      default :"
4018         "        ode4(C2F("+rdnom+"simblk),tout,h);"
4019         "        break;"
4020         "        }"
4021         "       tout=tout+h;"
4022         "    }"
4023         ""
4024         "    /* integration for the remainder piece of time */"
4025         "    he=t+dt-tout;"
4026         "    switch (solver) {"
4027         "    case 1:"
4028         "      ode1(C2F("+rdnom+"simblk),tout,he);"
4029         "      break;"
4030         "    case 2:"
4031         "      ode2(C2F("+rdnom+"simblk),tout,he);"
4032         "      break;"
4033         "    case 3:"
4034         "      ode4(C2F("+rdnom+"simblk),tout,he);"
4035         "      break;"
4036         "    default :"
4037         "      ode4(C2F("+rdnom+"simblk),tout,he);"
4038         "      break;"
4039         "    }"], fd);
4040     end
4041
4042     //** fix bug provided by Roberto Bucher
4043     //** Alan, 13/10/07
4044     if nX <> 0 then
4045         mputl([""
4046         "    /* update ptrs of continuous array */"], fd);
4047         for kf=1:nblk
4048             nx=xptr(kf+1)-xptr(kf);  //** number of continuous state
4049             if nx<>0 then
4050                 mputl(["    block_"+rdnom+"["+string(kf-1)+"].nx = "+...
4051                 string(nx)+";";
4052                 "    block_"+rdnom+"["+string(kf-1)+"].x  = "+...
4053                 "&(x["+string(xptr(kf)-1)+"]);"
4054                 "    block_"+rdnom+"["+string(kf-1)+"].xd = "+...
4055                 "&(xd["+string(xptr(kf)-1)+"]);"], fd);
4056             end
4057         end
4058     end
4059
4060     mputl([""
4061     "    /* update current time */"
4062     "    t=t+dt;"
4063     "  }"
4064     //** flag 5
4065     ""
4066     " sci_time = tf;"
4067     ""
4068     "  "+get_comment("flag",list(5))], fd);
4069
4070     for kf=1:nblk
4071         if or(kf==act) | or(kf==cap) then
4072             txt = call_block42(kf,0,5);
4073             if txt <> [] then
4074                 mputl(["";
4075                 "  "+txt], fd);
4076             end
4077         else
4078             txt = call_block42(kf,0,5);
4079             if txt <> [] then
4080                 mputl(["";
4081                 "  "+txt], fd);
4082             end
4083         end
4084     end
4085
4086     mputl([""
4087     "  return 0;"
4088     "}"
4089     ""
4090     "/*"+part("-",ones(1,40))+"  Lapack messag function */";
4091     "void C2F(xerbla)(char *SRNAME,int *INFO,long int L)"
4092     "{"
4093     "  printf(""** On entry to %s, parameter number %d"""
4094     "         ""  had an illegal value\n"",SRNAME,*INFO);"
4095     "}"
4096     ""
4097     "void set_block_error(int err)"
4098     "{"
4099     "  return;"
4100     "}"
4101     ""
4102     "int get_phase_simulation()"
4103     "{"
4104     "  return 1;"
4105     "}"
4106     ""
4107     "void * scicos_malloc(size_t size)"
4108     "{"
4109     "  return malloc(size);"
4110     "}"
4111     ""
4112     "void scicos_free(void *p)"
4113     "{"
4114     "  free(p);"
4115     "}"
4116     ""
4117     "double get_scicos_time()"
4118     "{"
4119     "  return sci_time;"
4120     "}"
4121     ""
4122     "void do_cold_restart()"
4123     "{"
4124     "  return;"
4125     "}"
4126     ""
4127     "void sciprint (char *fmt)"
4128     "{"
4129     "  return;"
4130     "}"
4131     ""
4132     "int getopt (int argc, char *argv[], char *optstring)"
4133     "{"
4134     "  char *group, option, *sopt;"
4135     "  int len;"
4136     "  option = -1;"
4137     "  optarg = NULL;"
4138     "  while ( optind < argc )"
4139     "    { "
4140     "      group = argv[optind];"
4141     "      if ( *group != ''-'' )"
4142     "        {"
4143     "         option = -1;"
4144     "         optarg = group;"
4145     "         optind++;"
4146     "         break;"
4147     "        }"
4148     "      len = strlen (group);"
4149     "      group = group + offset;"
4150     "      if ( *group == ''-'' )"
4151     "        {"
4152     "         group++;"
4153     "         offset += 2;"
4154     "        }"
4155     "      else"
4156     "        offset++ ;"
4157     "      option = *group ;"
4158     "      sopt = strchr ( optstring, option ) ;"
4159     "      if ( sopt != NULL )"
4160     "        {"
4161     "         sopt++ ;"
4162     "         if ( *sopt == '':'' )"
4163     "           {"
4164     "             optarg = group + 1;"
4165     "             if ( *optarg == ''\0'' )"
4166     "                optarg = argv[++optind];"
4167     "             if ( *optarg == ''-'' )"
4168     "                {"
4169     "                 fprintf ( stderr, '"\n%s: illegal option -- %c'","
4170     "                           argv[0], option );"
4171     "                 option = ''?'';"
4172     "                 break;"
4173     "                }"
4174     "             else"
4175     "                {"
4176     "                 optind++;"
4177     "                 offset = 0;"
4178     "                 break;"
4179     "                }"
4180     "           }"
4181     "         if ( offset >= len )"
4182     "           {"
4183     "             optind++;"
4184     "             offset = 0;"
4185     "           }"
4186     "         break;"
4187     "        }"
4188     "      else"
4189     "        {"
4190     "         fprintf ( stderr, '"\n%s: illegal option -- %c'", argv[0], option );"
4191     "         option = ''?'';"
4192     "         break;"
4193     "        }"
4194     "    }"
4195     "  return ( option );"
4196     "}"
4197     ""], fd);
4198
4199     if (x <> []) then
4200         mputl(["int C2F("+rdnom+"simblk)(t, x, xd)"
4201         ""
4202         "   double t, *x, *xd;"
4203         ""
4204         "     /*"
4205         "      *  !purpose"
4206         "      *  compute state derivative of the continuous part"
4207         "      *  !calling sequence"
4208         "      *  neq   : integer the size of the  continuous state"
4209         "      *  t     : current time"
4210         "      *  x     : double precision vector whose contains the continuous state"
4211         "      *  xd    : double precision vector, contain the computed derivative"
4212         "      *  of the state"
4213         "      */"
4214         "{"
4215         "  int phase=2;"
4216         "  int local_flag;"
4217         "  int nport;"
4218         "  int nevprt=1;"
4219         "  double *args[100];"
4220         "  dset(&neq, &c_b14,xd , &c__1);"
4221         ""
4222         "    "+get_comment("update_xd",list())], fd);
4223
4224         for kf=1:nblk
4225             if (xptr(kf+1)-xptr(kf)) > 0 then
4226                 mputl(["    block_"+rdnom+"["+string(kf-1)+"].x="+...
4227                 "&(x["+string(xptr(kf)-1)+"]);"
4228                 "    block_"+rdnom+"["+string(kf-1)+"].xd="+...
4229                 "&(xd["+string(xptr(kf)-1)+"]);"], fd);
4230             end
4231         end
4232
4233         mputl([""
4234         write_code_odoit(1)
4235         write_code_odoit(0)
4236         ""
4237         "  return 0;"
4238         "}"
4239         ""
4240         "int dset(int *n, double *dx, double *dy, int *incy)"
4241         "{"
4242         "    int iMax = 0;"
4243         "    int i = 0, iy = 0;"
4244         "    --dy;"
4245         "    if (*n <= 0) return 0;"
4246         "    iy = 1;"
4247         "    if (*incy < 0)"
4248         "    {"
4249         "      iy = (-(*n) + 1) * *incy + 1;"
4250         "    }"
4251         "    iMax = *n;"
4252         "    for (i = 1; i <= iMax; ++i)"
4253         "    {"
4254         "      dy[iy] = *dx;"
4255         "      iy += *incy;"
4256         "    }"
4257         "    return 0;"
4258         "}"
4259         ""
4260         "/* Euler''s Method */"
4261         "int ode1(f,t,h)"
4262         "  int (*f) ();"
4263         "  double t, h;"
4264         "{"
4265         "  int i;"
4266         ""
4267         "  /**/"
4268         "  (*f)(t,x, xd);"
4269         ""
4270         "  for (i=0;i<neq;i++) {"
4271         "   x[i]=x[i]+h*xd[i];"
4272         "  }"
4273         ""
4274         "  return 0;"
4275         "}"
4276         ""
4277         "/* Heun''s Method */"
4278         "int ode2(f,t,h)"
4279         "  int (*f) ();"
4280         "  double t, h;"
4281         "{"
4282         "  int i;"
4283         "  double y["+string(nX)+"],yh["+string(nX)+"],temp,f0["+string(nX)+"],th;"
4284         ""
4285         "  /**/"
4286         "  memcpy(y,x,neq*sizeof(double));"
4287         "  memcpy(f0,xd,neq*sizeof(double));"
4288         ""
4289         "  /**/"
4290         "  (*f)(t,y, f0);"
4291         ""
4292         "  /**/"
4293         "  for (i=0;i<neq;i++) {"
4294         "    x[i]=y[i]+h*f0[i];"
4295         "  }"
4296         "  th=t+h;"
4297         "  for (i=0;i<neq;i++) {"
4298         "    yh[i]=y[i]+h*f0[i];"
4299         "  }"
4300         "  (*f)(th,yh, xd);"
4301         ""
4302         "  /**/"
4303         "  temp=0.5*h;"
4304         "  for (i=0;i<neq;i++) {"
4305         "    x[i]=y[i]+temp*(f0[i]+xd[i]);"
4306         "  }"
4307         ""
4308         "  return 0;"
4309         "}"
4310         ""
4311         "/* Fourth-Order Runge-Kutta (RK4) Formula */"
4312         "int ode4(f,t,h)"
4313         "  int (*f) ();"
4314         "  double t, h;"
4315         "{"
4316         "  int i;"
4317         "  double y["+string(nX)+"],yh["+string(nX)+"],"+...
4318         "temp,f0["+string(nX)+"],th,th2,"+...
4319         "f1["+string(nX)+"],f2["+string(nX)+"];"
4320         ""
4321         "  /**/"
4322         "  memcpy(y,x,neq*sizeof(double));"
4323         "  memcpy(f0,xd,neq*sizeof(double));"
4324         ""
4325         "  /**/"
4326         "  (*f)(t,y, f0);"
4327         ""
4328         "  /**/"
4329         "  for (i=0;i<neq;i++) {"
4330         "    x[i]=y[i]+h*f0[i];"
4331         "  }"
4332         "  th2=t+h/2;"
4333         "  for (i=0;i<neq;i++) {"
4334         "    yh[i]=y[i]+(h/2)*f0[i];"
4335         "  }"
4336         "  (*f)(th2,yh, f1);"
4337         ""
4338         "  /**/"
4339         "  temp=0.5*h;"
4340         "  for (i=0;i<neq;i++) {"
4341         "    x[i]=y[i]+temp*f1[i];"
4342         "  }"
4343         "  for (i=0;i<neq;i++) {"
4344         "    yh[i]=y[i]+(h/2)*f1[i];"
4345         "  }"
4346         "  (*f)(th2,yh, f2);"
4347         ""
4348         "  /**/"
4349         "  for (i=0;i<neq;i++) {"
4350         "    x[i]=y[i]+h*f2[i];"
4351         "  }"
4352         "  th=t+h;"
4353         "  for (i=0;i<neq;i++) {"
4354         "    yh[i]=y[i]+h*f2[i];"
4355         "  }"
4356         "  (*f)(th2,yh, xd);"
4357         ""
4358         "  /**/"
4359         "  temp=h/6;"
4360         "  for (i=0;i<neq;i++) {"
4361         "    x[i]=y[i]+temp*(f0[i]+2.0*f1[i]+2.0*f2[i]+xd[i]);"
4362         "  }"
4363         ""
4364         "return 0;"
4365         "}"], fd);
4366     end
4367
4368     mclose(fd);
4369 endfunction
4370
4371 //generates  static table definitions
4372 //
4373 //Author : Rachid Djenidi, Alan Layec
4374 function txt=make_static_standalone42()
4375     oldFormat = format();
4376     format(25);
4377     txt=[];
4378
4379     //*** Continuous state ***//
4380     if x <> [] then
4381         txt=[txt;
4382         "/* def continuous state */"
4383         cformatline("double x[]={"+strcat(string(x),",")+"};",70)
4384         cformatline("double xd[]={"+strcat(string(x),",")+"};",70)
4385         "static int c__1 = 1;"
4386         "static double c_b14 = 0.;"
4387         "static int neq="+string(nX)+";"
4388         ""]
4389     end
4390     //************************//
4391
4392     txt=[txt;
4393     "char input[50], output[50],s[1],sf[1],se[1],ss[1],**p;"
4394     "static double sci_time;"
4395     "static int errflg = 0;"
4396     "static char *optarg = NULL ;"
4397     "static int optind = 1, offset = 0 ;"
4398     "scicos_block block_"+rdnom+"["+string(nblk)+"];"
4399     ""];
4400
4401     //*** Real parameters ***//
4402     if size(rpar,1) <> 0 then
4403         txt=[txt;
4404         "/* def real parameters */"
4405         "static double RPAR1[ ] = {"];
4406
4407         for i=1:(length(rpptr)-1)
4408             if rpptr(i+1)-rpptr(i)>0  then
4409
4410                 if size(corinv(i),"*")==1 then
4411                     OO=scs_m.objs(corinv(i));
4412                 else
4413                     path=list("objs");
4414                     for l=cpr.corinv(i)(1:$-1)
4415                         path($+1)=l;
4416                         path($+1)="model";
4417                         path($+1)="rpar";
4418                         path($+1)="objs";
4419                     end
4420                     path($+1)=cpr.corinv(i)($);
4421                     OO=scs_m(path);
4422                 end
4423
4424                 //** Add comments **//
4425                 txt($+1)="/* Routine name of block: "+strcat(string(cpr.sim.funs(i)));
4426                 txt($+1)=" * Gui name of block: "+strcat(string(OO.gui));
4427                 txt($+1)=" * Compiled structure index: "+strcat(string(i));
4428
4429                 if stripblanks(OO.model.label)~=emptystr() then
4430                     txt=[txt;cformatline(" * Label: "+strcat(string(OO.model.label)),70)];
4431                 end
4432                 if stripblanks(OO.graphics.exprs(1))~=emptystr() then
4433                     txt=[txt;cformatline(" * Exprs: "+strcat(OO.graphics.exprs(1),","),70)];
4434                 end
4435                 if stripblanks(OO.graphics.id)~=emptystr() then
4436                     txt=[txt;
4437                     cformatline(" * Identification: "+strcat(string(OO.graphics.id)),70)];
4438                 end
4439                 txt=[txt;" * rpar="];
4440                 txt($+1)=" */";
4441                 //******************//
4442
4443                 txt=[txt;
4444                 cformatline(strcat(msprintf("%.25E,\n",rpar(rpptr(i):rpptr(i+1)-1))),70);
4445                 ""]
4446
4447             end
4448         end
4449         txt=[txt;
4450         "};"
4451         ""]
4452
4453     else
4454         txt($+1)="static double RPAR1[1];";
4455     end
4456     //***********************//
4457
4458     //*** Integer parameters ***//
4459     if size(ipar,1) <> 0 then
4460         txt=[txt;
4461         "/* def integer parameters */"
4462         "static int IPAR1[ ] = {"];
4463
4464         for i=1:(length(ipptr)-1)
4465             if ipptr(i+1)-ipptr(i)>0  then
4466                 if size(corinv(i),"*")==1 then
4467                     OO=scs_m.objs(corinv(i));
4468                 else
4469                     path=list("objs");
4470                     for l=cpr.corinv(i)(1:$-1)
4471                         path($+1)=l
4472                         path($+1)="model"
4473                         path($+1)="rpar"
4474                         path($+1)="objs"
4475                     end
4476                     path($+1)=cpr.corinv(i)($);
4477                     OO=scs_m(path);
4478                 end
4479
4480                 //** Add comments **//
4481                 txt($+1)="/* Routine name of block: "+strcat(string(cpr.sim.funs(i)));
4482                 txt($+1)=" * Gui name of block: "+strcat(string(OO.gui));
4483                 txt($+1)=" * Compiled structure index: "+strcat(string(i));
4484                 if stripblanks(OO.model.label)~=emptystr() then
4485                     txt=[txt;cformatline(" * Label: "+strcat(string(OO.model.label)),70)];
4486                 end
4487
4488                 if stripblanks(OO.graphics.exprs(1))~=emptystr() then
4489                     txt=[txt;
4490                     cformatline(" * Exprs: "+strcat(OO.graphics.exprs(1),","),70)];
4491                 end
4492                 if stripblanks(OO.graphics.id)~=emptystr() then
4493                     txt=[txt;
4494                     cformatline(" * Identification: "+strcat(string(OO.graphics.id)),70)];
4495                 end
4496                 txt=[txt;
4497                 cformatline(" * ipar= {"+strcat(string(ipar(ipptr(i):ipptr(i+1)-1)),",")+"};",70)];
4498                 txt($+1)=" */";
4499                 //******************//
4500
4501                 txt=[txt;cformatline(strcat(string(ipar(ipptr(i):ipptr(i+1)-1))+","),70)];
4502                 txt($+1)="";
4503             end
4504         end
4505         txt=[txt;
4506         "};"
4507         ""]
4508     else
4509         txt($+1)="static int IPAR1[1];";
4510     end
4511     //**************************//
4512
4513     //Alan added opar (27/06/07)
4514     //*** Object parameters ***//
4515     if lstsize(opar)<>0 then
4516         txt=[txt;
4517         "/* def object parameters */"]
4518         for i=1:(length(opptr)-1)
4519             if opptr(i+1)-opptr(i)>0  then
4520
4521                 if size(corinv(i),"*")==1 then
4522                     OO=scs_m.objs(corinv(i));
4523                 else
4524                     path=list("objs");
4525                     for l=cpr.corinv(i)(1:$-1)
4526                         path($+1)=l;
4527                         path($+1)="model";
4528                         path($+1)="rpar";
4529                         path($+1)="objs";
4530                     end
4531                     path($+1)=cpr.corinv(i)($);
4532                     OO=scs_m(path);
4533                 end
4534
4535                 //** Add comments **//
4536                 txt($+1)="";
4537                 txt($+1)="/* Routine name of block: "+strcat(string(cpr.sim.funs(i)));
4538                 txt($+1)=" * Gui name of block: "+strcat(string(OO.gui));
4539                 txt($+1)=" * Compiled structure index: "+strcat(string(i));
4540                 if stripblanks(OO.model.label)~=emptystr() then
4541                     txt=[txt;cformatline(" * Label: "+strcat(string(OO.model.label)),70)];
4542                 end
4543                 if stripblanks(OO.graphics.id)~=emptystr() then
4544                     txt=[txt;
4545                     cformatline(" * Identification: "+strcat(string(OO.graphics.id)),70)];
4546                 end
4547                 txt($+1)=" */";
4548                 //******************//
4549
4550
4551                 for j=1:opptr(i+1)-opptr(i)
4552                     txt =[txt;
4553                     cformatline("static "+mat2c_typ(opar(opptr(i)+j-1)) +...
4554                     " OPAR_"+string(opptr(i)+j-1) + "[] = {"+...
4555                     strcat(string(opar(opptr(i)+j-1)),",")+"};",70)]
4556                 end
4557             end
4558         end
4559     end
4560     //*************************//
4561
4562     txt=[txt;
4563     ""]
4564     format(oldFormat(2), oldFormat(1));
4565 endfunction
4566
4567 //utilitary fonction used to format long C instruction
4568 //t : a string containing a C instruction
4569 //l : max line length allowed
4570
4571 //Author : Rachid Djenidi
4572 function t1=cformatline(t ,l)
4573     // Force format to avoid precision loss
4574     oldFormat = format();
4575     format(25);
4576
4577     sep=[",","+"]
4578     l1=l-2
4579     t1=[]
4580     kw=strindex(t," ")
4581     nw=0
4582     if kw<>[] then
4583         if kw(1)==1 then // there is leading blanks
4584             k1=find(kw(2:$)-kw(1:$-1)<>1)
4585             if k1==[] then // there is a single blank
4586                 nw=1
4587             else
4588                 nw=kw(k1(1))
4589             end
4590         end
4591     end
4592     t=part(t,nw+1:length(t));
4593     bl=part(" ",ones(1,nw))
4594     l1=l-nw;first=%t
4595     while %t
4596         if length(t)<=l then
4597             t1=[t1;bl+t]
4598             break
4599         end
4600         k=strindex(t,sep);
4601         if k==[] then
4602             t1=[t1;bl+t]
4603             break
4604         end
4605         k($+1)=length(t)+1 // positions of the commas
4606         i=find(k(1:$-1)<=l&k(2:$)>l) //nearest left comma (reltively to l)
4607         if i==[] then
4608             i=1
4609         end
4610         t1=[t1;bl+part(t,1:k(i))]
4611         t=part(t,k(i)+1:length(t))
4612         if first then
4613             l1=l1-2;bl=bl+"  ";
4614             first=%f;
4615         end
4616     end
4617
4618     format(oldFormat(2), oldFormat(1));
4619 endfunction
4620
4621 //
4622 // Generate Matrix of all binary code from
4623 // 1 to 2^n-1
4624 //
4625 function vec = binaryTable(n)
4626     n = int(n);
4627     vec = [];
4628     for i = 1:n
4629         accu = [];
4630         for j = 1:2^(n-i-1)
4631             accu = [accu ; zeros(2^(i-1),1) ; ones(2^(i-1),1)]
4632         end
4633         vec = [accu, vec]
4634     end
4635     vec=vec(2:$, :); // Remove first line [ 0 --- 0 ]
4636 endfunction
4637
4638 //used in do_compile_superblock
4639 function vec=codebinaire(v,szclkIN)
4640     vec=zeros(1,szclkIN)
4641     for i=1:szclkIN
4642         w=v/2;
4643         vec(i)=v-2*int(w);
4644         v=int(w);
4645     end
4646 endfunction
4647
4648 function t=filetype(m)
4649     m=int32(m)
4650     filetypes=["Directory","Character device","Block device",...
4651     "Regular file","FIFO","Symbolic link","Socket"]
4652     bits=[16384,8192,24576,32768,4096,40960,49152]
4653     m=int32(m)&int32(61440)
4654     t=filetypes(find(m==int32(bits)))
4655 endfunction
4656
4657 //get_blank : return blanks with a length
4658 //            of the given input string
4659 //
4660 //input : str : a string
4661 //
4662 //output : txt : blanks
4663 //
4664 //16/06/07 Author : A.Layec
4665 function [txt] = get_blank(str)
4666     txt="";
4667     for i=1:length(str)
4668         txt=txt+" ";
4669     end
4670 endfunction
4671
4672 // get_comment : return a C comment
4673 //               for generated code
4674 //
4675 //input : typ : a string
4676 //        param : a list
4677 //
4678 //output : a C comment
4679 //
4680 //16/06/07 Author : A.Layec
4681 function [txt]=get_comment(typ,param)
4682     txt = [];
4683     select typ
4684         //** main flag
4685     case "flag" then
4686         select param(1)
4687         case 0 then
4688             txt = "/* Continuous state computation */"
4689         case 1 then
4690             txt = "/* Output computation */"
4691         case 2 then
4692             txt = "/* Discrete state computation */"
4693         case 3 then
4694             txt = "/* Output Event computation */"
4695         case 4 then
4696             txt = "/* Initialization */"
4697         case 5 then
4698             txt = "/* Ending */"
4699         case 9 then
4700             txt = "/* Update zero crossing surfaces */"
4701         end
4702         //** blocks activated on event number
4703     case "ev" then
4704         txt = "/* Blocks activated on the event number "+string(param(1))+" */"
4705
4706         //** blk calling sequence
4707     case "call_blk" then
4708         txt = ["/* Call of ''"+param(1) + ...
4709         "'' (type "+string(param(2))+" - blk nb "+...
4710         string(param(3))+" - uid "+param(4)];
4711         if ztyp(param(3)) then
4712             txt=txt+" - with zcross) */";
4713         else
4714             txt=txt+") */";
4715         end
4716         //** proto calling sequence
4717     case "proto_blk" then
4718         txt = ["/* prototype of ''"+param(1) + ...
4719         "'' (type "+string(param(2))];
4720         if ztyp(param(3)) then
4721             txt=txt+" - with zcross) */";
4722         else
4723             txt=txt+") */";
4724         end
4725         //** ifthenelse calling sequence
4726     case "ifthenelse_blk" then
4727         txt = ["/* Call of ''if-then-else'' blk (blk nb "+...
4728         string(param(1))+") */"]
4729         //** eventselect calling sequence
4730     case "evtselect_blk" then
4731         txt = ["/* Call of ''event-select'' blk (blk nb "+...
4732         string(param(1))+") */"]
4733         //** set block structure
4734     case "set_blk" then
4735         txt = ["/* set blk struc. of ''"+param(1) + ...
4736         "'' (type "+string(param(2))+" - blk nb "+...
4737         string(param(3))++" - uid "+param(4)+") */"];
4738         //** Update xd vector ptr
4739     case "update_xd" then
4740         txt = ["/* Update xd vector ptr */"];
4741         //** Update g vector ptr
4742     case "update_g" then
4743         txt = ["/* Update g vector ptr */"];
4744     else
4745         break;
4746     end
4747 endfunction
4748
4749 //mat2c_typ : matrix to C type
4750 //sci2c_ttyp : get the C string of a scicos type
4751 //
4752 //input : outtb : a matrix
4753 //
4754 //output : txt : the string of the C scicos type
4755 //               of the data of outtb
4756 //
4757 //16/06/07 Author : A.Layec
4758 function [txt]=mat2c_typ(outtb)
4759     select type(outtb)
4760         //real matrix
4761     case 1 then
4762         if isreal(outtb) then
4763             txt = "double"
4764         else
4765             txt = "double"
4766         end
4767         //integer matrix
4768     case 8 then
4769         select typeof(outtb)
4770         case "int32" then
4771             txt = "long"
4772         case "int16" then
4773             txt = "short"
4774         case "int8" then
4775             txt = "char"
4776         case "uint32" then
4777             txt = "unsigned long"
4778         case "uint16" then
4779             txt = "unsigned short"
4780         case "uint8" then
4781             txt = "unsigned char"
4782         end
4783     else
4784         break;
4785     end
4786 endfunction
4787
4788 //mat2scs_c_nb  matrix to scicos C number (sci2sci_n)
4789 //
4790 //input : outtb : a matrix
4791 //
4792 //output : c_nb : the scicos C number
4793 //
4794 //16/06/07 Author : A.Layec
4795 function [c_nb]=mat2scs_c_nb(outtb)
4796     select type(outtb)
4797         //real matrix
4798     case 1 then
4799         if isreal(outtb) then
4800             c_nb = 10
4801         else
4802             c_nb = 11
4803         end
4804         //integer matrix
4805     case 8 then
4806         select typeof(outtb)
4807         case "int32" then
4808             c_nb = 84
4809         case "int16" then
4810             c_nb = 82
4811         case "int8" then
4812             c_nb = 81
4813         case "uint32" then
4814             c_nb = 814
4815         case "uint16" then
4816             c_nb = 812
4817         case "uint8" then
4818             c_nb = 811
4819         end
4820     else
4821         break;
4822     end
4823 endfunction
4824
4825 //mat2scs_c_ptr matrix to scicos C ptr (sci2c_typ)
4826 //
4827 //input : outtb : a matrix
4828 //
4829 //output : txt : the string of the C scicos type
4830 //               of the data of outtb
4831 //
4832 //16/06/07 Author : A.Layec
4833 function [txt]=mat2scs_c_ptr(outtb)
4834     select type(outtb)
4835         //real matrix
4836     case 1 then
4837         if isreal(outtb) then
4838             txt = "SCSREAL_COP"
4839         else
4840             txt = "SCSCOMPLEX_COP"
4841         end
4842         //integer matrix
4843     case 8 then
4844         select typeof(outtb)
4845         case "int32" then
4846             txt = "SCSINT32_COP"
4847         case "int16" then
4848             txt = "SCSINT16_COP"
4849         case "int8" then
4850             txt = "SCSINT8_COP"
4851         case "uint32" then
4852             txt = "SCSUINT32_COP"
4853         case "uint16" then
4854             txt = "SCSUINT16_COP"
4855         case "uint8" then
4856             txt = "SCSUINT8_COP"
4857         end
4858     else
4859         break;
4860     end
4861 endfunction
4862
4863 //mat2scs_c_typ matrix to scicos C type
4864 //
4865 //input : outtb : a matrix
4866 //
4867 //output : txt : the string of the C scicos type
4868 //               of the data of outtb
4869 //
4870 //16/06/07 Author : A.Layec
4871 function [txt]=mat2scs_c_typ(outtb)
4872     select type(outtb)
4873         //real matrix
4874     case 1 then
4875         if isreal(outtb) then
4876             txt = "SCSREAL_N"
4877         else
4878             txt = "SCSCOMPLEX_N"
4879         end
4880         //integer matrix
4881     case 8 then
4882         select typeof(outtb)
4883         case "int32" then
4884             txt = "SCSINT32_N"
4885         case "int16" then
4886             txt = "SCSINT16_N"
4887         case "int8" then
4888             txt = "SCSINT8_N"
4889         case "uint32" then
4890             txt = "SCSUINT32_N"
4891         case "uint16" then
4892             txt = "SCSUINT16_N"
4893         case "uint8" then
4894             txt = "SCSUINT8_N"
4895         end
4896     else
4897         break;
4898     end
4899 endfunction
4900
4901 //scs_c_n2c_fmt : scicos C number to C format
4902 //
4903 //input : c_nb : a C scicos type
4904 //
4905 //output : txt : the string of the C format string
4906 //               of the data of outtb
4907 //
4908 //16/06/07 Author : A.Layec
4909 function [txt]=scs_c_n2c_fmt(c_nb)
4910     select c_nb
4911         //real matrix
4912     case 10 then
4913         txt = "%f";
4914         //complex matrix
4915     case 11 then
4916         txt = "%f,%f";
4917         //int8 matrix
4918     case 81 then
4919         txt = "%d";
4920         //int16 matrix
4921     case 82 then
4922         txt = "%d";
4923         //int32 matrix
4924     case 84 then
4925         txt = "%d";
4926         //uint8 matrix
4927     case 811 then
4928         txt = "%d";
4929         //uint16 matrix
4930     case 812 then
4931         txt = "%d";
4932         //uint32 matrix
4933     case 814 then
4934         txt = "%d";
4935     else
4936         txt="%f"
4937         break;
4938     end
4939 endfunction
4940
4941 //scs_c_n2c_typ scicos C number to C type
4942 //
4943 //input : c_nb : a C scicos number
4944 //
4945 //output : txt : the string of the C format string
4946 //               of the data of outtb
4947 //
4948 //16/06/07 Author : A.Layec
4949 function [txt]=scs_c_n2c_typ(c_nb)
4950     select c_nb
4951         //real matrix
4952     case 10 then
4953         txt = "double";
4954         //complex matrix
4955     case 11 then
4956         txt = "double";
4957         //int8 matrix
4958     case 81 then
4959         txt = "char";
4960         //int16 matrix
4961     case 82 then
4962         txt = "short";
4963         //int32 matrix
4964     case 84 then
4965         txt = "long";
4966         //uint8 matrix
4967     case 811 then
4968         txt = "unsigned char";
4969         //uint16 matrix
4970     case 812 then
4971         txt = "unsigned short";
4972         //uint32 matrix
4973     case 814 then
4974         txt = "unsigned long";
4975     else
4976         txt="double"
4977         break;
4978     end
4979 endfunction
4980
4981 //scs_c_nb2scs_nb : scicos C number to scicos number
4982 //
4983 //input : c_nb  : the scicos C number type
4984 //
4985 //output : scs_nb : the scilab number type
4986 //
4987 //16/06/07 Author : A.Layec
4988 function [scs_nb]=scs_c_nb2scs_nb(c_nb)
4989     scs_nb=zeros(size(c_nb,1),size(c_nb,2));
4990     for i=1:size(c_nb,1)
4991         for j=1:size(c_nb,2)
4992             select (c_nb(i,j))
4993             case 10 then
4994                 scs_nb(i,j) = 1
4995             case 11 then
4996                 scs_nb(i,j) = 2
4997             case 81 then
4998                 scs_nb(i,j) = 5
4999             case 82 then
5000                 scs_nb(i,j) = 4
5001             case 84 then
5002                 scs_nb(i,j) = 3
5003             case 811 then
5004                 scs_nb(i,j) = 8
5005             case 812 then
5006                 scs_nb(i,j) = 7
5007             case 814 then
5008                 scs_nb(i,j) = 6
5009             else
5010                 scs_nb(i,j) = 1
5011             end
5012         end
5013     end
5014 endfunction
5015
5016 //used in do_compile_superblock
5017 function XX=update_block(XX)
5018     execstr("o="+rdnom+"_c(''define'')")
5019     XX.model=o.model
5020     XX.gui=rdnom+"_c";
5021     XX.graphics.gr_i=o.graphics.gr_i
5022 endfunction
5023
5024 //write_code_cdoit : generate body of the code for
5025 //                   for all time dependant blocks
5026 //
5027 //input : flag : flag number for block's call
5028 //
5029 //output : txt for cord blocks
5030 //
5031 //12/07/07 Alan Layec
5032 function [txt]=write_code_cdoit(flag)
5033     txt=[];
5034
5035     for j=1:ncord
5036         bk=cord(j,1);
5037         pt=cord(j,2);
5038         //** blk
5039         if funtyp(bk)>-1 then
5040             if or(bk==act) | or(bk==cap) then
5041                 if stalone then
5042                     txt2=call_block42(bk,pt,flag);
5043                     if txt2<>[] then
5044                         txt=[txt;
5045                         "    "+txt2
5046                         ""];
5047                     end
5048                 end
5049             else
5050                 txt2=call_block42(bk,pt,flag);
5051                 if txt2<>[] then
5052                     txt=[txt;
5053                     "    "+txt2
5054                     ""];
5055                 end
5056             end
5057             //** ifthenelse blk
5058         elseif funtyp(bk)==-1 then
5059             ix=-1+inplnk(inpptr(bk));
5060             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5061             thentxt=write_code_doit(clkptr(bk),flag);
5062             elsetxt=write_code_doit(clkptr(bk)+1,flag);
5063             if thentxt<>[] | elsetxt<>[] then
5064                 txt=[txt;
5065                 "    "+get_comment("ifthenelse_blk",list(bk));]
5066                 //** C **//
5067                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5068                 txt=[txt;
5069                 "    if("+tmp_+">0) {"]
5070                 //*******//
5071                 txt=[txt;
5072                 Indent+thentxt];
5073                 if elsetxt<>[] then
5074                     //** C **//
5075                     txt=[txt;
5076                     "    }";
5077                     "    else {";]
5078                     //*******//
5079                     txt=[txt;
5080                     Indent+elsetxt];
5081                 end
5082                 //** C **//
5083                 txt=[txt;
5084                 "    }"]
5085                 //*******//
5086             end
5087             //** eventselect blk
5088         elseif funtyp(bk)==-2 then
5089             Noutport=clkptr(bk+1)-clkptr(bk);
5090             ix=-1+inplnk(inpptr(bk));
5091             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5092             II=[];
5093             switchtxt=list()
5094             for i=1: Noutport
5095                 switchtxt(i)=write_code_doit(clkptr(bk)+i-1,flag);
5096                 if switchtxt(i)<>[] then II=[II i];end
5097             end
5098             if II<>[] then
5099                 txt=[txt;
5100                 "    "+get_comment("evtselect_blk",list(bk));]
5101                 //** C **//
5102                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5103                 txt=[txt;
5104                 "    i=max(min((int) "+...
5105                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"
5106                 "    switch(i)"
5107                 "    {"]
5108                 //*******//
5109                 for i=II
5110                     //** C **//
5111                     txt=[txt;
5112                     "     case "+string(i)+" :";]
5113                     //*******//
5114                     txt=[txt;
5115                     BigIndent+write_code_doit(clkptr(bk)+i-1,flag);]
5116                     //** C **//
5117                     txt=[txt;
5118                     BigIndent+"break;"]
5119                     //*******//
5120                 end
5121                 //** C **//
5122                 txt=[txt;
5123                 "    }"];
5124                 //*******//
5125             end
5126             //** Unknown block
5127         else
5128             error("Unknown block type "+string(bk));
5129         end
5130     end
5131
5132 endfunction
5133
5134 //write_code_doit : generate body of the code for
5135 //                  ordering calls of blocks during
5136 //                  flag 1,2 & flag 3
5137 //
5138 //input : ev  : evt number for block's call
5139 //       flag : flag number for block's call
5140 //
5141 //output : txt for flag 1 or 2, or flag 3
5142 //
5143 //12/07/07 Alan Layec
5144 function [txt]=write_code_doit(ev,flag)
5145     txt=[];
5146
5147     for j=ordptr(ev):ordptr(ev+1)-1
5148         bk=ordclk(j,1);
5149         pt=ordclk(j,2);
5150         //** blk
5151         if funtyp(bk)>-1 then
5152             if or(bk==act) | or(bk==cap) then
5153                 if stalone then
5154                     txt2=call_block42(bk,pt,flag);
5155                     if txt2<>[] then
5156                         txt=[txt;
5157                         "    "+txt2
5158                         ""];
5159                     end
5160                 end
5161             else
5162                 txt2=call_block42(bk,pt,flag);
5163                 if txt2<>[] then
5164                     txt=[txt;
5165                     "    "+txt2
5166                     ""];
5167                 end
5168             end
5169             //** ifthenelse blk
5170         elseif funtyp(bk)==-1 then
5171             ix=-1+inplnk(inpptr(bk));
5172             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5173             thentxt=write_code_doit(clkptr(bk),flag);
5174             elsetxt=write_code_doit(clkptr(bk)+1,flag);
5175             if thentxt<>[] | elsetxt<>[] then
5176                 txt=[txt;
5177                 "    "+get_comment("ifthenelse_blk",list(bk));]
5178                 //** C **//
5179                 tmp_ = "*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5180                 txt=[txt;
5181                 "    if("+tmp_+">0) {"]
5182                 //*******//
5183                 txt=[txt;
5184                 Indent+thentxt]
5185                 if elsetxt<>[] then
5186                     //** C **//
5187                     txt=[txt;
5188                     "    }";
5189                     "    else {";]
5190                     //*******//
5191                     txt=[txt;
5192                     Indent+elsetxt];
5193                 end
5194                 //** C **//
5195                 txt=[txt;
5196                 "    }"]
5197                 //*******//
5198             end
5199             //** eventselect blk
5200         elseif funtyp(bk)==-2 then
5201             Noutport=clkptr(bk+1)-clkptr(bk);
5202             ix=-1+inplnk(inpptr(bk));
5203             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5204             II=[];
5205             switchtxt=list()
5206             for i=1: Noutport
5207                 switchtxt(i)=write_code_doit(clkptr(bk)+i-1,flag);
5208                 if switchtxt(i)<>[] then II=[II i];end
5209             end
5210             if II<>[] then
5211                 txt=[txt;
5212                 "    "+get_comment("evtselect_blk",list(bk));]
5213                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5214                 //** C **//
5215                 txt=[txt;
5216                 "    i=max(min((int) "+...
5217                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"
5218                 "    switch(i)"
5219                 "    {"]
5220                 //*******//
5221                 for i=II
5222                     //** C **//
5223                     txt=[txt;
5224                     "     case "+string(i)+" :";]
5225                     //*******//
5226                     txt=[txt;
5227                     BigIndent+write_code_doit(clkptr(bk)+i-1,flag);]
5228                     //** C **//
5229                     txt=[txt;
5230                     BigIndent+"break;"]
5231                     //*******//
5232                 end
5233                 //** C **//
5234                 txt=[txt;
5235                 "    }"]
5236                 //*******//
5237             end
5238             //** Unknown block
5239         else
5240             error("Unknown block type "+string(bk));
5241         end
5242     end
5243
5244 endfunction
5245
5246 //write_code_idoit : generate body of the code for
5247 //                   ordering calls of initial
5248 //                   called blocks
5249 //
5250 //input : nothing (blocks are called with flag 1)
5251 //
5252 //output : txt for iord
5253 //
5254 //15/07/07 Alan Layec
5255 function [txt]=write_code_idoit()
5256     txt=[];
5257
5258     for j=1:niord
5259         bk=iord(j,1);
5260         pt=iord(j,2);
5261         //** blk
5262         if funtyp(bk)>-1 then
5263             if or(bk==act) then
5264                 if stalone then
5265                     txt2=call_block42(bk,pt,1);
5266                     if txt2<>[] then
5267                         txt=[txt;
5268                         "    "+txt2
5269                         ""];
5270                     end
5271                 end
5272             else
5273                 txt2=call_block42(bk,pt,1);
5274                 if txt2<>[] then
5275                     txt=[txt;
5276                     "    "+txt2
5277                     ""];
5278                 end
5279             end
5280             //** ifthenelse blk
5281         elseif funtyp(bk)==-1 then
5282             ix=-1+inplnk(inpptr(bk));
5283             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5284             thentxt=write_code_doit(clkptr(bk),1);
5285             elsetxt=write_code_doit(clkptr(bk)+1,1);
5286             if thentxt<>[] | elsetxt<>[] then
5287                 txt=[txt;
5288                 "    "+get_comment("ifthenelse_blk",list(bk));]
5289                 //** C **//
5290                 tmp_ = "*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5291                 txt=[txt;
5292                 "    if("+tmp_+">0) {"]
5293                 //*******//
5294                 txt=[txt;
5295                 Indent+thentxt];
5296                 if elsetxt<>[] then
5297                     //** C **//
5298                     txt=[txt;
5299                     "    }";
5300                     "    else {";]
5301                     //*******//
5302                     txt=[txt;
5303                     Indent+elsetxt];
5304                 end
5305                 //** C **//
5306                 txt=[txt;
5307                 "    }"]
5308                 //*******//
5309             end
5310             //** eventselect blk
5311         elseif funtyp(bk)==-2 then
5312             Noutport=clkptr(bk+1)-clkptr(bk);
5313             ix=-1+inplnk(inpptr(bk));
5314             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5315             II=[];
5316             switchtxt=list()
5317             for i=1: Noutport
5318                 switchtxt(i)=write_code_doit(clkptr(bk)+i-1,1);
5319                 if switchtxt(i)<>[] then II=[II i];end
5320             end
5321             if II<>[] then
5322                 txt=[txt;
5323                 "    "+get_comment("evtselect_blk",list(bk));]
5324                 //** C **//
5325                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5326                 txt=[txt;
5327                 "    i=max(min((int) "+...
5328                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"]
5329                 txt=[txt;
5330                 "    switch(i)"
5331                 "    {"]
5332                 //*******//
5333                 for i=II
5334                     //** C **//
5335                     txt=[txt;
5336                     "     case "+string(i)+" :";]
5337                     //*******//
5338                     txt=[txt;
5339                     BigIndent+write_code_doit(clkptr(bk)+i-1,1);]
5340                     //** C **//
5341                     txt=[txt;
5342                     BigIndent+"break;"]
5343                     //*******//
5344                 end
5345                 //** C **//
5346                 txt=[txt;
5347                 "    }"];
5348                 //*******//
5349             end
5350             //** Unknown block
5351         else
5352             error("Unknown block type "+string(bk));
5353         end
5354     end
5355
5356 endfunction
5357
5358 //write_code_odoit : generate body of the code for
5359 //                   ordering calls of blocks before
5360 //                   continuous time integration
5361 //
5362 //input : flag : flag number for block's call
5363 //
5364 //output : txt for flag 0
5365 //
5366 //12/07/07 Alan Layec
5367 function [txt]=write_code_odoit(flag)
5368     txt=[];
5369
5370     for j=1:noord
5371         bk=oord(j,1);
5372         pt=oord(j,2);
5373         //** blk
5374         if funtyp(bk)>-1 then
5375             txt2=call_block42(bk,pt,flag);
5376             if txt2<>[] then
5377                 txt=[txt;
5378                 "    "+txt2
5379                 ""];
5380             end
5381             //** ifthenelse blk
5382         elseif funtyp(bk)==-1 then
5383             ix=-1+inplnk(inpptr(bk));
5384             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5385             thentxt=write_code_ozdoit(clkptr(bk),flag);
5386             elsetxt=write_code_ozdoit(clkptr(bk)+1,flag);
5387             if thentxt<>[] | elsetxt<>[] then
5388                 txt=[txt;
5389                 "    "+get_comment("ifthenelse_blk",list(bk));]
5390                 //** C **//
5391                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5392                 txt=[txt;
5393                 "    if ((block_"+rdnom+"["+string(bk-1)+"].nmode<0"+...
5394                 " && "+tmp_+">0)"+...
5395                 " || \"
5396                 "        (block_"+rdnom+"["+string(bk-1)+"].nmode>0"+...
5397                 " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==1)) {"]
5398                 //*******//
5399                 txt=[txt;
5400                 Indent+thentxt]
5401                 //** C **//
5402                 txt=[txt;
5403                 "    }"];
5404                 //*******//
5405                 if elsetxt<>[] then
5406                     //** C **//
5407                     txt=[txt;
5408                     "    else if  ((block_"+rdnom+"["+string(bk-1)+"].nmode<0"+...
5409                     " && "+tmp_+"<=0)"+...
5410                     " || \"
5411                     "              (block_"+rdnom+"["+string(bk-1)+"].nmode>0"+...
5412                     " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==2)) {";]
5413                     //*******//
5414                     txt=[txt;
5415                     Indent+elsetxt]
5416                     //** C **//
5417                     txt=[txt;
5418                     "    }"];
5419                     //*******//
5420                 end
5421             end
5422             //** eventselect blk
5423         elseif funtyp(bk)==-2 then
5424             Noutport=clkptr(bk+1)-clkptr(bk);
5425             ix=-1+inplnk(inpptr(bk));
5426             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5427             II=[];
5428             switchtxt=list()
5429             for i=1: Noutport
5430                 switchtxt(i)=write_code_ozdoit(clkptr(bk)+i-1,flag);
5431                 if switchtxt(i)<>[] then II=[II i];end
5432             end
5433             if II<>[] then
5434                 txt=[txt;
5435                 "    "+get_comment("evtselect_blk",list(bk));]
5436                 //** C **//
5437                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5438                 txt=[txt;
5439                 "    if (block_"+rdnom+"["+string(bk-1)+"].nmode<0) {";
5440                 "      i=max(min((int) "+...
5441                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"
5442                 "    }"
5443                 "    else {"
5444                 "      i=block_"+rdnom+"["+string(bk-1)+"].mode[0];"
5445                 "    }"]
5446                 txt=[txt;
5447                 "    switch(i)"
5448                 "    {"];
5449                 //*******//
5450                 for i=II
5451                     //** C **//
5452                     txt=[txt;
5453                     "     case "+string(i)+" :";]
5454                     //*******//
5455                     txt=[txt;
5456                     BigIndent+write_code_ozdoit(clkptr(bk)+i-1,flag);]
5457                     //** C **//
5458                     txt=[txt;
5459                     BigIndent+"break;"]
5460                     //*******//
5461                 end
5462                 //** C **//
5463                 txt=[txt;
5464                 "    }"];
5465                 //*******//
5466             end
5467             //** Unknown block
5468         else
5469             error("Unknown block type "+string(bk));
5470         end
5471     end
5472
5473 endfunction
5474
5475 //write_code_ozdoit : generate body of the code for both
5476 //                    flag 0 & flag 9
5477 //
5478 //input: ev  : evt number for block's call
5479 //      flag : flag number for block's call
5480 //
5481 //output : txt for flag 0 or flag 9
5482 //
5483 //12/07/07 Alan Layec
5484 function [txt]=write_code_ozdoit(ev,flag)
5485     txt=[];
5486
5487     for j=ordptr(ev):ordptr(ev+1)-1
5488         bk=ordclk(j,1);
5489         pt=ordclk(j,2);
5490         //** blk
5491         if funtyp(bk)>-1 then
5492             if (or(bk==act) | or(bk==cap)) & (flag==1) then
5493                 if stalone then
5494                     txt=[txt;
5495                     "    "+call_block42(bk,pt,flag)
5496                     ""];
5497                 end
5498             else
5499                 txt2=call_block42(bk,pt,flag);
5500                 if txt2<>[] then
5501                     txt=[txt;
5502                     "    "+txt2
5503                     ""];
5504                 end
5505             end
5506             //** ifthenelse blk
5507         elseif funtyp(bk)==-1 then
5508             ix=-1+inplnk(inpptr(bk));
5509             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5510             thentxt=write_code_ozdoit(clkptr(bk),flag);
5511             elsetxt=write_code_ozdoit(clkptr(bk)+1,flag);
5512             if thentxt<>[] | elsetxt<>[] then
5513                 txt=[txt;
5514                 "    "+get_comment("ifthenelse_blk",list(bk));]
5515                 //** C **//
5516                 tmp_ = "*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5517                 txt=[txt;
5518                 "    if (((phase==1"+...
5519                 " || block_"+rdnom+"["+string(bk-1)+"].nmode==0)"+...
5520                 " && "+tmp_+">0)"+...
5521                 " || \"
5522                 "        ((phase!=1"+...
5523                 " && block_"+rdnom+"["+string(bk-1)+"].nmode!=0)"+...
5524                 " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==1)) {"]
5525                 //*******//
5526                 txt=[txt;
5527                 Indent+thentxt]
5528                 //** C **//
5529                 txt=[txt;
5530                 "    }"];
5531                 //*******//
5532                 if elsetxt<>[] then
5533                     //** C **//
5534                     txt=[txt;
5535                     "      else if (((phase==1"+...
5536                     " || block_"+rdnom+"["+string(bk-1)+"].nmode==0)"+...
5537                     " && "+tmp_+"<=0)"+...
5538                     " || \"
5539                     "               ((phase!=1"+...
5540                     " && block_"+rdnom+"["+string(bk-1)+"].nmode!=0)"+...
5541                     " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==2)) {";]
5542                     //*******//
5543                     txt=[txt;
5544                     Indent+elsetxt]
5545                     //** C **//
5546                     txt=[txt;
5547                     "    }"];
5548                     //*******//
5549                 end
5550             end
5551             //** eventselect blk
5552         elseif funtyp(bk)==-2 then
5553             Noutport=clkptr(bk+1)-clkptr(bk);
5554             ix=-1+inplnk(inpptr(bk));
5555             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5556             II=[];
5557             switchtxt=list()
5558             for i=1: Noutport
5559                 switchtxt(i)=write_code_ozdoit(clkptr(bk)+i-1,flag);
5560                 if switchtxt(i)<>[] then II=[II i];end
5561             end
5562             if II<>[] then
5563                 txt=[txt;
5564                 "    "+get_comment("evtselect_blk",list(bk));]
5565                 //** C **//
5566                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5567                 txt=[txt;
5568                 "    if (phase==1 || block_"+rdnom+"["+string(bk-1)+"].nmode==0) {";
5569                 "      i=max(min((int) "+...
5570                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"
5571                 "    }"
5572                 "    else {"
5573                 "      i=block_"+rdnom+"["+string(bk-1)+"].mode[0];"
5574                 "    }"]
5575                 txt=[txt;
5576                 "    switch(i)"
5577                 "    {"];
5578                 //*******//
5579                 for i=II
5580                     //** C **//
5581                     txt=[txt;
5582                     "     case "+string(i)+" :";]
5583                     //*******//
5584                     txt=[txt;
5585                     BigIndent+write_code_ozdoit(clkptr(bk)+i-1,flag);]
5586                     //** C **//
5587                     txt=[txt;
5588                     BigIndent+"break;"]
5589                     //*******//
5590                 end
5591                 //** C **//
5592                 txt=[txt;
5593                 "    }"];
5594                 //*******//
5595             end
5596             //** Unknown block
5597         else
5598             error("Unknown block type "+string(bk));
5599         end
5600     end
5601
5602 endfunction
5603
5604 //write_code_zdoit : generate body of the code for
5605 //                   ordering calls of blocks before
5606 //                   continuous time zero crossing
5607 //                   detection
5608 //
5609 //input : noting
5610 //
5611 //output : txt for flag 9
5612 //
5613 //12/07/07 Alan Layec
5614 function [txt]=write_code_zdoit()
5615     txt=[];
5616
5617     //** first pass (flag 1)
5618     for j=1:nzord
5619         bk=zord(j,1);
5620         pt=zord(j,2);
5621         //** blk
5622         if funtyp(bk)>-1 then
5623             if or(bk==act) | or(bk==cap) then
5624                 if stalone then
5625                     txt=[txt;
5626                     "    "+call_block42(bk,pt,1)
5627                     ""];
5628                 end
5629             else
5630                 txt2=call_block42(bk,pt,1);
5631                 if txt2<>[] then
5632                     txt=[txt;
5633                     "    "+txt2
5634                     ""];
5635                 end
5636             end
5637             //** ifthenelse blk
5638         elseif funtyp(bk)==-1 then
5639             ix=-1+inplnk(inpptr(bk));
5640             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5641             thentxt=write_code_ozdoit(clkptr(bk),1);
5642             elsetxt=write_code_ozdoit(clkptr(bk)+1,1);
5643             if thentxt<>[] | elsetxt<>[] then
5644                 txt=[txt;
5645                 "    "+get_comment("ifthenelse_blk",list(bk));]
5646                 //** C **//
5647                 tmp_ = "*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5648                 txt=[txt;
5649                 "    if (((phase==1"+...
5650                 " || block_"+rdnom+"["+string(bk-1)+"].nmode==0)"+...
5651                 " && "+tmp_+">0)"+...
5652                 " || \"
5653                 "        ((phase!=1"+...
5654                 " && block_"+rdnom+"["+string(bk-1)+"].nmode!=0)"+...
5655                 " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==1)) {"]
5656                 //*******//
5657                 txt=[txt;
5658                 Indent+thentxt]
5659                 //** C **//
5660                 txt=[txt;
5661                 "    }"];
5662                 //*******//
5663                 if elsetxt<>[] then
5664                     //** C **//
5665                     txt=[txt;
5666                     "      else if (((phase==1"+...
5667                     " || block_"+rdnom+"["+string(bk-1)+"].nmode==0)"+...
5668                     " && "+tmp_+"<=0)"+...
5669                     " || \"
5670                     "               ((phase!=1"+...
5671                     " && block_"+rdnom+"["+string(bk-1)+"].nmode!=0)"+...
5672                     " && block_"+rdnom+"["+string(bk-1)+"].mode[0]==2)) {";]
5673                     //*******//
5674                     txt=[txt;
5675                     Indent+elsetxt]
5676                     //** C **//
5677                     txt=[txt;
5678                     "    }"];
5679                     //*******//
5680                 end
5681             end
5682             //** eventselect blk
5683         elseif funtyp(bk)==-2 then
5684             Noutport=clkptr(bk+1)-clkptr(bk);
5685             ix=-1+inplnk(inpptr(bk));
5686             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5687             II=[];
5688             switchtxt=list()
5689             for i=1: Noutport
5690                 switchtxt(i)=write_code_ozdoit(clkptr(bk)+i-1,1);
5691                 if switchtxt(i)<>[] then II=[II i];end
5692             end
5693             if II<>[] then
5694                 txt=[txt;
5695                 "    "+get_comment("evtselect_blk",list(bk));]
5696                 //** C **//
5697                 tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5698                 txt=[txt;
5699                 "    if (phase==1 || block_"+rdnom+"["+string(bk-1)+"].nmode==0){";
5700                 "      i=max(min((int) "+...
5701                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].evout),1);"
5702                 "    else {"
5703                 "      i=block_"+rdnom+"["+string(bk-1)+"].mode[0];"
5704                 "    }"]
5705                 txt=[txt;
5706                 "      switch(i)"
5707                 "      {"];
5708                 //*******//
5709                 for i=II
5710                     //** C **//
5711                     txt=[txt;
5712                     "       case "+string(i)+" :";]
5713                     //*******//
5714                     txt=[txt;
5715                     BigIndent+write_code_ozdoit(clkptr(bk)+i-1,1);]
5716                     //** C **//
5717                     txt=[txt;
5718                     BigIndent+"break;"]
5719                     //*******//
5720                 end
5721                 //** C **//
5722                 txt=[txt;
5723                 "      }"];
5724                 //*******//
5725             end
5726             //** Unknown block
5727         else
5728             error("Unknown block type "+string(bk));
5729         end
5730     end
5731
5732     //** second pass (flag 9)
5733     for j=1:nzord
5734         bk=zord(j,1);
5735         pt=zord(j,2);
5736         //** blk
5737         if funtyp(bk)>-1 then
5738             if or(bk==act) | or(bk==cap) then
5739                 if stalone then
5740                     txt=[txt;
5741                     "    "+call_block42(bk,pt,9)
5742                     ""];
5743                 end
5744             else
5745                 txt2=call_block42(bk,pt,9);
5746                 if txt2<>[] then
5747                     txt=[txt;
5748                     "    "+txt2
5749                     ""];
5750                 end
5751             end
5752
5753             //** ifthenelse blk
5754         elseif funtyp(bk)==-1 then
5755             ix=-1+inplnk(inpptr(bk));
5756             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5757             //** C **//
5758             tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5759             //*******//
5760             thentxt=write_code_zzdoit(clkptr(bk),9);
5761             elsetxt=write_code_zzdoit(clkptr(bk)+1,9);
5762             txt=[txt;
5763             "    "+get_comment("ifthenelse_blk",list(bk));]
5764             //** C **//
5765             txt=[txt;
5766             "    g["+string(zcptr(bk)-1)+"]=(double)"+tmp_+";"]
5767             //*******//
5768             if thentxt<>[] | elsetxt<>[] then
5769                 //** C **//
5770                 txt=[txt;
5771                 "    if (g["+string(zcptr(bk)-1)+"] > 0.){"]
5772                 //*******//
5773                 txt=[txt;
5774                 Indent+thentxt]
5775                 //** C **//
5776                 txt=[txt;
5777                 "      }"]
5778                 //*******//
5779                 if elsetxt <> [] then
5780                     //** C **//
5781                     txt=[txt;
5782                     "      else {"]
5783                     //*******//
5784                     txt=[txt;
5785                     Indent+elsetxt]
5786                     //** C **//
5787                     txt=[txt;
5788                     "      }"]
5789                     //*******//
5790                 end
5791             end
5792             //** C **//
5793             txt=[txt;
5794             "    if(phase==1 && block_"+rdnom+"["+string(bk-1)+"].nmode > 0){"
5795             "      if (g["+string(zcptr(bk)-1)+"] > 0.){"
5796             "        block_"+rdnom+"["+string(bk-1)+"].mode[0] = 1;"
5797             "      }"
5798             "      else {"
5799             "        block_"+rdnom+"["+string(bk-1)+"].mode[0] = 2;"
5800             "      }"
5801             "    }"]
5802             //*******//
5803             //** eventselect blk
5804         elseif funtyp(bk)==-2 then
5805             Noutport=clkptr(bk+1)-clkptr(bk);
5806             ix=-1+inplnk(inpptr(bk));
5807             TYPE=mat2c_typ(outtb(ix+1));  //** scilab index start from 1
5808             //** C **//
5809             tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5810             //*******//
5811             II=[];
5812             switchtxt=list()
5813             for i=1:Noutport
5814                 switchtxt(i)=write_code_zzdoit(clkptr(bk)+i-1,9);
5815                 if switchtxt(i)<>[] then II=[II i];end
5816             end
5817             txt=[txt;
5818             "    "+get_comment("evtselect_blk",list(bk));]
5819             if II<>[] then
5820                 //** C **//
5821                 txt=[txt;
5822                 "    j=max(min((int) "+...
5823                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].nevout),1);"]
5824                 txt=[txt;
5825                 "    switch(j)"
5826                 "    {"];
5827                 //*******//
5828                 for i=II
5829                     //** C **//
5830                     txt=[txt;
5831                     "     case "+string(j)+" :";]
5832                     //*******//
5833                     txt=[txt;
5834                     BigIndent+write_code_zzdoit(clkptr(bk)+i-1,9);]
5835                     //** C **//
5836                     txt=[txt;
5837                     BigIndent+"break;"]
5838                     //*******//
5839                 end
5840                 //** C **//
5841                 txt=[txt;
5842                 "    }"];
5843                 //*******//
5844             end
5845             //** C **//
5846             txt=[txt;
5847             "  for (jj=0;jj<block_"+rdnom+"["+string(fun-1)+"].nevout-1;++jj) {"
5848             "    g["+string(zcptr(bk)-1)+"+jj]=(double)"+tmp_+"-(double)(jj+2);"
5849             "  }"
5850             "  if(phase==1 && block_"+rdnom+"["+string(bk-1)+"].nmode>0){"
5851             "    j=max(min((int) "+tmp_+","
5852             "              block_"+rdnom+"["+string(bk-1)+"].nevout),1);"
5853             "    block_"+rdnom+"["+string(bk-1)+"].mode[0]= j;"
5854             "  }"]
5855             //*******//
5856             //** Unknown block
5857         else
5858             error("Unknown block type "+string(bk));
5859         end
5860     end
5861
5862 endfunction
5863
5864 //write_code_zzdoit : generate body of the code for
5865 //                    flag 9
5866 //
5867 //input: ev  : evt number for block's call
5868 //      flag : flag number for block's call
5869 //
5870 //output : txt for flag 9
5871 //
5872 //12/07/07 Alan Layec
5873 function [txt]=write_code_zzdoit(ev,flag)
5874     txt=[];
5875
5876     for j=ordptr(ev):ordptr(ev+1)-1
5877         bk=ordclk(j,1);
5878         pt=ordclk(j,2);
5879         //** blk
5880         if funtyp(bk)>-1 then
5881             if or(bk==act) | or(bk==cap) then
5882                 if stalone then
5883                     txt=[txt;
5884                     "    "+call_block42(bk,pt,flag)
5885                     ""];
5886                 end
5887             else
5888                 txt2=call_block42(bk,pt,flag);
5889                 if txt2<>[] then
5890                     txt=[txt;
5891                     "    "+txt2
5892                     ""];
5893                 end
5894             end
5895             //** ifthenelse blk
5896         elseif funtyp(bk)==-1 then
5897             ix=-1+inplnk(inpptr(bk));
5898             TYPE=mat2c_typ(outtb(ix+1)); //** scilab index start from 1
5899             //** C **//
5900             tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5901             //*******//
5902             thentxt=write_code_zzdoit(clkptr(bk),9);
5903             elsetxt=write_code_zzdoit(clkptr(bk)+1,9);
5904             txt=[txt;
5905             "    "+get_comment("ifthenelse_blk",list(bk));]
5906             //** C **//
5907             txt=[txt;
5908             "    g["+string(zcptr(bk)-1)+"]=(double)"+tmp_+";"]
5909             //*******//
5910             if thentxt<>[] | elsetxt<>[] then
5911                 //** C **//
5912                 txt=[txt;
5913                 "    if (g["+string(zcptr(bk)-1)+"] > 0.){"]
5914                 //*******//
5915                 txt=[txt;
5916                 Indent+thentxt]
5917                 //** C **//
5918                 txt=[txt;
5919                 "      }"]
5920                 //*******//
5921                 if elsetxt <> [] then
5922                     //** C **//
5923                     txt=[txt;
5924                     "      else {"]
5925                     //*******//
5926                     txt=[txt;
5927                     Indent+elsetxt]
5928                     //** C **//
5929                     txt=[txt;
5930                     "      }"]
5931                     //*******//
5932                 end
5933             end
5934             //** C **//
5935             txt=[txt;
5936             "    if(phase==1 && block_"+rdnom+"["+string(bk-1)+"].nmode > 0){"
5937             "      if (g["+string(zcptr(bk)-1)+"] > 0.){"
5938             "        block_"+rdnom+"["+string(bk-1)+"].mode[0] = 1;"
5939             "      }"
5940             "      else {"
5941             "        block_"+rdnom+"["+string(bk-1)+"].mode[0] = 2;"
5942             "      }"
5943             "    }"]
5944             //*******//
5945
5946             //** eventselect blk
5947         elseif funtyp(bk)==-2 then
5948             Noutport=clkptr(bk+1)-clkptr(bk);
5949             ix=-1+inplnk(inpptr(bk));
5950             TYPE=mat2c_typ(outtb(ix+1));  //** scilab index start from 1
5951             //** C **//
5952             tmp_="*(("+TYPE+" *)"+rdnom+"_block_outtbptr["+string(ix)+"])"
5953             //*******//
5954             II=[];
5955             switchtxt=list()
5956             for i=1:Noutport
5957                 switchtxt(i)=write_code_zzdoit(clkptr(bk)+i-1,9);
5958                 if switchtxt(i)<>[] then II=[II i];end
5959             end
5960             txt=[txt;
5961             "    "+get_comment("evtselect_blk",list(bk));]
5962             if II<>[] then
5963                 //** C **//
5964                 txt=[txt;
5965                 "    j=max(min((int) "+...
5966                 tmp_+",block_"+rdnom+"["+string(bk-1)+"].nevout),1);"]
5967                 txt=[txt;
5968                 "    switch(j)"
5969                 "    {"];
5970                 //*******//
5971                 for i=II
5972                     //** C **//
5973                     txt=[txt;
5974                     "     case "+string(j)+" :";]
5975                     //*******//
5976                     txt=[txt;
5977                     BigIndent+write_code_zzdoit(clkptr(bk)+i-1,9);]
5978                     //** C **//
5979                     txt=[txt;
5980                     BigIndent+"break;"]
5981                     //*******//
5982                 end
5983                 //** C **//
5984                 txt=[txt;
5985                 "    }"];
5986                 //*******//
5987             end
5988             //** C **//
5989             txt=[txt;
5990             "  for (jj=0;jj<block_"+rdnom+"["+string(fun-1)+"].nevout-1;++jj) {"
5991             "    g["+string(zcptr(bk)-1)+"+jj]=(double)"+tmp_+"-(double)(jj+2);"
5992             "  }"
5993             "  if(phase==1 && block_"+rdnom+"["+string(bk-1)+"].nmode>0){"
5994             "    j=max(min((int) "+tmp_+","
5995             "              block_"+rdnom+"["+string(bk-1)+"].nevout),1);"
5996     &