cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

input.s (16125B)


      1        .include        "global.s"
      2
      3        ;; Note that while gets uses a pointer, the pointer had better
      4        ;; be in non-banked RAM else bad things will happen.
      5        
      6        .globl  .copy_vram
      7        .globl  .set_xy_wtt
      8        .globl  .mv_sprite
      9        .globl  .set_sprite_prop
     10        .globl  .set_sprite_tile
     11        .globl  .jpad
     12        .globl  .padup
     13
     14        .MINMSPOSX      = 0x02  ; In tiles
     15        .MINMSPOSY      = 0x0A
     16        .MAXMSPOSX      = 0x11
     17        .MAXMSPOSY      = 0x0F
     18        .INIMSPOSX      = .MINMSPOSX
     19        .INIMSPOSY      = .MINMSPOSY
     20
     21        .KBDWINPOSY     = 0x08  ; In tiles
     22        .KBDSIZE        = 0x1006
     23
     24        .MSOFFSETX      = 0x0C  ; In pixels
     25        .MSOFFSETY      = 0x14
     26
     27        .MINACCEL       = 0x0800
     28        .MAXACCEL       = 0x0100
     29
     30        .CR     = 0x0A          ; Unix
     31;       .CR     = 0x0D          ; Dos
     32
     33        .globl  .tmode_out      ; From 'output.s'
     34        .globl  .put_char
     35        .globl  .del_char
     36        .globl  .cury
     37
     38        .area   _INPUT_HEADER (ABS)
     39
     40        .org    .MODE_TABLE+4*.T_MODE_INOUT
     41        JP      .tmode_inout
     42
     43        .module Terminal
     44
     45        .area   _DATA
     46
     47.msx:                           ; Mouse position
     48        .ds     0x01
     49.msy:
     50        .ds     0x01
     51.msacc:                         ; Mouse acceleration
     52        .ds     0x02
     53.msstate:                       ; Mouse state
     54        .ds     0x01
     55.mschanged:                     ; Did the mouse move?
     56        .ds     0x01
     57.string_len:                    ; Used length of input buffer
     58        .ds     0x01
     59
     60        .area   _HOME
     61
     62        ;; Enter text mode with input
     63.tmode_inout::
     64        DI                      ; Disable interrupts
     65
     66        ;; Turn the screen off
     67        LDH     A,(.LCDC)
     68        AND     #LCDCF_ON
     69        JR      Z,1$
     70
     71        ;; Turn the screen off
     72        CALL    .display_off
     731$:
     74
     75        LD      A,(.mode)
     76        AND     #.T_MODE
     77        CALL    Z,.tmode_out
     78
     79        LD      BC,#.tp1        ; Move pointer
     80        LD      HL,#0x8000
     81        LD      DE,#.endtp1-.tp1
     82        CALL    .copy_vram
     83
     84        LD      A,#<.MINACCEL   ; Acceleration
     85        LD      (.msacc),A
     86        LD      A,#>.MINACCEL
     87        LD      (.msacc+1),A
     88
     89        ;; Initialize window
     90        LD      BC,#.frame_tiles
     91        LD      DE,#0x140A              ; 0x140A
     92        LD      HL,#0
     93        CALL    set_recoded_win_tiles
     94
     95        LD      BC,#.kbdtable
     96        LD      DE,#.KBDSIZE
     97        LD      HL,#(0x20 * 2 + 2)      ; X=2, Y=2
     98        CALL    set_recoded_win_tiles
     99        
    100        LD      A,#.MINWNDPOSX
    101        LDH     (.WX),A
    102        LD      A,#.MAXWNDPOSY  ; Hide window
    103        LDH     (.WY),A
    104
    105        XOR     A
    106        ;; Initialize sprite
    107        LD      C,A             ; Sprite 0x00
    108        LD      D,A             ; Default sprite properties
    109        CALL    .set_sprite_prop
    110        XOR     A
    111        LD      C,A             ; Sprite 0x00
    112        LD      D,A             ; Tile 0x00
    113        CALL    .set_sprite_tile
    114        LD      A,#0b00101100
    115        LDH     (.OBP0),A
    116
    117        ;; Turn the screen on
    118        LD      A,#(LCDCF_ON | LCDCF_WIN9C00 | LCDCF_WINOFF | LCDCF_BG8800 | LCDCF_BG9800 | LCDCF_OBJ8 | LCDCF_OBJOFF | LCDCF_BGON)
    119        LDH     (.LCDC),A
    120
    121        LD      A,#.T_MODE_INOUT
    122        LD      (.mode),A
    123
    124        EI                      ; Enable interrupts
    125
    126        RET
    127
    128set_recoded_win_tiles::
    129        LDH     A,(.LCDC)
    130        AND     #LCDCF_WIN9C00
    131        JR      Z,2$
    132        LD      A,#0x9C
    133        JR      3$
    1342$:
    135        LD      A,#0x98
    1363$:
    137        ADD     H
    138        LD      H,A
    139        PUSH    DE
    140        PUSH    HL
    1414$:     
    142        LD      A,(BC)
    143        INC     BC
    144        PUSH    BC
    145        PUSH    HL
    146        
    147        LD      C,A
    148        LD      HL,#(font_current+1)    ; font_current+sfont_handle_font
    149        LD      A,(HL+)
    150        LD      H,(HL)
    151        LD      L,A
    152        LD      A,(HL+)
    153        AND     #3
    154        CP      #2                      ; FONT_NOENCODING
    155        JR      Z,5$
    156        INC     HL
    157        LD      B,#0
    158        ADD     HL,BC
    159        LD      C,(HL)
    1605$:
    161        POP     HL
    162        
    163        WAIT_STAT
    164        LD      A,C
    165        LD      (HL+),A
    166        
    167        POP     BC
    168        
    169        DEC     D
    170        JR      NZ,4$
    171        
    172        POP     HL
    173        POP     DE
    174        
    175        LD      A,L
    176        ADD     #0x20
    177        LD      L,A
    178        ADC     H
    179        SUB     L
    180        LD      H,A
    181        DEC     E
    182
    183        PUSH    DE
    184        PUSH    HL
    185        JR      NZ,4$
    186
    187        ADD     SP,#4
    188        RET
    189        
    190        ;; Prompt the user for a char and return it in A
    191.get_char:
    192        PUSH    BC
    193        PUSH    DE
    194        PUSH    HL
    195        CALL    .show_kbd
    196        CALL    .show_mouse
    1971$:
    198        CALL    .track_mouse
    199        CALL    .update_mouse
    200        CALL    .jpad
    201        LD      D,A
    202        AND     #.A             ; Is A pressed ?
    203        JP      Z,1$
    204
    205        LD      A,(.msy)        ; Look for char under the mouse
    206        SUB     #.MINMSPOSY
    207        JR      Z,12$
    208        LD      E,A
    209        XOR     A
    21011$:
    211        ADD     #.MAXMSPOSX-.MINMSPOSX+1
    212        DEC     E
    213        JR      NZ,11$
    21412$:
    215        LD      E,A
    216        LD      A,(.msx)
    217        SUB     #.MINMSPOSX
    218        ADD     E
    219        LD      HL,#.kbdtable
    220        LD      B,#0x00
    221        LD      C,A
    222        ADD     HL,BC
    223        LD      B,(HL)
    224
    225        CALL    .hide_mouse
    226        CALL    .hide_kbd
    227        LD      A,B
    228
    229        POP     HL
    230        POP     DE
    231        POP     BC
    232        RET
    233
    234        ;; Prompt the user for a string and store it in (HL)
    235.get_string:
    236        PUSH    BC
    237        PUSH    DE
    238        PUSH    HL
    239        CALL    .show_kbd
    240        CALL    .show_bkg
    241        CALL    .show_mouse
    242        XOR     A
    243        LD      (.string_len),A
    2441$:
    245        CALL    .track_mouse
    246        CALL    .update_mouse
    247        CALL    .jpad
    248        LD      D,A
    249        AND     #.A             ; Is A pressed ?
    250        JP      NZ,10$
    251        LD      A,D
    252        AND     #.B             ; Is B pressed ?
    253        JP      NZ,20$
    254        LD      A,D
    255        AND     #.SELECT        ; Is SELECT pressed ?
    256        JP      NZ,30$
    257        LD      A,D
    258        AND     #.START         ; Is START pressed ?
    259        JR      Z,1$
    260        CALL    .padup          ; Wait for button to be depressed
    261
    262        LD      A,#.CR
    263        CALL    .put_char
    264        LD      (HL),#0x00
    265        CALL    .hide_mouse
    266        CALL    .hide_bkg
    267        CALL    .hide_kbd
    268        POP     HL
    269        POP     DE
    270        POP     BC
    271        RET
    272
    27310$:
    274        ;; Insert a character at cursor position
    275        LD      A,(.string_len) ; Check buffer length
    276;       CP      #.BUFLEN-1      ; Keep 1 char for EOS
    277;       JR      Z,13$
    278        INC     A
    279        LD      (.string_len),A ; Update it
    280        LD      A,(.msy)        ; Look for char under the mouse
    281        SUB     #.MINMSPOSY
    282        JR      Z,12$
    283        LD      E,A
    284        XOR     A
    28511$:
    286        ADD     #.MAXMSPOSX-.MINMSPOSX+1
    287        DEC     E
    288        JR      NZ,11$
    28912$:
    290        LD      E,A
    291        LD      A,(.msx)
    292        SUB     #.MINMSPOSX
    293        ADD     E
    294        PUSH    HL
    295        LD      HL,#.kbdtable
    296        LD      B,#0x00
    297        LD      C,A
    298        ADD     HL,BC
    299        LD      A,(HL)
    300        POP     HL
    301        LD      (HL+),A         ; Add it into input buffer
    302        CALL    .put_char       ; Print it
    303        CALL    .show_bkg       ; Ensure the text is not hidden
    30413$:
    305        CALL    .padup          ; Wait for button to be depressed
    306        JP      1$
    307
    30820$:
    309        ;; Delete a character at cursor position
    310        LD      A,(.string_len) ; Is there any char in the buffer ?
    311        OR      A
    312        JR      Z,21$
    313        DEC     A               ; Yes
    314        LD      (.string_len),A ; Update buffer length
    315        DEC     HL
    316        CALL    .del_char
    31721$:
    318        CALL    .padup          ; Wait for button to be depressed
    319        JP      1$
    320
    32130$:
    322        CALL    .hide_mouse
    323        CALL    .hide_bkg
    324        CALL    .hide_kbd
    325        CALL    .padup          ; Wait for button to be depressed
    326        CALL    .show_kbd
    327        CALL    .show_bkg
    328        CALL    .show_mouse
    329        JP      1$
    330
    331.show_kbd:
    332        PUSH    BC
    333        PUSH    DE
    334        LDH     A,(.LCDC)
    335        OR      #LCDCF_WINON    ; Window = On
    336        LDH     (.LCDC),A
    337        LD      A,#.MAXWNDPOSY  ; Show window
    3381$:
    339        BIT     0,A             ; Wait for VBL every 2 pixels (slow down)
    340        JR      NZ,2$
    341        LD      B,A
    342        CALL    .wait_vbl_done
    343        LD      A,B
    3442$:
    345        LDH     (.WY),A
    346        CP      #.KBDWINPOSY*0x08
    347        JR      Z,99$
    348        DEC     A
    349        JR      1$
    35099$:
    351        POP     DE
    352        POP     BC
    353        RET
    354
    355.hide_kbd:
    356        PUSH    BC
    357        PUSH    DE
    358        LD      A,#.KBDWINPOSY*0x08+1
    3591$:                             ; Hide window
    360        BIT     0,A             ; Wait for VBL every 2 pixels (slow down)
    361        JR      Z,2$
    362        LD      B,A
    363        CALL    .wait_vbl_done
    364        LD      A,B
    3652$:
    366        LDH     (.WY),A
    367        CP      #.MAXWNDPOSY
    368        JR      Z,3$
    369        INC     A
    370        JR      1$
    3713$:
    372        LDH     A,(.LCDC)
    373        AND     #~LCDCF_WINON   ; Window = Off
    374        LDH     (.LCDC),A
    375        POP     DE
    376        POP     BC
    377        RET
    378
    379.show_bkg:
    380        PUSH    BC
    381        PUSH    DE
    382        LDH     A,(.SCY)
    383        LD      D,A
    384        LD      A,(.cury)
    385        SUB     #.KBDWINPOSY-1
    386        JR      C,99$
    387        JR      Z,99$
    388        SLA     A               ; A = A * 8
    389        SLA     A
    390        SLA     A
    391        SUB     D
    392        JR      C,99$
    393        JR      Z,99$
    394        LD      C,A
    395        LDH     A,(.SCY)
    3961$:
    397        BIT     0,A             ; Wait for VBL every 2 pixels (slow down)
    398        JR      Z,2$
    399        LD      B,A
    400        CALL    .wait_vbl_done
    401        LD      A,B
    4022$:
    403        INC     A
    404        LDH     (.SCY),A
    405        DEC     C
    406        JR      Z,99$
    407        JR      1$
    40899$:
    409        POP     DE
    410        POP     BC
    411        RET
    412
    413.hide_bkg:
    414        LDH     A,(.SCY)
    415        OR      A
    416        RET     Z
    417        PUSH    BC
    418        PUSH    DE
    4191$:
    420        BIT     0,A             ; Wait for VBL every 2 pixels (slow down)
    421        JR      Z,2$
    422        LD      B,A
    423        CALL    .wait_vbl_done
    424        LD      A,B
    4252$:
    426        DEC     A
    427        LDH     (.SCY),A
    428        JR      Z,99$
    429        JR      1$
    43099$:
    431        POP     DE
    432        POP     BC
    433        RET
    434
    435.show_mouse:
    436        LD      A,#.INIMSPOSX
    437        LD      (.msx),A
    438        LD      A,#.INIMSPOSY
    439        LD      (.msy),A
    440        CALL    .set_mouse
    441        LDH     A,(.LCDC)
    442        OR      #LCDCF_OBJON    ; OBJ = On
    443        LDH     (.LCDC),A
    444        RET
    445
    446.hide_mouse:
    447        LDH     A,(.LCDC)
    448        AND     #~LCDCF_OBJON   ; OBJ = Off
    449        LDH     (.LCDC),A
    450        RET
    451
    452.track_mouse:
    453        PUSH    BC
    454        PUSH    DE
    455        PUSH    HL
    456        XOR     A
    457        LD      (.mschanged),A  ; Default to no change
    458        CALL    .jpad
    459        LD      D,A
    460
    461        LD      HL,#.msstate
    462        AND     #.UP+.DOWN+.LEFT+.RIGHT
    463        JR      NZ,1$
    464        LD      (HL),#0x00      ; Reset state
    465        JP      99$
    4661$:
    467        LD      A,(HL)
    468        LD      (HL),#0x01      ; Set state
    469        OR      A               ; Was it 0 ?
    470        LD      HL,#.msacc      ; Acceleration
    471        JR      NZ,2$
    472                                ; Yes
    473        LD      (HL),#<.MINACCEL
    474        INC     HL
    475        LD      (HL),#>.MINACCEL
    476        JR      4$              ; Update position
    4772$:
    478        LD      A,(HL+)
    479        LD      C, A
    480        LD      B,(HL)
    481        DEC     BC
    482        LD      A,B
    483        OR      C
    484        JR      Z,3$
    485        LD      (HL),B
    486        DEC     HL
    487        LD      (HL),C
    488        JP      99$
    4893$:                             ; Set new acceleration to maximum
    490        LD      (HL),#>.MAXACCEL
    491        DEC     HL
    492        LD      (HL),#<.MAXACCEL
    4934$:                             ; Update position
    494        LD      A,#0x01
    495        LD      (.mschanged),A
    496        LD      A,D
    497        AND     #.UP            ; Is UP pressed ?
    498        JR      Z,6$
    499        LD      A,(.msy)
    500        CP      #.MINMSPOSY
    501        JR      Z,5$
    502        DEC     A
    503        LD      (.msy),A
    504        JR      6$
    5055$:
    506        LD      A,#.MAXMSPOSY
    507        LD      (.msy),A
    5086$:
    509        LD      A,D
    510        AND     #.DOWN          ; Is DOWN pressed ?
    511        JR      Z,8$
    512        LD      A,(.msy)
    513        CP      #.MAXMSPOSY
    514        JR      Z,7$
    515        INC     A
    516        LD      (.msy),A
    517        JR      8$
    5187$:
    519        LD      A,#.MINMSPOSY
    520        LD      (.msy),A
    5218$:
    522        LD      A,D
    523        AND     #.LEFT          ; Is LEFT pressed ?
    524        JR      Z,10$
    525        LD      A,(.msx)
    526        CP      #.MINMSPOSX
    527        JR      Z,9$
    528        DEC     A
    529        LD      (.msx),A
    530        JR      10$
    5319$:
    532        LD      A,#.MAXMSPOSX
    533        LD      (.msx),A
    53410$:
    535        LD      A,D
    536        AND     #.RIGHT         ; Is RIGHT pressed ?
    537        JR      Z,99$
    538        LD      A,(.msx)
    539        CP      #.MAXMSPOSX
    540        JR      Z,11$
    541        INC     A
    542        LD      (.msx),A
    543        JR      99$
    54411$:
    545        LD      A,#.MINMSPOSX
    546        LD      (.msx),A
    54799$:
    548        POP     HL
    549        POP     DE
    550        POP     BC
    551        RET
    552
    553.update_mouse:
    554        LD      A,(.mschanged)  ; Did it change ?
    555        OR      A
    556        RET     Z               ; No
    557.set_mouse:
    558        PUSH    BC
    559        PUSH    DE
    560        PUSH    HL
    561        LD      C,#0x00         ; Sprite 0x00
    562        LD      A,(.msx)
    563        SLA     A               ; A = A * 8
    564        SLA     A
    565        SLA     A
    566        ADD     #.MSOFFSETX
    567        LD      D,A
    568        LD      A,(.msy)
    569        SLA     A               ; A = A * 8
    570        SLA     A
    571        SLA     A
    572        ADD     #.MSOFFSETY
    573        LD      E,A
    574        CALL    .mv_sprite
    575        POP     HL
    576        POP     DE
    577        POP     BC
    578        RET
    579
    580_getchar::
    581        LD      A,(.mode)
    582        CP      #.T_MODE_INOUT
    583        JR      Z,1$
    584        PUSH    BC
    585        CALL    .tmode_inout
    586        POP     BC
    5871$:
    588        CALL    .get_char
    589        LD      E,A
    590        RET
    591
    592_gets::
    593        LD      A,(.mode)
    594        CP      #.T_MODE_INOUT
    595        JR      Z,1$
    596        PUSH    BC
    597        CALL    .tmode_inout
    598        POP     BC
    5991$:
    600        LDA     HL,2(SP)        ; Skip return address
    601        LD      A,(HL+)
    602        LD      H,(HL)          ; HL = s
    603        LD      L,A
    604        PUSH    HL
    605        CALL    .get_string
    606        POP     DE
    607        RET
    608
    609.tp1:
    610
    611.pointers:
    612
    613        ; Tile 0x00
    614        .byte   0xFF,0xFF,0xFE,0x82,0xFC,0x84,0xFC,0x84,0xFE,0x82,0xFF,0xB1,0xCF,0xC9,0x87,0x87
    615
    616.endtp1:
    617
    618.frame_tiles:
    619        .byte   0x1C,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x1D
    620        .byte   0x0F,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x0F
    621        .byte   0x0F,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x0F
    622        .byte   0x0F,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x0F
    623        .byte   0x0F,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x0F
    624        .byte   0x0F,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x0F
    625        .byte   0x0F,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x0F
    626        .byte   0x0F,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x0F
    627        .byte   0x0F,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x0F
    628        .byte   0x1E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x1F
    629
    630.kbdtable:
    631        .db     0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27
    632        .db     0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F
    633        .ascii  "0123456789:"
    634        .db     0x3B            
    635        .ascii  "<=>?"
    636        .ascii  "@ABCDEFGHIJKLMNO"
    637        .ascii  "PQRSTUVWXYZ[\]^_"
    638        .ascii  "`abcdefghijklmno"
    639        .ascii  "pqrstuvwxyz{|}~ "