;**********************************************
; Macroses for global variables handling.
; This macro library is part of Fresh project.
;
; (C)2003, 2004
;
; Authors:
;   John Found
;   Tomasz Grisztar
;
; usage:
;   1. Include this library in the begining
;      of your source file.
;   2. Use it as blocks anywhere in the source.
;        iglobal
;           somelabel dd 1234h
;           otherlabel RECT
;           lbl3       db 12h
;        endg
;
;   3. Data section must be at the end of the
;      source file. Use 'IncludeAllGlobals' to
;      really define variables.
;
;**********************************************


;------------------------------------------------------------------
; 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 CmpStr newtext, [lbl, text] {

  forward
    if newtext eq text


}



;--------------------------------------------
; Use this macro to define non duplicated
; string constants.
;
; constStr Str1, 'This is string constant'
;--------------------------------------------
macro  constStr  lbl, text {
  __StringsArray equ __StringsArray, lbl, text     ;;  add to the list
}


macro IncludeStringConstants {
  macro __StringsArray dummy, [lbls, text] \{
    forward
      if text eqtype 'A'
        align 4

        lbls db text
        sizeof.#lbls = $ - lbls
        dw 0
      else
        if text eqtype 0
          lbls = text
        end if
      end if
  \}

  match S, __StringsArray \{
    S
  \}
}




macro IncludeAllGlobals {
  local begin

  begin = $
  IncludeIGlobals
  DispSize 'Initialized data', ($ - begin)

  begin = $
  IncludeStringConstants
  DispSize 'String constants data', ($ - begin)

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




iglobal
endg

uglobal
endg