]> git.sur5r.net Git - cc65/blob - src/sim65/chips/rom.c
Have 'avail' not be dependent on 'all'.
[cc65] / src / sim65 / chips / rom.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                   rom.c                                   */
4 /*                                                                           */
5 /*                  ROM plugin for the sim65 6502 simulator                  */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2003      Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 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 <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40
41 /* sim65 */
42 #include "chipif.h"
43
44
45
46 /*****************************************************************************/
47 /*                                   Forwards                                */
48 /*****************************************************************************/
49
50
51
52 static int InitChip (const struct SimData* Data);
53 /* Initialize the chip, return an error code */
54
55 static void* CreateInstance (unsigned Addr, unsigned Range, void* CfgInfo);
56 /* Create a new chip instance */
57
58 static void DestroyInstance (void* Data);
59 /* Destroy a chip instance */
60
61 static void WriteCtrl (void* Data, unsigned Offs, unsigned char Val);
62 /* Write control data */
63
64 static void Write (void* Data, unsigned Offs, unsigned char Val);
65 /* Write user data */
66
67 static unsigned char ReadCtrl (void* Data, unsigned Offs);
68 /* Read control data */
69
70 static unsigned char Read (void* Data, unsigned Offs);
71 /* Read user data */
72
73
74
75 /*****************************************************************************/
76 /*                                     Data                                  */
77 /*****************************************************************************/
78
79
80
81 /* Control data passed to the main program */
82 static const struct ChipData CData[1] = {
83     {
84         "ROM",                  /* Name of the chip */
85         CHIPDATA_TYPE_CHIP,     /* Type of the chip */
86         CHIPDATA_VER_MAJOR,     /* Version information */
87         CHIPDATA_VER_MINOR,
88
89         /* -- Exported functions -- */
90         InitChip,
91         CreateInstance,
92         DestroyInstance,
93         WriteCtrl,
94         Write,
95         ReadCtrl,
96         Read
97     }
98 };
99
100 /* The SimData pointer we get when InitChip is called */
101 static const SimData* Sim;
102
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 */
109 };
110
111
112
113 /*****************************************************************************/
114 /*                               Exported function                           */
115 /*****************************************************************************/
116
117
118
119 int GetChipData (const ChipData** Data, unsigned* Count)
120 {
121     /* Pass the control structure to the caller */
122     *Data = CData;
123     *Count = sizeof (CData) / sizeof (CData[0]);
124
125     /* Call was successful */
126     return 0;
127 }
128
129
130
131 /*****************************************************************************/
132 /*                                     Code                                  */
133 /*****************************************************************************/
134
135
136
137 static int InitChip (const struct SimData* Data)
138 /* Initialize the chip, return an error code */
139 {
140     /* Remember the pointer */
141     Sim = Data;
142
143     /* Always successful */
144     return 0;
145 }
146
147
148
149 static void* CreateInstance (unsigned Addr, unsigned Range, void* CfgInfo)
150 /* Create a new chip instance */
151 {
152     char* Name;
153     FILE* F;
154
155     /* Allocate a new instance structure */
156     InstanceData* D = Sim->Malloc (sizeof (InstanceData));
157
158     /* Initialize the structure, allocate RAM and attribute memory */
159     D->BaseAddr = Addr;
160     D->Range    = Range;
161     D->Mem      = Sim->Malloc (Range);
162
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");        /* ### */
167     }
168
169     /* Open the file with the given name */
170     F = fopen (Name, "rb");
171     if (F == 0) {
172         Sim->Error ("Cannot open `%s': %s", Name, strerror (errno));
173     }
174
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);
178     }
179
180     /* Close the file */
181     fclose (F);
182
183     /* Free the file name */
184     Sim->Free (Name);
185
186     /* Done, return the instance data */
187     return D;
188 }
189
190
191
192 static void DestroyInstance (void* Data)
193 /* Destroy a chip instance */
194 {
195     /* Cast the data pointer */
196     InstanceData* D = (InstanceData*) Data;
197
198     /* Free the ROM memory */
199     Sim->Free (D->Mem);
200
201     /* Free the instance data itself */
202     free (D);
203 }
204
205
206
207 static void WriteCtrl (void* Data, unsigned Offs, unsigned char Val)
208 /* Write control data */
209 {
210     /* Cast the data pointer */
211     InstanceData* D = (InstanceData*) Data;
212
213     /* Do the write */
214     D->Mem[Offs] = Val;
215 }
216
217
218
219 static void Write (void* Data, unsigned Offs, unsigned char Val)
220 /* Write user data */
221 {
222     /* Cast the data pointer */
223     InstanceData* D = (InstanceData*) Data;
224
225     /* Print a warning */
226     Sim->Break ("Writing to write protected memory at $%04X (value = $%02X)",
227                 D->BaseAddr+Offs, Val);
228 }
229
230
231
232 static unsigned char ReadCtrl (void* Data, unsigned Offs)
233 /* Read control data */
234 {
235     /* Cast the data pointer */
236     InstanceData* D = (InstanceData*) Data;
237
238     /* Read the cell and return the value */
239     return D->Mem[Offs];
240 }
241
242
243
244 static unsigned char Read (void* Data, unsigned Offs)
245 /* Read user data */
246 {
247     /* Cast the data pointer */
248     InstanceData* D = (InstanceData*) Data;
249
250     /* Read the cell and return the value */
251     return D->Mem[Offs];
252 }
253
254
255