-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRISCVmem.c
255 lines (207 loc) · 6.36 KB
/
RISCVmem.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
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
/* RISC-V memory access code file: */
#include "RISCVmem.h"
int32_t mem[MEM_SIZE];
/* Functions: */
void dump_mem(uint32_t addr, uint32_t wsize){
// Verify if addr is a 4 multiple, if not, round to the 4 multiple before addr
while( addr % 4 != 0){
addr--;
}
// Verify if addr exceeds memory size, if it does, return
if(addr >= MEM_SIZE){
printf("ERROR: Address surprass memory size\n");
return;
}
// Dump memory
for(uint32_t i = wsize; i != 0; i--){
printf("mem[%u] = 0x%08X\n", addr/4, mem[addr/4]);
addr += 4;
}
}
int32_t lw(uint32_t address, int32_t kte){
// Add kte with address
int32_t addr = (int32_t)address + kte;
address = (uint32_t)addr;
// Verify if address is aligned with memory
if( addr % 4 != 0){
printf("ERROR: Load address not aligned to word boundary");
exit(1);
}
// Get word on memory and return
int32_t ret = mem[address/4];
return ret;
}
int32_t lh(uint32_t address, int32_t kte){
// Add kte with address
int32_t addr = (int32_t)address + kte;
address = (uint32_t)addr;
// Verify if address is aligned with memory
if( addr % 2 != 0){
printf("ERROR: Load address not aligned to halfword boundary");
exit(1);
}
// Get word address where halfword is
uint32_t word_addr = address;
if( word_addr % 4 != 0){
word_addr -= 2;
}
// Get halfword on memory and return
int32_t ret = mem[word_addr/4];
// Case when halfword if on the least significant halfword
if(word_addr == address){
ret = ret << 16;
}
ret = ret >> 16;
return ret;
}
int32_t lhu(uint32_t address, int32_t kte){
// Add kte with address
int32_t addr = (int32_t)address + kte;
address = (uint32_t)addr;
// Verify if address is aligned with memory
if( addr % 2 != 0){
printf("ERROR: Load address not aligned to halfword boundary");
exit(1);
}
// Get word address where halfword is
uint32_t word_addr = address;
if( word_addr % 4 != 0){
word_addr -= 2;
}
// Get halfword on memory and return
uint32_t ret = (uint32_t)mem[word_addr/4];
// Case when halfword if on the least significant halfword
if(word_addr == address){
ret = ret << 16;
ret = ret >> 16;
}
// Case when halfword if on the most significant halfword
else{
ret = ret >> 16;
}
return (int32_t)ret;
}
int32_t lb(uint32_t address, int32_t kte){
// Add kte with address
int32_t addr = (int32_t)address + kte;
address = (uint32_t)addr;
// Get word address where byte is
uint32_t word_addr = address;
while( word_addr % 4 != 0){
word_addr--;
}
// Get byte on memory and return:
int32_t ret = mem[word_addr/4];
// Get difference between memory address and byte address
int32_t position = address - word_addr;
// Shift ret to the left acording to position
ret = ret << 8*(3-position);
// Shift right to take byte to the least significant position
ret = ret >> 24;
return ret;
}
int32_t lbu(uint32_t address, int32_t kte){
// Add kte with address
int32_t addr = (int32_t)address + kte;
address = (uint32_t)addr;
// Get word address where byte is
uint32_t word_addr = address;
while( word_addr % 4 != 0){
word_addr--;
}
// Get byte on memory and return:
uint32_t ret = (uint32_t)mem[word_addr/4];
// Get difference between memory address and byte address
int32_t position = address - word_addr;
// Shift ret to the left acording to position
ret = ret << 8*(3-position);
// Shift right to take byte to the least significant position
ret = ret >> 24;
return (int32_t)ret;
}
void sw(uint32_t address, int32_t kte, int32_t dado){
// Add kte with address
int32_t addr = (int32_t)address + kte;
address = (uint32_t)addr;
// Verify if address is aligned with memory
if( addr % 4 != 0){
printf("ERROR: Store address not aligned to word boundary");
exit(1);
}
// Get word on memory and return
mem[address/4] = dado;
}
void sh(uint32_t address, int32_t kte, int32_t dado){
// Add kte with address
int32_t addr = (int32_t)address + kte;
address = (uint32_t)addr;
// Verify if address is aligned with memory
if( addr % 2 != 0){
printf("ERROR: Store address not aligned to halfword boundary");
exit(1);
}
// Get word address where halfword is
uint32_t word_addr = address;
if( word_addr % 4 != 0){
word_addr -= 2;
}
// Read memory content
int32_t mem_data = mem[word_addr/4];
// Get difference between memory address and halfword address
int32_t position = address - word_addr;
// Create mask with what needs to continue on memory
int32_t mask;
switch(position){
case 0:
mask = 0xFFFF0000;
break;
case 2:
mask = 0x0000FFFF;
break;
}
// Shift data acording to position
dado = dado << 8*position;
// Use mask to clear area where data will go on mem_data
mem_data &= mask;
// Use negated mask to clear area on data that cant me modified on mem_data
dado &= ~(mask);
// Store
mem[word_addr/4] = mem_data + dado;
}
void sb(uint32_t address, int32_t kte, int32_t dado){
// Add kte with address
int32_t addr = (int32_t)address + kte;
address = (uint32_t)addr;
// Get word address where halfword is
uint32_t word_addr = address;
while( word_addr % 4 != 0){
word_addr--;
}
// Read memory content
int32_t mem_data = mem[word_addr/4];
// Get difference between memory address and byte address
int32_t position = address - word_addr;
// Create mask with what needs to continue on memory
int32_t mask;
switch(position){
case 0:
mask = 0xFFFFFF00;
break;
case 1:
mask = 0xFFFF00FF;
break;
case 2:
mask = 0xFF00FFFF;
break;
case 3:
mask = 0x00FFFFFF;
}
// Shift data acording to position
dado = dado << 8*position;
// Use mask to clear area where data will go on mem_data
mem_data &= mask;
// Use negated mask to clear area on data that cant me modified on mem_data
dado &= ~(mask);
// Store
mem[word_addr/4] = mem_data + dado;
}