         nolist

* Macros for generating object modules
* for the APM operating system
* Sequence of calls should be:
* {STACK SET requirement}
*  MODULE <module-name>
* {EXPORT <symbol>,<mode>}*
* {IMPORT <symbol>,<mode>}*
* {CODE
*  RESET ...
* {COPYDATA}
*  BEGIN ...}
* {SCALL/CALL/ADDRESS ...}*
* {DATA
* {symbol VECTOR <mode>}*}
*  ENDMODULE
*
* ALIASX A,B,C may be used instead of  X A,B
* where X is EXPORT or IMPORT
* and C is the aliassed name

         bin
         rorg 0

phase    set 0
STACK    SET 0

data     equ $c000
system   equ $d000
external equ $e000
dynamic  equ $f000

endexport macro  * internal use only
  ifeq phase-1
  ifne exportbeg-*
   dc.w 0
  endc
exportsize equ *-exportbeg
importbeg equ *
phase set 2
  endc
 endm

endimport macro  * internal use only
  ifeq phase-2
  ifne importbeg-*
   dc.w 0
  endc
importsize equ *-importbeg
codebeg equ *
phase set 3
  endc
 endm

endcode macro  * internal use only
  ifeq phase-3
databeg equ *
phase set 4
  endc
 endm

MODULE MACRO
  ifne phase
? only one module per file
  endc
   ttl \1 module
   dc.l $fe020000
   dc.w exportsize,importsize
   dc.l codesize
   dc.w (reset-codebeg)>>1
   dc.w (begin-codebeg)>>1
   dc.l datasize,stack
   dc.l 0,0
exportbeg equ *
phase set 1
   endm

EXPORT MACRO
  ifne phase-1
? EXPORT directive misplaced
  endc
  dc.w \2,0,0,0
  ifeq \2-data
   dc.l \1-databeg
  endc
  ifne \2-data
   dc.l \1-codebeg
  endc
  dc.b \@-*-1,'\1'
\@ equ *
  ds.w 0
 endm

ALIASEXPORT MACRO
  ifne phase-1
? EXPORT directive misplaced
  endc
  dc.w \2,0,0,0
  ifeq \2-data
   dc.l \1-databeg
  endc
  ifne \2-data
   dc.l \1-codebeg
  endc
  dc.b \@-*-1,'\3'
\@ equ *
  ds.w 0
 endm

IMPORT MACRO
  endexport
  ifne phase-2
? IMPORT directive misplaced
  endc
  dc.w \2,0,0,0
  dc.l \1-databeg
  dc.b \@-*-1,'\1'
\@ equ *
  ds.w 0
 endm

ALIASIMPORT MACRO
  endexport
  ifne phase-2
? IMPORT directive misplaced
  endc
  dc.w \2,0,0,0
  dc.l \1-databeg
  dc.b \@-*-1,'\3'
\@ equ *
  ds.w 0
 endm

CODE MACRO
  endexport
  endimport
  ifne phase-3
? CODE misplaced
  endc
 endm

ADDRESS MACRO
\* move.l \2-databeg(a4),\1
  endm

SCALL MACRO
\* jsr \1-databeg(a4)
  endm

CALL MACRO
\* move.l a4,-(sp)
  jsr \1-databeg(a4)
  move.l (sp)+,a4
  endm

COPYDATA MACRO
\* lea databeg,a0
   move.l a4,a1
   move #(datasize+1)>>1-1,d0
   bmi.s \@b
\@a move.w (a0)+,(a1)+
   dbra d0,\@a
\@b equ *
  endm

DATA MACRO
  endexport
  endimport
  endcode
  ifne phase-4
? DATA misplaced
  endc
 endm

VECTOR MACRO
  ifne phase-4
? VECTOR must be in data section
  endc
\* equ *
  ifeq \1-data
   dc.l $654321
  endc
  ifne \1-data
   ifne \1-system
    move.l #$654321,a4
   endc
   jmp $654321
  endc
 endm

ENDMODULE MACRO
  endexport
  endimport
  endcode
  ifne phase-4
? ENDMODULE misplaced
  endc
codesize equ *-codebeg
datasize equ *-databeg
  endm

  list
  end
