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;
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
67inline void console_pset(int x, int y, unsigned char r, unsigned char g, unsigned char b) {
68 console_pset32(x, y, (b<<24) + (g<<16) + (r<<8));
69 /* little indian:
70 * fbint[base] = b + (g<<8) + (r<<16); */
71}
72
73static void console_draw_char(const int x, const int y, const unsigned char c) {
74#define font_pixel(ch, x, y) ((fontdata_8x16[ch*16+y]>>(7-x))&1)
75 int lx, ly;
76 for (ly=0; ly < 16; ly++)
77 for (lx=0; lx < 8; lx++) {
78 console_pset32(x+lx, y+ly, console_color[font_pixel(c, lx, ly)]);
79 }
80}
81
83 unsigned int *fb = (unsigned int*)console_fb;
84 int count = console_width * console_height;
85 while (count--)
86 *fb++ = console_color[0];
87
88 memdcbst(console_fb, console_size*4);
89
90 cursor_x=0;
91 cursor_y=0;
92}
93
95 char sp[max_x];
96 memset(sp,' ', max_x);
97 sp[max_x-1]='\0';
98 printf("\r%s\r",sp);
99}
100
101static void console_scroll32(const unsigned int lines) {
102 int l, bs;
103 bs = console_width*32*4;
104 /* copy all tile blocks to a higher position */
105 for (l=lines; l*32 < console_height; l++) {
106 memcpy(console_fb + bs*(l-lines),
107 console_fb + bs*l,
108 bs);
109 }
110
111 /* fill up last lines with background color */
112 uint32_t *fb = (uint32_t*)(console_fb + console_size*4 - bs*lines);
113 uint32_t *end = (uint32_t*)(console_fb + console_size*4);
114 while (fb != end)
115 *fb++ = console_color[0];
116
117 memdcbst(console_fb, console_size*4);
118}
119
121#if 0
122 /* fill up with spaces */
123 while (cursor_x*8 < console_width) {
124 console_draw_char(cursor_x*8, cursor_y*16, ' ');
125 cursor_x++;
126 }
127#endif
128
129 /* reset to the left */
130 cursor_x = 0;
131 cursor_y++;
132 if (cursor_y >= max_y) {
133 /* XXX implement scrolling */
134 console_scroll32(1);
135 cursor_y -= 2;
136 }
137}
138
139void console_putch(const char c) {
140 if (!console_fb)
141 return;
142 if (c == '\r') {
143 cursor_x = 0;
144 } else if (c == '\n') {
146 } else {
147 console_draw_char(cursor_x*8 + offset_x, cursor_y*16 + offset_y, c);
148 cursor_x++;
149 if (cursor_x >= max_x)
151 }
152 memdcbst(console_fb, console_size*4);
153}
154
155static void console_stdout_hook(const char *buf, int len)
156{
157 while (len--)
158 console_putch(*buf++);
159}
160
161extern void (*stdout_hook)(const char *buf, int len);
162
163void console_init(void) {
164 struct ati_info *ai = (struct ati_info*)0xec806100ULL;
165
166 console_fb = (unsigned char*)(long)(ai->base | 0x80000000);
167 /* round up size to tiles of 32x32 */
168 console_width = ((ai->width+31)>>5)<<5;
169 console_height = ((ai->height+31)>>5)<<5;
170 console_size = console_width*console_height;
171
172 offset_x = offset_y = 0;
173 if (xenos_is_overscan())
174 {
175 offset_x = (ai->width/28); //50;
176 offset_y = (ai->height/28); //50;
177 }
178
179 cursor_x = cursor_y = 0;
180 max_x = (ai->width - offset_x * 2) / 8;
181 max_y = (ai->height - offset_y * 2) / 16;
182
184
185 stdout_hook = console_stdout_hook;
186
187 printf(" * Xenos FB with %dx%d (%dx%d) at %p initialized.\n",
188 max_x, max_y, ai->width, ai->height, console_fb);
189}
190
191void console_set_colors(unsigned int background, unsigned int foreground){
192 console_color[0]=background;
193 console_color[1]=foreground;
194}
195
196void console_get_dimensions(unsigned int * width,unsigned int * height){
197 if (width) *width=max_x;
198 if (height) *height=max_y;
199}
200
202{
203 stdout_hook = 0;
204}
uint32_t console_oldfg
Definition: console.c:41
uint32_t console_oldbg
Definition: console.c:41
void console_close(void)
Definition: console.c:201
uint32_t width
Definition: console.c:3
void console_clrscr()
Definition: console.c:82
#define fbint
#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:120
void console_set_colors(unsigned int background, unsigned int foreground)
Definition: console.c:191
void console_clrline()
Definition: console.c:94
void console_get_dimensions(unsigned int *width, unsigned int *height)
Definition: console.c:196
void console_init(void)
Definition: console.c:163
#define font_pixel(ch, x, y)
uint32_t console_color[2]
Definition: console.c:40
void console_putch(const char c)
Definition: console.c:139
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