From 87c7aa92492c33574efc8f878f16025f008c29cd Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Fri, 9 Mar 2018 16:45:20 -0600 Subject: [PATCH] Add hexdump function for vm_segment --- include/vm_segment.h | 1 + src/vm_segment.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/vm_segment.h b/include/vm_segment.h index ecbf0a9..5a255bd 100644 --- a/include/vm_segment.h +++ b/include/vm_segment.h @@ -72,6 +72,7 @@ extern vm_8bit vm_segment_get(vm_segment *, size_t); extern vm_segment *vm_segment_create(size_t); extern void *vm_segment_get_map_machine(); extern void vm_segment_free(vm_segment *); +extern void vm_segment_hexdump(vm_segment *, FILE *, size_t, size_t); extern void vm_segment_set_map_machine(void *); #endif diff --git a/src/vm_segment.c b/src/vm_segment.c index 01bd0cc..a6ee130 100644 --- a/src/vm_segment.c +++ b/src/vm_segment.c @@ -7,6 +7,7 @@ * etc. */ +#include #include #include #include @@ -352,3 +353,33 @@ vm_segment_set16(vm_segment *segment, size_t addr, vm_16bit value) // code. return err; } + +void +vm_segment_hexdump(vm_segment *seg, FILE *stream, size_t from, size_t to) +{ + char nbuf[51], sbuf[17]; + int ni = 0, si = 0; + int bytes = 0; + vm_8bit byte; + + while (from < to) { + byte = vm_segment_get(seg, from); + + ni += sprintf(nbuf + ni, "%02X ", byte); + si += sprintf(sbuf + si, "%c", isprint(byte) ? byte : '.'); + + from++; + bytes++; + + if (bytes == 8) { + ni += sprintf(nbuf + ni, " "); + } + + if (bytes >= 16) { + fprintf(stream, "%08zX %s [%s]\n", from - bytes, nbuf, sbuf); + bytes = 0; + ni = 0; + si = 0; + } + } +}