This repository was archived by the owner on Apr 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemory.c
More file actions
115 lines (102 loc) · 3.39 KB
/
Copy pathmemory.c
File metadata and controls
115 lines (102 loc) · 3.39 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
/*
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: Guillaume.Huard@imag.fr
B�timent IMAG
700 avenue centrale, domaine universitaire
38401 Saint Martin d'H�res
*/
#include <stdlib.h>
#include "memory.h"
#include "util.h"
#include "arm_constants.h"
struct memory_data {
size_t size;
uint8_t *data;
};
memory memory_create(size_t size) {
memory mem = malloc(sizeof(struct memory_data));
mem->size = size;
mem->data = calloc(size,sizeof(uint8_t));
if(mem->data == NULL){
free(mem);
return NULL;
}
return mem;
}
size_t memory_get_size(memory mem) {
return mem->size;
}
void memory_destroy(memory mem) {
free(mem->data);
free(mem);
}
int memory_read_byte(memory mem, uint32_t address, uint8_t *value) {
if(mem != NULL && address < mem->size){
*value = mem->data[address];
return 0;
}
return DATA_ABORT;
}
int memory_read_half(memory mem, uint32_t address, uint16_t *value, uint8_t be) {
if(mem != NULL && address < mem->size - 1){
*value = (uint16_t)mem->data[address] | (uint16_t)mem->data[address + 1] << 8;
if (be) {
*value = reverse_2(*value);
}
return 0;
}
return DATA_ABORT;
}
int memory_read_word(memory mem, uint32_t address, uint32_t *value, uint8_t be) {
if(mem != NULL && address < mem->size - 3){
*value = *(mem->data + address) | *(mem->data + address + 1) << 8 | *(mem->data + address + 2) << 16 | *(mem->data + address + 3) << 24;
if (be) {
*value = reverse_4(*value);
}
return 0;
}
return DATA_ABORT;
}
int memory_write_byte(memory mem, uint32_t address, uint8_t value) {
if(mem != NULL && address < mem->size){
*(mem->data + address) = value;
return 0;
}
return DATA_ABORT;
}
int memory_write_half(memory mem, uint32_t address, uint16_t value, uint8_t be) {
if(mem != NULL && address < mem->size - 1){
if (be) {
value = reverse_2(value);
}
mem->data[address] = (uint8_t)value;
mem->data[address+1] = (uint8_t)(value >> 8);
return 0;
}
return DATA_ABORT;
}
int memory_write_word(memory mem, uint32_t address, uint32_t value, uint8_t be) {
if(mem != NULL && address < mem->size - 3){
if (be) {
value = reverse_4(value);
}
mem->data[address] = (uint8_t)value;
mem->data[address+1] = (uint8_t)(value >> 8);
mem->data[address+2] = (uint8_t)(value >> 16);
mem->data[address+3] = (uint8_t)(value >> 24);
return 0;
}
return DATA_ABORT;
}