Subversion Repositories Spectranet

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 46 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
;
23 384 winston
.include        "sockdefs.inc"
24
.include        "sysvars.inc"
25
.include        "sysdefs.inc"
26
.include        "w5100_defs.inc"
27
 
28 46 winston
;
29 384 winston
; Sockinfo.asm0: Routines to get information about a socket, analagous
30 46 winston
; to filling a struct sockaddr_t in C.
31
;
32
; The data structure that is returned looks like this:
33
;
34
; remote IP   - 32 bits
35
; remote port - 16 bits
36
; local port  - 16 bits
37
;
38
; So the calling program must allow 6 bytes for the data to be returned.
39
;===========================================================================
40
; F_sockinfo:
41
; Returns information about a hardware socket. It's assumed that the W5100
42
; register area is paged in.
43
;
44
; Parameters:  H = High order of socket hardware register address
45
;             DE = Address of the buffer to fill.
46 384 winston
.globl F_sockinfo
47
F_sockinfo:
48 234 winston
        call F_checkpageA
49 46 winston
        ld l, Sn_DIPR0 % 256    ; remote IP address register
50
        ldi
51
        ldi
52
        ldi
53
        ldi
54
        ; convert ports from big to little endian
55
        ld l, Sn_DPORT1 % 256   ; remote port register
56
        ld a, (hl)
57
        ld (de), a
58
        dec l
59
        inc de
60
        ld a, (hl)
61
        ld (de), a
62
        inc de
63
        ld l, Sn_PORT1 % 256    ; local port register
64
        ld a, (hl)
65
        ld (de), a
66
        dec l
67
        inc de
68
        ld a, (hl)
69
        ld (de), a
70 234 winston
        ld a, (v_buf_pgb)
71
        and a
72
        jp nz, F_setpageB
73 46 winston
        ret
74
 
75
;========================================================================
76
; F_setsockinfo:
77
; Sets socket information from structure described above.
78
;
79
; Parameters:  H = MSB of W5100 socket register area
80
;             DE = address of the 8 byte socket info structure.
81 384 winston
.globl F_setsockinfo
82
F_setsockinfo:
83 234 winston
        call F_checkpageA
84 46 winston
        ex de, hl
85
        ld e, Sn_DIPR0 % 256    ; destination IP address
86
        ldi
87
        ldi
88
        ldi
89
        ldi
90 48 winston
        ld e, Sn_DPORT1 % 256   ; destination port
91
        ld a, (hl)
92
        ld (de), a
93
        dec e
94 46 winston
        inc hl
95
        ld a, (hl)
96
        ld (de), a
97 48 winston
        inc hl
98
 
99
        ; only set source port if source port is set.
100 46 winston
        ld a, (hl)
101 48 winston
        inc hl
102
        or (hl)
103
        ex de, hl               ; restore registers to expected order
104 384 winston
        jr z, .checkset2                ; need to set random source port?
105 219 winston
        ld l, Sn_PORT0 % 256    ; hl points at source port
106 48 winston
        ld a, (de)              ; source port MSB
107
        ld (hl), a              ; set MSB
108
        inc l
109
        dec de
110
        ld a, (de)
111
        ld (hl), a              ; set LSB
112 384 winston
        jr .leave2
113
.checkset2:
114 82 winston
        ld l, Sn_PORT0 % 256    ; port MSB
115
        ld a, (hl)
116
        inc l                   ; port LSB
117
        or (hl)                 ; is it zero?
118 384 winston
        jr nz, .leave2          ; no - nothing to do
119 82 winston
        ex de, hl               ; yes - set the local port
120
        ld hl, v_localport
121
        ld e, Sn_PORT1 % 256    ; set local port LSB
122
        ldi
123
        ld e, Sn_PORT0 % 256    ; set local port MSB
124
        ldi
125
        ld hl, (v_localport)    ; update the local port number
126
        inc hl
127
        ld (v_localport), hl
128
        ex de, hl
129 384 winston
.leave2:
130 234 winston
        ld a, (v_buf_pgb)
131
        and a
132
        jp nz, F_setpageB
133 82 winston
        ret
134 46 winston
 
135 81 winston
;---------------------------------------------------------------------------
136
; F_fillsockaddr
137
; Fills a struct sockaddr_in with the remote socket info
138
; Parameters: Socket in A
139
;             Pointer to sockaddr_in in DE
140 384 winston
.globl F_remoteaddress
141
F_remoteaddress:
142 81 winston
        call F_gethwsock
143 142 winston
        jp c, J_leavesockfn     ; invalid socket
144 234 winston
 
145
        call F_checkpageA
146 81 winston
        inc de                  ; increment past int sin_family
147
        inc de
148
        ld l, Sn_DPORT1 % 256   ; destination port LSB
149
        ldi
150
        ld l, Sn_DPORT0 % 256   ; destination port MSB
151
        ldi
152
 
153
        ld l, Sn_DIPR0 % 256    ; remote IP address register
154
        ldi                     ; copy in network order
155
        ldi
156
        ldi
157
        ldi
158 234 winston
        ld a, (v_buf_pgb)
159
        and a
160
        call nz, F_setpageB
161 142 winston
        jp J_leavesockfn
162 81 winston