Sync with trunk r64509.
[reactos.git] / boot / freeldr / freeldr / arch / i386 / xboxvideo.c
1 /*
2 * FreeLoader
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * Note: much of this code was based on knowledge and/or code developed
19 * by the Xbox Linux group: http://www.xbox-linux.org
20 */
21
22 #include <freeldr.h>
23
24 static PVOID FrameBuffer;
25 static ULONG ScreenWidth;
26 static ULONG ScreenHeight;
27 static ULONG BytesPerPixel;
28 static ULONG Delta;
29
30 #define CHAR_WIDTH 8
31 #define CHAR_HEIGHT 16
32
33 #define TOP_BOTTOM_LINES 0
34
35 #define FB_SIZE_MB 4
36
37 #define MAKE_COLOR(Red, Green, Blue) (0xff000000 | (((Red) & 0xff) << 16) | (((Green) & 0xff) << 8) | ((Blue) & 0xff))
38
39 BOOLEAN I2CTransmitByteGetReturn(UCHAR bPicAddressI2cFormat, UCHAR bDataToWrite, ULONG *Return);
40
41 static VOID
42 XboxVideoOutputChar(UCHAR Char, unsigned X, unsigned Y, ULONG FgColor, ULONG BgColor)
43 {
44 PUCHAR FontPtr;
45 PULONG Pixel;
46 UCHAR Mask;
47 unsigned Line;
48 unsigned Col;
49
50 FontPtr = XboxFont8x16 + Char * 16;
51 Pixel = (PULONG) ((char *) FrameBuffer + (Y * CHAR_HEIGHT + TOP_BOTTOM_LINES) * Delta
52 + X * CHAR_WIDTH * BytesPerPixel);
53 for (Line = 0; Line < CHAR_HEIGHT; Line++)
54 {
55 Mask = 0x80;
56 for (Col = 0; Col < CHAR_WIDTH; Col++)
57 {
58 Pixel[Col] = (0 != (FontPtr[Line] & Mask) ? FgColor : BgColor);
59 Mask = Mask >> 1;
60 }
61 Pixel = (PULONG) ((char *) Pixel + Delta);
62 }
63 }
64
65 static ULONG
66 XboxVideoAttrToSingleColor(UCHAR Attr)
67 {
68 UCHAR Intensity;
69
70 Intensity = (0 == (Attr & 0x08) ? 127 : 255);
71
72 return 0xff000000 |
73 (0 == (Attr & 0x04) ? 0 : (Intensity << 16)) |
74 (0 == (Attr & 0x02) ? 0 : (Intensity << 8)) |
75 (0 == (Attr & 0x01) ? 0 : Intensity);
76 }
77
78 static VOID
79 XboxVideoAttrToColors(UCHAR Attr, ULONG *FgColor, ULONG *BgColor)
80 {
81 *FgColor = XboxVideoAttrToSingleColor(Attr & 0xf);
82 *BgColor = XboxVideoAttrToSingleColor((Attr >> 4) & 0xf);
83 }
84
85 static VOID
86 XboxVideoClearScreenColor(ULONG Color, BOOLEAN FullScreen)
87 {
88 ULONG Line, Col;
89 PULONG p;
90
91 for (Line = 0; Line < ScreenHeight - (FullScreen ? 0 : 2 * TOP_BOTTOM_LINES); Line++)
92 {
93 p = (PULONG) ((char *) FrameBuffer + (Line + (FullScreen ? 0 : TOP_BOTTOM_LINES)) * Delta);
94 for (Col = 0; Col < ScreenWidth; Col++)
95 {
96 *p++ = Color;
97 }
98 }
99 }
100
101 VOID
102 XboxVideoClearScreen(UCHAR Attr)
103 {
104 ULONG FgColor, BgColor;
105
106 XboxVideoAttrToColors(Attr, &FgColor, &BgColor);
107
108 XboxVideoClearScreenColor(BgColor, FALSE);
109 }
110
111 VOID
112 XboxVideoPutChar(int Ch, UCHAR Attr, unsigned X, unsigned Y)
113 {
114 ULONG FgColor, BgColor;
115
116 XboxVideoAttrToColors(Attr, &FgColor, &BgColor);
117
118 XboxVideoOutputChar(Ch, X, Y, FgColor, BgColor);
119 }
120
121 VOID
122 XboxVideoInit(VOID)
123 {
124 ULONG AvMode;
125
126 FrameBuffer = (PVOID)((ULONG) XboxMemReserveMemory(FB_SIZE_MB) | 0xf0000000);
127
128 if (I2CTransmitByteGetReturn(0x10, 0x04, &AvMode))
129 {
130 if (1 == AvMode) /* HDTV */
131 {
132 ScreenWidth = 720;
133 }
134 else
135 {
136 /* FIXME Other possible values of AvMode:
137 * 0 - AV_SCART_RGB
138 * 2 - AV_VGA_SOG
139 * 4 - AV_SVIDEO
140 * 6 - AV_COMPOSITE
141 * 7 - AV_VGA
142 * other AV_COMPOSITE
143 */
144 ScreenWidth = 640;
145 }
146 }
147 else
148 {
149 ScreenWidth = 640;
150 }
151
152 ScreenHeight = 480;
153 BytesPerPixel = 4;
154 Delta = (ScreenWidth * BytesPerPixel + 3) & ~ 0x3;
155
156 XboxVideoClearScreenColor(MAKE_COLOR(0, 0, 0), TRUE);
157
158 /* Tell the nVidia controller about the framebuffer */
159 *((PULONG) 0xfd600800) = (ULONG) FrameBuffer;
160 }
161
162 VIDEODISPLAYMODE
163 XboxVideoSetDisplayMode(char *DisplayMode, BOOLEAN Init)
164 {
165 /* We only have one mode, semi-text */
166 return VideoTextMode;
167 }
168
169 VOID
170 XboxVideoGetDisplaySize(PULONG Width, PULONG Height, PULONG Depth)
171 {
172 *Width = ScreenWidth / CHAR_WIDTH;
173 *Height = (ScreenHeight - 2 * TOP_BOTTOM_LINES) / CHAR_HEIGHT;
174 *Depth = 0;
175 }
176
177 ULONG
178 XboxVideoGetBufferSize(VOID)
179 {
180 return (ScreenHeight - 2 * TOP_BOTTOM_LINES) / CHAR_HEIGHT * (ScreenWidth / CHAR_WIDTH) * 2;
181 }
182
183 VOID
184 XboxVideoSetTextCursorPosition(UCHAR X, UCHAR Y)
185 {
186 /* We don't have a cursor yet */
187 }
188
189 VOID
190 XboxVideoHideShowTextCursor(BOOLEAN Show)
191 {
192 /* We don't have a cursor yet */
193 }
194
195 VOID
196 XboxVideoCopyOffScreenBufferToVRAM(PVOID Buffer)
197 {
198 PUCHAR OffScreenBuffer = (PUCHAR) Buffer;
199 ULONG Col, Line;
200
201 for (Line = 0; Line < (ScreenHeight - 2 * TOP_BOTTOM_LINES) / CHAR_HEIGHT; Line++)
202 {
203 for (Col = 0; Col < ScreenWidth / CHAR_WIDTH; Col++)
204 {
205 XboxVideoPutChar(OffScreenBuffer[0], OffScreenBuffer[1], Col, Line);
206 OffScreenBuffer += 2;
207 }
208 }
209 }
210
211 BOOLEAN
212 XboxVideoIsPaletteFixed(VOID)
213 {
214 return FALSE;
215 }
216
217 VOID
218 XboxVideoSetPaletteColor(UCHAR Color, UCHAR Red, UCHAR Green, UCHAR Blue)
219 {
220 /* Not supported */
221 }
222
223 VOID
224 XboxVideoGetPaletteColor(UCHAR Color, UCHAR* Red, UCHAR* Green, UCHAR* Blue)
225 {
226 /* Not supported */
227 }
228
229 VOID
230 XboxVideoSync()
231 {
232 /* Not supported */
233 }
234
235 VOID
236 XboxBeep()
237 {
238 /* Call PC version */
239 PcBeep();
240 }
241
242 VOID
243 XboxVideoPrepareForReactOS(IN BOOLEAN Setup)
244 {
245 XboxVideoClearScreenColor(MAKE_COLOR(0, 0, 0), TRUE);
246 }
247
248 /* EOF */