//
// Game port I/O library based on the wiring library
//
include "inc/cmdsys.plh"

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

export def digitalRead(pin)
  return FLAG0[pin&3] > 127
end
export def portRead
  return (^FLAG0>>7)&1|(^FLAG1>>6)&2|(^FLAG2>>5)&4|(^FLAG3>>4)&8
end
export def digitalWrite(pin, val)#0
  ANN0[((pin&3)<<1)+(val&1)]
end
export def portWrite(val)#0
  ANN0[val&1]
  ANN1[(val>>1)&1]
  ANN2[(val>>2)&1]
  ANN3[(val>>3)&1]
end
export def analogRead(pin)
  return call(PREAD,0,pin&3,0,0).2
end
export def delay(time)#0
  call(WAIT,time,0,0,0)
end
//
// Keep module in memory
//
return modkeep
done
