TWLab commited on
Commit
ee5f65f
·
verified ·
1 Parent(s): d202df6

Upload streamlit_app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. streamlit_app.py +607 -0
streamlit_app.py ADDED
@@ -0,0 +1,607 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>BioSense FPGA 调试终端</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <script>
9
+ tailwind.config = {
10
+ theme: {
11
+ extend: {
12
+ colors: {
13
+ 'fpga-bg': '#0f172a',
14
+ 'fpga-panel': '#1e293b',
15
+ 'fpga-border': '#334155',
16
+ 'fpga-accent': '#0ea5e9', // Sky 500
17
+ 'fpga-success': '#10b981', // Emerald 500
18
+ 'fpga-warning': '#f59e0b', // Amber 500
19
+ 'fpga-danger': '#ef4444', // Red 500
20
+ 'fpga-text': '#e2e8f0',
21
+ 'fpga-dim': '#64748b',
22
+ },
23
+ fontFamily: {
24
+ mono: ['ui-monospace', 'SFMono-Regular', 'Menlo', 'Monaco', 'Consolas', "Liberation Mono", "Courier New", 'monospace'],
25
+ }
26
+ }
27
+ }
28
+ }
29
+ </script>
30
+ <style>
31
+ /* Custom Scrollbar */
32
+ ::-webkit-scrollbar {
33
+ width: 8px;
34
+ height: 8px;
35
+ }
36
+ ::-webkit-scrollbar-track {
37
+ background: #0f172a;
38
+ }
39
+ ::-webkit-scrollbar-thumb {
40
+ background: #334155;
41
+ border-radius: 4px;
42
+ }
43
+ ::-webkit-scrollbar-thumb:hover {
44
+ background: #475569;
45
+ }
46
+
47
+ /* CRT Scanline Effect */
48
+ .crt::before {
49
+ content: " ";
50
+ display: block;
51
+ position: absolute;
52
+ top: 0;
53
+ left: 0;
54
+ bottom: 0;
55
+ right: 0;
56
+ background: linear-gradient(rgba(18, 16, 16, 0) 50%, rgba(0, 0, 0, 0.25) 50%), linear-gradient(90deg, rgba(255, 0, 0, 0.06), rgba(0, 255, 0, 0.02), rgba(0, 0, 255, 0.06));
57
+ z-index: 2;
58
+ background-size: 100% 2px, 3px 100%;
59
+ pointer-events: none;
60
+ }
61
+
62
+ .canvas-container {
63
+ background-image:
64
+ linear-gradient(rgba(255, 255, 255, 0.03) 1px, transparent 1px),
65
+ linear-gradient(90deg, rgba(255, 255, 255, 0.03) 1px, transparent 1px);
66
+ background-size: 20px 20px;
67
+ }
68
+
69
+ /* Range Slider Styling */
70
+ input[type=range] {
71
+ -webkit-appearance: none;
72
+ background: transparent;
73
+ }
74
+ input[type=range]::-webkit-slider-thumb {
75
+ -webkit-appearance: none;
76
+ height: 16px;
77
+ width: 16px;
78
+ border-radius: 50%;
79
+ background: #0ea5e9;
80
+ cursor: pointer;
81
+ margin-top: -6px;
82
+ }
83
+ input[type=range]::-webkit-slider-runnable-track {
84
+ width: 100%;
85
+ height: 4px;
86
+ cursor: pointer;
87
+ background: #334155;
88
+ border-radius: 2px;
89
+ }
90
+ </style>
91
+ </head>
92
+ <body class="bg-fpga-bg text-fpga-text font-sans overflow-hidden h-screen flex flex-col selection:bg-fpga-accent selection:text-white">
93
+
94
+ <!-- Top Navigation / Toolbar -->
95
+ <header class="h-14 bg-fpga-panel border-b border-fpga-border flex items-center px-4 justify-between shrink-0 z-20">
96
+ <div class="flex items-center gap-3">
97
+ <div class="w-8 h-8 bg-gradient-to-br from-blue-600 to-cyan-400 rounded flex items-center justify-center shadow-lg shadow-blue-500/20">
98
+ <svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"></path></svg>
99
+ </div>
100
+ <div>
101
+ <h1 class="text-sm font-bold tracking-wide text-white">BioSense <span class="text-fpga-accent">Debugger</span></h1>
102
+ <div class="flex items-center gap-2 text-[10px] text-fpga-dim font-mono">
103
+ <span class="w-2 h-2 rounded-full bg-fpga-success animate-pulse"></span>
104
+ FPGA DEVKIT v4.2 [READY]
105
+ </div>
106
+ </div>
107
+ </div>
108
+
109
+ <div class="flex items-center gap-4">
110
+ <!-- Connection Controls -->
111
+ <div class="flex bg-fpga-bg rounded p-1 border border-fpga-border">
112
+ <button onclick="app.toggleConnection()" id="btn-connect" class="px-3 py-1 text-xs font-mono rounded hover:bg-fpga-border transition-colors text-fpga-success">CONNECT_JTAG</button>
113
+ <div class="w-px bg-fpga-border mx-1"></div>
114
+ <button class="px-3 py-1 text-xs font-mono rounded hover:bg-fpga-border transition-colors text-fpga-accent">FLASH_BITSTREAM</button>
115
+ </div>
116
+
117
+ <!-- View Controls -->
118
+ <div class="flex bg-fpga-bg rounded p-1 border border-fpga-border">
119
+ <button onclick="app.setMode('digital')" class="px-3 py-1 text-xs font-mono rounded hover:bg-fpga-border transition-colors text-fpga-dim hover:text-white" id="mode-digital">DIGITAL</button>
120
+ <button onclick="app.setMode('mixed')" class="px-3 py-1 text-xs font-mono rounded bg-fpga-border text-white transition-colors" id="mode-mixed">MIXED</button>
121
+ <button onclick="app.setMode('fft')" class="px-3 py-1 text-xs font-mono rounded hover:bg-fpga-border transition-colors text-fpga-dim hover:text-white" id="mode-fft">FFT</button>
122
+ </div>
123
+ </div>
124
+ </header>
125
+
126
+ <!-- Main Workspace -->
127
+ <div class="flex-1 flex overflow-hidden">
128
+
129
+ <!-- Left Sidebar: Channels -->
130
+ <aside class="w-64 bg-fpga-panel border-r border-fpga-border flex flex-col shrink-0 z-10">
131
+ <div class="p-3 border-b border-fpga-border flex justify-between items-center">
132
+ <span class="text-xs font-bold text-fpga-dim uppercase tracking-wider">Channels</span>
133
+ <span class="text-[10px] bg-fpga-bg px-2 py-0.5 rounded border border-fpga-border text-fpga-accent">8 ACTIVE</span>
134
+ </div>
135
+
136
+ <div class="flex-1 overflow-y-auto p-2 space-y-2" id="channel-list">
137
+ <!-- Channels injected by JS -->
138
+ </div>
139
+
140
+ <div class="p-3 border-t border-fpga-border bg-fpga-bg/50">
141
+ <div class="text-[10px] text-fpga-dim mb-2 uppercase font-bold">Trigger Settings</div>
142
+ <div class="flex items-center gap-2 mb-2">
143
+ <div class="w-2 h-2 rounded-full bg-fpga-warning"></div>
144
+ <span class="text-xs font-mono">CH2 Rising</span>
145
+ </div>
146
+ <div class="h-1 w-full bg-fpga-border rounded overflow-hidden">
147
+ <div class="h-full bg-fpga-warning w-1/3"></div>
148
+ </div>
149
+ </div>
150
+ </aside>
151
+
152
+ <!-- Center: Canvas Display -->
153
+ <main class="flex-1 flex flex-col min-w-0 bg-black relative">
154
+ <!-- Toolbar Overlay -->
155
+ <div class="absolute top-4 left-4 right-4 flex justify-between pointer-events-none z-10">
156
+ <div class="bg-fpga-panel/90 backdrop-blur border border-fpga-border rounded px-3 py-1 pointer-events-auto shadow-lg">
157
+ <span class="text-xs text-fpga-dim font-mono mr-2">TIMEBASE:</span>
158
+ <span class="text-xs text-white font-mono font-bold" id="timebase-display">50 ms/div</span>
159
+ </div>
160
+ <div class="bg-fpga-panel/90 backdrop-blur border border-fpga-border rounded px-3 py-1 pointer-events-auto shadow-lg flex gap-2">
161
+ <button onclick="app.zoomTime(1.2)" class="hover:text-fpga-accent">-</button>
162
+ <button onclick="app.zoomTime(0.8)" class="hover:text-fpga-accent">+</button>
163
+ </div>
164
+ </div>
165
+
166
+ <!-- Canvas Wrapper -->
167
+ <div class="flex-1 relative canvas-container overflow-hidden crt" id="canvas-wrapper">
168
+ <canvas id="mainCanvas" class="absolute top-0 left-0 w-full h-full block"></canvas>
169
+
170
+ <!-- Floating Stats -->
171
+ <div class="absolute bottom-4 left-4 bg-fpga-panel/80 backdrop-blur border border-fpga-border rounded p-3 shadow-lg">
172
+ <div class="grid grid-cols-2 gap-x-6 gap-y-1 text-xs font-mono">
173
+ <span class="text-fpga-dim">FPS:</span> <span class="text-fpga-accent text-right" id="fps-counter">60</span>
174
+ <span class="text-fpga-dim">Samples:</span> <span class="text-white text-right">4096</span>
175
+ <span class="text-fpga-dim">Bandwidth:</span> <span class="text-white text-right">1.2 GB/s</span>
176
+ </div>
177
+ </div>
178
+ </div>
179
+
180
+ <!-- Bottom Status Bar -->
181
+ <div class="h-8 bg-fpga-panel border-t border-fpga-border flex items-center px-4 justify-between text-[10px] font-mono shrink-0">
182
+ <div class="flex gap-4">
183
+ <span class="text-fpga-success">SYSTEM OK</span>
184
+ <span class="text-fpga-dim">PLL_LOCKED</span>
185
+ <span class="text-fpga-dim">ADC_0: 0x4F2A</span>
186
+ </div>
187
+ <div class="flex gap-4">
188
+ <span class="text-fpga-warning">WARN: FAN_SPEED > 80%</span>
189
+ <span class="text-fpga-dim">uptime: 14h 22m</span>
190
+ </div>
191
+ </div>
192
+ </main>
193
+
194
+ <!-- Right Sidebar: Properties & Controls -->
195
+ <aside class="w-72 bg-fpga-panel border-l border-fpga-border flex flex-col shrink-0 z-10 overflow-y-auto">
196
+
197
+ <!-- FPGA Register Map -->
198
+ <div class="p-4 border-b border-fpga-border">
199
+ <div class="flex justify-between items-center mb-3">
200
+ <h3 class="text-xs font-bold text-fpga-accent uppercase">Register Map</h3>
201
+ <button class="text-[10px] text-fpga-dim hover:text-white">REFRESH</button>
202
+ </div>
203
+ <div class="space-y-1 font-mono text-[11px]" id="register-list">
204
+ <!-- Registers injected by JS -->
205
+ </div>
206
+ </div>
207
+
208
+ <!-- Sensor Configuration -->
209
+ <div class="p-4 border-b border-fpga-border">
210
+ <h3 class="text-xs font-bold text-fpga-dim uppercase mb-3">Bio-Sensor Config</h3>
211
+
212
+ <div class="space-y-4">
213
+ <div>
214
+ <label class="block text-[10px] text-fpga-dim mb-1">Gain Control (dB)</label>
215
+ <div class="flex items-center gap-2">
216
+ <input type="range" min="0" max="60" value="24" class="w-full" oninput="document.getElementById('gain-val').innerText = this.value">
217
+ <span id="gain-val" class="text-xs font-mono w-6 text-right">24</span>
218
+ </div>
219
+ </div>
220
+
221
+ <div>
222
+ <label class="block text-[10px] text-fpga-dim mb-1">Filter Cutoff (Hz)</label>
223
+ <select class="w-full bg-fpga-bg border border-fpga-border rounded text-xs p-1 text-white outline-none focus:border-fpga-accent">
224
+ <option>100 Hz</option>
225
+ <option>1 kHz</option>
226
+ <option selected>10 kHz</option>
227
+ <option>50 kHz</option>
228
+ </select>
229
+ </div>
230
+
231
+ <div class="flex items-center justify-between">
232
+ <label class="text-xs text-fpga-dim">Impedance Check</label>
233
+ <button class="w-8 h-4 bg-fpga-accent rounded-full relative transition-colors" onclick="this.classList.toggle('bg-fpga-dim'); this.classList.toggle('bg-fpga-accent')">
234
+ <div class="absolute right-0.5 top-0.5 w-3 h-3 bg-white rounded-full shadow"></div>
235
+ </button>
236
+ </div>
237
+ </div>
238
+ </div>
239
+
240
+ <!-- Data Export -->
241
+ <div class="p-4">
242
+ <h3 class="text-xs font-bold text-fpga-dim uppercase mb-3">Data Export</h3>
243
+ <div class="grid grid-cols-2 gap-2">
244
+ <button class="bg-fpga-bg border border-fpga-border text-xs py-2 rounded hover:bg-fpga-border hover:text-white transition-colors">.CSV</button>
245
+ <button class="bg-fpga-bg border border-fpga-border text-xs py-2 rounded hover:bg-fpga-border hover:text-white transition-colors">.WAV</button>
246
+ <button class="col-span-2 bg-fpga-accent/20 border border-fpga-accent text-fpga-accent text-xs py-2 rounded hover:bg-fpga-accent hover:text-white transition-colors font-bold">CAPTURE FRAME</button>
247
+ </div>
248
+ </div>
249
+
250
+ </aside>
251
+ </div>
252
+
253
+ <script>
254
+ /**
255
+ * BioSense FPGA Debugger Logic
256
+ * Handles Canvas rendering, simulated data generation, and UI interactions.
257
+ */
258
+ const app = {
259
+ canvas: null,
260
+ ctx: null,
261
+ width: 0,
262
+ height: 0,
263
+ dpr: 1,
264
+
265
+ // State
266
+ channels: [],
267
+ registers: [],
268
+ isRunning: true,
269
+ timeScale: 0.05, // seconds per pixel approx
270
+ mode: 'mixed', // digital, mixed, fft
271
+ dataOffset: 0,
272
+
273
+ init() {
274
+ this.canvas = document.getElementById('mainCanvas');
275
+ this.ctx = this.canvas.getContext('2d');
276
+ this.dpr = window.devicePixelRatio || 1;
277
+
278
+ // Initialize Channels
279
+ this.channels = [
280
+ { id: 0, name: 'CLK_SYS', type: 'digital', color: '#ef4444', visible: true, data: [] },
281
+ { id: 1, name: 'STATE_MACH', type: 'digital', color: '#f59e0b', visible: true, data: [] },
282
+ { id: 2, name: 'ADC_RAW', type: 'analog', color: '#0ea5e9', visible: true, data: [], offset: 0 },
283
+ { id: 3, name: 'FILTER_OUT', type: 'analog', color: '#10b981', visible: true, data: [], offset: 0 },
284
+ { id: 4, name: 'IMPEDANCE', type: 'analog', color: '#a855f7', visible: false, data: [], offset: 0 },
285
+ ];
286
+
287
+ // Initialize Registers
288
+ this.registers = [
289
+ { addr: '0x00', name: 'CTRL_REG', val: '0x0001' },
290
+ { addr: '0x04', name: 'STAT_REG', val: '0x00F2' },
291
+ { addr: '0x08', name: 'ADC_DATA', val: '0x1A4F' },
292
+ { addr: '0x0C', name: 'DAC_VOL', val: '0x0800' },
293
+ { addr: '0x10', name: 'I2C_STAT', val: '0x0000' },
294
+ { addr: '0x14', name: 'PLL_DIV', val: '0x0010' },
295
+ { addr: '0x18', name: 'FIFO_CNT', val: '0x03FF' },
296
+ { addr: '0x1C', name: 'IRQ_MASK', val: '0x0000' },
297
+ ];
298
+
299
+ this.resize();
300
+ window.addEventListener('resize', () => this.resize());
301
+
302
+ this.renderUI();
303
+ this.loop();
304
+ },
305
+
306
+ resize() {
307
+ const parent = this.canvas.parentElement;
308
+ this.width = parent.clientWidth;
309
+ this.height = parent.clientHeight;
310
+
311
+ this.canvas.width = this.width * this.dpr;
312
+ this.canvas.height = this.height * this.dpr;
313
+
314
+ this.canvas.style.width = `${this.width}px`;
315
+ this.canvas.style.height = `${this.height}px`;
316
+
317
+ this.ctx.scale(this.dpr, this.dpr);
318
+ },
319
+
320
+ // Generate simulated data points
321
+ generateData() {
322
+ const now = Date.now();
323
+
324
+ this.channels.forEach(ch => {
325
+ // Keep a buffer of data points. We use a circular buffer concept but simple array push is fine for simulation.
326
+ // In a real app, we'd use a fixed size array.
327
+ const point = { x: now };
328
+
329
+ if (ch.type === 'digital') {
330
+ // Create square waves with noise/jitter
331
+ const freq = ch.id === 0 ? 5 : 2; // Hz
332
+ const val = Math.sin(now / 1000 * Math.PI * 2 * freq) > 0 ? 1 : 0;
333
+ // Add glitch
334
+ point.y = Math.random() > 0.98 ? (val ? 0 : 1) : val;
335
+ } else {
336
+ // Analog signals
337
+ let val = 0;
338
+ if (ch.name === 'ADC_RAW') {
339
+ // Simulated Bio-Signal (ECG-ish)
340
+ const t = now / 1000;
341
+ val = Math.sin(t * 10) * 20 +
342
+ (Math.sin(t * 50) > 0.8 ? Math.sin(t * 50) * 40 : 0) + // "QRS complex"
343
+ (Math.random() - 0.5) * 5; // Noise
344
+ } else if (ch.name === 'FILTER_OUT') {
345
+ // Low passed version
346
+ const t = now / 1000;
347
+ val = Math.sin(t * 10) * 18 +
348
+ (Math.sin(t * 50) > 0.8 ? Math.sin(t * 50) * 15 : 0);
349
+ } else {
350
+ // Random noise
351
+ val = (Math.random() - 0.5) * 10;
352
+ }
353
+ point.y = val;
354
+ }
355
+
356
+ ch.data.push(point);
357
+ // Trim old data to prevent memory leak and keep performance
358
+ if (ch.data.length > 2000) ch.data.shift();
359
+ });
360
+ },
361
+
362
+ drawGrid() {
363
+ const ctx = this.ctx;
364
+ const w = this.width;
365
+ const h = this.height;
366
+
367
+ ctx.strokeStyle = 'rgba(255, 255, 255, 0.05)';
368
+ ctx.lineWidth = 1;
369
+ ctx.beginPath();
370
+
371
+ // Vertical lines (Time)
372
+ const timeStep = 50; // px
373
+ for (let x = 0; x < w; x += timeStep) {
374
+ ctx.moveTo(x, 0);
375
+ ctx.lineTo(x, h);
376
+ }
377
+
378
+ // Horizontal lines (Voltage/Logic Level)
379
+ const voltStep = h / (this.mode === 'mixed' ? 8 : 4);
380
+ for (let y = 0; y < h; y += voltStep) {
381
+ ctx.moveTo(0, y);
382
+ ctx.lineTo(w, y);
383
+ }
384
+ ctx.stroke();
385
+
386
+ // Center line
387
+ ctx.strokeStyle = 'rgba(14, 165, 233, 0.2)';
388
+ ctx.beginPath();
389
+ ctx.moveTo(0, h/2);
390
+ ctx.lineTo(w, h/2);
391
+ ctx.stroke();
392
+ },
393
+
394
+ drawTrace(ch, yOffset, slotHeight) {
395
+ const ctx = this.ctx;
396
+ const data = ch.data;
397
+ if (data.length < 2) return;
398
+
399
+ ctx.strokeStyle = ch.color;
400
+ ctx.lineWidth = 2;
401
+ ctx.beginPath();
402
+
403
+ // We draw from right to left (newest to oldest) to align with screen edge
404
+ // Or rather, standard logic: map time to x.
405
+ // Let's map the last N points to the width.
406
+
407
+ const rightEdgeTime = data[data.length - 1].x;
408
+ const pxPerMs = this.width / 2000; // Show last 2 seconds worth of pixels roughly
409
+
410
+ let first = true;
411
+
412
+ for (let i = data.length - 1; i >= 0; i--) {
413
+ const point = data[i];
414
+ const age = rightEdgeTime - point.x;
415
+ const x = this.width - (age * pxPerMs); // Moving left from right edge
416
+
417
+ if (x < 0) break; // Off screen
418
+
419
+ if (ch.type === 'digital') {
420
+ // Draw stepped line
421
+ const yHigh = yOffset - slotHeight * 0.2;
422
+ const yLow = yOffset + slotHeight * 0.2;
423
+ const y = point.y ? yHigh : yLow;
424
+
425
+ if (first) {
426
+ ctx.moveTo(x, y);
427
+ first = false;
428
+ } else {
429
+ // Connect with step
430
+ const prevX = this.width - ((rightEdgeTime - data[i+1].x) * pxPerMs);
431
+ ctx.lineTo(prevX - 1, y); // Horizontal
432
+ ctx.lineTo(x, y); // Vertical
433
+ }
434
+
435
+ // Draw value label on right
436
+ if (i === data.length - 1) {
437
+ ctx.fillStyle = ch.color;
438
+ ctx.font = '10px monospace';
439
+ ctx.fillText(point.y, x + 10, y + 3);
440
+ }
441
+
442
+ } else {
443
+ // Analog
444
+ // Scale y. Center is 0.
445
+ const scale = slotHeight / 100; // arbitrary scale
446
+ const y = yOffset - (point.y * scale);
447
+
448
+ if (first) {
449
+ ctx.moveTo(x, y);
450
+ first = false;
451
+ } else {
452
+ ctx.lineTo(x, y);
453
+ }
454
+
455
+ // Draw latest value
456
+ if (i === data.length - 1) {
457
+ ctx.fillStyle = '#fff';
458
+ ctx.fillRect(x - 2, y - 2, 4, 4);
459
+ ctx.fillStyle = ch.color;
460
+ ctx.fillText(point.y.toFixed(1), x + 10, y + 3);
461
+ }
462
+ }
463
+ }
464
+ ctx.stroke();
465
+ },
466
+
467
+ render() {
468
+ // Clear
469
+ this.ctx.clearRect(0, 0, this.width, this.height);
470
+
471
+ this.drawGrid();
472
+
473
+ // Calculate slots for channels
474
+ const visibleChannels = this.channels.filter(c => c.visible);
475
+ const slotHeight = this.height / visibleChannels.length;
476
+
477
+ visibleChannels.forEach((ch, index) => {
478
+ const yCenter = (index * slotHeight) + (slotHeight / 2);
479
+
480
+ // Draw Channel Label Background
481
+ this.ctx.fillStyle = 'rgba(0,0,0,0.5)';
482
+ this.ctx.fillRect(5, yCenter - 10, 80, 20);
483
+
484
+ // Label
485
+ this.ctx.fillStyle = ch.color;
486
+ this.ctx.font = 'bold 11px monospace';
487
+ this.ctx.fillText(ch.name, 10, yCenter + 4);
488
+
489
+ // Draw Trace
490
+ this.drawTrace(ch, yCenter, slotHeight);
491
+ });
492
+ },
493
+
494
+ loop() {
495
+ if (this.isRunning) {
496
+ this.generateData();
497
+ this.render();
498
+
499
+ // Randomly update registers
500
+ if (Math.random() > 0.95) {
501
+ this.updateRegisters();
502
+ }
503
+ }
504
+ requestAnimationFrame(() => this.loop());
505
+ },
506
+
507
+ updateRegisters() {
508
+ // Simulate changing values
509
+ const r = this.registers[2]; // ADC_DATA
510
+ const val = parseInt(r.val, 16);
511
+ const newVal = val + Math.floor((Math.random() - 0.5) * 20);
512
+ r.val = `0x${(newVal & 0xFFFF).toString(16).toUpperCase().padStart(4, '0')}`;
513
+ this.renderUI();
514
+ },
515
+
516
+ renderUI() {
517
+ // Render Channel List
518
+ const list = document.getElementById('channel-list');
519
+ list.innerHTML = this.channels.map(ch => `
520
+ <div class="flex items-center justify-between bg-fpga-bg p-2 rounded border border-fpga-border hover:border-fpga-accent transition-colors cursor-pointer group" onclick="app.toggleChannel(${ch.id})">
521
+ <div class="flex items-center gap-2">
522
+ <div class="w-2 h-2 rounded-full" style="background-color: ${ch.visible ? ch.color : '#475569'}"></div>
523
+ <span class="text-xs font-mono ${ch.visible ? 'text-white' : 'text-fpga-dim'}">${ch.name}</span>
524
+ </div>
525
+ <div class="text-[10px] text-fpga-dim group-hover:text-white">${ch.type.toUpperCase()}</div>
526
+ </div>
527
+ `).join('');
528
+
529
+ // Render Registers
530
+ const regList = document.getElementById('register-list');
531
+ regList.innerHTML = this.registers.map(r => `
532
+ <div class="flex justify-between border-b border-fpga-border/50 py-1 last:border-0">
533
+ <span class="text-fpga-dim w-12">${r.addr}</span>
534
+ <span class="text-white font-bold w-20">${r.name}</span>
535
+ <span class="text-fpga-accent text-right w-16">${r.val}</span>
536
+ </div>
537
+ `).join('');
538
+ },
539
+
540
+ // Interaction Methods
541
+ toggleChannel(id) {
542
+ const ch = this.channels.find(c => c.id === id);
543
+ if (ch) ch.visible = !ch.visible;
544
+ this.renderUI();
545
+ },
546
+
547
+ setMode(mode) {
548
+ this.mode = mode;
549
+ // Update buttons
550
+ ['digital', 'mixed', 'fft'].forEach(m => {
551
+ const btn = document.getElementById(`mode-${m}`);
552
+ if (m === mode) {
553
+ btn.classList.remove('text-fpga-dim', 'hover:text-white');
554
+ btn.classList.add('bg-fpga-border', 'text-white');
555
+ } else {
556
+ btn.classList.add('text-fpga-dim', 'hover:text-white');
557
+ btn.classList.remove('bg-fpga-border', 'text-white');
558
+ }
559
+ });
560
+
561
+ // Reset visibility based on mode
562
+ if (mode === 'digital') {
563
+ this.channels.forEach(c => c.visible = c.type === 'digital');
564
+ } else if (mode === 'mixed') {
565
+ this.channels.forEach(c => c.visible = true); // Show all
566
+ } else if (mode === 'fft') {
567
+ // FFT mode would change rendering logic significantly,
568
+ // for this demo we'll just show analog channels
569
+ this.channels.forEach(c => c.visible = c.type === 'analog');
570
+ }
571
+ this.renderUI();
572
+ },
573
+
574
+ toggleConnection() {
575
+ const btn = document.getElementById('btn-connect');
576
+ if (btn.innerText === 'CONNECT_JTAG') {
577
+ btn.innerText = 'DISCONNECT';
578
+ btn.classList.remove('text-fpga-success');
579
+ btn.classList.add('text-fpga-danger');
580
+ this.isRunning = true;
581
+ } else {
582
+ btn.innerText = 'CONNECT_JTAG';
583
+ btn.classList.add('text-fpga-success');
584
+ btn.classList.remove('text-fpga-danger');
585
+ this.isRunning = false;
586
+ }
587
+ },
588
+
589
+ zoomTime(factor) {
590
+ // Adjust simulation speed or scale
591
+ // In a real app, this changes the timebase of the scope
592
+ // Here we just update text for effect
593
+ const display = document.getElementById('timebase-display');
594
+ let val = parseFloat(display.innerText);
595
+ val *= factor;
596
+ display.innerText = `${val.toFixed(1)} ms/div`;
597
+ }
598
+ };
599
+
600
+ // Start application
601
+ document.addEventListener('DOMContentLoaded', () => {
602
+ app.init();
603
+ });
604
+
605
+ </script>
606
+ </body>
607
+ </html>