This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Need ASM help for SNES controller (on NES)

Need ASM help for SNES controller (on NES)
by on (#93447)
Nintendulator has a function that has no documentation to use it with, the NES can use a normal SNES/SFC Controller, But can't be used right now without tech info on how to use in own NES projects.

Can Quietust or anyone else give any info on ASM controller-read implentations for SNES controls for NES?

by on (#93448)
Doesn't it work just like reading from the fourscore does? Reading $4016 and $4017 more times gives the extra buttons?

If so, the button order you get from the reads is this:

Quote:
B, Y, Select, Start, Up, Down, Left, Right, A, X, L, R.

From here:

http://wiki.nesdev.com/w/index.php/Standard_controller

by on (#93449)
Kasumi wrote:
Doesn't it work just like reading from the fourscore does? Reading $4016 and $4017 more times gives the extra buttons?

If so, the button order you get from the reads is this:

Quote:
B, Y, Select, Start, Up, Down, Left, Right, A, X, L, R.

From here:

http://wiki.nesdev.com/w/index.php/Standard_controller


I do not know, I need to be very careful about this...

Can there be a more straightfoward answer?

(And I already know about button conversions too...)

EDIT: Sorry for the Copy and Paste, Is it using 2 LSR instructions instead of one like this?:

Code:
;SNESCTRLR1
;bit:      7   6   5    4    3    2    1     0
;button:   B   Y  slct strt  up  down left right

;SNESCTRL2
;bit:      7   6   5   4
;button:   A   X   L   R

readjoy:
  lda #$01
  sta JOYPAD1
  sta buttons
  lsr a        ; now (button) is 0
  lsr a
  sta JOYPAD1
:
  lda JOYPAD1
  lsr a          ; bit0 -> Carry
  lsr a
  rol buttons  ; Carry -> bit0; bit 15 -> Carry
  bcc :-
  rts

by on (#93450)
Here's a quick test program that seems to read those buttons in Nintendulator.

The bottom bits of p3curstate/p3oldstate contain the state of the A, X, L, R buttons, and p1curstate/p1oldstate contain the rest. They're called p3 because this is a really quick mod of code I was using for the four score.

The actual joypad reading starts at main, the rest is just standard initialization.

Code:
; INES header setup
   .inesprg 1
   .ineschr 0
   .inesmir 0
   .inesmap 0
   
   .zp


p1curstate = $D8
p1oldstate = $D9



p3curstate = $DC
p3oldstate = $DD



   .code
   .bank 0
   .org $C000
   
reset:
      sei
   cld
   
   bit $2002
   
   ldx #$40
   stx $4017
   ldx #$00
   stx $2000
   stx $2001
   stx $4010
   stx $4015 
 
vb1
   bit $2002
   bpl vb1
   
   txa
clearram:
   sta $0000,x
   sta $0100,x
   sta $0200,x
   sta $0300,x
   sta $0400,x
   sta $0500,x
   sta $0600,x
   sta $0700,x
   inx
   bne clearram
   
   dex
   txs;Set up stack

vb2:
   bit $2002
   bpl vb2
   
main:
   BIT $2002
   BPL main

   lda <p1curstate
   sta <p1oldstate

   lda <p3curstate
   sta <p3oldstate
   
   lda #$01
   sta <p1curstate ;initialize the buffer with a flag
   
   sta $4016
   lsr a
   sta $4016
   
   lda #%00010000;initialize the buffer with a flag
   sta <p3curstate
   
p1loop:
   lda $4016
   ror a
   rol <p1curstate
   bcc p1loop ;loop if the flag wasn't shifted out yet
   
p3loop:
   lda $4016
   ror a
   rol <p3curstate
   bcc p3loop ;loop if the flag wasn't shifted out yet

   

mainend:
   jmp main

   
   
   
   
   

   .bank 1
   .org $E000

   
   .org $FFFA
   .dw 0         
   .dw reset         
   .dw 0            
   


I'm not sure about the two lsr instructions in your edit. I feel like that shifts one bit relevant to the button press out without checking it, so it would only work with famicom expansion controllers.

by on (#93451)
The SNES controller just has two NES shift registers in series. It works identically to the NES controller except there are 16 buttons vice 8. Whatever you're doing to read NES buttons is the same routine you just keep reading the buttons until you have all 16 compared to just stopping at 8. Nothing with the strobes or anything changes, the controller latches all 16 buttons on the single strobe similar to how the NES latches all 8.

Kasumi has the order correct.

by on (#93452)
Thanks, I will now use it and also put it up in the Wiki sometime this week.

Also gonna be converted to ASM6 myself.