1 /*****************************************************************************/
5 /* ROM plugin for the sim65 6502 simulator */
9 /* (C) 2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
46 /*****************************************************************************/
48 /*****************************************************************************/
52 static int InitChip (const struct SimData* Data);
53 /* Initialize the chip, return an error code */
55 static void* CreateInstance (unsigned Addr, unsigned Range, void* CfgInfo);
56 /* Create a new chip instance */
58 static void DestroyInstance (void* Data);
59 /* Destroy a chip instance */
61 static void WriteCtrl (void* Data, unsigned Offs, unsigned char Val);
62 /* Write control data */
64 static void Write (void* Data, unsigned Offs, unsigned char Val);
67 static unsigned char ReadCtrl (void* Data, unsigned Offs);
68 /* Read control data */
70 static unsigned char Read (void* Data, unsigned Offs);
75 /*****************************************************************************/
77 /*****************************************************************************/
81 /* Control data passed to the main program */
82 static const struct ChipData CData[1] = {
84 "ROM", /* Name of the chip */
85 CHIPDATA_TYPE_CHIP, /* Type of the chip */
86 CHIPDATA_VER_MAJOR, /* Version information */
89 /* -- Exported functions -- */
100 /* The SimData pointer we get when InitChip is called */
101 static const SimData* Sim;
103 /* Data for one ROM instance */
104 typedef struct InstanceData InstanceData;
105 struct InstanceData {
106 unsigned BaseAddr; /* Base address */
107 unsigned Range; /* Memory range */
108 unsigned char* Mem; /* The memory itself */
113 /*****************************************************************************/
114 /* Exported function */
115 /*****************************************************************************/
119 int GetChipData (const ChipData** Data, unsigned* Count)
121 /* Pass the control structure to the caller */
123 *Count = sizeof (CData) / sizeof (CData[0]);
125 /* Call was successful */
131 /*****************************************************************************/
133 /*****************************************************************************/
137 static int InitChip (const struct SimData* Data)
138 /* Initialize the chip, return an error code */
140 /* Remember the pointer */
143 /* Always successful */
149 static void* CreateInstance (unsigned Addr, unsigned Range, void* CfgInfo)
150 /* Create a new chip instance */
155 /* Allocate a new instance structure */
156 InstanceData* D = Sim->Malloc (sizeof (InstanceData));
158 /* Initialize the structure, allocate RAM and attribute memory */
161 D->Mem = Sim->Malloc (Range);
163 /* We must have a "file" attribute. Get it. */
164 if (Sim->GetCfgStr (CfgInfo, "file", &Name) == 0) {
165 /* Attribute not found */
166 Sim->Error ("Attribute `file' missing"); /* ### */
169 /* Open the file with the given name */
170 F = fopen (Name, "rb");
172 Sim->Error ("Cannot open `%s': %s", Name, strerror (errno));
175 /* Read the file into the memory */
176 if (fread (D->Mem, 1, D->Range, F) != D->Range) {
177 Sim->Warning ("Cannot read %u bytes from file `%s'", D->Range, Name);
183 /* Free the file name */
186 /* Done, return the instance data */
192 static void DestroyInstance (void* Data)
193 /* Destroy a chip instance */
195 /* Cast the data pointer */
196 InstanceData* D = (InstanceData*) Data;
198 /* Free the ROM memory */
201 /* Free the instance data itself */
207 static void WriteCtrl (void* Data, unsigned Offs, unsigned char Val)
208 /* Write control data */
210 /* Cast the data pointer */
211 InstanceData* D = (InstanceData*) Data;
219 static void Write (void* Data, unsigned Offs, unsigned char Val)
220 /* Write user data */
222 /* Cast the data pointer */
223 InstanceData* D = (InstanceData*) Data;
225 /* Print a warning */
226 Sim->Break ("Writing to write protected memory at $%04X (value = $%02X)",
227 D->BaseAddr+Offs, Val);
232 static unsigned char ReadCtrl (void* Data, unsigned Offs)
233 /* Read control data */
235 /* Cast the data pointer */
236 InstanceData* D = (InstanceData*) Data;
238 /* Read the cell and return the value */
244 static unsigned char Read (void* Data, unsigned Offs)
247 /* Cast the data pointer */
248 InstanceData* D = (InstanceData*) Data;
250 /* Read the cell and return the value */