Skip to content

Commit

Permalink
Merge branch 'main' into bug-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSouther authored Oct 25, 2023
2 parents 6703914 + 42ad025 commit 970064d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
21 changes: 13 additions & 8 deletions components/src/chips/alu.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
COMMANDS,
COMMANDS_ALU,
COMMANDS_OP,
Flags,
} from "@nand2tetris/simulator/cpu/alu.js";
Expand All @@ -20,12 +20,17 @@ export const ALUComponent = ({
}) => (
<div>
<span>ALU</span>
<dl>
<dt>A</dt> <dd>{bin(A)}</dd>
<dt>op</dt> <dd>{COMMANDS.op[op] ?? "(??)"}</dd>
<dt>D</dt> <dd>{bin(D)}</dd>
<dt>=</dt> <dd>{bin(out)}</dd>
<dd>{Flags[flag as keyof typeof Flags]}</dd>
</dl>
<svg width="250" height="250" xmlns="http://www.w3.org/2000/svg">
<g>
<rect x="1" y="20" height="85" width="70" fill="black" />
<rect x="1" y="145" height="85" width="70" fill="black" />
<rect x="180" y="95" height="60" width="70" fill="black" />
<polygon points="70,10 180,85 180,165 70,240 70,135 90,125 70,115" stroke="#000" fill="#6D97AB"/>
<text xmlSpace="preserve" text-anchor="middle" font-family="Noto Sans JP" font-size="14" stroke-width="0" id="svg_9" y="63" x="35" stroke="white" fill="#ffffff">{A}</text>
<text xmlSpace="preserve" text-anchor="middle" font-family="Noto Sans JP" font-size="14" id="svg_10" y="188" x="35" stroke-width="0" stroke="white" fill="#ffffff">{D}</text>
<text xmlSpace="preserve" text-anchor="middle" font-family="Noto Sans JP" font-size="14" id="svg_11" y="125" x="215" stroke-width="0" stroke="white" fill="#ffffff">{out}</text>
<text xmlSpace="preserve" text-anchor="middle" font-family="Noto Sans JP" font-size="14" id="svg_13" y="125" x="135" stroke-width="0" stroke="white" fill="#ffffff">{COMMANDS_ALU.op[op] ?? "(??)"}</text>
</g>
</svg>
</div>
);
8 changes: 6 additions & 2 deletions components/src/pinout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export const PinoutBlock = (
enableEdit={props.enableEdit}
signed={props.displayInfo.isSigned(immPin.pin.name)}
setInputValid={props.setInputValid}
internal={props.header === "Internal pins" ? true : false }
/>
</td>
</tr>
Expand Down Expand Up @@ -150,7 +151,7 @@ export const Pinout = ({
<tr key={immPin.pin.name}>
<td>{immPin.pin.name}</td>
<td>
<Pin pin={immPin} toggle={toggle} />
<Pin pin={immPin} toggle={toggle} internal/>
</td>
</tr>
))}
Expand All @@ -166,13 +167,15 @@ const Pin = ({
enableEdit = true,
signed = true,
setInputValid,
internal = false
}: {
pin: ImmPin;
toggle: ((pin: ChipPin, bit?: number) => void) | undefined;
disabled?: boolean;
enableEdit?: boolean;
signed?: boolean;
setInputValid?: (valid: boolean) => void;
internal: boolean;
}) => {
const [isBin, setIsBin] = useState(true);
let inputValid = true;
Expand Down Expand Up @@ -256,8 +259,9 @@ const Pin = ({
pin.bits.map(([i, v]) => (
<button
key={i}
onClick={() => toggle?.(pin.pin, i)}
disabled={disabled}
style={internal ? {backgroundColor: "grey"} : {}}
onClick={() => toggle?.(pin.pin, i)}
data-testid={`pin-${i}`}
>
{v}
Expand Down
46 changes: 46 additions & 0 deletions simulator/src/cpu/alu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,52 @@ export type COMMANDS_OP =
| 0b010101
| 0b010101;

//Usefull for the visualization of the ALU
export type COMMANDS_ALU =
| "0"
| "1"
| "-1"
| "x"
| "y"
| "!x"
| "!y"
| "-x"
| "-y"
| "x+1"
| "y+1"
| "x-1"
| "y-1"
| "x+y"
| "x-y"
| "y-x"
| "x&y"
| "x|y";

export const COMMANDS_ALU: {
op: Record<COMMANDS_OP, COMMANDS_ALU>
} = {
op: {
0x2a: "0",
0x3f: "1",
0x3a: "-1",
0x0c: "x",
0x30: "y",
0x0d: "!x",
0x31: "!y",
0x0f: "-x",
0x33: "-y",
0x1f: "x+1",
0x37: "y+1",
0x0e: "x-1",
0x32: "y-1",
0x02: "x+y",
0x13: "x-y",
0x07: "y-x",
0x00: "x&y",
0x15: "x|y",
}
}

export const COMMANDS: {
asm: Record<COMMANDS_ASM, COMMANDS_OP>;
op: Record<COMMANDS_OP, COMMANDS_ASM>;
Expand Down

0 comments on commit 970064d

Please sign in to comment.