Skip to content

Commit 68cfb22

Browse files
committed
changed bit widths to be part of node type, instead of diagram
1 parent fb03278 commit 68cfb22

File tree

10 files changed

+142
-59
lines changed

10 files changed

+142
-59
lines changed

src/assets/diagram.svg

Lines changed: 27 additions & 27 deletions
Loading

src/components/Diagram.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function Diagram(props: {
6565
} = useSimulationContext()
6666
const svgRef = useRef<SVGSVGElement | null>(null)
6767
const [hoveredWire, setHoveredWire] = useState<
68-
{ nodeId: string; inputId: string; bits: number } | undefined
68+
{ nodeId: string; inputId: string } | undefined
6969
>(undefined)
7070

7171
useEffect(() => {
@@ -94,10 +94,8 @@ export function Diagram(props: {
9494
current.appendChild(clone)
9595
clones.push(clone)
9696

97-
const bits = Number(e.dataset.bits ?? "32")
98-
9997
clone.addEventListener("mouseover", () => {
100-
setHoveredWire({ nodeId, inputId, bits })
98+
setHoveredWire({ nodeId, inputId })
10199
})
102100
clone.addEventListener("mouseleave", () => {
103101
setHoveredWire(undefined)
@@ -139,18 +137,19 @@ export function Diagram(props: {
139137
] as number | undefined
140138

141139
if (hoveredWireValue != undefined) {
142-
tooltipContent = int2hex(
143-
hoveredWireValue,
144-
Math.ceil(hoveredWire.bits / 4),
145-
)
140+
const bits =
141+
simulation.nodes[hoveredWire.nodeId].inputBitWidths?.[
142+
hoveredWire.inputId
143+
] ?? 32
144+
tooltipContent = int2hex(hoveredWireValue, Math.ceil(bits / 4))
146145
const iid = makeIID(hoveredWire.nodeId, hoveredWire.inputId)
147146
if (placedNodes.has(iid)) {
148147
tooltipContent = (
149148
<div className="grid grid-cols-[auto_auto] grid-rows-2 gap-x-2 items-baseline">
150149
<span className="text-sm text-muted-foreground">Before: </span>
151150
{int2hex(
152151
simulation.inputValues[placedNodeId(iid)].in,
153-
Math.ceil(hoveredWire.bits / 4),
152+
Math.ceil(bits / 4),
154153
)}
155154
<span className="text-sm text-muted-foreground">After: </span>
156155
{tooltipContent}

src/logic/nodeTypes/and.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ type Outputs = ["out"]
88
export const and: NodeType<Outputs> = nodeType(
99
[{ id: "in0" }, { id: "in1" }] as const,
1010
(_, inputs) => ({ out: inputs.in0 & inputs.in1 }),
11+
undefined,
12+
() => ({ out: 1 }),
1113
)

src/logic/nodeTypes/control.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { nodeType, type OutputObject } from "../simulation"
1+
import { nodeType, type NodeType, type OutputObject } from "../simulation"
22

33
type Outputs = [
44
"regDst",
@@ -104,11 +104,23 @@ const outputMap: Record<number, OutputObject<Outputs>> = {
104104
*
105105
* Its input is expected to be just the 6 opcode bits of an instruction.
106106
*/
107-
export const control = nodeType(
107+
export const control: NodeType<Outputs> = nodeType(
108108
[
109109
{
110110
id: "in",
111111
},
112112
] as const,
113113
(_, inputs) => outputMap[inputs.in],
114+
undefined,
115+
() => ({
116+
aluOp: 2,
117+
aluSrc: 1,
118+
branch: 1,
119+
jump: 1,
120+
memRead: 1,
121+
memToReg: 1,
122+
memWrite: 1,
123+
regDst: 1,
124+
regWrite: 1,
125+
}),
114126
)

src/logic/nodeTypes/mux.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export function makeMux(numInputs: number): Mux {
2929
(_, inputs) => ({
3030
out: inputs[`in${inputs.control}`],
3131
}),
32+
undefined,
33+
// The bit widths of each input are assumed to be the same, so we simply return the first one's.
34+
(get) => ({ out: get("in0") }),
3235
)
3336

3437
cache[numInputs] = mux

src/logic/nodeTypes/neg.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ export const neg: NodeType<Outputs> = nodeType(
1515
out: -inputs.in,
1616
}),
1717
undefined,
18+
(get) => ({ out: get("in") }),
1819
"Neg",
1920
)

src/logic/nodeTypes/not.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ export const not: NodeType<Outputs> = nodeType(
1515
out: ~inputs.in,
1616
}),
1717
undefined,
18+
(get) => ({ out: get("in") }),
1819
"Not",
1920
)

src/logic/nodeTypes/shift.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function makeShifter(kind: ShiftKind, bits: number): NodeType<Outputs> {
3434
out: shiftFuncs[kind](inputs.in, bits),
3535
}),
3636
undefined,
37+
undefined,
3738
`${shiftLabels[kind]} ${bits}`,
3839
)
3940
}

src/logic/nodeTypes/splitter.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,25 @@ function extractBits(n: number, start: number, end: number) {
2626
*/
2727
export function makeSplitter(
2828
numOutputs: number,
29-
bitRanges?: ([start: number, end: number] | undefined)[],
29+
bitRanges?: [start: number, end: number][],
3030
): NodeType<Outputs> {
31-
return nodeType(inputs, (_, inputs) => {
32-
const values: Record<OutTemplate, number> = {}
33-
for (let i = 0; i < numOutputs; i++) {
34-
const extractRange = bitRanges?.[i]
35-
values[`out${i}`] = extractRange
36-
? extractBits(inputs.in, ...extractRange)
37-
: inputs.in
38-
}
39-
return values
40-
})
31+
return nodeType(
32+
inputs,
33+
(_, inputs) => {
34+
const values: Record<OutTemplate, number> = {}
35+
for (let i = 0; i < numOutputs; i++) {
36+
const extractRange = bitRanges?.[i]
37+
values[`out${i}`] = extractRange
38+
? extractBits(inputs.in, ...extractRange)
39+
: inputs.in
40+
}
41+
return values
42+
},
43+
undefined,
44+
bitRanges &&
45+
(() =>
46+
Object.fromEntries(
47+
bitRanges.map((r, i) => [`out${i}`, r[1] - r[0] + 1]),
48+
)),
49+
)
4150
}

0 commit comments

Comments
 (0)