Terminal program for MailStation devices
1; vim:syntax=z8a:ts=8
2;
3; msTERM
4; WiFiStation parallel port routines
5;
6; Copyright (c) 2019-2021 joshua stein <jcs@jcs.org>
7;
8; Permission to use, copy, modify, and distribute this software for any
9; purpose with or without fee is hereby granted, provided that the above
10; copyright notice and this permission notice appear in all copies.
11;
12; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19;
20
21 .module wifi
22
23 .include "mailstation.inc"
24
25 .area _CODE
26
27 .equ CONTROL_DIR, #0x0a
28 .equ CONTROL_DIR_OUT, #0b00001111
29 .equ CONTROL_DIR_IN, #0
30
31 .equ CONTROL_PORT, #0x9
32 .equ CONTROL_STROBE_BIT, #0
33 .equ CONTROL_STROBE, #(1 << CONTROL_STROBE_BIT)
34 .equ CONTROL_LINEFEED_BIT, #1
35 .equ CONTROL_LINEFEED, #(1 << CONTROL_LINEFEED_BIT)
36 .equ CONTROL_INIT, #(1 << 2)
37 .equ CONTROL_SELECT, #(1 << 3)
38
39 .equ DATA_DIR, #0x2c
40 .equ DATA_DIR_OUT, #0xff
41 .equ DATA_DIR_IN, #0
42 .equ DATA_PORT, #0x2d
43
44 .equ STATUS_PORT, #0x21
45 .equ STATUS_BUSY, #(1 << 7)
46 .equ STATUS_ACK, #(1 << 6)
47
48; void wifi_init(void);
49_wifi_init::
50 ; lower control lines
51 ld a, #CONTROL_DIR_OUT
52 out (#CONTROL_DIR), a
53 xor a
54 out (#CONTROL_PORT), a
55 ret
56
57; at idle, lower all control lines
58; writer: reader:
59; raise strobe
60; see high strobe as high busy
61; raise linefeed
62; see high linefeed as high ack
63; write all data pins
64; lower strobe
65; see low strobe as low busy
66; read data
67; lower linefeed
68; see lower linefeed as high ack
69
70; int wifi_write(char); -1 on error, 0 on success
71_wifi_write::
72 push ix
73 ld ix, #0
74 add ix, sp
75 push bc
76 push de
77 ld c, 4(ix) ; char to send
78 ld a, #DATA_DIR_OUT
79 out (#DATA_DIR), a ; we're sending out
80 ld a, #CONTROL_STROBE
81 out (#CONTROL_PORT), a ; raise strobe
82 ld de, #0xffff
83wait_for_ack:
84 in a, (#STATUS_PORT)
85 and #STATUS_ACK ; is ack high?
86 jr nz, got_ack ; yes, break
87 dec de ; no, de--
88 ld a, d
89 cp #0
90 jr nz, wait_for_ack
91 ld a, e
92 cp #0
93 jr nz, wait_for_ack
94 jr abort_send ; de == 0, fail
95got_ack:
96 ld a, c
97 out (#DATA_PORT), a ; write data
98 xor a
99 out (#CONTROL_PORT), a ; lower strobe
100 ld de, #0xffff
101wait_for_final_ack:
102 in a, (#STATUS_PORT)
103 and #STATUS_ACK ; is ack low?
104 jr z, got_final_ack ; yes, break
105 dec de ; no, de--
106 ld a, d
107 cp #0
108 jr nz, wait_for_final_ack
109 ld a, e
110 cp #0
111 jr nz, wait_for_final_ack
112got_final_ack:
113 pop de
114 pop bc
115 pop ix
116 ld hl, #0 ; return 0
117 ret
118abort_send:
119 xor a
120 out (#CONTROL_PORT), a ; lower strobe
121 pop de
122 pop bc
123 pop ix
124 ld hl, #-1 ; return -1
125 ret
126
127
128; int wifi_read(void); -1 on nothing read, >= 0 on success returning char
129_wifi_read::
130 push ix
131 ld ix, #0
132 add ix, sp
133 push de
134 ld hl, #-1 ; return -1 unless we read something
135 in a, (#STATUS_PORT)
136 and #STATUS_BUSY ; is busy high?
137 jr z, recv_done ; no, bail
138 and #STATUS_ACK ; but is ack high too? probably bogus
139 jr nz, recv_done
140 ld a, #DATA_DIR_IN
141 out (#DATA_DIR), a ; we're reading in
142 ld a, #CONTROL_LINEFEED ; raise linefeed
143 out (#CONTROL_PORT), a
144 ld de, #0xffff
145wait_for_busy_ack:
146 in a, (#STATUS_PORT)
147 and #STATUS_BUSY ; is busy high?
148 jr z, read_data ; no, break
149 dec de ; no, de--
150 ld a, d
151 cp #0
152 jr nz, wait_for_busy_ack
153 ld a, e
154 cp #0
155 jr nz, wait_for_busy_ack
156 jr recv_done ; de == 0, fail
157read_data:
158 in a, (#DATA_PORT)
159 ld h, #0
160 ld l, a
161raise_lf:
162 xor a
163 out (#CONTROL_PORT), a ; lower linefeed
164recv_done:
165 pop de
166 pop ix
167 ret