Fix the spelling of "Boston"
[profile/ivi/syslinux.git] / conio.inc
1 ;; $Id$
2 ;; -----------------------------------------------------------------------
3 ;;   
4 ;;   Copyright 1994-2004 H. Peter Anvin - All Rights Reserved
5 ;;
6 ;;   This program is free software; you can redistribute it and/or modify
7 ;;   it under the terms of the GNU General Public License as published by
8 ;;   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 ;;   Boston MA 02111-1307, USA; either version 2 of the License, or
10 ;;   (at your option) any later version; incorporated herein by reference.
11 ;;
12 ;; -----------------------------------------------------------------------
13
14 ;;
15 ;; conio.inc
16 ;;
17 ;; Console I/O code, except:
18 ;;   writechr, writestr         - module-dependent
19 ;;   cwritestr, crlf            - writestr.inc
20 ;;   writehex*                  - writehex.inc
21 ;;
22
23 ;
24 ; loadkeys:     Load a LILO-style keymap; SI and DX:AX set by searchdir
25 ;
26 loadkeys:
27                 and dx,dx                       ; Should be 256 bytes exactly
28                 jne loadkeys_ret
29                 cmp ax,256
30                 jne loadkeys_ret
31
32                 mov bx,trackbuf
33                 mov cx,1                        ; 1 cluster should be >= 256 bytes
34                 call getfssec
35
36                 mov si,trackbuf
37                 mov di,KbdMap
38                 mov cx,256 >> 2
39                 rep movsd
40
41 loadkeys_ret:   ret
42                 
43 ;
44 ; get_msg_file: Load a text file and write its contents to the screen,
45 ;               interpreting color codes.  Is called with SI and DX:AX
46 ;               set by routine searchdir
47 ;
48 get_msg_file:
49                 push es
50                 shl edx,16                      ; EDX <- DX:AX (length of file)
51                 mov dx,ax
52                 mov ax,xfer_buf_seg             ; Use for temporary storage
53                 mov es,ax
54
55                 mov byte [TextAttribute],07h    ; Default grey on white
56                 mov byte [DisplayMask],07h      ; Display text in all modes
57                 call msg_initvars
58
59 get_msg_chunk:  push edx                        ; EDX = length of file
60                 xor bx,bx                       ; == xbs_textbuf
61                 mov cx,[BufSafe]
62                 call getfssec
63                 pop edx
64                 push si                         ; Save current cluster
65                 xor si,si                       ; == xbs_textbuf
66                 mov cx,[BufSafeBytes]           ; Number of bytes left in chunk
67 print_msg_file:
68                 push cx
69                 push edx
70                 es lodsb
71                 cmp al,1Ah                      ; DOS EOF?
72                 je msg_done_pop
73                 push si
74                 mov cl,[UsingVGA]
75                 inc cl                          ; 01h = text mode, 02h = graphics
76                 call [NextCharJump]             ; Do what shall be done
77                 pop si
78                 pop edx
79                 pop cx
80                 dec edx
81                 jz msg_done
82                 loop print_msg_file
83                 pop si
84                 jmp short get_msg_chunk
85 msg_done_pop:
86                 add sp,byte 6                   ; Drop pushed EDX, CX
87 msg_done:
88                 pop si
89                 pop es
90                 ret
91 msg_putchar:                                    ; Normal character
92                 cmp al,0Fh                      ; ^O = color code follows
93                 je msg_ctrl_o
94                 cmp al,0Dh                      ; Ignore <CR>
95                 je msg_ignore
96                 cmp al,0Ah                      ; <LF> = newline
97                 je msg_newline
98                 cmp al,0Ch                      ; <FF> = clear screen
99                 je msg_formfeed
100                 cmp al,19h                      ; <EM> = return to text mode
101                 je msg_novga
102                 cmp al,18h                      ; <CAN> = VGA filename follows
103                 je msg_vga
104                 jnb .not_modectl
105                 cmp al,10h                      ; 10h to 17h are mode controls
106                 jae msg_modectl
107 .not_modectl:
108
109 msg_normal:     call write_serial_displaymask   ; Write to serial port
110                 test [DisplayMask],cl
111                 jz msg_ignore                   ; Not screen
112                 mov bl,[TextAttribute]
113                 mov bh,[BIOS_page]
114                 mov ah,09h                      ; Write character/attribute
115                 mov cx,1                        ; One character only
116                 int 10h                         ; Write to screen
117                 mov al,[CursorCol]
118                 inc ax
119                 cmp al,[VidCols]
120                 ja msg_line_wrap                ; Screen wraparound
121                 mov [CursorCol],al
122
123 msg_gotoxy:     mov bh,[BIOS_page]
124                 mov dx,[CursorDX]
125                 mov ah,02h                      ; Set cursor position
126                 int 10h
127 msg_ignore:     ret
128 msg_ctrl_o:                                     ; ^O = color code follows
129                 mov word [NextCharJump],msg_setbg
130                 ret
131 msg_newline:                                    ; Newline char or end of line
132                 mov si,crlf_msg
133                 call write_serial_str_displaymask
134 msg_line_wrap:                                  ; Screen wraparound
135                 test [DisplayMask],cl
136                 jz msg_ignore
137                 mov byte [CursorCol],0
138                 mov al,[CursorRow]
139                 inc ax
140                 cmp al,[VidRows]
141                 ja msg_scroll
142                 mov [CursorRow],al
143                 jmp short msg_gotoxy
144 msg_scroll:     xor cx,cx                       ; Upper left hand corner
145                 mov dx,[ScreenSize]
146                 mov [CursorRow],dh              ; New cursor at the bottom
147                 mov bh,[ScrollAttribute]
148                 mov ax,0601h                    ; Scroll up one line
149                 int 10h
150                 jmp short msg_gotoxy
151 msg_formfeed:                                   ; Form feed character
152                 mov si,crff_msg
153                 call write_serial_str_displaymask
154                 test [DisplayMask],cl
155                 jz msg_ignore
156                 xor cx,cx
157                 mov [CursorDX],cx               ; Upper lefthand corner
158                 mov dx,[ScreenSize]
159                 mov bh,[TextAttribute]
160                 mov ax,0600h                    ; Clear screen region
161                 int 10h
162                 jmp msg_gotoxy
163 msg_setbg:                                      ; Color background character
164                 call unhexchar
165                 jc msg_color_bad
166                 shl al,4
167                 test [DisplayMask],cl
168                 jz .dontset
169                 mov [TextAttribute],al
170 .dontset:
171                 mov word [NextCharJump],msg_setfg
172                 ret
173 msg_setfg:                                      ; Color foreground character
174                 call unhexchar
175                 jc msg_color_bad
176                 test [DisplayMask],cl
177                 jz .dontset
178                 or [TextAttribute],al           ; setbg set foreground to 0
179 .dontset:
180                 jmp short msg_putcharnext
181 msg_vga:
182                 mov word [NextCharJump],msg_filename
183                 mov di, VGAFileBuf
184                 jmp short msg_setvgafileptr
185
186 msg_color_bad:
187                 mov byte [TextAttribute],07h    ; Default attribute
188 msg_putcharnext:
189                 mov word [NextCharJump],msg_putchar
190                 ret
191
192 msg_filename:                                   ; Getting VGA filename
193                 cmp al,0Ah                      ; <LF> = end of filename
194                 je msg_viewimage
195                 cmp al,' '
196                 jbe msg_ret                     ; Ignore space/control char
197                 mov di,[VGAFilePtr]
198                 cmp di,VGAFileBufEnd
199                 jnb msg_ret
200                 mov [di],al                     ; Can't use stosb (DS:)
201                 inc di
202 msg_setvgafileptr:
203                 mov [VGAFilePtr],di
204 msg_ret:        ret
205
206 msg_novga:
207                 call vgaclearmode
208                 jmp short msg_initvars
209
210 msg_viewimage:
211                 push es
212                 push ds
213                 pop es                          ; ES <- DS
214                 mov si,VGAFileBuf
215                 mov di,VGAFileMBuf
216                 push di
217                 call mangle_name
218                 pop di
219                 call searchdir
220                 pop es
221                 jz msg_putcharnext              ; Not there
222                 call vgadisplayfile
223                 ; Fall through
224
225                 ; Subroutine to initialize variables, also needed
226                 ; after loading a graphics file
227 msg_initvars:
228                 pusha
229                 mov bh,[BIOS_page]
230                 mov ah,03h                      ; Read cursor position
231                 int 10h
232                 mov [CursorDX],dx
233                 popa
234                 jmp short msg_putcharnext       ; Initialize state machine
235
236 msg_modectl:
237                 and al,07h
238                 mov [DisplayMask],al
239                 jmp short msg_putcharnext
240
241 ;
242 ; write_serial: If serial output is enabled, write character on serial port
243 ; write_serial_displaymask: d:o, but ignore if DisplayMask & 04h == 0
244 ;
245 write_serial_displaymask:
246                 test byte [DisplayMask], 04h
247                 jz write_serial.end
248 write_serial:
249                 pushfd
250                 pushad
251                 mov bx,[SerialPort]
252                 and bx,bx
253                 je .noserial
254                 push ax
255                 mov ah,[FlowInput]
256 .waitspace:
257                 ; Wait for space in transmit register
258                 lea dx,[bx+5]                   ; DX -> LSR
259                 in al,dx
260                 test al,20h
261                 jz .waitspace
262
263                 ; Wait for input flow control
264                 inc dx                          ; DX -> MSR
265                 in al,dx
266                 and al,ah
267                 cmp al,ah
268                 jne .waitspace  
269 .no_flow:               
270
271                 xchg dx,bx                      ; DX -> THR
272                 pop ax
273                 call slow_out                   ; Send data
274 .noserial:      popad
275                 popfd
276 .end:           ret
277
278 ;
279 ; write_serial_str: write_serial for strings
280 ; write_serial_str_displaymask: d:o, but ignore if DisplayMask & 04h == 0
281 ;
282 write_serial_str_displaymask:
283                 test byte [DisplayMask], 04h
284                 jz write_serial_str.end
285
286 write_serial_str:
287 .loop           lodsb
288                 and al,al
289                 jz .end
290                 call write_serial
291                 jmp short .loop
292 .end:           ret
293
294 ;
295 ; pollchar: check if we have an input character pending (ZF = 0)
296 ;
297 pollchar:
298                 pushad
299                 mov ah,11h              ; Poll keyboard
300                 int 16h
301                 jnz .done               ; Keyboard response
302                 mov dx,[SerialPort]
303                 and dx,dx
304                 jz .done                ; No serial port -> no input
305                 add dx,byte 5           ; DX -> LSR
306                 in al,dx
307                 test al,1               ; ZF = 0 if data pending
308                 jz .done
309                 inc dx                  ; DX -> MSR
310                 mov ah,[FlowIgnore]     ; Required status bits
311                 in al,dx
312                 and al,ah
313                 cmp al,ah
314                 setne al
315                 dec al                  ; Set ZF = 0 if equal
316 .done:          popad
317                 ret
318
319 ;
320 ; getchar: Read a character from keyboard or serial port
321 ;
322 getchar:
323                 RESET_IDLE
324 .again:
325                 DO_IDLE
326                 mov ah,11h              ; Poll keyboard
327                 int 16h
328                 jnz .kbd                ; Keyboard input?
329                 mov bx,[SerialPort]
330                 and bx,bx
331                 jz .again
332                 lea dx,[bx+5]           ; DX -> LSR
333                 in al,dx
334                 test al,1
335                 jz .again
336                 inc dx                  ; DX -> MSR
337                 mov ah,[FlowIgnore]
338                 in al,dx
339                 and al,ah
340                 cmp al,ah
341                 jne .again
342 .serial:        xor ah,ah               ; Avoid confusion
343                 xchg dx,bx              ; Data port
344                 in al,dx
345                 ret
346 .kbd:           mov ah,10h              ; Get keyboard input
347                 int 16h
348                 cmp al,0E0h
349                 jnz .not_ext
350                 xor al,al
351 .not_ext:
352                 and al,al
353                 jz .func_key
354                 mov bx,KbdMap           ; Convert character sets
355                 xlatb
356 .func_key:      ret
357
358 %ifdef DEBUG_TRACERS
359 ;
360 ; debug hack to print a character with minimal code impact
361 ;
362 debug_tracer:   pushad
363                 pushfd
364                 mov bp,sp
365                 mov bx,[bp+9*4]         ; Get return address
366                 mov al,[cs:bx]          ; Get data byte
367                 inc word [bp+9*4]       ; Return to after data byte
368                 call writechr
369                 popfd
370                 popad
371                 ret
372 %endif  ; DEBUG_TRACERS