summaryrefslogtreecommitdiff
path: root/cmd/sbc_harness/ihex.h
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/sbc_harness/ihex.h')
-rw-r--r--cmd/sbc_harness/ihex.h49
1 files changed, 49 insertions, 0 deletions
diff --git a/cmd/sbc_harness/ihex.h b/cmd/sbc_harness/ihex.h
new file mode 100644
index 0000000..35a3cbe
--- /dev/null
+++ b/cmd/sbc_harness/ihex.h
@@ -0,0 +1,49 @@
+/* sbc_harness/ihex.h - Intel Hex decoder
+ *
+ * Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+/* https://archive.org/details/IntelHEXStandard */
+
+#ifndef _SBC_HARNESS_IHEX_H_
+#define _SBC_HARNESS_IHEX_H_
+
+#include <stdbool.h> /* for bool */
+#include <stddef.h> /* for size_t */
+#include <stdint.h> /* for uint{n}_t */
+
+#include <libhw/generic/io.h>
+#include <libmisc/private.h>
+
+struct ihex_decoder {
+ void *handle_arg;
+ error (*handle_data)(void *arg, uint32_t off, uint8_t count, uint8_t *dat);
+ error (*handle_set_exec_start_seg)(void *arg, uint16_t cs, uint16_t ip);
+ error (*handle_set_exec_start_lin)(void *arg, uint32_t eip);
+ error (*handle_eof)(void *arg);
+
+ BEGIN_PRIVATE(IHEX_H);
+
+ bool seen_eof;
+ error sticky_err;
+
+ /* ihex_decoder_write: deal with ASCII soup */
+ bool in_record;
+ char buf_char; /* the first nibble of a byte (still in ASCII) */
+
+ /* ihex_decode_byte: build records from decoded bytes */
+ /* The currently-being-decoded record, after hex decoding. */
+ uint16_t buf_len;
+ uint8_t buf[0xFF+5];
+
+ /* ihex_handle_record: handle record semantics */
+ enum { _IHEX_MODE_NONE, _IHEX_MODE_SEG, _IHEX_MODE_LIN } addr_mode;
+ uint32_t addr_base;
+
+ END_PRIVATE(IHEX_H);
+};
+LO_IMPLEMENTATION_H(io_writer, struct ihex_decoder, ihex_decoder);
+LO_IMPLEMENTATION_H(io_closer, struct ihex_decoder, ihex_decoder);
+
+#endif /* _SBC_HARNESS_IHEX_H_ */