OpenCores
URL https://opencores.org/ocsvn/zipcpu/zipcpu/trunk

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [bench/] [cpp/] [pdump.cpp] - Blame information for rev 77

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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 69 dgisselq
//              Gisselquist Technology, LLC
16 58 dgisselq
//
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 75 dgisselq
        char    lna[NZIP], lnb[NZIP];
56 58 dgisselq
        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 75 dgisselq
                        zipi_to_string(ibuf[i], lna, lnb);
110 58 dgisselq
                        // 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 75 dgisselq
                        printf("%s\n", lna);
121
                        if (lnb[0])
122
                                printf("%26s%s\n", "", lnb);
123 58 dgisselq
 
124
                        addr++;
125
                }
126
        } fclose(fp);
127
}
128
 
129
int main(int argc, char **argv) {
130 75 dgisselq
        if (argc <= 1)
131
                printf("USAGE: pdump <dump-file> | less\n");
132 58 dgisselq
        for(int argn=1; argn<argc; argn++) {
133
                if(access(argv[argn], R_OK)==0)
134
                        dump_file(argv[argn]);
135
        }
136
 
137
        return 0;
138
}
139
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.