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