libnexus-rv
msg-decoder.h
Go to the documentation of this file.
1 // SPDX-License-Identifer: Apache 2.0
2 /*
3  * decoder.h - NexusRV 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 retstack_sz;
28  bool HTM;
29  bool VAO;
30  bool ext_sifive;
32 
38 typedef struct nexusrv_msg_decoder {
41  int fd;
42  int16_t src_filter;
43  void *buffer;
44  size_t bufsz;
45  size_t nread;
46  size_t filled;
47  size_t pos;
48  size_t lastmsg_len;
50 
51 int nexusrv_hwcfg_parse(nexusrv_msg_decoder *hwcfg,
52  const char *str);
53 
55 #define NEXUS_RV_MSG_MAX_BYTES 38
56 
70 ssize_t nexusrv_sync_forward(const uint8_t *buffer, size_t limit);
71 
85 ssize_t nexusrv_sync_backward(const uint8_t *buffer, size_t pos);
86 
101 ssize_t nexusrv_msg_decode(const nexusrv_hw_cfg *hwcfg,
102  const uint8_t *buffer, size_t limit,
103  nexusrv_msg *msg);
104 
114 static inline void nexusrv_msg_decoder_init(nexusrv_msg_decoder *decoder,
115  const nexusrv_hw_cfg *hwcfg,
116  int fd, int16_t src_filter,
117  uint8_t *buffer,
118  size_t bufsz) {
119  memset(decoder, 0, sizeof(*decoder));
120  decoder->hw_cfg = hwcfg;
121  decoder->fd = fd;
122  decoder->src_filter = src_filter;
123  decoder->buffer = buffer;
124  decoder->bufsz = bufsz;
125 }
126 
132 static inline size_t nexusrv_msg_decoder_offset(nexusrv_msg_decoder *decoder) {
133  size_t offset = decoder->nread;
134  if (decoder->pos != decoder->bufsz)
135  offset += decoder->pos;
136  return offset - decoder->lastmsg_len;
137 }
138 
160  nexusrv_msg *msg);
161 
172 
185 
191 int nexusrv_print_msg(FILE *fp, const nexusrv_msg *msg);
192 
193 #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.
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
bool ext_sifive
Definition: msg-decoder.h:30
unsigned ts_bits
Definition: msg-decoder.h:25
unsigned src_bits
Definition: msg-decoder.h:24
unsigned retstack_sz
Definition: msg-decoder.h:27
bool HTM
Definition: msg-decoder.h:28
bool VAO
Definition: msg-decoder.h:29
unsigned addr_bits
Definition: msg-decoder.h:26
NexusRV Message decoder context.
Definition: msg-decoder.h:38
const nexusrv_hw_cfg * hw_cfg
Definition: msg-decoder.h:39
size_t bufsz
Definition: msg-decoder.h:44
size_t lastmsg_len
Definition: msg-decoder.h:48
size_t nread
Definition: msg-decoder.h:45
int fd
Definition: msg-decoder.h:41
size_t filled
Definition: msg-decoder.h:46
void * buffer
Definition: msg-decoder.h:43
int16_t src_filter
Definition: msg-decoder.h:42
size_t pos
Definition: msg-decoder.h:47
Decoded NexusRV Message.
Definition: msg-types.h:92