Subversion Repositories Spectranet

[/] [branches/] [gnubinutils/] [rom/] [utility.asm] - Blame information for rev 384

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 50 winston
;The MIT License
2
;
3
;Copyright (c) 2008 Dylan Smith
4
;
5
;Permission is hereby granted, free of charge, to any person obtaining a copy
6
;of this software and associated documentation files (the "Software"), to deal
7
;in the Software without restriction, including without limitation the rights
8
;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
;copies of the Software, and to permit persons to whom the Software is
10
;furnished to do so, subject to the following conditions:
11
;
12
;The above copyright notice and this permission notice shall be included in
13
;all copies or substantial portions of the Software.
14
;
15
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
;THE SOFTWARE.
22 384 winston
.include        "sysvars.inc"
23
.include        "sysdefs.inc"
24
.include        "page2.xinc"
25 50 winston
 
26
; A collection of utility functions:
27
; F_rand16 - Generate a 16-bit random number
28
;
29
 
30
;========================================================================
31
; F_rand16
32 67 winston
; Generate a 16 bit pseudorandom number. This is used for things such as
33 50 winston
; DNS lookups (which require a 16 bit query identifier).
34 384 winston
; This was adapted from http://map.tni0.nl0/sources/external/z80bits.html0#3.20
35 50 winston
; which in turn was adapted from a game (not named).
36
;
37
; Pseudorandom number is returned in hl.
38 384 winston
.globl F_rand16
39
F_rand16:
40 50 winston
        push de
41
        push af
42
        ld de, (v_seed)
43
        ld a,d
44
        ld h,e
45
        ld l,253
46
        or a
47
        sbc hl,de
48
        sbc a,0
49
        sbc hl,de
50
        ld d,0
51
        sbc a,d
52
        ld e,a
53
        sbc hl,de
54 384 winston
        jr nc, .rand1
55 50 winston
        inc hl
56 384 winston
.rand1: ld (v_seed),hl
57 50 winston
        pop af
58
        pop de
59
        ret
60
 
61 67 winston
;-----------------------------------------------------------------------
62
; F_ipstring2long
63
; Converts a string containing an IP address to a big-endian 32 bit
64
; long word. Carry set if the string isn't an IP address.
65
; HL = pointer to string
66
; DE = pointer to 4 byte buffer for return value
67 384 winston
.globl F_ipstring2long
68
F_ipstring2long:
69 229 winston
        call F_util_getroutine
70
        call F_ipstring2long_u_impl
71
        jp F_util_restore
72 67 winston
 
73
;-----------------------------------------------------------------------
74
; F_long2ipstring
75
; Converts a 4 byte big-endian long word into a null terminated IP string.
76
; hl = pointer to 4 byte buffer containing the IP address
77
; de = pointer to a buffer where the IP address string will be returned
78 384 winston
.globl F_long2ipstring
79
F_long2ipstring:
80 229 winston
        call F_util_getroutine
81
        call F_long2ipstring_u_impl
82
        jp F_util_restore
83 67 winston
 
84
;-----------------------------------------------------------------------
85 94 winston
; F_mac2string
86
; Converts the MAC address pointed to by hl to a string
87
; Parameters: hl - address of MAC address
88
;             de - pointer to string buffer
89 384 winston
.globl F_mac2string
90
F_mac2string:
91 229 winston
        call F_util_getroutine
92
        call F_mac2string_u_impl
93
        jp F_util_restore
94 94 winston
 
95
;-----------------------------------------------------------------------
96
; F_string2mac
97
; Converts an ASCII string to a 6 byte MAC address.
98
; Carry flag is set if the string isn't a MAC address.
99
; Parameters: hl - address of MAC string
100
;             de - address of 6 byte MAC address buffer
101 384 winston
.globl F_string2mac
102
F_string2mac:
103 229 winston
        call F_util_getroutine
104
        call F_string2mac_u_impl
105
        jp F_util_restore
106 94 winston
 
107
;-----------------------------------------------------------------------
108 67 winston
; F_atoi8: Simple ascii-to-int 8 bit. hl=ptr to string. Positive values!
109
; Carry flag set on error. Returns value in c. Either a '.' or a null
110
; terminates the string. (The . being a delimiter in an IP address)
111
; This routine undoubtedly leaves some room for improvement...
112 384 winston
.globl F_atoi8
113
F_atoi8:
114 229 winston
        call F_util_getroutine
115
        call F_itoa8_u_impl
116
        jp F_util_restore
117 67 winston
 
118
;------------------------------------------------------------------------
119
; F_itoa8:
120
; Convert an 8 bit number to a string.
121
; a = number to convert
122
; hl = pointer to string buffer. On exit, hl points to string's end.
123
; No null terminator is added!
124 384 winston
.globl F_itoa8
125
F_itoa8:
126 229 winston
        call F_util_getroutine
127
        call F_itoa8_u_impl
128
        jp F_util_restore
129 67 winston
 
130 72 winston
;-------------------------------------------------------------------------
131 94 winston
; F_itoh8
132
; Converts an 8 bit number in A to a hex string.
133
; Parameters: HL - buffer to fill
134
; This routine is a minor modification of the one at
135 384 winston
; http://map.tni7.nl7/sources/external/z80bits.html7
136
.globl F_itoh8
137
F_itoh8:
138 229 winston
        call F_util_getroutine
139
        call F_itoh8_u_impl
140
        jp F_util_restore
141 94 winston
 
142
;---------------------------------------------------------------------------
143
; F_htoi8
144
; Converts the ascii at (hl) and (hl+1) to an int, returned in A.
145
; carry flag set on error
146
; Modifies hl, c and a.
147 384 winston
.globl F_htoi8
148
F_htoi8:
149 229 winston
        call F_util_getroutine
150
        call F_htoi8_u_impl
151
        jp F_util_restore
152 94 winston
 
153
;-------------------------------------------------------------------------
154 72 winston
; F_crc16:
155
; Calculate a 16 bit CRC on an arbitrary region of memory.
156 384 winston
; This was adapted from http://map.tni9.nl9/sources/external/z80bits.html9
157 72 winston
; Parameters: DE = start address for CRC calculation
158
;             BC = end number of bytes to check
159
; 16 bit CRC is returned in HL.
160
; Note: byte counter is modified compared to orginal code.
161 384 winston
.globl F_crc16
162
F_crc16:
163 229 winston
        call F_util_getroutine
164
        call F_crc16_u_impl
165
        jp F_util_restore
166 77 winston
 
167
;--------------------------------------------------------------------------
168
; F_checkromsig
169
; Checks a ROM page signature for a valid vector table. Returns with the
170
; Z flag set if the signature is valid, NZ if not.
171 110 winston
; Parameters: A = page to test
172 384 winston
.globl F_checkromsig
173
F_checkromsig:
174 77 winston
        call F_setpageB
175 163 winston
        ld a, (0x2000)
176
        cp 0xAA
177 77 winston
        ret
178
 
179 229 winston
; Get and restore routines from the utility ROM where we have only stubs.
180 384 winston
.globl F_util_getroutine
181
F_util_getroutine:
182 229 winston
        push af
183
        ld a, (v_pgb)
184
        ld (v_util_pgb), a
185
        ld a, UTILROM
186
        call F_setpageB
187
        pop af
188
        ret
189
 
190 384 winston
.globl F_util_restore
191
F_util_restore:
192 229 winston
        push af
193
        ld a, (v_util_pgb)
194
        call F_setpageB
195
        pop af
196
        ret
197