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
101 char sp[max_x];
102 memset(sp,' ', max_x);
103 sp[max_x-1]='\0';
104 printf("\r%s\r",sp);
105}
106
107static void console_scroll32(const unsigned int lines) {
108 int l, bs;
109 bs = console_width*32*4;
110 /* copy all tile blocks to a higher position */
111 for (l=lines; l*32 < console_height; l++) {
112 memcpy(console_fb + bs*(l-lines),
113 console_fb + bs*l,
114 bs);
115 }
116
117 /* fill up last lines with background color */
118 uint32_t *fb = (uint32_t*)(console_fb + console_size*4 - bs*lines);
119 uint32_t *end = (uint32_t*)(console_fb + console_size*4);
120 while (fb != end)
121 *fb++ = console_color[0];
122
123 memdcbst(console_fb, console_size*4);
124}
125
127#if 0
128 /* fill up with spaces */
129 while (cursor_x*8 < console_width) {
130 console_draw_char(cursor_x*8, cursor_y*16, ' ');
131 cursor_x++;
132 }
133#endif
134
135 /* reset to the left */
136 cursor_x = 0;
137 cursor_y++;
138 if (cursor_y >= max_y) {
139 /* XXX implement scrolling */
140 console_scroll32(1);
141 cursor_y -= 2;
142 }
143}
144
145void console_putch(const char c) {
146 if (!console_fb)
147 return;
148 if (c == '\r') {
149 cursor_x = 0;
150 } else if (c == '\n') {
152 } else {
153 console_draw_char(cursor_x*8 + offset_x, cursor_y*16 + offset_y, c);
154 cursor_x++;
155 if (cursor_x >= max_x)
157 }
158 memdcbst(console_fb, console_size*4);
159}
160
161static void console_stdout_hook(const char *buf, int len)
162{
163 while (len--)
164 console_putch(*buf++);
165}
166
167extern void (*stdout_hook)(const char *buf, int len);
168
169void console_init(void) {
170 struct ati_info *ai = (struct ati_info*)0xec806100ULL;
171
172 console_fb = (unsigned char*)(long)(ai->base | 0x80000000);
173 /* round up size to tiles of 32x32 */
174 console_width = ((ai->width+31)>>5)<<5;
175 console_height = ((ai->height+31)>>5)<<5;
176 console_size = console_width*console_height;
177
178 offset_x = offset_y = 0;
179 if (xenos_is_overscan())
180 {
181 offset_x = (ai->width/28); //50;
182 offset_y = (ai->height/28); //50;
183 }
184
185 cursor_x = cursor_y = 0;
186 pixel_max_x = ai->width - offset_x * 2;
187 max_x = pixel_max_x / 8;
188 pixel_max_y = ai->height - offset_y * 2;
189 max_y = pixel_max_y / 16;
190
192
193 stdout_hook = console_stdout_hook;
194
195 printf(" * Xenos FB with %dx%d (%dx%d) at %p initialized.\n",
196 max_x, max_y, ai->width, ai->height, console_fb);
197}
198
199void console_set_colors(unsigned int background, unsigned int foreground){
200 console_color[0]=background;
201 console_color[1]=foreground;
202}
203
204void console_get_dimensions(unsigned int * width,unsigned int * height){
205 if (width) *width=max_x;
206 if (height) *height=max_y;
207}
208
209void console_open(void)
210{
211 stdout_hook = console_stdout_hook;
212}
213
215{
216 stdout_hook = 0;
217}
uint32_t console_oldfg
Definition: console.c:41
uint32_t console_oldbg
Definition: console.c:41
void console_close(void)
Definition: console.c:214
uint32_t width
Definition: console.c:3
void console_clrscr()
Definition: console.c:88
#define fbint
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:126
void console_set_colors(unsigned int background, unsigned int foreground)
Definition: console.c:199
void console_open(void)
Definition: console.c:209
void console_clrline()
Definition: console.c:100
void console_get_dimensions(unsigned int *width, unsigned int *height)
Definition: console.c:204
void console_init(void)
Definition: console.c:169
#define font_pixel(ch, x, y)
uint32_t console_color[2]
Definition: console.c:40
void console_putch(const char c)
Definition: console.c:145
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