loadrom.c
3.94 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
/*
* Copyright 1995, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*
* $Revision: 1.1.1.1 $
*/
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <bstring.h>
#include <string.h>
#include <getopt.h>
#include <sys/u64gio.h>
#include <libaudio.h>
#include "midiApp.h"
#include "midiDmon.h"
#include "midiGlobals.h"
int writeNverify(int u64fd, unsigned int ramromOffset, int bufSize, unsigned char *buf);
extern int gRomImageSize;
extern char gRomImage[];
void loadrom(void)
{
int retval;
unsigned int romOffset = 0; /* rom will internally offset game to 0x800 */
int errorsFound = 0;
int u64fd;
if ((u64fd = open(DEV_U64, O_RDWR | O_NOCTTY)) < 0)
{
fprintf(stderr, "%s: unable to open %s",gAppName, DEV_U64);
exit(1);
}
/* Assert RESET */
if ( (retval = ioctl(u64fd, U64_RESET, 1) ) < 0 )
{
fprintf(stderr, "%s: Error starting U64_RESET ioctl",gAppName);
exit(1);
}
/* Write the contents of the ROM file into ramrom first. */
if (writeNverify(u64fd, romOffset, gRomImageSize, gRomImage) > 0)
{
fprintf(stderr, "%s: write & readback of rom image failed.\n",gAppName);
exit(1);
}
/* Release RESET; game will subsequently boot itself. */
if ( (retval = ioctl(u64fd, U64_RESET, 0) ) < 0 )
{
fprintf(stderr, "%s: Error after U64_RESET ioctl",gAppName);
exit(1);
}
close(u64fd);
}
/*
* Returns number of errors encountered.
* Can be greatly shortened by not verifying
*/
int writeNverify(int u64fd, unsigned int ramromOffset, int bufSize, unsigned char *buf)
{
unsigned char *readBuf, *pSrc, *pDst;
u64_write_arg_t arg;
int retval, i, error;
if ((ramromOffset + bufSize) > RAMROM_SIZE)
{
fprintf(stderr, "%s: Ramrom image too big! %d bytes\n",gAppName, bufSize);
return(1);
}
/* Tell the kernel to copyin our data into ramrom. */
arg.buffer = (void *)buf;
arg.ramrom_addr = ramromOffset;
arg.nbytes = bufSize;
arg.value = -1; /* Inhibits acknowledge cycle with game */
if ( (retval = ioctl(u64fd, U64_WRITE, &arg) ) < 0 )
{
fprintf(stderr, "%s: Error after U64_WRITE ioctl, return value %d",gAppName,retval);
return(1);
}
/* Read back & verify, to ensure our load worked. Not needed, just protective */
readBuf = malloc(bufSize);
if (readBuf == NULL)
{
fprintf(stderr, "%s: unable to malloc buffer to hold %d bytes\n",gAppName,bufSize);
return(1);
}
/* Tell the kernel to copyout ramrom data into our buffer. */
arg.buffer = (void *)readBuf;
arg.ramrom_addr = ramromOffset;
arg.nbytes = bufSize;
arg.value = -1; /* Inhibits acknowledge cycle with game */
if ( (retval = ioctl(u64fd, U64_READ, &arg) ) < 0 )
{
fprintf(stderr, "%s: Error after U64_READ ioctl, return value %d",gAppName,retval);
return(1);
}
/* check that what you read is what you wrote */
for (i = 0, error = 0, pSrc = buf, pDst = readBuf; i < bufSize; i++)
{
if (*pSrc != *pDst)
error++;
pSrc++;
pDst++;
}
return(error);
}