Skip to content

Commit 78b4643

Browse files
committed
Implement addhist command
1 parent 2c33e66 commit 78b4643

6 files changed

+66
-9
lines changed

sim/lex-riscv.c

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ TokenTab riscv_token_table [] =
7171
{"DUMPHIST", T_DUMPHIST}, /*+ Show the contents of a histogram register.:<histogram register> */
7272
{"DUMPHISTPRETTY", T_DUMPHISTPRETTY}, /*+ Show the contents of a histogram register, with ASCII-graph representation.:<histogram register> */
7373
{"LDHISTRND", T_LDHISTRND}, /*+ Fill a histogram register with random values.:<histogram register> */
74+
{"ADDHIST", T_ADDHIST}, /*+ Add two histograms. Write result to third histogram.:<histogram register addend 1> <histogram register addend 2> <histogram register result> */
7475
{"DUMPSYSREGS", T_DUMPSYSREGS}, /*+ Show the contents of the system registers.:none */
7576
{"DUMPMEM", T_DUMPMEM}, /*+ Show contents of memory.:<start mem address (hexadecimal)> <end mem address (hexadecimal)> */
7677
{"DUMPPIPE", T_DUMPPIPE}, /*+ Show the contents of the pipeline stages.:none */

sim/machine-riscv.c

+24
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ riscvfatalaction(Engine *E, State *S)
202202
return;
203203
}
204204

205+
206+
207+
205208
/*
206209
* Histograms
207210
*/
@@ -222,18 +225,38 @@ riscvdumphist(Engine *E, State *S, int histogram_id){
222225
// Print histogram with extra stats and ASCII graphics
223226
void
224227
riscvdumphistpretty(Engine *E, State *S, int histogram_id){
228+
mprint(E, S, nodeinfo, "Printing information for register %u\n", histogram_id);
225229
Histogram_PrettyPrint(E, S, &S->riscv->histograms[histogram_id]);
226230

227231
return;
228232
}
229233

234+
// Load histogram with bin values randomly filled
230235
void
231236
riscvldhistrandom(Engine *E, State *S, int histogram_id){
232237
Histogram_LDRandom(&S->riscv->histograms[histogram_id]);
233238

234239
return;
235240
}
236241

242+
// Add two histograms
243+
void
244+
riscvaddhist(Engine *E, State *S, int histogram_id0, int histogram_id1, int histogram_id_dest){
245+
Histogram_AddDist(
246+
&S->riscv->histograms[histogram_id0],
247+
&S->riscv->histograms[histogram_id1],
248+
&S->riscv->histograms[histogram_id_dest]
249+
);
250+
251+
return;
252+
}
253+
254+
255+
256+
257+
258+
259+
237260
static UncertainState *
238261
uncertainnewstate(Engine *E, char *ID)
239262
{
@@ -282,6 +305,7 @@ riscvnewstate(Engine *E, double xloc, double yloc, double zloc, char *trajfilena
282305
S->dumphist = riscvdumphist;
283306
S->dumphistpretty = riscvdumphistpretty;
284307
S->ldhistrandom = riscvldhistrandom;
308+
S->addhist = riscvaddhist;
285309

286310
S->fatalaction = riscvfatalaction;
287311
S->endian = Little;

sim/main.h

+1
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ struct State
723723
void (*dumphist)(Engine *, State *S, int histogram_id);
724724
void (*dumphistpretty)(Engine *, State *S, int histogram_id);
725725
void (*ldhistrandom)(Engine *, State *S, int histogram_id);
726+
void (*addhist)(Engine *, State *S, int histogram_id0, int histogram_id1, int histogram_id2);
726727

727728
/* Memory mapped device register read/write functions */
728729
uchar (*devreadbyte)(Engine *, State *S, ulong addr);

sim/regaccess-riscv.c

+28-8
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ void freg_set_riscv(Engine *E, State *S, uint8_t n, uint64_t data)
117117

118118

119119

120-
Histogram Histogram_AddDist(Histogram hist1, Histogram hist2, Histogram histDest){
120+
void
121+
Histogram_AddDist(Histogram *hist1, Histogram *hist2, Histogram *histDest){
121122
/*
122123
* Add two distributions, considering overflow
123124
*/
@@ -132,33 +133,53 @@ Histogram Histogram_AddDist(Histogram hist1, Histogram hist2, Histogram histDest
132133
// TODO Alexa wrote "just in case" -- why?
133134
// Presumably this is to say it is our responsibility to initialise
134135
for (int k = 0; k < kNBINS; k++){
135-
histDest.bins[k] = 0;
136+
histDest->bins[k] = 0;
136137
}
137138

138139
// Iterate, adding with overflow
139140
for (int j = 0; j < kNBINS; j++){
140141
for (int i = 0; i < kNBINS; i++){
141142
overflow_wid = i+j;
143+
/*printf("overflow_wid = %u\n", overflow_wid);*/
142144

143145
if (overflow_wid < kNBINS){
144-
overflow_hi = histDest.bins[i+j] + (uint32_t)((uint32_t)hist1.bins[i] * hist2.bins[j]);
146+
overflow_hi = histDest->bins[i+j] + (uint32_t)((uint32_t)hist1->bins[i] * hist2->bins[j]);
147+
/*printf("histdestbinsij = %u\n", histDest->bins[i+j]);*/
148+
/*printf("hist1i = %u\n", hist1->bins[i]);*/
149+
/*printf("hist2j = %u\n", hist2->bins[j]);*/
150+
/*printf("overflow_hi = %u\n", overflow_hi);*/
145151

146152
if (overflow_hi < 65536){
147-
histDest.bins[i+j] += hist1.bins[i] * hist2.bins[j];
153+
/*printf("overflow_hi<65536\n");*/
154+
histDest->bins[i+j] += hist1->bins[i] * hist2->bins[j];
148155
}
149156
else{
150157
// Bin overflow error
151-
// TODO implement (also missing from original implementation)
158+
// TODO implement (also missing from original implementation -- how to handle?)
159+
printf("UNIMPLEMENTED bin overflow error\n");
152160
}
153161
}
154162
else{
155163
// Value overflow error
156-
// TODO implement (also missing from original implementation)
164+
// TODO implement (also missing from original implementation -- how to handle?)
165+
printf("UNIMPLEMENTED value overflow error\n");
157166
}
158167
}
159168
}
160169

161-
return histDest;
170+
// TODO Idea: normalise to same mean frequency?
171+
/*double meanFreq = Histogram_MeanFrequency(histDest);*/
172+
/*for (int i = 0; i < kNBINS; i++){*/
173+
/*histDest->bins[i] /= (meanFreq);*/
174+
/*}*/
175+
176+
// TODO Idea: normalise to same full scale (say, 255?)
177+
/*for (int i = 0; i < kNBINS; i++){*/
178+
/*histDest->bins[i] /= ;*/
179+
/*}*/
180+
181+
182+
return;
162183
}
163184

164185
void Histogram_LDDist(Histogram *histogram, HistogramBinDatatype *bins){
@@ -235,7 +256,6 @@ void Histogram_PrettyPrint(Engine *E, State *S, Histogram *histogram){
235256
normalised[i] = histogram->bins[i] / FULLSCALE;
236257
}
237258

238-
mprint(E, S, nodeinfo, "Printing information for register __TODO__\n");
239259
mprint(E, S, nodeinfo, "Histogram mean frequency (mean bin occupation): %.3f\n", meanFreq);
240260
mprint(E, S, nodeinfo, "bin | val | graphical representation (scaled rel. to mean freq)\n");
241261
mprint(E, S, nodeinfo, "----+-----+----------------------------------------------------\n");

sim/regs-riscv.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ typedef struct
290290

291291

292292
// Add two distributions, considering overflow
293-
Histogram Histogram_AddDist(Histogram hist1, Histogram hist2, Histogram histDest);
293+
void Histogram_AddDist(Histogram *hist1, Histogram *hist2, Histogram *histDest);
294294

295295
// Load distribution
296296
void Histogram_LDDist(Histogram *histogram, HistogramBinDatatype bins[kNBINS]);
@@ -303,3 +303,6 @@ double Histogram_Mean(Histogram *histogram);
303303

304304
// Pretty-print histogram distribution
305305
void Histogram_PrettyPrint(Engine *E, State *S, Histogram *histogram);
306+
307+
// Return the mean frequency of a histogram, i.e. the average bin value (not weighted by index)
308+
double Histogram_MeanFrequency(Histogram *histogram);

sim/sf-riscv.y

+8
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
%token T_DUMPHIST
108108
%token T_DUMPHISTPRETTY
109109
%token T_LDHISTRND
110+
%token T_ADDHIST
110111
%token T_DUMPSYSREGS
111112
%token T_DUMPTIME
112113
%token T_DUMPTLB
@@ -1231,6 +1232,13 @@ sf_cmd : T_QUIT '\n'
12311232
yyengine->cp->ldhistrandom(yyengine, yyengine->cp, $2);
12321233
}
12331234
}
1235+
| T_ADDHIST uimm uimm uimm '\n'
1236+
{
1237+
if (!yyengine->scanning)
1238+
{
1239+
yyengine->cp->addhist(yyengine, yyengine->cp, $2, $3, $4);
1240+
}
1241+
}
12341242
| T_DUMPSYSREGS '\n'
12351243
{
12361244
if (!yyengine->scanning)

0 commit comments

Comments
 (0)