-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariables.c
More file actions
103 lines (82 loc) · 1.94 KB
/
Copy pathvariables.c
File metadata and controls
103 lines (82 loc) · 1.94 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
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "errors.h"
#include "variables.h"
#ifdef DEBUG
#include <stdio.h>
#endif
struct variable {
char *name;
unsigned int value;
};
#define ALLOC_CHUNK 64
static struct variable *vars = NULL;
static size_t vars_alloc = 0;
static size_t vars_size = 0;
static void resize_if_necessary(void) {
struct variable *temp;
if(vars_size >= vars_alloc) {
vars_alloc += ALLOC_CHUNK;
temp = realloc(vars, vars_alloc * sizeof(struct variable));
if(temp == NULL)
no_memory();
vars = temp;
}
}
void variables_destroy(void) {
size_t i;
for(i = 0; i < vars_size; ++i)
free(vars[i].name);
free(vars);
/* set global variables to an empty state */
vars = NULL;
vars_alloc = 0;
vars_size = 0;
}
unsigned int* load_int(const char *name) {
size_t i;
for(i = 0; i < vars_size; ++i)
if(strcmp(name, vars[i].name) == 0)
return &(vars[i].value);
return NULL;
}
int store_label(const char *name, unsigned int address) {
unsigned int real_address;
/* because of bank switching we have to calculate the real address when
it's loaded. Otherwise we could jump into wrong memory areas */
if(address < 0x4000)
real_address = address;
else
real_address = (address % 0x4000) + 0x4000;
return store_int(name, real_address);
}
int store_int(const char *name, unsigned int value) {
size_t i;
char *newname;
for(i = 0; i < vars_size; ++i)
if(strcmp(name, vars[i].name) == 0)
return 1; /* multiple definition */
resize_if_necessary();
newname = strdup(name);
if(newname == NULL)
no_memory();
vars[vars_size].value = value;
vars[vars_size].name = newname;
++vars_size;
return 0;
}
#ifdef DEBUG
void variables_inspect(void) {
size_t i;
printf("===Variables===\nSize: %zu\nAllocated: %zu\n", vars_size, vars_alloc);
for(i = 0; i < vars_size; ++i) {
printf("Variable %zu:\n"
"\tname: %s\n"
"\tvalue %d\n",
i,
vars[i].name,
vars[i].value);
}
}
#endif