video.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. #include <common.h>
  24. #include <devices.h>
  25. #include "memio.h"
  26. #include <part.h>
  27. unsigned char *cursor_position;
  28. unsigned int cursor_row;
  29. unsigned int cursor_col;
  30. unsigned char current_attr;
  31. unsigned int video_numrows = 25;
  32. unsigned int video_numcols = 80;
  33. unsigned int video_scrolls = 0;
  34. #define VIDEO_BASE (unsigned char *)0xFD0B8000
  35. #define VIDEO_ROWS video_numrows
  36. #define VIDEO_COLS video_numcols
  37. #define VIDEO_PITCH (2 * video_numcols)
  38. #define VIDEO_SIZE (video_numrows * video_numcols * 2)
  39. #define VIDEO_NAME "vga"
  40. void video_test(void);
  41. void video_putc(char ch);
  42. void video_puts(char *string);
  43. void video_scroll(int rows);
  44. void video_banner(void);
  45. int video_init(void);
  46. int video_start(void);
  47. int video_rows(void);
  48. int video_cols(void);
  49. char *prompt_string = "=>";
  50. void video_set_color(unsigned char attr)
  51. {
  52. unsigned char *fb = (unsigned char *)VIDEO_BASE;
  53. int i;
  54. current_attr = video_get_attr();
  55. for (i=0; i<VIDEO_SIZE; i+=2)
  56. {
  57. *(fb+i+1) = current_attr;
  58. }
  59. }
  60. unsigned char video_get_attr(void)
  61. {
  62. char *s;
  63. unsigned char attr;
  64. attr = 0x0f;
  65. s = getenv("vga_fg_color");
  66. if (s)
  67. {
  68. attr = atoi(s);
  69. }
  70. s = getenv("vga_bg_color");
  71. if (s)
  72. {
  73. attr |= atoi(s)<<4;
  74. }
  75. return attr;
  76. }
  77. int video_inited = 0;
  78. int drv_video_init(void)
  79. {
  80. int error, devices = 1 ;
  81. device_t vgadev ;
  82. if (video_inited) return 1;
  83. video_inited = 1;
  84. video_init();
  85. memset (&vgadev, 0, sizeof(vgadev));
  86. strcpy(vgadev.name, VIDEO_NAME);
  87. vgadev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_SYSTEM;
  88. vgadev.putc = video_putc;
  89. vgadev.puts = video_puts;
  90. vgadev.getc = NULL;
  91. vgadev.tstc = NULL;
  92. vgadev.start = video_start;
  93. error = device_register (&vgadev);
  94. if (error == 0)
  95. {
  96. char *s = getenv("stdout");
  97. if (s && strcmp(s, VIDEO_NAME)==0)
  98. {
  99. if (overwrite_console()) return 1;
  100. error = console_assign(stdout, VIDEO_NAME);
  101. if (error == 0) return 1;
  102. else return error;
  103. }
  104. return 1;
  105. }
  106. return error;
  107. }
  108. int video_init(void)
  109. {
  110. cursor_position = VIDEO_BASE; /* Color text display base */
  111. cursor_row = 0;
  112. cursor_col = 0;
  113. current_attr = video_get_attr(); /* Currently selected value for attribute. */
  114. /* video_test(); */
  115. video_set_color(current_attr);
  116. return 0;
  117. }
  118. void video_set_cursor(int line, int column)
  119. {
  120. unsigned short offset = line*video_numcols + column;
  121. cursor_position = VIDEO_BASE + line*VIDEO_PITCH + column*2;
  122. out_byte(0x3D4, 0x0E);
  123. out_byte(0x3D5, offset/256);
  124. out_byte(0x3D4, 0x0F);
  125. out_byte(0x3D5, offset%256);
  126. }
  127. void video_write_char(int character)
  128. {
  129. *cursor_position = character;
  130. *(cursor_position+1) = current_attr;
  131. }
  132. void video_test(void)
  133. {
  134. }
  135. void video_putc(char ch)
  136. {
  137. switch(ch)
  138. {
  139. case '\n':
  140. cursor_col = 0;
  141. cursor_row += 1;
  142. break;
  143. case '\r':
  144. cursor_col = 0;
  145. break;
  146. case '\b':
  147. if (cursor_col) cursor_col--;
  148. else return;
  149. break;
  150. case '\t':
  151. cursor_col = (cursor_col/8+1)*8;
  152. break;
  153. default:
  154. video_write_char(ch);
  155. cursor_col++;
  156. if (cursor_col > VIDEO_COLS-1)
  157. {
  158. cursor_row++;
  159. cursor_col=0;
  160. }
  161. }
  162. if (cursor_row > VIDEO_ROWS-1)
  163. video_scroll(1);
  164. video_set_cursor(cursor_row, cursor_col);
  165. }
  166. void video_scroll(int rows)
  167. {
  168. unsigned short clear = ((unsigned short)current_attr) | (' '<<8);
  169. unsigned short* addr16 = &((unsigned short *)VIDEO_BASE)[(VIDEO_ROWS-rows)*VIDEO_COLS];
  170. int i;
  171. char *s;
  172. s = getenv("vga_askscroll");
  173. video_scrolls += rows;
  174. if (video_scrolls >= video_numrows)
  175. {
  176. if (s && strcmp(s, "yes"))
  177. {
  178. while (-1 == tstc());
  179. }
  180. video_scrolls = 0;
  181. }
  182. memcpy(VIDEO_BASE, VIDEO_BASE+rows*(VIDEO_COLS*2), (VIDEO_ROWS-rows)*(VIDEO_COLS*2));
  183. for (i = 0 ; i < rows * VIDEO_COLS ; i++)
  184. addr16[i] = clear;
  185. cursor_row-=rows;
  186. cursor_col=0;
  187. }
  188. void video_puts(char *string)
  189. {
  190. while (*string)
  191. {
  192. video_putc(*string);
  193. string++;
  194. }
  195. }
  196. int video_start(void)
  197. {
  198. return 0;
  199. }
  200. unsigned char video_single_box[] =
  201. {
  202. 218, 196, 191,
  203. 179, 179,
  204. 192, 196, 217
  205. };
  206. unsigned char video_double_box[] =
  207. {
  208. 201, 205, 187,
  209. 186, 186,
  210. 200, 205, 188
  211. };
  212. unsigned char video_single_title[] =
  213. {
  214. 195, 196, 180, 180, 195
  215. };
  216. unsigned char video_double_title[] =
  217. {
  218. 204, 205, 185, 181, 198
  219. };
  220. #define SINGLE_BOX 0
  221. #define DOUBLE_BOX 1
  222. unsigned char *video_addr(int x, int y)
  223. {
  224. return VIDEO_BASE + 2*(VIDEO_COLS*y) + 2*x;
  225. }
  226. void video_bios_print_string(char *s, int x, int y, int attr, int count)
  227. {
  228. int cattr = current_attr;
  229. if (attr != -1) current_attr = attr;
  230. video_set_cursor(x,y);
  231. while (count)
  232. {
  233. char c = *s++;
  234. if (attr == -1) current_attr = *s++;
  235. video_putc(c);
  236. count--;
  237. }
  238. }
  239. void video_draw_box(int style, int attr, char *title, int separate, int x, int y, int w, int h)
  240. {
  241. unsigned char *fb, *fb2;
  242. unsigned char *st = (style == SINGLE_BOX)?video_single_box : video_double_box;
  243. unsigned char *ti = (style == SINGLE_BOX)?video_single_title : video_double_title;
  244. int i;
  245. fb = video_addr(x,y);
  246. *(fb) = st[0];
  247. *(fb+1) = attr;
  248. fb += 2;
  249. fb2 = video_addr(x,y+h-1);
  250. *(fb2) = st[5];
  251. *(fb2+1) = attr;
  252. fb2 += 2;
  253. for (i=0; i<w-2;i++)
  254. {
  255. *fb = st[1];
  256. fb++;
  257. *fb = attr;
  258. fb++;
  259. *fb2 = st[6];
  260. fb2++;
  261. *fb2 = attr;
  262. fb2++;
  263. }
  264. *fb = st[2];
  265. *(fb+1) = attr;
  266. *fb2 = st[7];
  267. *(fb2+1) = attr;
  268. fb = video_addr(x, y+1);
  269. fb2 = video_addr(x+w-1, y+1);
  270. for (i=0; i<h-2; i++)
  271. {
  272. *fb = st[3];
  273. *(fb+1) = attr; fb += 2*VIDEO_COLS;
  274. *fb2 = st[4];
  275. *(fb2+1) = attr; fb2 += 2*VIDEO_COLS;
  276. }
  277. /* Draw title */
  278. if (title)
  279. {
  280. if (separate == 0)
  281. {
  282. fb = video_addr(x+1, y);
  283. *fb = ti[3];
  284. fb += 2;
  285. *fb = ' ';
  286. fb += 2;
  287. while (*title)
  288. {
  289. *fb = *title;
  290. fb ++;
  291. *fb = attr;
  292. fb++; title++;
  293. }
  294. *fb = ' ';
  295. fb += 2;
  296. *fb = ti[4];
  297. }
  298. else
  299. {
  300. fb = video_addr(x, y+2);
  301. *fb = ti[0];
  302. fb += 2;
  303. for (i=0; i<w-2; i++)
  304. {
  305. *fb = ti[1];
  306. *(fb+1) = attr;
  307. fb += 2;
  308. }
  309. *fb = ti[2];
  310. *(fb+1) = attr;
  311. fb = video_addr(x+1, y+1);
  312. for (i=0; i<w-2; i++)
  313. {
  314. *fb = ' ';
  315. *(fb+1) = attr;
  316. fb += 2;
  317. }
  318. fb = video_addr(x+2, y+1);
  319. while (*title)
  320. {
  321. *fb = *title;
  322. *(fb+1) = attr;
  323. fb += 2;
  324. title++;
  325. }
  326. }
  327. }
  328. }
  329. void video_draw_text(int x, int y, int attr, char *text)
  330. {
  331. unsigned char *fb = video_addr(x,y);
  332. while (*text)
  333. {
  334. *fb++ = *text++;
  335. *fb++ = attr;
  336. }
  337. }
  338. void video_save_rect(int x, int y, int w, int h, void *save_area, int clearchar, int clearattr)
  339. {
  340. unsigned char *save = (unsigned char *)save_area;
  341. unsigned char *fb = video_addr(x,y);
  342. int i,j;
  343. for (i=0; i<h; i++)
  344. {
  345. unsigned char *fbb = fb;
  346. for (j=0; j<w; j++)
  347. {
  348. *save ++ = *fb;
  349. if (clearchar > 0) *fb = clearchar;
  350. fb ++;
  351. *save ++ = *fb;
  352. if (clearattr > 0) *fb = clearattr;
  353. }
  354. fb = fbb + 2*VIDEO_COLS;
  355. }
  356. }
  357. void video_restore_rect(int x, int y, int w, int h, void *save_area)
  358. {
  359. unsigned char *save = (unsigned char *)save_area;
  360. unsigned char *fb = video_addr(x,y);
  361. int i,j;
  362. for (i=0; i<h; i++)
  363. {
  364. unsigned char *fbb = fb;
  365. for (j=0; j<w; j++)
  366. {
  367. *fb ++ = *save ++;
  368. *fb ++ = *save ++;
  369. }
  370. fb = fbb + 2*VIDEO_COLS;
  371. }
  372. }
  373. int video_rows(void)
  374. {
  375. return VIDEO_ROWS;
  376. }
  377. int video_cols(void)
  378. {
  379. return VIDEO_COLS;
  380. }
  381. void video_size(int cols, int rows)
  382. {
  383. video_numrows = rows;
  384. video_numcols = cols;
  385. }
  386. void video_clear(void)
  387. {
  388. unsigned short *fbb = (unsigned short *)0xFD0B8000;
  389. int i,j;
  390. unsigned short val = 0x2000 | current_attr;
  391. for (i=0; i<video_rows(); i++)
  392. {
  393. for (j=0; j<video_cols(); j++)
  394. {
  395. *fbb++ = val;
  396. }
  397. }
  398. video_set_cursor(0,0);
  399. cursor_row = 0;
  400. cursor_col = 0;
  401. }
  402. #ifdef EASTEREGG
  403. int video_easteregg_active = 0;
  404. void video_easteregg(void)
  405. {
  406. video_easteregg_active = 1;
  407. }
  408. #endif
  409. extern block_dev_desc_t * ide_get_dev(int dev);
  410. extern char version_string[];
  411. void video_banner(void)
  412. {
  413. block_dev_desc_t *ide;
  414. DECLARE_GLOBAL_DATA_PTR;
  415. int i;
  416. char *s;
  417. int maxdev;
  418. if (video_inited == 0) return;
  419. #ifdef EASTEREGG
  420. if (video_easteregg_active)
  421. {
  422. prompt_string="";
  423. video_clear();
  424. printf("\n");
  425. printf(" **** COMMODORE 64 BASIC X2 ****\n\n");
  426. printf(" 64K RAM SYSTEM 38911 BASIC BYTES FREE\n\n");
  427. printf("READY\n");
  428. }
  429. else
  430. {
  431. #endif
  432. s = getenv("ide_maxbus");
  433. if (s)
  434. maxdev = atoi(s) * 2;
  435. else
  436. maxdev = 4;
  437. s = getenv("stdout");
  438. if (s && strcmp(s, "serial") == 0)
  439. return;
  440. video_clear();
  441. printf("%s\n\nCPU: ", version_string);
  442. checkcpu();
  443. printf("DRAM: %ld MB\n", gd->bd->bi_memsize/(1024*1024));
  444. printf("FSB: %ld MHz\n", gd->bd->bi_busfreq/1000000);
  445. printf("\n---- Disk summary ----\n");
  446. for (i = 0; i < maxdev; i++)
  447. {
  448. ide = ide_get_dev(i);
  449. printf("Device %d: ", i);
  450. dev_print(ide);
  451. }
  452. /*
  453. video_draw_box(SINGLE_BOX, 0x0F, "Test 1", 0, 0,18, 72, 4);
  454. video_draw_box(DOUBLE_BOX, 0x0F, "Test 2", 1, 4,10, 50, 6);
  455. video_draw_box(DOUBLE_BOX, 0x0F, "Test 3", 0, 40, 3, 20, 5);
  456. video_draw_text(1, 4, 0x2F, "Highlighted options");
  457. video_draw_text(1, 5, 0x0F, "Non-selected option");
  458. video_draw_text(1, 6, 0x07, "disabled option");
  459. */
  460. #ifdef EASTEREGG
  461. }
  462. #endif
  463. }