]> git.sur5r.net Git - cc65/blob - src/sim65/chips/rom.c
eebfc74827a0cd54e5a740058696a8984cadb975
[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* InitInstance (unsigned Addr, unsigned Range, void* CfgInfo);
56 /* Initialize a new chip instance */
57
58 static void WriteCtrl (void* Data, unsigned Offs, unsigned char Val);
59 /* Write control data */
60
61 static void Write (void* Data, unsigned Offs, unsigned char Val);
62 /* Write user data */
63
64 static unsigned char ReadCtrl (void* Data, unsigned Offs);
65 /* Read control data */
66
67 static unsigned char Read (void* Data, unsigned Offs);
68 /* Read user data */
69
70
71
72 /*****************************************************************************/
73 /*                                     Data                                  */
74 /*****************************************************************************/
75
76
77
78 /* Control data passed to the main program */
79 static const struct ChipData ROMData[1] = {
80     {
81         "ROM",                  /* Name of the chip */
82         CHIPDATA_TYPE_CHIP,     /* Type of the chip */
83         CHIPDATA_VER_MAJOR,     /* Version information */
84         CHIPDATA_VER_MINOR,
85
86         /* -- Exported functions -- */
87         InitChip,
88         InitInstance,
89         WriteCtrl,
90         Write,
91         ReadCtrl,
92         Read
93     }
94 };
95
96 /* The SimData pointer we get when InitChip is called */
97 static const SimData* Sim;
98
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 */
105 };
106
107
108
109 /*****************************************************************************/
110 /*                               Exported function                           */
111 /*****************************************************************************/
112
113
114
115 int GetChipData (const ChipData** Data, unsigned* Count)
116 {
117     /* Pass the control structure to the caller */
118     *Data = ROMData;
119     *Count = sizeof (Data) / sizeof (Data[0]);
120
121     /* Call was successful */
122     return 0;
123 }
124
125
126
127 /*****************************************************************************/
128 /*                                     Code                                  */
129 /*****************************************************************************/
130
131
132
133 static int InitChip (const struct SimData* Data)
134 /* Initialize the chip, return an error code */
135 {
136     /* Remember the pointer */
137     Sim = Data;
138
139     /* Always successful */
140     return 0;
141 }
142
143
144
145 static void* InitInstance (unsigned Addr, unsigned Range, void* CfgInfo)
146 /* Initialize a new chip instance */
147 {
148     char* Name;
149     FILE* F;
150
151     /* Allocate a new instance structure */
152     InstanceData* D = Sim->Malloc (sizeof (InstanceData));
153
154     /* Initialize the structure, allocate RAM and attribute memory */
155     D->BaseAddr = Addr;
156     D->Range    = Range;
157     D->Mem      = Sim->Malloc (Range);
158
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");        /* ### */
163     }
164
165     /* Open the file with the given name */
166     F = fopen (Name, "rb");
167     if (F == 0) {
168         Sim->Error ("Cannot open `%s': %s", Name, strerror (errno));
169     }
170
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);
174     }
175
176     /* Close the file */
177     fclose (F);
178
179     /* Free the file name */
180     Sim->Free (Name);
181
182     /* Done, return the instance data */
183     return D;
184 }
185
186
187
188 static void WriteCtrl (void* Data, unsigned Offs, unsigned char Val)
189 /* Write control data */
190 {
191     /* Cast the data pointer */
192     InstanceData* D = (InstanceData*) Data;
193
194     /* Do the write */
195     D->Mem[Offs] = Val;
196 }
197
198
199
200 static void Write (void* Data, unsigned Offs, unsigned char Val)
201 /* Write user data */
202 {
203     /* Cast the data pointer */
204     InstanceData* D = (InstanceData*) Data;
205
206     /* Print a warning */
207     Sim->Break ("Writing to write protected memory at $%04X (value = $%02X)",
208                 D->BaseAddr+Offs, Val);
209 }
210
211
212
213 static unsigned char ReadCtrl (void* Data, unsigned Offs)
214 /* Read control data */
215 {
216     /* Cast the data pointer */
217     InstanceData* D = (InstanceData*) Data;
218
219     /* Read the cell and return the value */
220     return D->Mem[Offs];
221 }
222
223
224
225 static unsigned char Read (void* Data, unsigned Offs)
226 /* Read user data */
227 {
228     /* Cast the data pointer */
229     InstanceData* D = (InstanceData*) Data;
230
231     /* Read the cell and return the value */
232     return D->Mem[Offs];
233 }
234
235
236