-
Notifications
You must be signed in to change notification settings - Fork 102
/
sim_bram_helpers_c.c
179 lines (145 loc) · 3.73 KB
/
sim_bram_helpers_c.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
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "sim_vhpi_c.h"
#undef DEBUG
#define ALIGN_UP(VAL, SIZE) (((VAL) + ((SIZE)-1)) & ~((SIZE)-1))
#define MAX_REGIONS 128
struct ram_behavioural {
char *filename;
unsigned long size;
void *m;
};
static struct ram_behavioural behavioural_regions[MAX_REGIONS];
static unsigned long region_nr;
unsigned long behavioural_initialize(void *__f, unsigned long size)
{
struct ram_behavioural *r;
int fd;
struct stat buf;
unsigned long tmp_size;
void *mem;
if (region_nr == MAX_REGIONS) {
fprintf(stderr, "%s: too many regions, bump MAX_REGIONS\n", __func__);
exit(1);
}
r = &behavioural_regions[region_nr];
r->filename = from_string(__f);
r->size = ALIGN_UP(size, getpagesize());
fd = open(r->filename, O_RDWR);
if (fd == -1) {
fprintf(stderr, "%s: could not open %s\n", __func__,
r->filename);
exit(1);
}
if (fstat(fd, &buf)) {
perror("fstat");
exit(1);
}
/* XXX Do we need to truncate the underlying file? */
tmp_size = ALIGN_UP(buf.st_size, getpagesize());
if (r->size > tmp_size) {
void *m;
/*
* We have to pad the file. Allocate the total size, then
* create a space for the file.
*/
mem = mmap(NULL, r->size, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (mem == MAP_FAILED) {
perror("mmap");
exit(1);
}
if (tmp_size) {
munmap(mem, tmp_size);
m = mmap(mem, tmp_size, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED, fd, 0);
if (m == MAP_FAILED) {
perror("mmap");
exit(1);
}
if (m != mem) {
fprintf(stderr, "%s: mmap(MAP_FIXED) failed\n",
__func__);
exit(1);
}
}
} else {
mem = mmap(NULL, tmp_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
fd, 0);
if (mem == MAP_FAILED) {
perror("mmap");
exit(1);
}
}
behavioural_regions[region_nr].m = mem;
return region_nr++;
}
void behavioural_read(unsigned char *__val, unsigned char *__addr,
unsigned long sel, int identifier)
{
struct ram_behavioural *r;
unsigned long val = 0;
unsigned long addr = from_std_logic_vector(__addr, 64);
unsigned char *p;
if (identifier > region_nr) {
fprintf(stderr, "%s: bad index %d\n", __func__, identifier);
exit(1);
}
r = &behavioural_regions[identifier];
for (unsigned long i = 0; i < 8; i++) {
#if 0
/* sel only used on writes */
if (!(sel & (1UL << i)))
continue;
#endif
if ((addr + i) > r->size) {
fprintf(stderr, "%s: bad memory access %lx %lx\n", __func__,
addr+i, r->size);
exit(1);
}
p = (unsigned char *)(((unsigned long)r->m) + addr + i);
val |= (((unsigned long)*p) << (i*8));
}
#ifdef DEBUG
printf("MEM behave %d read %016lx addr %016lx sel %02lx\n", identifier, val,
addr, sel);
#endif
to_std_logic_vector(val, __val, 64);
}
void behavioural_write(unsigned char *__val, unsigned char *__addr,
unsigned int sel, int identifier)
{
struct ram_behavioural *r;
unsigned long val = from_std_logic_vector(__val, 64);
unsigned long addr = from_std_logic_vector(__addr, 64);
unsigned char *p;
if (identifier > region_nr) {
fprintf(stderr, "%s: bad index %d\n", __func__, identifier);
exit(1);
}
r = &behavioural_regions[identifier];
p = (unsigned char *)(((unsigned long)r->m) + addr);
#ifdef DEBUG
printf("MEM behave %d write %016lx addr %016lx sel %02x\n", identifier, val,
addr, sel);
#endif
for (unsigned long i = 0; i < 8; i++) {
if (!(sel & (1UL << i)))
continue;
if ((addr + i) > r->size) {
fprintf(stderr, "%s: bad memory access %lx %lx\n", __func__,
addr+i, r->size);
exit(1);
}
p = (unsigned char *)(((unsigned long)r->m) + addr + i);
*p = (val >> (i*8)) & 0xff;
}
}