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
/
Copy pathmemory_test.c
180 lines (159 loc) · 5.98 KB
/
memory_test.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
/*
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 <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "memory.h"
#include "util.h"
int FAIL = 0;
void assert_print(int result)
{
if (result)
{
printf("\033[32mTEST PASSED\033[0m\n");
}
else
{
printf("\033[31mTEST FAILED\033[0m\n");
FAIL = 1;
}
}
int compare(void *a, void *b, size_t size, int reverse)
{
int i, j, j_step;
if (reverse)
{
j = size - 1;
j_step = -1;
}
else
{
j = 0;
j_step = 1;
}
for (i = 0; i < size; i++, j += j_step)
if (*((uint8_t *)a + i) != *((uint8_t *)b + j))
return 0;
return 1;
}
int compare_with_sim(void *a, memory m, size_t size, int reverse)
{
int i, j, j_step;
uint8_t value;
if (reverse)
{
j = size - 1;
j_step = -1;
}
else
{
j = 0;
j_step = 1;
}
for (i = 0; i < size; i++, j += j_step)
{
memory_read_byte(m, j, &value);
if (*((uint8_t *)a + i) != value)
return 0;
}
return 1;
}
int main()
{
char *endianess[] = {"little", "big"};
memory m;
uint32_t word_value = 0x11223344, word_read;
uint16_t half_value = 0x5566, half_read;
uint8_t *position;
int i, my_endianess;
m = memory_create(4);
if (m == NULL)
{
fprintf(stderr, "Error when creating simulated memory\n");
exit(1);
}
assert(memory_get_size(m) == 4);
my_endianess = is_big_endian();
printf("I'm a %s endian host\n", endianess[my_endianess]);
// Test reading from the simulated memory
printf("Writing 4 bytes at address 0, then reading the word and half, "
"the result should depend on endianess of the access to the simulated memory :\n");
position = (uint8_t *)&word_value;
for (i = 0; i < 4; i++)
{
memory_write_byte(m, i, *(position + i));
}
printf("- word read with the same endianess as me, ");
memory_read_word(m, 0, &word_read, my_endianess);
assert_print(compare(&word_value, &word_read, 4, 0));
printf("- half read with the same endianess as me, ");
memory_read_half(m, 0, &half_read, my_endianess);
assert_print(compare(&word_value, &half_read, 2, 0));
printf("- word read with a different endianess than me, ");
memory_read_word(m, 0, &word_read, 1 - my_endianess);
assert_print(compare(&word_value, &word_read, 4, 1));
printf("- half read with a different endianess than me, ");
memory_read_half(m, 0, &half_read, 1 - my_endianess);
assert_print(compare(&word_value, &half_read, 2, 1));
// Test reading from the simulated memory
printf("- word read with the same endianess as me, ");
uint32_t read_word_value;
memory_read_word(m, 0, &read_word_value, my_endianess);
assert_print(compare_with_sim(&read_word_value, m, 4, 0));
uint16_t read_half_value;
memory_read_half(m, 0, &read_half_value, my_endianess);
assert_print(compare_with_sim(&read_half_value, m, 2, 0));
memory_read_word(m, 0, &read_word_value, 1 - my_endianess);
assert_print(compare_with_sim(&read_word_value, m, 4, 1));
memory_read_half(m, 0, &read_half_value, 1 - my_endianess);
assert_print(compare_with_sim(&read_half_value, m, 2, 1));
printf("Writing word and half at address 0, then reading the bytes, "
"the result should depend on simulated memory endianess :\n");
printf("- word write with the same endianess as me, ");
memory_write_word(m, 0, word_value, my_endianess);
assert_print(compare_with_sim(&word_value, m, 4, 0));
printf("- half write with the same endianess as me, ");
memory_write_half(m, 0, half_value, my_endianess);
assert_print(compare_with_sim(&half_value, m, 2, 0));
printf("- word write with a different endianess than me, ");
memory_write_word(m, 0, word_value, 1 - my_endianess);
assert_print(compare_with_sim(&word_value, m, 4, 1));
printf("- half write with a different endianess than me, ");
memory_write_half(m, 0, half_value, 1 - my_endianess);
assert_print(compare_with_sim(&half_value, m, 2, 1));
// Test writing to the simulated memory with an invalid address
uint32_t INVALID_ADDRESS = (uint32_t)memory_get_size(m) + 1;
int MEMORY_ERROR = -1;
int read_result, write_result;
printf("- word write to an invalid address, ");
write_result = memory_write_word(m, INVALID_ADDRESS, word_value, my_endianess);
assert_print(write_result == MEMORY_ERROR);
printf("- half write to an invalid address, ");
write_result = memory_write_half(m, INVALID_ADDRESS, half_value, my_endianess);
assert_print(write_result == MEMORY_ERROR);
printf("- word read from an invalid address, ");
read_result = memory_read_word(m, INVALID_ADDRESS, &word_read, my_endianess);
assert_print(read_result == MEMORY_ERROR);
printf("- half read from an invalid address, ");
read_result = memory_read_half(m, INVALID_ADDRESS, &half_read, my_endianess);
assert_print(read_result == MEMORY_ERROR);
memory_destroy(m);
return FAIL;
}