-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemoria.cpp
More file actions
139 lines (123 loc) · 3.22 KB
/
memoria.cpp
File metadata and controls
139 lines (123 loc) · 3.22 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
#include <stdio.h>
#include <stdint.h>
#define MEM_SIZE 4096
int32_t mem[MEM_SIZE];
//Prototipos das funções a serem desenvolvidas
int32_t lw(uint32_t, int32_t);
int32_t lb(uint32_t, int32_t);
int32_t lbu(uint32_t, int32_t);
void sw(uint32_t, int32_t, int32_t);
void sb(uint32_t,int32_t, int8_t);
int32_t lw(uint32_t address, int32_t kte){
uint32_t address_mem = address + kte;
if(address_mem % 4 != 0){
printf("O endereço não é um múltiplo de 4.\n");
return 0;
}
address_mem /= 4;
return mem[address_mem];
}
int32_t lb(uint32_t address, int32_t kte){
if(address %4 != 0){
printf("O endereço precisa ser um múltiplo de 4.\n");
return 0;
}
uint32_t address_aux = address/4;
int32_t aux = mem[address_aux];
switch(kte){
case 0:
aux &= 0xFF;
break;
case 1:
aux &= 0xFF00;
break;
case 2:
aux &= 0xFF0000;
break;
case 3:
aux &= 0xFF000000;
break;
default:
if(kte > 3){
aux = lb(address + 4, (kte-4));
return aux;
}
else{return 0;}
break;
}
aux = aux >> 8*kte;
if(aux > 0x7F) aux |= 0xffffff00;
return aux;
}
int32_t lbu(uint32_t address, int32_t kte){
if(address%4 != 0){
printf("O endereço precisa ser um múltiplo de 4.\n");
return 0;
}
uint32_t address_aux = address/4;
int32_t aux = mem[address_aux];
switch(kte){
case 0:
aux &= 0xFF;
break;
case 1:
aux &= 0xFF00;
break;
case 2:
aux &= 0xFF0000;
break;
case 3:
aux &= 0xFF000000;
break;
default:
if(kte > 3){
aux = lbu(address + 4, (kte-4));
return aux;
}
else{return 0;}
break;
}
aux = aux >> 8*kte;
return aux &= 0xFF;
}
void sw(uint32_t address, int32_t kte, int32_t data){
int32_t address_mem = address + kte;
if(address_mem % 4 != 0){
printf("O endereço precisa ser um múltiplo de 4.\n");
}else
{
address_mem /= 4;
mem[address_mem] = data;
}
}
void sb(uint32_t address, int32_t kte, int8_t data){
if(address%4 != 0){
printf("O endereço precisa ser um múltiplo de 4.\n");
}
else{
if(kte <= 3){
address = address/4;
int32_t temp = data;
temp = temp&0xFF;
temp = temp << kte*8;
switch(kte){
case 0:
mem[address] &= 0xFFFFFF00;
break;
case 1:
mem[address] &= 0xFFFF00FF;
break;
case 2:
mem[address] &= 0xFF00FFFF;
break;
case 3:
mem[address] &= 0x00FFFFFF;
break;
}
mem[address] = mem[address] | temp;
}
else{
sb((address + 4), (kte-4), data);
}
}
}