joypadread.asm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. CheckJoypadModeLUT:
  2. .dw CheckJoypadSinglePlayer
  3. .dw CheckJoypad8Player
  4. .dw CheckJoypadVoid
  5. .dw CheckJoypadVoid
  6. CheckJoypadVoid:
  7. rts
  8. ;slow joypad checker, gets data for all 8 joypads with iobit=0.
  9. ;writes collected data to 8 individual joypad buffers
  10. CheckJoypad8Player:
  11. lda $4212
  12. bit #$01
  13. bne CheckJoypad8Player
  14. rep #$31
  15. lda.w $4218
  16. sta.w JoyPortBuffer&$ffff ;port1 pad 1
  17. lda.w $421c
  18. sta.w JoyPortBuffer&$ffff+2 ;port1 pad 2
  19. lda.w $421a
  20. sta.w JoyPortBuffer&$ffff+8 ;port2 pad 1
  21. lda.w $421e
  22. sta.w JoyPortBuffer&$ffff+10 ;port2 pad 2
  23. ;assume we don't need to latch again:
  24. sep #$20
  25. lda.b #%11000000 ;enable iobit on joyport1&2
  26. sta.w $4201
  27. lda.b #1
  28. sta.w $4016 ;enable latching
  29. stz.w $4016 ;latching end
  30. rep #$31
  31. ldx #16 ;get 32 bits from every port
  32. CheckJoyPortsLoop3:
  33. lda.w $4016
  34. lsr a ;put bit0 into carry
  35. rol.w JoyPortBuffer&$ffff+4 ;port 1 pad 3
  36. lsr a ;put bit1 into carry
  37. rol.w JoyPortBuffer&$ffff+6 ;port 1 pad 4
  38. asl a ;get upper byte
  39. asl a
  40. xba
  41. lsr a ;put bit0 into carry
  42. rol.w JoyPortBuffer&$ffff+12 ;port 2 pad 3
  43. lsr a ;put bit1 into carry
  44. rol.w JoyPortBuffer&$ffff+14 ;port 2 pad 4
  45. dex
  46. bne CheckJoyPortsLoop3
  47. sep #$20
  48. lda.b #1
  49. sta.w $4016 ;enable latching
  50. nop ;short delay is needed, otherwise mp5 sometimes doesn't respond in time.
  51. nop
  52. lda.w $4016 ;discard first read
  53. lda.w $4016
  54. and.b #%00000010 ;get data1,2 bits only
  55. ; cmp.b #%10 ;mp5 on port1?
  56. bne Port1MP5Connected
  57. stz.w JoyPortBuffer&$ffff+4 ;clear this joypads data if no mp5 connected
  58. stz.w JoyPortBuffer&$ffff+5 ;clear this joypads data if no mp5 connected
  59. Port1MP5Connected:
  60. lda.w $4017 ;discard first read
  61. lda.w $4017
  62. and.b #%00000010 ;get data1,2 bits only
  63. ; cmp.b #%10 ;mp5 on port2?
  64. bne Port2MP5Connected
  65. stz.w JoyPortBuffer&$ffff+$c ;clear this joypads data if no mp5 connected
  66. stz.w JoyPortBuffer&$ffff+$d ;clear this joypads data if no mp5 connected
  67. Port2MP5Connected:
  68. stz.w $4016 ;latching end
  69. rep #$31
  70. ldx.w #0
  71. CheckJoyPortsTriggerLoop:
  72. lda.w JoyPortBufferOld&$ffff,x ;get last button state
  73. eor.w #$ffff ;xor
  74. sta.w JoyPortBufferTrigger&$ffff,x
  75. lda.w JoyPortBuffer&$ffff,x
  76. sta.w JoyPortBufferOld&$ffff,x
  77. and.w JoyPortBufferTrigger&$ffff,x ;and and only get buttons that werent pressed last frame
  78. sta.w JoyPortBufferTrigger&$ffff,x ;store in old joypad trigger buffer
  79. inx
  80. inx
  81. cpx.w #8*2 ;process 8 entries
  82. bne CheckJoyPortsTriggerLoop
  83. sep #$20
  84. stz.w $4201 ;clear iobit again.
  85. rts
  86. ;fast joy1 checker. check this late in nmi so we don't have to wait for auto joypad read to finish:
  87. CheckJoypadSinglePlayer:
  88. lda $4212
  89. bit #$01
  90. bne CheckJoypadSinglePlayer
  91. rep #$30
  92. lda.w JoyPortBufferOld&$ffff ;get last button state
  93. eor.w #$ffff ;xor
  94. sta.w JoyPortBufferTrigger&$ffff
  95. lda $4218
  96. sta.w JoyPortBuffer&$ffff
  97. sta.w JoyPortBufferOld&$ffff
  98. and.w JoyPortBufferTrigger&$ffff ;and and only get buttons that werent pressed last frame
  99. sta.w JoyPortBufferTrigger&$ffff ;store in joypad buffer
  100. sep #$20
  101. rts