cscg22-gearboy

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

bcd.s (6103B)


      1;--------------------------------------------------------------------------
      2;  bcd.s
      3;
      4;  Copyright (C) 2020, Tony Pavlov
      5;
      6;  This library is free software; you can redistribute it and/or modify it
      7;  under the terms of the GNU General Public License as published by the
      8;  Free Software Foundation; either version 2, or (at your option) any
      9;  later version.
     10;
     11;  This library is distributed in the hope that it will be useful,
     12;  but WITHOUT ANY WARRANTY; without even the implied warranty of
     13;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     14;  GNU General Public License for more details.
     15;
     16;  You should have received a copy of the GNU General Public License 
     17;  along with this library; see the file COPYING. If not, write to the
     18;  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
     19;   MA 02110-1301, USA.
     20;
     21;  As a special exception, if you link this library with other files,
     22;  some of which are compiled with SDCC, to produce an executable,
     23;  this library does not by itself cause the resulting executable to
     24;  be covered by the GNU General Public License. This exception does
     25;  not however invalidate any other reasons why the executable file
     26;   might be covered by the GNU General Public License.
     27;--------------------------------------------------------------------------
     28
     29        .module bcd
     30
     31        .area   _HOME
     32
     33; void uint2bcd(uint16_t i, BCD * value) __naked
     34_uint2bcd::
     35            lda     HL, 2(SP)
     36            ld      A, (HL+)        ; DE: uint
     37            ld      E, A
     38            ld      A, (HL+)
     39            ld      D, A
     40            ld      A, (HL+)
     41            ld      H, (HL)
     42            ld      L, A            ; HL: dest
     43
     44            push    BC
     45
     46            ; clear value
     47            xor     A
     48            ld      BC, #-4
     49            
     50            ld      (HL+), A
     51            ld      (HL+), A
     52            ld      (HL+), A
     53            ld      (HL+), A
     54            add     HL, BC
     55
     56            ld      B, #16
     571$:            
     58            sla     E
     59            rl      D
     60            
     61            ld      A, (HL)
     62            adc     A
     63            daa
     64            ld      (HL+), A
     65            ld      A, (HL)
     66            adc     A
     67            daa
     68            ld      (HL+), A
     69            ld      A, (HL)
     70            adc     A
     71            daa
     72            ld      (HL-), A
     73            
     74            dec     HL
     75            
     76            dec     B
     77            jr      NZ, 1$
     78
     79            pop     BC
     80            ret
     81
     82;void bcd_add(BCD * sour, BCD * value) __naked
     83_bcd_add::
     84            lda     HL, 5(SP)
     85            ld      D, (HL)         
     86            dec     HL
     87            ld      E, (HL)         ; DE: value
     88            dec     HL
     89            ld      A, (HL-)
     90            ld      L, (HL)
     91            ld      H, A            ; HL: sour
     92
     93            or      A               ; clear C, HC
     94
     95            ld      A,(DE)
     96            add     (HL)
     97            daa
     98            ld      (HL+), A
     99            inc     DE
    100            
    101            ld      A,(DE)
    102            adc     (HL)
    103            daa
    104            ld      (HL+), A
    105            inc     DE
    106            
    107            ld      A,(DE)
    108            adc     (HL)
    109            daa
    110            ld      (HL+), A
    111            inc     DE
    112            
    113            ld      A,(DE)
    114            adc     (HL)
    115            daa
    116            ld      (HL+), A
    117            
    118            ret
    119
    120; void bcd_sub(BCD * sour, BCD * value) __naked
    121_bcd_sub::
    122            lda     HL, 2(SP)
    123            ld      E, (HL)         
    124            inc     HL
    125            ld      D, (HL)         ; DE: sour
    126            inc     HL
    127            ld      A, (HL+)
    128            ld      H, (HL)
    129            ld      L, A            ; HL: value
    130
    131            or      A               ; clear C, HC
    132
    133            ld      A,(DE)
    134            sub     (HL)
    135            daa
    136            ld      (DE), A
    137            inc     DE
    138            inc     HL
    139            
    140            ld      A,(DE)
    141            sbc     (HL)
    142            daa
    143            ld      (DE), A
    144            inc     DE
    145            inc     HL
    146            
    147            ld      A,(DE)
    148            sbc     (HL)
    149            daa
    150            ld      (DE), A
    151            inc     DE
    152            inc     HL
    153            
    154            ld      A,(DE)
    155            sbc     (HL)
    156            daa
    157            ld      (DE), A
    158            
    159            ret
    160
    161;uint8_t bcd2text(BCD * bcd, uint8_t tile_offset, uint8_t * buffer) __naked
    162_bcd2text::
    163            push    BC
    164            
    165            lda     HL, 4(SP)
    166            ld      E, (HL)         
    167            inc     HL
    168            ld      D, (HL)         ; DE: bcd
    169            inc     HL
    170            ld      C, (HL)         ; C: digit offset
    171            inc     HL
    172            ld      A, (HL+)
    173            ld      H, (HL)
    174            ld      L, A            ; HL: buffer
    175
    176            inc     DE
    177            inc     DE
    178            inc     DE
    179
    180            ld      B, #0x0f
    181
    182            ld      A, (DE)
    183            swap    A
    184            and     B
    185            add     C
    186            ld      (HL+), A
    187            ld      A, (DE)
    188            and     B
    189            add     C
    190            ld      (HL+), A
    191            dec     DE
    192            
    193            ld      A, (DE)
    194            swap    A
    195            and     B
    196            add     C
    197            ld      (HL+), A
    198            ld      A, (DE)
    199            and     B
    200            add     C
    201            ld      (HL+), A
    202            dec     DE
    203
    204            ld      A, (DE)
    205            swap    A
    206            and     B
    207            add     C
    208            ld      (HL+), A
    209            ld      A, (DE)
    210            and     B
    211            add     C
    212            ld      (HL+), A
    213            dec     DE
    214            
    215            ld      A, (DE)
    216            swap    A
    217            and     B
    218            add     C
    219            ld      (HL+), A
    220            ld      A, (DE)
    221            and     B
    222            add     C
    223            ld      (HL+), A
    224            dec     DE
    225            
    226            xor     A
    227            ld      (HL+), A
    228            
    229            ld      D, A
    230            ld      E, #0x08
    231            
    232            pop     BC
    233            ret