added deprecation note, and link to Uroborus
[urforth.git] / level0 / urforth0_w_termio_low.asm
blob8c6a9341330e550374ddcdc35ae238ab02a564d9
1 ;; Native x86 GNU/Linux Forth System, Direct Threaded Code
2 ;;
3 ;; Copyright (C) 2020 Ketmar Dark // Invisible Vector
4 ;;
5 ;; This program is free software: you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, version 3 of the License ONLY.
8 ;;
9 ;; This program is distributed in the hope that it will be useful,
10 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;; GNU General Public License for more details.
14 ;; You should have received a copy of the GNU General Public License
15 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17 urword_value "STDIN-FD",stdin_fd,0
18 urword_value "STDOUT-FD",stdout_fd,1
19 urword_value "STDERR-FD",stderr_fd,2
21 if TTYLOW_ALLOW_BUFFERED
22 urword_value "(TTY-LOW-USE-BUFFER)",par_ttylow_use_buffer,1
23 urword_hidden
24 urword_value "(TTY-LOW-BUFFER-ADDR)",par_ttylow_buffer_addr,0
25 urword_hidden
26 urword_value "(TTY-LOW-BUFFER-POS)",par_ttylow_buffer_pos,0
27 urword_hidden
28 urword_value "(TTY-LOW-BUFFER-SIZE)",par_ttylow_buffer_size,32768
29 urword_hidden
30 end if
32 urword_var "(EMIT-COL)",par_emit_col,0
33 urword_hidden
35 urword_code "(TTY-LOW-FLUSH)",par_ttylow_flush
36 urword_hidden
37 ;; ( -- )
38 call fttylow_do_flush
39 urnext
41 fttylow_do_flush:
42 if TTYLOW_ALLOW_BUFFERED
43 ld eax,[fval_par_ttylow_buffer_pos_data]
44 or eax,eax
45 jr z,.done
46 push EIP
47 push TOS
48 mov eax,4 ; function
49 mov ebx,[fval_stdout_fd_data]
50 mov ecx,[fval_par_ttylow_buffer_addr_data]
51 mov edx,[fval_par_ttylow_buffer_pos_data]
52 syscall
53 pop TOS
54 pop EIP
55 ld dword [fval_par_ttylow_buffer_pos_data],0
56 .done:
57 end if
58 ret
59 urword_end
61 urword_code "(RESET-EMIT-COL)",par_reset_emitcol
62 urword_hidden
63 urword_uses par_ttylow_flush
64 ;; ( -- )
65 ;call fttylow_do_flush
66 ld dword [fvar_par_emit_col_data],0
67 urnext
68 urword_end
70 ;; this will also flush buffer
71 urword_code "(EMIT-FIX-COL-CODEBLOCK)",par_emit_fixcol_codeblock
72 ;; TOS: ch
73 ;; EAX: dead
74 urword_hidden
75 urword_codeblock
76 urword_uses par_ttylow_flush
77 emit_fix_col_subr:
78 cp cl,10
79 jr nz,@f
80 ;call fttylow_do_flush
81 ld dword [fvar_par_emit_col_data],0
82 ret
83 @@:
84 cp cl,13
85 jr nz,@f
86 ;call fttylow_do_flush
87 ld dword [fvar_par_emit_col_data],0
88 ret
89 @@:
90 if TTYLOW_ALLOW_BUFFERED = 0
91 cp cl,9
92 jr nz,@f
93 ld eax,[fvar_par_emit_col_data]
94 or eax,7
95 inc eax
96 ld [fvar_par_emit_col_data],eax
97 ret
98 @@:
99 end if
100 cp cl,8
101 jr nz,@f
102 cp dword [fvar_par_emit_col_data],0
103 jr z,.skipdec
104 dec dword [fvar_par_emit_col_data]
105 .skipdec:
108 cp cl,32
109 jr c,@f
110 inc dword [fvar_par_emit_col_data]
113 urword_end
115 urword_code "(EMIT-FIX-COL)",par_emit_fixcol
116 urword_hidden
117 ;; ( ch -- ch )
118 call emit_fix_col_subr
119 urnext
120 urword_end
122 urword_code "(EMIT)",paremit
123 urword_uses par_ttylow_flush
124 ;; ( ch -- )
125 call emit_fix_col_subr
126 if TTYLOW_ALLOW_BUFFERED
127 cp cl,9
128 jr nz,.normal
129 ; tab
130 .tabloop:
131 ld cl,32
132 call .normal
133 ld eax,[fvar_par_emit_col_data]
134 inc eax
135 ld [fvar_par_emit_col_data],eax
136 and al,7
137 jr nz,.tabloop
138 pop TOS
139 urnext
141 .normal:
142 call .again
143 .quit:
144 pop TOS
145 urnext
147 .again:
148 cp dword [fval_par_ttylow_use_buffer_data],1
149 jr c,.turnedoff
150 ld eax,[fval_par_ttylow_buffer_pos_data]
151 cp eax,[fval_par_ttylow_buffer_size_data]
152 jr nc,.toomuch
153 add eax,[fval_par_ttylow_buffer_addr_data]
154 ld [eax],cl
155 inc eax
156 inc dword [fval_par_ttylow_buffer_pos_data]
157 jr .done
158 .toomuch:
159 call fttylow_do_flush
160 jr .again
161 .turnedoff:
162 call fttylow_do_flush
163 end if
165 push EIP
166 push TOS
167 mov eax,4 ; function
168 mov ebx,[fval_stdout_fd_data]
169 mov ecx,esp ; address
170 mov edx,1 ; length
171 syscall
172 pop TOS
173 pop EIP
174 .done:
175 if TTYLOW_ALLOW_BUFFERED
177 else
178 pop TOS
179 urnext
180 end if
181 urword_end
183 urword_code "(CR)",parcr
184 urword_uses paremit
185 ;; ( -- )
186 push TOS
187 ld TOS,10
188 jr fword_paremit
189 urword_end
191 urword_code "(BELL)",parbell
192 urword_uses paremit
193 ;; ( -- )
194 push EIP
195 push TOS
196 ld eax,7
197 push eax
198 mov eax,4 ; function
199 mov ebx,[fval_stdout_fd_data]
200 mov ecx,esp ; address
201 mov edx,1 ; length
202 syscall
203 pop eax
204 pop TOS
205 pop EIP
206 urnext
207 urword_end
209 urword_code "(ENDCR)",parendcr
210 urword_uses parcr
211 ;; ( -- )
212 cp dword [fvar_par_emit_col_data],0
213 jr z,@f
214 jr fword_parcr
216 urnext
217 urword_end
219 urword_code "(?ENDCR)",parqendcr
220 urword_uses parcr
221 ;; ( -- )
222 push TOS
223 cp dword [fvar_par_emit_col_data],0
224 setnz cl
225 movzx ecx,cl
226 urnext
227 urword_end
230 if 0
231 urword_code "(TYPE)",partype
232 urword_uses par_ttylow_flush
233 ;; ( addr length -- )
234 test TOS,0x80000000
235 jnz .fucked_length
236 or TOS,TOS
237 jz .fucked_length
239 push TOS
240 push esi
241 call fttylow_do_flush
242 ld esi,[esp+4*2]
243 ld eax,TOS
244 .fixcol_loop:
245 ld cl,[esi]
246 call emit_fix_col_subr
247 inc esi
248 dec eax
249 jr nz,.fixcol_loop
250 pop esi
251 pop TOS
253 mov edx,TOS
254 mov eax,4
255 mov ebx,[fval_stdout_fd_data]
256 pop ecx
257 push EIP
258 syscall
259 pop EIP
260 pop TOS
261 urnext
262 .fucked_length:
263 pop TOS
264 pop TOS
265 urnext
266 urword_end
267 end if
270 urword_code "(GETCH)",pargetch
271 urword_uses par_ttylow_flush
272 ;; ( -- ch )
273 ;; returns -1 on EOF
274 push TOS
275 call fttylow_do_flush
276 xor eax,eax
277 push eax
278 mov eax,3 ; read
279 ld ebx,[fval_stdin_fd_data]
280 mov ecx,esp ; address
281 mov edx,1 ; length
282 push EIP
283 syscall
284 pop EIP
285 pop TOS ; read char
286 or eax,eax
287 jnz @f
288 mov TOS,-1 ; oops
290 urnext
291 urword_end