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* InitInstance (unsigned Addr, unsigned Range, void* CfgInfo);
56 /* Initialize a new chip instance */
58 static void WriteCtrl (void* Data, unsigned Offs, unsigned char Val);
59 /* Write control data */
61 static void Write (void* Data, unsigned Offs, unsigned char Val);
64 static unsigned char ReadCtrl (void* Data, unsigned Offs);
65 /* Read control data */
67 static unsigned char Read (void* Data, unsigned Offs);
72 /*****************************************************************************/
74 /*****************************************************************************/
78 /* Control data passed to the main program */
79 static const struct ChipData ROMData[1] = {
81 "ROM", /* Name of the chip */
82 CHIPDATA_TYPE_CHIP, /* Type of the chip */
83 CHIPDATA_VER_MAJOR, /* Version information */
86 /* -- Exported functions -- */
96 /* The SimData pointer we get when InitChip is called */
97 static const SimData* Sim;
99 /* Data for one ROM instance */
100 typedef struct InstanceData InstanceData;
101 struct InstanceData {
102 unsigned BaseAddr; /* Base address */
103 unsigned Range; /* Memory range */
104 unsigned char* Mem; /* The memory itself */
109 /*****************************************************************************/
110 /* Exported function */
111 /*****************************************************************************/
115 int GetChipData (const ChipData** Data, unsigned* Count)
117 /* Pass the control structure to the caller */
119 *Count = sizeof (Data) / sizeof (Data[0]);
121 /* Call was successful */
127 /*****************************************************************************/
129 /*****************************************************************************/
133 static int InitChip (const struct SimData* Data)
134 /* Initialize the chip, return an error code */
136 /* Remember the pointer */
139 /* Always successful */
145 static void* InitInstance (unsigned Addr, unsigned Range, void* CfgInfo)
146 /* Initialize a new chip instance */
151 /* Allocate a new instance structure */
152 InstanceData* D = Sim->Malloc (sizeof (InstanceData));
154 /* Initialize the structure, allocate RAM and attribute memory */
157 D->Mem = Sim->Malloc (Range);
159 /* We must have a "file" attribute. Get it. */
160 if (Sim->GetCfgStr (CfgInfo, "file", &Name) == 0) {
161 /* Attribute not found */
162 Sim->Error ("Attribute `file' missing"); /* ### */
165 /* Open the file with the given name */
166 F = fopen (Name, "rb");
168 Sim->Error ("Cannot open `%s': %s", Name, strerror (errno));
171 /* Read the file into the memory */
172 if (fread (D->Mem, 1, D->Range, F) != D->Range) {
173 Sim->Warning ("Cannot read %u bytes from file `%s'", D->Range, Name);
179 /* Free the file name */
182 /* Done, return the instance data */
188 static void WriteCtrl (void* Data, unsigned Offs, unsigned char Val)
189 /* Write control data */
191 /* Cast the data pointer */
192 InstanceData* D = (InstanceData*) Data;
200 static void Write (void* Data, unsigned Offs, unsigned char Val)
201 /* Write user data */
203 /* Cast the data pointer */
204 InstanceData* D = (InstanceData*) Data;
206 /* Print a warning */
207 Sim->Break ("Writing to write protected memory at $%04X (value = $%02X)",
208 D->BaseAddr+Offs, Val);
213 static unsigned char ReadCtrl (void* Data, unsigned Offs)
214 /* Read control data */
216 /* Cast the data pointer */
217 InstanceData* D = (InstanceData*) Data;
219 /* Read the cell and return the value */
225 static unsigned char Read (void* Data, unsigned Offs)
228 /* Cast the data pointer */
229 InstanceData* D = (InstanceData*) Data;
231 /* Read the cell and return the value */