diff options
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | src/common.h | 1 | ||||
-rw-r--r-- | tools/bin2carr/Makefile | 47 | ||||
-rw-r--r-- | tools/bin2carr/src/main.c | 232 | ||||
-rw-r--r-- | tools/bin2carr/src/shorthand.h | 35 |
5 files changed, 319 insertions, 3 deletions
@@ -52,10 +52,10 @@ else | |||
52 | CFLAGS += $(RELEASE_CFLAGS) | 52 | CFLAGS += $(RELEASE_CFLAGS) |
53 | endif | 53 | endif |
54 | 54 | ||
55 | main: $(BUILD_DIR) $(ROM) $(BIN) | 55 | main: 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 | |||
81 | clean: | 81 | clean: |
82 | rm $(ROM) | 82 | rm $(ROM) |
83 | rm -r $(BUILD_DIR) | 83 | rm -r $(BUILD_DIR) |
84 | |||
85 | tools: | ||
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 | ||
654 | typedef u8 WaveBank[32]; | 654 | typedef 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 | ||
659 | typedef enum { | 658 | typedef 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. | ||
5 | SRC_DIR := src | ||
6 | SRC_MAIN := $(SRC_DIR)/main.c | ||
7 | WATCH_SRC := $(wildcard $(SRC_DIR)/*.c) | ||
8 | WATCH_SRC += $(wildcard $(SRC_DIR)/*.h) | ||
9 | |||
10 | # Output library names and executables. | ||
11 | BIN_NAME := bin2carr | ||
12 | BUILD_DIR := build | ||
13 | BIN := $(BUILD_DIR)/$(BIN_NAME) | ||
14 | |||
15 | # Compiler and linker configuration. | ||
16 | CC := gcc | ||
17 | CFLAGS := -Wall -Wextra -pedantic -std=c99 -DBIN_NAME=\"$(BIN_NAME)\" | ||
18 | LDFLAGS := -lm | ||
19 | LDLIBS := | ||
20 | RELEASE_CFLAGS := -DNDEBUG -O2 | ||
21 | DEBUG_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 | ||
28 | DEBUG ?= 0 | ||
29 | ifeq ($(DEBUG), 1) | ||
30 | CFLAGS += $(DEBUG_CFLAGS) | ||
31 | else | ||
32 | CFLAGS += $(RELEASE_CFLAGS) | ||
33 | endif | ||
34 | |||
35 | static: $(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 | |||
43 | run: $(BIN) | ||
44 | exec $(BIN) | ||
45 | |||
46 | clean: | ||
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 | |||
8 | typedef enum { | ||
9 | ELEM_U8, ELEM_S8, ELEM_U16, ELEM_S16, | ||
10 | ELEM_U32, ELEM_S32, ELEM_U64, ELEM_S64, | ||
11 | } ElemSize; | ||
12 | |||
13 | void | ||
14 | write_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 | |||
108 | void | ||
109 | print_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 | |||
118 | int | ||
119 | main(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 | |||
19 | typedef uint8_t u8; | ||
20 | typedef uint16_t u16; | ||
21 | typedef uint32_t u32; | ||
22 | typedef uint64_t u64; | ||
23 | typedef int8_t s8; | ||
24 | typedef int16_t s16; | ||
25 | typedef int32_t s32; | ||
26 | typedef int64_t s64; | ||
27 | typedef float f32; | ||
28 | typedef 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 | ||