|
| 1 | +import { buildHex } from './compile'; |
| 2 | +import './index.css'; |
| 3 | +import { AVRRunner } from './execute'; |
| 4 | +import { formatTime } from './format-time'; |
| 5 | +import { LED } from './led'; |
| 6 | + |
| 7 | +let editor: any; |
| 8 | +const BLINK_CODE = ` |
| 9 | +// Green LED connected to LED_BUILTIN, |
| 10 | +// Red LED connected to pin 12. Enjoy! |
| 11 | +
|
| 12 | +void setup() { |
| 13 | + pinMode(LED_BUILTIN, OUTPUT); |
| 14 | +} |
| 15 | +
|
| 16 | +void loop() { |
| 17 | + digitalWrite(LED_BUILTIN, HIGH); |
| 18 | + delay(500); |
| 19 | + digitalWrite(LED_BUILTIN, LOW); |
| 20 | + delay(500); |
| 21 | +}`.trim(); |
| 22 | + |
| 23 | +// Load Editor |
| 24 | +declare var window: any; |
| 25 | +declare var monaco: any; |
| 26 | +window.require.config({ |
| 27 | + paths: { vs: 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.18.0/min/vs' } |
| 28 | +}); |
| 29 | +window.require(['vs/editor/editor.main'], () => { |
| 30 | + editor = monaco.editor.create(document.querySelector('.code-editor'), { |
| 31 | + value: BLINK_CODE, |
| 32 | + language: 'cpp', |
| 33 | + minimap: { enabled: false } |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +// Set up LEDs |
| 38 | +const leds = document.querySelector('.leds'); |
| 39 | +const led13 = new LED({ color: 'green', lightColor: '#80ff80' }); |
| 40 | +const led12 = new LED({ color: 'red', lightColor: '#ff8080' }); |
| 41 | +leds.appendChild(led13.el); |
| 42 | +leds.appendChild(led12.el); |
| 43 | + |
| 44 | +// Set up toolbar |
| 45 | +let runner: AVRRunner; |
| 46 | + |
| 47 | +const runButton = document.querySelector('#run-button'); |
| 48 | +runButton.addEventListener('click', compileAndRun); |
| 49 | +const stopButton = document.querySelector('#stop-button'); |
| 50 | +stopButton.addEventListener('click', stopCode); |
| 51 | +const statusLabel = document.querySelector('#status-label'); |
| 52 | +const compilerOutputText = document.querySelector('#compiler-output-text'); |
| 53 | + |
| 54 | +function executeProgram(hex: string) { |
| 55 | + runner = new AVRRunner(hex); |
| 56 | + const MHZ = 16000000; |
| 57 | + |
| 58 | + // Hook to PORTB output |
| 59 | + runner.cpu.writeHooks[0x25] = (value: number) => { |
| 60 | + const DDRB = runner.cpu.data[0x24]; |
| 61 | + value &= DDRB; |
| 62 | + const D12bit = 1 << 4; |
| 63 | + const D13bit = 1 << 5; |
| 64 | + led12.value = value & D12bit ? true : false; |
| 65 | + led13.value = value & D13bit ? true : false; |
| 66 | + }; |
| 67 | + |
| 68 | + runner.execute((cpu) => { |
| 69 | + const time = formatTime(cpu.cycles / MHZ); |
| 70 | + statusLabel.textContent = 'Simulation time: ' + time; |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +async function compileAndRun() { |
| 75 | + led12.value = false; |
| 76 | + led13.value = false; |
| 77 | + |
| 78 | + runButton.setAttribute('disabled', '1'); |
| 79 | + try { |
| 80 | + statusLabel.textContent = 'Compiling...'; |
| 81 | + const result = await buildHex(editor.getModel().getValue()); |
| 82 | + compilerOutputText.textContent = result.stderr || result.stdout; |
| 83 | + if (result.hex) { |
| 84 | + compilerOutputText.textContent += '\nProgram running...'; |
| 85 | + stopButton.removeAttribute('disabled'); |
| 86 | + executeProgram(result.hex); |
| 87 | + } else { |
| 88 | + runButton.removeAttribute('disabled'); |
| 89 | + } |
| 90 | + } catch (err) { |
| 91 | + runButton.removeAttribute('disabled'); |
| 92 | + alert('Failed: ' + err); |
| 93 | + } finally { |
| 94 | + statusLabel.textContent = ''; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +function stopCode() { |
| 99 | + stopButton.setAttribute('disabled', '1'); |
| 100 | + runButton.removeAttribute('disabled'); |
| 101 | + if (runner) { |
| 102 | + runner.stop(); |
| 103 | + runner = null; |
| 104 | + } |
| 105 | +} |
0 commit comments