Subversion Repositories Spectranet

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 25 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        "w5100_defs.inc"
23
.include        "sysvars.inc"
24
.include        "sockdefs.inc"
25 25 winston
 
26
; Socket control routines. In this file:
27
; F_bind
28
; F_connect
29
;
30
; Bind is used to set the socket parameters for a port that is going to
31
; listen. Connect is used to set the parameters when initiating a connection
32
; to a remote host.
33
;
34
; There's an important difference with the assembler routines compared to
35
; the BSD interface. On Unix, bind() is defined as follows:
36
;
37
;   int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen);
38
;
39
; The asm function F_bind only takes sockfd and port as a parameter.
40
; This is because we only will ever support AF_INET family sockets regardless
41
; of the hardware.
42
;
43
; The full BSD bind() implementation uses the struct sockaddr to pass
44
; various parameters. It is intended that the z88dk C library will take
45
; a struct sockaddr pointer. But this is overkill for assembler programs
46
; so we don't do it.
47
;
48
; Connect is similar, except an IP address must be supplied as well.
49
; The C version will use a struct sockaddr pointer.
50
;
51
;---------------------------------------------------------------------------
52
; F_bind:
53
; Set socket information for a socket that will be used for listening
54
; (the local port to listen on).
55
;
56
; Parameters: A  = socket fd
57
;             DE = port
58
; On error, carry flag is set and A contains error number.
59 384 winston
.text
60
.globl F_bind
61
F_bind:
62 25 winston
        call F_gethwsock        ; H now is hardware socket MSB address
63 142 winston
        jp c, J_leavesockfn     ; carry is set on error
64 25 winston
        ld l, Sn_PORT0 % 256    ; port register
65
        ld (hl), d              ; set port MSB
66
        inc l
67
        ld (hl), e              ; set port LSB
68 142 winston
        jp J_leavesockfn
69 25 winston
 
70
;--------------------------------------------------------------------------
71
; F_listen:
72
; Tell a socket to listen for incoming connections.
73
;
74
; Parameters: A  = socket fd
75
;
76
; On error, the carry flag is set and A contains the error number.
77 384 winston
.globl F_listen
78
F_listen:
79 25 winston
        call F_gethwsock        ; H is now hardware socket MSB address
80 142 winston
        jp c, J_leavesockfn     ; unless carry is set because of an error
81 25 winston
 
82
        ld l, Sn_CR % 256       ; hl points at hardware socket's command reg
83
        ld (hl), S_CR_LISTEN    ; tell the socket to listen
84
        ld l, Sn_SR % 256       ; hl points at the status register
85
        ld a, (hl)              ; read it
86
        cp S_SR_SOCK_LISTEN     ; check state is now listening
87 142 winston
        jp z, J_leavesockfn     ; Socket is listening so return
88 25 winston
        ld a, EBUGGERED         ; hardware error
89
        scf                     ; set carry flag and return
90 142 winston
        jp J_leavesockfn
91 25 winston
 
92
;-------------------------------------------------------------------------
93 43 winston
; F_connect:
94
; Connect to a remote host.
95
;
96
; Parameters: A = socket fd
97
;            DE = pointer to 4 byte buffer containing destination IP
98
;            BC = port number
99
; On error, the carry flag is set and A contains the error number.
100
; On success, returns zero in A
101 384 winston
.globl F_connect
102
F_connect:
103 43 winston
        call F_gethwsock        ; H = socket register bank MSB
104
        ld l, Sn_DPORT0 % 256   ; destination port register
105
        ld (hl), b              ; high order of port
106
        inc l
107
        ld (hl), c              ; low order of port
108
        ld l, Sn_DIPR0 % 256    ; destination IP address register
109
        ex de, hl               ; passed buffer value now in hl
110
        ldi
111
        ldi
112
        ldi
113
        ldi
114 82 winston
        ld hl, v_localport
115
        ld e, Sn_PORT1 % 256    ; set local port LSB
116
        ldi
117
        ld e, Sn_PORT0 % 256    ; set local port MSB
118
        ldi
119
        ld hl, (v_localport)    ; update the local port number
120
        inc hl
121
        ld (v_localport), hl
122
 
123 43 winston
        ex de, hl
124
        ld l, Sn_CR % 256       ; command register
125
        ld (hl), S_CR_CONNECT   ; instruction to connect to remote host
126
 
127
        ld l, Sn_IR % 256       ; interrupt register
128 384 winston
.waitforconn3:
129 43 winston
        ld a, (hl)
130
        bit BIT_IR_CON, a
131 384 winston
        jr nz, .connected3
132 43 winston
        and a                   ; if not, is it zero (no flags set?)
133 384 winston
        jr z, .waitforconn3
134 43 winston
        and S_IR_DISCON         ; connection refused?
135 384 winston
        jr nz, .refused3
136 43 winston
        ld a, ETIMEDOUT         ; connection timed out
137
        scf
138 142 winston
        jp J_leavesockfn
139 384 winston
.refused3:
140 43 winston
        ld a, ECONNREFUSED
141
        scf
142 142 winston
        jp J_leavesockfn
143 384 winston
.connected3:
144 43 winston
        set BIT_IR_CON, (hl)    ; reset interrupt bit
145
        xor a                   ; connection OK
146 142 winston
        jp J_leavesockfn
147 25 winston