'Program: ConvertFd - created Aug 9, 1985
'This program converts .fd files, like 'graphics_lib.fd' to
' .bmap format files, like 'graphics.bmap', so BASIC
' programs can access libraries of machine language routines
' by name via the LIBRARY statement.
'
' Modified  01/86 by Carolyn Scheppner  CBM
'   Prepends an x to all function names which
'    conflict with AmigaBasic keywords.
'    See data statements at end of program for
'    known conflicts.  To call these functions,
'    prepend an x  (example  xRead).
'   Saves the .bmap file in current or specified
'    directory (previously saved in LIBS:).
'    For your program to access the .bmap via
'    LIBRARY, it must be in the current dir
'    or the LIBS: dir.
'   The name of a .bmap file must match its
'    library's name.  The libraryname is the
'    part of the .fd file name before the _.
'    (example   dos.bmap from dos_lib.fd)


  DEFINT a-Z    'by default, all variables ares integer

  REM ******** for conflicting tokens ********
  READ cnt       'count of conflicting tokens
  DIM con$(cnt)
  FOR k = 0 TO cnt-1: READ con$(k): NEXT
  REM ****************************************

  INPUT "Enter name of .fd file to read > ",fdFilename$
  OPEN fdFilename$ FOR INPUT AS #1
  INPUT "Enter name of .bmap file to produce > ",bmapFilename$
  OPEN bmapFilename$ FOR OUTPUT AS #2
  WHILE NOT EOF(1)
    GetLine
    IF char$ = "#" THEN
      'lines which begin with "#" are command lines
      GOSUB GotCommand
    ELSEIF char$ = "*" THEN
      'lines which begin with "*" are comment lines
    ELSEIF char$ = CHR$(10) OR char$ = CHR$(13) THEN
      'blank line
    ELSE
      'all other lines define a function in the library
      GOSUB GotFunction
    END IF
  WEND
  CLOSE
  END

GotCommand:
  GetChar  'skip 1st "#"
  GetChar  'skip 2nd "#"
  GetToken
  IF token$ = "bias" THEN
    GetNum
    offset = -num
  END IF
 ram: RETURN

GotFunction:
  GetToken  'token$=function's name

  REM **** prepend conflicting tokens with 'x' ****
  k$ = token$
  FOR k = 0 TO cnt-1
     IF k$ = con$(k) THEN token$ = "x" + token$ 
  NEXT   
  REM **********************************************

  funcOffset=offset
  offset=offset-6
  parms$=""
  SkipTill "(": IF char$="" THEN BadFileFormat
  SkipTill ")": IF char$="" THEN BadFileFormat
  GetChar
  IF char$<>"" THEN
    SkipTill "(": IF char$="" THEN BadFileFormat
    WHILE char$ <> ")"
      GetChar 'skip ( or , or /
      IF char$<>")" THEN
        GOSUB GetRegister
        IF register=0 THEN BadFileFormat
        IF register=-1 THEN
          PRINT "Warning: Function ";token$;" not included because it"
          PRINT " needs a parameter passed in a register BASIC cannot"
          PRINT " conform to."
          PRINT
          RETURN
        END IF
        parms$ = parms$+CHR$(register)
         'tells BASIC which register to put this parm into
      END IF
    WEND
  END IF
  AddEntry token$,funcOffsetARY "graphics.library"
PRINT "found them."

PRINT:PRINT "ENTER FILESPEC:"
PRINT "( Note: You can create an ACBM file with LoadILBM-SaveACBM )"
PRINT
GetNames:
INPUT "   ACBM filespec";ACBMname$
IF (ACBMname$ = "") GOTO Mcleanup2
PRINT

REM - Load the ACBM pic
loadError$ = ""
GOSUB LoadACBM
IF loadError$ <> "" THEN GOTO Mcleanup

REM - Demo Graphicraft color cycling
IF foundCCRT AND ccrtDir% THEN
   REM - Save colors
   FOR kk = 0 TO nColors% -1
      cTabSave%(kk) = PEEKW(colorTab&+(kk*2))   
      cTabWork%(kk) = cTabSave%(kk)
   NEXT
   
   REM - Cycle colors
   FOR kk = 0 TO 80
      IF ccrtDir% = 1 THEN
         GOSUB Fcycle
      ELSE   
         GOSUB Bcycle
      END IF

      CALL LoadRGB4&(sViewPort&,VARPTR(cTabWork%(0)),nColors%)
      REM - Delays approximated
      FOR de1 = 0 TO ccrtSecs& * 3000
         FOR de2 = 0 TO ccrtMics& / 500
         NEXT
      NEXT
   NEXT

   REM - Restore colors
   CALL LoadRGB4&(sViewPort&,VARPTR(cTabSave%(0)),nColors%)
END IF

Mcleanup:
FOR de = 1 TO 20000:NEXT
WINDOW CLOSE 2
SCREEN CLOSE 2

Mcleanup2:
LIBRARY CLOSE
IF loadError$ <> "" THEN PRINT loadError$
END


Bcycle:  'Backward color cycle
cTemp% = cTabWork%(ccrtEnd%)
FOR jj = ccrtEnd%-1 TO ccrtStart% STEP -1
   cTabWork%(jj+1) = cTabWork%(jj)
NEXT
cTabWork%(ccrtStart%) = cTemp%
RETURN

Fcycle:  'Forward color cycle
cTemp% = cTabWork%(ccrtStart%)
FOR jj = ccrtStart%+1 TO ccrtEnd%
   cTabWork%(jj-1) = cTabWork%(jj)
NEXT
cTabWork%(ccrtEnd%) = cTemp%
RETURN


LoadACBM:
REM - Requires the following variables
REM - to have been initialized:
REM -    ACBMname$ (ACBM filespec)

REM - init variables
f$ = ACBMname$
fHandle& = 0
mybuf& = 0
foundBMHD = 0
foundCMAP = 0
foundCamg = 0
foundCCRT = 0
foundABIT = 0

REM - From include/libraries/dos.h
REM - MODE_NEWFILE = 1006 
REM - MODE_OLDFILE = 1005

filename$ = f$ + CHR$(0)
fHandle& = xOpen&(SADD(filen