-
Notifications
You must be signed in to change notification settings - Fork 0
/
sof.c
104 lines (96 loc) · 2.28 KB
/
sof.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
/*
* sof.c
* Copyright (C) 2015 Samuel Dominguez Lorenzo
*/
#include "sof.h"
extern struct opt_t opts;
int get_symbols(FILE *in)
{
char line[MAX_SYMBOL_LINE_SIZE];
char symbol[MAX_SYMBOL_SIZE];
char value[MAX_VALUE_SIZE];
int i, j;
rewind(in);
if (strcmp(SOF_SYMBOL_START, fgets(line, MAX_SYMBOL_LINE_SIZE, in)) != 0) {
error("corrupt SOF file format");
return 1;
}
while (fgets(line, MAX_SYMBOL_LINE_SIZE, in) != NULL) {
if (strcmp(line, SOF_BINARY_START) == 0) break;
else if (strcmp(line, SOF_END) == 0) break;
for (i = 0; line[i] != ' '; ++i) ;
strncpy(symbol, line, i);
symbol[i] = '\0';
i++;
for (j = i; line[j] != '\n'; ++j) ;
strncpy(value, line + i, j - i);
add_label(symbol, (int) strtol(value, NULL, 0) + opts.org);
}
return 0;
}
int write_to_ram(FILE *in)
{
char line[MAX_SYMBOL_LINE_SIZE];
char c;
fpos_t filepos;
fpos_t tmpfilepos;
int i;
int length;
int error_flag;
int value;
char errorstring[100];
fgetpos(in, &filepos);
fseek(in, -1, SEEK_END);
c = (char) fgetc(in);
if (c != 'd') {
error("corrupt file format");
return 1;
}
fsetpos(in, &filepos);
fseek(in, -1, SEEK_CUR);
c = (char) fgetc(in);
if (c == 'd') {
/* its a library object file, nothing to do here */
return 0;
} else {
fsetpos(in, &filepos);
while (1) {
i = filepos.__pos;
while ((c = (char) fgetc(in)) != ' ' && c != '\n') ;
fseek(in, 1, SEEK_CUR);
if (c == '\n') break;
fseek(in, -1, SEEK_CUR);
fgetpos(in, &filepos);
length = (int) filepos.__pos - i;
fgetpos(in, &tmpfilepos);
tmpfilepos.__pos = i;
fsetpos(in, &tmpfilepos);
fgets(line, length, in);
line[length] = '\0';
if (currw + 1 == DISK_SIZE) {
error("binary generation exceeds floppy disk size");
}
if (line[0] == '0') {
ram[currw] = (u16) strtol(line, NULL, 0);
currw++;
} else {
value = get_symbol_value(line, &error_flag);
if (error_flag) {
sprintf(errorstring, "undefined symbol '%s'", line);
error(errorstring);
return 1;
} else if (strcmp(SOF_ENTRY_POINT, line) == 0) {
printf("entry point!\n");
reloc.entry_point = value;
printf("entry_point: %d\n", reloc.entry_point);
}
ram[currw] = (u16) value;
add_reloc(currw);
currw++;
}
fgetc(in);
fgetpos(in, &filepos);
}
}
return 0;
}