summaryrefslogtreecommitdiff
path: root/cmd/sbc_harness/ihex.h
blob: 35a3cbeb39e31702fda195be033483ab79b4c66d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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_ */