gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-support/ihxcheck/areas.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 <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 // #include <unistd.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11
12 #include "areas.h"
13
14 #define AREA_GROW_SIZE 500
15
16 extern bank_info banks[];
17
18 area_item * arealist;
19 uint32_t arealist_size;
20 uint32_t arealist_count;
21
22
23 uint32_t min(uint32_t a, uint32_t b) {
24 return (a < b) ? a : b;
25 }
26
27 uint32_t max(uint32_t a, uint32_t b) {
28 return (a > b) ? a : b;
29 }
30
31
32 // Returns size of overlap between two address ranges,
33 // if zero then no overlap
34 static uint32_t addrs_check_overlap(uint32_t a_start, uint32_t a_end, uint32_t b_start, uint32_t b_end) {
35
36 uint32_t size_used;
37
38 // Check whether the address range *doesn't* overlap
39 if ((b_start > a_end) || (b_end < a_start)) {
40 size_used = 0; // no overlap, size = 0
41 } else {
42 size_used = min(b_end, a_end) - max(b_start, a_start) + 1; // Calculate minimum overlap
43
44 uint32_t overlap_start = max(a_start, b_start);
45 uint32_t overlap_end = min(a_end, b_end);
46
47 // Flag a multiple write warning in the bank where the overflow ENDS
48 // Used later to check for overflows
49 banks[BANK_NUM(overlap_end)].had_multiple_write_warning = true;
50
51 printf("Warning: Multiple write of %5d bytes at 0x%x -> 0x%x writes:(0x%x -> 0x%x, 0x%x -> 0x%x)\n",
52 size_used, overlap_start, overlap_end, a_start, a_end, b_start, b_end);
53 }
54 return size_used;
55 }
56
57
58 void arealist_additem(area_item * p_area) {
59
60 arealist_count++;
61 // Grow array if needed
62 if (arealist_count == arealist_size) {
63 arealist_size += AREA_GROW_SIZE;
64 arealist = (area_item *)realloc(arealist, arealist_size * sizeof(area_item));
65 }
66
67 arealist[arealist_count-1] = *p_area;
68 }
69
70
71 void areas_init(void) {
72 arealist_count = 0;
73 arealist_size = AREA_GROW_SIZE;
74 arealist = (area_item *)malloc(arealist_size * sizeof(area_item));
75 }
76
77
78 void areas_cleanup(void) {
79 if (arealist)
80 free (arealist);
81 }
82
83
84 int areas_add(area_item * p_area) {
85
86 uint32_t c;
87 uint32_t size_used;
88 int ret = true; // default to success
89
90 // Check for overlap with existing areas
91 for (c = 0; c < arealist_count; c++) {
92
93 size_used = addrs_check_overlap(arealist[c].start, arealist[c].end,
94 p_area->start, p_area->end);
95 // Signal failure on any overlap
96 // (Keep looping to display all warnings though)
97 if (size_used > 0)
98 ret = false;
99 }
100
101 // Now add the area
102 arealist_additem(p_area);
103
104 return ret;
105 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.