comparison lwlink/trunk/src/output.c @ 121:7bc853cb2ca9

Added simplistic output module for DECB target
author lost
date Thu, 22 Jan 2009 02:05:08 +0000
parents
children 6084a3859fb4
comparison
equal deleted inserted replaced
120:36ce328df3c3 121:7bc853cb2ca9
1 /*
2 output.c
3 Copyright © 2009 William Astle
4
5 This file is part of LWLINK.
6
7 LWLINK is free software: you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation, either version 3 of the License, or (at your option) any later
10 version.
11
12 This program is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 more details.
16
17 You should have received a copy of the GNU General Public License along with
18 this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 Actually output the binary
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "lwlink.h"
32
33 // this prevents warnings about not using the return value of fwrite()
34 // and, theoretically, can be replaced with a function that handles things
35 // better in the future
36 #define writebytes(s, l, c, f) do { int r; r = fwrite((s), (l), (c), (f)); } while (0)
37
38 void do_output_decb(FILE *of);
39 void do_output_raw(FILE *of);
40
41 void do_output(void)
42 {
43 FILE *of;
44
45 of = fopen(outfile, "wb");
46 if (!of)
47 {
48 fprintf(stderr, "Cannot open output file %s: ", outfile);
49 perror("");
50 exit(1);
51 }
52
53 switch (outformat)
54 {
55 case OUTPUT_DECB:
56 do_output_decb(of);
57 break;
58
59 case OUTPUT_RAW:
60 do_output_raw(of);
61 break;
62
63 default:
64 fprintf(stderr, "Unknown output format doing output!\n");
65 exit(111);
66 }
67
68 fclose(of);
69 }
70
71 void do_output_decb(FILE *of)
72 {
73 int sn;
74 unsigned char buf[5];
75
76 for (sn = 0; sn < nsects; sn++)
77 {
78 if (sectlist[sn].ptr -> flags & SECTION_BSS)
79 {
80 // no output for a BSS section
81 continue;
82 }
83 // write a preamble
84 buf[0] = 0x00;
85 buf[1] = sectlist[sn].ptr -> codesize >> 8;
86 buf[2] = sectlist[sn].ptr -> codesize & 0xff;
87 buf[3] = sectlist[sn].ptr -> loadaddress >> 8;
88 buf[4] = sectlist[sn].ptr -> loadaddress & 0xff;
89 writebytes(buf, 1, 5, of);
90 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
91 }
92 // write a postamble
93 buf[0] = 0xff;
94 buf[1] = 0x00;
95 buf[2] = 0x00;
96 buf[3] = linkscript.execaddr >> 8;
97 buf[4] = linkscript.execaddr & 0xff;
98 writebytes(buf, 1, 5, of);
99 }
100
101 void do_output_raw(FILE *of)
102 {
103 }