1 |
58 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: pdump.cpp
|
4 |
|
|
//
|
5 |
|
|
// Project: Zip CPU -- a small, lightweight, RISC CPU core
|
6 |
|
|
//
|
7 |
|
|
// Purpose: Disassemble machine code files onto the stdout file. Unlike
|
8 |
|
|
// the zdump program that is part of the assembler suite, this
|
9 |
|
|
// program takes the pfile.bin output of the bench test suite and adds
|
10 |
|
|
// profiling information to the output. It's useful for finding out where,
|
11 |
|
|
// at least in simulation, your time is being spent. It can also be used,
|
12 |
|
|
// after the fact, to get a trace of what instructions the CPU executed.
|
13 |
|
|
//
|
14 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
15 |
|
|
// Gisselquist Tecnology, LLC
|
16 |
|
|
//
|
17 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
18 |
|
|
//
|
19 |
|
|
// Copyright (C) 2015, Gisselquist Technology, LLC
|
20 |
|
|
//
|
21 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
22 |
|
|
// modify it under the terms of the GNU General Public License as published
|
23 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
24 |
|
|
// your option) any later version.
|
25 |
|
|
//
|
26 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
27 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
28 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
29 |
|
|
// for more details.
|
30 |
|
|
//
|
31 |
|
|
// You should have received a copy of the GNU General Public License along
|
32 |
|
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
33 |
|
|
// target there if the PDF file isn't present.) If not, see
|
34 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
35 |
|
|
//
|
36 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
37 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
38 |
|
|
//
|
39 |
|
|
//
|
40 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
41 |
|
|
#include <algorithm>
|
42 |
|
|
#include <stdio.h>
|
43 |
|
|
#include <unistd.h>
|
44 |
|
|
#include <ctype.h>
|
45 |
|
|
|
46 |
|
|
#include "zopcodes.h"
|
47 |
|
|
|
48 |
|
|
typedef struct { unsigned clks, addr; } ALT;
|
49 |
|
|
bool altcmp(const ALT &a, const ALT &b) {
|
50 |
|
|
return a.clks < b.clks;
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
void dump_file(const char *fn) {
|
54 |
|
|
const int NZIP = 4096;
|
55 |
|
|
char ln[NZIP];
|
56 |
|
|
ZIPI ibuf[NZIP];
|
57 |
|
|
FILE *fp, *pf;
|
58 |
|
|
int nr;
|
59 |
|
|
unsigned addr=0x08000, mina = -1, maxa = 0,
|
60 |
|
|
*pfcnt = NULL, *pfclk = NULL;
|
61 |
|
|
|
62 |
|
|
fp = fopen(fn, "r");
|
63 |
|
|
if (!fp)
|
64 |
|
|
return;
|
65 |
|
|
|
66 |
|
|
pf = fopen("pfile.bin","rb");
|
67 |
|
|
if (pf) {
|
68 |
|
|
ALT *pfalt;
|
69 |
|
|
unsigned buf[2], total_clks = 0;
|
70 |
|
|
while(2 == fread(buf, sizeof(unsigned), 2, pf)) {
|
71 |
|
|
if (mina > buf[0])
|
72 |
|
|
mina = buf[0];
|
73 |
|
|
if (maxa < buf[0])
|
74 |
|
|
maxa = buf[0];
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
addr = mina;
|
78 |
|
|
pfcnt = new unsigned[(maxa+2-mina)];
|
79 |
|
|
pfclk = new unsigned[(maxa+2-mina)];
|
80 |
|
|
pfalt = new ALT[(maxa+2-mina)];
|
81 |
|
|
unsigned ncnt = maxa+2-mina;
|
82 |
|
|
for(int i=0; i<(int)ncnt; i++)
|
83 |
|
|
pfcnt[i] = pfclk[i] = 0;
|
84 |
|
|
for(int i=0; i<(int)ncnt; i++)
|
85 |
|
|
pfalt[i].addr = pfalt[i].clks = 0;
|
86 |
|
|
|
87 |
|
|
rewind(pf);
|
88 |
|
|
while(2 == fread(buf, sizeof(unsigned), 2, pf)) {
|
89 |
|
|
pfcnt[buf[0]-addr]++;
|
90 |
|
|
pfclk[buf[0]-addr] += buf[1];
|
91 |
|
|
pfalt[buf[0]-addr].clks += buf[1];
|
92 |
|
|
pfalt[buf[0]-addr].addr = buf[0];
|
93 |
|
|
total_clks += buf[1];
|
94 |
|
|
|
95 |
|
|
printf("%08x\n", buf[0]);
|
96 |
|
|
} fclose(pf);
|
97 |
|
|
|
98 |
|
|
printf("%08x (%8d) total clocks\n", total_clks, total_clks);
|
99 |
|
|
|
100 |
|
|
std::sort(&pfalt[0], &pfalt[ncnt], altcmp);
|
101 |
|
|
|
102 |
|
|
for(int i=0; i<(int)ncnt; i++)
|
103 |
|
|
printf("%08x: %8d\n", pfalt[i].addr, pfalt[i].clks);
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
printf("%s:\n", fn);
|
107 |
|
|
while((nr=fread(ibuf, sizeof(ZIPI), NZIP, fp))>0) {
|
108 |
|
|
for(int i=0; i<nr; i++) {
|
109 |
|
|
zipi_to_string(ibuf[i], ln);
|
110 |
|
|
// printf("%s\n", ln);
|
111 |
|
|
printf("%08x: (0x%08x %c%c%c%c) ", addr,
|
112 |
|
|
ibuf[i],
|
113 |
|
|
isgraph((ibuf[i]>>24)&0x0ff)?((ibuf[i]>>24)&0x0ff) : '.',
|
114 |
|
|
isgraph((ibuf[i]>>16)&0x0ff)?((ibuf[i]>>16)&0x0ff) : '.',
|
115 |
|
|
isgraph((ibuf[i]>> 8)&0x0ff)?((ibuf[i]>> 8)&0x0ff) : '.',
|
116 |
|
|
isgraph((ibuf[i] )&0x0ff)?((ibuf[i] )&0x0ff) : '.'
|
117 |
|
|
);
|
118 |
|
|
if (pfcnt)
|
119 |
|
|
printf("%8d %8d ", pfcnt[addr-mina], pfclk[addr-mina]);
|
120 |
|
|
printf("%s\n", ln);
|
121 |
|
|
|
122 |
|
|
addr++;
|
123 |
|
|
}
|
124 |
|
|
} fclose(fp);
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
int main(int argc, char **argv) {
|
128 |
|
|
for(int argn=1; argn<argc; argn++) {
|
129 |
|
|
if(access(argv[argn], R_OK)==0)
|
130 |
|
|
dump_file(argv[argn]);
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
return 0;
|
134 |
|
|
}
|
135 |
|
|
|