]> git.sur5r.net Git - cc65/blob - src/sim65/chip.c
Cosmetic change
[cc65] / src / sim65 / chip.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                    chip.c                                 */
4 /*                                                                           */
5 /*                        Interface for the chip plugins                     */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2002      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
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 "coll.h"
40 #include "print.h"
41 #include "xmalloc.h"
42
43 /* sim65 */
44 #include "chipdata.h"
45 #include "chiplib.h"
46 #include "error.h"
47 #include "chip.h"
48
49
50
51 /*****************************************************************************/
52 /*                                     Data                                  */
53 /*****************************************************************************/
54
55
56
57 /* Sorted list of all chip data structures */
58 static Collection Chips = STATIC_COLLECTION_INITIALIZER;
59
60 /* SimData instance */
61 static const SimData Sim65Data = {
62     1,                  /* MajorVersion */
63     1,                  /* MinorVersion */
64     xmalloc,            /* void* (*Malloc) (size_t Size); */
65     Warning,            /* void (*Warning) (const char* Format, ...); */
66     Error               /* void (*Error) (const char* Format, ...); */
67 };
68
69
70
71 /*****************************************************************************/
72 /*                               Helper functions                            */
73 /*****************************************************************************/
74
75
76
77 static int CmpChips (void* Data attribute ((unused)),
78                      const void* lhs, const void* rhs)
79 /* Compare function for CollSort */
80 {
81     /* Cast the object pointers */
82     const Chip* Left  = (const Chip*) rhs;
83     const Chip* Right = (const Chip*) lhs;
84
85     /* Do the compare */
86     return strcmp (Left->Data->ChipName, Right->Data->ChipName);
87 }
88
89
90
91 /*****************************************************************************/
92 /*                                   Code                                    */
93 /*****************************************************************************/
94
95
96
97 static Chip* NewChip (ChipLibrary* Library, const ChipData* Data)
98 /* Allocate a new chip structure, initialize and return it */
99 {
100     /* Allocate memory */
101     Chip* C = xmalloc (sizeof (Chip));
102
103     /* Initialize the fields */
104     C->Library   = Library;
105     C->Data      = Data;
106     C->Instances = EmptyCollection;
107
108     /* Return the structure */
109     return C;
110 }
111
112
113
114 #if 0
115 static void FreeChip (Chip* C)
116 /* ## Free the given chip structure */
117 {
118     /* Free the structure itself */
119     xfree (C);
120 }
121 #endif
122
123
124
125 void LoadChips (void)
126 /* Load all chips from all libraries */
127 {
128     unsigned I, J;
129
130     /* Walk through all libraries */
131     for (I = 0; I < CollCount (&ChipLibraries); ++I) {
132
133         /* Get the library entry */
134         ChipLibrary* L = CollAt (&ChipLibraries, I);
135
136         /* Create the chips */
137         for (J = 0; J < L->ChipCount; ++J) {
138
139             /* Get a pointer to the chip data */
140             const ChipData* Data = L->Data + J;
141
142             /* Check if the chip data has the correct version */
143             if (Data->MajorVersion != CHIPDATA_VER_MAJOR) {
144                 Warning ("Version mismatch for `%s' (%s), expected %u, got %u",
145                          Data->ChipName, L->LibName,
146                          CHIPDATA_VER_MAJOR, Data->MajorVersion);
147                 /* Ignore this chip */
148                 continue;
149             }
150
151             /* Generate a new chip and insert it into the collection */
152             CollAppend (&Chips, NewChip (L, Data));
153         }
154     }
155
156     /* Last act: Sort the chips by name */
157     CollSort (&Chips, CmpChips, 0);
158 }
159
160
161
162 const Chip* FindChip (const char* Name)
163 /* Find a chip by name. Returns the Chip data structure or NULL if the chip
164  * could not be found.
165  */
166 {
167     return 0;
168 }
169
170
171