Skip to content

Commit 2fc0f05

Browse files
committed
[feature] - Add latches
1 parent c9480f1 commit 2fc0f05

File tree

10 files changed

+3928
-92
lines changed

10 files changed

+3928
-92
lines changed

.eslintrc.js

+21-38
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,30 @@
11
module.exports = {
2-
"env": {
3-
"browser": true,
4-
"es6": true
2+
env: {
3+
browser: true,
4+
es6: true,
5+
jest: true,
56
},
6-
"extends": [
7-
"eslint:recommended",
8-
"prettier",
9-
],
10-
"globals": {
11-
"Atomics": "readonly",
12-
"SharedArrayBuffer": "readonly"
7+
extends: ["eslint:recommended", "prettier"],
8+
globals: {
9+
Atomics: "readonly",
10+
SharedArrayBuffer: "readonly",
1311
},
14-
"parserOptions": {
15-
"ecmaVersion": 2019,
16-
"sourceType": "module"
12+
parserOptions: {
13+
ecmaVersion: 2019,
14+
sourceType: "module",
1715
},
18-
"plugins": [
19-
"svelte3",
20-
"prettier"
21-
],
16+
plugins: ["svelte3", "prettier"],
2217
overrides: [
2318
{
24-
files: ['**/*.svelte'],
25-
processor: 'svelte3/svelte3'
26-
}
19+
files: ["**/*.svelte"],
20+
processor: "svelte3/svelte3",
21+
},
2722
],
28-
"rules": {
23+
rules: {
2924
"prettier/prettier": "error",
30-
"indent": [
31-
"error",
32-
"tab"
33-
],
34-
"linebreak-style": [
35-
"error",
36-
"unix"
37-
],
38-
"quotes": [
39-
"error",
40-
"double"
41-
],
42-
"semi": [
43-
"error",
44-
"always"
45-
]
46-
}
25+
indent: ["error", "tab"],
26+
"linebreak-style": ["error", "unix"],
27+
quotes: ["error", "double"],
28+
semi: ["error", "always"],
29+
},
4730
};

babel.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
"@babel/preset-env",
5+
{
6+
targets: {
7+
node: "current",
8+
},
9+
},
10+
],
11+
],
12+
};

jest.config.js

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// For a detailed explanation regarding each configuration property, visit:
2+
// https://jestjs.io/docs/en/configuration.html
3+
4+
module.exports = {
5+
// All imported modules in your tests should be mocked automatically
6+
// automock: false,
7+
8+
// Stop running tests after `n` failures
9+
// bail: 0,
10+
11+
// Respect "browser" field in package.json when resolving modules
12+
// browser: false,
13+
14+
// The directory where Jest should store its cached dependency information
15+
// cacheDirectory: "C:\\Users\\Rob\\AppData\\Local\\Temp\\jest",
16+
17+
// Automatically clear mock calls and instances between every test
18+
clearMocks: true,
19+
20+
// Indicates whether the coverage information should be collected while executing the test
21+
// collectCoverage: false,
22+
23+
// An array of glob patterns indicating a set of files for which coverage information should be collected
24+
// collectCoverageFrom: undefined,
25+
26+
// The directory where Jest should output its coverage files
27+
coverageDirectory: "coverage",
28+
29+
// An array of regexp pattern strings used to skip coverage collection
30+
// coveragePathIgnorePatterns: [
31+
// "\\\\node_modules\\\\"
32+
// ],
33+
34+
// A list of reporter names that Jest uses when writing coverage reports
35+
// coverageReporters: [
36+
// "json",
37+
// "text",
38+
// "lcov",
39+
// "clover"
40+
// ],
41+
42+
// An object that configures minimum threshold enforcement for coverage results
43+
// coverageThreshold: undefined,
44+
45+
// A path to a custom dependency extractor
46+
// dependencyExtractor: undefined,
47+
48+
// Make calling deprecated APIs throw helpful error messages
49+
// errorOnDeprecated: false,
50+
51+
// Force coverage collection from ignored files using an array of glob patterns
52+
// forceCoverageMatch: [],
53+
54+
// A path to a module which exports an async function that is triggered once before all test suites
55+
// globalSetup: undefined,
56+
57+
// A path to a module which exports an async function that is triggered once after all test suites
58+
// globalTeardown: undefined,
59+
60+
// A set of global variables that need to be available in all test environments
61+
// globals: {},
62+
63+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
64+
// maxWorkers: "50%",
65+
66+
// An array of directory names to be searched recursively up from the requiring module's location
67+
// moduleDirectories: [
68+
// "node_modules"
69+
// ],
70+
71+
// An array of file extensions your modules use
72+
// moduleFileExtensions: [
73+
// "js",
74+
// "json",
75+
// "jsx",
76+
// "ts",
77+
// "tsx",
78+
// "node"
79+
// ],
80+
81+
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
82+
// moduleNameMapper: {},
83+
84+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
85+
// modulePathIgnorePatterns: [],
86+
87+
// Activates notifications for test results
88+
// notify: false,
89+
90+
// An enum that specifies notification mode. Requires { notify: true }
91+
// notifyMode: "failure-change",
92+
93+
// A preset that is used as a base for Jest's configuration
94+
// preset: undefined,
95+
96+
// Run tests from one or more projects
97+
// projects: undefined,
98+
99+
// Use this configuration option to add custom reporters to Jest
100+
// reporters: undefined,
101+
102+
// Automatically reset mock state between every test
103+
// resetMocks: false,
104+
105+
// Reset the module registry before running each individual test
106+
// resetModules: false,
107+
108+
// A path to a custom resolver
109+
// resolver: undefined,
110+
111+
// Automatically restore mock state between every test
112+
// restoreMocks: false,
113+
114+
// The root directory that Jest should scan for tests and modules within
115+
// rootDir: undefined,
116+
117+
// A list of paths to directories that Jest should use to search for files in
118+
// roots: [
119+
// "<rootDir>"
120+
// ],
121+
122+
// Allows you to use a custom runner instead of Jest's default test runner
123+
// runner: "jest-runner",
124+
125+
// The paths to modules that run some code to configure or set up the testing environment before each test
126+
// setupFiles: [],
127+
128+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
129+
// setupFilesAfterEnv: [],
130+
131+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
132+
// snapshotSerializers: [],
133+
134+
// The test environment that will be used for testing
135+
testEnvironment: "node",
136+
137+
// Options that will be passed to the testEnvironment
138+
// testEnvironmentOptions: {},
139+
140+
// Adds a location field to test results
141+
// testLocationInResults: false,
142+
143+
// The glob patterns Jest uses to detect test files
144+
// testMatch: [
145+
// "**/__tests__/**/*.[jt]s?(x)",
146+
// "**/?(*.)+(spec|test).[tj]s?(x)"
147+
// ],
148+
149+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
150+
// testPathIgnorePatterns: [
151+
// "\\\\node_modules\\\\"
152+
// ],
153+
154+
// The regexp pattern or array of patterns that Jest uses to detect test files
155+
// testRegex: [],
156+
157+
// This option allows the use of a custom results processor
158+
// testResultsProcessor: undefined,
159+
160+
// This option allows use of a custom test runner
161+
// testRunner: "jasmine2",
162+
163+
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
164+
// testURL: "http://localhost",
165+
166+
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
167+
// timers: "real",
168+
169+
// A map from regular expressions to paths to transformers
170+
// transform: undefined,
171+
172+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
173+
// transformIgnorePatterns: [
174+
// "\\\\node_modules\\\\"
175+
// ],
176+
177+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
178+
// unmockedModulePathPatterns: undefined,
179+
180+
// Indicates whether each individual test should be reported during the run
181+
// verbose: undefined,
182+
183+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
184+
// watchPathIgnorePatterns: [],
185+
186+
// Whether to use watchman for file crawling
187+
// watchman: true,
188+
};

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@
77
"scripts": {
88
"build": "rollup -c",
99
"dev": "rollup -c -w",
10-
"start": "sirv public"
10+
"start": "sirv public",
11+
"test:unit": "jest"
1112
},
1213
"devDependencies": {
14+
"@babel/core": "^7.9.0",
15+
"@babel/preset-env": "^7.9.5",
1316
"@rollup/plugin-commonjs": "11.0.2",
1417
"@rollup/plugin-node-resolve": "^7.0.0",
18+
"babel-jest": "^25.3.0",
1519
"eslint": "^6.8.0",
1620
"eslint-config-prettier": "^6.10.1",
1721
"eslint-plugin-prettier": "^3.1.3",
1822
"eslint-plugin-svelte3": "^2.7.3",
23+
"jest": "^25.3.0",
1924
"prettier": "^2.0.4",
2025
"rollup": "^1.20.0",
2126
"rollup-plugin-livereload": "^1.0.0",

src/components/gates/not.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
* @param {0b0 | 0b1} input - A single bit
44
* @returns {0b0 | 0b1}
55
*/
6-
export function not(input = 0b0) {
6+
export function notGate(input = 0b0) {
77
return input ? 0b0 : 0b1;
88
}

src/components/latches/and-or.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { orGate, andGate, notGate } from "../gates";
2+
3+
const defaultBit = 0b0;
4+
5+
export class AndOrLatch {
6+
constructor() {
7+
this.current = defaultBit;
8+
}
9+
10+
get output() {
11+
return this.current;
12+
}
13+
14+
trigger(set = defaultBit, reset = defaultBit) {
15+
const orResult = orGate(this.current, set);
16+
const andResult = andGate(orResult, notGate(reset));
17+
this.current = andResult;
18+
}
19+
}

src/components/latches/gated.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { andGate, notGate } from "../gates";
2+
import { AndOrLatch } from "./and-or";
3+
4+
const defaultBit = 0b0;
5+
6+
export class GatedLatch {
7+
constructor() {
8+
this.andOrLatch = new AndOrLatch();
9+
}
10+
11+
get output() {
12+
return this.andOrLatch.current;
13+
}
14+
15+
trigger(dataInput = defaultBit, writeEnable = defaultBit) {
16+
const set = andGate(dataInput, writeEnable);
17+
const reset = andGate(notGate(dataInput), writeEnable);
18+
this.andOrLatch.trigger(set, reset);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { AndOrLatch } from "../and-or";
2+
3+
describe("Latches - AndOrLatch", () => {
4+
test("new latch outputs 0", () => {
5+
const latch = new AndOrLatch();
6+
expect(latch.output).toBe(0b0);
7+
});
8+
9+
test("Setting input to 1 outputs 1", () => {
10+
const latch = new AndOrLatch();
11+
latch.trigger(0b1, 0b0);
12+
expect(latch.output).toBe(0b1);
13+
});
14+
15+
test("Setting input to 0 outputs 0", () => {
16+
const latch = new AndOrLatch();
17+
latch.trigger(0b0, 0b0);
18+
expect(latch.output).toBe(0b0);
19+
});
20+
21+
test("Latch remembers its value", () => {
22+
const latch = new AndOrLatch();
23+
24+
(() => {
25+
// source 1
26+
latch.trigger(0b1, 0b0);
27+
latch.trigger(0b0, 0b0);
28+
})();
29+
30+
(() => {
31+
// source 2
32+
latch.trigger(0b0, 0b0);
33+
latch.trigger(0b1, 0b0);
34+
})();
35+
36+
expect(latch.output).toBe(0b1);
37+
});
38+
39+
test("Resetting latch outputs 0", () => {
40+
const latch = new AndOrLatch();
41+
42+
latch.trigger(0b1, 0b1);
43+
expect(latch.output).toBe(0b0);
44+
45+
latch.trigger(0b0, 0b1);
46+
expect(latch.output).toBe(0b0);
47+
});
48+
});

0 commit comments

Comments
 (0)