cscg22-gearboy

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

mul.s (3926B)


      1;--------------------------------------------------------------------------
      2;  mul.s
      3;
      4;  Copyright (C) 2000, Michael Hope
      5;  Copyright (C) 2021-2022, Sebastian 'basxto' Riedel (sdcc@basxto.de)
      6;  Copyright (c) 2021, Philipp Klaus Krause
      7;
      8;  This library is free software; you can redistribute it and/or modify it
      9;  under the terms of the GNU General Public License as published by the
     10;  Free Software Foundation; either version 2, or (at your option) any
     11;  later version.
     12;
     13;  This library is distributed in the hope that it will be useful,
     14;  but WITHOUT ANY WARRANTY; without even the implied warranty of
     15;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     16;  GNU General Public License for more details.
     17;
     18;  You should have received a copy of the GNU General Public License 
     19;  along with this library; see the file COPYING. If not, write to the
     20;  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
     21;   MA 02110-1301, USA.
     22;
     23;  As a special exception, if you link this library with other files,
     24;  some of which are compiled with SDCC, to produce an executable,
     25;  this library does not by itself cause the resulting executable to
     26;  be covered by the GNU General Public License. This exception does
     27;  not however invalidate any other reasons why the executable file
     28;   might be covered by the GNU General Public License.
     29;--------------------------------------------------------------------------
     30
     31        ;; Originally from GBDK by Pascal Felber.
     32
     33        .module mul
     34        .area   _CODE
     35
     36.globl  __mulsuchar
     37.globl  __muluschar
     38.globl  __mulschar
     39.globl  __muluchar
     40.globl  __mulint
     41
     42; operands with different sign
     43
     44__mulsuchar:
     45        ld      c, a
     46        jr      signexte
     47
     48__muluschar:
     49        ld      c, e
     50        ld      e, a
     51
     52signexte:
     53        ld      a,e
     54        rla
     55        sbc     a,a
     56        ld      d,a
     57
     58        xor     a
     59        jr      .mul8
     60
     61__mulschar:
     62        ; Sign-extend before going in.
     63        ld      c,a
     64
     65        rla
     66        sbc     a,a
     67        ld      b,a
     68
     69        ld      a,e
     70        rla
     71        sbc     a,a
     72        ld      d,a
     73
     74__mulint:
     75        ;; 16-bit multiplication
     76        ;;
     77        ;; Entry conditions
     78        ;;   BC = multiplicand
     79        ;;   DE = multiplier
     80        ;;
     81        ;; Exit conditions
     82        ;;   BC = less significant word of product
     83        ;;
     84        ;; Register used: AF,BC,DE,HL
     85.mul16:
     86        ;; Let the smaller number loop
     87        ld      a,b
     88        cp      a,d
     89        jr      c, keep
     90        ;; d <= b
     91        ld      a, e
     92        ld      e, c
     93        ld      c, a
     94        ld      a, d
     95        ld      d, b
     96        ld      b, a
     97keep:
     98        ;; Optimise for the case when this side has 8 bits of data or
     99        ;; less.  This is often the case with support address calls.
    100        or      a
    101        jp      Z, .mul8
    102        
    103        ld      l,#0
    104        ld      b,#16
    105loop16:
    106        ;; Taken from z88dk, which originally borrowed from the
    107        ;; Spectrum rom.
    108        add     hl,hl
    109        rl      c
    110        rla                     ;DLE 27/11/98
    111        jr      NC,skip16
    112        add     hl,de
    113skip16:
    114        dec     b
    115        jr      NZ,loop16
    116
    117        ;; Return in bc
    118        ld      c,l
    119        ld      b,h
    120
    121        ret
    122
    123__muluchar:
    124        ld      c, a
    125        xor     a
    126        ;; Clear the top
    127        ld      d, a
    128
    129        ;; Version that uses an 8bit multiplicand
    130        ;;
    131        ;; Entry conditions
    132        ;;   C  = multiplicand
    133        ;;   DE = multiplier
    134        ;;   A  = 0
    135        ;;
    136        ;; Exit conditions
    137        ;;   BC = less significant word of product
    138        ;;
    139        ;; Register used: AF,BC,DE,HL
    140.mul8:
    141        ld      l,a
    142        ld      b,#8
    143        ld      a,c
    144loop8:
    145        add     hl,hl
    146        rla
    147        jr      NC,skip8
    148        add     hl,de
    149skip8:
    150        dec     b
    151        jr      NZ,loop8
    152
    153        ;; Return in bc
    154        ld      c,l
    155        ld      b,h
    156
    157        ret
    158