1 | /* |
---|
2 | * outch.c - This file contains code for displaying characters |
---|
3 | * on the console uisng information that should be |
---|
4 | * maintained by the BIOS in its data Area. |
---|
5 | * |
---|
6 | * Copyright (C) 1998 Eric Valette (valette@crf.canon.fr) |
---|
7 | * Canon Centre Recherche France. |
---|
8 | * |
---|
9 | * The license and distribution terms for this file may be |
---|
10 | * found in the file LICENSE in this distribution or at |
---|
11 | * http://www.rtems.org/license/LICENSE. |
---|
12 | * |
---|
13 | * Till Straumann <strauman@slac.stanford.edu>, 2003/9: |
---|
14 | * - added handling of basic escape sequences (cursor movement |
---|
15 | * and erasing; just enough for the line editor 'libtecla' to |
---|
16 | * work...) |
---|
17 | */ |
---|
18 | |
---|
19 | #include <bsp.h> |
---|
20 | |
---|
21 | #include <stdlib.h> |
---|
22 | #include <string.h> |
---|
23 | |
---|
24 | #include <crt.h> |
---|
25 | |
---|
26 | extern void wr_cursor(int, unsigned short); |
---|
27 | |
---|
28 | #define TAB_SPACE 4 |
---|
29 | static unsigned short *bitMapBaseAddr; |
---|
30 | static unsigned short ioCrtBaseAddr; |
---|
31 | static unsigned short maxCol; |
---|
32 | static unsigned short maxRow; |
---|
33 | static unsigned char row; |
---|
34 | static unsigned char column; |
---|
35 | static unsigned short attribute; |
---|
36 | static unsigned int nLines; |
---|
37 | |
---|
38 | static void |
---|
39 | scroll(void) |
---|
40 | { |
---|
41 | int i, j; /* Counters */ |
---|
42 | unsigned short *pt_scroll, *pt_bitmap; /* Pointers on the bit-map */ |
---|
43 | |
---|
44 | pt_bitmap = bitMapBaseAddr; |
---|
45 | j = 0; |
---|
46 | pt_bitmap = pt_bitmap + j; |
---|
47 | pt_scroll = pt_bitmap + maxCol; |
---|
48 | for (i = j; i < (maxRow - 1) * maxCol; i++) { |
---|
49 | *pt_bitmap++ = *pt_scroll++; |
---|
50 | } |
---|
51 | |
---|
52 | /* |
---|
53 | * Blank characters are displayed on the last line. |
---|
54 | */ |
---|
55 | for (i = 0; i < maxCol; i++) { |
---|
56 | *pt_bitmap++ = (short) (' ' | attribute); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | static void |
---|
61 | doCRNL(int cr, int nl) |
---|
62 | { |
---|
63 | if (nl) { |
---|
64 | if (++row == maxRow) { |
---|
65 | scroll(); /* Scroll the screen now */ |
---|
66 | row = maxRow - 1; |
---|
67 | } |
---|
68 | nLines++; |
---|
69 | } |
---|
70 | if (cr) |
---|
71 | column = 0; |
---|
72 | /* Move cursor on the next location */ |
---|
73 | if (cr || nl) |
---|
74 | wr_cursor(row * maxCol + column, ioCrtBaseAddr); |
---|
75 | } |
---|
76 | |
---|
77 | int (*videoHook)(char, int *)=0; |
---|
78 | |
---|
79 | static void |
---|
80 | advanceCursor(void) |
---|
81 | { |
---|
82 | if (++column == maxCol) |
---|
83 | doCRNL(1,1); |
---|
84 | else |
---|
85 | wr_cursor(row * maxCol + column, ioCrtBaseAddr); |
---|
86 | } |
---|
87 | |
---|
88 | static void |
---|
89 | gotorc(int r, int c) |
---|
90 | { |
---|
91 | column = c; |
---|
92 | row = r; |
---|
93 | |
---|
94 | wr_cursor(row * maxCol + column, ioCrtBaseAddr); |
---|
95 | } |
---|
96 | |
---|
97 | #define ESC ((char)27) |
---|
98 | /* erase current location without moving the cursor */ |
---|
99 | #define BLANK ((char)0x7f) |
---|
100 | |
---|
101 | static void |
---|
102 | videoPutChar(char car) |
---|
103 | { |
---|
104 | unsigned short *pt_bitmap = bitMapBaseAddr + row * maxCol + column; |
---|
105 | |
---|
106 | switch (car) { |
---|
107 | case '\b': { |
---|
108 | if (column) column--; |
---|
109 | /* Move cursor on the previous location */ |
---|
110 | wr_cursor(row * maxCol + column, ioCrtBaseAddr); |
---|
111 | return; |
---|
112 | } |
---|
113 | case '\t': { |
---|
114 | int i; |
---|
115 | |
---|
116 | i = TAB_SPACE - (column & (TAB_SPACE - 1)); |
---|
117 | column += i; |
---|
118 | if (column >= maxCol) { |
---|
119 | doCRNL(1,1); |
---|
120 | return; |
---|
121 | } |
---|
122 | while (i--) *pt_bitmap++ = ' ' | attribute; |
---|
123 | wr_cursor(row * maxCol + column, ioCrtBaseAddr); |
---|
124 | return; |
---|
125 | } |
---|
126 | case '\n': { |
---|
127 | doCRNL(0,1); |
---|
128 | return; |
---|
129 | } |
---|
130 | case 7: { /* Bell code must be inserted here */ |
---|
131 | return; |
---|
132 | } |
---|
133 | case '\r' : { |
---|
134 | doCRNL(1,0); |
---|
135 | return; |
---|
136 | } |
---|
137 | case BLANK: { |
---|
138 | *pt_bitmap = ' ' | attribute; |
---|
139 | /* DONT move the cursor... */ |
---|
140 | return; |
---|
141 | } |
---|
142 | default: { |
---|
143 | *pt_bitmap = (unsigned char)car | attribute; |
---|
144 | advanceCursor(); |
---|
145 | return; |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | /* trivial state machine to handle escape sequences: |
---|
151 | * |
---|
152 | * --------------------------------- |
---|
153 | * | | |
---|
154 | * | | |
---|
155 | * KEY: esc V [ DCABHKJ esc | |
---|
156 | * STATE: 0 -----> 27 -----> '[' ----------> -1 ----- |
---|
157 | * ^\ \ \ \ |
---|
158 | * KEY: | \other \ other \ other \ other |
---|
159 | * <------------------------------------- |
---|
160 | * |
---|
161 | * in state '-1', the DCABHKJ cases are handled |
---|
162 | * |
---|
163 | * (cursor motion and screen clearing) |
---|
164 | */ |
---|
165 | |
---|
166 | #define DONE (-1) |
---|
167 | |
---|
168 | static int |
---|
169 | handleEscape(int oldState, char car) |
---|
170 | { |
---|
171 | int rval = 0; |
---|
172 | int ro,co; |
---|
173 | |
---|
174 | switch ( oldState ) { |
---|
175 | case DONE: /* means the previous char terminated an ESC sequence... */ |
---|
176 | case 0: |
---|
177 | if ( 27 == car ) { |
---|
178 | rval = 27; /* START of an ESC sequence */ |
---|
179 | } |
---|
180 | break; |
---|
181 | |
---|
182 | case 27: |
---|
183 | if ( '[' == car ) { |
---|
184 | rval = car; /* received ESC '[', so far */ |
---|
185 | } else { |
---|
186 | /* dump suppressed 'ESC'; outch will append the char */ |
---|
187 | videoPutChar(ESC); |
---|
188 | } |
---|
189 | break; |
---|
190 | |
---|
191 | case '[': |
---|
192 | /* handle 'ESC' '[' sequences here */ |
---|
193 | ro = row; co = column; |
---|
194 | rval = DONE; /* done */ |
---|
195 | |
---|
196 | switch (car) { |
---|
197 | case 'D': /* left */ |
---|
198 | if ( co > 0 ) co--; |
---|
199 | break; |
---|
200 | case 'C': /* right */ |
---|
201 | if ( co < maxCol ) co++; |
---|
202 | break; |
---|
203 | case 'A': /* up */ |
---|
204 | if ( ro > 0 ) ro--; |
---|
205 | break; |
---|
206 | case 'B': /* down */ |
---|
207 | if ( ro < maxRow ) ro++; |
---|
208 | break; |
---|
209 | case 'H': /* home */ |
---|
210 | ro = co = 0; |
---|
211 | break; |
---|
212 | case 'K': /* clear to end of line */ |
---|
213 | while ( column < maxCol - 1 ) |
---|
214 | videoPutChar(' '); |
---|
215 | videoPutChar(BLANK); |
---|
216 | break; |
---|
217 | case 'J': /* clear to end of screen */ |
---|
218 | while ( ((row < maxRow-1) || (column < maxCol-1)) ) |
---|
219 | videoPutChar(' '); |
---|
220 | videoPutChar(BLANK); |
---|
221 | break; |
---|
222 | default: |
---|
223 | videoPutChar(ESC); |
---|
224 | videoPutChar('['); |
---|
225 | /* DONT move the cursor */ |
---|
226 | ro = -1; |
---|
227 | rval = 0; |
---|
228 | break; |
---|
229 | } |
---|
230 | /* reset cursor */ |
---|
231 | if ( ro >= 0) |
---|
232 | gotorc(ro,co); |
---|
233 | |
---|
234 | default: |
---|
235 | break; |
---|
236 | |
---|
237 | } |
---|
238 | |
---|
239 | return rval; |
---|
240 | } |
---|
241 | |
---|
242 | void |
---|
243 | clear_screen(void) |
---|
244 | { |
---|
245 | int i,j; |
---|
246 | |
---|
247 | for (j = 0; j <= maxRow; j++) { |
---|
248 | for (i = 0; i <= maxCol; i++) { |
---|
249 | videoPutChar(' '); |
---|
250 | } |
---|
251 | } |
---|
252 | column = 0; |
---|
253 | row = 0; |
---|
254 | } |
---|
255 | |
---|
256 | /*-------------------------------------------------------------------------+ |
---|
257 | | Function: _IBMPC_outch |
---|
258 | | Description: Higher level (console) interface to consPutc. |
---|
259 | | Global Variables: None. |
---|
260 | | Arguments: c - character to write to console. |
---|
261 | | Returns: Nothing. |
---|
262 | +--------------------------------------------------------------------------*/ |
---|
263 | void |
---|
264 | _IBMPC_outch(char c) |
---|
265 | { |
---|
266 | static int escaped = 0; |
---|
267 | |
---|
268 | if ( ! (escaped = handleEscape(escaped, c)) ) { |
---|
269 | if ( '\n' == c ) |
---|
270 | videoPutChar('\r'); |
---|
271 | videoPutChar(c); |
---|
272 | } |
---|
273 | } /* _IBMPC_outch */ |
---|
274 | |
---|
275 | /*-------------------------------------------------------------------------+ |
---|
276 | | Function: _IBMPC_initVideo |
---|
277 | | Description: Video system initialization. Hook for any early setup. |
---|
278 | | Global Variables: bitMapBaseAddr, ioCrtBaseAddr, maxCol, maxRow, row |
---|
279 | | column, attribute, nLines; |
---|
280 | | Arguments: None. |
---|
281 | | Returns: Nothing. |
---|
282 | +--------------------------------------------------------------------------*/ |
---|
283 | void |
---|
284 | _IBMPC_initVideo(void) |
---|
285 | { |
---|
286 | unsigned char* pt = (unsigned char*) (VIDEO_MODE_ADDR); |
---|
287 | |
---|
288 | if (*pt == VGAMODE7) { |
---|
289 | bitMapBaseAddr = (unsigned short*) V_MONO; |
---|
290 | } |
---|
291 | else { |
---|
292 | bitMapBaseAddr = (unsigned short*) V_COLOR; |
---|
293 | } |
---|
294 | ioCrtBaseAddr = *(unsigned short*) DISPLAY_CRT_BASE_IO_ADDR; |
---|
295 | maxCol = * (unsigned short*) NB_MAX_COL_ADDR; |
---|
296 | maxRow = * (unsigned char*) NB_MAX_ROW_ADDR; |
---|
297 | column = 0; |
---|
298 | row = 0; |
---|
299 | attribute = ((BLACK << 4) | WHITE)<<8; |
---|
300 | nLines = 0; |
---|
301 | clear_screen(); |
---|
302 | #ifdef DEBUG_EARLY_STAGE |
---|
303 | printk("bitMapBaseAddr = %X, display controller base IO = %X\n", |
---|
304 | (unsigned) bitMapBaseAddr, |
---|
305 | (unsigned) ioCrtBaseAddr); |
---|
306 | videoPrintf("maxCol = %d, maxRow = %d\n", (unsigned) maxCol, (unsigned) maxRow); |
---|
307 | #endif |
---|
308 | } /* _IBMPC_initVideo */ |
---|
309 | |
---|
310 | /* for old DOS compatibility n-curses type of applications */ |
---|
311 | void gotoxy( int x, int y ) |
---|
312 | { |
---|
313 | gotorc(y,x); |
---|
314 | } |
---|
315 | |
---|
316 | int whereX( void ) |
---|
317 | { |
---|
318 | return row; |
---|
319 | } |
---|
320 | |
---|
321 | int whereY( void ) |
---|
322 | { |
---|
323 | return column; |
---|
324 | } |
---|