Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emulation refactor #17

Merged
merged 5 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Emulator from "@/components/emulator/emulator";

export default function Home() {
return (
<div>
<Emulator />
</div>
);
}
import EmulatorComponent from "@/components/emulator/emulatorComponent";

export default function Home() {
return (
<div>
<EmulatorComponent />
</div>
);
}
2 changes: 1 addition & 1 deletion src/app/ui/emulator.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
height: auto;
align-content: center;
padding: 10px 0 10px 5px;
margin: 2px;
margin: 2px !important;
}

.register {
Expand Down
106 changes: 54 additions & 52 deletions src/components/canvas/canvas.tsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
import React, {useEffect, useRef, useState} from 'react'

interface ICanvasProps {
className?: string
vram: Array<number>
}

export default function Canvas(props: ICanvasProps) {
const [vram, setVram] = useState(Array<number>(32768));
const canvasRef = useRef(null);

useEffect(() => {
setVram(props.vram);
}, [props.vram])

useEffect(() => {
if(vram[0] != undefined) {
const canvas = canvasRef.current;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const context = canvas.getContext('2d');

context.fillStyle = '#ffffff';
context.fillRect(0, 0, context.canvas.width, context.canvas.height);

//each addr contains 2 pixels, with 8bpp
//xxx xxx xx | xxx xxx xx

let pixel = 0;

for(let x = 0; x < 120; x++){
for(let y = 0; y < 160; y++) {
const data = vram[pixel];

const red = Math.floor(((data & 224) / 7) * 255);
const green = Math.floor(((data & 28) / 7) * 255);
const blue = Math.floor(((data & 3) / 3) * 255);

const hexRed = ('00' + red.toString(16)).slice(-2);
const hexGreen = ('00' + green.toString(16)).slice(-2);
const hexBlue = ('00' + blue.toString(16)).slice(-2);

context.fillStyle = '#'+ hexRed + hexGreen + hexBlue;
context.fillRect(y*2, x*2, 2, 2)
pixel++;
}
}
}
}, [vram])

return <canvas suppressHydrationWarning ref={canvasRef} width={320} height={240} {...props}/>
import React, {useEffect, useRef, useState} from 'react'

interface ICanvasProps {
className?: string
memory: Array<number>
}

export default function Canvas(props: ICanvasProps) {
const [memory, setMemory] = useState(Array<number>(65535));
const canvasRef = useRef(null);

useEffect(() => {
setMemory(props.memory);
}, [props.memory])

useEffect(() => {
if(memory[0] != undefined) {
const canvas = canvasRef.current;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const context = canvas.getContext('2d');

context.fillStyle = '#ffffff';
context.fillRect(0, 0, context.canvas.width, context.canvas.height);

//each addr contains 2 pixels, with 8bpp
//xxx xxx xx | xxx xxx xx

let pixel = 0;

const vramLocationOffset = 32768;

for(let x = 0; x < 120; x++){
for(let y = 0; y < 160; y++) {
const data = memory[pixel + vramLocationOffset];

const red = Math.floor(((data & 224) / 7) * 255);
const green = Math.floor(((data & 28) / 7) * 255);
const blue = Math.floor(((data & 3) / 3) * 255);

const hexRed = ('00' + red.toString(16)).slice(-2);
const hexGreen = ('00' + green.toString(16)).slice(-2);
const hexBlue = ('00' + blue.toString(16)).slice(-2);

context.fillStyle = '#'+ hexRed + hexGreen + hexBlue;
context.fillRect(y*2, x*2, 2, 2)
pixel++;
}
}
}
}, [memory])

return <canvas suppressHydrationWarning ref={canvasRef} width={320} height={240} {...props}/>
}
Loading
Loading