Diff markup
001 001
002 002
003 003
004 004
005 005
006 006
007 007
008 008
009 009
010 010
011 011
012 012
013 013
014 014
015 015
016 016
017 017
018 #include <sos/klibc.h> 018 #include <sos/klibc.h>
019 #include <hwcore/ioports.h> 019 #include <hwcore/ioports.h>
020 020
021 #include "x86_videomem.h" 021 #include "x86_videomem.h"
022 022
023 023
024 024
025 025
026 #define VIDEO 0xb8000 026 #define VIDEO 0xb8000
027 027
028 028
029 029
030 #define LINES 25 030 #define LINES 25
031 #define COLUMNS 80 031 #define COLUMNS 80
032 032
033 033
034 034
035 035
036 036
037 037
038 038
039 039
040 040
041 #define VGA_COMMAND_PORT 0x3D4 041 #define VGA_COMMAND_PORT 0x3D4
042 #define VGA_DATA_PORT 0x3D5 042 #define VGA_DATA_PORT 0x3D5
043 043
044 044
045 #define VGA_SET_CURSOR_START 0xA 045 #define VGA_SET_CURSOR_START 0xA
046 #define VGA_SET_CURSOR_END 0xB 046 #define VGA_SET_CURSOR_END 0xB
047 #define VGA_SET_CURSOR_HIGH 0xE 047 #define VGA_SET_CURSOR_HIGH 0xE
048 #define VGA_SET_CURSOR_LOW 0xF 048 #define VGA_SET_CURSOR_LOW 0xF
049 049
050 050
051 051
052 typedef struct { 052 typedef struct {
053 unsigned char character; 053 unsigned char character;
054 unsigned char attribute; 054 unsigned char attribute;
055 } __attribute__ ((packed)) x86_video_mem[LINES 055 } __attribute__ ((packed)) x86_video_mem[LINES*COLUMNS];
056 056
057 057
058 058
059 059
060 static volatile x86_video_mem *video = (volati 060 static volatile x86_video_mem *video = (volatile x86_video_mem*)VIDEO;
061 061
062 sos_ret_t sos_x86_videomem_setup(void) 062 sos_ret_t sos_x86_videomem_setup(void)
063 { 063 {
064 064
065 065
066 outb(0x0a, VGA_COMMAND_PORT); 066 outb(0x0a, VGA_COMMAND_PORT);
067 067
068 068
069 outb(1 << 5, VGA_DATA_PORT); 069 outb(1 << 5, VGA_DATA_PORT);
070 070
071 return SOS_OK; 071 return SOS_OK;
072 } 072 }
073 073
074 074
075 sos_ret_t sos_x86_videomem_cls(unsigned char a 075 sos_ret_t sos_x86_videomem_cls(unsigned char attribute)
076 { 076 {
077 077
078 int i; 078 int i;
079 for(i = 0 ; i < LINES*COLUMNS ; i++) 079 for(i = 0 ; i < LINES*COLUMNS ; i++)
080 { 080 {
081 (*video)[i].character = 0; 081 (*video)[i].character = 0;
082 (*video)[i].attribute = attribute; 082 (*video)[i].attribute = attribute;
083 } 083 }
084 084
085 return SOS_OK; 085 return SOS_OK;
086 } 086 }
087 087
088 088
089 sos_ret_t sos_x86_videomem_putstring(unsigned 089 sos_ret_t sos_x86_videomem_putstring(unsigned char row, unsigned char col,
090 unsigned 090 unsigned char attribute,
091 const cha 091 const char *str)
092 { 092 {
093 unsigned video_offs = row*COLUMNS + col; 093 unsigned video_offs = row*COLUMNS + col;
094 094
095 if (video_offs >= LINES*COLUMNS) 095 if (video_offs >= LINES*COLUMNS)
096 return -SOS_EINVAL; 096 return -SOS_EINVAL;
097 097
098 for ( ; str && *str && (video_offs < LINES*C 098 for ( ; str && *str && (video_offs < LINES*COLUMNS) ; str++, video_offs++)
099 { 099 {
100 (*video)[video_offs].character = (unsign 100 (*video)[video_offs].character = (unsigned char)*str;
101 (*video)[video_offs].attribute = attribu 101 (*video)[video_offs].attribute = attribute;
102 } 102 }
103 103
104 return SOS_OK; 104 return SOS_OK;
105 } 105 }
106 106
107 107
108 sos_ret_t sos_x86_videomem_putchar(unsigned ch 108 sos_ret_t sos_x86_videomem_putchar(unsigned char row, unsigned char col,
109 unsigned ch 109 unsigned char attribute,
110 unsigned ch 110 unsigned char c)
111 { 111 {
112 unsigned video_offs = row*COLUMNS + col; 112 unsigned video_offs = row*COLUMNS + col;
113 113
114 if (video_offs >= LINES*COLUMNS) 114 if (video_offs >= LINES*COLUMNS)
115 return -SOS_EINVAL; 115 return -SOS_EINVAL;
116 116
117 (*video)[video_offs].character = c; 117 (*video)[video_offs].character = c;
118 (*video)[video_offs].attribute = attribute; 118 (*video)[video_offs].attribute = attribute;
119 119
120 return SOS_OK; 120 return SOS_OK;
121 } 121 }
122 122
123 123
124 sos_ret_t sos_x86_videomem_printf(unsigned cha 124 sos_ret_t sos_x86_videomem_printf(unsigned char row, unsigned char col,
125 unsigned cha 125 unsigned char attribute,
126 const char * 126 const char *format, ...)
127 { 127 {
128 char buff[256]; 128 char buff[256];
129 va_list ap; 129 va_list ap;
130 130
131 va_start(ap, format); 131 va_start(ap, format);
132 vsnprintf(buff, sizeof(buff), format, ap); 132 vsnprintf(buff, sizeof(buff), format, ap);
133 va_end(ap); 133 va_end(ap);
134 134
135 return sos_x86_videomem_putstring(row, col, 135 return sos_x86_videomem_putstring(row, col, attribute, buff);
136 } 136 }
137 137
138 138
139 139
140 140
141 141
142 142
143 143
144 144
145 145
146 146
147 147
148 static int row; 148 static int row;
149 149
150 150
151 151
152 static int col; 152 static int col;
153 153
154 154
155 155
156 156
157 #define CONSOLE_ROW_START 12 157 #define CONSOLE_ROW_START 12
158 158
159 static void sos_screen_set_cursor (unsigned in !! 159 static void sos_screen_set_cursor (unsigned int row, unsigned int col)
160 { 160 {
161 unsigned int pos; 161 unsigned int pos;
162 162
163 pos = (_row * COLUMNS + _col); !! 163 pos = (row * COLUMNS + col);
164 164
165 outb(VGA_SET_CURSOR_HIGH, VGA_COMMAND_PORT); 165 outb(VGA_SET_CURSOR_HIGH, VGA_COMMAND_PORT);
166 outb( (pos >> 8), VGA_DATA_PORT); 166 outb( (pos >> 8), VGA_DATA_PORT);
167 outb(VGA_SET_CURSOR_LOW, VGA_COMMAND_PORT); 167 outb(VGA_SET_CURSOR_LOW, VGA_COMMAND_PORT);
168 outb( (pos & 0xFF), VGA_DATA_PORT); 168 outb( (pos & 0xFF), VGA_DATA_PORT);
169 } 169 }
170 170
171 sos_ret_t sos_screen_putchar (char c) 171 sos_ret_t sos_screen_putchar (char c)
172 { 172 {
173 if (c == '\r') 173 if (c == '\r')
174 { 174 {
175 175
176 col = 0; 176 col = 0;
177 } 177 }
178 178
179 179
180 else if (c == '\n') 180 else if (c == '\n')
181 { 181 {
182 182
183 col = 0; 183 col = 0;
184 row ++; 184 row ++;
185 } 185 }
186 186
187 187
188 else if (c == '\b') 188 else if (c == '\b')
189 { 189 {
190 190
191 191
192 col --; 192 col --;
193 193
194 194
195 if (col < 0) 195 if (col < 0)
196 { 196 {
197 row --; 197 row --;
198 col = COLUMNS-1; 198 col = COLUMNS-1;
199 199
200 if (row < CONSOLE_ROW_START) 200 if (row < CONSOLE_ROW_START)
201 { 201 {
202 row = CONSOLE_ROW_START; 202 row = CONSOLE_ROW_START;
203 col = 0; 203 col = 0;
204 } 204 }
205 } 205 }
206 206
207 207
208 sos_x86_videomem_putchar 208 sos_x86_videomem_putchar
209 (row, col, SOS_X86_VIDEO_FG_BLUE | SOS 209 (row, col, SOS_X86_VIDEO_FG_BLUE | SOS_X86_VIDEO_BG_LTGRAY, ' ');
210 } 210 }
211 else if (c != 0) 211 else if (c != 0)
212 { 212 {
213 sos_x86_videomem_putchar 213 sos_x86_videomem_putchar
214 (row, col, SOS_X86_VIDEO_FG_BLUE | SOS 214 (row, col, SOS_X86_VIDEO_FG_BLUE | SOS_X86_VIDEO_BG_LTGRAY, c);
215 col++; 215 col++;
216 if (col == COLUMNS) 216 if (col == COLUMNS)
217 { 217 {
218 col = 0; 218 col = 0;
219 row++; 219 row++;
220 } 220 }
221 } 221 }
222 222
223 223
224 if (row == LINES) 224 if (row == LINES)
225 { 225 {
226 int i; 226 int i;
227 227
228 228
229 for (i = CONSOLE_ROW_START; i < LINES; i 229 for (i = CONSOLE_ROW_START; i < LINES; i++)
230 memcpy ((char*) video + i * COLUMNS * 230 memcpy ((char*) video + i * COLUMNS * 2,
231 (char*) video + ((i + 1) * COL 231 (char*) video + ((i + 1) * COLUMNS * 2),
232 COLUMNS * 2); 232 COLUMNS * 2);
233 233
234 234
235 for (i = 0; i < COLUMNS; i++) 235 for (i = 0; i < COLUMNS; i++)
236 sos_x86_videomem_putchar 236 sos_x86_videomem_putchar
237 (LINES-1, i, SOS_X86_VIDEO_FG_BLUE | 237 (LINES-1, i, SOS_X86_VIDEO_FG_BLUE | SOS_X86_VIDEO_BG_LTGRAY, ' ');
238 238
239 row--; 239 row--;
240 } 240 }
241 241
242 sos_screen_set_cursor (row, col); 242 sos_screen_set_cursor (row, col);
243 243
244 return SOS_OK; 244 return SOS_OK;
245 } 245 }
246 246
247 sos_ret_t sos_screen_init (void) 247 sos_ret_t sos_screen_init (void)
248 { 248 {
249 int i, j; 249 int i, j;
250 250
251 row = CONSOLE_ROW_START; 251 row = CONSOLE_ROW_START;
252 col = 0; 252 col = 0;
253 253
254 254
255 255
256 256
257 outb(VGA_SET_CURSOR_START, VGA_COMMAND_PORT) 257 outb(VGA_SET_CURSOR_START, VGA_COMMAND_PORT);
258 outb(((0x2 << 5) | 14), VGA_DATA_PORT); 258 outb(((0x2 << 5) | 14), VGA_DATA_PORT);
259 259
260 for (i = CONSOLE_ROW_START; i < LINES; i++) 260 for (i = CONSOLE_ROW_START; i < LINES; i++)
261 { 261 {
262 for (j = 0; j < COLUMNS; j++) 262 for (j = 0; j < COLUMNS; j++)
263 sos_x86_videomem_putchar 263 sos_x86_videomem_putchar
264 (i, j, SOS_X86_VIDEO_FG_BLUE | SOS_X 264 (i, j, SOS_X86_VIDEO_FG_BLUE | SOS_X86_VIDEO_BG_LTGRAY, ' ');
265 } 265 }
266 266
267 sos_screen_set_cursor (row, col); 267 sos_screen_set_cursor (row, col);
268 268
269 return SOS_OK; 269 return SOS_OK;
270 } 270 }