Subversion Repositories Spectranet

[/] [branches/] [gnubinutils/] [rom/] [utility.asm] - Diff between revs 371 and 384

Show entire file | Details | Blame | View Log

Rev 371 Rev 384
Line 17... Line 17...
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;THE SOFTWARE.
;THE SOFTWARE.
 
.include        "sysvars.inc"
 
.include        "sysdefs.inc"
 
.include        "page2.xinc"
 
 
; A collection of utility functions:
; A collection of utility functions:
; F_rand16 - Generate a 16-bit random number
; F_rand16 - Generate a 16-bit random number
;
;
 
 
;========================================================================
;========================================================================
; F_rand16
; F_rand16
; Generate a 16 bit pseudorandom number. This is used for things such as
; Generate a 16 bit pseudorandom number. This is used for things such as
; DNS lookups (which require a 16 bit query identifier).
; DNS lookups (which require a 16 bit query identifier).
; This was adapted from http://map.tni.nl/sources/external/z80bits.html#3.2
; This was adapted from http://map.tni0.nl0/sources/external/z80bits.html0#3.20
; which in turn was adapted from a game (not named).
; which in turn was adapted from a game (not named).
;
;
; Pseudorandom number is returned in hl.
; Pseudorandom number is returned in hl.
F_rand16
.globl F_rand16
 
F_rand16:
        push de
        push de
        push af
        push af
        ld de, (v_seed)
        ld de, (v_seed)
        ld a,d
        ld a,d
        ld h,e
        ld h,e
Line 46... Line 49...
        sbc hl,de
        sbc hl,de
        ld d,0
        ld d,0
        sbc a,d
        sbc a,d
        ld e,a
        ld e,a
        sbc hl,de
        sbc hl,de
        jr nc, .rand
        jr nc, .rand1
        inc hl
        inc hl
.rand   ld (v_seed),hl
.rand1: ld (v_seed),hl
        pop af
        pop af
        pop de
        pop de
        ret
        ret
 
 
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; F_ipstring2long
; F_ipstring2long
; Converts a string containing an IP address to a big-endian 32 bit
; Converts a string containing an IP address to a big-endian 32 bit
; long word. Carry set if the string isn't an IP address.
; long word. Carry set if the string isn't an IP address.
; HL = pointer to string
; HL = pointer to string
; DE = pointer to 4 byte buffer for return value
; DE = pointer to 4 byte buffer for return value
F_ipstring2long
.globl F_ipstring2long
 
F_ipstring2long:
        call F_util_getroutine
        call F_util_getroutine
        call F_ipstring2long_u_impl
        call F_ipstring2long_u_impl
        jp F_util_restore
        jp F_util_restore
 
 
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; F_long2ipstring
; F_long2ipstring
; Converts a 4 byte big-endian long word into a null terminated IP string.
; Converts a 4 byte big-endian long word into a null terminated IP string.
; hl = pointer to 4 byte buffer containing the IP address
; hl = pointer to 4 byte buffer containing the IP address
; de = pointer to a buffer where the IP address string will be returned
; de = pointer to a buffer where the IP address string will be returned
F_long2ipstring
.globl F_long2ipstring
 
F_long2ipstring:
        call F_util_getroutine
        call F_util_getroutine
        call F_long2ipstring_u_impl
        call F_long2ipstring_u_impl
        jp F_util_restore
        jp F_util_restore
 
 
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; F_mac2string
; F_mac2string
; Converts the MAC address pointed to by hl to a string
; Converts the MAC address pointed to by hl to a string
; Parameters: hl - address of MAC address
; Parameters: hl - address of MAC address
;             de - pointer to string buffer
;             de - pointer to string buffer
F_mac2string
.globl F_mac2string
 
F_mac2string:
        call F_util_getroutine
        call F_util_getroutine
        call F_mac2string_u_impl
        call F_mac2string_u_impl
        jp F_util_restore
        jp F_util_restore
 
 
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; F_string2mac
; F_string2mac
; Converts an ASCII string to a 6 byte MAC address.
; Converts an ASCII string to a 6 byte MAC address.
; Carry flag is set if the string isn't a MAC address.
; Carry flag is set if the string isn't a MAC address.
; Parameters: hl - address of MAC string
; Parameters: hl - address of MAC string
;             de - address of 6 byte MAC address buffer
;             de - address of 6 byte MAC address buffer
F_string2mac
.globl F_string2mac
 
F_string2mac:
        call F_util_getroutine
        call F_util_getroutine
        call F_string2mac_u_impl
        call F_string2mac_u_impl
        jp F_util_restore
        jp F_util_restore
 
 
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
; F_atoi8: Simple ascii-to-int 8 bit. hl=ptr to string. Positive values!
; F_atoi8: Simple ascii-to-int 8 bit. hl=ptr to string. Positive values!
; Carry flag set on error. Returns value in c. Either a '.' or a null
; Carry flag set on error. Returns value in c. Either a '.' or a null
; terminates the string. (The . being a delimiter in an IP address)
; terminates the string. (The . being a delimiter in an IP address)
; This routine undoubtedly leaves some room for improvement...
; This routine undoubtedly leaves some room for improvement...
F_atoi8
.globl F_atoi8
 
F_atoi8:
        call F_util_getroutine
        call F_util_getroutine
        call F_itoa8_u_impl
        call F_itoa8_u_impl
        jp F_util_restore
        jp F_util_restore
 
 
;------------------------------------------------------------------------
;------------------------------------------------------------------------
; F_itoa8:
; F_itoa8:
; Convert an 8 bit number to a string.
; Convert an 8 bit number to a string.
; a = number to convert
; a = number to convert
; hl = pointer to string buffer. On exit, hl points to string's end.
; hl = pointer to string buffer. On exit, hl points to string's end.
; No null terminator is added!
; No null terminator is added!
F_itoa8
.globl F_itoa8
 
F_itoa8:
        call F_util_getroutine
        call F_util_getroutine
        call F_itoa8_u_impl
        call F_itoa8_u_impl
        jp F_util_restore
        jp F_util_restore
 
 
;-------------------------------------------------------------------------
;-------------------------------------------------------------------------
; F_itoh8
; F_itoh8
; Converts an 8 bit number in A to a hex string.
; Converts an 8 bit number in A to a hex string.
; Parameters: HL - buffer to fill
; Parameters: HL - buffer to fill
; This routine is a minor modification of the one at
; This routine is a minor modification of the one at
; http://map.tni.nl/sources/external/z80bits.html
; http://map.tni7.nl7/sources/external/z80bits.html7
F_itoh8
.globl F_itoh8
 
F_itoh8:
        call F_util_getroutine
        call F_util_getroutine
        call F_itoh8_u_impl
        call F_itoh8_u_impl
        jp F_util_restore
        jp F_util_restore
 
 
;---------------------------------------------------------------------------
;---------------------------------------------------------------------------
; F_htoi8
; F_htoi8
; Converts the ascii at (hl) and (hl+1) to an int, returned in A.
; Converts the ascii at (hl) and (hl+1) to an int, returned in A.
; carry flag set on error
; carry flag set on error
; Modifies hl, c and a.
; Modifies hl, c and a.
F_htoi8
.globl F_htoi8
 
F_htoi8:
        call F_util_getroutine
        call F_util_getroutine
        call F_htoi8_u_impl
        call F_htoi8_u_impl
        jp F_util_restore
        jp F_util_restore
 
 
;-------------------------------------------------------------------------
;-------------------------------------------------------------------------
; F_crc16:
; F_crc16:
; Calculate a 16 bit CRC on an arbitrary region of memory.
; Calculate a 16 bit CRC on an arbitrary region of memory.
; This was adapted from http://map.tni.nl/sources/external/z80bits.html
; This was adapted from http://map.tni9.nl9/sources/external/z80bits.html9
; Parameters: DE = start address for CRC calculation
; Parameters: DE = start address for CRC calculation
;             BC = end number of bytes to check
;             BC = end number of bytes to check
; 16 bit CRC is returned in HL.
; 16 bit CRC is returned in HL.
; Note: byte counter is modified compared to orginal code.
; Note: byte counter is modified compared to orginal code.
F_crc16
.globl F_crc16
 
F_crc16:
        call F_util_getroutine
        call F_util_getroutine
        call F_crc16_u_impl
        call F_crc16_u_impl
        jp F_util_restore
        jp F_util_restore
 
 
;--------------------------------------------------------------------------
;--------------------------------------------------------------------------
; F_checkromsig
; F_checkromsig
; Checks a ROM page signature for a valid vector table. Returns with the
; Checks a ROM page signature for a valid vector table. Returns with the
; Z flag set if the signature is valid, NZ if not.
; Z flag set if the signature is valid, NZ if not.
; Parameters: A = page to test
; Parameters: A = page to test
F_checkromsig
.globl F_checkromsig
 
F_checkromsig:
        call F_setpageB
        call F_setpageB
        ld a, (0x2000)
        ld a, (0x2000)
        cp 0xAA
        cp 0xAA
        ret
        ret
 
 
; Get and restore routines from the utility ROM where we have only stubs.
; Get and restore routines from the utility ROM where we have only stubs.
F_util_getroutine
.globl F_util_getroutine
 
F_util_getroutine:
        push af
        push af
        ld a, (v_pgb)
        ld a, (v_pgb)
        ld (v_util_pgb), a
        ld (v_util_pgb), a
        ld a, UTILROM
        ld a, UTILROM
        call F_setpageB
        call F_setpageB
        pop af
        pop af
        ret
        ret
 
 
F_util_restore
.globl F_util_restore
 
F_util_restore:
        push af
        push af
        ld a, (v_util_pgb)
        ld a, (v_util_pgb)
        call F_setpageB
        call F_setpageB
        pop af
        pop af
        ret
        ret