-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsof.c
71 lines (61 loc) · 1.64 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
/*
* sof.c
* Copyright (C) 2015 Samuel Dominguez Lorenzo
*
* This is an implementation of the Simple Object Format (SOF)
* See file SIMPLE_OBJECT_FORMAT for reference.
*/
#include <stdio.h>
#include <string.h>
#include "output.h"
#include "label.h"
#include "opt.h"
extern struct opt_t opts;
/* max symbol pair length, then used for single binary section element.
* max label/symbol length = 63 , max hex word length = 6, newline + space = 2
* 63 + 6 + 2 = 71 (+ null byte = 72)
*/
#define ATOM_MAX_LENGTH 72
/* max hex word length: "0x" = 2 + 4 hex digits = 6 (+ null byte = 7)
* + newline for convenience = 8
*/
#define WORD_MAX_LENGTH 8
/* writes the sof to file "out", expects the stream to be
* already open for writing
*/
void write_sof(FILE *out)
{
struct label undefined;
char line[ATOM_MAX_LENGTH];
char word[WORD_MAX_LENGTH];
int i;
/* first write the first section */
fputs("symbols:\n", out);
/* now write defined symbols with their values */
for (i = 0; i < label_count; ++i) {
strcpy(line, label_table[i].name);
strcat(line, " ");
/* we add the org address to these values, default is 0 */
sprintf(word, "0x%04x\n",
label_table[i].ram_address + opts.org);
strcat(line, word);
fputs(line, out);
}
/* generate symbols only? */
if (opts.sof_lib_mode) goto sof_end;
/* next section, binary */
fputs("binary:\n", out);
for (i = start_write_address; i < currw; ++i) {
if (check_undefined_at_address(i, &undefined)) {
fputs(undefined.name, out);
fputs(" ", out);
} else {
sprintf(word, "0x%04x", ram[i]);
fputs(word, out);
fputs(" ", out);
}
}
fputs("\n", out);
sof_end:
fputs("end", out);
}