; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "Fresh artistic license."         |
;|_______________________________________________________________________________________|
;
;   Description: Macto library for global variables handling.
;
;   Target OS: Any
;
;   Dependencies:
;
;   Notes:
;_________________________________________________________________________________________

;------------------------------------------------------------------
; use "iglobal" for inserting initialized global data definitions.
;------------------------------------------------------------------
macro iglobal {
  IGlobals equ IGlobals,
  macro __IGlobalBlock \{
}


;-------------------------------------------------------------
; use 'uglobal' for inserting uninitialized global definitions.
; even when you define some data values, these variables
; will be stored as uninitialized data.
;-------------------------------------------------------------
macro uglobal {
  UGlobals equ UGlobals,
  macro __UGlobalBlock \{
}

; Use endg for ending iglobal and uglobal blocks.
endg fix }


macro IncludeIGlobals {
  macro IGlobals dummy, [n] \{
    __IGlobalBlock
    purge __IGlobalBlock
  \}
  match I, IGlobals \{
    I
  \}
}


macro IncludeUGlobals {
  macro UGlobals dummy, [n] \{
    \common
      \local begin, size
      begin = $
      virtual at $
    \forward
        __UGlobalBlock
        purge __UGlobalBlock
    \common
        size = $ - begin
      end virtual
      rb size
  \}
  match U, UGlobals \{
    U
  \}
}




macro InitStringList {
  local r,v,c
  c equ 0

  struc text [val] \{
    \common
      if ~val eqtype 1
      virtual at 0
    \forward
        db val
    \common
      .length = $
      end virtual
      end if
    rept 1 x:c+1 \\{
      c equ x
      r\\#x equ .
      v\\#x equ val
    \\}
  \}

  macro __IncludeAllStrings \{
    \common
    \local ..start, len, i, j, f, ch1, ch2
    label ..start byte
    if defined options.ShowSizes and options.ShowSizes
      disp 3, 'String data address: $', <..start, 16>, 13
    end if

    rept c x \\{
      if used r\\#x
        if ~v\\#x eqtype 1
          r\\#x db v\\#x, 0
        else
          label r\\#x byte at v\\#x
        end if
      end if
    \\}
  \}
}

;macro InitStringList {
;  local r,v,c
;  c equ 0
;  struc string [val] \{
;    \common
;    rept 1 x:c+1 \\{
;      c equ x
;      r\\#x equ .
;      v\\#x equ val
;    \\}
;  \}
;
;  macro __IncludeAllStrings \{
;    \common
;    \local ..start
;    label ..start byte
;    if defined options.ShowSizes and options.ShowSizes
;      disp 3, 'String data address: $', <..start, 16>, 13
;    end if
;    rept c x \\{
;      \\common
;      \\forward
;      \\local len, i, j, f, ch1, ch2, hash, hash2
;      if ~v\\#x eqtype 1
;        virtual at 0
;          db v\\#x, 0
;          len = $
;
;          i = 0
;          hash = 1234
;          while i<len
;            load ch1 byte from i
;            hash = hash + ch1
;            i=i+1
;          end while
;        end virtual
;
;        i = ..start
;        j = 0
;        f = 0
;        while i<=($-len)
;          j = 0
;          f = 0
;          hash2 = 1234
;          while j<len
;            load ch1 byte from i+j
;            hash2=hash2+ch1
;            if hash2>=hash
;              break
;            end if
;            j=j+1
;          end while
;
;          if hash2=hash
;            j=0
;            while j<len
;              load ch1 byte from i+j
;              virtual at 0
;                db v\\#x, 0
;                load ch2 byte from j
;              end virtual
;              if ch1 <> ch2
;                break
;              end if
;              j=j+1
;            end while
;            if (ch1 =0) & (ch2 = 0)
;              f = 1
;              break
;            end if
;          end if
;          i = i + 1
;        end while
;
;        if ~f
;          r\\#x db v\\#x, 0
;          sizeof.#r\\#x = len
;        else
;          label r\\#x byte at i
;        end if
;      else
;        label r\\#x byte at v\\#x
;      end if
;    \\}
;  \}
;}

InitStringList


macro IncludeAllGlobals {
  local begin

  begin = $
  disp 3, 'Initialized data address: $', <begin, 16>, 13
  IncludeIGlobals
  DispSize 'Initialized data', ($ - begin)

  begin = $
  __IncludeAllStrings
  DispSize 'String data', ($ - begin)

  begin = $
  IncludeUGlobals
  DispSize 'Uninitialized data', ($ - begin)
}



macro var expr {
common
  match name==value, expr \{
    if used name
      name dd value
    else
      name = 0
    end if
  \}
}



iglobal
endg

uglobal
endg
