| 1 |
2 |
sinclairrf |
; Copyright 2013, Sinclair R.F., Inc.
|
| 2 |
|
|
;
|
| 3 |
|
|
; Character manipulation functions
|
| 4 |
|
|
|
| 5 |
|
|
.IFNDEF C__INCLUDED__CHAR_S__
|
| 6 |
|
|
.constant C__INCLUDED__CHAR_S__ 0
|
| 7 |
|
|
|
| 8 |
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
| 9 |
|
|
;
|
| 10 |
3 |
sinclairrf |
; Convert to and from binary.
|
| 11 |
2 |
sinclairrf |
;
|
| 12 |
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
| 13 |
|
|
|
| 14 |
3 |
sinclairrf |
; Convert a single binary digit to its byte value. Return 0x00 on success and
|
| 15 |
|
|
; 0xFF on failure.
|
| 16 |
|
|
; ( u_binary_n - u f )
|
| 17 |
|
|
.function char__binary_to_byte
|
| 18 |
|
|
'0' -
|
| 19 |
|
|
dup 0xFE & .jumpc(error)
|
| 20 |
|
|
.return(0)
|
| 21 |
|
|
:error .return(0xFF)
|
| 22 |
|
|
|
| 23 |
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
| 24 |
|
|
;
|
| 25 |
|
|
; Convert to and from hex.
|
| 26 |
|
|
;
|
| 27 |
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
| 28 |
|
|
|
| 29 |
2 |
sinclairrf |
; Convert a byte to its 2-digit hex representation with the digit for the most
|
| 30 |
3 |
sinclairrf |
; significant nibble at the top of the data stack.
|
| 31 |
2 |
sinclairrf |
; ( u - u_hex_lsn u_hex_msn )
|
| 32 |
|
|
.function char__byte_to_2hex
|
| 33 |
3 |
sinclairrf |
; ( u - u u_hex_lsn )
|
| 34 |
2 |
sinclairrf |
dup 0x0F .call(char__nibble_to_hex,&)
|
| 35 |
3 |
sinclairrf |
; ( u u_hex_lsn - u_hex_lsn u_hex_msn )
|
| 36 |
2 |
sinclairrf |
swap 0>> 0>> 0>> .call(char__nibble_to_hex,0>>)
|
| 37 |
|
|
.return
|
| 38 |
|
|
|
| 39 |
3 |
sinclairrf |
; Convert a byte to the minimal 1 or 2 digit hex representation with the digit
|
| 40 |
|
|
; for the most significant nibble at the top of the data stack.
|
| 41 |
|
|
; ( u - u_hex_lsn u_hex_msn ) or ( u - u_hex_lsn )
|
| 42 |
|
|
.function char__byte_to_hex
|
| 43 |
|
|
dup 0xF0 & .jumpc(include_msn)
|
| 44 |
|
|
.call(char__nibble_to_hex) .return
|
| 45 |
|
|
:include_msn
|
| 46 |
|
|
.call(char__byte_to_2hex) .return
|
| 47 |
|
|
|
| 48 |
|
|
; Convert a 4 byte value to its 8-digit hexadecimal representation.
|
| 49 |
|
|
; ( u_LSB u u u_MSB - )
|
| 50 |
|
|
.function char__4byte_to_8hex
|
| 51 |
|
|
>r >r >r >r
|
| 52 |
|
|
${4-1} :loop r> swap >r .call(char__byte_to_2hex) r> .jumpc(loop,1-)
|
| 53 |
|
|
.return(drop)
|
| 54 |
|
|
|
| 55 |
2 |
sinclairrf |
; Convert a nibble between 0x00 and 0x0F inclusive to it hex digit.
|
| 56 |
|
|
; ( u - u_hex_n )
|
| 57 |
|
|
.function char__nibble_to_hex
|
| 58 |
|
|
0x09 over - 0x80 & 0<> ${ord('A')-ord('9')-1} & + '0' .return(+)
|
| 59 |
|
|
|
| 60 |
|
|
; Convert two hex digits to their byte value. Return 0x00 on success and 0xFF
|
| 61 |
|
|
; on failure.
|
| 62 |
|
|
; ( u_hex_lsn u_hex_msn - u f )
|
| 63 |
|
|
.function char__2hex_to_byte
|
| 64 |
|
|
; convert the msn to its position and save the error indication
|
| 65 |
|
|
; ( u_hex_lsn u_hex_lsn - u_hex_msn u_msn ) r:( - f_msn )
|
| 66 |
|
|
.call(char__hex_to_nibble) >r <<0 <<0 <<0 <<0
|
| 67 |
|
|
; ( u_hex_lsn u_msn - u ) r:( f_msn - f_lsn f_msn )
|
| 68 |
|
|
; convert the lsn to its position, save the error indication, and combine the two nibble conversions
|
| 69 |
|
|
.call(char__hex_to_nibble,swap) >r or
|
| 70 |
|
|
; compute the return status and return
|
| 71 |
|
|
; ( u - u f ) r:( f_lsn f_msn - )
|
| 72 |
|
|
r> r> .return(or)
|
| 73 |
|
|
|
| 74 |
|
|
; Convert a single hex digit to its nibble value. Return 0x00 on success and
|
| 75 |
|
|
; 0xFF on failure.
|
| 76 |
|
|
; ( u_hex_n - u f )
|
| 77 |
|
|
.function char__hex_to_nibble
|
| 78 |
|
|
dup 0x80 & .jumpc(error)
|
| 79 |
|
|
dup '0' - 0x80 & .jumpc(error)
|
| 80 |
|
|
'9' over - 0x80 & .jumpc(not_value_0_to_9) '0' - .return(0)
|
| 81 |
|
|
:not_value_0_to_9
|
| 82 |
|
|
dup 'A' - 0x80 & .jumpc(error)
|
| 83 |
|
|
'F' over - 0x80 & .jumpc(not_value_A_to_F) ${ord('A')-10} - .return(0)
|
| 84 |
|
|
:not_value_A_to_F
|
| 85 |
|
|
dup 'a' - 0x80 & .jumpc(error)
|
| 86 |
|
|
'f' over - 0x80 & .jumpc(error) ${ord('a')-10} - .return(0)
|
| 87 |
|
|
:error .return(0xFF)
|
| 88 |
|
|
|
| 89 |
|
|
.ENDIF ; C__INCLUDED__CHAR_S__
|