1 |
204 |
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 zip-objdump program that is part of the binutils 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 Technology, LLC
|
16 |
|
|
//
|
17 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
18 |
|
|
//
|
19 |
|
|
// Copyright (C) 2015,2017, 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 "zipelf.h"
|
47 |
|
|
#include "zopcodes.h"
|
48 |
|
|
#include "byteswap.h"
|
49 |
|
|
|
50 |
|
|
typedef uint32_t ZIPI; // A ZipCPU instruction word
|
51 |
|
|
|
52 |
|
|
typedef struct { unsigned clks, addr; } ALT;
|
53 |
|
|
bool altcmp(const ALT &a, const ALT &b) {
|
54 |
|
|
return a.clks < b.clks;
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
|
58 |
|
|
void dump_file(const char *fn) {
|
59 |
|
|
const int NZIP = 4096;
|
60 |
|
|
char lna[NZIP], lnb[NZIP];
|
61 |
|
|
FILE *pf;
|
62 |
|
|
unsigned addr=0x0100000, mina = -1, maxa = 0,
|
63 |
|
|
*pfcnt = NULL, *pfclk = NULL;
|
64 |
|
|
|
65 |
|
|
pf = fopen("pfile.bin","rb");
|
66 |
|
|
if (pf) {
|
67 |
|
|
ALT *pfalt;
|
68 |
|
|
unsigned buf[2], total_clks = 0;
|
69 |
|
|
while(2 == fread(buf, sizeof(unsigned), 2, pf)) {
|
70 |
|
|
if (mina > buf[0])
|
71 |
|
|
mina = buf[0];
|
72 |
|
|
if (maxa < buf[0])
|
73 |
|
|
maxa = buf[0];
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
addr = mina;
|
77 |
|
|
pfcnt = new unsigned[(maxa+2-mina)];
|
78 |
|
|
pfclk = new unsigned[(maxa+2-mina)];
|
79 |
|
|
pfalt = new ALT[(maxa+2-mina)];
|
80 |
|
|
unsigned ncnt = maxa+2-mina;
|
81 |
|
|
for(int i=0; i<(int)ncnt; i++)
|
82 |
|
|
pfcnt[i] = pfclk[i] = 0;
|
83 |
|
|
for(int i=0; i<(int)ncnt; i++)
|
84 |
|
|
pfalt[i].addr = pfalt[i].clks = 0;
|
85 |
|
|
|
86 |
|
|
rewind(pf);
|
87 |
|
|
while(2 == fread(buf, sizeof(unsigned), 2, pf)) {
|
88 |
|
|
pfcnt[buf[0]-addr]++;
|
89 |
|
|
pfclk[buf[0]-addr] += buf[1];
|
90 |
|
|
pfalt[buf[0]-addr].clks += buf[1];
|
91 |
|
|
pfalt[buf[0]-addr].addr = buf[0];
|
92 |
|
|
total_clks += buf[1];
|
93 |
|
|
|
94 |
|
|
printf("%08x\n", buf[0]);
|
95 |
|
|
} fclose(pf);
|
96 |
|
|
|
97 |
|
|
printf("%08x (%8d) total clocks\n", total_clks, total_clks);
|
98 |
|
|
|
99 |
|
|
std::sort(&pfalt[0], &pfalt[ncnt], altcmp);
|
100 |
|
|
|
101 |
|
|
for(int i=0; i<(int)ncnt; i++)
|
102 |
|
|
printf("%08x: %8d\n", pfalt[i].addr, pfalt[i].clks);
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
printf("%s:\n", fn);
|
106 |
|
|
if (iself(fn)) {
|
107 |
|
|
ELFSECTION **secpp=NULL, *secp;
|
108 |
|
|
unsigned entry;
|
109 |
|
|
elfread(fn, entry, secpp);
|
110 |
|
|
for(int i=0; secpp[i]->m_len; i++) {
|
111 |
|
|
secp = secpp[i];
|
112 |
|
|
for(unsigned j=0; j<secp->m_len; j+=4) {
|
113 |
|
|
uint32_t w, a;
|
114 |
|
|
|
115 |
|
|
a = secp->m_start+(j<<2);
|
116 |
|
|
w = buildword((const unsigned char *)&secp->m_data[(j<<2)]);
|
117 |
|
|
zipi_to_double_string(a, w, lna, lnb);
|
118 |
|
|
// printf("%s\n", ln);
|
119 |
|
|
printf("%08x[%08x-%08x]: (0x%08x %c%c%c%c) ",
|
120 |
|
|
a, maxa, mina, w,
|
121 |
|
|
isgraph((w>>24)&0x0ff)?((w>>24)&0x0ff) : '.',
|
122 |
|
|
isgraph((w>>16)&0x0ff)?((w>>16)&0x0ff) : '.',
|
123 |
|
|
isgraph((w>> 8)&0x0ff)?((w>> 8)&0x0ff) : '.',
|
124 |
|
|
isgraph((w )&0x0ff)?((w )&0x0ff) : '.'
|
125 |
|
|
);
|
126 |
|
|
if ((a>=mina)&&(a<maxa)&&(pfcnt))
|
127 |
|
|
printf("%8d %8d ", pfcnt[a-mina], pfclk[a-mina]);
|
128 |
|
|
printf("%s\n", lna);
|
129 |
|
|
if (lnb[0])
|
130 |
|
|
printf("-%24s%s\n", "", lnb);
|
131 |
|
|
}
|
132 |
|
|
}
|
133 |
|
|
}
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
int main(int argc, char **argv) {
|
137 |
|
|
if (argc <= 1)
|
138 |
|
|
printf("USAGE: pdump <dump-file> | less\n");
|
139 |
|
|
for(int argn=1; argn<argc; argn++) {
|
140 |
|
|
if(access(argv[argn], R_OK)==0)
|
141 |
|
|
dump_file(argv[argn]);
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
return 0;
|
145 |
|
|
}
|
146 |
|
|
|