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

Align project file structure with software suite #417

Merged
merged 5 commits into from
Aug 1, 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
30 changes: 15 additions & 15 deletions components/src/stores/chip.store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { ChipStoreDispatch, makeChipStore } from "./chip.store.js";

function testChipStore(
fs: Record<string, string> = {
"projects/01/Not/Not.hdl": not.hdl,
"projects/01/Not/Not.tst": not.tst,
"projects/01/Not/Not.cmp": not.cmp,
"projects/01/Not.hdl": not.hdl,
"projects/01/Not.tst": not.tst,
"projects/01/Not.cmp": not.cmp,
},
storage: Record<string, string> = {},
) {
Expand Down Expand Up @@ -59,9 +59,9 @@ describe("ChipStore", () => {

it("reloads initial chip not", async () => {
const store = testChipStore({
"projects/01/Not/Not.hdl": not.hdl,
"projects/01/Not/Not.tst": not.tst,
"projects/01/Not/Not.cmp": not.cmp,
"projects/01/Not.hdl": not.hdl,
"projects/01/Not.tst": not.tst,
"projects/01/Not.cmp": not.cmp,
});

await store.actions.initialize();
Expand All @@ -77,12 +77,12 @@ describe("ChipStore", () => {
it("loads saved state", () => {
const { state } = testChipStore(
{
"projects/01/Not/Not.hdl": not.hdl,
"projects/01/Not/Not.tst": not.tst,
"projects/01/Not/Not.cmp": not.cmp,
"projects/03/Bit/Bit.hdl": bit.hdl,
"projects/03/Bit/Bit.tst": bit.tst,
"projects/03/Bit/Bit.cmp": bit.cmp,
"projects/01/Not.hdl": not.hdl,
"projects/01/Not.tst": not.tst,
"projects/01/Not.cmp": not.cmp,
"projects/03/Bit.hdl": bit.hdl,
"projects/03/Bit.tst": bit.tst,
"projects/03/Bit.cmp": bit.cmp,
},
{
"/chip/project": "03",
Expand Down Expand Up @@ -122,9 +122,9 @@ describe("ChipStore", () => {
describe("execution", () => {
const state = cleanState(async () => {
const store = testChipStore({
"projects/01/Not/Not.hdl": not.hdl,
"projects/01/Not/Not.tst": not.tst,
"projects/01/Not/Not.cmp": not.cmp,
"projects/01/Not.hdl": not.hdl,
"projects/01/Not.tst": not.tst,
"projects/01/Not.cmp": not.cmp,
});
await store.actions.initialize();
return { store };
Expand Down
33 changes: 15 additions & 18 deletions components/src/stores/chip.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ async function getTemplate(
];
}

return (
ChipProjects[project].CHIPS as Record<string, Record<string, string>>
)[chipName][`${chipName}.hdl`] as string;
return (ChipProjects[project].CHIPS as Record<string, string>)[
`${chipName}.hdl`
] as string;
}

async function getBuiltinCode(
Expand Down Expand Up @@ -421,8 +421,7 @@ export function makeChipStore(

async loadChip(project: string, name: string) {
storage["/chip/chip"] = name;
const fsName = (ext: string) =>
`/projects/${project}/${name}/${name}.${ext}`;
const fsName = (ext: string) => `/projects/${project}/${name}.${ext}`;

const hdl = await fs.readFile(fsName("hdl")).catch(() => makeHdl(name));

Expand All @@ -431,8 +430,10 @@ export function makeChipStore(
},

async initializeTest(name: string) {
tests = (await fs.scandir(`/projects/${project}/${name}`))
.filter((file) => file.name.endsWith(".tst"))
tests = (await fs.scandir(`/projects/${project}`))
.filter(
(file) => file.name.startsWith(name) && file.name.endsWith(".tst"),
)
.map((file) => file.name);
if (tests.length > 0) {
await this.setTest(tests[0]);
Expand All @@ -441,13 +442,9 @@ export function makeChipStore(

async loadTest(test: string) {
const [tst, cmp] = await Promise.all([
fs.readFile(`/projects/${project}/${test}`).catch(() => makeTst()),
fs
.readFile(`/projects/${project}/${chipName}/${test}`)
.catch(() => makeTst()),
fs
.readFile(
`/projects/${project}/${chipName}/${test}`.replace(".tst", ".cmp"),
)
.readFile(`/projects/${project}/${test}`.replace(".tst", ".cmp"))
.catch(() => makeCmp()),
]);
dispatch.current({ action: "setFiles", payload: { cmp, tst } });
Expand All @@ -456,7 +453,7 @@ export function makeChipStore(

async saveChip(hdl: string, prj = project, name = chipName) {
dispatch.current({ action: "setFiles", payload: { hdl } });
const path = `/projects/${prj}/${name}/${name}.hdl`;
const path = `/projects/${prj}/${name}.hdl`;
await fs.writeFile(path, hdl);
setStatus(`Saved ${path}`);
},
Expand Down Expand Up @@ -567,17 +564,17 @@ export function makeChipStore(

async resetFile() {
const { ChipProjects } = await import("@nand2tetris/projects/full.js");
const template = (
ChipProjects[project].CHIPS as Record<string, Record<string, string>>
)[chipName][`${chipName}.hdl`];
const template = (ChipProjects[project].CHIPS as Record<string, string>)[
`${chipName}.hdl`
];
dispatch.current({ action: "setFiles", payload: { hdl: template } });
},

async getProjectFiles() {
return await Promise.all(
CHIP_PROJECTS[project].map((chip) => ({
name: `${chip}.hdl`,
content: fs.readFile(`/projects/${project}/${chip}/${chip}.hdl`),
content: fs.readFile(`/projects/${project}/${chip}.hdl`),
})),
);
},
Expand Down
126 changes: 47 additions & 79 deletions projects/src/project_01/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,85 +19,53 @@ import * as DMux8Way from "./14_dmux8way.js";
import * as Or8Way from "./15_or8way.js";

export const CHIPS = {
Nand: {
"Nand.tst": Nand.tst,
"Nand.cmp": Nand.cmp,
},
Not: {
"Not.hdl": Not.hdl,
"Not.tst": Not.tst,
"Not.cmp": Not.cmp,
},
And: {
"And.hdl": And.hdl,
"And.tst": And.tst,
"And.cmp": And.cmp,
},
Or: {
"Or.hdl": Or.hdl,
"Or.tst": Or.tst,
"Or.cmp": Or.cmp,
},
Xor: {
"Xor.hdl": Xor.hdl,
"Xor.tst": Xor.tst,
"Xor.cmp": Xor.cmp,
},
Mux: {
"Mux.hdl": Mux.hdl,
"Mux.tst": Mux.tst,
"Mux.cmp": Mux.cmp,
},
DMux: {
"DMux.hdl": DMux.hdl,
"DMux.tst": DMux.tst,
"DMux.cmp": DMux.cmp,
},
Not16: {
"Not16.hdl": Not16.hdl,
"Not16.tst": Not16.tst,
"Not16.cmp": Not16.cmp,
},
And16: {
"And16.hdl": And16.hdl,
"And16.tst": And16.tst,
"And16.cmp": And16.cmp,
},
Or16: {
"Or16.hdl": Or16.hdl,
"Or16.tst": Or16.tst,
"Or16.cmp": Or16.cmp,
},
Mux16: {
"Mux16.hdl": Mux16.hdl,
"Mux16.tst": Mux16.tst,
"Mux16.cmp": Mux16.cmp,
},
Mux4Way16: {
"Mux4Way16.hdl": Mux4Way16.hdl,
"Mux4Way16.tst": Mux4Way16.tst,
"Mux4Way16.cmp": Mux4Way16.cmp,
},
Mux8Way16: {
"Mux8Way16.hdl": Mux8Way16.hdl,
"Mux8Way16.tst": Mux8Way16.tst,
"Mux8Way16.cmp": Mux8Way16.cmp,
},
DMux4Way: {
"DMux4Way.hdl": DMux4Way.hdl,
"DMux4Way.tst": DMux4Way.tst,
"DMux4Way.cmp": DMux4Way.cmp,
},
DMux8Way: {
"DMux8Way.hdl": DMux8Way.hdl,
"DMux8Way.tst": DMux8Way.tst,
"DMux8Way.cmp": DMux8Way.cmp,
},
Or8Way: {
"Or8Way.hdl": Or8Way.hdl,
"Or8Way.tst": Or8Way.tst,
"Or8Way.cmp": Or8Way.cmp,
},
"Nand.tst": Nand.tst,
"Nand.cmp": Nand.cmp,
"Not.hdl": Not.hdl,
"Not.tst": Not.tst,
"Not.cmp": Not.cmp,
"And.hdl": And.hdl,
"And.tst": And.tst,
"And.cmp": And.cmp,
"Or.hdl": Or.hdl,
"Or.tst": Or.tst,
"Or.cmp": Or.cmp,
"Xor.hdl": Xor.hdl,
"Xor.tst": Xor.tst,
"Xor.cmp": Xor.cmp,
"Mux.hdl": Mux.hdl,
"Mux.tst": Mux.tst,
"Mux.cmp": Mux.cmp,
"DMux.hdl": DMux.hdl,
"DMux.tst": DMux.tst,
"DMux.cmp": DMux.cmp,
"Not16.hdl": Not16.hdl,
"Not16.tst": Not16.tst,
"Not16.cmp": Not16.cmp,
"And16.hdl": And16.hdl,
"And16.tst": And16.tst,
"And16.cmp": And16.cmp,
"Or16.hdl": Or16.hdl,
"Or16.tst": Or16.tst,
"Or16.cmp": Or16.cmp,
"Mux16.hdl": Mux16.hdl,
"Mux16.tst": Mux16.tst,
"Mux16.cmp": Mux16.cmp,
"Mux4Way16.hdl": Mux4Way16.hdl,
"Mux4Way16.tst": Mux4Way16.tst,
"Mux4Way16.cmp": Mux4Way16.cmp,
"Mux8Way16.hdl": Mux8Way16.hdl,
"Mux8Way16.tst": Mux8Way16.tst,
"Mux8Way16.cmp": Mux8Way16.cmp,
"DMux4Way.hdl": DMux4Way.hdl,
"DMux4Way.tst": DMux4Way.tst,
"DMux4Way.cmp": DMux4Way.cmp,
"DMux8Way.hdl": DMux8Way.hdl,
"DMux8Way.tst": DMux8Way.tst,
"DMux8Way.cmp": DMux8Way.cmp,
"Or8Way.hdl": Or8Way.hdl,
"Or8Way.tst": Or8Way.tst,
"Or8Way.cmp": Or8Way.cmp,
};

export const BUILTIN_CHIPS = {
Expand Down
44 changes: 17 additions & 27 deletions projects/src/project_02/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,23 @@ import * as Inc16 from "./04_inc16.js";
import * as Alu from "./06_alu.js";

export const CHIPS = {
HalfAdder: {
"HalfAdder.hdl": HalfAdder.hdl,
"HalfAdder.tst": HalfAdder.tst,
"HalfAdder.cmp": HalfAdder.cmp,
},
FullAdder: {
"FullAdder.hdl": FullAdder.hdl,
"FullAdder.tst": FullAdder.tst,
"FullAdder.cmp": FullAdder.cmp,
},
Add16: {
"Add16.hdl": Add16.hdl,
"Add16.tst": Add16.tst,
"Add16.cmp": Add16.cmp,
},
Inc16: {
"Inc16.hdl": Inc16.hdl,
"Inc16.tst": Inc16.tst,
"Inc16.cmp": Inc16.cmp,
},
ALU: {
"ALU.hdl": Alu.hdl,
"ALU.tst": Alu.tst,
"ALU.cmp": Alu.cmp,
"ALU-basic.tst": Alu.basic_tst,
"ALU-basic.cmp": Alu.basic_cmp,
},
"HalfAdder.hdl": HalfAdder.hdl,
"HalfAdder.tst": HalfAdder.tst,
"HalfAdder.cmp": HalfAdder.cmp,
"FullAdder.hdl": FullAdder.hdl,
"FullAdder.tst": FullAdder.tst,
"FullAdder.cmp": FullAdder.cmp,
"Add16.hdl": Add16.hdl,
"Add16.tst": Add16.tst,
"Add16.cmp": Add16.cmp,
"Inc16.hdl": Inc16.hdl,
"Inc16.tst": Inc16.tst,
"Inc16.cmp": Inc16.cmp,
"ALU.hdl": Alu.hdl,
"ALU.tst": Alu.tst,
"ALU.cmp": Alu.cmp,
"ALU-basic.tst": Alu.basic_tst,
"ALU-basic.cmp": Alu.basic_cmp,
};

export const BUILTIN_CHIPS = {};
Expand Down
Loading
Loading