2 * thunix/kernel/console.c
4 * The implemention of console write and something about console.
6 * Aleaxander (C) 2007-2008
13 /*#include <string.h>*/
15 #include <asm/system.h>
16 /*#include <keyboard.h>*/
18 static unsigned long video_num_columns
;
19 static unsigned long video_size_row
;
20 static unsigned long video_size_all
;
21 static unsigned long video_num_lines
;
22 static unsigned long video_mem_start
;
23 static unsigned long video_mem_end
;
24 static unsigned long origin
;
25 static unsigned long scr_end
;
26 static unsigned long pos
;
27 static unsigned long x
,y
;
28 static unsigned long top
,bottom
;
29 //static unsigned char attr = 0x07;
30 //static unsigned char space = 0x20;
31 static unsigned short video_port_reg
;
32 static unsigned short video_port_val
;
33 static unsigned short video_erase_char
;
35 static void sysbeep(void);
38 static inline void gotoxy(unsigned int new_x
, unsigned int new_y
)
40 if (new_x
> video_num_columns
|| new_y
>= video_num_lines
)
44 pos
= origin
+ y
*video_size_row
+ (x
<< 1);
45 /* pos = origin + (y<<8) + (y<<6) +(x<<1); */
48 static inline void set_origin(void)
51 outb_p(12,video_port_reg
);
52 outb_p(0xff&((origin
-video_mem_start
)>>9),video_port_val
);
53 outb_p(13,video_port_reg
);
54 outb_p(0xff&((origin
-video_mem_start
)>>1),video_port_val
);
63 for (; i
< video_size_all
/2; i
++ ) {
64 con_write((char *)&c
, 1);
71 static void scrup(void)
73 if (!top
&& bottom
== video_num_lines
) {
74 origin
+= video_size_row
;
75 pos
+= video_size_row
;
76 scr_end
+= video_size_row
;
77 if (scr_end
> video_mem_end
) {
81 "movl video_num_columns,%1\n\t"
84 ::"a" (video_erase_char
),
85 "c" ((video_num_lines
-1)*video_num_columns
>>1),
86 "D" (video_mem_start
),
88 scr_end
-= origin
-video_mem_start
;
89 pos
-= origin
-video_mem_start
;
90 origin
= video_mem_start
;
95 ::"a" (video_erase_char
),
96 "c" (video_num_columns
),
97 "D" (scr_end
-video_size_row
) );
105 "movl video_num_columns,%%ecx\n\t"
108 ::"a" (video_erase_char
),
109 "c" ((bottom
-top
-1)*video_num_columns
>>1),
110 "D" (origin
+video_size_row
*top
),
111 "S" (origin
+video_size_row
*(top
+1)));
115 static void scrdown(void)
121 "movl video_num_columns,%%ecx\n\t"
124 ::"a" (video_erase_char
),
125 "c" ((bottom
-top
-1)*video_num_columns
>>1),
126 "D" (origin
+video_size_row
*bottom
-4),
127 "S" (origin
+video_size_row
*(bottom
-1)-4)
134 if (y
+ 1 < bottom
) {
136 pos
= pos
+ video_size_row
;
146 pos
-= video_size_row
;
158 static void del(void)
163 *(unsigned short *) pos
= video_erase_char
;
167 static void insert_char(void)
170 unsigned short tmp
, old
= video_erase_char
;
171 unsigned short * p
= (unsigned short *)pos
;
173 while (i
++ < video_num_columns
) {
181 static void insert_line(void)
183 int oldtop
, oldbottom
;
187 bottom
= video_num_lines
;
193 static void delete_char(void)
196 unsigned short *p
= (unsigned short *) pos
;
198 if (x
>= video_num_columns
)
202 while (++i
< video_num_columns
) {
206 *p
= video_erase_char
;
209 static void delete_line(void)
211 int oldtop
, oldbottom
;
216 bottom
= video_num_lines
;
223 static int saved_x
= 0;
224 static int saved_y
= 0;
226 static void save_cur(void)
232 static void restore_cur(void)
234 gotoxy(saved_x
, saved_y
);
237 void get_cursor(int *new_x
, int *new_y
)
243 void set_cursor(void)
246 outb_p(14, video_port_reg
);
247 outb_p(0xff & ((pos
- video_mem_start
)>>9), video_port_val
);
248 outb_p(15, video_port_reg
);
249 outb_p(0xff & ((pos
- video_mem_start
)>>1), video_port_val
);
255 /* We have seen os goes there ! */
256 extern void keyboard_interrupt(void);
258 video_num_columns
= ORIG_VIDEO_COLS
;
259 video_size_row
= video_num_columns
* 2;
260 video_num_lines
= ORIG_VIDEO_LINES
;
261 video_size_all
= video_size_row
* video_num_lines
;
262 video_erase_char
= 0x0720;
264 video_mem_start
= 0xb8000;
265 video_mem_end
= 0xba000;
266 video_port_reg
= 0x3d4;
267 video_port_val
= 0x3d5;
269 origin
= video_mem_start
;
270 scr_end
= video_mem_start
+ video_num_lines
* video_size_row
;
272 bottom
= video_num_lines
;
274 gotoxy(ORIG_X
, ORIG_Y
);
277 gotoxy(ORIG_X
, ORIG_Y
);
282 * Something else need do here
283 * say, keyboard interrupt and so on
288 void con_write(char *buf
, int nr
)
294 case 10: case 11: case 12:
313 *(unsigned short*)pos
= video_erase_char
;
321 if (x
> video_num_columns
) {
322 x
-=video_num_columns
;
323 pos
-=video_size_row
;
331 /* special key handled here */
332 case 0xE0: /* HOME */
338 x
= video_num_columns
- 1;
353 if ( y
== video_num_lines
) {
361 case 0xE4: /* LeFt */
363 x
= video_num_columns
;
370 case 0xE5: /* RighT */
371 if (x
== video_num_columns
) {
379 case 0xE6: /* PGUP */
381 case 0xE7: /* PGDN */
388 if (x
>= video_num_columns
) {
389 x
-= video_num_columns
;
390 pos
-= video_size_row
;
394 *(unsigned char *)pos
++ = c
;
395 *(unsigned char *)pos
++ = 0x02;
405 static void sysbeep(void)
416 * Following functions shoud be in lib dir but not created yet,
419 extern int printk(char *fmt
, ...);
422 return printk("%s",s
);