c664eef5d3ca04a842df350310e1006f7fd250b9
[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 #include <debug.h>
24
25 DBG_DEFAULT_CHANNEL(UI);
26
27 PVOID FrameBuffer;
28 ULONG FrameBufferSize;
29 static ULONG ScreenWidth;
30 static ULONG ScreenHeight;
31 static ULONG BytesPerPixel;
32 static ULONG Delta;
33
34 #define CHAR_WIDTH 8
35 #define CHAR_HEIGHT 16
36
37 #define TOP_BOTTOM_LINES 0
38
39 #define FB_SIZE_MB 4
40
41 #define MAKE_COLOR(Red, Green, Blue) (0xff000000 | (((Red) & 0xff) << 16) | (((Green) & 0xff) << 8) | ((Blue) & 0xff))
42
43 BOOLEAN I2CTransmitByteGetReturn(UCHAR bPicAddressI2cFormat, UCHAR bDataToWrite, ULONG *Return);
44
45 static VOID
46 XboxVideoOutputChar(UCHAR Char, unsigned X, unsigned Y, ULONG FgColor, ULONG BgColor)
47 {
48 PUCHAR FontPtr;
49 PULONG Pixel;
50 UCHAR Mask;
51 unsigned Line;
52 unsigned Col;
53
54 FontPtr = XboxFont8x16 + Char * 16;
55 Pixel = (PULONG) ((char *) FrameBuffer + (Y * CHAR_HEIGHT + TOP_BOTTOM_LINES) * Delta
56 + X * CHAR_WIDTH * BytesPerPixel);
57 for (Line = 0; Line < CHAR_HEIGHT; Line++)
58 {
59 Mask = 0x80;
60 for (Col = 0; Col < CHAR_WIDTH; Col++)
61 {
62 Pixel[Col] = (0 != (FontPtr[Line] & Mask) ? FgColor : BgColor);
63 Mask = Mask >> 1;
64 }
65 Pixel = (PULONG) ((char *) Pixel + Delta);
66 }
67 }
68
69 static ULONG
70 XboxVideoAttrToSingleColor(UCHAR Attr)
71 {
72 UCHAR Intensity;
73
74 Intensity = (0 == (Attr & 0x08) ? 127 : 255);
75
76 return 0xff000000 |
77 (0 == (Attr & 0x04) ? 0 : (Intensity << 16)) |
78 (0 == (Attr & 0x02) ? 0 : (Intensity << 8)) |
79 (0 == (Attr & 0x01) ? 0 : Intensity);
80 }
81
82 static VOID
83 XboxVideoAttrToColors(UCHAR Attr, ULONG *FgColor, ULONG *BgColor)
84 {
85 *FgColor = XboxVideoAttrToSingleColor(Attr & 0xf);
86 *BgColor = XboxVideoAttrToSingleColor((Attr >> 4) & 0xf);
87 }
88
89 static VOID
90 XboxVideoClearScreenColor(ULONG Color, BOOLEAN FullScreen)
91 {
92 ULONG Line, Col;
93 PULONG p;
94
95 for (Line = 0; Line < ScreenHeight - (FullScreen ? 0 : 2 * TOP_BOTTOM_LINES); Line++)
96 {
97 p = (PULONG) ((char *) FrameBuffer + (Line + (FullScreen ? 0 : TOP_BOTTOM_LINES)) * Delta);
98 for (Col = 0; Col < ScreenWidth; Col++)
99 {
100 *p++ = Color;
101 }
102 }
103 }
104
105 VOID
106 XboxVideoClearScreen(UCHAR Attr)
107 {
108 ULONG FgColor, BgColor;
109
110 XboxVideoAttrToColors(Attr, &FgColor, &BgColor);
111
112 XboxVideoClearScreenColor(BgColor, FALSE);
113 }
114
115 VOID
116 XboxVideoPutChar(int Ch, UCHAR Attr, unsigned X, unsigned Y)
117 {
118 ULONG FgColor, BgColor;
119
120 XboxVideoAttrToColors(Attr, &FgColor, &BgColor);
121
122 XboxVideoOutputChar(Ch, X, Y, FgColor, BgColor);
123 }
124
125 VOID
126 XboxVideoInit(VOID)
127 {
128 ULONG AvMode;
129
130 /* Reuse framebuffer that was set up by firmware */
131 FrameBuffer = (PVOID)*((PULONG) 0xfd600800);
132 /* Verify that framebuffer address is page-aligned */
133 ASSERT((ULONG_PTR)FrameBuffer % PAGE_SIZE == 0);
134
135 /* FIXME: obtain fb size from firmware somehow (Cromwell reserves high 4 MB of RAM) */
136 FrameBufferSize = 4 * 1024 * 1024;
137
138 /* FIXME: don't use SMBus, obtain current video resolution directly from NV2A */
139 if (I2CTransmitByteGetReturn(0x10, 0x04, &AvMode))
140 {
141 if (1 == AvMode) /* HDTV */
142 {
143 ScreenWidth = 720;
144 }
145 else
146 {
147 /* FIXME Other possible values of AvMode:
148 * 0 - AV_SCART_RGB
149 * 2 - AV_VGA_SOG
150 * 4 - AV_SVIDEO
151 * 6 - AV_COMPOSITE
152 * 7 - AV_VGA
153 * other AV_COMPOSITE
154 */
155 ScreenWidth = 640;
156 }
157 }
158 else
159 {
160 ScreenWidth = 640;
161 }
162
163 ScreenHeight = 480;
164 BytesPerPixel = 4;
165 Delta = (ScreenWidth * BytesPerPixel + 3) & ~ 0x3;
166
167 XboxVideoClearScreenColor(MAKE_COLOR(0, 0, 0), TRUE);
168 }
169
170 VIDEODISPLAYMODE
171 XboxVideoSetDisplayMode(char *DisplayMode, BOOLEAN Init)
172 {
173 /* We only have one mode, semi-text */
174 return VideoTextMode;
175 }
176
177 VOID
178 XboxVideoGetDisplaySize(PULONG Width, PULONG Height, PULONG Depth)
179 {
180 *Width = ScreenWidth / CHAR_WIDTH;
181 *Height = (ScreenHeight - 2 * TOP_BOTTOM_LINES) / CHAR_HEIGHT;
182 *Depth = 0;
183 }
184
185 ULONG
186 XboxVideoGetBufferSize(VOID)
187 {
188 return (ScreenHeight - 2 * TOP_BOTTOM_LINES) / CHAR_HEIGHT * (ScreenWidth / CHAR_WIDTH) * 2;
189 }
190
191 VOID
192 XboxVideoGetFontsFromFirmware(PULONG RomFontPointers)
193 {
194 TRACE("XboxVideoGetFontsFromFirmware(): UNIMPLEMENTED\n");
195 }
196
197 VOID
198 XboxVideoSetTextCursorPosition(UCHAR X, UCHAR Y)
199 {
200 /* We don't have a cursor yet */
201 }
202
203 VOID
204 XboxVideoHideShowTextCursor(BOOLEAN Show)
205 {
206 /* We don't have a cursor yet */
207 }
208
209 VOID
210 XboxVideoCopyOffScreenBufferToVRAM(PVOID Buffer)
211 {
212 PUCHAR OffScreenBuffer = (PUCHAR) Buffer;
213 ULONG Col, Line;
214
215 for (Line = 0; Line < (ScreenHeight - 2 * TOP_BOTTOM_LINES) / CHAR_HEIGHT; Line++)
216 {
217 for (Col = 0; Col < ScreenWidth / CHAR_WIDTH; Col++)
218 {
219 XboxVideoPutChar(OffScreenBuffer[0], OffScreenBuffer[1], Col, Line);
220 OffScreenBuffer += 2;
221 }
222 }
223 }
224
225 BOOLEAN
226 XboxVideoIsPaletteFixed(VOID)
227 {
228 return FALSE;
229 }
230
231 VOID
232 XboxVideoSetPaletteColor(UCHAR Color, UCHAR Red, UCHAR Green, UCHAR Blue)
233 {
234 /* Not supported */
235 }
236
237 VOID
238 XboxVideoGetPaletteColor(UCHAR Color, UCHAR* Red, UCHAR* Green, UCHAR* Blue)
239 {
240 /* Not supported */
241 }
242
243 VOID
244 XboxVideoSync(VOID)
245 {
246 /* Not supported */
247 }
248
249 VOID
250 XboxBeep(VOID)
251 {
252 /* Call PC version */
253 PcBeep();
254 }
255
256 VOID
257 XboxVideoPrepareForReactOS(VOID)
258 {
259 XboxVideoClearScreenColor(MAKE_COLOR(0, 0, 0), TRUE);
260 XboxVideoHideShowTextCursor(FALSE);
261 }
262
263 /* EOF */