mirror of
https://github.com/pnx/m16vm
synced 2026-07-02 11:33:40 +02:00
vm/mm.c: remove base_addr variable, we can do the stuff with macros and pointer casts :)
This commit is contained in:
parent
67a272b715
commit
e92b7ee551
1 changed files with 11 additions and 10 deletions
21
vm/mm.c
21
vm/mm.c
|
|
@ -26,29 +26,30 @@
|
||||||
#define MEM_SIZE 65536
|
#define MEM_SIZE 65536
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Get a WORD aligned (16-bit) address.
|
||||||
|
// Casts the 8-bit pointer (memory) to 16-bit (WORD) and add offset.
|
||||||
|
#define addr_align_word(offset) \
|
||||||
|
(((uint16_t*) memory) + (offset))
|
||||||
|
|
||||||
#define EXCEPTION_STRING "Runtime exception: Memory address '%u' is out of bounds.\n"
|
#define EXCEPTION_STRING "Runtime exception: Memory address '%u' is out of bounds.\n"
|
||||||
|
|
||||||
int16_t* base_addr = NULL;
|
|
||||||
uint8_t *memory = NULL;
|
uint8_t *memory = NULL;
|
||||||
|
|
||||||
#define __check_bounds(addr) \
|
#define __check_bounds(addr) \
|
||||||
if ((addr) > MEM_SIZE) { \
|
if ((addr) + 1 > MEM_SIZE) { \
|
||||||
fprintf(stderr, EXCEPTION_STRING, addr);\
|
fprintf(stderr, EXCEPTION_STRING, addr);\
|
||||||
exit(1); \
|
exit(1); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void mm_init() {
|
void mm_init() {
|
||||||
|
|
||||||
base_addr = malloc(MEM_SIZE);
|
memory = (uint8_t*) malloc(MEM_SIZE);
|
||||||
memory = (uint8_t*) base_addr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void mm_exit() {
|
void mm_exit() {
|
||||||
|
|
||||||
if (base_addr) {
|
if (memory) {
|
||||||
free(base_addr);
|
free(memory);
|
||||||
base_addr = NULL;
|
|
||||||
memory = NULL;
|
memory = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -57,12 +58,12 @@ void mm_sw(uint16_t addr, int16_t value) {
|
||||||
|
|
||||||
__check_bounds(addr);
|
__check_bounds(addr);
|
||||||
|
|
||||||
base_addr[addr] = value;
|
*addr_align_word(addr) = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t mm_lw(uint16_t addr) {
|
int16_t mm_lw(uint16_t addr) {
|
||||||
|
|
||||||
__check_bounds(addr);
|
__check_bounds(addr);
|
||||||
|
|
||||||
return base_addr[addr];
|
return *addr_align_word(addr);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue