1 | /*****************************************************************************/ |
2 | /* */ |
3 | /* segment.c */ |
4 | /* */ |
5 | /* Segment handling for da65 */ |
6 | /* */ |
7 | /* */ |
8 | /* */ |
9 | /* (C) 2007-2014, Ullrich von Bassewitz */ |
10 | /* Roemerstrasse 52 */ |
11 | /* D-70794 Filderstadt */ |
12 | /* EMail: uz@cc65.org */ |
13 | /* */ |
14 | /* */ |
15 | /* This software is provided 'as-is', without any expressed or implied */ |
16 | /* warranty. In no event will the authors be held liable for any damages */ |
17 | /* arising from the use of this software. */ |
18 | /* */ |
19 | /* Permission is granted to anyone to use this software for any purpose, */ |
20 | /* including commercial applications, and to alter it and redistribute it */ |
21 | /* freely, subject to the following restrictions: */ |
22 | /* */ |
23 | /* 1. The origin of this software must not be misrepresented; you must not */ |
24 | /* claim that you wrote the original software. If you use this software */ |
25 | /* in a product, an acknowledgment in the product documentation would be */ |
26 | /* appreciated but is not required. */ |
27 | /* 2. Altered source versions must be plainly marked as such, and must not */ |
28 | /* be misrepresented as being the original software. */ |
29 | /* 3. This notice may not be removed or altered from any source */ |
30 | /* distribution. */ |
31 | /* */ |
32 | /*****************************************************************************/ |
33 | |
34 | |
35 | |
36 | #include <string.h> |
37 | |
38 | /* common */ |
39 | #include "addrsize.h" |
40 | #include "xmalloc.h" |
41 | |
42 | /* da65 */ |
43 | #include "attrtab.h" |
44 | #include "segment.h" |
45 | |
46 | |
47 | |
48 | /*****************************************************************************/ |
49 | /* Data */ |
50 | /*****************************************************************************/ |
51 | |
52 | |
53 | |
54 | /* Hash definitions */ |
55 | #define HASH_SIZE 53 |
56 | |
57 | /* Segment definition */ |
58 | typedef struct Segment Segment; |
59 | struct Segment { |
60 | Segment* NextStart; /* Pointer to next segment */ |
61 | unsigned long Start; |
62 | unsigned AddrSize; |
63 | char Name[1]; /* Name, dynamically allocated */ |
64 | }; |
65 | |
66 | /* Table containing the segments. A segment is inserted using its hash |
67 | ** value. Collisions are handled by single-linked lists. |
68 | */ |
69 | static Segment* StartTab[HASH_SIZE]; /* Table containing segment starts */ |
70 | |
71 | |
72 | |
73 | /*****************************************************************************/ |
74 | /* Code */ |
75 | /*****************************************************************************/ |
76 | |
77 | |
78 | |
79 | void AddAbsSegment (unsigned Start, unsigned End, const char* Name) |
80 | /* Add an absolute segment to the segment table */ |
81 | { |
82 | /* Get the length of the name */ |
83 | unsigned Len = strlen (Name); |
84 | |
85 | /* Create a new segment */ |
86 | Segment* S = xmalloc (sizeof (Segment) + Len); |
87 | |
88 | /* Fill in the data */ |
89 | S->Start = Start; |
90 | S->AddrSize = ADDR_SIZE_ABS; |
91 | memcpy (S->Name, Name, Len + 1); |
92 | |
93 | /* Insert the segment into the hash table */ |
94 | S->NextStart = StartTab[Start % HASH_SIZE]; |
95 | StartTab[Start % HASH_SIZE] = S; |
96 | |
97 | /* Mark start and end of the segment */ |
98 | MarkAddr (Start, atSegmentStart); |
99 | MarkAddr (End, atSegmentEnd); |
100 | |
101 | /* Mark the addresses within the segment */ |
102 | MarkRange (Start, End, atSegment); |
103 | } |
104 | |
105 | |
106 | |
107 | char* GetSegmentStartName (unsigned Addr) |
108 | /* Return the name of the segment which starts at the given address */ |
109 | { |
110 | Segment* S = StartTab[Addr % HASH_SIZE]; |
111 | |
112 | /* Search the collision list for the exact address */ |
113 | while (S != 0) { |
114 | if (S->Start == Addr) { |
115 | return S->Name; |
116 | } |
117 | S = S->NextStart; |
118 | } |
119 | |
120 | return 0; |
121 | } |
122 | |
123 | |
124 | |
125 | unsigned GetSegmentAddrSize (unsigned Addr) |
126 | /* Return the address size of the segment which starts at the given address */ |
127 | { |
128 | Segment* S = StartTab[Addr % HASH_SIZE]; |
129 | |
130 | /* Search the collision list for the exact address */ |
131 | while (S != 0) { |
132 | if (S->Start == Addr) { |
133 | return S->AddrSize; |
134 | } |
135 | S = S->NextStart; |
136 | } |
137 | |
138 | return 0; |
139 | } |
140 | |