!14/05
!
!    input & output to/from files
!
%endoflist

%conststring(255)%array sides(topp:bottom)= "TOP    ",
                                            "RIGHT  ",
                                            "LEFT   ",
                                            "BOTTOM "


!______________________________________________________________________
%string(255)%fn GET INPUT(%string(255) prompt)
%integer a,
         count=0,
         colour
%string(255) file= "",
             s
CHANGE TOGGLE (no,no,no,prompt)
selectinput(0)
SET TERMINAL MODE(noecho)

a= testsymbol %until a= -1

%cycle
a = testsymbol
%if a= 10 %or a= 13 %then %exit   
  colour= yellow
  %if a# -1 %start
      %if a# 127 %start
        count= count + 1
        s= tostring(a)
        file= file.s
      %finish %else %if count# 0 %start
         s= substring(file,count,count)
         file= substring(file,1,count-1) 
        colour= black
      %finish
     DISPLAY (s,8*count + mouse display x + 60 ,screen y + 10,colour)
     %if colour= black %then count= count-1
  %finish
%repeat
change toggle(no,no,no,"")
%result= file
%end

!________________________________________________________________________

%routine PROCESS FILE(%integer mode)
%integer flipscreen= screen top
%onevent(9) %start
 OUT(" ....file not found !")
 %return
%finish

%ownstring(255)s,
               file
%owninteger count= 1,
            colour,
                a
%own %integer max x= 0,
              max y= 0,
              min x= 0,
              min y= 0

!____________________________________________________________________

%routine GET CELLS
!
!%integer value,
!         j,
!         i= 0,
!         old cells= no
!%string(255) s
!
!  %on %event(9) %start
!     printstring("    ....... Cell does not exist !
!")
!     %stop
!  %finish
!
!clear frame
!select output(0)
!select input(1)
!
!%if file# "" %start
!  %for i=1,1,5 %cycle
!      readstring(s)
!  %repeat
!  read(cell size x)
!  readstring(s)
!  readstring(s)
!  read(cell size y)
!  readstring(s)
!  readstring(s)
!  old cells= no
!%finish %else old cells= yes %and select input(0) 
!
!printstring("
!SUB CELLS
!~~~~~~~~~
!
!The following sub cells are used in this design.
!  Please add to this list any new cells required.
!(terminate with '*')
!
!
!")
!i=1
!%if file= ".n" %or file= ".N" %then printstring("   ] ")
!%cycle
!  readstring(cell name(i))
!  printstring("   ] ")
!  %if cell name(i)= "*" %start
!     cell name(i)= ""
!     %if old cells= no  %start
!         old cells= yes
!         select input(0)
!     %finish %else %exit
!  %finish
!
!   %if old cells= no %start
!    printstring(":")
!    printstring(cellname(i))
!    readstring(s)
!    readstring(s)
!    READ(cell x(i))
!    readstring(s)
!    readstring(s)
!    READ(cell y(i))
!    printstring("
!")
!    i=i+1
!   %finish %else %if cell name(i)# "" %start
!    open input(2,cellname(i))
!    reset input
!    select input(2)
!    %for j=1,1,5 %cycle
!      readstring(s)
!    %repeat
!    READ(value)
!    cell x(i)= REGULARISE(value)
!    readstring(s)
!    readstring(s)
!    READ(value)
!    cell y(i)= REGULARISE(value)
!    select input(0)
!    i=i+1
!   %finish
! 
!%repeat
%end

!_________________________________________________________________________

%routine OUTPUT NODES(%string(255) file)
%record(NODE)%name nodeptr,edge
%record(NODELIST)%name list
%integer i,pass,other,this type 
%string(12) s
%string(255) type

   %routine WHERE(%integer x,y)
    printsymbol('(')
    write(x,0)
    write(y,1)
    printsymbol(')')
   %end


SPLIT AND JOIN(yes)
open output(1,file)
reset output
select output(1)

printstring("START Cell  ")
printstring(file)
!printstring("
!     size x= ")
!write(max x-min x,3)
!printstring("
!     size y= ")      
!write(max y-min y,3)
!
!printstring("
!
!Cells used:
!
!")
!i=1
!%while cellname(i)# "" %cycle
!  printstring(cellname(i))
!  printstring("
!     side x= ")
!  write(cell x(i),0)
!  printstring("
!     side y= ")
!  write(cell y(i),0)
!  newline
!i=i+1
!%repeat
!printstring("*")

  newlines(2)
  %for i= topp,1,bottom %cycle
    list== port list(i)_next
       printstring(sides(i)."PORTs ")
       write(port side(i),3)
       newline
    %while list## nil %cycle
     %if i= topp %or i= bottom %then other= list_node_x %else %c
                                     other= list_node_y
     %if list_rx# 0 %then %c
         printstring(port name(list_rx)." at ".itos(other,0)."
")
     list== list_next
    %repeat
    printstring(".
")
  %repeat

%for pass= 1,1,2 %cycle
%for this type= poly,1,metal %cycle
edge== screens(xdir)_ptr_next y
%while edge## right edge %cycle
  nodeptr== edge_next x
  
 %while nodeptr## nil %cycle
   %if nodeptr_class# notional %and nodeptr_type# edges %start

  %if CPDE <= nodeptr_type <= CPDS %or CPM <= nodeptr_type <= CDM %then %c
              nodeptr_type= contact
  %if PUE <= nodeptr_type <= PUN %then nodeptr_type= pull up

    %if nodeptr_class= cell %start
              newline
              WHERE(nodeptr_x,nodeptr_y)
              printstring("  ->  cell: ")
              printstring(cell name(nodeptr_type))
              printstring("  rotation= ")
              write(nodeptr_rot,3)
              printstring(" degrees")
   %finish %else %if nodeptr_class= device %and pass= 1 %and this type= poly %start
              newline
              WHERE(nodeptr_x,nodeptr_y)
              printstring("  ->  device: ")
              printstring(devices(nodeptr_type))
              printstring(" ratio")
              write(nodeptr_ratio,3)
   %finish %else %if nodeptr_class= horiz wire %and pass= 2 %start
          %if nodeptr_type= this type %start
              newline
              WHERE(nodeptr_x,nodeptr_y)
              printstring("  <- ")
              printstring("horizontal ")
              printstring(wires(nodeptr_type))
              printstring("  Wire  ->  ") 
              WHERE(nodeptr_coord3,nodeptr_y)
              printstring(" width")
              write(nodeptr_width,3)
          %finish
   %finish %else %if nodeptr_class=vert wire %and pass= 2 %start
          %if nodeptr_type= this type %start
              newline
              WHERE(nodeptr_x,nodeptr_y)
              printstring("  <- ")
              printstring("vertical ")
              printstring(wires(nodeptr_type))
              printstring("  Wire  ->  ")
              WHERE(nodeptr_x,nodeptr_coord3)
              printstring(" width")
              write(nodeptr_width,3)
          %finish
   %finish 
  %finish
  nodeptr== nodeptr_next x
 %repeat
 edge== edge_next y
%repeat
%repeat
%repeat
printstring("
END Cell ")
printstring(file)
newlines(3)
close output
select output(0)
select input(0)
%end

!______________________________________________________________________

%routine INPUT NODES(%string(255) file)
%const %integer rcell=  switch 1
%const %integer rwire=  switch 2
%const %integer rdevice=switch 3
%integer x,y,ratio
%byte  type= 0,j, function
%integer len x,len y
%integer dump,i= 0
%string(255) s
%integerarray port pos(1:30,xdir:ydir)
%integerarray port on(1:30)
%string(255)%array port name(1:30)
%integer port count= 1


!__________________________________________________
%routine readstrings(%integer num)
%integer i,dump
%for i= 1,1,num %cycle
  read string(s)
%repeat
%end


%if file# "" %start
openinput(1,file)
select input(1)
reset input
readstrings(3)
readsymbol(dump)
%for i= topp,1,bottom %cycle
 readstrings(2)
 read(portside(i))
 %cycle
  readstring(s)
  %if s= "." %then %exit
     portname(portcount)= s
     readstring(s)
     %if i= topp %or i= bottom %start
          read(port pos(portcount,Xdir))
          port pos(portcount,Ydir)= port side(i)
     %else
          read(port pos(portcount,Ydir))
          port pos(portcount,Xdir)= port side(i)
     %finish
     port on(port count)= i
     portcount= portcount + 1
 %repeat
%repeat
readsymbol(dump)

origin x=0
origin y=0
toggle= 1
%cycle 
  settings= 0
  readsymbol(dump)
  readsymbol(dump)
  %if dump= 'E' %then close input %and %exit
  read(x)
  read(y)
  readsymbol(dump)
  readstrings(2)
   %if s= "device:" %start
     function= rdevice
     readstring(s)
     %for j=0,1,4 %cycle
      %if devices(j)= s %then type= j %and %exit
     %repeat
   %finish %else %if s= "cell:" %start
     function= rcell
     settings_cell= yes
     readstring(s)
     %for i=1,1,20 %cycle
      %if cellname(i)= s %then current cell= i
     %repeat
     readstring(s)
     read(current rot)
     ROTATION(len x,len y,current rot,current cell)
     x= x+len x//2
     y= y+len y//2
     readstring(s)
   %finish %else %if s= "horizontal"  %or s= "vertical"  %start
     function= rwire
     readstring(s)
     %for j= 0,1,4 %cycle
       %if wires(j)= s %then type= j %and %exit
     %repeat
     readstrings(2)
     %for i=1,1,3 %cycle    
       readsymbol(dump)
     %repeat
     settings_start= fileload
     settings== SWITCH OP(function,type,x,y,x,y,settings)
     read(trace x)
     read(trace y)
     readstring(s)
   %finish
   readstring(s)
   read(settings_ratio)
   settings_start= fileload
   settings== SWITCH OP(function,type,x,y,x,y,settings)

   %for i=1,1,port count-1 %cycle
     %if (x=port pos(i,Xdir) %and y= port pos(i,Ydir)) %or %c
         (trace x= port pos(i,Xdir) %and trace y= port pos(i,Ydir)) %start
         ADD PORT(port on(i),settings_node,port name(i))
     %finish
   %repeat
%repeat
%finish
settings_start= no
close input
%end

!______________________________________________________________________

%routine OUTPUT PDF(%string(255) file)
%constinteger edred=  4,
              edgreen=3,
              edblue= 2,
              edblack=1
%integer max x= 0,
         max y= 0
%record(NODE)%name edge== screens(Xdir)_ptr_next y,
                   nodde

   %integer %function  CONVERT(%integer colour)
    %if colour= red %then %result= edred %else %c
    %if colour= green %then %result= edgreen %else %c
    %if colour= blue  %then %result= edblue
    %result= edblack
   %end

   %routine DRAW DEVICE(%record(NODE)%name node)
   %integer x1,x2,y1,y2
   %switch device(contact:pass tran)
   NEW COLOUR(edblack)

-> device(node_type)

      device(pull up):
                   y1= node_y+9
                   MOVE ABS(node_x,y1)
                   LINE ABS(node_x-12,node_y-9)
                   LINE ABS(node_x+12,node_y-9)
                   LINE ABS(node_x,y1)
                         ->outtt
      device(pull down):
                   y1= node_Y-9
                   MOVE ABS(node_x,y1)
                   LINE ABS(node_x-12,node_y+9)
                   LINE ABS(node_x+12,node_y+9)
                   LINE ABS(node_x,y1)
                         ->outtt
      device(contact): 
                   MOVE ABS(node_x,node_y)
                   ED CIRCLE(9)
                         ->outtt
      device(pass tran):
                   x1= node_x-9
                   x2= node_x+9
                   y1= node_y-9
                   y2= node_y+9
                   MOVE ABS(x1,y1)
                   LINE ABS(x1,y2)
                   LINE ABS(x2,y2)
                   LINE ABS(x2,y1)
                   LINE ABS(x1,y1)
                         ->outtt
                  outtt:
   %end


   %routine DRAW CELL(%record(NODE)%name node)
   %integer lenx,len y,mid x,mid y
   NEW COLOUR(edblack)
   ROTATION(len x,len y,node_rot,node_type)
   mid x= node_x + len x//2
   mid y= node_y + len y//2
   len x= node_x + len x
   len y= node_y + len y
   SET CHAR ROT(node_rot)
   LINE ABS(node_x,len y)
   LINE ABS(len x,len y)
   LINE ABS(len x,node_y)
   LINE ABS(node_x,node_y)
   MOVE ABS(mid x,mid y)
   TEXT(cellname(node_type))
 %end

open output(2,file.".pdf")
reset output
select output(2)
initialise for(default device)
SPLIT AND JOIN(yes)
VIEW OFF
STORE ON(2)
CLIP OFF
SET ARC POINTS(20)
!VIEWPORT(0,512,0,512)
WINDOW(port side(left),port side(right),port side(bottom),port side(topp)+40)
SET CHAR SIZE(20)
%while edge## right edge %cycle
  nodde== edge_next x
  %while nodde## nil %cycle
   NEW COLOUR(convert(nodde_type))
   %if nodde_y > max y %then max y= nodde_y
   %if nodde_x > max x %then max x= nodde_x
   MOVE ABS(nodde_x,nodde_y)
   %if nodde_class= horiz wire %start
             LINE ABS(nodde_coord3,nodde_y) 
             %if nodde_coord 3 > max x %then max x= nodde_coord3
   %finish %else %c
   %if nodde_class= vert wire  %start
             LINE ABS(nodde_x,nodde_coord3) 
             %if nodde_coord3 > max y %then max y= nodde_coord3
   %finish %else %c
   %if nodde_class= device %then DRAW DEVICE(nodde) %else %c
   %if nodde_class= cell   %then DRAW CELL(nodde)
   nodde== nodde_next x
  %repeat
  edge== edge_next y
%repeat
MOVE ABS(port side(left),port side(topp)+10)
NEW COLOUR(edblack)
LINE ABS(port side(right),port side(topp)+10)
NEW COLOUR(edgreen)
MOVE ABS(port side(left) +10,port side(topp)+20)
SET CHAR ROT(0)
TEXT("CELL>>")
NEW COLOUR(edblue)
TEXT(file)
TERMINATE EDWIN
close output
%end

!----------{start process file}-----------------

%if mode= finput %then %c
    file= GET INPUT("Input")  %else %c
    file= GET INPUT("Output")
a= testsymbol %until a= -1
%if mode= finput %start
  OUT("Reading ".file)
  INPUT NODES(file)
  DRAW(screen top)
  offset(0,-screen top)
  DRAW(0)
  offset(0,0)
%else %if mode= foutput
 OUT("Writing ".file)
 OUTPUT NODES(file)
%finish
CHANGE TOGGLE(no,no,no,"")
%end


%list
%endoffile

