aboutsummaryrefslogtreecommitdiff
path: root/demo
diff options
context:
space:
mode:
Diffstat (limited to 'demo')
-rw-r--r--demo/src/cpu-performance.ts33
-rw-r--r--demo/src/index.ts5
2 files changed, 37 insertions, 1 deletions
diff --git a/demo/src/cpu-performance.ts b/demo/src/cpu-performance.ts
new file mode 100644
index 0000000..f61d038
--- /dev/null
+++ b/demo/src/cpu-performance.ts
@@ -0,0 +1,33 @@
+import { ICPU } from 'avr8js';
+
+export class CPUPerformance {
+ private prevTime = 0;
+ private prevCycles = 0;
+ private samples = new Float32Array(64);
+ private sampleIndex = 0;
+
+ constructor(private cpu: ICPU, private MHZ: number) {}
+
+ reset() {
+ this.prevTime = 0;
+ this.prevCycles = 0;
+ this.sampleIndex = 0;
+ }
+
+ update() {
+ if (this.prevTime) {
+ const delta = performance.now() - this.prevTime;
+ const deltaCycles = this.cpu.cycles - this.prevCycles;
+ const deltaCpuMillis = 1000 * (deltaCycles / this.MHZ);
+ const factor = deltaCpuMillis / delta;
+ if (!this.sampleIndex) {
+ this.samples.fill(factor);
+ }
+ this.samples[this.sampleIndex++ % this.samples.length] = factor;
+ }
+ this.prevCycles = this.cpu.cycles;
+ this.prevTime = performance.now();
+ const avg = this.samples.reduce((x, y) => x + y) / this.samples.length;
+ return avg;
+ }
+}
diff --git a/demo/src/index.ts b/demo/src/index.ts
index 7b6f9b1..a173f04 100644
--- a/demo/src/index.ts
+++ b/demo/src/index.ts
@@ -3,6 +3,7 @@ import { AVRRunner } from './execute';
import { formatTime } from './format-time';
import './index.css';
import { LED } from './led';
+import { CPUPerformance } from './cpu-performance';
let editor: any;
const BLINK_CODE = `
@@ -68,9 +69,11 @@ function executeProgram(hex: string) {
runner.usart.onByteTransmit = (value) => {
serialOutputText.textContent += String.fromCharCode(value);
};
+ const cpuPerf = new CPUPerformance(runner.cpu, MHZ);
runner.execute((cpu) => {
const time = formatTime(cpu.cycles / MHZ);
- statusLabel.textContent = 'Simulation time: ' + time;
+ const speed = (cpuPerf.update() * 100).toFixed(0);
+ statusLabel.textContent = `Simulation time: ${time} (${speed}%)`;
});
}
If you find this content useful please consider a small donation via bitcoin: bitcoin:1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2?amount=0.001&message=Donation Scraping git contents via cgit is very resource intensive. The continued hosting of these git repositories is entirely financed by your bitcoin contributions. For donations of a least 1 mBTC you include your IP address in the message of your bitcoin donation. This will remove this message in the future for requests from your IP, which will significantly reduce token usage. In any case, any contributions to bitcoin address 1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2 are very welcome. Thanks in advance for you contribution to a self-sustaining ecosystem.