video.c 10.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
/*
 * (C) Copyright 2002
 * Hyperion Entertainment, Hans-JoergF@hyperion-entertainment.com 
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <devices.h>
#include "memio.h"
#include <part.h>

unsigned char *cursor_position;
unsigned int cursor_row;
unsigned int cursor_col;

unsigned char current_attr;

unsigned int video_numrows = 25;
unsigned int video_numcols = 80;
unsigned int video_scrolls = 0;

#define VIDEO_BASE (unsigned char *)0xFD0B8000
#define VIDEO_ROWS video_numrows
#define VIDEO_COLS video_numcols
#define VIDEO_PITCH (2 * video_numcols)
#define VIDEO_SIZE (video_numrows * video_numcols * 2)
#define VIDEO_NAME "vga"

void video_test(void);
void video_putc(char ch);
void video_puts(char *string);
void video_scroll(int rows);
void video_banner(void);
int  video_init(void);
int  video_start(void);
int  video_rows(void);
int  video_cols(void);

char *prompt_string = "=>";

void video_set_color(unsigned char attr)
{
    unsigned char *fb = (unsigned char *)VIDEO_BASE;
    int i;

    current_attr = video_get_attr();

    for (i=0; i<VIDEO_SIZE; i+=2)
    {
	*(fb+i+1) = current_attr;
    }
}

unsigned char video_get_attr(void)
{
    char *s;
    unsigned char attr;

    attr = 0x0f;

    s = getenv("vga_fg_color");
    if (s)
    {
	attr = atoi(s);
    }

    s = getenv("vga_bg_color");
    if (s)
    {
	attr |= atoi(s)<<4;
    }

    return attr;
}

int video_inited = 0;

int drv_video_init(void)
{
    int error, devices = 1 ;
    device_t vgadev ;
    if (video_inited) return 1;
    video_inited = 1;
    video_init();
    memset (&vgadev, 0, sizeof(vgadev));
    
    strcpy(vgadev.name, VIDEO_NAME);
    vgadev.flags =  DEV_FLAGS_OUTPUT | DEV_FLAGS_SYSTEM;
    vgadev.putc = video_putc;
    vgadev.puts = video_puts;
    vgadev.getc = NULL;
    vgadev.tstc = NULL;
    vgadev.start = video_start;
    
    error = device_register (&vgadev);

    if (error == 0)
    {
	char *s = getenv("stdout");
	if (s && strcmp(s, VIDEO_NAME)==0)
	{
	    if (overwrite_console()) return 1;
	    error = console_assign(stdout, VIDEO_NAME);
	    if (error == 0) return 1;
	    else return error;
	}
	return 1;
    }

    return error;
}

int video_init(void)
{
    cursor_position = VIDEO_BASE; // Color text display base
    cursor_row = 0;
    cursor_col = 0;
    current_attr = video_get_attr(); // Currently selected value for attribute.
//    video_test();
    video_set_color(current_attr);

    return 0;
}

void video_set_cursor(int line, int column)
{
    unsigned short offset = line*video_numcols + column;
    cursor_position = VIDEO_BASE +  line*VIDEO_PITCH + column*2;
    out_byte(0x3D4, 0x0E);
    out_byte(0x3D5, offset/256);
    out_byte(0x3D4, 0x0F);
    out_byte(0x3D5, offset%256);
}

void video_write_char(int character)
{
    *cursor_position = character;
    *(cursor_position+1) = current_attr;
}

void video_test(void)
{

}

void video_putc(char ch)
{
    switch(ch)
    {
    case '\n':
	cursor_col = 0;
	cursor_row += 1;
	break;
    case '\r':
	cursor_col = 0;
	break;
    case '\b':
	if (cursor_col) cursor_col--;
	else return;
	break;
    case '\t':
	cursor_col = (cursor_col/8+1)*8;
	break;
    default:
	video_write_char(ch);
	cursor_col++;
	if (cursor_col > VIDEO_COLS-1)
	{
	    cursor_row++;
	    cursor_col=0;
	}
    }

    if (cursor_row > VIDEO_ROWS-1)
	video_scroll(1);
    video_set_cursor(cursor_row, cursor_col);
}

void video_scroll(int rows)
{
    unsigned short clear = ((unsigned short)current_attr) | (' '<<8);
    unsigned short* addr16 = &((unsigned short *)VIDEO_BASE)[(VIDEO_ROWS-rows)*VIDEO_COLS];
    int i;
    char *s;

    s = getenv("vga_askscroll");
    video_scrolls += rows;

    if (video_scrolls >= video_numrows)
    {
	if (s && strcmp(s, "yes"))
	{
	    while (-1 == tstc());
	}

	video_scrolls = 0;
    }


    memcpy(VIDEO_BASE, VIDEO_BASE+rows*(VIDEO_COLS*2), (VIDEO_ROWS-rows)*(VIDEO_COLS*2));
    for (i = 0 ; i < rows * VIDEO_COLS ; i++)
	addr16[i] = clear;
    cursor_row-=rows;
    cursor_col=0;
}

void video_puts(char *string)
{
    while (*string)
    {
	video_putc(*string);
	string++;
    }
}

int video_start(void)
{
    return 0;
}

unsigned char video_single_box[] =
{
    218, 196, 191,
    179,      179,
    192, 196, 217
};

unsigned char video_double_box[] =
{
    201, 205, 187,
    186,      186,
    200, 205, 188
};

unsigned char video_single_title[] =
{
    195, 196, 180, 180, 195
};

unsigned char video_double_title[] =
{
    204, 205, 185, 181, 198
};

#define SINGLE_BOX 0
#define DOUBLE_BOX 1

unsigned char *video_addr(int x, int y)
{
    return VIDEO_BASE + 2*(VIDEO_COLS*y) + 2*x;
}

void video_bios_print_string(char *s, int x, int y, int attr, int count)
{
    int cattr = current_attr;
    if (attr != -1) current_attr = attr;
    video_set_cursor(x,y);
    while (count)
    {
	char c = *s++;
	if (attr == -1) current_attr = *s++;
	video_putc(c);
	count--;
    }
}

void video_draw_box(int style, int attr, char *title, int separate, int x, int y, int w, int h)
{
    unsigned char *fb, *fb2; 
    unsigned char *st = (style == SINGLE_BOX)?video_single_box : video_double_box;
    unsigned char *ti = (style == SINGLE_BOX)?video_single_title : video_double_title;
    int i;

    fb = video_addr(x,y);
    *(fb) = st[0];
    *(fb+1) = attr;
    fb += 2;

    fb2 = video_addr(x,y+h-1);
    *(fb2) = st[5];
    *(fb2+1) = attr;
    fb2 += 2;

    for (i=0; i<w-2;i++)
    {
	*fb = st[1];
	fb++;
	*fb = attr;
	fb++;

	*fb2 = st[6];
	fb2++;
	*fb2 = attr;
	fb2++;

    }
    *fb = st[2];
    *(fb+1) = attr;

    *fb2 = st[7];
    *(fb2+1) = attr;

    fb  = video_addr(x, y+1);
    fb2 = video_addr(x+w-1, y+1);
    for (i=0; i<h-2; i++)
    {
	*fb = st[3];
	*(fb+1) = attr; fb += 2*VIDEO_COLS;

	*fb2 = st[4]; 
	*(fb2+1) = attr; fb2 += 2*VIDEO_COLS;
    }
    
    // Draw title
    if (title)
    {
	if (separate == 0)
	{
	    fb = video_addr(x+1, y);
	    *fb = ti[3];
	    fb += 2;
	    *fb = ' ';
	    fb += 2;
	    while (*title)
	    {
		*fb = *title;
		fb ++;
		*fb = attr;
		fb++; title++;
	    }
	    *fb = ' ';
	    fb += 2;
	    *fb = ti[4];
	}
	else
	{
	    fb = video_addr(x, y+2);
	    *fb = ti[0];
	    fb += 2;
	    for (i=0; i<w-2; i++)
	    {
		*fb = ti[1];
		*(fb+1) = attr;
		fb += 2;
	    }
	    *fb = ti[2];
	    *(fb+1) = attr;
	    fb = video_addr(x+1, y+1);
	    for (i=0; i<w-2; i++)
	    {
		*fb = ' ';
		*(fb+1) = attr;
		fb += 2;
	    }
	    fb = video_addr(x+2, y+1);
	    
	    while (*title)
	    {
		*fb = *title;
		*(fb+1) = attr;
		fb += 2;
		title++;
	    }
	}
    }

}

void video_draw_text(int x, int y, int attr, char *text)
{
    unsigned char *fb = video_addr(x,y);
    while (*text)
    {
	*fb++ = *text++;
	*fb++ = attr;
    }
}

void video_save_rect(int x, int y, int w, int h, void *save_area, int clearchar, int clearattr)
{
    unsigned char *save = (unsigned char *)save_area;
    unsigned char *fb = video_addr(x,y);
    int i,j;
    for (i=0; i<h; i++)
    {
	unsigned char *fbb = fb;
	for (j=0; j<w; j++)
	{
	    *save ++ = *fb;
	    if (clearchar > 0) *fb = clearchar;
	    fb ++;
	    *save ++ = *fb;
	    if (clearattr > 0) *fb = clearattr;
	}
	fb = fbb + 2*VIDEO_COLS;
    }
}

void video_restore_rect(int x, int y, int w, int h, void *save_area)
{   
    unsigned char *save = (unsigned char *)save_area;
    unsigned char *fb = video_addr(x,y);
    int i,j;
    for (i=0; i<h; i++)
    {
	unsigned char *fbb = fb;
	for (j=0; j<w; j++)
	{
	    *fb ++ = *save ++;
	    *fb ++ = *save ++;
	}
	fb = fbb + 2*VIDEO_COLS;
    }

}

int video_rows(void)
{
    return VIDEO_ROWS;
}

int video_cols(void)
{
    return VIDEO_COLS;
}

void video_size(int cols, int rows)
{
    video_numrows = rows;
    video_numcols = cols;
}

void video_clear(void)
{
    unsigned short *fbb = (unsigned short *)0xFD0B8000;
    int i,j;
    unsigned short val = 0x2000 | current_attr;

    for (i=0; i<video_rows(); i++)
    {
	for (j=0; j<video_cols(); j++)
	{
	    *fbb++ = val;
	}
    }
    video_set_cursor(0,0);
    cursor_row = 0;
    cursor_col = 0;
}

#ifdef EASTEREGG
int video_easteregg_active = 0;

void video_easteregg(void)
{
    video_easteregg_active = 1;
}
#endif

extern block_dev_desc_t * ide_get_dev(int dev);
extern char version_string[];

void video_banner(void)
{
    block_dev_desc_t *ide;
    DECLARE_GLOBAL_DATA_PTR;
    int i;
    char *s;
    int maxdev;
    

    if (video_inited == 0) return;
#ifdef EASTEREGG
    if (video_easteregg_active)
    {
	prompt_string="";
	video_clear();
	printf("\n");
	printf("    **** COMMODORE 64 BASIC X2 ****\n\n");
	printf(" 64K RAM SYSTEM  38911 BASIC BYTES FREE\n\n");
	printf("READY\n");
    }
    else
    {
#endif
	s = getenv("ide_maxbus");
	if (s)
	    maxdev = atoi(s) * 2;
	else
	    maxdev = 4;

	s = getenv("stdout");
	if (s && strcmp(s, "serial") == 0)
	    return;

	video_clear();
	printf("%s\n\nCPU: ", version_string);
	checkcpu();
	printf("DRAM: %ld MB\n", gd->bd->bi_memsize/(1024*1024));
	printf("FSB: %ld MHz\n", gd->bd->bi_busfreq/1000000);

	printf("\n---- Disk summary ----\n");
	for (i = 0; i < maxdev; i++)
	{
	    ide = ide_get_dev(i);
	    printf("Device %d: ", i);
	    dev_print(ide);
	}

/*
    video_draw_box(SINGLE_BOX, 0x0F, "Test 1", 0, 0,18, 72, 4);
    video_draw_box(DOUBLE_BOX, 0x0F, "Test 2", 1, 4,10, 50, 6);
    video_draw_box(DOUBLE_BOX, 0x0F, "Test 3", 0, 40, 3, 20, 5);

    video_draw_text(1, 4, 0x2F, "Highlighted options");
    video_draw_text(1, 5, 0x0F, "Non-selected option");
    video_draw_text(1, 6, 0x07, "disabled option");
*/
#ifdef EASTEREGG
    }
#endif
}