-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.c
More file actions
142 lines (130 loc) · 3.27 KB
/
memory.c
File metadata and controls
142 lines (130 loc) · 3.27 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
139
140
141
142
#include "data.h"
static BinaryWord *binaryImg = NULL;
static HexWord *hexImg = NULL;
unsigned static IC = MEMORY_START;
unsigned static DC = 0;
unsigned static ICF = 0;
unsigned static DCF = 0;
extern HexWord *convertBinaryWordToHex(BinaryWord *word);
extern char *numToBin(int num);
extern unsigned binaryStringToHexNumber(char binaryStr[4]);
unsigned getDC() { return DC; }
unsigned getIC() { return IC; }
unsigned getICF() { return ICF; }
unsigned getDCF() { return DCF; }
void increaseDataCounter(int amount)
{
DC += amount;
}
void increaseInstructionCounter(int amount)
{
IC += amount;
}
void allocMemoryImg()
{
const int totalSize = DCF - MEMORY_START;
int i, j;
if (binaryImg != NULL)
free(binaryImg);
if (hexImg != NULL)
free(hexImg);
binaryImg = (BinaryWord *)malloc(totalSize * sizeof(BinaryWord));
hexImg = (HexWord *)malloc(totalSize * sizeof(HexWord));
for (i = 0; i < totalSize; i++)
{
hexImg[i]._A = 0;
hexImg[i]._B = 0;
hexImg[i]._C = 0;
hexImg[i]._D = 0;
hexImg[i]._E = 0;
for (j = 0; j < BINARY_WORD_SIZE; j++)
{
binaryImg[i].digit[j].on = 0;
}
}
}
void resetMemoryCounters()
{
IC = MEMORY_START;
DC = 0;
ICF = 0;
DCF = 0;
}
void printBinaryImg()
{
int i;
int totalSize = DCF - MEMORY_START;
for (i = 0; i < totalSize; i++)
{
printf("%04d ", MEMORY_START + i);
printWordBinary(i);
}
}
void addWord(int value, DataType type)
{
if (type == Code)
addWordToCodeImage(numToBin(value));
else if (type == Data)
addWordToDataImage(numToBin(value));
}
void addWordToDataImage(char *s)
{
wordStringToWordObj(s, Data);
DC++;
}
void addWordToCodeImage(char *s)
{
wordStringToWordObj(s, Code);
IC++;
}
void wordStringToWordObj(char *s, DataType type)
{
int j;
int index = type == Code ? IC - MEMORY_START : DC - MEMORY_START;
for (j = 0; j < BINARY_WORD_SIZE; j++)
binaryImg[index].digit[j].on = s[j] == '1' ? 1 : 0;
}
void printWordBinary(unsigned index)
{
int j;
for (j = 0; j < BINARY_WORD_SIZE; j++)
{
if (j % 4 == 0)
printf(" ");
printf("%d", binaryImg[index].digit[j].on ? 1 : 0);
}
printf("\n");
}
void calcFinalAddrsCountersValues()
{
ICF = IC;
DCF = ICF + DC;
DC = IC;
IC = MEMORY_START;
}
void printMemoryImgInRequiredObjFileFormat()
{
extern BinaryWord *binaryImg;
extern HexWord *hexImg;
int i;
int totalSize = DCF - MEMORY_START;
printf("%d %d\n", ICF - MEMORY_START, DCF - ICF);
for (i = 0; i < totalSize; i++)
{
hexImg[i] = *convertBinaryWordToHex(&binaryImg[i]);
printf("%04d A%x-B%x-C%x-D%x-E%x\n", MEMORY_START + i, hexImg[i]._A, hexImg[i]._B, hexImg[i]._C, hexImg[i]._D, hexImg[i]._E);
}
}
void writeMemoryImageToObFile(FILE *fp)
{
extern BinaryWord *binaryImg;
extern HexWord *hexImg;
int i;
int totalSize = DCF - MEMORY_START;
fprintf(fp, "%d %d\n", ICF - MEMORY_START, DCF - ICF);
for (i = 0; i < totalSize; i++)
{
hexImg[i] = *convertBinaryWordToHex(&binaryImg[i]);
fprintf(fp, "%04d A%x-B%x-C%x-D%x-E%x\n", MEMORY_START + i, hexImg[i]._A, hexImg[i]._B, hexImg[i]._C, hexImg[i]._D, hexImg[i]._E);
}
}