ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
_cleanup_original.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#if defined(__pc128op__) || defined(__mo5__)
42
43// -------------------------------------------------------------------------
44// CONVERT BINARY TO K7 TAPE FILE
45// FOR THOMSON MO6 / PC128 OLIVETTI PRODEST
46//
47// This code was originally present in the repository
48// "PC 128 Olivetti Prodest (Thomson MO6) tools collection"
49// https://github.com/dinoflorenzi/MO5
50//
51// Code courtesy of Dino Florenzi (https://github.com/dinoflorenzi)
52// Adapted by Marco Spedaletti for ugBASIC
53// -------------------------------------------------------------------------
54
55#include<stdlib.h>
56#include<stdio.h>
57#include<math.h>
58
59static unsigned int sum=0;
60static unsigned char chk=0;
61static unsigned short l;
62static int nblock;
63static int rest;
64static int cnt=0;
65static int frmt( Environment * _environment, char * ptr, char size,char* out)
66{
67 memset(out,0x20,11);
68 char * point=".";
69 int k=0,i=0;
70 int p=strcspn(ptr,point);
71 if(p==size-1||p>8||size>12||p==0)
72 {
74 }
75 for(i=0;i<size;i++)
76 {
77 if(i==p)
78 k+=(8-p);
79 else
80 {
81 out[k]=ptr[i];
82 k++;
83 }
84
85 }
86 return 0;
87}
88
89// FIX #173
90#if ( defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN ) || \
91 defined(__BIG_ENDIAN__) || \
92 defined(__ARMEB__) || \
93 defined(__THUMBEB__) || \
94 defined(__AARCH64EB__) || \
95 defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__)
96
97 static void swapen(unsigned short *num)
98 {
99 return *num;
100 }
101
102#else
103
104 static void swapen(unsigned short *num)
105 {
106 unsigned short swapped;
107 swapped = (*num>>8) | (*num<<8);
108 *num=swapped;
109 }
110
111#endif
112
113static void crc ( unsigned char * data, int l,unsigned int * old)
114{
115
116 cnt+=l;
117 for (int i= 0;i<l;i++)
118 {
119 (*old)+=data[i];
120
121 }
122}
123
124static unsigned char cmpl(unsigned int n)
125{
126 return 0x100- ((unsigned char)n);
127}
128
130{
131 // if(argc==1)
132 // {
133 // printf("Missing arguments !!!!!!\n"
134 // "bin2k7 source pc128_filename K7_filename\n"
135 // "load and exec address is set to &H0000.\n"
136 // "Use offset to load to any address. EX: LOADM\"CASS:\",&H6000 will load starting to &H6000\n"
137 // );
138 // return -1;
139 // }
140 unsigned short start=0x3000;
141 unsigned short size;
142 unsigned short runaddr=0x3000;
143 FILE *fr,*fw;
144 char nome[12];
145 char nomecod[12];
146 char source[20];
147 char destin[100];
148
149 // Rename the output file into a temporary filename
150 char temporaryFileName[MAX_TEMPORARY_STORAGE];
151 sprintf(temporaryFileName, "%s.bin", get_temporary_filename( _environment ) );
152 remove( temporaryFileName );
153 BUILD_SAFE_MOVE( _environment, _environment->exeFileName, temporaryFileName );
154
155 fr=fopen(temporaryFileName,"rb");
156 if(!fr)
157 {
158 CRITICAL_CANNOT_OPEN_EXECUTABLE_FILE( temporaryFileName );
159 // printf("source file not found!!!!");
160 // return -1;
161 }
162
163 if(frmt(_environment, "main.exe",strlen("main.exe"),nome))
164 {
166 }
167
168 unsigned char byt;
169 unsigned short l;
170 unsigned char h[]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0x3c,0x5a};
171 unsigned char h1[]={1,0,0};
172 unsigned char h2[]={1,0};
173 unsigned char n[]={0x00,0x10};
174 unsigned char n1[]={0x02,0x00,0x00};
175 unsigned char end[]={0xff,0x02,0x00};
176 unsigned char z=0;
177 unsigned char u=1;
178 unsigned char preend[]={0xff,0x00,0x00};
179 fr=fopen(temporaryFileName,"rb");
180 // if(argv[3]==NULL)
181 // {
182 strcopy(destin,_environment->exeFileName);
183 // strcat(destin,".K7");
184 // }
185 // else
186 // strcopy(destin,argv[3]);
187 fw=fopen(destin,"wb");
188 fseek(fr, 0L, SEEK_END);
189 size = ftell(fr);
190 l=size;
191 nblock=floor((size+12)/254);
192 // printf("n blocks: %d\n",nblock);
193 // printf("size: %d\n",size);
194 rest=size+12-nblock*254;
195
196 // Fix #187
197 unsigned char restByte = (unsigned char)rest;
198
199 rewind(fr);
200 swapen(&size);
201 swapen(&start);
202 swapen(&runaddr);
203 fwrite(&h,sizeof(h),1,fw);
204 fwrite(&n,sizeof(n),1,fw);
205 fwrite(&nome,sizeof(nome)-1,1,fw);
206 fwrite(&n1,3,1,fw);
207 crc(nome,11,&sum);
208 crc(n1,1,&sum);
209
210 chk=cmpl(sum);
211 sum=0;
212 cnt=0;
213 fwrite(&chk,1,1,fw);
214 fwrite(&h,sizeof(h),1,fw);
215 if(nblock>0)
216 {
217 fwrite(&h1,sizeof(h1),1,fw);
218 }
219 else
220 {
221 fwrite(&u,sizeof(u),1,fw);
222 // Fix #187
223 fwrite(&restByte,1,1,fw);
224 fwrite(&z,sizeof(z),1,fw);
225 }
226 cnt++;
227 fwrite(&size,sizeof(size),1,fw);
228 fwrite(&start,sizeof(start),1,fw);
229 crc((unsigned char *)&size,sizeof(size),&sum);
230 crc((unsigned char *)&start,sizeof(start),&sum);
231 for (int i=0;i<l;i++)
232 {
233 if(cnt>253)
234 {
235 nblock--;
236 chk=cmpl(sum);
237 sum=0;
238 //printf("%d\n",chk);
239 fwrite(&chk,1,1,fw);
240 fwrite(&h,sizeof(h),1,fw);
241 if(nblock>0)
242 fwrite(&h2,sizeof(h2),1,fw);
243 else
244 {
245 fwrite(&u,sizeof(u),1,fw);
246 // Fix #187
247 fwrite(&restByte,1,1,fw);
248 // printf("*****\n");
249 }
250 cnt=0;
251
252 }
253 (void)!fread(&byt,1,1,fr);
254 crc(&byt,1,&sum);
255 fwrite(&byt,1,1,fw);
256 }
257 crc((unsigned char *)&preend,3,&sum);
258 fwrite(&preend,3,1,fw);
259 crc((unsigned char *)&runaddr,2,&sum);
260 fwrite(&runaddr,2,1,fw);
261 chk=cmpl(sum);
262 sum=0;
263 fwrite(&chk,1,1,fw);
264 fwrite(&h,sizeof(h),1,fw);
265 fwrite(&end,sizeof(end),1,fw);
266 // printf("chk %x rest %x block %d\n",chk,rest,nblock);
267
268 fclose(fr);
269 fclose(fw);
270
271 remove(temporaryFileName);
272
273 return 0;
274}
275
276#endif
char * get_temporary_filename(Environment *_environment)
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
int convertbintok7_original(Environment *_environment)
void end(Environment *_environment)
Emit ASM code for END.
Definition end.c:91
char * exeFileName
Definition ugbc.h:2290
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
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_SAFE_MOVE(_environment, source, destination)
Definition ugbc.h:4751
@ L
Definition ugbc.tab.h:508
char * strcopy(char *_dest, char *_source)