video.c 10 KB

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