1 /*
2 * GXemul demo: Random rectangles
3 *
4 * This file is in the Public Domain.
5 */
6
7 #include "dev_fb.h"
8
9
10 #ifdef MIPS
11 /* Note: The ugly cast to a signed int (32-bit) causes the address to be
12 sign-extended correctly on MIPS when compiled in 64-bit mode */
13 #define PHYSADDR_OFFSET ((signed int)0xa0000000)
14 #else
15 #define PHYSADDR_OFFSET 0
16 #endif
17
18
19 /* Framebuffer linear memory and controller base addresss: */
20 #define FB_BASE (PHYSADDR_OFFSET + DEV_FB_ADDRESS)
21 #define FBCTRL_BASE (PHYSADDR_OFFSET + DEV_FBCTRL_ADDRESS)
22
23
24 #define XRES 800
25 #define YRES 600
26
27
my_memset(unsigned char * a,int x,int len)28 void my_memset(unsigned char *a, int x, int len)
29 {
30 while (len-- > 0)
31 *a++ = x;
32 }
33
34
draw_rectangle(int x1,int y1,int x2,int y2,int c)35 void draw_rectangle(int x1, int y1, int x2, int y2, int c)
36 {
37 int y, len;
38
39 for (y=y1; y<=y2; y++) {
40 len = 3 * (x2-x1+1);
41 if (len > 0) {
42 my_memset((unsigned char *)FB_BASE +
43 3 * (XRES * y + x1), c, len);
44 }
45 }
46 }
47
48
my_random()49 unsigned int my_random()
50 {
51 static int a = 0x124879b;
52 static int b = 0xb7856fa2;
53 int c = a ^ (b * 51);
54 a = 17 * c - (b * 171);
55 return c;
56 }
57
58
fbctrl_write_port(int p)59 void fbctrl_write_port(int p)
60 {
61 *(volatile int *)(FBCTRL_BASE + DEV_FBCTRL_PORT) = p;
62 }
63
64
fbctrl_write_data(int d)65 void fbctrl_write_data(int d)
66 {
67 *(volatile int *)(FBCTRL_BASE + DEV_FBCTRL_DATA) = d;
68 }
69
70
fbctrl_set_x1(int v)71 void fbctrl_set_x1(int v)
72 {
73 fbctrl_write_port(DEV_FBCTRL_PORT_X1);
74 fbctrl_write_data(v);
75 }
76
77
fbctrl_set_y1(int v)78 void fbctrl_set_y1(int v)
79 {
80 fbctrl_write_port(DEV_FBCTRL_PORT_Y1);
81 fbctrl_write_data(v);
82 }
83
84
fbctrl_command(int c)85 void fbctrl_command(int c)
86 {
87 fbctrl_write_port(DEV_FBCTRL_PORT_COMMAND);
88 fbctrl_write_data(c);
89 }
90
91
change_resolution(int xres,int yres)92 void change_resolution(int xres, int yres)
93 {
94 fbctrl_set_x1(xres);
95 fbctrl_set_y1(yres);
96 fbctrl_command(DEV_FBCTRL_COMMAND_SET_RESOLUTION);
97 }
98
99
f(void)100 void f(void)
101 {
102 /* Change to the resolution we want: */
103 change_resolution(XRES, YRES);
104
105 /* Draw random rectangles forever: */
106 for (;;) {
107 draw_rectangle(my_random() % XRES, my_random() % YRES,
108 my_random() % XRES, my_random() % YRES, my_random());
109 }
110 }
111
112