This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
memory.c
176 lines (156 loc) · 3.94 KB
/
memory.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
/*
Armator - simulateur de jeu d'instruction ARMv5T � but p�dagogique
Copyright (C) 2011 Guillaume Huard
Ce programme est libre, vous pouvez le redistribuer et/ou le modifier selon les
termes de la Licence Publique G�n�rale GNU publi�e par la Free Software
Foundation (version 2 ou bien toute autre version ult�rieure choisie par vous).
Ce programme est distribu� car potentiellement utile, mais SANS AUCUNE
GARANTIE, ni explicite ni implicite, y compris les garanties de
commercialisation ou d'adaptation dans un but sp�cifique. Reportez-vous � la
Licence Publique G�n�rale GNU pour plus de d�tails.
Vous devez avoir re�u une copie de la Licence Publique G�n�rale GNU en m�me
temps que ce programme ; si ce n'est pas le cas, �crivez � la Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
�tats-Unis.
Contact: [email protected]
B�timent IMAG
700 avenue centrale, domaine universitaire
38401 Saint Martin d'H�res
*/
#include <stdlib.h>
#include "memory.h"
#include "util.h"
struct memory_data
{
size_t size;
uint8_t *data;
};
memory memory_create(size_t size)
{
memory mem = (memory)malloc(sizeof(struct memory_data));
if (mem == NULL)
{
return NULL;
}
mem->size = size;
mem->data = (uint8_t *)malloc(size);
if (mem->data == NULL)
{
free(mem);
return NULL;
}
return mem;
}
size_t memory_get_size(memory mem)
{
if (mem != NULL)
{
return mem->size;
}
else
{
return 0;
}
}
void memory_destroy(memory mem)
{
if (mem == NULL)
{
return;
}
else
{
free(mem->data);
free(mem);
}
}
int memory_read_byte(memory mem, uint32_t address, uint8_t *value)
{
if (mem == NULL || address >= mem->size)
{
return -1;
}
*value = mem->data[address];
return 0;
}
int memory_read_half(memory mem, uint32_t address, uint16_t *value, uint8_t be)
{
if (mem == NULL || address >= mem->size)
{
return -1;
}
if (be)
{
*value = (mem->data[address] << 8) | mem->data[address + 1];
}
else
{
*value = (mem->data[address + 1] << 8) | mem->data[address];
}
return 0;
}
int memory_read_word(memory mem, uint32_t address, uint32_t *value, uint8_t be)
{
if (mem == NULL || address >= mem->size)
{
return -1;
}
if (be)
{
*value = mem->data[address] << 24 | mem->data[address + 1] << 16 | mem->data[address + 2] << 8 | mem->data[address + 3];
}
else
{
*value = mem->data[address + 3] << 24 | mem->data[address + 2] << 16 | mem->data[address + 1] << 8 | mem->data[address];
}
return 0;
}
int memory_write_byte(memory mem, uint32_t address, uint8_t value)
{
if (mem == NULL || address >= mem->size)
{
return -1;
}
mem->data[address] = value;
return 0;
}
int memory_write_half(memory mem, uint32_t address, uint16_t value, uint8_t be)
{
if (mem == NULL || address >= mem->size)
{
return -1;
}
if (be)
{
mem->data[address] = (uint8_t)(value >> 8);
mem->data[address + 1] = (uint8_t)value;
}
else
{
mem->data[address + 1] = (uint8_t)(value >> 8);
mem->data[address] = (uint8_t)value;
}
return 0;
}
int memory_write_word(memory mem, uint32_t address, uint32_t value, uint8_t be)
{
if (mem == NULL || address >= mem->size)
{
return -1;
}
if (be)
{
mem->data[address] = (uint8_t)(value >> 24);
mem->data[address + 1] = (uint8_t)(value >> 16);
mem->data[address + 2] = (uint8_t)(value >> 8);
mem->data[address + 3] = (uint8_t)value;
}
else
{
mem->data[address + 3] = (uint8_t)(value >> 24);
mem->data[address + 2] = (uint8_t)(value >> 16);
mem->data[address + 1] = (uint8_t)(value >> 8);
mem->data[address] = (uint8_t)value;
}
return 0;
}