LibXenon
Bare-metal Xbox 360 homebrew library
Loading...
Searching...
No Matches
console.c
Go to the documentation of this file.
1// Copyright 2009 Georg Lukas <georg@op-co.de>
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7// * Redistributions of source code must retain the above copyright
8// notice, this list of conditions and the following disclaimer.
9// * Redistributions in binary form must reproduce the above copyright
10// notice, this list of conditions and the following disclaimer in the
11// documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23// THE POSSIBILITY OF SUCH DAMAGE.
24//
25// hacked to fit into libxenon by Felix Domke <tmbinc@elitedvb.net>
26// This is a bit ugly, and it's not Georg's fault.
27
28#include <stdio.h>
29#include <stdint.h>
30#include <string.h>
31#include <ppc/cache.h>
32#include "font_8x16.h"
33#include "console.h"
34#include <xenos/xenos.h>
35
36static int console_width, console_height,
37 console_size;
38
39/* Colors in BGRA: background and foreground */
40uint32_t console_color[2] = { 0x00000000, 0xFFA0A000 };
42
43static unsigned char *console_fb = 0LL;
44
45static int cursor_x, cursor_y, max_x, max_y, offset_x, offset_y, pixel_max_x, pixel_max_y;
46
47struct ati_info {
53} __attribute__ ((__packed__)) ;
54
55
56/* set a pixel to RGB values, must call console_init() first */
57static inline void console_pset32(int x, int y, int color)
58{
59#define fbint ((uint32_t*)console_fb)
60#define base (((y >> 5)*32*console_width + ((x >> 5)<<10) \
61 + (x&3) + ((y&1)<<2) + (((x&31)>>2)<<3) + (((y&31)>>1)<<6)) ^ ((y&8)<<2))
62 fbint[base] = color;
63#undef fbint
64#undef base
65}
66
67void console_pset(int x, int y, unsigned char r, unsigned char g, unsigned char b) {
68 console_pset32(x + offset_x, y + offset_y, (b<<24) + (g<<16) + (r<<8));
69 /* little endian:
70 * fbint[base] = b + (g<<8) + (r<<16); */
71}
72
73void console_pset_right(int x, int y, unsigned char r, unsigned char g, unsigned char b) {
74 console_pset32(pixel_max_x - x, y + offset_y, (b<<24) + (g<<16) + (r<<8));
75 /* little endian:
76 * fbint[base] = b + (g<<8) + (r<<16); */
77}
78
79static void console_draw_char(const int x, const int y, const unsigned char c) {
80#define font_pixel(ch, x, y) ((fontdata_8x16[ch*16+y]>>(7-x))&1)
81 int lx, ly;
82 for (ly=0; ly < 16; ly++)
83 for (lx=0; lx < 8; lx++) {
84 console_pset32(x+lx, y+ly, console_color[font_pixel(c, lx, ly)]);
85 }
86}
87
89 unsigned int *fb = (unsigned int*)console_fb;
90 int count = console_width * console_height;
91 while (count--)
92 *fb++ = console_color[0];
93
94 memdcbst(console_fb, console_size*4);
95
96 cursor_x=0;
97 cursor_y=0;
98}
99
102}
103
104void console_clear_to_eol(int startpos) {
105 char sp[max_x - startpos];
106 memset(sp,' ', max_x - startpos);
107 sp[max_x - startpos - 1]='\0';
108
109 cursor_x = startpos;
110 printf("%s",sp);
111 cursor_x = startpos;
112}
113
114static void console_scroll32(const unsigned int lines) {
115 int l, bs;
116 bs = console_width*32*4;
117 /* copy all tile blocks to a higher position */
118 for (l=lines; l*32 < console_height; l++) {
119 memcpy(console_fb + bs*(l-lines),
120 console_fb + bs*l,
121 bs);
122 }
123
124 /* fill up last lines with background color */
125 uint32_t *fb = (uint32_t*)(console_fb + console_size*4 - bs*lines);
126 uint32_t *end = (uint32_t*)(console_fb + console_size*4);
127 while (fb != end)
128 *fb++ = console_color[0];
129
130 memdcbst(console_fb, console_size*4);
131}
132
134#if 0
135 /* fill up with spaces */
136 while (cursor_x*8 < console_width) {
137 console_draw_char(cursor_x*8, cursor_y*16, ' ');
138 cursor_x++;
139 }
140#endif
141
142 /* reset to the left */
143 cursor_x = 0;
144 cursor_y++;
145 if (cursor_y >= max_y) {
146 /* XXX implement scrolling */
147 console_scroll32(1);
148 cursor_y -= 2;
149 }
150}
151
152void console_putch(const char c) {
153 if (!console_fb)
154 return;
155 if (c == '\r') {
156 cursor_x = 0;
157 } else if (c == '\n') {
159 } else {
160 console_draw_char(cursor_x*8 + offset_x, cursor_y*16 + offset_y, c);
161 cursor_x++;
162 if (cursor_x >= max_x)
164 }
165 memdcbst(console_fb, console_size*4);
166}
167
168static void console_stdout_hook(const char *buf, int len)
169{
170 while (len--)
171 console_putch(*buf++);
172}
173
174extern void (*stdout_hook)(const char *buf, int len);
175
176void console_init(void) {
177 struct ati_info *ai = (struct ati_info*)0xec806100ULL;
178
179 console_fb = (unsigned char*)(long)(ai->base | 0x80000000);
180 /* round up size to tiles of 32x32 */
181 console_width = ((ai->width+31)>>5)<<5;
182 console_height = ((ai->height+31)>>5)<<5;
183 console_size = console_width*console_height;
184
185 offset_x = offset_y = 0;
186 if (xenos_is_overscan())
187 {
188 offset_x = (ai->width/28); //50;
189 offset_y = (ai->height/28); //50;
190 }
191
192 cursor_x = cursor_y = 0;
193 pixel_max_x = ai->width - offset_x * 2;
194 max_x = pixel_max_x / 8;
195 pixel_max_y = ai->height - offset_y * 2;
196 max_y = pixel_max_y / 16;
197
199
200 stdout_hook = console_stdout_hook;
201
202 printf(" * Xenos FB with %dx%d (%dx%d) at %p initialized.\n",
203 max_x, max_y, ai->width, ai->height, console_fb);
204}
205
206void console_set_colors(unsigned int background, unsigned int foreground){
207 console_color[0]=background;
208 console_color[1]=foreground;
209}
210
211void console_get_dimensions(unsigned int * width,unsigned int * height){
212 if (width) *width=max_x;
213 if (height) *height=max_y;
214}
215
217{
218 return cursor_x;
219}
220
222{
223 return cursor_y;
224}
225
227{
228 return max_x;
229}
230
232{
233 return max_y;
234}
235
236
237void console_set_cursor(int x, int y)
238{
239 cursor_x = x;
240 cursor_y = y;
241}
242
243void console_open(void)
244{
245 stdout_hook = console_stdout_hook;
246}
247
249{
250 stdout_hook = 0;
251}
uint32_t console_oldfg
Definition: console.c:41
uint32_t console_oldbg
Definition: console.c:41
void console_close(void)
Definition: console.c:248
void console_clear_to_eol(int startpos)
Definition: console.c:104
int console_get_cursor_max_x(void)
Definition: console.c:226
uint32_t width
Definition: console.c:3
void console_clrscr()
Definition: console.c:88
#define fbint
int console_get_cursor_y(void)
Definition: console.c:221
void console_pset_right(int x, int y, unsigned char r, unsigned char g, unsigned char b)
Definition: console.c:73
#define base
void(* stdout_hook)(const char *buf, int len)
Definition: newlib.c:19
uint32_t height
Definition: console.c:4
void console_newline()
Definition: console.c:133
void console_set_colors(unsigned int background, unsigned int foreground)
Definition: console.c:206
int console_get_cursor_x(void)
Definition: console.c:216
void console_open(void)
Definition: console.c:243
void console_clrline()
Definition: console.c:100
int console_get_cursor_max_y(void)
Definition: console.c:231
void console_get_dimensions(unsigned int *width, unsigned int *height)
Definition: console.c:211
void console_init(void)
Definition: console.c:176
void console_set_cursor(int x, int y)
Definition: console.c:237
#define font_pixel(ch, x, y)
uint32_t console_color[2]
Definition: console.c:40
void console_putch(const char c)
Definition: console.c:152
void console_pset(int x, int y, unsigned char r, unsigned char g, unsigned char b)
Definition: console.c:67
void memdcbst(void *addr, int len)
u32 uint32_t
Definition: libfdt_env.h:11
unsigned int __mf_uintptr_t __attribute__((__mode__(__pointer__)))
Definition: mf-runtime.h:34
uint32_t height
Definition: console.c:52
uint32_t width
Definition: console.c:51
uint32_t base
Definition: console.c:49
uint32_t unknown1[4]
Definition: console.c:48
uint32_t unknown2[8]
Definition: console.c:50
int xenos_is_overscan()
Definition: xenos.c:635
struct detailed_data_wpindex color
Definition: xenos_edid.h:8
u8 c
Definition: xenos_edid.h:7