diff --git a/src/components/Instructions/InstructionItem.tsx b/src/components/Instructions/InstructionItem.tsx index c52d9c1d..9260e5e6 100644 --- a/src/components/Instructions/InstructionItem.tsx +++ b/src/components/Instructions/InstructionItem.tsx @@ -1,4 +1,4 @@ -import { mapInstructionsArgsByType, valueToNumeralSystem } from "./utils"; +import { argType, mapInstructionsArgsByType, valueToBinary, valueToNumeralSystem } from "./utils"; import classNames from "classnames"; import { getStatusColor } from "../Registers/utils"; import { ExpectedState, RegistersArray, Status } from "@/types/pvm"; @@ -86,7 +86,7 @@ export const InstructionItem = forwardRef( onClick(programRow); }, [programRow, onClick]); - const { backgroundColor, hasTooltip } = getHighlightStatus(workers, programRow, status); + const { backgroundColor, hasOpacity } = getHighlightStatus(workers, programRow, status); const renderContent = () => { return ( @@ -143,8 +143,16 @@ export const InstructionItem = forwardRef( style={{ borderTop: programRow.block.isStart ? "2px solid #bbb" : undefined }} > - {"args" in programRow && - mapInstructionsArgsByType(programRow.args, numeralSystem, programRow.counter)} + {"args" in programRow && ( + instruction.value) + .join(", ") ?? "", + }} + /> + )} @@ -153,7 +161,67 @@ export const InstructionItem = forwardRef( ); }; - return hasTooltip ? ( + const renderTooltipContentInstructionInfo = () => { + return ( +
+
+
+
opcode
+
+
+ {valueToBinary(programRow.instructionCode, 8)} +
+
+ {valueToNumeralSystem(programRow.instructionCode, numeralSystem)} +
+
+
+ + {"args" in programRow && + mapInstructionsArgsByType(programRow.args, numeralSystem, programRow.counter)?.map( + (instruction, index) => ( +
+
{instruction.type}
+
+
+ {valueToBinary( + instruction.value, + instruction.type === argType.EXTENDED_WIDTH_IMMEDIATE ? 16 : 8, + )} +
+
+
+
+ ), + )} +
+
+ ); + }; + + return hasOpacity ? ( {renderContent()} @@ -172,7 +240,12 @@ export const InstructionItem = forwardRef( ) : ( - renderContent() + + + {renderContent()} + {renderTooltipContentInstructionInfo()} + + ); }, ); @@ -197,7 +270,7 @@ function getHighlightStatus(workers: WorkerState[], programRow: ProgramRow, stat return { backgroundColor, - hasTooltip: bgOpacity > 0 && bgOpacity < 1, + hasOpacity: bgOpacity > 0 && bgOpacity < 1, }; } diff --git a/src/components/Instructions/utils.tsx b/src/components/Instructions/utils.tsx index 0cad1ce9..97e42a48 100644 --- a/src/components/Instructions/utils.tsx +++ b/src/components/Instructions/utils.tsx @@ -15,84 +15,173 @@ export const valueToNumeralSystem = ( : (value ?? 0).toString().padStart(padStartVal, "0"); }; -export const mapInstructionsArgsByType = (args: Args | null, numeralSystem: NumeralSystem, counter: number) => { +export const valueToBinary = (value?: number | bigint | string, padStartVal: number = 0): string => { + return ((Number(value) ?? 0) >>> 0).toString(2).padStart(padStartVal, "0"); +}; + +export enum argType { + IMMEDIATE = "IMMEDIATE", + OFFSET = "OFFSET", + REGISTER = "REGISTER", + EXTENDED_WIDTH_IMMEDIATE = "EXTENDED_WIDTH_IMMEDIATE", +} + +export const mapInstructionsArgsByType = ( + args: Args | null, + numeralSystem: NumeralSystem, + counter: number, +): + | { + type: argType; + value: string | number; + }[] + | null => { switch (args?.type) { case ArgumentType.NO_ARGUMENTS: - return ""; + return []; case ArgumentType.ONE_IMMEDIATE: - return {valueToNumeralSystem(args.immediateDecoder.getI64(), numeralSystem)}; + return [{ type: argType.IMMEDIATE, value: valueToNumeralSystem(args.immediateDecoder.getI64(), numeralSystem) }]; case ArgumentType.TWO_IMMEDIATES: - return ( - - {valueToNumeralSystem(args?.firstImmediateDecoder.getI64(), numeralSystem)},{" "} - {valueToNumeralSystem(args?.secondImmediateDecoder.getI64(), numeralSystem)} - - ); + return [ + { + type: argType.IMMEDIATE, + value: valueToNumeralSystem(args?.firstImmediateDecoder.getI64(), numeralSystem), + }, + { + type: argType.IMMEDIATE, + value: valueToNumeralSystem(args?.secondImmediateDecoder.getI64(), numeralSystem), + }, + ]; case ArgumentType.ONE_OFFSET: - return {valueToNumeralSystem(args?.nextPc - counter, numeralSystem)}; + return [{ type: argType.OFFSET, value: valueToNumeralSystem(args?.nextPc - counter, numeralSystem) }]; case ArgumentType.ONE_REGISTER_ONE_IMMEDIATE: - return ( - - ω{args?.registerIndex}, {valueToNumeralSystem(args.immediateDecoder.getI64(), numeralSystem)} - - ); + return [ + { + type: argType.REGISTER, + value: `ω${args?.registerIndex}`, + }, + { + type: argType.IMMEDIATE, + value: valueToNumeralSystem(args.immediateDecoder.getI64(), numeralSystem), + }, + ]; case ArgumentType.ONE_REGISTER_TWO_IMMEDIATES: - return ( - - ω{args?.registerIndex}, {valueToNumeralSystem(args?.firstImmediateDecoder.getI64(), numeralSystem)} - , {valueToNumeralSystem(args?.secondImmediateDecoder.getI64(), numeralSystem)} - - ); + return [ + { + type: argType.REGISTER, + value: `ω${args?.registerIndex}`, + }, + { + type: argType.IMMEDIATE, + value: valueToNumeralSystem(args?.firstImmediateDecoder.getI64(), numeralSystem), + }, + { + type: argType.IMMEDIATE, + value: valueToNumeralSystem(args?.secondImmediateDecoder.getI64(), numeralSystem), + }, + ]; case ArgumentType.ONE_REGISTER_ONE_IMMEDIATE_ONE_OFFSET: - return ( - - ω{args?.registerIndex}, {valueToNumeralSystem(args?.immediateDecoder.getI64(), numeralSystem)},{" "} - {valueToNumeralSystem(args?.nextPc - counter, numeralSystem)} - - ); + return [ + { + type: argType.REGISTER, + value: `ω${args?.registerIndex}`, + }, + { + type: argType.IMMEDIATE, + value: valueToNumeralSystem(args?.immediateDecoder.getI64(), numeralSystem), + }, + { + type: argType.OFFSET, + value: valueToNumeralSystem(args?.nextPc - counter, numeralSystem), + }, + ]; case ArgumentType.ONE_REGISTER_ONE_EXTENDED_WIDTH_IMMEDIATE: - return ( - - ω{args?.registerIndex}, {valueToNumeralSystem(args?.immediateDecoder.getValue(), numeralSystem)} - - ); + return [ + { + type: argType.REGISTER, + value: `ω${args?.registerIndex}`, + }, + { + type: argType.EXTENDED_WIDTH_IMMEDIATE, + value: valueToNumeralSystem(args?.immediateDecoder.getValue(), numeralSystem), + }, + ]; case ArgumentType.TWO_REGISTERS: - return ( - - ω{args?.firstRegisterIndex}, ω{args?.secondRegisterIndex} - - ); + return [ + { + type: argType.REGISTER, + value: `ω${args?.firstRegisterIndex}`, + }, + { + type: argType.REGISTER, + value: `ω${args?.secondRegisterIndex}`, + }, + ]; case ArgumentType.TWO_REGISTERS_ONE_IMMEDIATE: - return ( - - ω{args?.firstRegisterIndex}, ω{args?.secondRegisterIndex},{" "} - {valueToNumeralSystem(args?.immediateDecoder.getI64(), numeralSystem)} - - ); + return [ + { + type: argType.REGISTER, + value: `ω${args?.firstRegisterIndex}`, + }, + { + type: argType.REGISTER, + value: `ω${args?.secondRegisterIndex}`, + }, + { + type: argType.IMMEDIATE, + value: valueToNumeralSystem(args?.immediateDecoder.getI64(), numeralSystem), + }, + ]; case ArgumentType.TWO_REGISTERS_ONE_OFFSET: - return ( - - ω{args?.firstRegisterIndex}, ω{args?.secondRegisterIndex},{" "} - {valueToNumeralSystem(args?.nextPc - counter, numeralSystem)} - - ); + return [ + { + type: argType.REGISTER, + value: `ω${args?.firstRegisterIndex}`, + }, + { + type: argType.REGISTER, + value: `ω${args?.secondRegisterIndex}`, + }, + { + type: argType.OFFSET, + value: valueToNumeralSystem(args?.nextPc - counter, numeralSystem), + }, + ]; case ArgumentType.TWO_REGISTERS_TWO_IMMEDIATES: - return ( - - ω{args?.firstRegisterIndex}, ω{args?.secondRegisterIndex},{" "} - {valueToNumeralSystem(args?.firstImmediateDecoder.getI64(), numeralSystem)},{" "} - {valueToNumeralSystem(args?.secondImmediateDecoder.getI64(), numeralSystem)} - - ); + return [ + { + type: argType.REGISTER, + value: `ω${args?.firstRegisterIndex}`, + }, + { + type: argType.REGISTER, + value: `ω${args?.secondRegisterIndex}`, + }, + { + type: argType.IMMEDIATE, + value: valueToNumeralSystem(args?.firstImmediateDecoder.getI64(), numeralSystem), + }, + { + type: argType.IMMEDIATE, + value: valueToNumeralSystem(args?.secondImmediateDecoder.getI64(), numeralSystem), + }, + ]; case ArgumentType.THREE_REGISTERS: - return ( - - ω{args?.firstRegisterIndex}, ω{args?.secondRegisterIndex}, ω - {args?.thirdRegisterIndex} - - ); - + return [ + { + type: argType.REGISTER, + value: `ω${args?.firstRegisterIndex}`, + }, + { + type: argType.REGISTER, + value: `ω${args?.secondRegisterIndex}`, + }, + { + type: argType.REGISTER, + value: `ω${args?.thirdRegisterIndex}`, + }, + ]; default: - return "err"; + return null; } }; diff --git a/src/context/NumeralSystem.tsx b/src/context/NumeralSystem.tsx index e9a6cb3d..a36c2bc6 100644 --- a/src/context/NumeralSystem.tsx +++ b/src/context/NumeralSystem.tsx @@ -1,4 +1,5 @@ export enum NumeralSystem { DECIMAL, HEXADECIMAL, + BINARY, }