Skip to content

Commit

Permalink
feat: ui_in buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
urish committed May 21, 2024
1 parent 845bca8 commit a36d76b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@
<div id="code-editor"></div>
<div id="vga-canvas-container">
<span id="fps-display">FPS: <span id="fps-count">00</span></span>
<div id="input-values">
ui_in:
<button>0</button>
<button>1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button>5</button>
<button>6</button>
<button>7</button>
</div>
<canvas width="736" height="520" id="vga-canvas"></canvas>
</div>
</main>
Expand Down
10 changes: 10 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@ main {
cursor: pointer;
fill: white;
}

#input-values {
padding: 8px 8px;
color: #ccc;
background: #333;
}

#input-values button.active {
background: #0f0;
}
26 changes: 26 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { compileVerilator } from './verilator/compile';

let currentProject = structuredClone(examples[0]);

const inputButtons = Array.from(document.querySelectorAll('#input-values button'));

const codeEditorDiv = document.getElementById('code-editor');
const editor = monaco.editor.create(codeEditorDiv!, {
value: currentProject.sources['project.v'],
Expand Down Expand Up @@ -81,6 +83,7 @@ editor.onDidChangeModelContent(async () => {
if (jmod) {
jmod.dispose();
}
inputButtons.map((b) => b.classList.remove('active'));
jmod = new HDLModuleWASM(res.output.modules['TOP'], res.output.modules['@CONST-POOL@']);
await jmod.init();
reset();
Expand Down Expand Up @@ -168,3 +171,26 @@ window.addEventListener('visibilitychange', () => {
document.querySelector('#download-button')?.addEventListener('click', () => {
exportProject(currentProject);
});

function toggleButton(index: number) {
const bit = 1 << index;
jmod.state.ui_in = jmod.state.ui_in ^ bit;
if (jmod.state.ui_in & bit) {
inputButtons[index].classList.add('active');
} else {
inputButtons[index].classList.remove('active');
}
}

document.addEventListener('keydown', (e) => {
if ('r' === e.key) {
reset();
}
if (['0', '1', '2', '3', '4', '5', '6', '7'].includes(e.key)) {
toggleButton(parseInt(e.key, 10));
}
});

inputButtons.forEach((button, index) => {
button.addEventListener('click', () => toggleButton(index));
});

0 comments on commit a36d76b

Please sign in to comment.