1
0
mirror of https://github.com/pevans/erc-c.git synced 2024-10-05 04:56:46 +00:00

Add set16 function

This commit is contained in:
Peter Evans 2018-01-10 16:43:14 -06:00
parent 6af0ae85a4
commit fcba14f91b
3 changed files with 32 additions and 0 deletions

View File

@ -58,6 +58,7 @@ extern int vm_segment_copy_buf(vm_segment *, const vm_8bit *, size_t, size_t, si
extern int vm_segment_fread(vm_segment *, FILE *, size_t, size_t);
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_set16(vm_segment *, size_t, vm_16bit);
extern int vm_segment_write_map(vm_segment *, size_t, vm_segment_write_fn);
extern vm_16bit vm_segment_get16(vm_segment *, size_t);
extern vm_8bit vm_segment_get(vm_segment *, size_t);

View File

@ -303,3 +303,28 @@ vm_segment_get_map_machine()
{
return map_mach;
}
int
vm_segment_set16(vm_segment *segment, size_t addr, vm_16bit value)
{
vm_8bit lsb, msb;
int err;
lsb = value & 0xff;
msb = value >> 8;
// This data needs to be saved in little-endian order; e.g. if we
// get $1234, then we need to store it as $34 $12.
err = vm_segment_set(segment, addr, lsb);
// If the previous set() worked out, then let's try it again with
// the msb.
if (err == OK) {
err = vm_segment_set(segment, addr + 1, msb);
}
// If err != OK above, we will just return the err code. If err was
// OK, but is not OK after the msb set, then we'll return with that
// code.
return err;
}

View File

@ -174,3 +174,9 @@ Test(vm_segment, get16)
cr_assert_eq(vm_segment_get16(segment, 0), 0x1234);
}
Test(vm_segment, set16)
{
vm_segment_set16(segment, 0, 0x2345);
cr_assert_eq(vm_segment_get16(segment, 0), 0x2345);
}