gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-support/romusage/src/common.c
1 // This is free and unencumbered software released into the public domain.
2 // For more information, please refer to <https://unlicense.org>
3 // bbbbbr 2020
4
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <stdint.h>
10
11 #include "common.h"
12 #include "logging.h"
13 #include "rom_file.h"
14
15 bool banks_display_areas;
16 bool banks_display_headers;
17 bool banks_display_minigraph;
18 bool banks_display_largegraph;
19 bool option_compact_mode;
20 bool option_json_output;
21 bool option_summarized_mode;
22
23 // -B
24 unsigned int option_merged_banks;
25 // -F
26 unsigned int option_forced_display_max_bank_ROM;
27 unsigned int option_forced_display_max_bank_SRAM;
28
29 unsigned int option_platform;
30 bool option_display_asciistyle;
31 bool option_all_areas_exclusive;
32 bool option_quiet_mode;
33 bool option_suppress_duplicates;
34 bool option_error_on_warning;
35 bool option_hide_banners;
36 int option_input_source;
37 int option_area_sort;
38 int option_color_mode;
39 bool option_percentage_based_color;
40 uint32_t option_area_hide_size;
41 bool option_is_web_mode;
42
43 bool exit_error;
44
45 int banks_hide_count;
46 char banks_hide_list[BANKS_HIDE_SZ][DEFAULT_STR_LEN];
47
48
49 // Need a way to reset all options to default when running
50 // as wasm and called multiple times
51 void options_reset_all(void) {
52 banks_display_areas = false;
53 banks_display_headers = false;
54 banks_display_minigraph = false;
55 banks_display_largegraph = false;
56 option_compact_mode = false;
57 option_json_output = false;
58 option_summarized_mode = false;
59
60 // -B
61 option_merged_banks = OPT_MERGED_BANKS_NONE;
62 // -F
63 option_forced_display_max_bank_ROM = 0;
64 option_forced_display_max_bank_SRAM = 0;
65
66 option_platform = OPT_PLAT_GAMEBOY;
67 option_display_asciistyle = false;
68 option_all_areas_exclusive = false;
69 option_quiet_mode = false;
70 option_suppress_duplicates = true;
71 option_error_on_warning = false;
72 option_hide_banners = false;
73 option_input_source = OPT_INPUT_SRC_NONE;
74 option_area_sort = OPT_AREA_SORT_DEFAULT;
75 option_color_mode = OPT_PRINT_COLOR_OFF;
76 option_percentage_based_color = false;
77 option_area_hide_size = OPT_AREA_HIDE_SIZE_DEFAULT;
78 option_is_web_mode = true;
79
80 exit_error = false;
81
82 banks_hide_count = 0;
83 }
84
85
86 // Turn on/off display of areas within bank
87 void banks_output_show_areas(bool do_show) {
88 banks_display_areas = do_show;
89 }
90
91 // Turn on/off display of areas within bank
92 void banks_output_show_headers(bool do_show) {
93 banks_display_headers = do_show;
94 }
95
96 // Turn on/off display of mini usage graph per bank
97 void banks_output_show_minigraph(bool do_show) {
98 banks_display_minigraph = do_show;
99 }
100
101 // Turn on/off display of large usage graph per bank
102 void banks_output_show_largegraph(bool do_show) {
103 banks_display_largegraph = do_show;
104 }
105
106 // Turn on/off compact display mode
107 void set_option_show_compact(bool value) {
108 option_compact_mode = value;
109 }
110
111 // Turn on/off JSON output mode
112 void set_option_show_json(bool value) {
113 option_json_output = value;
114 }
115
116 // Turn on/off brief / summarized mode for banked regions
117 void set_option_summarized(bool value) {
118 option_summarized_mode = value;
119 }
120
121 // Turns on merged WRAM_0 + WRAM_1 display
122 void set_option_merged_banks(unsigned int value) {
123 option_merged_banks |= value;
124 }
125
126 // Sets console platform (changes memory map templates)
127 void set_option_platform(unsigned int value) {
128 option_platform = value;
129 }
130
131 // Turn on/off whether to use ascii style
132 // block characters for graphs
133 void set_option_display_asciistyle(bool value) {
134 option_display_asciistyle = value;
135 }
136
137 // Turn on/off whether all areas are exclusive,
138 // and whether to warn for any overlap
139 void set_option_all_areas_exclusive(bool value) {
140 option_all_areas_exclusive = value;
141 }
142
143 // Turn on/off quiet mode
144 void set_option_quiet_mode(bool value) {
145 option_quiet_mode = value;
146 }
147
148 // Turn on/off suppression of duplicates
149 void set_option_suppress_duplicates(bool value) {
150 option_suppress_duplicates = value;
151 }
152
153 // Turn on/off setting an error on exit for serious warnings encountered
154 void set_option_error_on_warning(bool value) {
155 option_error_on_warning = value;
156 }
157
158 // Turn on/off banners
159 void set_option_hide_banners(bool value) {
160 option_hide_banners = value;
161 }
162
163 // Input source file format
164 void set_option_input_source(int value) {
165 option_input_source = value;
166 }
167
168 // Area output sort order
169 void set_option_area_sort(int value) {
170 option_area_sort = value;
171 }
172
173 // Color output mode
174 void set_option_color_mode(int value) {
175 option_color_mode = value;
176 }
177
178 // Use Percentage based color
179 // Turns on color mode to default if not enabled
180 void set_option_percentage_based_color(bool value) {
181 option_percentage_based_color = value;
182
183 if (get_option_color_mode() == OPT_PRINT_COLOR_OFF)
184 set_option_color_mode(OPT_PRINT_COLOR_DEFAULT);
185 }
186
187 // Hide areas smaller than size
188 void set_option_area_hide_size(uint32_t value) {
189 option_area_hide_size = value;
190 }
191
192
193 // -sP : Custom Color Palette. Each colon separated entry is decimal VT100 color code
194 // -sP:DEFAULT:ROM:VRAM:SRAM:WRAM:HRAM
195 //
196 // Custom color scheme for output
197 bool set_option_displayed_bank_range(char * arg_str) {
198
199 #define MAX_SPLIT_WORDS 4
200 #define EXPECTED_COLS 3
201
202 int cols;
203 char * p_str;
204 char * p_words[MAX_SPLIT_WORDS];
205
206 // Split string into words separated by - and : chars
207 cols = 0;
208 p_str = strtok(arg_str,"-:");
209 while (p_str != NULL)
210 {
211 p_words[cols++] = p_str;
212 p_str = strtok(NULL, "-:");
213 if (cols >= MAX_SPLIT_WORDS) break;
214 }
215
216 if (cols == EXPECTED_COLS) {
217 option_forced_display_max_bank_ROM = strtol(p_words[1], NULL, 10);
218 option_forced_display_max_bank_SRAM = strtol(p_words[2], NULL, 10);
219 // printf("2:%s\n", p_words[2]);
220 return true;
221 } else
222 return false; // Signal failure
223 }
224
225
226
227 // Input source file format
228 int get_option_input_source(void) {
229 return option_input_source;
230 }
231
232 // Area output sort order
233 int get_option_area_sort(void) {
234 return option_area_sort;
235 }
236
237 // Color output mode
238 int get_option_color_mode(void) {
239 return option_color_mode;
240 }
241
242 // Use Percentage based color
243 bool get_option_percentage_based_color(void) {
244 return option_percentage_based_color;
245 }
246
247 // Turn on/off banners
248 bool get_option_hide_banners(void) {
249 return option_hide_banners;
250 }
251
252 // Hide areas smaller than size
253 uint32_t get_option_area_hide_size(void) {
254 return option_area_hide_size;
255 }
256
257 // Current platform (used for changing memory map templates)
258 unsigned int get_option_platform(void) {
259 return option_platform;
260 }
261
262 // Turn on/off whether to use ascii style
263 // block characters for graphs
264 bool get_option_display_asciistyle(void) {
265 return option_display_asciistyle;
266 }
267
268
269 // Add a substring for hiding banks
270 bool set_option_banks_hide_add(char * str_bank_hide_substring) {
271
272 if (banks_hide_count < BANKS_HIDE_SZ) {
273 snprintf(banks_hide_list[banks_hide_count], (DEFAULT_STR_LEN - 1), "%s", str_bank_hide_substring);
274 banks_hide_count++;
275 return true;
276 } else
277 log_error("Error: no bank hide string slots available\n");
278
279
280 return false;
281 }
282
283
284 // Set hex bytes treated as Empty in ROM files (.gb/etc) -b:HEXVAL:HEXVAL...
285 // -b:FF
286 // Value passed in has "-e" stripped off the front
287 bool set_option_binary_rom_empty_values(char * arg_str) {
288
289 #define MAX_ROMFILE_ENTRIES 256
290 #define MIN_EXPECTED_ENTRIES 1
291
292 int entries_found;
293 char * p_str;
294 char * p_words[MAX_ROMFILE_ENTRIES];
295
296 // Clear existing defaults
297 romfile_empty_value_table_clear();
298
299 // Split string into words separated by : chars
300 entries_found = 0;
301 p_str = strtok(arg_str,":");
302 while (p_str != NULL)
303 {
304 p_words[entries_found++] = p_str;
305 p_str = strtok(NULL, "-:");
306 if (entries_found >= MAX_ROMFILE_ENTRIES) break;
307 }
308
309 for (int c = 0; c < entries_found; c++)
310 romfile_empty_value_table_add_entry( (uint8_t)strtol(p_words[c], NULL, 16) );
311
312 if (entries_found >= MIN_EXPECTED_ENTRIES)
313 return true;
314 else
315 return false; // Signal failure
316 }
317
318
319 void set_exit_error(void) {
320 exit_error = true;
321 }
322
323 bool get_exit_error(void) {
324 return exit_error;
325 }
326
327
328 uint32_t round_up_power_of_2(uint32_t val) {
329
330 val--;
331 val |= val >> 1;
332 val |= val >> 2;
333 val |= val >> 4;
334 val |= val >> 8;
335 val |= val >> 16;
336 val++;
337
338 return val;
339 }
340
341
342 uint32_t min(uint32_t a, uint32_t b) {
343 return (a < b) ? a : b;
344 }
345
346
347 uint32_t max(uint32_t a, uint32_t b) {
348 return (a > b) ? a : b;
349 }
350
351
352 void set_option_is_web_mode(void) {
353 option_is_web_mode = true;
354 }
355
356 bool get_option_is_web_mode(void) {
357 return option_is_web_mode;
358 }
359
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.