i2c tools better naming scheme
[cr816-sim.git] / log.c
blobc70b21fa3a2a231cec43a99470ff874086ae9345
1 /*
2 * TODO TODO TODO TODO TODO TODO TODO
4 * move reg dump here to be more consistent
6 */
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <string.h>
11 #include "disasm.h"
14 /** ************* log functions ***********/
16 //strings
17 #define LOG_ADDR_STR_MAX 8
18 static char log_addr_str[LOG_ADDR_STR_MAX+1];
20 #define LOG_OPCODE_STR_MAX 10
21 static char log_opcode_str[LOG_OPCODE_STR_MAX+1];
23 #define LOG_INSTR_NAME_STR_MAX 10
24 static char log_instr_name_str[LOG_INSTR_NAME_STR_MAX+1];
26 #define LOG_INSTR_ARGS_STR_MAX 30
27 static char log_instr_args_str[LOG_INSTR_ARGS_STR_MAX+1];
29 #define LOG_ACCESS_STR_MAX 80
30 static char log_access_read_str[LOG_ACCESS_STR_MAX+1];
31 static unsigned log_access_read_idx = 0;
32 static char log_access_write_str[LOG_ACCESS_STR_MAX+1];
33 static unsigned log_access_write_idx = 0;
35 #define LOG_COMMENT_STR_MAX 1000
36 static char log_comment_str[LOG_COMMENT_STR_MAX+1];
37 static unsigned log_comment_idx = 0;
39 //functions
40 void log_addr(u16 addr)
42 snprintf(log_addr_str, LOG_ADDR_STR_MAX, "%04hx", addr);
45 void log_opcode(struct opcode_word opcode)
47 snprintf(log_opcode_str, LOG_OPCODE_STR_MAX, "%06x", opcode.raw);
50 void log_instr_name(const char *fmt, ...)
52 va_list args;
53 va_start(args, fmt);
54 vsnprintf(log_instr_name_str,
55 LOG_INSTR_NAME_STR_MAX,
56 fmt,
57 args
59 va_end(args);
62 void log_instr_args(const char *fmt, ...)
64 va_list args;
65 va_start(args, fmt);
66 vsnprintf(log_instr_args_str,
67 LOG_INSTR_ARGS_STR_MAX,
68 fmt,
69 args
71 va_end(args);
74 //TODO use trace
75 void log_access_read_add(const char *fmt, ...)
77 if (log_access_read_idx < LOG_ACCESS_STR_MAX) {
78 va_list args;
79 va_start(args, fmt);
80 log_access_read_idx += vsnprintf(
81 &log_access_read_str[log_access_read_idx],
82 LOG_ACCESS_STR_MAX - log_access_read_idx,
83 fmt,
84 args
86 va_end(args);
90 void log_access_write_add(const char *fmt, ...)
92 if (log_access_write_idx < LOG_ACCESS_STR_MAX) {
93 va_list args;
94 va_start(args, fmt);
95 log_access_write_idx += vsnprintf(
96 &log_access_write_str[log_access_write_idx],
97 LOG_ACCESS_STR_MAX - log_access_write_idx,
98 fmt,
99 args
101 va_end(args);
105 void log_comment_add(const char *fmt, ...)
107 if (log_comment_idx < LOG_COMMENT_STR_MAX) {
108 va_list args;
109 va_start(args, fmt);
110 log_comment_idx += vsnprintf(
111 &log_comment_str[log_comment_idx],
112 LOG_COMMENT_STR_MAX - log_comment_idx,
113 fmt,
114 args
116 va_end(args);
121 void log_comment_reg_bits(
122 const char *caption,
123 const char *reg_names[],
124 u8 old,
125 u8 new)
127 u8 diff = old ^ new;
129 if (diff) {
130 log_comment_add(" %s(%02hhx->%02hhx)\n", caption, old, new);
131 } else {
132 log_comment_add(" %s(%02hhx)\n", caption, new);
135 log_comment_add(" ");
136 for(unsigned bit=0; bit<8; bit++) {
137 log_comment_add(" %1d:%s",
138 7-bit,
139 reg_names[7-bit]
142 log_comment_add("\n");
145 log_comment_add(" ");
146 for(unsigned bit=0; bit<8; bit++) {
147 if (diff & (1 << (7-bit))) {
148 log_comment_add(" %.*s%c->%c",
149 strlen(reg_names[7-bit])-2,
150 " ",
151 old & (1<<(7-bit))?'1':'0',
152 new & (1<<(7-bit))?'1':'0'
154 } else {
155 log_comment_add(" %.*s%c",
156 strlen(reg_names[7-bit])+1,
157 " ",
158 new & (1<<(7-bit))?'1':'0'
162 log_comment_add("\n");
165 ///main flush
167 void log_flush(unsigned flags)
169 //addr opbytes name+args comments
171 if (flags) {
172 printf("%4s:\t%s %s %-*s\n",
173 log_addr_str,
174 log_opcode_str,
175 log_instr_name_str,
176 25-(int)strlen(log_instr_name_str),
177 log_instr_args_str
179 } else {
180 //TODO split, no W, no R (don't do if trace log will be used)
181 printf("%4s:\t%s %s %-*s ;R{%s} ;W{%s}\n",
182 log_addr_str,
183 log_opcode_str,
184 log_instr_name_str,
185 25-(int)strlen(log_instr_name_str),
186 log_instr_args_str,
187 log_access_read_str,
188 log_access_write_str
192 if (strlen(log_comment_str)) {
193 // printf("\n%s\n", log_comment_str);
194 printf("%s", log_comment_str);
197 log_addr_str[0] = '\0';
198 log_opcode_str[0] = '\0';
199 log_instr_name_str[0] = '\0';
200 log_instr_args_str[0] = '\0';
201 log_access_read_str[0] = '\0';
202 log_access_write_str[0] = '\0';
203 log_comment_str[0] = '\0';
205 log_access_read_idx = 0;
206 log_access_write_idx = 0;
207 log_comment_idx = 0;
210 extern u8 read_mem(u16 addr, unsigned flags);
212 #define CHAR_PER_LINE 16
213 void log_buf(char *name, u16 base, u16 len)
215 //TODO use log_comment buffer? problem with really long memdumps (limited buffer)
216 printf("%s:\n", name);
218 for(unsigned off=0;off<len;off++){
219 if ((off%CHAR_PER_LINE) == 0) {
220 printf("%04hx: ", off);
223 printf("%02hhx ", read_mem(base+off, 0));
225 if ((off%CHAR_PER_LINE) == (CHAR_PER_LINE-1)) {
226 printf(" ");
227 u8 val;
228 for(unsigned charoff=0;charoff<CHAR_PER_LINE;charoff++){
229 val = read_mem(base + charoff + (off/CHAR_PER_LINE)*CHAR_PER_LINE, 0);
231 printf("%c", ((val>=32)&&(val<=128))?val:'.');
233 printf("\n");