Skip to content
Draft

wip #59

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
4 changes: 1 addition & 3 deletions src/pages/edit/Editor/components/NodePinPropertyEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ export function CCComponentEditorNodePinPropertyEditor() {
"NodePinPropertyEditor can only be used for node pins with user specified bit width",
);
const componentPinAttributes = nullthrows(
IntrinsicComponentDefinition.intrinsicComponentPinAttributesByComponentPinId.get(
target.componentPinId,
),
IntrinsicComponentDefinition.getPinAttributesByPinId(target.componentPinId),
"NodePinPropertyEditor can only be used for intrinsic component pins",
);

Expand Down
14 changes: 12 additions & 2 deletions src/store/componentPin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,25 @@ export const ccPinTypes: CCComponentPinType[] = ["input", "output"];
/** null for intrinsic components */
export type CCPinImplementation = CCNodePinId | null;

/**
* The resolved bit width status of a node pin instance.
* - `isFixed: false` — the bit width has not yet been determined.
* - `isFixed: true` — the bit width is known and available as `bitWidth`.
*/
export type CCNodePinBitWidthStatus =
| { isFixed: false }
| { isFixed: true; bitWidth: number };

/**
* The bit width status of a component pin definition.
* - `isFixed: false, fixMode: "automatic"` — the bit width is not yet determined and will be inferred automatically from connections.
* - `isFixed: false, fixMode: "manual"` — the bit width is not yet determined and must be specified manually by the user.
* - `isFixed: true` — the bit width is known and available as `bitWidth`.
*/
export type CCComponentPinBitWidthStatus =
| { isFixed: false; fixMode: "automatic" | "manual" }
| { isFixed: true; bitWidth: number };

export type CCNodePinFixedBitWidth = number;

export type CCComponentPinStoreEvents = {
didRegister(pin: CCComponentPin): void;
willUnregister(pin: CCComponentPin): void;
Expand Down Expand Up @@ -217,6 +226,7 @@ export class CCComponentPinStore extends EventEmitter<CCComponentPinStoreEvents>
): CCComponentPinBitWidthStatus {
const pin = this.#pins.get(pinId);
invariant(pin);
// TODO: Remove hardcoded intrinsic component pin IDs and replace with a more flexible system, such as metadata on the component definitions.
switch (pin.id) {
case nullthrows(and.inputPin.A.id):
case nullthrows(and.inputPin.B.id):
Expand Down
33 changes: 13 additions & 20 deletions src/store/intrinsics/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type Context = {

type IntrinsicComponentPinAttributes = {
name: string;
bitWidthFixMode?: "fixed" | "configurable" | "splittable";
isBitWidthConfigurable?: boolean;
isSplittable?: boolean;
};
Expand All @@ -48,11 +49,6 @@ type Props<Spec extends CCIntrinsicComponentSpec> = {
export class IntrinsicComponentDefinition<
Spec extends CCIntrinsicComponentSpec = CCIntrinsicComponentSpec,
> {
static intrinsicComponentPinAttributesByComponentPinId: Map<
CCComponentPinId,
IntrinsicComponentPinAttributes
> = new Map();

readonly id: CCComponentId;
readonly type: CCIntrinsicComponentType;
readonly name: string;
Expand Down Expand Up @@ -98,7 +94,7 @@ export class IntrinsicComponentDefinition<
order: this._lastLocalIndex++,
name: attributes.name,
};
IntrinsicComponentDefinition.intrinsicComponentPinAttributesByComponentPinId.set(
IntrinsicComponentDefinition._pinAttributesByPinId.set(
pin.id,
attributes,
);
Expand All @@ -114,26 +110,23 @@ export class IntrinsicComponentDefinition<
order: this._lastLocalIndex++,
name: attributes.name,
};
IntrinsicComponentDefinition.intrinsicComponentPinAttributesByComponentPinId.set(
IntrinsicComponentDefinition._pinAttributesByPinId.set(
pin.id,
attributes,
);
this.allPins.push(pin);
return pin;
});
this.initialConfig = props.initialConfig;
// this.outputPin = {
// id: this._generateId() as CCComponentPinId,
// componentId: this.id,
// type: "output",
// implementation: null,
// order: this._lastLocalIndex++,
// name: props.out.name,
// };
// IntrinsicComponentDefinition.intrinsicComponentPinAttributesByComponentPinId.set(
// this.outputPin.id,
// props.out
// );
// this.allPins.push(this.outputPin);
}

private static _pinAttributesByPinId: Map<
CCComponentPinId,
IntrinsicComponentPinAttributes
> = new Map();
static getPinAttributesByPinId(pinId: CCComponentPinId) {
return (
IntrinsicComponentDefinition._pinAttributesByPinId.get(pinId) ?? null
);
}
}
15 changes: 12 additions & 3 deletions src/store/intrinsics/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ function createBinaryOperator(
}
invariant(
inputValueA.length === inputValueB.length,
"Input lengths must match",
"Input lengths must match (name: " +
name +
", nodeId: " +
nodeId +
", inputValueA: " +
inputValueA +
", inputValueB: " +
inputValueB +
")",
);
const outputValue = Array.from({ length: inputValueA.length }, (_, i) =>
evaluate(nullthrows(inputValueA[i]), nullthrows(inputValueB[i])),
Expand Down Expand Up @@ -149,7 +157,7 @@ export const input =
type: ccIntrinsicComponentTypes.INPUT,
name: "Input",
in: {},
out: { Out: { name: "Out" } },
out: { Out: { name: "In" } },
initialConfig: null,
evaluate: (_context, _nodeId, _shape) => {
return true;
Expand All @@ -160,7 +168,7 @@ export const output =
new IntrinsicComponentDefinition<CCIntrinsicComponentOutputSpec>({
type: ccIntrinsicComponentTypes.OUTPUT,
name: "Output",
in: { In: { name: "In" } },
in: { In: { name: "Out" } },
out: {},
initialConfig: null,
evaluate: (_context, _nodeId, _shape) => {
Expand Down Expand Up @@ -272,6 +280,7 @@ export const flipflop =
const outputShape = shape.outputShape.Out;
invariant(outputShape[0] && !outputShape[1]);
const nodePinIdToValue = context.currentFrame.nodes.get(nodeId)?.pins;
console.log("FlipFlop evaluate", { nodeId, inputShape, outputShape });
const previousValue =
context.previousFrame?.nodes
.get(nodeId)
Expand Down
7 changes: 3 additions & 4 deletions src/store/nodePin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,9 @@ export class CCNodePinStore extends EventEmitter<CCNodePinStoreEvents> {
partialPin: Omit<CCNodePin, "id" | "manualBitWidth"> &
Partial<Pick<CCNodePin, "manualBitWidth">>,
): CCNodePin {
const attributes =
IntrinsicComponentDefinition.intrinsicComponentPinAttributesByComponentPinId.get(
partialPin.componentPinId,
);
const attributes = IntrinsicComponentDefinition.getPinAttributesByPinId(
partialPin.componentPinId,
);
return {
...partialPin,
id: crypto.randomUUID() as CCNodePinId,
Expand Down
Loading