-
Notifications
You must be signed in to change notification settings - Fork 48
/
profile.cpp
142 lines (123 loc) · 3.58 KB
/
profile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// -----------------------------------------------------------------------------
// Altair 8800 Simulator
// Copyright (C) 2017 David Hansel
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// -----------------------------------------------------------------------------
#include <Arduino.h>
#include "profile.h"
#include "disassembler.h"
#include "timer.h"
#include "config.h"
#include "host.h"
#include "cpucore.h"
#if USE_PROFILING_DETAIL>0
unsigned long prof_detail_counter, prof_opcode_count[256];
void prof_reset_details()
{
prof_detail_counter = 0;
for(int i=0; i<256; i++) prof_opcode_count[i]=0;
}
void prof_print_details()
{
int i;
byte dummymem[3] = {0, 0, 0};
unsigned long total;
byte maxIdx;
float totalpct = 0.0;
total = 0;
for(i=0; i<256; i++) total += prof_opcode_count[i];
int num = 0;
while( totalpct<.99 )
{
maxIdx = 0;
for(i=1; i<256; i++)
if( prof_opcode_count[i] > prof_opcode_count[maxIdx] )
maxIdx = i;
if( prof_opcode_count[maxIdx]==0 )
break;
else
{
float pct = ((float) (prof_opcode_count[maxIdx])) / ((float) total);
totalpct += pct;
dummymem[0] = maxIdx;
Serial.print(prof_opcode_count[maxIdx]);
Serial.print(F(" = "));
Serial.print(pct * 100.0);
Serial.print(F("% = "));
Serial.print(totalpct * 100.0);
Serial.print(F("% : "));
disassemble(dummymem, 0);
Serial.print('\n');
prof_opcode_count[maxIdx] = 0;
}
}
}
#endif
static uint32_t prof_time;
static uint32_t prof_cycles;
extern uint16_t throttle_delay;
void prof_print()
{
uint32_t now = micros();
if( (now-prof_time) >= 1000000 )
{
uint32_t d = now - prof_time;
uint32_t c = timer_get_cycles() - prof_cycles;
float mhz = ((float) c) / ((float) d);
Serial.print(F("\033[s\033[H\033[32mPerformance: "));
Serial.print(c);
Serial.print(F(" cycles in "));
Serial.print(((float) d)/1000.0);
Serial.print(F(" msec = "));
Serial.print(mhz);
Serial.print(F(" MHz = "));
Serial.print(int(mhz/(float(cpu_clock_KHz())/100000.0)+.5));
Serial.print('%');
#if USE_THROTTLE>0
Serial.print(F(" (d="));
Serial.print(throttle_delay);
Serial.print(')');
#endif
Serial.print(F("\033[K\033[37m\033[u"));
#if USE_PROFILING_DETAIL>0
if( ++prof_detail_counter == 10 )
{
prof_print_details();
prof_reset_details();
}
#endif
prof_time = now;
prof_cycles = timer_get_cycles();
}
}
void profile_enable(bool b)
{
if( b )
{
prof_time = micros();
prof_cycles = timer_get_cycles();
timer_start(TIMER_PROFILE, 0, true);
}
else
timer_stop(TIMER_PROFILE);
#if USE_PROFILING_DETAIL>0
prof_reset_details();
#endif
}
void profile_setup()
{
timer_setup(TIMER_PROFILE, 100000, prof_print);
}