Interfacing function
eng


ADDER_c

File content


function [x,y,typ]=ADDER_c(job,arg1,arg2)
//Copyright INRIA
  x=[]
  y=[]
  typ=[]

  select job
    case 'plot' then
      standard_draw(arg1,%f,adder_draw_ports)

    case 'getinputs' then
      [x,y,typ]=adder_inputs(arg1)

    case 'getoutputs' then
      [x,y,typ]=standard_outputs(arg1)

    case 'getorigin' then
      [x,y]=standard_origin(arg1)

    case 'set' then
      x=arg1;
      graphics=arg1.graphics;
      model=arg1.model;
      exprs=graphics.exprs;

      while %t do
        //## define dialog box
        ttitle=['Set Digital Adder Block Parameters';
                ''
                'Number of bits :'
                ' The number of bits of the adder'
                ''
                'Output type :'
                ' Set the ouput data type'
                '  1      - double'
                '  3/4/5 - int32/int16/int8'
                '  6/7/8 - uint32/uint16/uint8'
                '']
        [ok,nbit,outtype,exprs]=getvalue(ttitle,...
             ['Number of bits';'Output type'],...
             list('vec',1,'vec',1),exprs)

        if ~ok then break,end;

        //## check parameters
        if nbit<=0 | nbit>32 then
          message('Number of bits must be positive and less or equal to 32 bits.')
          ok = %f
        else
          if find(outtype==[1 3 4 5 6 7 8])==[] then
            message('Output data type must be double/int32/int16/int8/uint32/uint16 or uint8.')
            ok = %f
          end
        end

        //## set model and graphics
        if ok then
          [model,graphics,ok]=set_io(model,graphics,...
                                     list([-1 -2;-1 -2],[-1 -1]),...
                                     list([-1 -2;-1 -2],[outtype outtype]),[],[])
          model.ipar=nbit
      model.blocktype='c'
          x.model=model
          graphics.exprs=exprs
          x.graphics=graphics;
          break;
        end
      end
    case 'define' then
      model=scicos_model()
      //## define regular input ports
      model.in=[-1;-1]
      model.in2=[-2;-2]
      model.intyp=[-1;-1]
      //## define regular output ports
      model.out=[-1;-1]
      model.out2=[-2;-2]
      model.outtyp=[3;3]
      //## define integer parameter
      model.ipar=8
      //## define computational function
      model.sim=list('adder_c',4)
      model.blocktype='c'
      model.dep_ut=[%t %f]
      //## define default parameters values
      exprs=[string(8);string(3)]
      //## define look of block
      gr_i=['o1=orig(1)'
            'o2=orig(2)'
            's1=sz(1)'
            's2=sz(2)'
            'phi=acos((s1)/(sqrt((s2/6)^2+(s1)^2)))'
            'yh=tan(phi)*(1/3*s1)'
            'if orient then'
            '  x=[o1;o1+s1;o1+s1;o1;o1;o1+(1/3)*s1;o1+(1/3)*s1;o1]'
            'else'
            '  x=[o1+s1;o1;o1;o1+s1;o1+s1;o1+s1-(1/3)*s1;o1+s1-(1/3)*s1;o1+s1]'
            'end'
            'y=[o2;o2+(1/6)*s2;o2+(5/6)*s2;o2+s2;'+...
                 'o2+s2-(2/6)*s2;o2+s2-(2/6)*s2-yh;o2+s2-(4/6)*s2+yh;o2+s2-(4/6)*s2]'
            'xpoly(x,y,""lines"",1)'
            'e=gce()'
            'e.line_mode=""on""'
            'e.thickness=2'
            'e.fill_mode=""on""'
            'dx=s1/3'
            'dy=s2/6'
            'if orient then'
            '  x=[o1+0.5*s1;o1+0.5*s1+dx]'
            'else'
            '  x=[o1+0.5*s1/3;o1+0.5*s1/3+dx]'
            'end'
            'y=[o2+1/2*s2;o2+1/2*s2]'
            'xpoly(x,y,""lines"")'
            'e=gce()'
            'e.thickness=2'
            'if orient then'
            '  x=[o1+2/3*s1;o1+2/3*s1]'
            'else'
            '  x=[o1+1/3*s1;o1+1/3*s1]'
            'end'
            'y=[o2+2.5/6*s2;o2+2.5/6*s2+dy]'
            'xpoly(x,y,""lines"")'
            'e=gce()'
            'e.thickness=2']
      x=standard_define([1.5 3],model,exprs,gr_i)
  end
endfunction

//## function to draw inputs ports
function adder_draw_ports(o)
//Copyright INRIA
  [orig,sz,orient]=(o.graphics.orig,o.graphics.sz,o.graphics.flip)

  if orient then //standard orientation
    //set port shape
    out1=[ 0  -1
           1   0
           0   1
           0  -1]*diag([xf/7,yf/14])

    in1= [-1  -1
           0   0
          -1   1
          -1  -1]*diag([xf/7,yf/14])

    //draw outputs
    dy=sz(2)/(2+1)
    xfpoly(out1(:,1)+ones(4,1)*(orig(1)+sz(1)), out1(:,2)+ones(4,1)*(orig(2)+sz(2)-dy), 1)
    xfpoly(out1(:,1)+ones(4,1)*(orig(1)+sz(1)), out1(:,2)+ones(4,1)*(orig(2)+sz(2)-dy*2), 1)

    //draw inputs
    xfpoly(in1(:,1)+ones(4,1)*orig(1), in1(:,2)+ones(4,1)*(orig(2)+sz(2)-(1/6)*sz(2)),1)
    xfpoly(in1(:,1)+ones(4,1)*orig(1), in1(:,2)+ones(4,1)*(orig(2)+sz(2)-(5/6)*sz(2)),1)
  else //tilded orientation
    //set port shape
    out1=[0  -1
         -1   0
          0   1
          0  -1]*diag([xf/7,yf/14])

    in1= [1  -1
          0   0
          1   1
          1  -1]*diag([xf/7,yf/14])

    //draw outputs
    dy=sz(2)/(2+1)
    xfpoly(out1(:,1)+ones(4,1)*orig(1)-1, out1(:,2)+ones(4,1)*(orig(2)+sz(2)-dy),1)
    xfpoly(out1(:,1)+ones(4,1)*orig(1)-1, out1(:,2)+ones(4,1)*(orig(2)+sz(2)-dy*2),1)

    //draw inputs
    xfpoly(in1(:,1)+ones(4,1)*(orig(1)+sz(1))+1, in1(:,2)+ones(4,1)*(orig(2)+sz(2)-(1/6)*sz(2)),1)
    xfpoly(in1(:,1)+ones(4,1)*(orig(1)+sz(1))+1, in1(:,2)+ones(4,1)*(orig(2)+sz(2)-(5/6)*sz(2)),1)
  end
endfunction

//## function to define position of inputs ports
function [x,y,typ]=adder_inputs(o)
//Copyright INRIA
  xf=60
  yf=40

  [orig,sz,orient]=(o.graphics.orig,o.graphics.sz,o.graphics.flip)

  if orient then
    xo=orig(1)
    dx=-xf/7
  else
    xo=orig(1)+sz(1)
    dx=yf/7
  end

  y=orig(2)+sz(2)-(sz(2)*[1/6 5/6])
  x=(xo+dx)*ones(y)
  typ=[1 1]
endfunction