/
section.h
142 lines (89 loc) · 4.15 KB
/
section.h
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
/**
** \file
** \brief Section & Data management
** \author Sandor Zsuga (Jubatian)
** \copyright 2013 - 2014, GNU GPLv3 (version 3 of the GNU General Public
** License) extended as RRPGEvt (temporary version of the RRPGE
** License): see LICENSE.GPLv3 and LICENSE.RRPGEvt in the project
** root.
** \date 2014.10.29
**
** Manages the sections and their data during the compilation. Currently
** singleton, but desinged so it is possible to extend later.
*/
#ifndef SECTION_H
#define SECTION_H
#include "types.h"
/* Section object structure */
typedef struct section_s section_t;
/* Sections supported */
#define SECT_CODE 0U
#define SECT_DATA 1U
#define SECT_HEAD 2U
#define SECT_DESC 3U
#define SECT_ZERO 4U
#define SECT_FILE 5U
/* Count of sections */
#define SECT_CNT 6U
/* The FILE section is special: it does not accept operations; it is meant
** to be used for adding data to the application binary. */
/* The ZERO section is special: it has no data, however it accepts operations
** normally. */
/* Maximal allowed CPU RAM usage (SECT_DATA & SECT_ZERO) */
#define SECT_MAXRAM (0x10000U - 0x800U - 0x40U)
/* Error codes. OVR: Overlap. OVF: Overflow (out of range). */
#define SECT_ERR_OK 0U
#define SECT_ERR_OVR 1U
#define SECT_ERR_OVF 2U
/* Get built-in singleton object handle. */
section_t* section_getobj(void);
/* Retrieves string for identifying the section base symbol. These are:
** '@.code', '@.data', '@.head', '@.desc', '@.zero', and '@.file'. */
uint8 const* section_getsbstr(auint sec);
/* Initializes or resets a section object. */
void section_init(section_t* hnd);
/* Sets section to perform operations on. */
void section_setsect(section_t* hnd, auint sect);
/* Gets currently selected section. */
auint section_getsect(section_t* hnd);
/* Sets offset within section, word granularity. */
void section_setoffw(section_t* hnd, auint off);
/* Gets word offset for a subsequent use with section_setw(). */
auint section_getoffw(section_t* hnd);
/* Gets byte offset for a subsequent use with section_setb(). */
auint section_getoffb(section_t* hnd);
/* Pushes word data to the section. If necessary, aligns offset to word
** boundary first. Returns error code on failure (overlap or out of section's
** allowed size). */
auint section_pushw(section_t* hnd, auint data);
/* Pushes byte data to the section. Returns error code on failure (overlap or
** out of section's allowed size). */
auint section_pushb(section_t* hnd, auint data);
/* Changes an unit of word data at a given offset. This is meant to be used by
** second pass to substitue values which could not be resolved earlier. Will
** only have effect in areas already occupied. OR combines. */
void section_setw(section_t* hnd, auint off, auint data);
/* Changes an unit of byte data at a given (byte) offset. This is meant to be
** used by second pass to substitue values which could not be resolved
** earlier. Will only have effect in areas already occupied. OR combines. */
void section_setb(section_t* hnd, auint off, auint data);
/* Forces an unit of word data into the section, overriding if anything is
** there. This is meant to be used by autofills. Sets occupation for the
** forced word. */
void section_fsetw(section_t* hnd, auint off, auint data);
/* Adds padding 0x20 (space) character if necessary: overrides zeros in
** occupied areas (string terminator), and writes any unoccupied area. Uses
** word offsets, applying the padding to both bytes as necessary. */
void section_strpad(section_t* hnd, auint off);
/* Set section base offset. */
void section_setbase(section_t* hnd, auint base);
/* Get aligned word address for an offset within the section (that is, base
** added to it). */
auint section_getaddr(section_t* hnd, auint off);
/* Get section's size in words. This is calculated from the last occupied word
** of the section. The FILE section always returns zero size. */
auint section_getsize(section_t* hnd);
/* Get section data pointer for reading, combining into an application binary.
** The size of the memory area can be retrieved by section_getsize(), */
uint16 const* section_getdata(section_t* hnd);
#endif