Browse Source

added bin2asm for font file source creation

patch
curt mayer 6 years ago
parent
commit
39f7403b78
  1. 2
      Tools/unix/Makefile
  2. 21
      Tools/unix/bin2asm/LICENSE
  3. 31
      Tools/unix/bin2asm/Makefile
  4. 5
      Tools/unix/bin2asm/README.md
  5. 103
      Tools/unix/bin2asm/bin2asm.c

2
Tools/unix/Makefile

@ -9,7 +9,7 @@ ifeq ($(UNAME), Darwin)
SUFFIX=osx
endif
SUBDIRS= bst uz80as zx cpmtools
SUBDIRS= bst uz80as zx cpmtools bin2asm
all:
@for i in $(SUBDIRS) ; do \

21
Tools/unix/bin2asm/LICENSE

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2016 ipatix
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

31
Tools/unix/bin2asm/Makefile

@ -0,0 +1,31 @@
CC = gcc
STRIP = strip
CFLAGS = -Werror -Wall -Wextra -Wconversion -O2 -D NDEBUG
BINARY = bin2asm
DEST := ../../$(shell uname)
SRC_FILES = $(wildcard *.c)
OBJ_FILES = $(SRC_FILES:.c=.o)
all: $(BINARY) $(DEST)
cp $(BINARY) $(DEST)
$(DEST):
mkdir -p $(DEST)
.PHONY: clean
clean:
rm -f $(OBJ_FILES)
clobber:
rm -f $(DEST)/$(BINARY) $(BINARY)
$(BINARY): $(OBJ_FILES)
$(CC) -o $@ $^ $(LIBS)
$(STRIP) -s $@
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS) $(IMPORT)

5
Tools/unix/bin2asm/README.md

@ -0,0 +1,5 @@
# bin2asm
Converts binary files to ASM compatible assembly files
Usage:
$ bin2asm hello.bin > hello.asm

103
Tools/unix/bin2asm/bin2asm.c

@ -0,0 +1,103 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
void die(const char msg[]) {
perror(msg);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]) {
for (int i = 1; i < argc; i++) {
// determine file size
FILE *input_file = fopen(argv[i], "rb");
if (!input_file) {
fprintf(stderr, "fail %s\n", argv[i]);
die("Couldn't open input file");
}
if (fseek(input_file, 0, SEEK_END) == -1) {
die("Error determining file size");
}
long file_size = ftell(input_file);
if (file_size == -1) {
die("Error determining file size (2)");
}
if (fseek(input_file, 0, SEEK_SET) == -1) {
die("Error determining file size (3)");
}
// get file name
char *name = basename(argv[i]);
char *dot = strrchr(name, '.');
if (dot) {
*dot = '\0';
}
// print header
printf(" .section .rodata\r\n");
printf(" .global %s\r\n", name);
printf(" .align 2\r\n\r\n");
printf("%s:\r\n\r\n", name);
// write lines
while (file_size > 0) {
size_t bytes_read = (file_size > 8) ? 8 : (size_t)file_size;
unsigned char data_buf[8];
size_t actual_read = fread(data_buf, 1, bytes_read, input_file);
if (actual_read != bytes_read) {
fprintf(stderr, "Error while reading file, only %d read instead of %d\n", (int)actual_read, (int)bytes_read);
if (feof(input_file))
fprintf(stderr, "Reached end of file\n");
if (ferror(input_file))
fprintf(stderr, "An unknown error occured while reading the file\n");
perror("ERROR");
exit(EXIT_FAILURE);
}
switch (bytes_read) {
case 1:
printf(" .byte 0x%02X\r\n",
data_buf[0]);
break;
case 2:
printf(" .byte 0x%02X, 0x%02X\r\n",
data_buf[0], data_buf[1]);
break;
case 3:
printf(" .byte 0x%02X, 0x%02X, 0x%02X\r\n",
data_buf[0], data_buf[1], data_buf[2]);
break;
case 4:
printf(" .byte 0x%02X, 0x%02X, 0x%02X, 0x%02X\r\n",
data_buf[0], data_buf[1], data_buf[2], data_buf[3]);
break;
case 5:
printf(" .byte 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X\r\n",
data_buf[0], data_buf[1], data_buf[2], data_buf[3], data_buf[4]);
break;
case 6:
printf(" .byte 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X\r\n",
data_buf[0], data_buf[1], data_buf[2], data_buf[3], data_buf[4], data_buf[5]);
break;
case 7:
printf(" .byte 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X\r\n",
data_buf[0], data_buf[1], data_buf[2], data_buf[3], data_buf[4], data_buf[5], data_buf[6]);
break;
case 8:
printf(" .byte 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X\r\n",
data_buf[0], data_buf[1], data_buf[2], data_buf[3], data_buf[4], data_buf[5], data_buf[6], data_buf[7]);
break;
default:
fprintf(stderr, "Invalid program state\n");
exit(EXIT_FAILURE);
}
file_size -= (long)bytes_read;
}
printf("\r\n");
if (fclose(input_file)) {
die("Error while closing file");
}
}
return 0;
}
Loading…
Cancel
Save