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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <psxetc.h>
#include <psxgte.h>
#include <psxgpu.h>
#include <psxapi.h>
#include <siofs.h>
#define OT_LEN 8
#define SCREEN_XRES 320
#define SCREEN_YRES 240
#define CENTER_X SCREEN_XRES/2
#define CENTER_Y SCREEN_YRES/2
/* Display and drawing environments */
DISPENV disp[2];
DRAWENV draw[2];
char pribuff[2][16384]; /* Primitive packet buffers */
unsigned int ot[2][OT_LEN]; /* Ordering tables */
char *nextpri; /* Pointer to next packet buffer offset */
int db = 0; /* Double buffer index */
void init() {
int i;
/* Reset GPU (also installs event handler for VSync) */
printf("Init GPU... ");
ResetGraph( 0 );
printf("Done.\n");
printf("Set video mode... ");
/* Set display and draw environment parameters */
SetDefDispEnv( &disp[0], 0, 0, SCREEN_XRES, SCREEN_YRES );
SetDefDrawEnv( &draw[0], 0, SCREEN_YRES, SCREEN_XRES, SCREEN_YRES );
SetDefDispEnv( &disp[1], 0, SCREEN_YRES, SCREEN_XRES, SCREEN_YRES );
SetDefDrawEnv( &draw[1], 0, 0, SCREEN_XRES, SCREEN_YRES );
//disp[0].isinter = 1; /* Enable interlace (required for hires) */
//disp[1].isinter = 1;
/* Set clear color, area clear and dither processing */
setRGB0( &draw[0], 63, 0, 127 );
draw[0].isbg = 1;
draw[0].dtd = 1;
setRGB0( &draw[1], 63, 0, 127 );
draw[1].isbg = 1;
draw[1].dtd = 1;
/* Apply the display and drawing environments */
PutDispEnv( &disp[1] );
PutDrawEnv( &draw[1] );
/* Enable video output */
SetDispMask( 1 );
printf("Done.\n");
FntLoad( 960, 0 );
FntOpen( 8, 16, 304, 216, 0, 100 );
}
extern void _InitPadDirect(void);
extern int _PadReadDirect(int port, unsigned char *data, int len);
extern int _CardRead(int port, unsigned char *data, int lba);
extern int _CardWrite(int port, unsigned char *data, int lba);
extern int _PadExchng(int byte);
extern void _PadSetPort(int port);
extern unsigned char _pad_mot_values[];
unsigned char padbuff[2][34];
char *hex_tbl = "0123456789ABCDEF";
void byte2str(unsigned char *in, int bytes, char *out)
{
int i;
for( i=0; i<bytes; i++ )
{
*out = hex_tbl[in[i]>>4]; out++;
*out = hex_tbl[in[i]&0xF]; out++;
}
*out = 0x0;
}
unsigned char card_buff[128];
void dump_data(unsigned char *buff)
{
int ix,iy;
for( iy=0; iy<8; iy++ )
{
for( ix=0; ix<16; ix++ )
{
printf( "%02x", *buff );
buff++;
}
printf( "\n" );
}
}
void TurnOnVibrators(void)
{
_PadSetPort(0);
if( _PadExchng(0x01) & 0x100 )
{
ExitCriticalSection();
return;
}
if( _PadExchng(0x43) & 0x100 )
{
ExitCriticalSection();
return;
}
_PadExchng(0x00);
_PadExchng(0x01);
while( !(_PadExchng(0) & 0x100) );
_PadSetPort(2);
ExitCriticalSection();
VSync(0);
EnterCriticalSection();
// Set analog state
_PadSetPort(0);
_PadExchng(0x01);
_PadExchng(0x4D);
_PadExchng(0x00);
_PadExchng(0x00);
_PadExchng(0x01);
_PadExchng(0xFF);
_PadExchng(0xFF);
_PadExchng(0xFF);
_PadExchng(0xFF);
_PadSetPort(2);
ExitCriticalSection();
VSync(0);
EnterCriticalSection();
// Exit configuration mode
_PadSetPort(0);
_PadExchng(0x01);
_PadExchng(0x43);
_PadExchng(0x00);
_PadExchng(0x00);
while( !(_PadExchng(0) & 0x100) );
_PadSetPort(2);
ExitCriticalSection();
VSync(0);
}
void TurnOnAnalog(void)
{
EnterCriticalSection();
// Enter configuration mode
_PadSetPort(0);
if( _PadExchng(0x01) & 0x100 )
{
ExitCriticalSection();
return;
}
if( _PadExchng(0x43) & 0x100 )
{
ExitCriticalSection();
return;
}
_PadExchng(0x00);
_PadExchng(0x01);
while( !(_PadExchng(0) & 0x100) );
_PadSetPort(2);
ExitCriticalSection();
VSync(0);
EnterCriticalSection();
// Set analog state
_PadSetPort(0);
_PadExchng(0x01);
_PadExchng(0x44);
_PadExchng(0x00);
_PadExchng(0x01); // 0 - analog off, 1 - analog on
_PadExchng(0x02);
while( !(_PadExchng(0) & 0x100) );
_PadSetPort(2);
ExitCriticalSection();
VSync(0);
EnterCriticalSection();
// Exit configuration mode
_PadSetPort(0);
_PadExchng(0x01);
_PadExchng(0x43);
_PadExchng(0x00);
_PadExchng(0x00);
while( !(_PadExchng(0) & 0x100) );
_PadSetPort(2);
ExitCriticalSection();
VSync(0);
}
int main(int argc, const char* argv[]) {
DR_TPAGE *tpri;
int i,j,counter=0,fd;
char textbuff[64];
char *memcard_image,*p;
/* Init graphics and stuff before doing anything else */
init();
_InitPadDirect();
/*
if( fsInit(115200) >= 0 )
{
printf("Loading memory card image... ");
fd = fsOpen("cardimage.bin", FS_READ|FS_BINARY);
if( fd < 0 )
{
printf("File not found\n");
}
else
{
memcard_image = (char*)malloc(131072);
fsRead(fd, memcard_image, 131072);
fsClose(fd);
printf("Done.\n");
p = memcard_image;
for( j=0; j<1024; j++ )
{
i = _CardWrite(0, p, j);
printf("Writing memory card (%d/1024)... \r", j);
if( i )
printf("\nError %d at sector %d\n", i, j);
p += 128;
// Memory cards need at least 2 vsyncs between each write
// apparently
VSync(2);
}
}
}
else
{
fd = -1;
memcard_image = NULL;
}
*/
/*
for( j=0; j<1024; j++ )
{
memset( card_buff, 0xff, 128 );
i = _CardRead( 0, card_buff, j );
printf("Reading memory card (%d/1024)... \r", j);
if( i != 0 )
{
printf("\nPotential read error at sector %d\n", j);
}
if( fd >= 0 )
fsWrite(fd, (u_char*)card_buff, 128);
if( i )
break;
}
if( fd >= 0 )
fsClose(fd);
printf("\nSuccessfully read entire memory card\n");
*/
/*
i = _CardRead( 0, card_buff, j );
printf("Clearing memory card FAT.\n");
memset( card_buff, 0xff, 128 );
for(j=0; j<4; j++)
{
i = _CardWrite( 0, card_buff, 4 );
printf("_CardWrite returned %d\n", i);
VSync(2);
}
*/
TurnOnAnalog();
TurnOnVibrators();
/* Main loop */
printf("Entering loop...\n");
while(1) {
/*
if((counter>>4)&0x1)
{
_pad_mot_values[0] = 0xff;
}
else
{
_pad_mot_values[0] = 0x0;
}
*/
i = _PadReadDirect(0, padbuff[0], 34);
//_PadReadDirect(1, padbuff[1], 34);
FntPrint( -1, "HELLO WORLD %d\n", counter );
byte2str( padbuff[0], i, textbuff );
FntPrint( -1, "P1:%s\n", textbuff );
//byte2str( padbuff[1], 8, textbuff );
//FntPrint( -1, "P2:%s\n", textbuff );
/* Clear ordering table and set start address of primitive */
/* buffer for next frame */
ClearOTagR( ot[db], OT_LEN );
nextpri = pribuff[db];
/* Wait for GPU and VSync */
FntFlush(-1);
DrawSync( 0 );
VSync( 0 );
/* Since draw.isbg is non-zero this clears the screen */
PutDispEnv( &disp[db] );
PutDrawEnv( &draw[db] );
/* Begin drawing the new frame */
DrawOTag( ot[db]+(OT_LEN-1) );
/* Alternate to the next buffer */
db = !db;
/* Increment counter for the snake animation */
counter++;
}
return 0;
}
|