From 70516a7f914fefc1d864654b2e125850f02f794c Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Fri, 5 Jan 2018 16:14:51 -0600 Subject: [PATCH] Add function to return 16-bit value from a segment address --- include/vm_segment.h | 1 + src/vm_segment.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/vm_segment.h b/include/vm_segment.h index 7a2f2a5..07c09ae 100644 --- a/include/vm_segment.h +++ b/include/vm_segment.h @@ -59,6 +59,7 @@ 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_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); extern vm_segment *vm_segment_create(size_t); extern void vm_segment_free(vm_segment *); diff --git a/src/vm_segment.c b/src/vm_segment.c index 0c75bf0..fc0745e 100644 --- a/src/vm_segment.c +++ b/src/vm_segment.c @@ -146,6 +146,23 @@ vm_segment_get(vm_segment *segment, size_t index) return segment->memory[index]; } +/* + * Return a 16-bit value from a given address. This will read the byte + * at addr and the byte at addr+1, then fit those into a two-byte + * variable such that addr contains the most significant byte and addr+1 + * contains the least significant byte. + */ +vm_16bit +vm_segment_get16(vm_segment *segment, size_t addr) +{ + vm_16bit msb, lsb; + + msb = (vm_16bit)vm_segment_get(segment, addr); + lsb = (vm_16bit)vm_segment_get(segment, addr+1); + + return (msb << 8) | lsb; +} + /* * Copy a set of bytes from `src` (at `src_index`) to `dest` (at * `dest_index`), such that the range is `length` bytes long. Note that