REM                MERGE THIS FILE TO YOUR PROGRAMS

REM - C.VASSALLO, 1989  - All rights reserved. You can freely use or
'     distribute this file or parts of it for any non-commercial application.

REM - This file contains the various Basic subprograms for using my main
'     assembly routines for AmigaBasic programs. Insert it in your program
'     with the command
'                   MERGE "path/MergeFile"
'     (where "path" stands for the right path to find this file) and remove
'     the unnecessary parts.

    
' -------------------- Loading routines --------------------


'             ILBM.bcode     : 2978
'             FileReq.bcode  : 7138
'             ShowText.bcode : 1940
'             HPal.bcode     : 3708
'             VPal.bcode     : 3376

LoadRoutines:
 
 DIM PaletteCode%(1854)
 CALL Bload(":ILBM/HPal.bcode",PaletteCode%(),3708)
 
 DIM ILBM%(1490)
 CALL Bload(":ILBM/ILBM.bcode",ILBM%(),2978)
 
 DIM FileReq%(3570)
 CALL Bload(":ILBM/FileReq.bcode",FileReq%(),7138)
 
 DIM ShowText%(970)
 CALL Bload(":ILBM/ShowText.bcode",ShowText%(),1940)
 
RETURN 

REM - loads size% bytes from "file$" to code%() array. AmigaBasic can only
'     put these bytes into a string, not into an array.
 
 SUB Bload(file$,code%(1),size%) STATIC
   copy&(0)=2^16*&H206F + 4&
   copy&(1)=2^16*&H226F + 8&
   copy&(2)=2^16*&H202F + 12&
   copy&(3)=2^16*&H5380 + 4824&    
   copy&(4)=2^16*&H51C8 + 65532&
   copy&(5)=2^16*&H4E75
   OPEN file$ AS #1 LEN=size%
   FIELD #1, size% AS a$
   GET #1
   co&=VARPTR(copy&(0))
   CALL co&(SADD(a$),VARPTR(code%(0)),size%)
   CLOSE #1
 END SUB




'---------------------  IFF/ILBM ---------------------


' opt&=0& (don't forget the trailing &) = load in active Basic Window
' opt&=1& = View mode. Click top left to close
' opt&=2& = third mode. The special screen is not closed

'errmsg$  = error message. Empty if success


SUB ILBMLoad(file$,opt&,errmsg$,ILBM%(1)) STATIC
       erreur$="" : i%=0 : n%=0 : ErrAd&=0
       file0$=file$+CHR$(0)
       ad&=opt& : IF ad&=0 THEN ad&=WINDOW(7)
       loading&=VARPTR(ILBM%(0))
       CALL loading&(SADD(file0$),ad&,VARPTR(ErrAd&))
       IF ErrAd& = 0 THEN EXIT SUB
       n%=PEEK(ErrAd&)
       WHILE n% <> 0
          errmsg$=errmsg$+CHR$(n%)
          i%=i%+1 : n%=PEEK(ErrAd&+i%)
       WEND
       n%=PEEK(VARPTR(ILBM%(0))+2479)
       IF n% <> 0 THEN errmsg$=errmsg$+" - DOS error "+STR$(n%)
END SUB
       
       
REM - To close the special screen of third mode (opt&=2&)

CloseScreen:
      CloseAll&=VARPTR(ILBM%(0))+1434
      CALL CloseAll&
      RETURN
      

REM - saving

SUB ILBMsave(file$,errmsg$,ILBM%(1)) STATIC
       errmsg$="" : i%=0 : n%=0 : ErrAd&=0
       file0$=file$+CHR$(0)
       saving&=VARPTR(ILBM%(0))+1606
       CALL saving&(SADD(file0$),WINDOW(7),VARPTR(ErrAd&))
       IF ErrAd& = 0 THEN EXIT SUB
       n%=PEEK(ErrAd&)
       WHILE n% <> 0
          errmsg$=errmsg$+CHR$(n%)
          i%=i%+1 : n%=PEEK(ErrAd&+i%)
       WEND
       n%=PEEK(VARPTR(ILBM%(0))+2479)
       IF n% <> 0 THEN errmsg$=errmsg$+" - DOS error "+STR$(n%)
END SUB


'  RastPort& = PEEKL(VARPTR(ILBM%(0))+2506) : RastPort
'  ViewPort& = PEEKL(VARPTR(ILBM%(0))+2510) : ViewPort
'  Screen&   = PEEKL(VARPTR(ILBM%(0))+2514) : Screen
'       Wdw& = PEEKL(VARPTR(ILBM%(0))+2518) : Window




'----------------- File Requester -------------------------  

' dir$ = displayed directory
' op%  = 0 for LOAD mode, 1 for SAVE mode
' file$= returned filename (with a complete path)

SUB SelectFile(dir$,FileReq%(1),op%,file$) STATIC
  dir0$=dir$+CHR$(0)
  op&=op% : n=0 : i=0 :file$="" : buff&=0
  FileReq&=VARPTR(FileReq%(0))  
  buff&=FileReq&+5988
  CALL FileReq&(SADD(dir0$),op&,WINDOW(7))
  n=PEEK(buff&)  : file$=""
  WHILE n<>0
    file$=file$+CHR$(n)
    i=i+1 : n=PEEK(buff&+i)
  WEND
END SUB


REM - frees internal permanent buffers - DON'T FORGET IT WHEN CLOSING
'                                             YOUR PROGRAM

CloseFileReq:
  a&=VARPTR(FileReq%(0))+4348
  CALL a&
  RETURN  

REM - Empties the name of the displayed directory, kept in memory by
'     the routine

FreeCurrDir:
  a&=VARPTR(FileReq%(0))+6148  
  POKE a&,0
  RETURN  
  
REM - returns the name of the last displayed directory into "dir$"

CurrentDir:
  dir$="" : n=0 : a&=VARPTR(FileReq%(0))+6148
  n=PEEK(a&)
  WHILE n<>0
    dir$=dir$+CHR$(n)
    a&=a&+1 : n=PEEK(a&)
  WEND
  RETURN


'USEFUL ADDRESSES (add the following figures to VARPTR(ReqCode%(0))

'5635 : address of "df0:" word (in "drive" menu)
'5640 :            "df1:"
'5645 :            "ram:"
'5650 :            "dh0:"
'5655 :            "vd0:"

'To replace "vd0" with "rad", merely execute

'        a&=VARPTR(ReqCode%(0))+5655
'        POKE a&,ASC("r") : POKE a&+1,ASC("a") : POKE a&+2,ASC("d")

'$1BC8 : pointer to the buffer containing the catalog
'$1BCC : byte size of this buffer
'$1804 : last displayed directory (Null-terminated string, up to 48 bytes)


'------------------- Text file display -------------


  SUB Display(file$,ShowText%(1)) STATIC
    note0$=file$+CHR$(0)
    SText&=VARPTR(ShowText%(0))
    CALL SText&(SADD(note0$))
  END SUB

'Example to change the display colors. Each color (2nd argument of POKEW)
'is coded as &Hrvb. 
'  a&=VARPTR(ShowText%(0))
'  POKEW a&+&H734,&H000 :'background color
'  POKEW a&+&H736,&HFFF :'text color
'  POKEW a&+&H5C, &HF00 :' .../... or "end of text"
'  POKEW a&+&H96, &H555 :'gadget background
'  POKEW a&+&HB2, &HFF0 :'gadget text

'------------------------- Palette ---------------------------

  ColorPalette:
    Pal&=VARPTR(PaletteCode%(0))
    CALL Pal&(WINDOW(7))
  RETURN

'------------------------ PAL or NTSC ? -----------------------

'haut% is returned as 200 or 256

  SUB PAL.NTSC(haut%) STATIC
    DIM pa%(30)
    FOR i=0 TO 30 : READ pa%(i) : NEXT i
    haut%=0 : ecr&=VARPTR(pa%(0))
    CALL ecr&(VARPTR(haut%))
    ERASE pa%
  END SUB
  DATA &H246F, &H0004, &H4280, &H2C79, &H0000, &H0004, &H43FA, &H001E
  DATA &H4EAE, &HFE68, &H4A80, &H6700, &H0012, &H2040, &HD1FC, &H0000
  DATA &H00D8, &H3490, &H2240, &H4EAE, &HFE62, &H4E75, &H6772, &H6170
  DATA &H6869, &H6373, &H2E6C, &H6962, &H7261, &H7279, &H0000



