zaru.c
14.7 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/**************************************************************************
* *
* Copyright (C) 1995, 1996 Silicon Graphics, Inc. *
* *
* These coded instructions, statements, and computer programs contain *
* unpublished proprietary information of Silicon Graphics, Inc., and *
* are protected by Federal copyright law. They may not be disclosed *
* to third parties or copied or duplicated in any form, in whole or *
* in part, without the prior written consent of Silicon Graphics, Inc. *
* *
*************************************************************************/
/*
* File: zaru.c
* Creator: scott@sgi.com
* Purpose: Code for the Zaru Pitts Memorial test jig.
*
*/
/**************************************************************************
$B2~D{MzNr(B $BOBED(B
96/10 ZaruReadMemXX$B$NCf$N(BInitSi$B4X?t$,ITMW$J$N$G:o=|(B
$B%3%s%H%m!<%i%G!<%?$N0l3gFI$_=P$74X?t$rIUM?(B
************************************************************************/
#define USE_UJI_PIF 1
#include <ultra64.h>
#include "gng.h"
#include "zaru.h"
extern void playSubway(void);
#ifdef USE_UJI_PIF
#include "../uji/uji_pif.h"
SI si1, *si;
#endif
/* Shadowed registers */
static u8 Register2000;
static u8 Register2800;
static OSMesgQueue *siqueue;
static int zaruchannel;
static u8 buffer[BLOCKSIZE];
/*
* Initialize the Zaru, return 0 on failure, 1 on success
*/
int
ZaruInit(OSMesgQueue *mq, int joychannel)
{
siqueue = mq;
zaruchannel = joychannel;
zaruexists = 1; /* Assume it exists for the first write to MemA */
#ifdef USE_UJI_PIF
si = &si1;
#endif
/* Use Uji's procedure to write and read 0x55 then 0xaa */
ZaruSetMemA(0x55);
if (ZaruGetMemA() == 0x55) {
ZaruSetMemA(0xaa);
if (ZaruGetMemA() != 0xaa) {
zaruexists = 0;
/* return 0; by sasano */
return 0; /* by DLE */
}
} else {
zaruexists = 0;
/* return 0; by sasano */
return 0; /* by DLE */
}
/* more intialization */
Register2000 = 0;
Register2800 = 0;
ZaruWriteReg(REG_2000, Register2000);
ZaruWriteReg(REG_2800, Register2800);
zaruexists = 1;
return 1;
}
#ifdef USE_LIBULTRA
/*
* This code was an attempt to use the PIF routines in libultra. However,
* the libultra routines would have to be modified to add direct access to
* the rampack. Currently they only support the filesystem and full checking
* of the CRCs
*/
/*
* Write data value to the Zaru register. Use PIF routines to Write to
* Joy1's "rampack"
*/
s32
ZaruWriteReg(ZARUReg reg, u8 data)
{
int i;
s32 retvalue;
for (i = 0; i < BLOCKSIZE; i++)
buffer[i] = data;
PRINTF("ZaruWriteReg calling __osContRamWrite\n");
retvalue = __osContRamWrite(siqueue, zaruchannel, reg/BLOCKSIZE, buffer, 1);
PRINTF("retvalue of osContRamWrite is 0x%x\n", retvalue);
return retvalue;
}
u8
ZaruReadReg(ZARUReg reg)
{
int i;
s32 retvalue;
for (i = 0; i < BLOCKSIZE; i++)
buffer[i] = 0xff;
PRINTF("ZaruReadReg calling __osContRamRead\n");
retvalue = __osContRamRead(siqueue, zaruchannel, reg/BLOCKSIZE, buffer, 1);
PRINTF("ZaruReadReg returning 0x%x\n", buffer[0]);
return buffer[0];
}
#endif
#ifdef USE_UJI_PIF
/*
* Write data value to the Zaru register. Use PIF routines to Write to
* Joy1's "rampack"
*/
void
ZaruWriteReg(ZARUReg reg, u8 data)
{
ZaruWriteReg2(reg,data);
return;
/* sasano
int i;
for(i = 0; i < MAXCONTROLLERS ; i++) {
if( i == zaruchannel )
si->command[i] = CNT_WRITE;
else
si->command[i] = NOP;
}
FormatSiBuffer(si);
SetWritePadRam(si, zaruchannel, reg/BLOCKSIZE);
for(i = 0 ; i < BLOCKSIZE ; i++)
RAW_DATA(si,zaruchannel,i+5) = data;
WriteSi(siqueue,si);
ReadSi(siqueue,si);
return 0;
*/
}
u8
ZaruReadReg(ZARUReg reg)
{
return ZaruReadReg2(reg);
/* sasano
int i;
InitSi(siqueue, si);
for(i = 0; i < MAXCONTROLLERS ; i++) {
if( i == zaruchannel )
si->command[i] = CNT_READ;
else
si->command[i] = NOP;
}
FormatSiBuffer(si);
SetReadPadRam(si, zaruchannel, reg/BLOCKSIZE);
WriteSi(siqueue, si);
ReadSi(siqueue,si);
for(i = 0 ; i < BLOCKSIZE ; i++)
buffer[i]=RAW_DATA(si,zaruchannel,i+5);
return(RAW_DATA(si,zaruchannel,5));
*/
}
void
ZaruWriteReg2(ZARUReg reg, u8 data)
{
int i;
ZaruReadReg(reg);
for(i = 0; i < MAXCONTROLLERS ; i++) {
if( i == zaruchannel )
si->command[i] = CNT_WRITE;
else
si->command[i] = NOP;
}
FormatSiBuffer(si);
SetWritePadRam(si, zaruchannel, reg/BLOCKSIZE);
RAW_DATA(si,zaruchannel,5+(reg%0x20))=data;
WriteSi(siqueue,si);
ReadSi(siqueue,si);
}
void
ZaruWriteReg3(ZARUReg reg,u8 num,u8* data)
{
int i;
ZaruReadReg(reg);
for(i = 0; i < MAXCONTROLLERS ; i++) {
if( i == zaruchannel )
si->command[i] = CNT_WRITE;
else
si->command[i] = NOP;
}
FormatSiBuffer(si);
SetWritePadRam(si, zaruchannel, reg/BLOCKSIZE);
for(i=0;i<num;i++) RAW_DATA(si,zaruchannel,5+(reg%0x20)+i)=data[i];
WriteSi(siqueue,si);
ReadSi(siqueue,si);
}
void
ZaruWriteReg4(ZARUReg reg,u8 num,u8* data,int channel)
{
int i;
ZaruReadReg4(reg,channel);
for(i = 0; i < MAXCONTROLLERS ; i++) {
if( i == channel )
si->command[i] = CNT_WRITE;
else
si->command[i] = NOP;
}
FormatSiBuffer(si);
SetWritePadRam(si, channel, reg/BLOCKSIZE);
for(i=0;i<num;i++ ) RAW_DATA(si,channel,5+(reg%0x20)+i) = data[i];
WriteSi(siqueue,si);
ReadSi(siqueue,si);
}
u8
ZaruReadReg2(ZARUReg reg)
{
int i;
for(i = 0; i < MAXCONTROLLERS ; i++) {
if( i == zaruchannel )
si->command[i] = CNT_READ;
else
si->command[i] = NOP;
}
FormatSiBuffer(si);
SetReadPadRam(si, zaruchannel, reg/BLOCKSIZE);
WriteSi(siqueue, si);
ReadSi(siqueue,si);
for(i = 0 ; i < BLOCKSIZE ; i++)
buffer[i]=RAW_DATA(si,zaruchannel,i+5);
return(RAW_DATA(si,zaruchannel,5+(reg%0x20)));
}
u8
ZaruReadReg4(ZARUReg reg,int channel)
{
int i;
for(i = 0; i < MAXCONTROLLERS ; i++) {
if( i == channel )
si->command[i] = CNT_READ;
else
si->command[i] = NOP;
}
FormatSiBuffer(si);
SetReadPadRam(si, channel, reg/BLOCKSIZE);
WriteSi(siqueue, si);
ReadSi(siqueue,si);
for(i = 0 ; i < BLOCKSIZE ; i++)
buffer[i]=RAW_DATA(si,channel,i+5);
return(RAW_DATA(si,channel,5+(reg%0x20)));
}
u16
ZaruReadControllerButton(int joychannel)
{
SiContStartReadData(siqueue, si);
return((RAW_DATA((si),joychannel,4)<<8)+RAW_DATA((si),joychannel,3));
}
u32
ZaruReadControllerButton2(int joychannel)
{
SiContStartReadData(siqueue, si);
return((RAW_DATA((si),joychannel,1)<<24)+(RAW_DATA((si),joychannel,0)<<16)+(RAW_DATA((si),joychannel,4)<<8)+RAW_DATA((si),joychannel,3));
}
/**************************************************************************
$BA4%A%c%s%M%k$N%3%s%H%m!<%i%G!<%?$rFI$_=P$9(B
************************************************************************/
void ZaruReadAllControllerButton( u8 *Buffer ){
int Channel;
SiContStartReadData(siqueue, si);
for( Channel=0 ; Channel<4 ; Channel++ ){
*Buffer ++= RAW_DATA( (si), Channel, 1 );
*Buffer ++= RAW_DATA( (si), Channel, 0 );
*Buffer ++= RAW_DATA( (si), Channel, 4 );
*Buffer ++= RAW_DATA( (si), Channel, 3 );
}
}
#endif /* USE_UJI_PIF */
/* Set the data in register A. This also controls the LEDs */
void
ZaruSetMemA(u8 data)
{
if (zaruexists) {
ZaruWriteReg(REG_0800, data);
}
}
u8
ZaruGetMemA()
{
if (zaruexists) {
return ZaruReadReg(REG_0800);
} else {
return 0;
}
}
/* Set the data in register B. */
void
ZaruSetMemB(u8 data)
{
if (zaruexists) {
ZaruWriteReg(REG_1000, data);
}
}
u8
ZaruGetMemB()
{
if (zaruexists) {
return ZaruReadReg(REG_1000);
} else {
return 0;
}
}
/*
* Set the data in Joy Switch register.
* Can read back for buttons on Joy 2, 3 & 4
* Use ZaruSetJoySelect for joy port number
*/
void
ZaruSetJoyButtons(u8 data)
{
if (zaruexists) {
ZaruWriteReg(REG_1800, data);
}
}
/*
* Select Joy channel for setting button bits
* Changes low order bits for 0x2000
*/
void
ZaruSetJoySelect(u8 data)
{
if (zaruexists) {
Register2000 &= ~ZARU_JOY_SELECT_MASK;
Register2000 |= data;
ZaruWriteReg(REG_2000, Register2000);
}
}
/*
* Set the video standard to either NTSC or PAL
* Changes D3 in REG_2000.
*/
void
ZaruSetVideoStd(u8 std)
{
if (zaruexists) {
Register2000 &= ~ZARU_VIDEO_MASK;
if (std == ZARU_NTSC) {
Register2000 |= ZARU_VIDEO_NTSC;
}
ZaruWriteReg(REG_2000, Register2000);
}
}
/*
* Set the video source to either SVideo or composite video
* Changes D4 in REG_2000.
*/
void
ZaruSetVideoSource(u8 source)
{
if (zaruexists) {
Register2000 &= ~ZARU_VIDEO_SOURCE_MASK;
if (source == ZARU_COMPOSITE) {
Register2000 |= ZARU_VIDEO_SOURCE_COMPOSITE;
}
ZaruWriteReg(REG_2000, Register2000);
}
}
/*
* Resart second board in Zaru. Do this by toggling the restart bit.
* REG_2000 bit D5
*/
void
ZaruRestartBoard2(void)
{
if (zaruexists) {
ZaruWriteReg(REG_2000, Register2000 | ZARU_RESTART_BOARD2);
ZaruWriteReg(REG_2000, Register2000);
}
}
/*
* Set the expansion command.
* (REG_2800 D0,D1,D2)
*/
void
ZaruSetExpCommand(u8 command)
{
if (zaruexists) {
Register2800 &= ~ZARU_EXP_CMD_MASK;
Register2800 |= command;
ZaruWriteReg(REG_2800, Register2800);
ZaruRestartBoard2();
}
}
/*
* Return the address AD16 address latched by the Zaru. Force writes to the AD16
* interface using the supplied address.
*/
unsigned int *
ZaruGetAD16Address(unsigned int *pAddress)
{
unsigned int *returnAddress, *cpuAddress, readWord;
unsigned char nibble0, nibble1, nibble2, nibble3;
if (zaruexists) {
cpuAddress = (unsigned int *) ( PHYS_TO_K1(pAddress) );
readWord = *cpuAddress;
ZaruSetExpCommand(ZARU_EXP_CMD_UPPER_AD16);
nibble3 = ZaruGetExpHigh();
nibble2 = ZaruGetExpLow();
ZaruSetExpCommand(ZARU_EXP_CMD_LOWER_AD16);
readWord = *cpuAddress;
nibble1 = ZaruGetExpHigh();
nibble0 = ZaruGetExpLow();
returnAddress = (unsigned int *)
( (nibble3 << 24) | (nibble2 << 16) | (nibble1 << 8) | (nibble0) );
return(returnAddress);
} else {
return 0;
}
}
/*
* Control the external sound generator.
*
* Changes bits D6 & D7 in REG_2800
*/
void
ZaruSetSound(u8 sound)
{
if (zaruexists) {
Register2800 &= ~ZARU_EXP_SOUND_MASK;
if (sound & ZARU_SOUND_R)
Register2800 |= ZARU_EXP_SOUND_R;
if (sound & ZARU_SOUND_L)
Register2800 |= ZARU_EXP_SOUND_L;
ZaruWriteReg(REG_2800, Register2800);
ZaruRestartBoard2();
}
}
/*
* Control the Read/Write byte count monitoring of AD16 bus.
*
* Changes bit D3 in REG_2800.
*/
void
ZaruSetADBusCheck(u8 value, int doReset)
{
if (zaruexists) {
Register2800 &= ~ZARU_EXP_RDWR_ENABLE;
if (value == ZARU_ON) {
Register2800 |= ZARU_EXP_RDWR_ENABLE;
}
ZaruWriteReg(REG_2800, Register2800);
if (doReset) {
ZaruRestartBoard2();
}
}
}
/*
* Get the value of the DIP switch
*/
u8
ZaruGetDIPSwitch()
{
if (zaruexists) {
ZaruSetExpCommand(ZARU_EXP_CMD_DIP);
return ZaruGetExpHigh();
} else {
return 0;
}
}
/*
* Get the value of the INT1 or CART Interrupt signal
* on bottom connector. Is generated from top cartridge.
* Note that the interrupts to the R4300 are active low.
* XXX I don't know what the Zaru does with the bit, so check
* this code.
*/
int
ZaruGetInt1()
{
if (zaruexists) {
ZaruSetExpCommand(ZARU_EXP_CMD_INT1);
if (ZaruGetExpHigh() & ZARU_INT1) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
}
/*
* Get the value of the reset time.
*/
int
ZaruGetResetTime()
{
int resettime = 0;
if (zaruexists) {
ZaruSetExpCommand(ZARU_EXP_CMD_RESET_TIME);
resettime = (ZaruGetExpHigh() << 8) | ZaruGetExpLow();;
/*
* resettime is in units of 256 microseconds; convert to milliseconds.
*/
resettime *= 256; /* convert to microseconds first */
resettime /= 1000; /* then convert to milliseconds */
}
return resettime;
}
/*
* Get the power check bits from Register 0x1800
*/
u8
ZaruGetPowerCheck()
{
if (zaruexists) {
return ZaruReadReg(REG_1800);
} else {
return 0;
}
}
/*
* Get the power check bits from Register 0x2000
*/
u8
ZaruGetMisc()
{
if (zaruexists) {
return ZaruReadReg(REG_2000);
} else {
return 0;
}
}
/*
* Get the expansion high bits from 0x2800.
* Value depends on command programmed with ZaruSetExpCommand()
*/
u8
ZaruGetExpHigh()
{
if (zaruexists) {
return ZaruReadReg(REG_2800);
} else {
return 0;
}
}
/*
* Get the expansion high bits from 0x3000.
* Value depends on command programmed with ZaruSetExpCommand()
*/
u8
ZaruGetExpLow()
{
if (zaruexists) {
return ZaruReadReg(REG_3000);
} else {
return 0;
}
}
/*
* Return state of Next test button. This is the Yellow button on the Zaru,
* or the A button on the controller.
*
* return 1 if button is pushed, 0 if not
*/
int
ZaruGetNextTestButton()
{
if (zaruexists) {
if (ZaruGetMisc() & ZARU_MISC_SWITCH)
return 0;
else
return 1;
} else {
if (readControllerButton(controllerSlot) & CONT_A)
return 1;
else
return 0;
}
}
/*
* Block until next test button is pushed.
*/
void
ZaruWaitForNextTestButton()
{
playSubway();
ZaruWriteReg2(REG_WAIT,ZARU_WAIT); /* sasano added */
while (1) {
if ( ZaruGetNextTestButton() == 1 ) {
(void)osRecvMesg(&retraceMessageQ, NULL, OS_MESG_BLOCK);
while (ZaruGetNextTestButton() == 1) {
(void)osRecvMesg(&retraceMessageQ, NULL, OS_MESG_BLOCK);
}
ZaruWriteReg2(REG_WAIT,ZARU_ZERO); /* sasano added */
return;
}
(void)osRecvMesg(&retraceMessageQ, NULL, OS_MESG_BLOCK);
}
}
/*
* Check that the switch settings for the joy buttons match what is
* read from the controller
*/
int
ZaruButtonCheck(u8 zaruswitch, u16 buttons)
{
if (zaruswitch & ZARU_UP_SW) {
buttons ^= U_CBUTTONS;
}
if (zaruswitch & ZARU_DOWN_SW) {
buttons ^= D_CBUTTONS;
}
if (zaruswitch & ZARU_LEFT_SW) {
buttons ^= L_CBUTTONS;
}
if (zaruswitch & ZARU_RIGHT_SW) {
buttons ^= R_CBUTTONS;
}
if (zaruswitch & ZARU_A_SW) {
buttons ^= 0x0080; /* no define for JSRES */
}
if (zaruswitch & ZARU_B_SW) {
buttons ^= 0x0040; /* no define for UNUSED */
}
if (zaruswitch & ZARU_C_SW) {
buttons ^= L_JPAD;
}
if (zaruswitch & ZARU_D_SW) {
buttons ^= D_JPAD;
}
if (buttons != 0) {
return GNG_TEST_FAILURE;
} else {
return GNG_TEST_SUCCESS;
}
}