rdram.c
7.06 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
/*
* These routines mmap two files that contain the contents of the
* Verilog rdram core. After initializing, the copy & write routines can
* be called. The read routine is used to copy the contents of the file
* to the internal rdram. The rdram write task can call the write routine
* to update the mmap file at the same time the internal core is being
* written. Unfortunately, there is no way for writes to the mmap file
* to automatically update the internal core. (have to copy entire image)
*
* The system (RDP, R4K, ...) views the memory in BE mode. However, the
* rdram is LE internally. The mmap files will show the memory in BE
* format. This is to alleviate or create confusion. (Take your pick)
*
* A "word" is 64/72 bits
*
* Use strings to pass ascii representations of 72 bit words back & forth
*
*/
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include "verify.h"
#include "acc_user.h"
#include "vcsuser.h"
/* XXX Need to put somewhere else 2 Meg system */
#define MAIN_MEMORY_SIZE 0x200000
#define HIDDEN_FACTOR 8
#define HIDDEN_MEMORY_SIZE (MAIN_MEMORY_SIZE/HIDDEN_FACTOR)
#define TOTAL_MEMORY_SIZE (MAIN_MEMORY_SIZE + HIDDEN_MEMORY_SIZE)
/*
#define DEBUG1(s, a0) if (!data) fprintf(stderr, s, a0)
#define DEBUG2(s, a0, a1) if (!data) fprintf(stderr, s, a0, a1)
*/
#define DEBUG1(s, a0)
#define DEBUG2(s, a0, a1)
static s_setval_value value_s = { accBinStrVal };
static s_setval_delay delay_s = { { accRealTime, 0, 0, 0 }, accNoDelay };
/* Global file pointers (Should be passed in as arguments, but it's a hassle) */
static int rdfd = 0;
static unsigned char *mainaddr = 0, *hiddenaddr = 0;
static int tot_bytes = 0;
static void
syncfile(int sig)
{
msync(mainaddr, tot_bytes, MS_SYNC);
fprintf(stderr, "msync mmap file completed\n");
}
/*
* Usage: $rdram_mmap_init(filename, nrdrams)
*/
void
rdram_mmap_init(int data, int reason)
{
struct stat statbuf;
char *file;
char filename[1024];
int memsize;
long nbytes;
VerifyInfo *vp;
acc_initialize();
acc_configure(accDevelopmentVersion, "1.6");
file = tf_getcstringp(1);
memsize = tf_getp(2);
fprintf(stderr, "rdram_mmap_init: file is %s, size %d\n", file, memsize);
if (mainaddr != NULL) {
fprintf(stderr, "already initialized mainaddr is 0x%x\n", mainaddr);
tf_putp(0, 0);
return;
}
if (memsize != 1 && memsize != 2 && memsize != 4) {
fprintf(stderr, "memsize not 1, 2 or 4!! (%d)\n", memsize);
tf_putp(0, -1);
return;
}
nbytes = TOTAL_MEMORY_SIZE * memsize;
/* Open files, and then mmap them in */
sprintf(filename, "%s.rdram", file);
if ((rdfd = open(filename, (O_RDWR | O_CREAT), 0644)) == -1) {
fprintf(stderr, "Could not open <%s>\n", filename);
tf_putp(0, -1);
return;
}
if (fstat(rdfd, &statbuf) == -1) {
fprintf(stderr, "Could not stat <%s>\n", filename);
tf_putp(0, -1);
return;
}
mainaddr = mmap(NULL, nbytes, (PROT_READ | PROT_WRITE),
(MAP_SHARED | MAP_AUTOGROW), rdfd, 0);
hiddenaddr = mainaddr + (MAIN_MEMORY_SIZE * memsize);
fprintf(stderr, "rdram_mmap_init: mainaddr is 0x%x, hiddenaddr is 0x%x\n",
mainaddr, hiddenaddr);
if (mainaddr == (unsigned char *)-1) {
fprintf(stderr, "Could not mmap <%s>\n", filename);
tf_putp(0, -1);
return;
}
/* File created of resized? print warning and autogrow */
if (statbuf.st_size != nbytes) {
fprintf(stderr, "Warning! %s grew, was 0x%x now 0x%x\n",
filename, statbuf.st_size, nbytes);
*(mainaddr + nbytes - 1) = 0;
/*
* need to copy hidden bits up if expanding from 2.25 to 4.5 or 9 Meg
*/
if (statbuf.st_size == TOTAL_MEMORY_SIZE &&
nbytes == memsize * TOTAL_MEMORY_SIZE) {
bcopy(mainaddr + MAIN_MEMORY_SIZE, hiddenaddr, HIDDEN_MEMORY_SIZE);
bzero(mainaddr + MAIN_MEMORY_SIZE, HIDDEN_MEMORY_SIZE);
}
}
vp = (VerifyInfo *)(mainaddr + VERIFY_INFO_PHYSADDR);
if (vp->magicNumber != MAGICNUMBER) {
fprintf(stderr, "Warning! VerifyInfo magic number is 0x%x, not 0x%x\n",
vp->magicNumber, MAGICNUMBER);
}
tot_bytes = nbytes;
sigset(SIGUSR1, syncfile);
tf_putp(0, 0);
}
/*
* Usage: $rdram_mmap_init_check(id)
*/
void
rdram_mmap_init_check(int data, int reason)
{
int memid;
memid = tf_getp(1);
fprintf(stderr, "rdram_mmap_init_check: id %d ", memid);
if (mainaddr != NULL) {
fprintf(stderr, "already initialized\n");
tf_putp(0, 0);
} else {
fprintf(stderr, "NOT initialized!!\n");
tf_putp(0, -1);
}
return;
}
/*
* Usage: $rdram_mmap_read(memory_id, offset, core word)
* Read from file
*/
void
rdram_mmap_read(int data, int reason)
{
handle hcore_word;
int memid, offset, i, j;
unsigned char hiddenbyte, mainbyte, *mp;
char binstr[73], *cp;
if(tf_nump() != 3) {
tf_error("rdram_mmap_read illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
memid = acc_fetch_tfarg_int(1);
offset = acc_fetch_tfarg_int(2);
hcore_word = acc_handle_tfarg(3);
DEBUG2("\nread: id %d, offset is 0x%x\n", memid, offset);
hiddenbyte = *(hiddenaddr + (HIDDEN_MEMORY_SIZE * memid) + offset);
DEBUG2("hidden: %d-->0x%x\n", hiddenaddr + offset, hiddenbyte);
mp = mainaddr + (MAIN_MEMORY_SIZE * memid) + (offset*8) + 7;
cp = binstr;
for (i=0; i<8; i++) {
if (hiddenbyte & 0x01)
*cp++ = '1';
else
*cp++ = '0';
mainbyte = *mp--;
DEBUG2("%d-->0x%x, ", mp+1, mainbyte);
for (j=0; j<8; j++) {
if (mainbyte & 0x80)
*cp++ = '1';
else
*cp++ = '0';
mainbyte <<= 1;
}
hiddenbyte >>= 1;
}
*cp = '\0';
DEBUG1("\nstr: [%s]\n", binstr);
DEBUG1("strlen is %d\n", strlen(binstr));
value_s.value.str = binstr;
acc_set_value(hcore_word, &value_s, &delay_s);
tf_putp(0, 0);
}
/*
* Usage: $rdram_mmap_write(memory_id, offset, data)
* Slame the data in,
*/
void
rdram_mmap_write(int data, int reason)
{
handle hcore_word;
char *val;
int memid, offset, i, j;
unsigned char hiddenbyte, mainbyte, *mp;
char *cp;
if(tf_nump() != 3) {
tf_error("rdram_mmap_read illegal number of arguments: %d", tf_nump());
tf_putp(0, -1);
}
memid = acc_fetch_tfarg_int(1);
offset = acc_fetch_tfarg_int(2);
hcore_word = acc_handle_tfarg(3);
DEBUG2("\nWrite: id %d, offset is 0x%x\n", memid, offset);
val = acc_fetch_value(hcore_word, "%b");
DEBUG1("str: [%s]\n", val);
DEBUG1("strlen is %d\n", strlen(val));
hiddenbyte = 0;
mp = mainaddr + (MAIN_MEMORY_SIZE * memid) + (offset*8) + 7;
for (i=0; i<8; i++) {
hiddenbyte >>= 1;
if (*val++ == '1')
hiddenbyte |= 0x80;
mainbyte = 0;
for (j=0; j<8; j++) {
mainbyte <<= 1;
if (*val++ == '1')
mainbyte |= 1;
}
DEBUG2("%d<--0x%x, ", mp, mainbyte);
*mp-- = mainbyte;
}
DEBUG2("\nhidden: %d<--0x%x\n", hiddenaddr + offset, hiddenbyte);
*(hiddenaddr + (HIDDEN_MEMORY_SIZE * memid) + offset) = hiddenbyte;
tf_putp(0, 0);
}