controller:
            sw      ra,contreturn

            la      a0,temp
            lw      d0,0(a0)
            addiu   d0,d0,$FFFFFFFF
            bgezl   d0,exitcont
            sw      d0,0(a0)
            addiu   d1,r0,$1E00
            sw      d1,color
            
            lui     a0,$A480
            la      d0,pif2
            sw      d0,$0000(a0) ;si dram address
            lui     d0,$1FC0     
            ori     d0,d0,$07C0  ;PIF joychannel
            sw      d0,$0004(a0) ;64B PIF->DRAM
            lui     a0,$BFC0
;            lbu     d1,$7FF(a0)
;            beq     d1,r0,nosystem_reset
;            nop
;            j       system_reset
;            nop
nosystem_reset
            lhu     d0,$7C4(a0) ;**HWORD(JOY)->d0**


        ;joy 2
            lhu     d1,$7CC(a0)
            lhu     d9,lastbutton
            sh      d1,lastbutton
;            beq     d1,d9,@here
            NOP
            andi    d2,d1,CONT_B   ;B
            beqz    d2,@joy2_a
            nop
            lh  d3,object1+(3*2)
            addu        d3,1
            andi        d3,$1ff
            sh  d3,object1+(3*2)


            nop
            j       exitcont
@joy2_a:      andi    d2,d1,CONT_A    ;A
            beqz    d2,@joy2_up
            nop
            lh  d3,object1+(3*2)
            addu        d3,-1
            andi        d3,$1ff
            sh  d3,object1+(3*2)


            j       exitcont
            nop
@joy2_up:   
            andi    d2,d1,$800    ;up
            beqz    d2,@joy2_down
  nop
            lh  d3,object1+4
            addu  d3,40
            sh  d3,object1+4

            j exitcont
            nop
@joy2_down  andi    d2,d1,$400    ;down
            beqz    d2,@joy2_left
  nop
            lh  d3,object1+4
            addu  d3,-40
            sh  d3,object1+4
            j exitcont
            nop

@joy2_left  andi    d2,d1,$200    ;left
            beqz    d2,@joy2_right
            nop
            lh  d3,object1
            addu  d3,-40
            sh  d3,object1



            j exitcont
            nop
@joy2_right andi    d2,d1,$100    ;right
            beqz    d2,@checkanalog
            nop
            lh  d3,object1
            addu  d3,40
            sh  d3,object1

            j exitcont
            nop

@checkanalog 


@here       beq     d0,r0,exitcont
            nop
            andi    d1,d0,$4000    ;BUTTON B
            beq     d1,r0,next1
;            j       exitcont
            nop     
next1:      andi    d1,d0,$8000     ;BUTTON A
            beq     d1,r0,next2
;            j       exitcont
            nop
next2:      andi    d1,d0,$200      ;LEFT 
            beq     d1,r0,next3
            nop
            lw      d3,position2
            addiu   d3,d3,-1
            andi    d3,d3,127
            sw      d3,position2
            addiu   d1,r0,7
            sw      d1,temp
            j       exitcont
            nop
next3:      andi    d1,d0,$100      ;RIGHT
            beq     d1,r0,next4
            nop
            lw      d3,position2
            addiu   d3,d3,1
            andi    d3,d3,127
            sw      d3,position2
            addiu   d1,r0,7
            sw      d1,temp  ;delay
            j       exitcont
            nop
next4:      andi    d1,d0,%100000000000  ;UP
            beq     d1,r0,next5
            nop
            lw      d3,position2
            addiu   d3,d3,$FFFFFFE0
            andi    d3,d3,127
            sw      d3,position2
            addiu   d1,r0,7
            sw      d1,temp ;delay
            j       exitcont
            nop
next5:      andi    d1,d0,%10000000000  ;DOWN
            beq     d1,r0,next6
            nop
            lw      d3,position2
            addiu   d3,d3,32
            andi    d3,d3,127
            sw      d3,position2
            addiu   d1,r0,7
            sw      d1,temp ;delay
            j       exitcont
            nop
next6:      andi    d1,d0,%100000   ;LPAD
            beq     d1,r0,next7
            nop
            sw      d0,joypad
            lw      d1,position2
            srl     d2,d1,1
            la      a0,memory
            addu    a0,a0,d2
            addiu   d3,r0,-16       ;for decrease
            addiu   d4,r0,-1
            jal     adjustmem
            nop
            lw      d0,joypad
            andi    d1,d0,%10000000000000  ;Z button
            addiu   d0,r0,7
            la      a0,temp
            beq     d1,r0,exitcont
            sw      d0,0(a0)
            ori     d1,r0,$F001
            sw      d1,color
            lw      d0,xstat
            beq     d0,r0,exitcont
            nop
            jal     pokemem
            nop
            j       exitcont
            nop
next7:      andi    d1,d0,%10000    ;RPAD
            beq     d1,r0,next8
            nop
            sw      d0,joypad
            lw      d1,position2
            srl     d2,d1,1
            la      a0,memory
            addu    a0,a0,d2
            addiu   d3,r0,$10       ;for increase
            addiu   d4,r0,1
            jal     adjustmem
            nop
            lw      d0,joypad
            andi    d1,d0,%10000000000000  ;Z button
            addiu   d0,r0,7
            la      a0,temp
            beq     d1,r0,exitcont
            sw      d0,0(a0)
            ori     d1,r0,$F001
            sw      d1,color
            lw      d0,xstat
            beq     d0,r0,exitcont
            nop
           jal     pokemem
            nop

            j  exitcont
next8:      andi    d1,d0,%1        ;RIGHT C
            beq     d1,r0,next9
            nop
            lw      d0,position1      
            addiu   d0,d0,1
            andi    d0,d0,7
            sw      d0,position1
            addiu   d0,r0,7
            sw      d0,temp    ;DELAY
            j       exitcont
            nop

next9:      andi    d1,d0,%10        ;LEFT C
            beq     d1,r0,next10
            nop
            lw      d0,position1
            addiu   d0,d0,-1
            andi    d0,d0,7
            sw      d0,position1
            addiu   d0,r0,7
            sw      d0,temp    ;DELAY
            j       exitcont
            nop
next10:     andi    d1,d0,$1000     ;START
            beq     d1,r0,next11
            NOP


            addiu   d1,r0,8
            sw      d1,temp      ;delay joypad
            la      a0,xstat
            lw      d0,0(a0)
            addiu   d1,r0,2      ;xstat limit
            beql    d0,d1,exitcont
            sw      r0,0(a0)
            addiu   d0,d0,1
            sw      d0,xstat
            j       exitcont
next11:     andi    d1,d0,%1000  ;C up
            beq     d1,r0,next12
            nop
            lw      d1,position1
            srl     d2,d1,1
            la      a0,temp4
            addu    a0,a0,d2
            addiu   d3,r0,$10      ;for increase
            addiu   d4,r0,1
            jal     adjustmem
            nop
            addiu   d0,r0,7
            sw      d0,temp    ;DELAY
            j       exitcont
            nop
next12:     andi    d1,d0,%100  ;C down
            beq     d1,r0,next13
            nop
            lw      d1,position1
            srl     d2,d1,1
            la      a0,temp4
            addu    a0,a0,d2
            addiu   d3,r0,-16    ;for decrementing
            addiu   d4,r0,-1
            jal     adjustmem
            nop
            addiu   d0,r0,7
            sw      d0,temp    ;DELAY
            j       exitcont
            nop
next13:     andi    d1,d0,%10000000000000  ;Z button
            beq     d1,r0,exitcont
            lw      d3,temp4
            andi    d3,-4
            sw      d3,temp3
            sw      d3,temp4
            ori     d1,r0,$F001
            sw      d1,color
            addiu   d2,r0,10
            sw      d2,temp  ;delay

            lw      d0,xstat
            beq     d0,r0,exitcont
            nop
            jal     pokemem
            nop
            j  exitcont
            nop

exitcont:   lw  ra,contreturn
            jr  ra
            nop


Readcontroller1:
            la      d0,pif1
            lui     a0,$A480       ;SI DRAM ADDR.
            sw      d0,0(a0)  ; pif1
            li      d0,$1FC007C0
            sw      d0,$10(a0)  ;64B DRAM -> PIF
            jr      ra
            nop

Readcontroller2:
            lui     a0,$A480
            la      d0,pif2
            sw      d0,$0000(a0) ;si dram address
            lui     d0,$1FC0     
            ori     d0,d0,$07C0  ;PIF joychannel
            sw      d0,$0004(a0) ;64B PIF->DRAM
            jr ra
            nop

lastbutton dw 0
contreturn dw 0

 CNOP 0,8
pif1:  DW $FF010401
 DW $00000000
 DW $FF010401
 DW $00000000 
 DW $FF010401
 DW $00000000
 DW $FF010401
 DW $00000000
 DW $FE000000
 DW $00000000
 DW $00000000
 DW $00000000
 DW $00000000
 DW $00000000
 DW $00000000
 DW $00000001
 
pif2: DW 0,0,0,0,0,0,0,0
      DW 0,0,0,0,0,0,0,0
