! ILAP stack generator
! improved by DJR 20/12/85

%include "ilap:nmos.inc"

{#########################################################################}
{#                                                                       #}
{#      This program is part of the ILAP library, and was written in     #}
{#   The Department of Computer Science at the University of Edinburgh   #}
{#       (James Clerk Maxwell Building, Kings Buildings, Edinburgh)      #}
{#                                                                       #}
{#   This software is available free to other educational establisments  #}
{#   but the University of Edinburgh, retains all commercial rights.     #}
{#   It is a condition of having this software is that the sources are   #}
{#   not passed on to any other site, and that Edinburgh University is   #}
{#   given credit in any re-implementations of any of the algorithms     #}
{#   used, or articles published which refer to the software.            #}
{#                                                                       #}
{#   There is no formal support for this software, but any bugs should   #}
{#   be reported to Ken Chisholm or David Rees at the above address,     #}
{#   and these are likely to be fixed in a future release.               #}
{#                                                                       #}
{#########################################################################}

%externalroutine stack %alias "ILAP_STACK" (%string(31) name,
   %integer bits,words,
   %integer %name controlx, controly, pushcony, popcony,
   phi1x, phi2x, phi3x, phi4x,
   %integer %array %name pushy,
   %integer %array %name popy)
%integer i,j,curx,cury,ewords

  %if name="" %or bits<=0 %or words<=0 %then %start
    select output(0)
    print string("Stack Generator : invalid parameter
")
    %return
  %finish

  %if symbol exists("stackcell")=true %then ->skip
    %include "stackcells"
skip:! all symbols defined

  phi1x=4
  phi2x=13
  phi3x=60
  phi4x=77
  controlx=sx("stackcont")
  controly=sy("stackcont")

  symbol(name)
    curx=0
    %if words=1 %then ewords=2 %else ewords=words
    %for i=1,1,ewords %cycle
      cury=0
      draw("stackvdd",curx,cury)
      cury=cury+sy("stackvdd")
      %for j=1,1,bits %cycle
        %if i<=words %then draw("stackcell",curx,cury) %c
                     %else draw("stackcell0",curx,cury)
        popy(j)=cury+2
        cury=cury+sy("stackcell")-1    {to overlap}
        pushy(j)=cury-3
      %repeat
      cury=cury+1            {not to overlap}
      %if i=1 %then %start
        %if words=1 %then %start
          draw("stackcon1",curx,cury)
          cury=cury+sy("stackcon1")
        %finish %else %start
          %if words=2 %then draw("stackcon2",curx,cury) %c
                      %else draw("stackconn",curx,cury)
          cury=cury+sy("stackconn")
        %finish
        draw("stackcont",curx,cury)
        cury=cury+sy("stackcont")
      %finish %else %c
      %if i>2 %then %start
        %if i=words %then draw("stackconl",curx,cury) %c
                    %else draw("stackcon",curx,cury)
      %finish
      curx=curx+sx("stackvdd")-4    {to overlap}
    %repeat
  endsymbol
  popcony=sy(name)-2
  pushcony=popcony-5
%end
%endoffile
