SNES2COM.spin 1.88 KB
Newer Older
Ada Gottensträter's avatar
Ada Gottensträter committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
CON

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000      
_stack   = 128

'' gamepad pin configuration (adjust for your setup)
  JOY_CLK = 16
  JOY_LCH = 17
  JOY_DATAOUT0 = 19
  JOY_DATAOUT1 = 18

  NUM_PADS = 2
  IBUS_CHANNELS = NUM_PADS*3 'Three channels per pad - One for the buttons, two for directions

OBJ

  com           : "Parallax Serial Terminal"

VAR
  word checksum
  word pad[NUM_PADS]

  

PUB main | i,axis

  com.start(230_400)

  repeat
    read_pads
    ibus_start(IBUS_CHANNELS)
    repeat i from 0 to NUM_PADS-1
      ibus_channel(pad[i])
      ' X Axis
      axis := $8000
      if pad[i]&%0100_0000 'left?
        axis -= $7FFF
      if pad[i]&%1000_0000 'right?
        axis += $7FFF
      ibus_channel(axis)
      ' Y Axis
      axis := $8000
      if pad[i]&%0001_0000 'up?
        axis -= $7FFF
      if pad[i]&%0010_0000 'down?
        axis += $7FFF
      ibus_channel(axis)
    ibus_end
    
    
PRI ibus_start(numChannels) | length
  length := 4+numChannels*2
  checksum := $FFFF - length - $40
  com.char(length)
  com.char($40)

PRI ibus_channel(val) | b
  b := val & $FF
  com.char(b)
  checksum -= b
  b := (val>>8) & $FF
  com.char(b)
  checksum -= b

PRI ibus_end
  com.char(byte[@checksum+0])
  com.char(byte[@checksum+1])
  
PRI read_pads |i,p0,p1

DIRA [JOY_LCH] := 1 ' output
DIRA [JOY_CLK] := 1 ' output
DIRA [JOY_DATAOUT0] := 0 ' input
DIRA [JOY_DATAOUT1] := 0 ' input

OUTA [JOY_CLK] := 0
waitcnt(cnt+100)
OUTA [JOY_LCH] := 0
waitcnt(cnt+100)
OUTA [JOY_LCH] := 1
waitcnt(cnt+100)
OUTA [JOY_LCH] := 0
waitcnt(cnt+100)
p0 := INA[JOY_DATAOUT0]
p1 := INA[JOY_DATAOUT1]

repeat i from 0 to 14
  OUTA [JOY_CLK] := 1
  waitcnt(cnt+100)
  OUTA [JOY_CLK] := 0
  waitcnt(cnt+100)
  p0 <<= 1
  p0 |= INA[JOY_DATAOUT0]
  p1 <<= 1
Ada Gottensträter's avatar
Ada Gottensträter committed
96
  p1 |= INA[JOY_DATAOUT1]
Ada Gottensträter's avatar
Ada Gottensträter committed
97 98

pad[0] := !(p0><16)
Ada Gottensträter's avatar
Ada Gottensträter committed
99
pad[1] := !(p1><16)