wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Hyperion Entertainment, Hans-JoergF@hyperion-entertainment.com |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <devices.h> |
| 26 | #include "memio.h" |
| 27 | #include <part.h> |
| 28 | |
| 29 | unsigned char *cursor_position; |
| 30 | unsigned int cursor_row; |
| 31 | unsigned int cursor_col; |
| 32 | |
| 33 | unsigned char current_attr; |
| 34 | |
| 35 | unsigned int video_numrows = 25; |
| 36 | unsigned int video_numcols = 80; |
| 37 | unsigned int video_scrolls = 0; |
| 38 | |
| 39 | #define VIDEO_BASE (unsigned char *)0xFD0B8000 |
| 40 | #define VIDEO_ROWS video_numrows |
| 41 | #define VIDEO_COLS video_numcols |
| 42 | #define VIDEO_PITCH (2 * video_numcols) |
| 43 | #define VIDEO_SIZE (video_numrows * video_numcols * 2) |
| 44 | #define VIDEO_NAME "vga" |
| 45 | |
| 46 | void video_test(void); |
| 47 | void video_putc(char ch); |
| 48 | void video_puts(char *string); |
| 49 | void video_scroll(int rows); |
| 50 | void video_banner(void); |
| 51 | int video_init(void); |
| 52 | int video_start(void); |
| 53 | int video_rows(void); |
| 54 | int video_cols(void); |
| 55 | |
| 56 | char *prompt_string = "=>"; |
| 57 | |
| 58 | void video_set_color(unsigned char attr) |
| 59 | { |
| 60 | unsigned char *fb = (unsigned char *)VIDEO_BASE; |
| 61 | int i; |
| 62 | |
| 63 | current_attr = video_get_attr(); |
| 64 | |
| 65 | for (i=0; i<VIDEO_SIZE; i+=2) |
| 66 | { |
| 67 | *(fb+i+1) = current_attr; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | unsigned char video_get_attr(void) |
| 72 | { |
| 73 | char *s; |
| 74 | unsigned char attr; |
| 75 | |
| 76 | attr = 0x0f; |
| 77 | |
| 78 | s = getenv("vga_fg_color"); |
| 79 | if (s) |
| 80 | { |
| 81 | attr = atoi(s); |
| 82 | } |
| 83 | |
| 84 | s = getenv("vga_bg_color"); |
| 85 | if (s) |
| 86 | { |
| 87 | attr |= atoi(s)<<4; |
| 88 | } |
| 89 | |
| 90 | return attr; |
| 91 | } |
| 92 | |
| 93 | int video_inited = 0; |
| 94 | |
| 95 | int drv_video_init(void) |
| 96 | { |
| 97 | int error, devices = 1 ; |
| 98 | device_t vgadev ; |
| 99 | if (video_inited) return 1; |
| 100 | video_inited = 1; |
| 101 | video_init(); |
| 102 | memset (&vgadev, 0, sizeof(vgadev)); |
| 103 | |
| 104 | strcpy(vgadev.name, VIDEO_NAME); |
| 105 | vgadev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_SYSTEM; |
| 106 | vgadev.putc = video_putc; |
| 107 | vgadev.puts = video_puts; |
| 108 | vgadev.getc = NULL; |
| 109 | vgadev.tstc = NULL; |
| 110 | vgadev.start = video_start; |
| 111 | |
| 112 | error = device_register (&vgadev); |
| 113 | |
| 114 | if (error == 0) |
| 115 | { |
| 116 | char *s = getenv("stdout"); |
| 117 | if (s && strcmp(s, VIDEO_NAME)==0) |
| 118 | { |
| 119 | if (overwrite_console()) return 1; |
| 120 | error = console_assign(stdout, VIDEO_NAME); |
| 121 | if (error == 0) return 1; |
| 122 | else return error; |
| 123 | } |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | return error; |
| 128 | } |
| 129 | |
| 130 | int video_init(void) |
| 131 | { |
| 132 | cursor_position = VIDEO_BASE; // Color text display base |
| 133 | cursor_row = 0; |
| 134 | cursor_col = 0; |
| 135 | current_attr = video_get_attr(); // Currently selected value for attribute. |
| 136 | // video_test(); |
| 137 | video_set_color(current_attr); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | void video_set_cursor(int line, int column) |
| 143 | { |
| 144 | unsigned short offset = line*video_numcols + column; |
| 145 | cursor_position = VIDEO_BASE + line*VIDEO_PITCH + column*2; |
| 146 | out_byte(0x3D4, 0x0E); |
| 147 | out_byte(0x3D5, offset/256); |
| 148 | out_byte(0x3D4, 0x0F); |
| 149 | out_byte(0x3D5, offset%256); |
| 150 | } |
| 151 | |
| 152 | void video_write_char(int character) |
| 153 | { |
| 154 | *cursor_position = character; |
| 155 | *(cursor_position+1) = current_attr; |
| 156 | } |
| 157 | |
| 158 | void video_test(void) |
| 159 | { |
| 160 | |
| 161 | } |
| 162 | |
| 163 | void video_putc(char ch) |
| 164 | { |
| 165 | switch(ch) |
| 166 | { |
| 167 | case '\n': |
| 168 | cursor_col = 0; |
| 169 | cursor_row += 1; |
| 170 | break; |
| 171 | case '\r': |
| 172 | cursor_col = 0; |
| 173 | break; |
| 174 | case '\b': |
| 175 | if (cursor_col) cursor_col--; |
| 176 | else return; |
| 177 | break; |
| 178 | case '\t': |
| 179 | cursor_col = (cursor_col/8+1)*8; |
| 180 | break; |
| 181 | default: |
| 182 | video_write_char(ch); |
| 183 | cursor_col++; |
| 184 | if (cursor_col > VIDEO_COLS-1) |
| 185 | { |
| 186 | cursor_row++; |
| 187 | cursor_col=0; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | if (cursor_row > VIDEO_ROWS-1) |
| 192 | video_scroll(1); |
| 193 | video_set_cursor(cursor_row, cursor_col); |
| 194 | } |
| 195 | |
| 196 | void video_scroll(int rows) |
| 197 | { |
| 198 | unsigned short clear = ((unsigned short)current_attr) | (' '<<8); |
| 199 | unsigned short* addr16 = &((unsigned short *)VIDEO_BASE)[(VIDEO_ROWS-rows)*VIDEO_COLS]; |
| 200 | int i; |
| 201 | char *s; |
| 202 | |
| 203 | s = getenv("vga_askscroll"); |
| 204 | video_scrolls += rows; |
| 205 | |
| 206 | if (video_scrolls >= video_numrows) |
| 207 | { |
| 208 | if (s && strcmp(s, "yes")) |
| 209 | { |
| 210 | while (-1 == tstc()); |
| 211 | } |
| 212 | |
| 213 | video_scrolls = 0; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | memcpy(VIDEO_BASE, VIDEO_BASE+rows*(VIDEO_COLS*2), (VIDEO_ROWS-rows)*(VIDEO_COLS*2)); |
| 218 | for (i = 0 ; i < rows * VIDEO_COLS ; i++) |
| 219 | addr16[i] = clear; |
| 220 | cursor_row-=rows; |
| 221 | cursor_col=0; |
| 222 | } |
| 223 | |
| 224 | void video_puts(char *string) |
| 225 | { |
| 226 | while (*string) |
| 227 | { |
| 228 | video_putc(*string); |
| 229 | string++; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | int video_start(void) |
| 234 | { |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | unsigned char video_single_box[] = |
| 239 | { |
| 240 | 218, 196, 191, |
| 241 | 179, 179, |
| 242 | 192, 196, 217 |
| 243 | }; |
| 244 | |
| 245 | unsigned char video_double_box[] = |
| 246 | { |
| 247 | 201, 205, 187, |
| 248 | 186, 186, |
| 249 | 200, 205, 188 |
| 250 | }; |
| 251 | |
| 252 | unsigned char video_single_title[] = |
| 253 | { |
| 254 | 195, 196, 180, 180, 195 |
| 255 | }; |
| 256 | |
| 257 | unsigned char video_double_title[] = |
| 258 | { |
| 259 | 204, 205, 185, 181, 198 |
| 260 | }; |
| 261 | |
| 262 | #define SINGLE_BOX 0 |
| 263 | #define DOUBLE_BOX 1 |
| 264 | |
| 265 | unsigned char *video_addr(int x, int y) |
| 266 | { |
| 267 | return VIDEO_BASE + 2*(VIDEO_COLS*y) + 2*x; |
| 268 | } |
| 269 | |
| 270 | void video_bios_print_string(char *s, int x, int y, int attr, int count) |
| 271 | { |
| 272 | int cattr = current_attr; |
| 273 | if (attr != -1) current_attr = attr; |
| 274 | video_set_cursor(x,y); |
| 275 | while (count) |
| 276 | { |
| 277 | char c = *s++; |
| 278 | if (attr == -1) current_attr = *s++; |
| 279 | video_putc(c); |
| 280 | count--; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void video_draw_box(int style, int attr, char *title, int separate, int x, int y, int w, int h) |
| 285 | { |
| 286 | unsigned char *fb, *fb2; |
| 287 | unsigned char *st = (style == SINGLE_BOX)?video_single_box : video_double_box; |
| 288 | unsigned char *ti = (style == SINGLE_BOX)?video_single_title : video_double_title; |
| 289 | int i; |
| 290 | |
| 291 | fb = video_addr(x,y); |
| 292 | *(fb) = st[0]; |
| 293 | *(fb+1) = attr; |
| 294 | fb += 2; |
| 295 | |
| 296 | fb2 = video_addr(x,y+h-1); |
| 297 | *(fb2) = st[5]; |
| 298 | *(fb2+1) = attr; |
| 299 | fb2 += 2; |
| 300 | |
| 301 | for (i=0; i<w-2;i++) |
| 302 | { |
| 303 | *fb = st[1]; |
| 304 | fb++; |
| 305 | *fb = attr; |
| 306 | fb++; |
| 307 | |
| 308 | *fb2 = st[6]; |
| 309 | fb2++; |
| 310 | *fb2 = attr; |
| 311 | fb2++; |
| 312 | |
| 313 | } |
| 314 | *fb = st[2]; |
| 315 | *(fb+1) = attr; |
| 316 | |
| 317 | *fb2 = st[7]; |
| 318 | *(fb2+1) = attr; |
| 319 | |
| 320 | fb = video_addr(x, y+1); |
| 321 | fb2 = video_addr(x+w-1, y+1); |
| 322 | for (i=0; i<h-2; i++) |
| 323 | { |
| 324 | *fb = st[3]; |
| 325 | *(fb+1) = attr; fb += 2*VIDEO_COLS; |
| 326 | |
| 327 | *fb2 = st[4]; |
| 328 | *(fb2+1) = attr; fb2 += 2*VIDEO_COLS; |
| 329 | } |
| 330 | |
| 331 | // Draw title |
| 332 | if (title) |
| 333 | { |
| 334 | if (separate == 0) |
| 335 | { |
| 336 | fb = video_addr(x+1, y); |
| 337 | *fb = ti[3]; |
| 338 | fb += 2; |
| 339 | *fb = ' '; |
| 340 | fb += 2; |
| 341 | while (*title) |
| 342 | { |
| 343 | *fb = *title; |
| 344 | fb ++; |
| 345 | *fb = attr; |
| 346 | fb++; title++; |
| 347 | } |
| 348 | *fb = ' '; |
| 349 | fb += 2; |
| 350 | *fb = ti[4]; |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | fb = video_addr(x, y+2); |
| 355 | *fb = ti[0]; |
| 356 | fb += 2; |
| 357 | for (i=0; i<w-2; i++) |
| 358 | { |
| 359 | *fb = ti[1]; |
| 360 | *(fb+1) = attr; |
| 361 | fb += 2; |
| 362 | } |
| 363 | *fb = ti[2]; |
| 364 | *(fb+1) = attr; |
| 365 | fb = video_addr(x+1, y+1); |
| 366 | for (i=0; i<w-2; i++) |
| 367 | { |
| 368 | *fb = ' '; |
| 369 | *(fb+1) = attr; |
| 370 | fb += 2; |
| 371 | } |
| 372 | fb = video_addr(x+2, y+1); |
| 373 | |
| 374 | while (*title) |
| 375 | { |
| 376 | *fb = *title; |
| 377 | *(fb+1) = attr; |
| 378 | fb += 2; |
| 379 | title++; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | } |
| 385 | |
| 386 | void video_draw_text(int x, int y, int attr, char *text) |
| 387 | { |
| 388 | unsigned char *fb = video_addr(x,y); |
| 389 | while (*text) |
| 390 | { |
| 391 | *fb++ = *text++; |
| 392 | *fb++ = attr; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | void video_save_rect(int x, int y, int w, int h, void *save_area, int clearchar, int clearattr) |
| 397 | { |
| 398 | unsigned char *save = (unsigned char *)save_area; |
| 399 | unsigned char *fb = video_addr(x,y); |
| 400 | int i,j; |
| 401 | for (i=0; i<h; i++) |
| 402 | { |
| 403 | unsigned char *fbb = fb; |
| 404 | for (j=0; j<w; j++) |
| 405 | { |
| 406 | *save ++ = *fb; |
| 407 | if (clearchar > 0) *fb = clearchar; |
| 408 | fb ++; |
| 409 | *save ++ = *fb; |
| 410 | if (clearattr > 0) *fb = clearattr; |
| 411 | } |
| 412 | fb = fbb + 2*VIDEO_COLS; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | void video_restore_rect(int x, int y, int w, int h, void *save_area) |
| 417 | { |
| 418 | unsigned char *save = (unsigned char *)save_area; |
| 419 | unsigned char *fb = video_addr(x,y); |
| 420 | int i,j; |
| 421 | for (i=0; i<h; i++) |
| 422 | { |
| 423 | unsigned char *fbb = fb; |
| 424 | for (j=0; j<w; j++) |
| 425 | { |
| 426 | *fb ++ = *save ++; |
| 427 | *fb ++ = *save ++; |
| 428 | } |
| 429 | fb = fbb + 2*VIDEO_COLS; |
| 430 | } |
| 431 | |
| 432 | } |
| 433 | |
| 434 | int video_rows(void) |
| 435 | { |
| 436 | return VIDEO_ROWS; |
| 437 | } |
| 438 | |
| 439 | int video_cols(void) |
| 440 | { |
| 441 | return VIDEO_COLS; |
| 442 | } |
| 443 | |
| 444 | void video_size(int cols, int rows) |
| 445 | { |
| 446 | video_numrows = rows; |
| 447 | video_numcols = cols; |
| 448 | } |
| 449 | |
| 450 | void video_clear(void) |
| 451 | { |
| 452 | unsigned short *fbb = (unsigned short *)0xFD0B8000; |
| 453 | int i,j; |
| 454 | unsigned short val = 0x2000 | current_attr; |
| 455 | |
| 456 | for (i=0; i<video_rows(); i++) |
| 457 | { |
| 458 | for (j=0; j<video_cols(); j++) |
| 459 | { |
| 460 | *fbb++ = val; |
| 461 | } |
| 462 | } |
| 463 | video_set_cursor(0,0); |
| 464 | cursor_row = 0; |
| 465 | cursor_col = 0; |
| 466 | } |
| 467 | |
| 468 | #ifdef EASTEREGG |
| 469 | int video_easteregg_active = 0; |
| 470 | |
| 471 | void video_easteregg(void) |
| 472 | { |
| 473 | video_easteregg_active = 1; |
| 474 | } |
| 475 | #endif |
| 476 | |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 477 | extern block_dev_desc_t * ide_get_dev(int dev); |
| 478 | extern char version_string[]; |
| 479 | |
| 480 | void video_banner(void) |
| 481 | { |
| 482 | block_dev_desc_t *ide; |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 483 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 484 | int i; |
| 485 | char *s; |
| 486 | int maxdev; |
| 487 | |
| 488 | |
| 489 | if (video_inited == 0) return; |
| 490 | #ifdef EASTEREGG |
| 491 | if (video_easteregg_active) |
| 492 | { |
| 493 | prompt_string=""; |
| 494 | video_clear(); |
| 495 | printf("\n"); |
| 496 | printf(" **** COMMODORE 64 BASIC X2 ****\n\n"); |
| 497 | printf(" 64K RAM SYSTEM 38911 BASIC BYTES FREE\n\n"); |
| 498 | printf("READY\n"); |
| 499 | } |
| 500 | else |
| 501 | { |
| 502 | #endif |
| 503 | s = getenv("ide_maxbus"); |
| 504 | if (s) |
| 505 | maxdev = atoi(s) * 2; |
| 506 | else |
| 507 | maxdev = 4; |
| 508 | |
| 509 | s = getenv("stdout"); |
| 510 | if (s && strcmp(s, "serial") == 0) |
| 511 | return; |
| 512 | |
| 513 | video_clear(); |
| 514 | printf("%s\n\nCPU: ", version_string); |
| 515 | checkcpu(); |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 516 | printf("DRAM: %ld MB\n", gd->bd->bi_memsize/(1024*1024)); |
| 517 | printf("FSB: %ld MHz\n", gd->bd->bi_busfreq/1000000); |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 518 | |
| 519 | printf("\n---- Disk summary ----\n"); |
| 520 | for (i = 0; i < maxdev; i++) |
| 521 | { |
| 522 | ide = ide_get_dev(i); |
| 523 | printf("Device %d: ", i); |
| 524 | dev_print(ide); |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | video_draw_box(SINGLE_BOX, 0x0F, "Test 1", 0, 0,18, 72, 4); |
| 529 | video_draw_box(DOUBLE_BOX, 0x0F, "Test 2", 1, 4,10, 50, 6); |
| 530 | video_draw_box(DOUBLE_BOX, 0x0F, "Test 3", 0, 40, 3, 20, 5); |
| 531 | |
| 532 | video_draw_text(1, 4, 0x2F, "Highlighted options"); |
| 533 | video_draw_text(1, 5, 0x0F, "Non-selected option"); |
| 534 | video_draw_text(1, 6, 0x07, "disabled option"); |
| 535 | */ |
| 536 | #ifdef EASTEREGG |
| 537 | } |
| 538 | #endif |
| 539 | } |