aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2023-08-30 11:09:34 +0200
committerBad Diode <bd@badd10de.dev>2023-08-30 11:09:34 +0200
commit6a01445ef315c60a5f40b842d24e1e0d34a5d070 (patch)
tree8c279272574cfe487392eceef43108b75776a36b /tools
parent033b0d78a5081e86fdfb70ca23fab627f8cf2ce0 (diff)
downloaduxngba-6a01445ef315c60a5f40b842d24e1e0d34a5d070.tar.gz
uxngba-6a01445ef315c60a5f40b842d24e1e0d34a5d070.zip
Fix k mode instructions
Diffstat (limited to 'tools')
-rw-r--r--tools/bin2carr/Makefile47
-rw-r--r--tools/bin2carr/src/main.c247
-rw-r--r--tools/bin2carr/src/shorthand.h35
3 files changed, 0 insertions, 329 deletions
diff --git a/tools/bin2carr/Makefile b/tools/bin2carr/Makefile
deleted file mode 100644
index a9ff816..0000000
--- a/tools/bin2carr/Makefile
+++ /dev/null
@@ -1,47 +0,0 @@
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
deleted file mode 100644
index 10ddcd8..0000000
--- a/tools/bin2carr/src/main.c
+++ /dev/null
@@ -1,247 +0,0 @@
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 if (n_read == 0) {
68 break;
69 }
70 fprintf(file_out, "0x%02x,", elem);
71 break;
72 }
73 case ELEM_U16:
74 case ELEM_S16: {
75 u16 elem;
76 n_read = fread(&elem, sizeof(u16), 1, file_in);
77 if (n_read == 0) {
78 break;
79 }
80 fprintf(file_out, "0x%04x,", elem);
81 break;
82 }
83 case ELEM_U32:
84 case ELEM_S32: {
85 u32 elem;
86 n_read = fread(&elem, sizeof(u32), 1, file_in);
87 if (n_read == 0) {
88 break;
89 }
90 fprintf(file_out, "0x%08x,", elem);
91 break;
92 }
93 case ELEM_U64:
94 case ELEM_S64: {
95 u64 elem;
96 n_read = fread(&elem, sizeof(u64), 1, file_in);
97 if (n_read == 0) {
98 break;
99 }
100 fprintf(file_out, "0x%016lx,", elem);
101 break;
102 }
103 }
104 if (n_read == 0) {
105 break;
106 }
107
108 if (counter == n_elem - 1) {
109 fprintf(file_out, "\n");
110 counter = 0;
111 } else {
112 fprintf(file_out, " ");
113 counter++;
114 }
115 } while(n_read != 0);
116 if (counter != 0) {
117 fseek(file_out, -1, SEEK_CUR);
118 fprintf(file_out, "\n");
119 }
120 fprintf(file_out, "};\n");
121}
122
123void
124print_usage(void) {
125 printf("Usage: %s [options] <filename>\n", BIN_NAME);
126 printf("\n");
127 printf("\t-e <u16>\tSize of array elements.\n");
128 printf("\t-n <arr_name>\tName of the output array.\n");
129 printf("\t-o <out_file.c>\tPath to the output file. If blank, stdout will be used.\n");
130 printf("\n");
131}
132
133int
134main(int argc, char *argv[]) {
135 // Initialize default parameters.
136 char *in_file_path = NULL;
137 char *out_file_path = NULL;
138 char arr_name[256] = {0};
139 ElemSize elem_size = ELEM_U8;
140
141 // Process options.
142 int option;
143 while ((option = getopt(argc, argv, "e:n:o:")) != -1) {
144 switch (option) {
145 case 'e': {
146 // Element size.
147 char *size = optarg;
148 if (strcmp(size, "u8") == 0) {
149 elem_size = ELEM_U8;
150 } else if (strcmp(size, "s8") == 0) {
151 elem_size = ELEM_S8;
152 } else if (strcmp(size, "u16") == 0) {
153 elem_size = ELEM_U16;
154 } else if (strcmp(size, "s16") == 0) {
155 elem_size = ELEM_S16;
156 } else if (strcmp(size, "u32") == 0) {
157 elem_size = ELEM_U32;
158 } else if (strcmp(size, "s32") == 0) {
159 elem_size = ELEM_S32;
160 } else if (strcmp(size, "u64") == 0) {
161 elem_size = ELEM_U64;
162 } else if (strcmp(size, "s64") == 0) {
163 elem_size = ELEM_S64;
164 }
165 } break;
166 case 'n': {
167 // Array name.
168 char *ptr = optarg;
169 size_t k = 0;
170 while(*ptr) {
171 switch (*ptr) {
172 case '.':
173 case '/':
174 case '-':
175 case '\\':
176 ptr++;
177 break;
178 default: {
179 arr_name[k++] = *ptr++;
180 } break;
181 }
182 }
183 } break;
184 case 'o': {
185 // Output file.
186 out_file_path = optarg;
187 } break;
188 default: {
189 print_usage();
190 return EXIT_FAILURE;
191 } break;
192 }
193 }
194
195 // Get the path to the file to be exported.
196 if (optind != argc - 1) {
197 fprintf(stderr, "%s: No input file given.\n", BIN_NAME);
198 print_usage();
199 return EXIT_FAILURE;
200 }
201 in_file_path = argv[optind];
202
203 // Check if there is a default array name.
204 if (arr_name[0] == '\0') {
205 char *ptr = in_file_path;
206 size_t k = 0;
207 while(*ptr) {
208 switch (*ptr) {
209 case '.':
210 case '/':
211 case '-':
212 case '\\':
213 ptr++;
214 break;
215 default: {
216 arr_name[k++] = *ptr++;
217 } break;
218 }
219 }
220 }
221
222 // Try to open input file.
223 FILE *file_in = fopen(in_file_path, "r");
224 if (file_in == NULL) {
225 fprintf(stderr, "%s: can't open input file: %s\n", BIN_NAME,
226 in_file_path);
227 return EXIT_FAILURE;
228 }
229
230 // Write output to file.
231 if (out_file_path == NULL) {
232 write_array(file_in, stdout, arr_name, elem_size);
233 } else {
234 FILE *file_out = fopen(out_file_path, "w");
235 if (!file_out) {
236 fprintf(stderr, "%s: can't open output file: %s\n", BIN_NAME, out_file_path);
237 fclose(file_in);
238 return EXIT_FAILURE;
239 }
240 write_array(file_in, file_out, arr_name, elem_size);
241 fclose(file_out);
242 }
243
244 // Cleanup.
245 fclose(file_in);
246 return EXIT_SUCCESS;
247}
diff --git a/tools/bin2carr/src/shorthand.h b/tools/bin2carr/src/shorthand.h
deleted file mode 100644
index 9c2e2f0..0000000
--- a/tools/bin2carr/src/shorthand.h
+++ /dev/null
@@ -1,35 +0,0 @@
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