aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-05-22 23:17:50 +0200
committerBad Diode <bd@badd10de.dev>2021-05-22 23:17:50 +0200
commit97d1c4246e167ab493175d890409c4154628760c (patch)
tree41e9b05c3e4a890cb7e8491d55173dcd84964ab8
parentefbee96caa1452486a007eeeabb5073aa9025dae (diff)
downloaduxngba-97d1c4246e167ab493175d890409c4154628760c.tar.gz
uxngba-97d1c4246e167ab493175d890409c4154628760c.zip
Include bin2carr on the tools directory
-rw-r--r--Makefile7
-rw-r--r--src/common.h1
-rw-r--r--tools/bin2carr/Makefile47
-rw-r--r--tools/bin2carr/src/main.c232
-rw-r--r--tools/bin2carr/src/shorthand.h35
5 files changed, 319 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index 89f64a2..d91466d 100644
--- a/Makefile
+++ b/Makefile
@@ -52,10 +52,10 @@ else
52 CFLAGS += $(RELEASE_CFLAGS) 52 CFLAGS += $(RELEASE_CFLAGS)
53endif 53endif
54 54
55main: $(BUILD_DIR) $(ROM) $(BIN) 55main: tools $(BUILD_DIR) $(ROM) $(BIN)
56 56
57$(ROM): 57$(ROM):
58 ../tools/bin2carr/build/bin2carr $(ROM_SRC) \ 58 ./tools/bin2carr/build/bin2carr $(ROM_SRC) \
59 -n uxn_rom \ 59 -n uxn_rom \
60 -e u16 \ 60 -e u16 \
61 -o $(ROM) 61 -o $(ROM)
@@ -81,3 +81,6 @@ run: main
81clean: 81clean:
82 rm $(ROM) 82 rm $(ROM)
83 rm -r $(BUILD_DIR) 83 rm -r $(BUILD_DIR)
84
85tools:
86 make -C tools/bin2carr
diff --git a/src/common.h b/src/common.h
index 08736ea..5d3f497 100644
--- a/src/common.h
+++ b/src/common.h
@@ -653,7 +653,6 @@ sound_volume(SoundChannel channels, u8 volume) {
653 653
654typedef u8 WaveBank[32]; 654typedef u8 WaveBank[32];
655 655
656// typedef u32 WaveBank[4];
657#define SOUND_WAVE_RAM ((WaveBank*)(MEM_IO + 0x90)) 656#define SOUND_WAVE_RAM ((WaveBank*)(MEM_IO + 0x90))
658 657
659typedef enum { 658typedef enum {
diff --git a/tools/bin2carr/Makefile b/tools/bin2carr/Makefile
new file mode 100644
index 0000000..a9ff816
--- /dev/null
+++ b/tools/bin2carr/Makefile
@@ -0,0 +1,47 @@
1.POSIX:
2.SUFFIXES:
3
4# Source code location and files to watch for changes.
5SRC_DIR := src
6SRC_MAIN := $(SRC_DIR)/main.c
7WATCH_SRC := $(wildcard $(SRC_DIR)/*.c)
8WATCH_SRC += $(wildcard $(SRC_DIR)/*.h)
9
10# Output library names and executables.
11BIN_NAME := bin2carr
12BUILD_DIR := build
13BIN := $(BUILD_DIR)/$(BIN_NAME)
14
15# Compiler and linker configuration.
16CC := gcc
17CFLAGS := -Wall -Wextra -pedantic -std=c99 -DBIN_NAME=\"$(BIN_NAME)\"
18LDFLAGS := -lm
19LDLIBS :=
20RELEASE_CFLAGS := -DNDEBUG -O2
21DEBUG_CFLAGS := -DDEBUG -g
22
23.PHONY: static clean run
24
25# Setup debug/release builds.
26# make clean && make <target> DEBUG=0
27# make clean && make <target> DEBUG=1
28DEBUG ?= 0
29ifeq ($(DEBUG), 1)
30 CFLAGS += $(DEBUG_CFLAGS)
31else
32 CFLAGS += $(RELEASE_CFLAGS)
33endif
34
35static: $(BUILD_DIR) $(BIN)
36
37$(BIN): $(SRC_MAIN) $(WATCH_SRC)
38 $(CC) $(CFLAGS) $(LDFLAGS) -o $(BIN) $(SRC_MAIN) $(LDLIBS)
39
40$(BUILD_DIR):
41 mkdir -p $(BUILD_DIR)
42
43run: $(BIN)
44 exec $(BIN)
45
46clean:
47 rm -r $(BUILD_DIR)
diff --git a/tools/bin2carr/src/main.c b/tools/bin2carr/src/main.c
new file mode 100644
index 0000000..699db61
--- /dev/null
+++ b/tools/bin2carr/src/main.c
@@ -0,0 +1,232 @@
1#include <getopt.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6#include "shorthand.h"
7
8typedef enum {
9 ELEM_U8, ELEM_S8, ELEM_U16, ELEM_S16,
10 ELEM_U32, ELEM_S32, ELEM_U64, ELEM_S64,
11} ElemSize;
12
13void
14write_array(FILE *file_in, FILE *file_out, char *arr_name, ElemSize elem_size) {
15 char *arr_type;
16 size_t n_elem = 0;
17 switch (elem_size) {
18 case ELEM_U8:
19 arr_type = "u8";
20 n_elem = 8;
21 break;
22 case ELEM_S8:
23 arr_type = "s8";
24 n_elem = 8;
25 break;
26 case ELEM_U16:
27 arr_type = "u16";
28 n_elem = 8;
29 break;
30 case ELEM_S16:
31 arr_type = "s16";
32 n_elem = 8;
33 break;
34 case ELEM_U32:
35 arr_type = "u32";
36 n_elem = 4;
37 break;
38 case ELEM_S32:
39 arr_type = "s32";
40 n_elem = 4;
41 break;
42 case ELEM_U64:
43 arr_type = "u64";
44 n_elem = 4;
45 break;
46 case ELEM_S64:
47 arr_type = "s64";
48 n_elem = 4;
49 break;
50 default:
51 arr_type = "u8";
52 n_elem = 8;
53 break;
54 }
55 fprintf(file_out, "const %s %s[] = {\n", arr_type, arr_name);
56 size_t n_read = 0;
57 size_t counter = 0;
58 do {
59 if (counter == 0) {
60 fprintf(file_out, " ");
61 }
62 switch (elem_size) {
63 case ELEM_U8:
64 case ELEM_S8: {
65 u8 elem;
66 n_read = fread(&elem, sizeof(u8), 1, file_in);
67 fprintf(file_out, "0x%02x,", elem);
68 break;
69 }
70 case ELEM_U16:
71 case ELEM_S16: {
72 u16 elem;
73 n_read = fread(&elem, sizeof(u16), 1, file_in);
74 fprintf(file_out, "0x%04x,", elem);
75 break;
76 }
77 case ELEM_U32:
78 case ELEM_S32: {
79 u32 elem;
80 n_read = fread(&elem, sizeof(u32), 1, file_in);
81 fprintf(file_out, "0x%08x,", elem);
82 break;
83 }
84 case ELEM_U64:
85 case ELEM_S64: {
86 u64 elem;
87 n_read = fread(&elem, sizeof(u64), 1, file_in);
88 fprintf(file_out, "0x%016lx,", elem);
89 break;
90 }
91 }
92
93 if (counter == n_elem - 1) {
94 fprintf(file_out, "\n");
95 counter = 0;
96 } else {
97 fprintf(file_out, " ");
98 counter++;
99 }
100 } while(n_read != 0);
101 if (counter != 0) {
102 fseek(file_out, -1, SEEK_CUR);
103 fprintf(file_out, "\n");
104 }
105 fprintf(file_out, "};\n");
106}
107
108void
109print_usage(void) {
110 printf("Usage: %s [options] <filename>\n", BIN_NAME);
111 printf("\n");
112 printf("\t-e <u16>\tSize of array elements.\n");
113 printf("\t-n <arr_name>\tName of the output array.\n");
114 printf("\t-o <out_file.c>\tPath to the output file. If blank, stdout will be used.\n");
115 printf("\n");
116}
117
118int
119main(int argc, char *argv[]) {
120 // Initialize default parameters.
121 char *in_file_path = NULL;
122 char *out_file_path = NULL;
123 char arr_name[256] = {0};
124 ElemSize elem_size = ELEM_U8;
125
126 // Process options.
127 int option;
128 while ((option = getopt(argc, argv, "e:n:o:")) != -1) {
129 switch (option) {
130 case 'e': {
131 // Element size.
132 char *size = optarg;
133 if (strcmp(size, "u8") == 0) {
134 elem_size = ELEM_U8;
135 } else if (strcmp(size, "s8") == 0) {
136 elem_size = ELEM_S8;
137 } else if (strcmp(size, "u16") == 0) {
138 elem_size = ELEM_U16;
139 } else if (strcmp(size, "s16") == 0) {
140 elem_size = ELEM_S16;
141 } else if (strcmp(size, "u32") == 0) {
142 elem_size = ELEM_U32;
143 } else if (strcmp(size, "s32") == 0) {
144 elem_size = ELEM_S32;
145 } else if (strcmp(size, "u64") == 0) {
146 elem_size = ELEM_U64;
147 } else if (strcmp(size, "s64") == 0) {
148 elem_size = ELEM_S64;
149 }
150 } break;
151 case 'n': {
152 // Array name.
153 char *ptr = optarg;
154 size_t k = 0;
155 while(*ptr) {
156 switch (*ptr) {
157 case '.':
158 case '/':
159 case '-':
160 case '\\':
161 ptr++;
162 break;
163 default: {
164 arr_name[k++] = *ptr++;
165 } break;
166 }
167 }
168 } break;
169 case 'o': {
170 // Output file.
171 out_file_path = optarg;
172 } break;
173 default: {
174 print_usage();
175 return EXIT_FAILURE;
176 } break;
177 }
178 }
179
180 // Get the path to the file to be exported.
181 if (optind != argc - 1) {
182 fprintf(stderr, "%s: No input file given.\n", BIN_NAME);
183 print_usage();
184 return EXIT_FAILURE;
185 }
186 in_file_path = argv[optind];
187
188 // Check if there is a default array name.
189 if (arr_name[0] == '\0') {
190 char *ptr = in_file_path;
191 size_t k = 0;
192 while(*ptr) {
193 switch (*ptr) {
194 case '.':
195 case '/':
196 case '-':
197 case '\\':
198 ptr++;
199 break;
200 default: {
201 arr_name[k++] = *ptr++;
202 } break;
203 }
204 }
205 }
206
207 // Try to open input file.
208 FILE *file_in = fopen(in_file_path, "r");
209 if (file_in == NULL) {
210 fprintf(stderr, "%s: can't open input file: %s\n", BIN_NAME,
211 in_file_path);
212 return EXIT_FAILURE;
213 }
214
215 // Write output to file.
216 if (out_file_path == NULL) {
217 write_array(file_in, stdout, arr_name, elem_size);
218 } else {
219 FILE *file_out = fopen(out_file_path, "w");
220 if (!file_out) {
221 fprintf(stderr, "%s: can't open output file: %s\n", BIN_NAME, out_file_path);
222 fclose(file_in);
223 return EXIT_FAILURE;
224 }
225 write_array(file_in, file_out, arr_name, elem_size);
226 fclose(file_out);
227 }
228
229 // Cleanup.
230 fclose(file_in);
231 return EXIT_SUCCESS;
232}
diff --git a/tools/bin2carr/src/shorthand.h b/tools/bin2carr/src/shorthand.h
new file mode 100644
index 0000000..9c2e2f0
--- /dev/null
+++ b/tools/bin2carr/src/shorthand.h
@@ -0,0 +1,35 @@
1#ifndef MIC_SHORTHAND_H
2#define MIC_SHORTHAND_H
3
4#include <assert.h>
5#include <stdbool.h>
6#include <stddef.h>
7#include <stdint.h>
8
9//
10// This simple header just typedefs the basic C define types to a shorter name,
11// loads the quality of life bool macro for _Bool and defines shorthand macros
12// for byte sizes. We need that the targeted architecture uses the floating
13// point representation as described on the IEEE-754 standard.
14//
15
16_Static_assert(sizeof(double) == 8, "no support for IEEE-754");
17_Static_assert(sizeof(float) == 4, "no support for IEEE-754");
18
19typedef uint8_t u8;
20typedef uint16_t u16;
21typedef uint32_t u32;
22typedef uint64_t u64;
23typedef int8_t s8;
24typedef int16_t s16;
25typedef int32_t s32;
26typedef int64_t s64;
27typedef float f32;
28typedef double f64;
29
30#define KB(N) ((u64)(N) * 1024)
31#define MB(N) ((u64)KB(N) * 1024)
32#define GB(N) ((u64)MB(N) * 1024)
33#define TB(N) ((u64)GB(N) * 1024)
34
35#endif // MIC_SHORTHAND_H