cscg22-gearboy

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

sgb.s (2994B)


      1        .include        "global.s"
      2
      3        .area   _CODE
      4
      5        ;; Check if running on SGB
      6        ;;   Set A to 0xFF when running on SGB
      7        ;;   Clear A when running on DMG
      8.sgb_check::
      9_sgb_check::
     10        PUSH    BC
     11        ADD     SP, #-0x10
     12        LDHL    SP, #0
     13        LD      C, #0x10
     14        XOR     A
     15        RST     0x28
     16        LDHL    SP,#0
     17        LD      A,#((.MLT_REQ << 3) | 1)
     18        LD      (HL+),A
     19        LD      A,#0x03
     20        LD      (HL-),A
     21        CALL    .sgb_transfer
     22        LD      C,#.P1
     23        LD      A,#(.P14 | .P15)
     24        LDH     (C),A
     25        LDH     A,(C)
     26        LDH     A,(C)
     27        LD      E, #4
     28        LDH     A,(C)           ; read delayed
     293$:
     30        LD      B, A
     31
     32        LD      A,#.P15
     33        LDH     (C),A
     34        LDH     A,(C)
     35        LDH     A,(C)
     36
     37        LD      A,#.P14
     38        LDH     (C),A
     39        LDH     A,(C)
     40        LDH     A,(C)
     41
     42        LD      A,#(.P14 | .P15)
     43        LDH     (C),A
     44        LDH     A,(C)
     45        LDH     A,(C)
     46        LDH     A,(C)
     47        LDH     A,(C)           ; read delayed
     48        CP      B
     49        JR      NZ,1$
     50        
     51        DEC     E
     52        JR      NZ,3$
     532$:
     54        LD      E,#0
     55        JR      4$
     561$:
     57        LDHL    SP,#1
     58        LD      A,#0x00
     59        LD      (HL-),A
     60        CALL    .sgb_transfer
     61        LD      E,#1
     624$:
     63        ADD     SP,#0x10
     64        POP     BC
     65        RET
     66
     67_sgb_transfer::
     68        LDHL    SP,#2
     69        LD      A,(HL+)
     70        LD      H,(HL)
     71        LD      L,A
     72        
     73.sgb_transfer::
     74        PUSH    BC
     75        LD      A,(HL)          ; Top of command data
     76        AND     #0x03
     77        JR      Z,6$
     781$:
     79        PUSH    AF
     80        LD      C,#.P1
     81        LDH     (C),A           ; Send reset
     82        LD      A,#(.P14 | .P15)
     83        LDH     (C),A
     84        LD      B,#0x10         ; Set counter to transfer 16 byte
     852$:     LD      E,#0x08         ; Set counter to transfer 8 bit
     86        LD      A,(HL+)
     87        LD      D,A
     88
     893$:
     90        SRL     D
     91        LD      A,#.P14         ; P14 = high, P15 = low  (output "1")
     92        JR      C,4$
     93        LD      A,#.P15         ; P14 = low,  P15 = high (output "0")
     944$:
     95        LDH     (C),A
     96        LDH     A,(C)           ; delay
     97        LDH     A,(C)
     98        LD      A,#(.P14 | .P15); P14 = high, P15 = high
     99        LDH     (C),A
    100        LDH     A,(C)           ; delay
    101        LDH     A,(C)
    102        DEC     E
    103        JR      NZ,3$
    104
    105        DEC     B
    106        JR      NZ,2$
    107        
    108        LD      A,#.P15         ; 129th bit "0" output
    109        LDH     (C),A
    110        LDH     A,(C)           ; delay
    111        LDH     A,(C)
    112        LD      A,#(.P14 | .P15)
    113        LDH     (C),A
    114
    115        LD      DE,#8400		; was: 7000
    1165$:
    117        LDH     A,(.P1)         ; 3 +
    118        DEC     DE              ; 2 +
    119        LD      A,D             ; 1 +
    120        OR      E               ; 1 +
    121        JR      NZ,5$           ; 3 = 10 cycles
    122
    123        POP     AF
    124        DEC     A
    125        JR      NZ, 1$
    1266$:
    127        POP     BC
    128        RET