video.c 10 KB

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