ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
_linker.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2026 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "../../ugbc.h"
36
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
41static void linker_setup_variables_memory( Environment * _environment, Variable * _variable ) {
42
43 Variable * variable = _variable;
44
45 while( variable ) {
46
47 if ( (variable->memoryArea && variable->bankAssigned != -1 && !variable->assigned) || ( !variable->assigned || ( variable->assigned && !variable->temporary ) ) && !variable->imported && !variable->memoryArea ) {
48 switch( variable->type ) {
49 case VT_MUSIC:
50 if ( variable->sidFile ) {
51 if ( variable->sidFile->loadAddress ) {
52 //cfgline3("MEM%s: file = \"\", start = $%4.4x, size = $%4.4x;", variable->name, variable->sidFile->loadAddress, variable->sidFile->size );
53 }
54 }
55 }
56 }
57
58 variable = variable->next;
59
60 }
61
62}
63
64static void linker_setup_variables_segment( Environment * _environment, Variable * _variable ) {
65
66 Variable * variable = _variable;
67
68 while( variable ) {
69
70 if ( (variable->memoryArea && variable->bankAssigned != -1 && !variable->assigned) || ( !variable->assigned || ( variable->assigned && !variable->temporary ) ) && !variable->imported && !variable->memoryArea ) {
71 switch( variable->type ) {
72 case VT_MUSIC:
73 if ( variable->sidFile ) {
74 if ( variable->sidFile->loadAddress ) {
75 cfgline2("%s: load = MAIN, type = overwrite, optional = yes, start = $%4.4x;", variable->name, variable->sidFile->loadAddress );
76 }
77 }
78 }
79 }
80
81 variable = variable->next;
82
83 }
84
85}
86
94void linker_setup( Environment * _environment ) {
95
96 if ( _environment->program.startingAddress < 0x1c0e ) {
98 }
99
100 cfghead0("FEATURES {");
101 cfgline0("STARTADDRESS: default = $1c01;");
102 cfghead0("}");
103 cfghead0("MEMORY {");
104 cfgline0("ZP: file = \"\", start = $0002, size = $00FE, define = yes;");
105 // cfgline0("LOADADDR: file = %O, start = %S - 2, size = $0002;");
106
107 MemoryArea * actual = _environment->memoryAreas;
108 int maxAddress = 0xFBFF - 4096;
109 while( actual ) {
110 int realMaxAddress = 0x800 + actual->start + ( actual->end - actual->start );
111 if ( actual->type != MAT_RAM && realMaxAddress > maxAddress ) {
112 maxAddress = realMaxAddress;
113 }
114 actual = actual->next;
115 }
116
117 cfgline1("MAIN: file = %%O, start = %%S-2, size = $%4.4x - %%S;", (unsigned short)maxAddress);
118
119 actual = _environment->memoryAreas;
120 while( actual ) {
121 if ( actual->type == MAT_RAM ) {
122 cfgline3("RAM%3.3x: file = \"\", start = $%4.4x, size = $%4.4x;", actual->id, (unsigned short)actual->start, (unsigned short)(actual->end - actual->start) );
123 }
124 actual = actual->next;
125 }
126
127 linker_setup_variables_memory( _environment, _environment->variables );
128 for( int j=0; j< (_environment->currentProcedure+1); ++j ) {
129 linker_setup_variables_memory( _environment, _environment->tempVariables[j] );
130 }
131
132 cfghead0("}");
133 cfghead0("SEGMENTS {");
134 cfgline0("ZEROPAGE: load = ZP, type = zp, optional = yes;");
135 // cfgline0("LOADADDR: load = LOADADDR, type = ro;");
136 // cfgline0("EXEHDR: load = MAIN, type = ro, optional = yes;");
137
138 cfgline0("BASIC: load = MAIN, type = ro, optional = no;");
139 cfgline1("CODE: load = MAIN, type = rw, start = $%4.4x;", _environment->program.startingAddress );
140 cfgline0("RODATA: load = MAIN, type = ro, optional = yes;");
141 cfgline0("DATA: load = MAIN, type = rw, optional = yes;");
142 cfgline0("BSS: load = MAIN, type = bss, optional = yes, define = yes;");
143 cfgline0("UDCCHAR: load = MAIN, type = overwrite, optional = yes, start = $9800;");
144
145 actual = _environment->memoryAreas;
146 while( actual ) {
147 if ( actual->type == MAT_RAM ) {
148 cfgline3("MA%3.3x: load = RAM%3.3x, type = overwrite, optional = yes, start = $%4.4x;", actual->id, actual->id, actual->start);
149 } else {
150 cfgline2("MA%3.3x: load = MAIN, type = overwrite, optional = yes, start = $%4.4x;", actual->id, actual->start);
151 }
152 actual = actual->next;
153 }
154
155 linker_setup_variables_segment( _environment, _environment->variables );
156 for( int j=0; j< (_environment->currentProcedure+1); ++j ) {
157 linker_setup_variables_segment( _environment, _environment->tempVariables[j] );
158 }
159
160}
161
169void linker_cleanup( Environment * _environment ) {
170
171 cfghead0("}");
172
173}
void linker_setup(Environment *_environment)
Emit tail of linker's configuration file lines.
Definition _linker.c:48
void linker_cleanup(Environment *_environment)
Emit tail of linker's configuration file lines.
Definition _linker.c:133
Program program
Definition ugbc.h:3179
Variable * tempVariables[MAX_PROCEDURES]
Definition ugbc.h:2606
MemoryArea * memoryAreas
Definition ugbc.h:2689
int currentProcedure
Definition ugbc.h:2601
Variable * variables
Definition ugbc.h:2616
int start
Definition ugbc.h:737
struct _MemoryArea * next
Definition ugbc.h:760
int id
Definition ugbc.h:732
MemoryAreaType type
Definition ugbc.h:757
int end
Definition ugbc.h:747
int startingAddress
Definition ugbc.h:2217
int loadAddress
Definition sid_file.h:36
int bankAssigned
Definition ugbc.h:1172
SIDFILE * sidFile
Definition ugbc.h:1213
int assigned
Definition ugbc.h:1020
struct _Variable * next
Definition ugbc.h:1225
char * name
Definition ugbc.h:979
VariableType type
Definition ugbc.h:988
MemoryArea * memoryArea
Definition ugbc.h:1107
int imported
Definition ugbc.h:1014
int temporary
Definition ugbc.h:996
#define CRITICAL_INVALID_PROGRAM_START(a)
Definition ugbc.h:3742
@ MAT_RAM
Definition ugbc.h:726
#define cfgline0(s)
Definition ugbc.h:4273
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
struct _MemoryArea MemoryArea
@ VT_MUSIC
Definition ugbc.h:516
#define cfgline1(s, a)
Definition ugbc.h:4274
#define cfgline2(s, a, b)
Definition ugbc.h:4275
#define cfgline3(s, a, b, c)
Definition ugbc.h:4276
#define cfghead0(s)
Definition ugbc.h:4267