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

Fix check for wrapping with sub-bus #227

Merged
merged 2 commits into from
Feb 11, 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
40 changes: 35 additions & 5 deletions simulator/src/chip/chip.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { bin } from "../util/twos.js";
import { And, Mux16, Not, Not16, Or, Xor } from "./builtins/index.js";
import { Nand } from "./builtins/logic/nand.js";
import { Bit, PC } from "./builtins/sequential/bit.js";
import { DFF } from "./builtins/sequential/dff.js";
import {
Bus,
Chip,
Expand All @@ -11,12 +16,7 @@ import {
parseToPin,
printChip,
} from "./chip.js";
import { Nand } from "./builtins/logic/nand.js";
import { And, Mux16, Not, Not16, Or, Xor } from "./builtins/index.js";
import { bin } from "../util/twos.js";
import { DFF } from "./builtins/sequential/dff.js";
import { Clock } from "./clock.js";
import { Bit, PC } from "./builtins/sequential/bit.js";

describe("Chip", () => {
it("parses toPin", () => {
Expand Down Expand Up @@ -228,6 +228,36 @@ describe("Chip", () => {
expect(notPart.in().busVoltage).toBe(0b1);
});

it("wires SubBus in[0]=a", () => {
const chip = new Chip(["a", "b"], ["out[3]"]);
const not3 = new Not3();

// Not3(in[0]=a, in[1]=b, in[2]=b, out=out)
chip.wire(not3, [
{
from: { name: "a", start: 0, width: undefined },
to: { name: "in", start: 0, width: 1 },
},
{
from: { name: "b", start: 0, width: undefined },
to: { name: "in", start: 1, width: 1 },
},
{
from: { name: "b", start: 0, width: undefined },
to: { name: "in", start: 2, width: 1 },
},
{
from: { name: "out", start: 0, width: undefined },
to: { name: "out", start: 0, width: undefined },
},
]);

chip.in("b")!.busVoltage = 1;
chip.in("a")!.busVoltage = 0;
chip.eval();
expect(chip.out().busVoltage).toBe(0b001);
});

it("wires SubBus out=out[1]", () => {
const threeChip = new (class ThreeChip extends Chip {
constructor() {
Expand Down
4 changes: 2 additions & 2 deletions simulator/src/chip/chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ export class Chip {
}

// Wrap the chipPin in an OutBus when the part side is dimensioned
DavidSouther marked this conversation as resolved.
Show resolved Hide resolved
if (to.start > 0 || to.width !== chipPin.width) {
if (to.start > 0 || to.width !== partPin.width) {
chipPin = new OutSubBus(chipPin, to.start, to.width);
}

Expand All @@ -433,7 +433,7 @@ export class Chip {
from.width ??= chipPin.width;

// Wrap the partPin in an InBus when the part side is dimensioned
if (to.start > 0 || to.width !== chipPin.width) {
if (to.start > 0 || to.width !== partPin.width) {
partPin = new InSubBus(partPin, to.start, to.width);
}

Expand Down
Loading