1
0
mirror of https://github.com/pevans/erc-c.git synced 2024-11-23 23:32:45 +00:00

Add support for a map machine pointer

To be sent to the read/write functions that are implemented by said
machine.
This commit is contained in:
Peter Evans 2018-01-02 21:11:43 -06:00
parent 3fe7a4e0bb
commit ee7b5f66b0
2 changed files with 26 additions and 4 deletions

View File

@ -55,11 +55,12 @@ struct vm_segment {
extern int vm_segment_copy(vm_segment *, vm_segment *, size_t, size_t, size_t);
extern int vm_segment_fread(vm_segment *, FILE *, size_t);
extern int vm_segment_read_map(vm_segment *, size_t, void *, vm_segment_read_fn);
extern int vm_segment_read_map(vm_segment *, size_t, vm_segment_read_fn);
extern int vm_segment_set(vm_segment *, size_t, vm_8bit);
extern int vm_segment_write_map(vm_segment *, size_t, void *, vm_segment_write_fn);
extern int vm_segment_write_map(vm_segment *, size_t, vm_segment_write_fn);
extern vm_8bit vm_segment_get(vm_segment *, size_t);
extern vm_segment *vm_segment_create(size_t);
extern void vm_segment_free(vm_segment *);
extern void vm_segment_set_map_machine(void *);
#endif

View File

@ -14,6 +14,18 @@
#include "log.h"
#include "vm_segment.h"
/*
* This is sort of regrettable, but we need a machine pointer that we
* can pass into the read/write map functions (which will assume to have
* access to the machine architecture). The alternative is an update to
* a lot more of the codebase to add machine pointers -- void pointers
* at that -- which is even uglier.
*
* FIXME: we might consider a dependency injection container at some
* point.
*/
static void *map_mach = NULL;
/*
* Create a new segment, such that it contains a number of bytes indicated
* by `size`.
@ -100,7 +112,7 @@ vm_segment_set(vm_segment *segment, size_t index, vm_8bit value)
// Check if we have a write mapper
if (segment->write_table[index]) {
segment->write_table[index](segment, index, value);
segment->write_table[index](segment, index, value, map_mach);
return OK;
}
@ -128,7 +140,7 @@ vm_segment_get(vm_segment *segment, size_t index)
// We may have a read mapper for this address
if (segment->read_table[index]) {
return segment->read_table[index](segment, index);
return segment->read_table[index](segment, index, map_mach);
}
return segment->memory[index];
@ -229,3 +241,12 @@ vm_segment_fread(vm_segment *segment, FILE *stream, size_t len)
return OK;
}
/*
* Change the internal notion of the machine used by map functions
*/
void
vm_segment_set_map_machine(void *mach)
{
map_mach = mach;
}