diff --git a/include/vm_segment.h b/include/vm_segment.h index e87a20d..640a67e 100644 --- a/include/vm_segment.h +++ b/include/vm_segment.h @@ -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); diff --git a/src/vm_segment.c b/src/vm_segment.c index 8f1a7ac..0ffc106 100644 --- a/src/vm_segment.c +++ b/src/vm_segment.c @@ -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; +} diff --git a/tests/vm_segment.c b/tests/vm_segment.c index 773eed2..86b908a 100644 --- a/tests/vm_segment.c +++ b/tests/vm_segment.c @@ -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); +}