sdk: added `Bit2Bin` tool

This commit is contained in:
Ivan Tatarinov 2021-04-25 14:12:04 +03:00
parent d58ac43c82
commit 4ff18c37f4
5 changed files with 160 additions and 99 deletions

Binary file not shown.

1
sdk/bin/.gitignore vendored
View File

@ -11,3 +11,4 @@ fpad
fpoke fpoke
GenRom GenRom
AddItem AddItem
Bit2Bin

BIN
sdk/bin/Bit2Bin.exe Executable file

Binary file not shown.

View File

@ -0,0 +1,11 @@
SPDX-FileName: Bit2Bin.exe
SPDX-FileType: BINARY
SPDX-FileChecksum: SHA1: f1484d968816b72f7c1a246e11a468c175e8822a
SPDX-FileCopyrightText: Copyright (C) 2019-2021 Antonio Villena
SPDX-License-Identifier: GPL-3.0-only
SPDX-FileComment: Bit2Bin version 0.05 (2020-02-19) - strip .bit header and align binary to 16K.

View File

@ -1,99 +1,148 @@
#include <stdio.h> /*
#include <stdlib.h> * Bit2Bin - strip .bit header and align binary to 16K.
*
FILE *fi, *fo; * Copyright (C) 2019-2021 Antonio Villena
int i, length; *
unsigned char mem[0x4000]; * This program is free software: you can redistribute it and/or modify
unsigned short j; * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
int main(int argc, char *argv[]) { *
if( argc==1 ) * This program is distributed in the hope that it will be useful,
printf("\n" * but WITHOUT ANY WARRANTY; without even the implied warranty of
"Bit2Bin v0.05, strip .bit header and align binary to 16k, 2020-02-19\n\n" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
" Bit2Bin <input_file> <output_file>\n\n" * GNU General Public License for more details.
" <input_file> Input BIT file\n" *
" <output_file> Output BIN file\n\n" * You should have received a copy of the GNU General Public License
"All params are mandatory\n\n"), * along with this program. If not, see <https://www.gnu.org/licenses/>.
exit(0); *
if( argc!=3 ) * SPDX-FileCopyrightText: Copyright (C) 2019-2021 Antonio Villena
printf("\nInvalid number of parameters\n"), *
exit(-1); * SPDX-License-Identifier: GPL-3.0-only
fi= fopen(argv[1], "rb"); */
if( !fi )
printf("\nInput file not found: %s\n", argv[1]), #include <stdio.h>
exit(-1); #include <stdlib.h>
fseek(fi, 0, SEEK_END); #include <string.h>
i= ftell(fi);
fseek(fi, 0, SEEK_SET); #include <stdio.h>
fread(mem, 1, 2, fi); #include <stdlib.h>
i-= (j= mem[1]|mem[0]<<8)+4; #include <string.h>
fread(mem, 1, j+2, fi);
i-= (j= mem[j+1]|mem[j]<<8)+3; #define PROGRAM "Bit2Bin"
fread(mem, 1, j+3, fi); #define DESCRIPTION "strip .bit header and align binary to 16K."
i-= (j= mem[j+1]|mem[j]<<8)+3; #define VERSION "0.05 (2020-02-19)"
fread(mem, 1, j+3, fi); #define COPYRIGHT "Copyright (C) 2019-2021 Antonio Villena"
i-= (j= mem[j+1]|mem[j]<<8)+3; #define LICENSE \
fread(mem, 1, j+3, fi); "This program is free software: you can redistribute it and/or modify\n" \
i-= (j= mem[j+1]|mem[j]<<8)+3; "it under the terms of the GNU General Public License as published by\n" \
fread(mem, 1, j+3, fi); "the Free Software Foundation, version 3."
i-= (j= mem[j+1]|mem[j]<<8)+4; #define HOMEPAGE "https://github.com/zxdos/zxuno/"
fread(mem, 1, j+4, fi);
length= mem[j+3]|mem[j+2]<<8|mem[j+1]<<16|mem[j]<<24; FILE *fi, *fo;
if( i!=length ) int i, length;
printf("\nInvalid file length\n"), unsigned char mem[0x4000];
exit(-1); unsigned short j;
fo= fopen(argv[2], "wb+");
if( !fo ) void show_help() {
printf("\nCannot create output file: %s\n", argv[2]), printf(
exit(-1); PROGRAM " version " VERSION " - " DESCRIPTION "\n"
j= i>>14; COPYRIGHT "\n"
if( j>71 ){ LICENSE "\n"
for ( i= 0; i<72; i++ ) "Home page: " HOMEPAGE "\n"
fread(mem, 1, 0x4000, fi), "\n"
fwrite(mem, 1, 0x4000, fo); "Usage:\n"
fclose(fo); " " PROGRAM " <input_file> <output_file>\n"
argv[2][strlen(argv[2])-5]++; "\n"
if(argv[2][strlen(argv[2])-5]==':') " <input_file> Input BIT file\n"
if(strlen(argv[2])==9) " <output_file> Output BIN file\n"
argv[2]= "CORE10.ZX3"; "\n"
else "All parameters are mandatory.\n"
argv[2][4]++, );
argv[2][5]='0'; }
fo= fopen(argv[2], "wb+");
if( !fo ) int main(int argc, char *argv[]) {
printf("\nCannot create output file: %s\n", argv[2]), if( argc==1 )
exit(-1); show_help(),
for ( i= 0; i<j-72; i++ ) exit(0);
fread(mem, 1, 0x4000, fi), if( argc!=3 )
fwrite(mem, 1, 0x4000, fo); printf("Invalid number of parameters\n"),
memset(mem, 0, 0x4000); exit(-1);
fread(mem, 1, length&0x3fff, fi), fi= fopen(argv[1], "rb");
fwrite(mem, 1, 0x4000, fo); if( !fi )
memset(mem, 0, 0x4000); printf("Input file not found: %s\n", argv[1]),
for ( i= 0; i<143-j; i++ ) exit(-1);
fwrite(mem, 1, 0x4000, fo); fseek(fi, 0, SEEK_END);
} i= ftell(fi);
else{ fseek(fi, 0, SEEK_SET);
if( j ) fread(mem, 1, 2, fi);
for ( i= 0; i<j; i++ ) i-= (j= mem[1]|mem[0]<<8)+4;
fread(mem, 1, 0x4000, fi), fread(mem, 1, j+2, fi);
fwrite(mem, 1, 0x4000, fo); i-= (j= mem[j+1]|mem[j]<<8)+3;
memset(mem, 0, 0x4000); fread(mem, 1, j+3, fi);
fread(mem, 1, length&0x3fff, fi), i-= (j= mem[j+1]|mem[j]<<8)+3;
fwrite(mem, 1, 0x4000, fo); fread(mem, 1, j+3, fi);
memset(mem, 0, 0x4000); i-= (j= mem[j+1]|mem[j]<<8)+3;
if( j>48 ) fread(mem, 1, j+3, fi);
for ( i= 0; i<71-j; i++ ) i-= (j= mem[j+1]|mem[j]<<8)+3;
fwrite(mem, 1, 0x4000, fo); fread(mem, 1, j+3, fi);
else if( j>28 ) i-= (j= mem[j+1]|mem[j]<<8)+4;
for ( i= 0; i<48-j; i++ ) fread(mem, 1, j+4, fi);
fwrite(mem, 1, 0x4000, fo); length= mem[j+3]|mem[j+2]<<8|mem[j+1]<<16|mem[j]<<24;
else if( j>20 ) if( i!=length )
for ( i= 0; i<28-j; i++ ) printf("Invalid file length\n"),
fwrite(mem, 1, 0x4000, fo); exit(-1);
else fo= fopen(argv[2], "wb+");
for ( i= 0; i<20-j; i++ ) if( !fo )
fwrite(mem, 1, 0x4000, fo); printf("Cannot create output file: %s\n", argv[2]),
} exit(-1);
printf("\nFile generated successfully\n"); j= i>>14;
} if( j>71 ){
for ( i= 0; i<72; i++ )
fread(mem, 1, 0x4000, fi),
fwrite(mem, 1, 0x4000, fo);
fclose(fo);
argv[2][strlen(argv[2])-5]++;
if(argv[2][strlen(argv[2])-5]==':')
if(strlen(argv[2])==9)
argv[2]= "CORE10.ZX3";
else
argv[2][4]++,
argv[2][5]='0';
fo= fopen(argv[2], "wb+");
if( !fo )
printf("Cannot create output file: %s\n", argv[2]),
exit(-1);
for ( i= 0; i<j-72; i++ )
fread(mem, 1, 0x4000, fi),
fwrite(mem, 1, 0x4000, fo);
memset(mem, 0, 0x4000);
fread(mem, 1, length&0x3fff, fi),
fwrite(mem, 1, 0x4000, fo);
memset(mem, 0, 0x4000);
for ( i= 0; i<143-j; i++ )
fwrite(mem, 1, 0x4000, fo);
}
else{
if( j )
for ( i= 0; i<j; i++ )
fread(mem, 1, 0x4000, fi),
fwrite(mem, 1, 0x4000, fo);
memset(mem, 0, 0x4000);
fread(mem, 1, length&0x3fff, fi),
fwrite(mem, 1, 0x4000, fo);
memset(mem, 0, 0x4000);
if( j>48 )
for ( i= 0; i<71-j; i++ )
fwrite(mem, 1, 0x4000, fo);
else if( j>28 )
for ( i= 0; i<48-j; i++ )
fwrite(mem, 1, 0x4000, fo);
else if( j>20 )
for ( i= 0; i<28-j; i++ )
fwrite(mem, 1, 0x4000, fo);
else
for ( i= 0; i<20-j; i++ )
fwrite(mem, 1, 0x4000, fo);
}
printf("File `%s' successfully created\n", argv[2]);
}