1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
//
// bus/memorymap.c: System memory mapper.
//
// CEN64: Cycle-Accurate Nintendo 64 Emulator.
// Copyright (C) 2015, Tyler J. Stachecki.
//
// This file is subject to the terms and conditions defined in
// 'LICENSE', which is part of this source code package.
//
#include "common.h"
#include "memorymap.h"
static void fixup(struct memory_map *, struct memory_map_node *);
static void rotate_left(struct memory_map *, struct memory_map_node *);
static void rotate_right(struct memory_map *, struct memory_map_node *);
// Creates a new memory map.
void create_memory_map(struct memory_map *map) {
map->next_map_index = 1;
map->nil = map->mappings;
map->root = map->nil;
}
// Rebalances the tree after a node is inserted.
void fixup(struct memory_map *map, struct memory_map_node *node) {
struct memory_map_node *cur;
// Rebalance the whole tree as needed.
while (node->parent->color == MEMORY_MAP_RED) {
if (node->parent == node->parent->parent->left) {
cur = node->parent->parent->right;
// Case 1: We only need to update colors.
if (cur->color == MEMORY_MAP_RED) {
node->parent->color = MEMORY_MAP_BLACK;
cur->color = MEMORY_MAP_BLACK;
node->parent->parent->color = MEMORY_MAP_RED;
node = node->parent->parent;
}
else {
// Case 2: We need to perform a left rotation.
if (node == node->parent->right) {
node = node->parent;
rotate_left(map, node);
}
// Case 3: We need to perform a right rotation.
node->parent->color = MEMORY_MAP_BLACK;
node->parent->parent->color = MEMORY_MAP_RED;
rotate_right(map, node->parent->parent);
}
}
else {
cur = node->parent->parent->left;
// Case 1: We only need to update colors.
if (cur->color == MEMORY_MAP_RED) {
node->parent->color = MEMORY_MAP_BLACK;
cur->color = MEMORY_MAP_BLACK;
node->parent->parent->color = MEMORY_MAP_RED;
node = node->parent->parent;
}
else {
// Case 2: We need to perform a right rotation.
if (node == node->parent->left) {
node = node->parent;
rotate_right(map, node);
}
// Case 3: We need to perform a left rotation.
node->parent->color = MEMORY_MAP_BLACK;
node->parent->parent->color = MEMORY_MAP_RED;
rotate_left(map, node->parent->parent);
}
}
}
// When we rebalanced the tree, we might have accidentally colored
// the root red, so unconditionally color if back after rebalancing.
map->root->color = MEMORY_MAP_BLACK;
}
// Inserts a mapping into the tree.
int map_address_range(struct memory_map *map, uint32_t start, uint32_t length,
void *instance, memory_rd_function on_read, memory_wr_function on_write) {
struct memory_map_node *check = map->root;
struct memory_map_node *cur = map->nil;
uint32_t end = start + length - 1;
struct memory_map_node *new_node;
struct memory_mapping mapping;
// Make sure we have enough space in the map.
const unsigned num_mappings = sizeof(map->mappings) /
sizeof(map->mappings[0]) - 1;
if (unlikely(map->next_map_index >= num_mappings)) {
debug("map_address_range: Out of free mappings.");
return 1;
}
new_node = &map->mappings[map->next_map_index++];
// Walk down the tree.
while (check != map->nil) {
cur = check;
check = (start < cur->mapping.start)
? check->left : check->right;
}
// Insert the entry.
if (cur == map->nil)
map->root = new_node;
else if (start < cur->mapping.start)
cur->left = new_node;
else
cur->right = new_node;
new_node->left = map->nil;
new_node->right = map->nil;
new_node->parent = cur;
// Initialize the entry.
mapping.instance = instance;
mapping.on_read = on_read;
mapping.on_write = on_write;
mapping.end = end;
mapping.length = length;
mapping.start = start;
new_node->mapping = mapping;
// Rebalance the tree.
new_node->color = MEMORY_MAP_RED;
fixup(map, new_node);
return 0;
}
// Returns a pointer to a region given an address.
const struct memory_mapping *resolve_mapped_address(
const struct memory_map *map, uint32_t address) {
const struct memory_map_node *cur = map->root;
do {
if (address < cur->mapping.start)
cur = cur->left;
else if (address > cur->mapping.end)
cur = cur->right;
else
return &cur->mapping;
} while (cur != map->nil);
return NULL;
}
// Performs a left rotation centered at n.
static void rotate_left(struct memory_map *map, struct memory_map_node *n) {
struct memory_map_node *y = n->right;
// Turn y's left subtree into n's right subtree.
n->right = y->left;
if (y->left != map->nil)
y->left->parent = n;
// Link n's parent to y.
y->parent = n->parent;
if (n->parent == map->nil)
map->root = y;
else if (n == n->parent->left)
n->parent->left = y;
else
n->parent->right = y;
// Put n on y's left.
y->left = n;
n->parent = y;
}
// Performs a right rotation centered at n.
static void rotate_right(struct memory_map *map, struct memory_map_node *n) {
struct memory_map_node *y = n->left;
// Turn y's right subtree into n's left subtree.
n->left = y->right;
if (y->right != map->nil)
y->right->parent = n;
// Link n's parent to y.
y->parent = n->parent;
if (n->parent == map->nil)
map->root = y;
else if (n == n->parent->left)
n->parent->left = y;
else
n->parent->right = y;
// Put n on y's right.
y->right = n;
n->parent = y;
}
|