//
// GAME PORT I/O LIBRARY
// BASED ON THE WIRING LIBRARY
//
CONST ANN0   = $C058
CONST ANN1   = $C05A
CONST ANN2   = $C05C
CONST ANN3   = $C05E
CONST OFF    = 0
CONST ON     = 1
CONST STROBE = $C040
CONST FLAG0  = $C060
CONST FLAG1  = $C061
CONST FLAG2  = $C062
CONST FLAG3  = $C063
CONST PREAD  = $FB1E
CONST WAIT   = $FCA8

DEF DIGITALREAD(PIN)
  RETURN FLAG0[PIN&3] > 127
END

DEF PORTREAD
  RETURN (^FLAG0>>7)&1|(^FLAG1>>6)&2|(^FLAG2>>5)&4|(^FLAG3>>4)&8
END

DEF DIGITALWRITE(PIN, VAL)
  RETURN ^ANN0[((PIN&3)<<1)+VAL&1]
END

DEF PORTWRITE(VAL)
  ^ANN0[VAL&1]
  ^ANN1[(VAL>>1)&1]
  ^ANN2[(VAL>>2)&1]
  RETURN ^ANN3[(VAL>>3)&1]
END

DEF ANALOGREAD(PIN)
  RETURN CALL(PREAD,0,PIN&3,0,0).2
END

DEF DELAY(TIME)
  RETURN CALL(WAIT,TIME,0,0,0)
END

//
// OUTPUT THE ANALOG INPUT TO
// THE DIGITAL PORT
//
WHILE ^$C000 < 128
  PORTWRITE((255 - ANALOGREAD(2)) / 16)
LOOP
^$C010

DONE
