1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - WM - demos - wep-1
3   File:     print.c
4 
5   Copyright 2006-2008 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #ifdef SDK_TWL
19 #include <twl.h>
20 #else
21 #include <nitro.h>
22 #endif
23 #include "print.h"
24 #include "wh.h"
25 
PR_ClearScreen(PRScreen * scr)26 void PR_ClearScreen(PRScreen * scr)
27 {
28     MI_CpuClear16((void *)scr->screen, PR_SCREEN_SIZE * sizeof(u16));
29     scr->curx = 0;
30     scr->cury = 0;
31 }
32 
PR_Locate(PRScreen * scr,int x,int y)33 void PR_Locate(PRScreen * scr, int x, int y)
34 {
35     scr->curx = x;
36     scr->cury = y;
37 }
38 
PR_ScrollUp(PRScreen * scr)39 static void PR_ScrollUp(PRScreen * scr)
40 {
41     int     y;
42 
43     if (!scr->scroll)
44     {
45         PR_ClearScreen(scr);
46         return;
47     }
48 
49     for (y = 0; y < PR_SCREEN_HEIGHT - 1; ++y)
50     {
51         MI_CpuCopy16((void *)(scr->screen + (y + 1) * PR_SCREEN_WIDTH),
52                      (void *)(scr->screen + y * PR_SCREEN_WIDTH), PR_SCREEN_WIDTH * sizeof(u16));
53     }
54 
55     (void)MI_CpuClear16((void *)(scr->screen
56                                  + (PR_SCREEN_HEIGHT - 1) * PR_SCREEN_WIDTH),
57                         PR_SCREEN_WIDTH * sizeof(u16));
58 }
59 
PR_PutString(PRScreen * scr,const char * text)60 void PR_PutString(PRScreen * scr, const char *text)
61 {
62     const char *p;
63     u8      pal;
64 
65     p = text;
66     pal = 0x0f;
67 
68     while (*p != '\0')
69     {
70         if (*p == '\n')
71         {
72             scr->curx = 0;
73             ++scr->cury;
74             if (scr->cury > PR_SCREEN_HEIGHT - 1)
75             {
76                 scr->cury = PR_SCREEN_HEIGHT - 1;
77                 PR_ScrollUp(scr);
78             }
79             ++p;
80             continue;
81 
82         }
83         else if (*p == '\\')
84         {
85             ++p;
86 
87             if (('0' <= *p) && (*p <= '9'))
88             {
89                 pal = (u8)(*p - '0');
90                 ++p;
91 
92             }
93             else if (('a' <= *p) && (*p <= 'f'))
94             {
95                 pal = (u8)(*p - 'a' + 10);
96                 ++p;
97             }
98 
99             continue;
100         }
101 
102         scr->screen[scr->cury * PR_SCREEN_WIDTH + scr->curx] = (u16)((pal << 12) | (0x0fff & *p));
103         ++p;
104 
105         ++scr->curx;
106         if (scr->curx > PR_SCREEN_WIDTH - 1)
107         {
108             scr->curx = 0;
109             ++scr->cury;
110             if (scr->cury > PR_SCREEN_HEIGHT - 1)
111             {
112                 scr->cury = PR_SCREEN_HEIGHT - 1;
113                 PR_ScrollUp(scr);
114             }
115         }
116     }
117 }
118 
PR_VPrintString(PRScreen * scr,const char * fmt,va_list vlist)119 void PR_VPrintString(PRScreen * scr, const char *fmt, va_list vlist)
120 {
121     char    buf[256];
122 
123     // Will not work if vsnprintf
124     // (void)vsnprintf(buf, 256 - 2, fmt, vlist);
125 
126     (void)OS_VSNPrintf(buf, 256 - 2, fmt, vlist);
127     buf[255] = '\0';
128 
129     PR_PutString(scr, buf);
130     // OS_Printf("%s", buf);
131 }
132 
PR_PrintString(PRScreen * scr,const char * fmt,...)133 void PR_PrintString(PRScreen * scr, const char *fmt, ...)
134 {
135     va_list vlist;
136     va_start(vlist, fmt);
137     PR_VPrintString(scr, fmt, vlist);
138     va_end(vlist);
139 }
140