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