-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
204 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './shallowCompareArrays'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { Nullable } from '../types'; | ||
|
||
export const shallowCompareArrays = <T>( | ||
a: Nullable<readonly T[]>, | ||
b: Nullable<readonly T[]>, | ||
) => { | ||
if (a === b) { | ||
return true; | ||
} | ||
|
||
if (!a || !b) { | ||
return false; | ||
} | ||
|
||
for (let i = 0; i < a.length; ++i) { | ||
if (a[i] !== b[i]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useInstantUpdateEffect'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useState } from 'react'; | ||
import type { DependencyList } from 'react'; | ||
|
||
import { shallowCompareArrays } from '../helpers'; | ||
|
||
export const useInstantUpdateEffect = (fn: VoidFunction, deps: DependencyList) => { | ||
const [prevDeps, setPrevDeps] = useState(deps); | ||
|
||
if (prevDeps && !shallowCompareArrays(prevDeps, deps)) { | ||
setPrevDeps(deps); | ||
fn(); | ||
} | ||
}; |
66 changes: 66 additions & 0 deletions
66
apps/site/src/modules/Editor/EditorScreen/EditorScreen.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { useRef, useEffect } from 'react'; | ||
import { either as E } from 'fp-ts'; | ||
|
||
import { VGARenderLoopDriver, X86CPU } from '@ts-c-compiler/x86-cpu'; | ||
|
||
import { useInstantUpdateEffect } from 'hooks'; | ||
import { useEditorState } from '../EditorStateProvider'; | ||
|
||
export const EditorScreen = () => { | ||
const { emulation } = useEditorState(); | ||
const screenRef = useRef<HTMLDivElement>(null); | ||
const cpuRef = useRef<X86CPU>(); | ||
|
||
useEffect(() => { | ||
if (!screenRef.current) { | ||
return; | ||
} | ||
|
||
const cpu = new X86CPU(); | ||
cpu.attach(VGARenderLoopDriver, { | ||
screenElement: screenRef.current, | ||
}); | ||
|
||
cpuRef.current = cpu; | ||
|
||
return () => { | ||
cpu.release(); | ||
}; | ||
}, []); | ||
|
||
useInstantUpdateEffect(() => { | ||
const cpu = cpuRef.current; | ||
|
||
if (!cpu) { | ||
return; | ||
} | ||
|
||
switch (emulation.info.state) { | ||
case 'stop': | ||
cpu.halt(); | ||
break; | ||
|
||
case 'pause': | ||
cpu.freeze(); | ||
break; | ||
|
||
case 'running': { | ||
if (E.isLeft(emulation.info.result)) { | ||
cpu.halt(); | ||
return; | ||
} | ||
|
||
cpu.boot(emulation.info.result.right.blob); | ||
break; | ||
} | ||
} | ||
}, [emulation.info.state]); | ||
|
||
return ( | ||
<div | ||
ref={screenRef} | ||
className="mx-auto block text-center" | ||
style={{ imageRendering: 'pixelated' }} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './EditorScreen'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
import { EditorScreen } from '../EditorScreen'; | ||
|
||
export const EditorSidebar = () => { | ||
return <div className="p-4" />; | ||
return ( | ||
<div className="grid grid-rows-2 p-4"> | ||
<EditorScreen /> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
.editor-codemirror :global { | ||
.CodeMirror { | ||
height: 100%; | ||
|
||
&-code { | ||
padding-top: 16px; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './nullable.type'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type Nullable<T> = T | null | undefined; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters