mirror of
https://github.com/pnx/m16vm
synced 2026-06-17 03:50:03 +02:00
Initial commit
This commit is contained in:
commit
2ba17c52bd
16 changed files with 683 additions and 0 deletions
69
src/mm.c
Normal file
69
src/mm.c
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/* mm.c
|
||||
*
|
||||
* Copyright (C) 2012,2014 Henrik Hautakoski <henrik.hautakoski@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "mm.h"
|
||||
|
||||
#ifndef MEM_SIZE
|
||||
/* Set a default memory size. (2^16) */
|
||||
#define MEM_SIZE 65536
|
||||
#endif
|
||||
|
||||
#define EXCEPTION_STRING "Runtime exception: Memory address '%u' is out of bounds.\n"
|
||||
|
||||
int16_t* base_addr = NULL;
|
||||
uint8_t *memory = NULL;
|
||||
|
||||
static void check_bounds(uint16_t addr) {
|
||||
|
||||
if (addr > MEM_SIZE) {
|
||||
fprintf(stderr, EXCEPTION_STRING, addr);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void mm_init() {
|
||||
|
||||
base_addr = malloc(MEM_SIZE);
|
||||
memory = (uint8_t*) base_addr;
|
||||
}
|
||||
|
||||
void mm_exit() {
|
||||
|
||||
if (base_addr) {
|
||||
free(base_addr);
|
||||
base_addr = NULL;
|
||||
memory = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void mm_sw(uint16_t addr, int16_t value) {
|
||||
|
||||
check_bounds(addr);
|
||||
|
||||
base_addr[addr] = value;
|
||||
}
|
||||
|
||||
int16_t mm_lw(uint16_t addr) {
|
||||
|
||||
check_bounds(addr);
|
||||
|
||||
return base_addr[addr];
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue