PS2SDK
PS2 Homebrew Libraries
scr_printf.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 */
10 
16 #include <stdio.h>
17 #include <tamtypes.h>
18 #include <sifcmd.h>
19 #include <kernel.h>
20 #include <rom0_info.h>
21 #include <stdarg.h>
22 #include <debug.h>
23 #include <ee_regs.h>
24 
25 static short int X = 0, Y = 0;
26 static short int MX = 80, MY = 40;
27 static u32 bgcolor = 0, fontcolor = 0xffffff, cursorcolor = 0xffffff;
28 static short int cursor = 1;
29 
30 struct t_setupscr
31 {
32  u64 dd0[6];
33  u32 dw0[2];
34  u64 dd1[1];
35  u16 dh[4];
36  u64 dd2[21];
37 };
38 
40 {
41  u64 dd0[4];
42  u32 dw0[1];
43  u16 x, y;
44  u64 dd1[1];
45  u32 dw1[2];
46  u64 dd2[5];
47 };
48 
49 // from gsKit
50 static int debug_detect_signal()
51 {
52  char romname[14];
53  GetRomName(romname);
54  return ((romname[4] == 'E') ? 1 : 0);
55 }
56 
57 static void Init_GS(int interlace, int omode, int ffmd)
58 {
59  // Reset GS
60  *R_EE_GS_CSR = 0x200;
61  // Mask interrupts
62  GsPutIMR(0xff00);
63  // Configure GS CRT
64  SetGsCrt(interlace, omode, ffmd);
65 }
66 
67 static void SetVideoMode(void)
68 {
69  /* DISPLAY1 0x0983227c001bf9ff
70  DX = 0x27C (636)
71  DY = 0x032 (50)
72 
73  MAGH = 0x03 (4x)
74  MAGV = 0x01 (2x)
75 
76  DW = 0x9FF (2560)
77  DH = 0x1BF (447) */
78 
79  *R_EE_GS_PMODE = 0xff62;
80  *R_EE_GS_DISPFB2 = 0x1400;
81  *R_EE_GS_DISPLAY2 = 0x001bf9ff0983227c;
82 }
83 
84 static inline void Dma02Wait(void)
85 {
86  while ((*R_EE_D2_CHCR & 0x100) != 0); // Wait until STR bit of D2_CHCR = 0
87 }
88 
89 static void DmaReset(void)
90 {
91  // This appears to have been based on code from Sony that initializes DMA channels 0-9, in bulk.
92  *((vu32 *)0x1000a080) = 0; // D2_SADR = 0. Documented to not exist, but is done.
93  *R_EE_D2_CHCR = 0;
94  *R_EE_D2_TADR = 0;
95  *R_EE_D2_MADR = 0;
96  *R_EE_D2_ASR1 = 0;
97  *R_EE_D2_ASR0 = 0;
98  *R_EE_D_STAT = 0xff1f; // Clear all interrupt status under D_STAT, other than SIF0, SIF1 & SIF2.
99  *R_EE_D_STAT &= 0xff1f << 16; // Clear all interrupt masks under D_STAT, other SIF0, SIF1 & SIF2. Writing a 1 reverses the bit.
100  *R_EE_D_CTRL = 0;
101  *R_EE_D_PCR = 0;
102  *R_EE_D_SQWC = 0;
103  *R_EE_D_RBOR = 0;
104  *R_EE_D_RBSR = 0;
105  *R_EE_D_CTRL |= 1; // (DMAE 1)
106 }
107 
114 static inline void progdma(void *addr, int size)
115 {
116  *R_EE_D2_QWC = (u32)size; // D2_QWC
117  *R_EE_D2_MADR = (u32)addr; // D2_MADR
118  *R_EE_D2_CHCR = 0x101; // D2_CHCR = STR 1, DIR 1
119 }
120 
121 void scr_setbgcolor(u32 color)
122 {
123  bgcolor = color;
124 }
125 
126 void scr_setfontcolor(u32 color)
127 {
128  fontcolor = color;
129 }
130 
131 void scr_setcursorcolor(u32 color)
132 {
133  cursorcolor = color;
134 }
135 
136 void init_scr(void)
137 {
138  static struct t_setupscr setupscr __attribute__((aligned(16))) = {
139  {0x100000000000800E, 0xE, 0xA0000, 0x4C, 0x8C, 0x4E}, // GIFtag (REGS: A+D, NREG 1, FLG PACKED, EOP, NLOOP 14), FRAME_1 (PSM PSMCT32, FBW 10, FBP 0), ZBUF_1 (PSM PSMZ32, ZBP 140)
140  {27648, 30976},
141  {0x18}, // XYOFFSET_1 (OFX 1728.0, OFY 1936.0)
142  {0, 639, 0, 223}, // SCISSOR_1 (SCAX0 0, SCAX1 639, SCAY0 0, SCAY1 223)
143  {0x40, 1, 0x1a, 1, 0x46, 0, 0x45, 0x70000, // PRMODECONT (AC PRIM), COLCLAMP (CLAMP 1), DTHE (DTHE 0)
144  0x47, 0x30000, 0x47, 6, 0, 0x3F80000000000000, 1, 0x79006C00, 5, // TEST_1 (ZTST GREATER, ZTE 1), TEST_1 (ZTST ALWAYS, ZTE 1), PMODE (CRTMD 1, EN2 1), RGBAQ (Q 1.0, A 0, B 0, G 0, R 0), XYZ2 (Z 0.0, Y 1728.0, X 1936.0)
145  0x87009400, 5, 0x70000, 0x47} // XYZ2 (Z 0, Y 2160.0, X 2368.0), TEST_1 (ZTST GREATER, ZTE 1)
146  };
147 
148  X = Y = 0;
149  DmaReset();
150 
151  Init_GS(1, debug_detect_signal() == 1 ? 3 : 2, 0); // Interlaced, NTSC/PAL and FIELD mode
152 
153  SetVideoMode();
154  Dma02Wait();
155  progdma(&setupscr, 15);
156  Dma02Wait();
157 }
158 
159 extern u8 msx[];
160 
161 void scr_putchar(int x, int y, u32 color, int ch)
162 {
163  static struct t_setupchar setupchar __attribute__((aligned(16))) = {
164  {0x1000000000000004, 0xE, 0xA000000000000, 0x50}, // GIFtag (REGS: A+D, NREG 1, FLG PACKED, NLOOP 4), BITBLTBUF (DPSM PSMCT32, DBW 10, DBP 0)
165  {0},
166  100,
167  100,
168  {0x51}, // TRXPOS (DSAX 100, DSAY 100)
169  {8, 8}, // TRXREG (RRW 8, RRH 8)
170  {0x52, 0, 0x53, 0x800000000008010, 0} // TRXDIR (XDIR Host -> Local), GIFtag (FLG IMAGE, EOP, NLOOP 16)
171  };
172  /* charmap must be aligned to a 16-bye boundary. */
173  static u32 charmap[64] __attribute__((aligned(16)));
174  int i, j, l;
175  u8 *font;
176  u32 pixel;
177 
178  ((struct t_setupchar *)UNCACHED_SEG(&setupchar))->x = x;
179  ((struct t_setupchar *)UNCACHED_SEG(&setupchar))->y = y;
180 
181  progdma(&setupchar, 6);
182 
183  font = &msx[ch * 8];
184  for (i = l = 0; i < 8; i++, l += 8, font++) {
185  for (j = 0; j < 8; j++) {
186  pixel = ((*font & (128 >> j))) ? color : bgcolor;
187  *(u32 *)UNCACHED_SEG(&charmap[l + j]) = pixel;
188  }
189  }
190 
191  Dma02Wait();
192 
193  progdma(charmap, (8 * 8 * 4) / 16);
194  Dma02Wait();
195 }
196 
197 void scr_clearchar(int X, int Y)
198 {
199  scr_putchar(X * 8, Y * 8, bgcolor, ' ');
200 }
201 
202 void scr_clearline(int Y)
203 {
204  int i;
205  for (i = 0; i < MX; i++)
206  scr_putchar(i * 8, Y * 8, bgcolor, ' ');
207 }
208 
209 void scr_printf(const char *format, ...)
210 {
211  va_list opt;
212  va_start(opt, format);
213  scr_vprintf(format, opt);
214  va_end(opt);
215 }
216 
217 void scr_vprintf(const char *format, va_list opt)
218 {
219  char buff[2048];
220  int i, bufsz, j;
221 
222 
223  bufsz = vsnprintf(buff, sizeof(buff), format, opt);
224 
225  for (i = 0; i < bufsz; i++) {
226  char c;
227  c = buff[i];
228  switch (c) {
229  case '\n':
230  X = 0;
231  Y++;
232  if (Y == MY)
233  Y = 0;
234  scr_clearline(Y);
235  break;
236  case '\t':
237  for (j = 0; j < 5; j++) {
238  scr_putchar(X * 7, Y * 8, fontcolor, ' ');
239  X++;
240  }
241  break;
242  case '\r':
243  X = 0;
244  // scr_clearline(Y); //Should we clear the line?
245  break;
246  default:
247  scr_putchar(X * 7, Y * 8, fontcolor, c);
248  X++;
249  if (X == MX) {
250  X = 0;
251  Y++;
252  if (Y == MY)
253  Y = 0;
254  scr_clearline(Y);
255  }
256  }
257  }
258  if (cursor)
259  scr_putchar(X * 7, Y * 8, cursorcolor, 219);
260 }
261 
262 void scr_setXY(int x, int y)
263 {
264  if (x < MX && x >= 0)
265  X = x;
266  if (y < MY && y >= 0)
267  Y = y;
268 }
269 
270 int scr_getX()
271 {
272  return X;
273 }
274 
275 int scr_getY()
276 {
277  return Y;
278 }
279 
280 void scr_clear()
281 {
282  int y;
283  for (y = 0; y < MY; y++)
284  scr_clearline(y);
285  scr_setXY(0, 0);
286 }
287 
288 void scr_setCursor(int enable)
289 {
290  cursor = enable;
291 }
kernel.h
R_EE_GS_PMODE
#define R_EE_GS_PMODE
Definition: ee_regs.h:567
R_EE_D_PCR
#define R_EE_D_PCR
Definition: ee_regs.h:452
rom0_info.h
R_EE_D2_MADR
#define R_EE_D2_MADR
Definition: ee_regs.h:386
R_EE_D_RBSR
#define R_EE_D_RBSR
Definition: ee_regs.h:456
GetRomName
char * GetRomName(char *romname)
R_EE_D_CTRL
#define R_EE_D_CTRL
Definition: ee_regs.h:448
R_EE_D2_ASR1
#define R_EE_D2_ASR1
Definition: ee_regs.h:394
progdma
static void progdma(void *addr, int size)
Definition: scr_printf.c:114
__attribute__
typedef __attribute__
Definition: tlbfunc.c:60
t_setupscr
Definition: scr_printf.c:30
R_EE_GS_DISPFB2
#define R_EE_GS_DISPFB2
Definition: ee_regs.h:585
t_setupchar
Definition: scr_printf.c:39
R_EE_D_STAT
#define R_EE_D_STAT
Definition: ee_regs.h:450
tamtypes.h
stdio.h
R_EE_D2_CHCR
#define R_EE_D2_CHCR
Definition: ee_regs.h:384
__attribute__
Definition: gif_registers.h:38
R_EE_D_RBOR
#define R_EE_D_RBOR
Definition: ee_regs.h:458
R_EE_D2_QWC
#define R_EE_D2_QWC
Definition: ee_regs.h:388
R_EE_D_SQWC
#define R_EE_D_SQWC
Definition: ee_regs.h:454
R_EE_GS_CSR
#define R_EE_GS_CSR
Definition: ee_regs.h:597
R_EE_D2_TADR
#define R_EE_D2_TADR
Definition: ee_regs.h:390
ee_regs.h
R_EE_GS_DISPLAY2
#define R_EE_GS_DISPLAY2
Definition: ee_regs.h:587
R_EE_D2_ASR0
#define R_EE_D2_ASR0
Definition: ee_regs.h:392