v6502
The MOS 6502 Virtual Machine and Toolchain Infrastructure
mem.h
Go to the documentation of this file.
1 
4 /*
5  * Copyright (c) 2013 Daniel Loffgren
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to
9  * deal in the Software without restriction, including without limitation the
10  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11  * sell copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23  * IN THE SOFTWARE.
24  */
25 
26 #ifndef v6502_mem_h
27 #define v6502_mem_h
28 
29 #include <stdint.h>
30 #include <stddef.h>
31 
32 #ifndef YES
33 
34 #define YES 1
35 #endif
36 
37 #ifndef NO
38 
39 #define NO 0
40 #endif
41 
45 // Memory Starts
47 #define v6502_memoryStartWorkMemory 0x0000
48 
49 #define v6502_memoryStartStack 0x0100
50 
51 #define v6502_memoryStartPPURegisters 0x2000
52 
53 #define v6502_memoryStartAPURegisters 0x4000
54 
55 #define v6502_memoryStartExpansionRom 0x4020
56 
57 #define v6502_memoryStartSRAM 0x6000
58 
59 #define v6502_memoryStartPRGROM 0x8000
60 
61 #define v6502_memoryStartInterruptVectors 0xFFFA
62 
63 #define v6502_memoryStartCeiling 0xFFFF
64 
65 // Memory Blob Sizes
67 #define v6502_memorySizeWorkMemory 0x0800
68 
69 #define v6502_memorySizePPURegisters 0x0008
70 
71 #define v6502_memorySizeInterruptVectors (v6502_memoryStartCeiling - v6502_memoryStartInterruptVectors)
72 
73 // Vector Locations
75 #define v6502_memoryVectorNMILow 0xFFFA
76 
77 #define v6502_memoryVectorNMIHigh 0xFFFB
78 
79 #define v6502_memoryVectorResetLow 0xFFFC
80 
81 #define v6502_memoryVectorResetHigh 0xFFFD
82 
83 #define v6502_memoryVectorInterruptLow 0xFFFE
84 
85 #define v6502_memoryVectorInterruptHigh 0xFFFF
86 
89 #define BYTE_MAX 0xFF
90 
92 /* Forward declaration needed for circular dependency of mapping function and structures */
93 struct _v6502_memory;
98 typedef uint8_t (v6502_readFunction)(struct _v6502_memory *memory, uint16_t offset, int trap, void *context);
100 typedef void (v6502_writeFunction)(struct _v6502_memory *memory, uint16_t offset, uint8_t value, void *context);
101 
104 typedef struct {
106  uint16_t start;
108  size_t size;
114  void *context;
116 
119 typedef struct _v6502_memory {
121  uint8_t *bytes;
123  size_t size;
125  void(*fault_callback)(void *context, const char *reason);
131  size_t rangeCount;
139  void **contextCache;
140 } v6502_memory;
141 
145 v6502_memory *v6502_createMemory(size_t size);
147 void v6502_destroyMemory(v6502_memory *memory);
149 void v6502_loadExpansionRomIntoMemory(v6502_memory *memory, uint8_t *rom, uint16_t size);
156 int v6502_map(v6502_memory *memory, uint16_t start, size_t size, v6502_readFunction *read, v6502_writeFunction *write, void *context);
161 uint8_t v6502_read(v6502_memory *memory, uint16_t offset, int trap);
164 void v6502_write(v6502_memory *memory, uint16_t offset, uint8_t value);
168 int8_t v6502_signedValueOfByte(uint8_t byte);
170 uint8_t v6502_byteValueOfSigned(int8_t i);
173 #endif
v6502_readFunction ** readCache
Memory map read cache (See: Memory Map Cache)
Definition: mem.h:135
uint8_t v6502_read(v6502_memory *memory, uint16_t offset, int trap)
Read a byte from v6502_memory.
Definition: mem.c:162
uint8_t * bytes
Memory accessible as a byte-array.
Definition: mem.h:121
void v6502_loadExpansionRomIntoMemory(v6502_memory *memory, uint8_t *rom, uint16_t size)
Load a binary blob of expansion ROM into a given v6502_memory.
Definition: mem.c:187
uint8_t() v6502_readFunction(struct _v6502_memory *memory, uint16_t offset, int trap, void *context)
The function prototype for memory mapped accessors to be used by external virtual hardware...
Definition: mem.h:98
uint8_t v6502_byteValueOfSigned(int8_t i)
Convert a signed value to its raw byte.
Definition: mem.c:239
v6502_writeFunction * write
Memory access callback for writing bytes within this memory range.
Definition: mem.h:112
v6502_writeFunction ** writeCache
Memory map write cache (See: Memory Map Cache)
Definition: mem.h:137
uint16_t start
Start address of mapped range.
Definition: mem.h:106
size_t size
Byte-length of memory object.
Definition: mem.h:123
void ** contextCache
Memory map context cache (See: Memory Map Cache)
Definition: mem.h:139
int mapCacheEnabled
Memory Map Cache control
Definition: mem.h:133
v6502_memory * v6502_createMemory(size_t size)
Create v6502_memory, type is a size_t so that you can alloc an entire 64k with 0x1,0000.
Definition: mem.c:198
void() v6502_writeFunction(struct _v6502_memory *memory, uint16_t offset, uint8_t value, void *context)
The function prototype for memory mapped accessors to be used by external virtual hardware...
Definition: mem.h:100
int8_t v6502_signedValueOfByte(uint8_t byte)
Convert a raw byte to its signed value.
Definition: mem.c:235
size_t size
Byte-length of mapped range.
Definition: mem.h:108
void v6502_destroyMemory(v6502_memory *memory)
Destroy v6502_memory.
Definition: mem.c:218
int v6502_map(v6502_memory *memory, uint16_t start, size_t size, v6502_readFunction *read, v6502_writeFunction *write, void *context)
Map an address in v6502_memory.
Definition: mem.c:76
void * context
Context pointer, generally used to point to hardware data structures so that they can be referenced w...
Definition: mem.h:114
void * fault_context
Fault Callback Context.
Definition: mem.h:127
void v6502_write(v6502_memory *memory, uint16_t offset, uint8_t value)
Write a byte to v6502_memory.
Definition: mem.c:135
v6502_mappedRange * mappedRanges
Array of memory map ranges.
Definition: mem.h:129
size_t rangeCount
Number of memory map ranges in array.
Definition: mem.h:131
Virtual Memory Object.
Definition: mem.h:119
Memory Map Range Record.
Definition: mem.h:104
v6502_readFunction * read
Memory access callback for reading bytes within this memory range.
Definition: mem.h:110
v6502_mappedRange * v6502_mappedRangeForOffset(v6502_memory *memory, uint16_t offset)
Locate a v6502_mappedRange inside of v6502_memory, if it exists.
Definition: mem.c:43