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

Fixed all NaN #6

Merged
merged 2 commits into from
Dec 6, 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
52 changes: 31 additions & 21 deletions src/components/emulator/emulator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ export default function Emulator() {

}, [dynamicAssemble, code])

useEffect(() => {
console.log("Assembler finished.")
}, [assemblerComplete])

useEffect(() => {
setRandomRam()
setInterval(() => {
Expand Down Expand Up @@ -248,7 +252,7 @@ export default function Emulator() {
PC = 65535 + PC + 1

// fetch
const currentInstruction = ('000000000000000' + (PC > 32767? videoMemory[PC - 32768] : memory[PC]).toString(2)).slice(-16);
const currentInstruction = ('000000000000000' + (PC > 32767? videoMemory[PC - 32768] : memory[PC])?.toString(2)).slice(-16);

//decode
const opcode = parseInt(currentInstruction.substring(9, 14), 2);
Expand All @@ -259,7 +263,8 @@ export default function Emulator() {
let didJump = false;

// execute
switch(opcode) {
switch(opcode as instruction) {
case undefined:
case instruction.NOP: {
//%PC = %PC + 1
break;
Expand All @@ -272,36 +277,44 @@ export default function Emulator() {
}
case instruction.LI: {
//%dest = $imm
regs[dest] = memory[++PC];

const addr = ++PC;
regs[dest] = addr > 32767? videoMemory[addr - 32768] : memory[addr];
//setReg(dest, memory[PC + 1]);
break;
}
case instruction.LD: {
//%dest = memory[$imm]
regs[dest] = memory[memory[++PC]];
const addr = ++PC;
const imm = addr > 32767? videoMemory[addr - 32768] : memory[addr];

regs[dest] = imm > 32767? videoMemory[imm - 32768] : memory[imm];
//setReg(dest, memory[memory[PC + 1]]);
break;
}
case instruction.LDIND: {
//%dest = memory[%srcA]
regs[dest] = memory[regs[srcA]];
const addr = regs[srcA];

regs[dest] = addr > 32767? videoMemory[addr - 32768] : memory[addr];
//setReg(dest, memory[getReg(srcA)]);
break;
}
case instruction.LDIO: {
//%dest = memory[%srcA + $imm]
regs[dest] = memory[regs[srcA] + memory[++PC]];
let addr = regs[srcA] + (++PC > 32767? videoMemory[PC - 32768] : memory[PC]);
addr = addr > 65535? addr - 65536 : addr;

regs[dest] = addr > 32767? videoMemory[addr - 32768] : memory[addr];
//setReg(dest, memory[getReg(srcA) + memory[PC + 1]]);
break;
}
case instruction.STIO: {
//memory[%srcA + $imm] = %srcB
const val = regs[srcB];
const addr = regs[srcA] + memory[++PC];
const addr = regs[srcA] + (++PC > 32767? videoMemory[PC - 32768] : memory[PC]);

if(addr > 32767) {
videoMemory[addr - 32767] = val;
videoMemory[addr - 32768] = val;
} else {
memory[addr] = val;
}
Expand Down Expand Up @@ -366,20 +379,20 @@ export default function Emulator() {
}
case instruction.JNZ: {
//%PC = %srcA == 0 ? %PC + 1 : $imm
PC = regs[srcA] == 0 ? PC + 2 : memory[++PC];
PC = regs[srcA] == 0 ? PC + 2 : (++PC > 32767? videoMemory[PC - 32768] : memory[PC]);
didJump = true;
//jumpaddr = getReg(srcA) == 0 ? -1 : memory[PC + 1];
break;
}
case instruction.JIMM: {
//%PC = $imm
PC = memory[++PC];
PC = ++PC > 32767? videoMemory[PC - 32768] : memory[PC];
didJump = true;
break;
}
case instruction.ADDI: {
//%dest = %srcA + $imm
const result = regs[srcA] + memory[++PC];
const result = regs[srcA] + (++PC > 32767? videoMemory[PC - 32768] : memory[PC]);
regs[dest] = result > 65535 ? result - 65536 : result;
//setReg(dest, getReg(srcA) + memory[PC + 1]);
//PC 5
Expand All @@ -388,7 +401,7 @@ export default function Emulator() {
case instruction.ST: {
//memory[%imm] = %srcA
const val = regs[srcA];
const addr = memory[++PC];
const addr = ++PC > 32767? videoMemory[PC - 32768] : memory[PC];

if(addr > 32767) {
videoMemory[addr - 32768] = val;
Expand All @@ -399,13 +412,13 @@ export default function Emulator() {
}
case instruction.JZ: {
//%PC = %srcA != 0 ? %PC + 1 : $imm
PC = regs[srcA] != 0 ? PC + 2 : memory[++PC];
PC = regs[srcA] != 0 ? PC + 2 : (++PC > 32767? videoMemory[PC - 32768] : memory[PC]);
didJump = true;
break;
}
case instruction.JN: {
//%PC = %srcA != 0 ? %PC + 1 : $imm
PC = regs[srcA] < 32768 ? PC + 2 : memory[++PC];
PC = regs[srcA] < 32768 ? PC + 2 : (++PC > 32767? videoMemory[PC - 32768] : memory[PC]);
didJump = true;
break;
}
Expand Down Expand Up @@ -831,8 +844,6 @@ export default function Emulator() {
}
}

//console.log("Labels", labels, labelsAddr, usedLabels);

for(let i = 0; i < usedLabels.length; i++) {
const foundIndex = labels.findIndex((el) => el === usedLabels[i]);
if(foundIndex === -1) {
Expand All @@ -848,7 +859,6 @@ export default function Emulator() {
setAssemblerComplete(true);
setAssemblerError(false);
setAssemblerMessage("Success - assembled " + byteCount + " bytes.");
console.log("Interpreting finished successfully, RAM is valid.")
}

return <>
Expand All @@ -865,15 +875,15 @@ export default function Emulator() {
{" "}
<Tooltip describeChild title={loaded? "Step to the next instruction." : "Start the CPU and manually step through instructions."}>
<span>
<Button variant="contained" color="info" onClick={() => stepProgram()} disabled={!assemblerComplete} endIcon={<MoveDownIcon />}>
<Button variant="contained" color="info" onClick={() => stepProgram()} endIcon={<MoveDownIcon />}>
Step
</Button>
</span>
</Tooltip>
{" "}
<Tooltip describeChild title={running? "Pause the CPU." : loaded? "Start running the CPU from this point." : "Start the CPU."}>
<span>
<Button sx={{width: "125px"}} variant="contained" color={running? 'warning': loaded? 'success' : 'success'} onClick={() => runProgram()} disabled={!assemblerComplete} endIcon={running? <PauseIcon /> : loaded? <PlayArrowIcon /> : <PlayArrowIcon />}>
<Button sx={{width: "125px"}} variant="contained" color={running? 'warning': loaded? 'success' : 'success'} onClick={() => runProgram()} endIcon={running? <PauseIcon /> : loaded? <PlayArrowIcon /> : <PlayArrowIcon />}>
{running? 'Pause' : loaded? 'Continue' : 'Run'}
</Button>
</span>
Expand Down Expand Up @@ -947,7 +957,7 @@ export default function Emulator() {
<Typography variant="subtitle1" sx={{marginLeft: "5px"}}>BBC state</Typography>
<Card variant="outlined" className={styles.registers}>
<Typography variant="h6">Instruction</Typography>
<ViewInstruction memoryContent={ram[reg_PC]? ram[reg_PC] : 0} immContent={ram[reg_PC + 1]? ram[reg_PC + 1] : 0} />
<ViewInstruction memoryContent={reg_PC > 32767? vram[reg_PC - 32768] : ram[reg_PC]} immContent={reg_PC + 1 > 32767? vram[reg_PC - 32767] : ram[reg_PC + 1]} />
</Card>
<Card variant="outlined" className={styles.registers}>
<Typography variant="h6" sx={{marginBottom: "10px"}}>Registers</Typography>
Expand Down
14 changes: 10 additions & 4 deletions src/components/viewInstruction/viewInstruction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export default function ViewInstruction(props: IViewInstructionProps) {
const [decodedInstruction, setDecodedInstruction] = useState("");

useEffect(() => {
if(props.memoryContent === undefined || props.immContent === undefined) {
setDecodedInstruction("Memory undefined.")
return;
}

const val = ('00000000000000' + props.memoryContent.toString(2)).slice(-16);
const inst = val.slice(9, 14);

Expand All @@ -21,9 +26,9 @@ export default function ViewInstruction(props: IViewInstructionProps) {
const dest = "r" + parseInt(val.slice(0, 3).split("").reverse().join(("")), 2);
const imm = parseInt(('0000000000000000' + props.immContent.toString(2)).slice(-16), 2);

const instn = instruction[parseInt(inst, 2)];

let instn = instruction[parseInt(inst, 2)];
let theRest = " "

switch(instruction[instn as keyof typeof instruction]) {
case instruction.NOP: {
break;
Expand Down Expand Up @@ -76,8 +81,9 @@ export default function ViewInstruction(props: IViewInstructionProps) {
theRest += imm + ", " + srcA;
break;
}
default: {
theRest += ""
case undefined: {
theRest += "";
instn = "???"
}
}

Expand Down
Loading