Function ctrl_gamemite$(init)
  If Not init Then
    Local bits = Inv Port(GP8, 8) And &hFF, s$
    
    Select Case bits
        Case 0    : Exit Function
        Case &h01 : s$ = "down"
        Case &h02 : s$ = "left"
        Case &h04 : s$ = "up"
        Case &h08 : s$ = "right"
        Case &h10 : s$ = "map"           ' Select
        Case &h20 : s$ = "use-item"      ' Start
        Case &h41 : s$ = "toggle-item"   ' Down + Fire B
        Case &h42 : s$ = "search"        ' Left + Fire B
        Case &h44 : s$ = "toggle-weapon" ' Up + Fire B
        Case &h48 : s$ = "move"          ' Right + Fire B
        Case &h80 : s$ = "fire-a"
        Case &h81 : s$ = "fire-down"     ' Down + Fire A
        Case &h82 : s$ = "fire-left"     ' Left + Fire A
        Case &h84 : s$ = "fire-up"       ' Up + Fire A
        Case &h88 : s$ = "fire-right"    ' Right + Fire A
        Case &hC0 : s$ = "quit"          ' Fire A + Fire B
    End Select
    
    ctrl_gamemite$ = s$
    Exit Function
  Else
    ' Initialise GP8-GP15 as digital inputs with PullUp resistors
    Local i
    For i = 8 To 15
      SetPin MM.Info(PinNo "GP" + Str$(i)), Din, PullUp
    Next
  EndIf
End Function
