forked from EXL/P2kElfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_gba.c
187 lines (149 loc) · 4.16 KB
/
main_gba.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
Copyright (C) 2003 - Derek John Evans
This file is part of Yeti3D Portable Engine
Yeti3D is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Original Source: 2003 - Derek J. Evans <[email protected]>
Prepared for public release: 10/24/2003 - Derek J. Evans <[email protected]>
*/
/*
** Name: Yeti3D
** Desc: GBA port
** Auth: Derek Evans
**
** Copyright (C) 2003, Derek Evans
**
** YY YY EEEEEE TTTTTT IIIIII 33333 DDDDD
** YY YY EE TT II 33 DD DD
** YYYY EEEE TT II 333 DD DD
** YY EE TT II 33 DD DD
** YY EEEEEE TT IIIIII 33333 DDDDD
*/
#include "yeti.h"
#include "font.h"
#include "gba.h"
IN_EWRAM yeti_t yeti;
int min(int a, int b)
{
return MIN(a, b);
}
int max(int a, int b)
{
return MAX(a, b);
}
typedef struct
{
int Sx, l, u, v;
} edge_t;
typedef struct
{
u16 attribute0;
u16 attribute1;
u16 attribute2;
u16 attribute3;
} OAMEntry;
edge_t EdgeLeft[200];
edge_t EdgeRight[200];
/*
** Name: yeti_putc
** Desc: Puts a given character onto the screen.
*/
void yeti_putc(int x, int c)
{
((OAMEntry*)0x7000000)[x].attribute2 = ((c - ' ') << 1) + 512;
}
/*
** Name: yeti_putc
** Desc: Puts a given string onto the screen.
*/
void yeti_puts(int x, char* s)
{
while (*s) yeti_putc(x++, *s++);
}
void itos(int i, char* s)
{
//#define DIGITS(X) if(i>=X){c=0;do{i-=X;c++;}while(i >= X);*s++='0'+c;}
#define DIGITS(X) for(c=0;i>=X;i-=X,c++);*s++='0'+c;
int c;
DIGITS(100000); DIGITS(10000); DIGITS(1000); DIGITS(100); DIGITS(10);
*s++ = '0' + i;
*s = 0;
}
void gba_init(void)
{
int i;
REG_DISPCNT_L = 0x1445;
REG_BG2PA = 171;
REG_BG2PD = 206;
// Setup the sprites to be used for overlayed text.
for(i = 0; i < 128; i++)
{
((OAMEntry*)0x7000000)[i].attribute0 = ((i / 30) + 16) << 3 | 0x2000;
((OAMEntry*)0x7000000)[i].attribute1 = ((i % 30) + 0) << 3;
((OAMEntry*)0x7000000)[i].attribute2 = 0;
((OAMEntry*)0x7000000)[i].attribute3 = 0;
}
// Copy the rom font into sprite ram.
memcpy((void*)0x6014000, fontData, 91 * sizeof(fontchar_t));
// Copy our rom palette into palette ram.
memcpy((void*)0x5000200, palette, 256 * 2);
((u16*)0x5000200)[255] = RGB_SET(31, 31, 0);
REG_TM2CNT = 0x80;
REG_TM3CNT = 0x84;
}
void gba_flip(void)
{
int i;
framebuffer_t* temp;
i = ~(*(u8*)0x04000130);
yeti.keyboard.a = i & 1;
yeti.keyboard.b = i & 2;
yeti.keyboard.select = i & 4;
yeti.keyboard.right = i & 16;
yeti.keyboard.left = i & 32;
yeti.keyboard.up = i & 64;
yeti.keyboard.down = i & 128;
i = ~(*(u8*)0x04000131);
yeti.keyboard.l = i & 1;
yeti.keyboard.r = i & 2;
// Flip the frame buffers.
*(u8*)0x04000000 ^= 0x10;
temp = yeti.viewport.back;
yeti.viewport.back = yeti.viewport.front;
yeti.viewport.front = temp;
}
int main(void)
{
char s[10];
u16 real = 0;
gba_init();
yeti_init(&yeti, (framebuffer_t*) 0x06000000, (framebuffer_t*) 0x0600A000, textures, palette, lua);
game_init(&yeti);
yeti_puts(60 + 6, "Yeti3D Engine (GBA)");
yeti_puts(90 + 5, "(C) 2003 Derek/Torlus");
//yeti_puts(82, "FPS:");
#define timelag() (int)((*(volatile u16*)0x400010C) - real)
while (!yeti.keyboard.select)
{
game_tick(&yeti);
game_draw(&yeti);
for (real += YETI_VIEWPORT_INTERVAL; timelag() > YETI_VIEWPORT_INTERVAL_ERROR; real += YETI_VIEWPORT_INTERVAL)
{
game_tick(&yeti);
}
//itos(yeti.tick * 1000 / (*(volatile u16*)0x400010C), s);
//yeti_puts(0, s);
while (timelag() < -YETI_VIEWPORT_INTERVAL_ERROR);
gba_flip();
}
return 0;
}