@@ -17,11 +17,12 @@ struct Segment {
17
17
struct RootAS {
18
18
AddressSpace as ;
19
19
Segment rom_seg ;
20
+ Segment ram_seg ;
20
21
};
21
22
22
23
23
24
uint64_t
24
- rom_fetch_dword (AddressSpace * as , uint64_t addr ) {
25
+ mem_fetch_dword (AddressSpace * as , uint64_t addr ) {
25
26
Segment * s = (Segment * )as ;
26
27
int i ;
27
28
@@ -44,7 +45,7 @@ rom_fetch_dword(AddressSpace *as, uint64_t addr) {
44
45
}
45
46
46
47
uint32_t
47
- rom_fetch_word (AddressSpace * as , uint64_t addr ) {
48
+ mem_fetch_word (AddressSpace * as , uint64_t addr ) {
48
49
Segment * s = (Segment * )as ;
49
50
int i ;
50
51
@@ -60,10 +61,11 @@ rom_fetch_word(AddressSpace *as, uint64_t addr) {
60
61
| (s -> image [i + 2 ] << 16 )
61
62
| (s -> image [i + 3 ] << 24 )
62
63
);
63
- }
64
+ };
65
+
64
66
65
67
uint16_t
66
- rom_fetch_hword (AddressSpace * as , uint64_t addr ) {
68
+ mem_fetch_hword (AddressSpace * as , uint64_t addr ) {
67
69
Segment * s = (Segment * )as ;
68
70
int i ;
69
71
@@ -80,7 +82,7 @@ rom_fetch_hword(AddressSpace *as, uint64_t addr) {
80
82
}
81
83
82
84
uint8_t
83
- rom_fetch_byte (AddressSpace * as , uint64_t addr ) {
85
+ mem_fetch_byte (AddressSpace * as , uint64_t addr ) {
84
86
Segment * s = (Segment * )as ;
85
87
int i ;
86
88
@@ -117,25 +119,101 @@ rom_store_byte(AddressSpace *as, uint64_t addr, uint8_t datum) {
117
119
/* Do nothing, for we are ROM. */
118
120
}
119
121
122
+ void
123
+ ram_store_dword (AddressSpace * as , uint64_t addr , uint64_t datum ) {
124
+ Segment * s = (Segment * )as ;
125
+ int i ;
126
+
127
+ if (addr & 7 ) {
128
+ fprintf (stderr , "TODO: raise a misalignment trap to the processor\n" );
129
+ }
130
+
131
+ i = (int )(addr - s -> bottom );
132
+ s -> image [i ] = (uint8_t )(datum & 0xFF );
133
+ s -> image [i + 1 ] = (uint8_t )((datum >> 8 ) & 0xFF );
134
+ s -> image [i + 2 ] = (uint8_t )((datum >> 16 ) & 0xFF );
135
+ s -> image [i + 3 ] = (uint8_t )((datum >> 24 ) & 0xFF );
136
+ s -> image [i + 4 ] = (uint8_t )((datum >> 32 ) & 0xFF );
137
+ s -> image [i + 5 ] = (uint8_t )((datum >> 40 ) & 0xFF );
138
+ s -> image [i + 6 ] = (uint8_t )((datum >> 48 ) & 0xFF );
139
+ s -> image [i + 7 ] = (uint8_t )((datum >> 56 ) & 0xFF );
140
+ }
141
+
142
+ void
143
+ ram_store_word (AddressSpace * as , uint64_t addr , uint32_t datum ) {
144
+ Segment * s = (Segment * )as ;
145
+ int i ;
146
+
147
+ if (addr & 3 ) {
148
+ fprintf (stderr , "TODO: raise a misalignment trap to the processor\n" );
149
+ }
150
+
151
+ i = (int )(addr - s -> bottom );
152
+ s -> image [i ] = (uint8_t )(datum & 0xFF );
153
+ s -> image [i + 1 ] = (uint8_t )((datum >> 8 ) & 0xFF );
154
+ s -> image [i + 2 ] = (uint8_t )((datum >> 16 ) & 0xFF );
155
+ s -> image [i + 3 ] = (uint8_t )((datum >> 24 ) & 0xFF );
156
+ }
157
+
158
+ void
159
+ ram_store_hword (AddressSpace * as , uint64_t addr , uint16_t datum ) {
160
+ Segment * s = (Segment * )as ;
161
+ int i ;
162
+
163
+ if (addr & 1 ) {
164
+ fprintf (stderr , "TODO: raise a misalignment trap to the processor\n" );
165
+ }
166
+
167
+ i = (int )(addr - s -> bottom );
168
+ s -> image [i ] = (uint8_t )(datum & 0xFF );
169
+ s -> image [i + 1 ] = (uint8_t )((datum >> 8 ) & 0xFF );
170
+ }
171
+
172
+ void
173
+ ram_store_byte (AddressSpace * as , uint64_t addr , uint8_t datum ) {
174
+ Segment * s = (Segment * )as ;
175
+ int i ;
176
+
177
+ i = (int )(addr - s -> bottom );
178
+ s -> image [i ] = datum ;
179
+ }
180
+
120
181
struct IAddressSpace rom_segment_interface = {
121
- .fetch_dword = rom_fetch_dword ,
122
- .fetch_word = rom_fetch_word ,
123
- .fetch_hword = rom_fetch_hword ,
124
- .fetch_byte = rom_fetch_byte ,
182
+ .fetch_dword = mem_fetch_dword ,
183
+ .fetch_word = mem_fetch_word ,
184
+ .fetch_hword = mem_fetch_hword ,
185
+ .fetch_byte = mem_fetch_byte ,
125
186
.store_dword = rom_store_dword ,
126
187
.store_word = rom_store_word ,
127
188
.store_hword = rom_store_hword ,
128
189
.store_byte = rom_store_byte ,
129
190
};
130
191
131
192
193
+ struct IAddressSpace ram_segment_interface = {
194
+ .fetch_dword = mem_fetch_dword ,
195
+ .fetch_word = mem_fetch_word ,
196
+ .fetch_hword = mem_fetch_hword ,
197
+ .fetch_byte = mem_fetch_byte ,
198
+ .store_dword = ram_store_dword ,
199
+ .store_word = ram_store_word ,
200
+ .store_hword = ram_store_hword ,
201
+ .store_byte = ram_store_byte ,
202
+ };
203
+
204
+
132
205
uint64_t
133
206
root_fetch_dword (AddressSpace * as , uint64_t addr ) {
134
207
RootAS * root = (RootAS * )as ;
135
208
209
+ fprintf (stderr , "$%016llX @D\n" , addr );
210
+
136
211
if ((root -> rom_seg .bottom <= addr ) && (addr < root -> rom_seg .top ))
137
212
return root -> rom_seg .as .i -> fetch_dword (& root -> rom_seg .as , addr );
138
213
214
+ if ((root -> ram_seg .bottom <= addr ) && (addr < root -> ram_seg .top ))
215
+ return root -> ram_seg .as .i -> fetch_dword (& root -> ram_seg .as , addr );
216
+
139
217
fprintf (stderr , "TODO: raise access error trap to CPU\n" );
140
218
return 0xCCCCCCCCCCCCCCCC ;
141
219
}
@@ -144,9 +222,14 @@ uint32_t
144
222
root_fetch_word (AddressSpace * as , uint64_t addr ) {
145
223
RootAS * root = (RootAS * )as ;
146
224
225
+ fprintf (stderr , "$%016llX @W\n" , addr );
226
+
147
227
if ((root -> rom_seg .bottom <= addr ) && (addr < root -> rom_seg .top ))
148
228
return root -> rom_seg .as .i -> fetch_word (& root -> rom_seg .as , addr );
149
229
230
+ if ((root -> ram_seg .bottom <= addr ) && (addr < root -> ram_seg .top ))
231
+ return root -> ram_seg .as .i -> fetch_word (& root -> ram_seg .as , addr );
232
+
150
233
fprintf (stderr , "TODO: raise access error trap to CPU\n" );
151
234
return 0xCCCCCCCC ;
152
235
}
@@ -155,8 +238,13 @@ uint16_t
155
238
root_fetch_hword (AddressSpace * as , uint64_t addr ) {
156
239
RootAS * root = (RootAS * )as ;
157
240
241
+ fprintf (stderr , "$%016llX @H\n" , addr );
242
+
158
243
if ((root -> rom_seg .bottom <= addr ) && (addr < root -> rom_seg .top ))
159
- return root -> rom_seg .as .i -> fetch_dword (& root -> rom_seg .as , addr );
244
+ return root -> rom_seg .as .i -> fetch_hword (& root -> rom_seg .as , addr );
245
+
246
+ if ((root -> ram_seg .bottom <= addr ) && (addr < root -> ram_seg .top ))
247
+ return root -> ram_seg .as .i -> fetch_hword (& root -> ram_seg .as , addr );
160
248
161
249
fprintf (stderr , "TODO: raise access error trap to CPU\n" );
162
250
return 0xCCCC ;
@@ -166,8 +254,13 @@ uint8_t
166
254
root_fetch_byte (AddressSpace * as , uint64_t addr ) {
167
255
RootAS * root = (RootAS * )as ;
168
256
257
+ fprintf (stderr , "$%016llX @B\n" , addr );
258
+
169
259
if ((root -> rom_seg .bottom <= addr ) && (addr < root -> rom_seg .top ))
170
- return root -> rom_seg .as .i -> fetch_dword (& root -> rom_seg .as , addr );
260
+ return root -> rom_seg .as .i -> fetch_byte (& root -> rom_seg .as , addr );
261
+
262
+ if ((root -> ram_seg .bottom <= addr ) && (addr < root -> ram_seg .top ))
263
+ return root -> ram_seg .as .i -> fetch_byte (& root -> ram_seg .as , addr );
171
264
172
265
fprintf (stderr , "TODO: raise access error trap to CPU\n" );
173
266
return 0xCC ;
@@ -177,47 +270,75 @@ void
177
270
root_store_dword (AddressSpace * as , uint64_t addr , uint64_t datum ) {
178
271
RootAS * root = (RootAS * )as ;
179
272
273
+ fprintf (stderr , "$%016llX $%016llX !D\n" , datum , addr );
274
+
180
275
if ((root -> rom_seg .bottom <= addr ) && (addr < root -> rom_seg .top )) {
181
276
root -> rom_seg .as .i -> store_dword (& root -> rom_seg .as , addr , datum );
182
277
return ;
183
278
}
184
279
280
+ if ((root -> ram_seg .bottom <= addr ) && (addr < root -> ram_seg .top )) {
281
+ root -> ram_seg .as .i -> store_dword (& root -> ram_seg .as , addr , datum );
282
+ return ;
283
+ }
284
+
185
285
fprintf (stderr , "TODO: raise access error trap to CPU\n" );
186
286
}
187
287
188
288
void
189
289
root_store_word (AddressSpace * as , uint64_t addr , uint32_t datum ) {
190
290
RootAS * root = (RootAS * )as ;
191
291
292
+ fprintf (stderr , "$%08lX $%016llX !W\n" , datum , addr );
293
+
192
294
if ((root -> rom_seg .bottom <= addr ) && (addr < root -> rom_seg .top )) {
193
295
root -> rom_seg .as .i -> store_word (& root -> rom_seg .as , addr , datum );
194
296
return ;
195
297
}
196
298
299
+ if ((root -> ram_seg .bottom <= addr ) && (addr < root -> ram_seg .top )) {
300
+ root -> ram_seg .as .i -> store_word (& root -> ram_seg .as , addr , datum );
301
+ return ;
302
+ }
303
+
197
304
fprintf (stderr , "TODO: raise access error trap to CPU\n" );
198
305
}
199
306
200
307
void
201
308
root_store_hword (AddressSpace * as , uint64_t addr , uint16_t datum ) {
202
309
RootAS * root = (RootAS * )as ;
203
310
311
+ fprintf (stderr , "$%04X $%016llX !H\n" , datum , addr );
312
+
204
313
if ((root -> rom_seg .bottom <= addr ) && (addr < root -> rom_seg .top )) {
205
314
root -> rom_seg .as .i -> store_hword (& root -> rom_seg .as , addr , datum );
206
315
return ;
207
316
}
208
317
318
+ if ((root -> ram_seg .bottom <= addr ) && (addr < root -> ram_seg .top )) {
319
+ root -> ram_seg .as .i -> store_hword (& root -> ram_seg .as , addr , datum );
320
+ return ;
321
+ }
322
+
209
323
fprintf (stderr , "TODO: raise access error trap to CPU\n" );
210
324
}
211
325
212
326
void
213
327
root_store_byte (AddressSpace * as , uint64_t addr , uint8_t datum ) {
214
328
RootAS * root = (RootAS * )as ;
215
329
330
+ fprintf (stderr , "$%02X $%016llX !B\n" , datum , addr );
331
+
216
332
if ((root -> rom_seg .bottom <= addr ) && (addr < root -> rom_seg .top )) {
217
333
root -> rom_seg .as .i -> store_byte (& root -> rom_seg .as , addr , datum );
218
334
return ;
219
335
}
220
336
337
+ if ((root -> ram_seg .bottom <= addr ) && (addr < root -> ram_seg .top )) {
338
+ root -> ram_seg .as .i -> store_byte (& root -> ram_seg .as , addr , datum );
339
+ return ;
340
+ }
341
+
221
342
fprintf (stderr , "TODO: raise access error trap to CPU\n" );
222
343
}
223
344
@@ -236,26 +357,53 @@ void
236
357
dispose_root_address_space (RootAS * ras ) {
237
358
if (ras ) {
238
359
if (ras -> rom_seg .image ) free (ras -> rom_seg .image );
360
+ if (ras -> ram_seg .image ) free (ras -> ram_seg .image );
239
361
free (ras );
240
362
}
241
363
}
242
364
243
365
RootAS *
244
366
new_root_address_space (void ) {
245
367
RootAS * ras = (RootAS * )(malloc (sizeof (RootAS )));
368
+ FILE * fh ;
369
+ size_t size , actual ;
370
+
246
371
if (ras ) {
247
372
memset (ras , 0 , sizeof (RootAS ));
248
373
ras -> as .i = & root_interface ;
249
374
250
375
ras -> rom_seg .as .i = & rom_segment_interface ;
251
376
ras -> rom_seg .bottom = 0x0000000000000000 ;
252
377
ras -> rom_seg .top = 0x0000000000100000 ;
253
- ras -> rom_seg .image = malloc (ras -> rom_seg .top - ras -> rom_seg .bottom );
378
+ size = ras -> rom_seg .top - ras -> rom_seg .bottom ;
379
+ ras -> rom_seg .image = malloc (size );
254
380
if (!ras -> rom_seg .image ) {
255
381
fprintf (stderr , "cannot allocate 1MB ROM image\n" );
256
382
dispose_root_address_space (ras );
257
383
return NULL ;
258
384
}
385
+ memset (ras -> rom_seg .image , 0xAA , size );
386
+ fh = fopen ("rom-image.bin" , "rb" );
387
+ if (!fh ) {
388
+ fprintf (stderr , "cannot read rom-image.bin\n" );
389
+ dispose_root_address_space (ras );
390
+ return NULL ;
391
+ }
392
+ actual = fread (ras -> rom_seg .image , 1 , size , fh );
393
+ if (actual < size ) {
394
+ fprintf (stderr , "WARNING: Expected %d byte file, but got %d.\n" , size , actual );
395
+ }
396
+ fclose (fh );
397
+
398
+ ras -> ram_seg .as .i = & ram_segment_interface ;
399
+ ras -> ram_seg .bottom = 0x0000000040000000 ;
400
+ ras -> ram_seg .top = 0x0000000040100000 ;
401
+ ras -> ram_seg .image = malloc (ras -> ram_seg .top - ras -> ram_seg .bottom );
402
+ if (!ras -> ram_seg .image ) {
403
+ fprintf (stderr , "cannot allocate 1MB RAM image\n" );
404
+ dispose_root_address_space (ras );
405
+ return NULL ;
406
+ }
259
407
}
260
408
return ras ;
261
409
}
0 commit comments