! Load one NS32000 object file into APM memory

%include "inc:util.imp"
%begin

%constinteger -
obj esc = 16_5e,
obj code = 0,
obj data = 1,
obj eom  = 2,
obj org  = 3,
obj export = 4,
obj import = 5

%switch sw(obj code:obj import)
%integer code,data,start,size,org,c,d
%integername l
%bytename b
%string(255)s

%integerfn word
%integer n
  n = ((b[3]<<8+b[2])<<8+b[1])<<8+b
  b == b[4]; size = size-1
  %result = n
%end

%string(*)%map symbolname
%string(*)%name s
  s == string(addr(b))
  b == b[length(s)+1]; size = size-length(s)-1
  %result == s
%end

  s = cliparam.".o32"
  connectfile(s,0,start,size)
  code=heapget(65536); data=heapget(65536)
  b == byte(start)
  c = code
  d = data
  l == c

  %while size>0 %cycle
    %if b=obj esc#b[1] %start
      b == b[2]; size = size-2; ->sw(b[-1])
sw(objcode):
      l == c
      %continue
sw(objdata):
      l == d
      %continue
sw(objorg):
      org = word
      %if l==c %start
        org = org+code %if org<=65535
      %else
        org = org+data %if org<=65535
      %finish
      l = org
      %continue
sw(objexport):
      s = symbolname
      %continue
sw(objimport):
      printstring("External symbols not supported"); newline; %stop
    %else
      byte(l) = b; l = l+1
      b == b[1] %and size = size-1 %if b=obj esc
      b == b[1]; size = size-1
    %finish
  %repeat
sw(objeom):

%unless c=code %start
  printstring("Code from "); phex(code); printstring(" to "); phex(c); newline
%finish
%unless c=data %start
  printstring("Data from "); phex(data); printstring(" to "); phex(d); newline
%finish

%end
