/
loader.c
356 lines (317 loc) · 9.79 KB
/
loader.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "bool.h"
#include "loader.h"
#include "tools.h"
/**
* Sam Radford and Alex Fitzgerald
*/
/**
* load
* This function takes a command line argument containing
* the name of a file and loads the contents of the file
* into memory byte by byte until there is an error
* @param: argc: the number of args
* args: the name of the file
* @return: goodLine: returns false if an error was found
*/
bool load(int argc, char *args) {
bool goodLine = TRUE;
int lineNumber = 1;
if(validFileName(args)){
FILE * fi;
fi = fopen(args,"r");
char record[80];
int prevAdr = -1,startAdr,lineSize;
int firstLine = 0;
int loop;
int dataLoc,byteLoc;
int dataLength;
bool lastLineData;
bool memError;
while(fgets(record,80,fi)!=NULL && goodLine){
dataLoc = 9; //Where data starts
byteLoc = 0; //Which byte of data to get
if (isAddress(record)){
startAdr=setLastAddress(record); //Address in a record
}
int lineSize = lineLength(record); //Length of record
if(record[lineSize]!='\n') {
discardRest(record,fi);
}
//Calls the checkLine
if(checkLine(record)) {
dataLength = lengthOfData(record); //Length of data in a record
//If the record isn't the first line of the file
if(firstLine==1) {
if(!(lastLineData&&startAdr>prevAdr)) { //If there was data on the last line make sure the address is bigger
goodLine = FALSE;
//printf("Error on line %d\n",lineNumber);
}
}
//If the record is the first line
else{
if(isAddress(record)) {
firstLine = 1;
}
}
}
//checkLine returns false
else{
//printf("Error on line %d\n",lineNumber);
goodLine = FALSE;
}
//If all checks have passed
if(goodLine){
//Goes through the data
for(loop = 0; loop < dataLength;loop+=2){
//Adds the byte to memory
putByte(startAdr,grabDataByte(record,byteLoc),&memError);
//printf("%x\n",grabDataByte(record,byteLoc));
startAdr++;
byteLoc+=2;
}
//puts(record); //Output record
prevAdr = startAdr - 1;
lineNumber++;
lastLineData = isData(record);
}
else if(goodLine == FALSE){
printf("Error on line %d\n", lineNumber);
puts(record);
}
}
fclose(fi);
}
//If the file didn't open
else{
goodLine = FALSE;
printf("Error opening file");
}
return goodLine;
}
/**
* validFileName
* This function checks to make sure the file name
* given as an argument is valid(fileName.yo,exists)
* @param: fileName: the name of the file to check
* @return valid: Returns true if the filename ends in .yo and the file exists
*/
bool validFileName(char* fileName) {
bool valid = FALSE;;
int loop =0;
while(fileName[loop] !='.' && loop < BUFFER_SIZE) {
loop++;
}
//Checks to make sure it's long enough to end in .yo, that it does end in .yo
//and that the file with the given name actually exits
if(fileName[loop] == '.' && fileName[loop+1] == 'y' && fileName[loop+2] == 'o'){
FILE * fi;
fi = fopen(fileName,"r");
if(fi != NULL){
valid = TRUE;
fclose(fi);
}
valid = TRUE;
}
//printf("%d\n",valid);
if (valid == FALSE){
printf("File Opening Failed\n");
printf("Correct Usage: yess <filename>.yo\n");
}
return valid;
}
/** grabAddress
* This function returns the address written in a record
* @param: dataRecord: The record to grab the address from
* @return: retAddr: The address to return
*/
char* grabAddress(char* dataRecord) {
char* retAddr = strndup(dataRecord+2,5); //The char from 2-5 make up the address
return retAddr;
}
/**
* discardRest
* This function moves the character pointer
* in a file until it finds a new line or an end of file
* @param: recentLine: The most recently read line
* userFile: The pointer to the file being read
*/
void discardRest(char* recentLine, FILE * userFile) {
int x = 0;//fgetc(userFile);
while(x != '\n'&& !feof(userFile)){
x = fgetc(userFile); //Moves the pointer until it finds '\n'
}
}
/**
* isAddress
* This function checks to see if the record
* contains an address and that the address is valid
* @param: record: The array of char to check
* @return: valid: Returns true if the address follows the format
* and the address is after the previous one.
*/
//Check that the address follows the previous
bool isAddress(char* record){
int loop;
bool valid = FALSE;
//Checks for correct format of 0xhexhexhex
if(record[2]=='0'&& record[3]=='x'){
if(record[7] == ':'){
if(checkHex(record,4,6)){
valid = TRUE;
}
}
}
return valid;
}
/**
* isData
* This function checks to see if the record
* contains data and that the data is valid
* @param: record: the array of char to check
* @return: valid: returns true if there is data
* and the data is valid
*/
bool isData(char* record) {
int loop;
bool valid = TRUE;
bool foundSpace = FALSE;
int digits = 0;
for(loop = 9; loop <= 20; loop++){
if(record[loop] == 32){
foundSpace == TRUE; //If there is a space no data can follow
}
else if(isxdigit(record[loop])){
digits++;
if(foundSpace){
valid = FALSE; //If there is data after a space: False
}
}
else{
valid = FALSE; //If something not a space or hex is found: False
}
}
if(digits % 2 != 0){
valid = FALSE; //Checks to make sure there is a not a 4-bit char alone
}
return valid;
}
/**
* isSpaces
* This function checks a portion of a record
* and checks to see if every char is a space
* @param: record: The array of char
* start: The starting index to check
* end: The last index to check
* @return: vald: returns true if the char are all spaces
*/
bool isSpaces(char* record, int start, int end){
int loop;
bool valid = TRUE;
for(loop = start; loop<=end; loop++){
if(record[loop] != ' '){
valid = FALSE; //If one char isn't a space then return false
}
}
return valid;
}
/**
* checkHex
* This function checks a portion of a record
* and determines if every char is a hex digit
* @param: record: The array of char
* start: The starting index to check from
* end: The last index to check
* @return: valid: Returns true if all char are hex
*/
bool checkHex(char* record, int start, int end){
int loop;
bool valid = TRUE;
for(loop = start; loop <= end; loop++){
if(isxdigit(record[loop])==0){
valid = FALSE; //If the char isn't a hex then the chars aren't hex
}
}
return valid;
}
unsigned char grabDataByte(char* record, int start) {
//Convert data into an unsigned int
//Pass that and start into getByteNumber in tools.c
char* data = strndup(record+9+start,2);
unsigned char numString[3];
numString[0] = data[0];
numString[1] = data[1];
numString[2] = 0;
long int intVal = strtol(numString,NULL,16);
unsigned char byte = getByteNumber(0,intVal);
return byte;
}
bool checkLine(char * record) {
bool validLine = FALSE;
if(isAddress(record)) {
//If there is an address and there is data
if(isData(record)){
if(isSpaces(record,0,1) && isSpaces(record,8,8) && isSpaces(record,21,21)){
if(record[22] == '|'){
validLine =TRUE;
}
}
}
//If there is an address and no data
else{
if(isSpaces(record,0,1) && isSpaces(record,8,21) && record[22] == '|')
validLine = TRUE;
}
}
//If there is no address
else{
if(isSpaces(record,0,21) && record[22]== '|')
validLine = TRUE;
}
return validLine;
}
/**
* lineLength
* This function takes a record and returns the length
* @param: record: The record to check the length
* @return: x: the size of the record
*/
int lineLength(char record[80]){
int x = 0;
while(record[x] != '\n' && x < 80){
x++; //Determines length
}
return x;
}
/**
* setLastAddress
* returns the value of the last address that had a value
* stored into it
* @param: record: The record containing the address
* @return: intVal: The value of the address
*/
int setLastAddress(char* record){
char* address = grabAddress(record);
unsigned char numAddr[4];
numAddr[0] = address[2];
numAddr[1] = address[3];
numAddr[2] = address[4];
numAddr[3] = 0;
long int intVal = strtol(numAddr,NULL,16);
return intVal;
}
/**
* lengthOfData
* returns the length of the data in a record
* @param: record: The record containing the data
* @return: x: the length of the data
*/
int lengthOfData(char * record) {
int x = 0;
while(record[x+9]!=' ') {
x++;
}
return x;
}