libnexus-rv
msg-decoder.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache 2.0
2 /*
3  * msg-decoder.h - NexusRV Message decoder functions and declarations
4  *
5  * Copyright (C) 2025, Bo Gan <ganboing@gmail.com>
6  */
7 
8 #ifndef LIBNEXUS_RV_MSG_DECODER_H
9 #define LIBNEXUS_RV_MSG_DECODER_H
10 
11 #include "msg-types.h"
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <stdbool.h>
15 #include <string.h>
16 #include <unistd.h>
23 typedef struct nexusrv_hw_cfg {
24  unsigned src_bits;
25  unsigned ts_bits;
26  unsigned addr_bits;
27  unsigned max_stack;
28  uint64_t timer_freq;
29  bool HTM;
30  bool VAO;
31  bool quirk_sifive;
33 
39 typedef struct nexusrv_msg_decoder {
42  int fd;
43  int16_t src_filter;
44  void *buffer;
45  size_t bufsz;
46  size_t nread;
47  size_t filled;
48  size_t pos;
49  size_t lastmsg_len;
51 
76  const char *str);
77 
79 #define NEXUS_RV_MSG_MAX_BYTES 38
80 
94 ssize_t nexusrv_sync_forward(const uint8_t *buffer, size_t limit);
95 
109 ssize_t nexusrv_sync_backward(const uint8_t *buffer, size_t pos);
110 
125 ssize_t nexusrv_msg_decode(const nexusrv_hw_cfg *hwcfg,
126  const uint8_t *buffer, size_t limit,
127  nexusrv_msg *msg);
128 
138 static inline void nexusrv_msg_decoder_init(nexusrv_msg_decoder *decoder,
139  const nexusrv_hw_cfg *hwcfg,
140  int fd, int16_t src_filter,
141  uint8_t *buffer,
142  size_t bufsz) {
143  memset(decoder, 0, sizeof(*decoder));
144  decoder->hw_cfg = hwcfg;
145  decoder->fd = fd;
146  decoder->src_filter = src_filter;
147  decoder->buffer = buffer;
148  decoder->bufsz = bufsz;
149 }
150 
156 static inline size_t nexusrv_msg_decoder_offset(nexusrv_msg_decoder *decoder) {
157  size_t offset = decoder->nread;
158  if (decoder->pos != decoder->bufsz)
159  offset += decoder->pos;
160  return offset - decoder->lastmsg_len;
161 }
162 
184  nexusrv_msg *msg);
185 
196 
209 
215 int nexusrv_print_msg(FILE *fp, const nexusrv_msg *msg);
216 
217 #endif
uint8_t * nexusrv_msg_decoder_lastmsg(nexusrv_msg_decoder *decoder)
Get the pointer to the last successfully decoded Message bytes.
ssize_t nexusrv_msg_decode(const nexusrv_hw_cfg *hwcfg, const uint8_t *buffer, size_t limit, nexusrv_msg *msg)
Decode the full NexusRV Message from buffer into msg.
struct nexusrv_hw_cfg nexusrv_hw_cfg
NexusRV Message decoder configuration.
int nexusrv_print_msg(FILE *fp, const nexusrv_msg *msg)
Print the msg in human readable form to fp.
ssize_t nexusrv_sync_forward(const uint8_t *buffer, size_t limit)
Sync forward for the first full Message.
ssize_t nexusrv_msg_decoder_next(nexusrv_msg_decoder *decoder, nexusrv_msg *msg)
Iteratively decode the next Nexus Message from trace file.
void nexusrv_msg_decoder_rewind_last(nexusrv_msg_decoder *decoder)
Rewind the decoder to the beginning of last Message.
int nexusrv_hwcfg_parse(nexusrv_hw_cfg *hwcfg, const char *str)
Parse the hwcfg string into hwcfg structure.
ssize_t nexusrv_sync_backward(const uint8_t *buffer, size_t pos)
Sync backward for the last full Message.
struct nexusrv_msg_decoder nexusrv_msg_decoder
NexusRV Message decoder context.
NexusRV Message decoder configuration.
Definition: msg-decoder.h:23
unsigned ts_bits
Definition: msg-decoder.h:25
unsigned src_bits
Definition: msg-decoder.h:24
unsigned max_stack
Definition: msg-decoder.h:27
bool HTM
Definition: msg-decoder.h:29
uint64_t timer_freq
Definition: msg-decoder.h:28
bool VAO
Definition: msg-decoder.h:30
bool quirk_sifive
Definition: msg-decoder.h:31
unsigned addr_bits
Definition: msg-decoder.h:26
NexusRV Message decoder context.
Definition: msg-decoder.h:39
const nexusrv_hw_cfg * hw_cfg
Definition: msg-decoder.h:40
size_t bufsz
Definition: msg-decoder.h:45
size_t lastmsg_len
Definition: msg-decoder.h:49
size_t nread
Definition: msg-decoder.h:46
int fd
Definition: msg-decoder.h:42
size_t filled
Definition: msg-decoder.h:47
void * buffer
Definition: msg-decoder.h:44
int16_t src_filter
Definition: msg-decoder.h:43
size_t pos
Definition: msg-decoder.h:48
Decoded NexusRV Message.
Definition: msg-types.h:102