Reading the nes (dendy) controller in assembler

In the previous article I told you about the basics of the 6502 assembler about some memory areas and how to work with them, until today I had the following series of tasks: reading the controller, animation, timer. Below under the cut I'll tell you how I learned to read the controller.






Everything, as before, started with a goal, I went to the nesdev wiki (the link will be at the end of the article) in order to read about how to work with the controller. After all, if you look at it, there are only a few external triggers for the game, upon which the game performs certain actions. Firstly, this is an internal timer, the actions of the characters are performed at the onset of a certain time, a striking example of this can be simple bosses in games, their behavior is described by a template that is tied to any time period. Secondly, an important trigger for the game is pressing the controller buttons, which the main character of the game mainly responds to, by pressing up, down, left and right, start and select. The hero can move, jump, crawl and so on. That is, pressing the button tells the game to do this and that.Running ahead programming for the NES, I try to focus not on the object that will be on the screen, as many modern programmers are possible, but what will be drawn in this or that frame.





But closer to the subject, there are a lot of controllers on the NES, ranging from classic to inflatable motorcycles and microphones, but the dandy had only two classic gamepads (joystick) and a light pistol. We will probably talk about the latter later. And so the dandy has two processor ports for reading controllers $ 4016 for the 1st gamepad, $ 4017 for the second gamepad.





The first step is to "reboot the controller" or "initialize" it by simply writing the values ​​$ 01 and $ 00 to the port sequentially





deadyJoy:
 lda #$01
 sta $4016
 lda #$00
 sta $4016
      
      



Next, the dandy reads the press in the following sequence A, B, Select, Start, Up, Down, Left, Right and writes to port $ 4016, so in order to receive a button press, we only need to read from this port, the value sequentially 8 times





lda #4016 ; A
lda #4016 ; B
lda #4016 ; Select
lda #4016 ; Start
lda #4016 ; Up
lda #4016 ; Down
lda #4016 ; Left
lda #4016 ; Right
      
      



A . , . . X Y . zeropage ( ) $00 - $FF , , , .





ReadA:
  LDA $4016       ;  A
  AND #%00000001 ;    N  Z
  bne walkHeroRight ;    A
  beq ReadB ;       B

ReadB:
  LDA $4016       ;  B
  AND #%00000001 
  bne walkHeroLeft ;   B walkHeroLeft     
  beq ReadSelect ;    Select 
  
;  5       

ReadRight:
  LDA $4016       ;  
  AND #%00000001
  bne walkHeroRight ;     
  beq heroStay ;         
      
      



assembler , , . , . . X Y





walkHeroRight:
  inc herroXCoordinate
  jmp drawHero

walkHeroLeft:
  dec herroXCoordinate
  jmp drawHero
      
      








drawHero:
  lda herroYCoordinate
  sta $2004
  lda $00
  sta $2004
  lda #%00010111
  sta $2004
  lda herroXCoordinate
  sta $2004

  lda herroYCoordinate
  sta $2004
  lda #01
  sta $2004
  lda #%00010111
  sta $2004
  lda herroXCoordinate
  adc #$07
  sta $2004

  lda herroYCoordinate
  adc #$07
  sta $2004
  lda $02
  sta $2004
  lda #%00010111
  sta $2004
  lda herroXCoordinate
  sta $2004

  lda herroYCoordinate
  adc #$07
  sta $2004
  lda #03
  sta $2004
  lda #%00010111
  sta $2004
  lda herroXCoordinate
  adc #$07
  sta $2004

  nmi_delay 4
  jmp mainLoop
      
      



And the cycle repeats: reading, determining, drawing the hero's state.

This is how it turned out, not everything is as complicated as it seemed at first glance. The assembler itself causes more difficulties, sometimes some points are not so transparent, but I would like to say one simple thing, if there is a dream it is worth going to it, and in no case should you give up such undertakings. In the next articles I would like to talk about animation and graphics creation.





Past article

Literature : http://wiki.nesdev.com/

Example entirely on github








All Articles