ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
_build.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
41#include<stdlib.h>
42#include<stdio.h>
43#include<math.h>
44
45extern char OUTPUT_FILE_TYPE_AS_STRING[][16];
46
47void target_linkage( Environment * _environment ) {
48
49 char commandLine[8*MAX_TEMPORARY_STORAGE];
50 char executableName[MAX_TEMPORARY_STORAGE];
51 char listingFileName[MAX_TEMPORARY_STORAGE];
52
53 if ( _environment->outputFileType != OUTPUT_FILE_TYPE_K7_ORIGINAL && _environment->outputFileType != OUTPUT_FILE_TYPE_K7_NEW ) {
55 }
56
57 BUILD_SAFE_REMOVE( _environment, _environment->exeFileName );
58
59 BUILD_TOOLCHAIN_ASM6809_GET_EXECUTABLE( _environment, executableName );
60
61 BUILD_TOOLCHAIN_ASM6809_GET_LISTING_FILE( _environment, listingFileName );
62
63 BUILD_TOOLCHAIN_ASM6809EXEC( _environment, "-B", 10240, executableName, listingFileName );
64
65 if ( _environment->listingFileName ) {
66
67 if ( _environment->profileFileName && _environment->profileCycles ) {
68 if ( _environment->executerFileName ) {
69 sprintf(executableName, "%s", _environment->executerFileName );
70 } else if( access( "run6809.exe", F_OK ) == 0 ) {
71 sprintf(executableName, "%s", "run6809.exe" );
72 } else {
73 sprintf(executableName, "%s", "run6809" );
74 }
75
76 sprintf( commandLine, "\"%s\" -i \"%s\" -R 3000 -b -l 3000 \"%s\" -p \"%s\" %d",
77 executableName,
78 _environment->listingFileName,
79 _environment->exeFileName,
80 _environment->profileFileName,
81 _environment->profileCycles ? _environment->profileCycles : 1000000
82 );
83
84 if ( system_call( _environment, commandLine ) ) {
85 printf("The profiling of assembly program failed.\n\n");
86 return;
87 };
88
89 }
90
91 }
92
93 if ( _environment->outputFileType == OUTPUT_FILE_TYPE_K7_NEW ) {
94 convertbintok7( _environment );
95 } else {
96 to8_convertbintok7_original( _environment );
97 }
98
99}
100
101static unsigned int sum=0;
102static unsigned char chk=0;
103static unsigned short l;
104static int nblock;
105static int rest;
106static int cnt=0;
107static int frmt( Environment * _environment, char * ptr, char size,char* out)
108{
109 memset(out,0x20,11);
110 char * point=".";
111 int k=0,i=0;
112 int p=strcspn(ptr,point);
113 if(p==size-1||p>8||size>12||p==0)
114 {
116 }
117 for(i=0;i<size;i++)
118 {
119 if(i==p)
120 k+=(8-p);
121 else
122 {
123 out[k]=ptr[i];
124 k++;
125 }
126
127 }
128 return 0;
129}
130
131// FIX #173
132#if ( defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN ) || \
133 defined(__BIG_ENDIAN__) || \
134 defined(__ARMEB__) || \
135 defined(__THUMBEB__) || \
136 defined(__AARCH64EB__) || \
137 defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__)
138
139 static void swapen(unsigned short *num)
140 {
141 return *num;
142 }
143
144#else
145
146 static void swapen(unsigned short *num)
147 {
148 unsigned short swapped;
149 swapped = (*num>>8) | (*num<<8);
150 *num=swapped;
151 }
152
153#endif
154
155static void crc ( unsigned char * data, int l,unsigned int * old)
156{
157
158 cnt+=l;
159 for (int i= 0;i<l;i++)
160 {
161 (*old)+=data[i];
162
163 }
164}
165
166static unsigned char cmpl(unsigned int n)
167{
168 return 0x100- ((unsigned char)n);
169}
170
172{
173 // if(argc==1)
174 // {
175 // printf("Missing arguments !!!!!!\n"
176 // "bin2k7 source pc128_filename K7_filename\n"
177 // "load and exec address is set to &H0000.\n"
178 // "Use offset to load to any address. EX: LOADM\"CASS:\",&H6000 will load starting to &H6000\n"
179 // );
180 // return -1;
181 // }
182 unsigned short start=0x8100;
183 unsigned short size;
184 unsigned short runaddr=0x8100;
185 FILE *fr,*fw;
186 char nome[12];
187 char nomecod[12];
188 char source[20];
189 char destin[100];
190
191 // Rename the output file into a temporary filename
192 char temporaryFileName[MAX_TEMPORARY_STORAGE];
193 sprintf(temporaryFileName, "%s.bin", get_temporary_filename( _environment ) );
194
195 remove( temporaryFileName );
196
197 BUILD_SAFE_MOVE( _environment, _environment->exeFileName, temporaryFileName );
198
199 fr=fopen(temporaryFileName,"rb");
200 if(!fr)
201 {
202 CRITICAL_CANNOT_OPEN_EXECUTABLE_FILE( temporaryFileName );
203 // printf("source file not found!!!!");
204 // return -1;
205 }
206
207 if(frmt(_environment, "main.exe",strlen("main.exe"),nome))
208 {
210 }
211
212 unsigned char byt;
213 unsigned short l;
214 unsigned char h[]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0x3c,0x5a};
215 unsigned char h1[]={1,0,0};
216 unsigned char h2[]={1,0};
217 unsigned char n[]={0x00,0x10};
218 unsigned char n1[]={0x02,0x00,0x00};
219 unsigned char end[]={0xff,0x02,0x00};
220 unsigned char z=0;
221 unsigned char u=1;
222 unsigned char preend[]={0xff,0x00,0x00};
223 fr=fopen(temporaryFileName,"rb");
224 // if(argv[3]==NULL)
225 // {
226 strcopy(destin,_environment->exeFileName);
227 // strcat(destin,".K7");
228 // }
229 // else
230 // strcopy(destin,argv[3]);
231 fw=fopen(destin,"wb");
232 fseek(fr, 0L, SEEK_END);
233 size = ftell(fr);
234 l=size;
235 nblock=floor((size+12)/254);
236 // printf("n blocks: %d\n",nblock);
237 // printf("size: %d\n",size);
238 rest=size+12-nblock*254;
239
240 // Fix #187
241 unsigned char restByte = (unsigned char)rest;
242
243 rewind(fr);
244 swapen(&size);
245 swapen(&start);
246 swapen(&runaddr);
247 fwrite(&h,sizeof(h),1,fw);
248 fwrite(&n,sizeof(n),1,fw);
249 fwrite(&nome,sizeof(nome)-1,1,fw);
250 fwrite(&n1,3,1,fw);
251 crc(nome,11,&sum);
252 crc(n1,1,&sum);
253
254 chk=cmpl(sum);
255 sum=0;
256 cnt=0;
257 fwrite(&chk,1,1,fw);
258 fwrite(&h,sizeof(h),1,fw);
259 if(nblock>0)
260 {
261 fwrite(&h1,sizeof(h1),1,fw);
262 }
263 else
264 {
265 fwrite(&u,sizeof(u),1,fw);
266 // Fix #187
267 fwrite(&restByte,1,1,fw);
268 fwrite(&z,sizeof(z),1,fw);
269 }
270 cnt++;
271 fwrite(&size,sizeof(size),1,fw);
272 fwrite(&start,sizeof(start),1,fw);
273 crc((unsigned char *)&size,sizeof(size),&sum);
274 crc((unsigned char *)&start,sizeof(start),&sum);
275 for (int i=0;i<l;i++)
276 {
277 if(cnt>253)
278 {
279 nblock--;
280 chk=cmpl(sum);
281 sum=0;
282 //printf("%d\n",chk);
283 fwrite(&chk,1,1,fw);
284 fwrite(&h,sizeof(h),1,fw);
285 if(nblock>0)
286 fwrite(&h2,sizeof(h2),1,fw);
287 else
288 {
289 fwrite(&u,sizeof(u),1,fw);
290 // Fix #187
291 fwrite(&restByte,1,1,fw);
292 // printf("*****\n");
293 }
294 cnt=0;
295
296 }
297 (void)!fread(&byt,1,1,fr);
298 crc(&byt,1,&sum);
299 (void)!fwrite(&byt,1,1,fw);
300 }
301 crc((unsigned char *)&preend,3,&sum);
302 fwrite(&preend,3,1,fw);
303 crc((unsigned char *)&runaddr,2,&sum);
304 fwrite(&runaddr,2,1,fw);
305 chk=cmpl(sum);
306 sum=0;
307 fwrite(&chk,1,1,fw);
308 fwrite(&h,sizeof(h),1,fw);
309 fwrite(&end,sizeof(end),1,fw);
310 // printf("chk %x rest %x block %d\n",chk,rest,nblock);
311
312 fclose(fr);
313 fclose(fw);
314
315 return 0;
316}
317
318void target_cleanup( Environment * _environment ) {
319
320 remove( _environment->asmFileName );
321
322 if ( _environment->analysis && _environment->listingFileName ) {
323 target_analysis( _environment );
324 }
325
326}
327
int system_call(Environment *_environment, char *_commandline)
Call an external executable.
char * get_temporary_filename(Environment *_environment)
void target_linkage(Environment *_environment)
Convert C64's assembly to executable.
Definition _build.c:327
void target_cleanup(Environment *_environment)
Definition _build.c:343
void target_analysis(Environment *_environment)
Definition _cleanup.c:68
int size
Definition _optimizer.c:678
Variable * point(Environment *_environment, char *_x, char *_y)
Definition point.c:46
int frmt(Environment *_environment, char *ptr, char size, char *out)
Definition _cleanup.c:59
void end(Environment *_environment)
Emit ASM code for END.
Definition end.c:91
int convertbintok7(Environment *_environment)
Definition _cleanup.c:120
char * listingFileName
Definition ugbc.h:2305
int analysis
Definition ugbc.h:2365
char * executerFileName
Definition ugbc.h:2315
OutputFileType outputFileType
Definition ugbc.h:2452
char * profileFileName
Definition ugbc.h:2310
char * asmFileName
Definition ugbc.h:2285
int profileCycles
Definition ugbc.h:2375
char * exeFileName
Definition ugbc.h:2290
int mo5_convertbintok7_original(Environment *_environment)
Definition _build.c:171
int to8_convertbintok7_original(Environment *_environment)
#define BUILD_TOOLCHAIN_ASM6809_GET_LISTING_FILE(_environment, listingFileName)
Definition ugbc.h:4872
@ OUTPUT_FILE_TYPE_K7_NEW
Definition ugbc.h:262
@ OUTPUT_FILE_TYPE_K7_ORIGINAL
Definition ugbc.h:261
#define BUILD_SAFE_REMOVE(_environment, filename)
Definition ugbc.h:4748
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
#define BUILD_TOOLCHAIN_ASM6809EXEC(_environment, flag, startingAddress, executableName, listingFileName)
Definition ugbc.h:4880
struct _Environment Environment
Structure of compilation environment.
#define CRITICAL_CANNOT_OPEN_EXECUTABLE_FILE(c)
Definition ugbc.h:3520
#define CRITICAL_BUILD_INVALID_FILENAME_K7(f)
Definition ugbc.h:3869
#define BUILD_TOOLCHAIN_ASM6809_GET_EXECUTABLE(_environment, executableName)
Definition ugbc.h:4855
#define BUILD_SAFE_MOVE(_environment, source, destination)
Definition ugbc.h:4751
#define CRITICAL_UNSUPPORTED_OUTPUT_FILE_TYPE(t)
Definition ugbc.h:3537
char OUTPUT_FILE_TYPE_AS_STRING[][16]
@ L
Definition ugbc.tab.h:508
char * strcopy(char *_dest, char *_source)