From 8dcecf649b13a7b22e9194f03d815fcea96e4d76 Mon Sep 17 00:00:00 2001 From: Mike A Date: Thu, 11 Jan 2024 11:20:30 +0100 Subject: [PATCH] Add getCellTypeStr() for cell name conversion --- src/circuit.mjs | 118 +++++++++++++++++++++++++----------------------- src/index.mjs | 4 +- 2 files changed, 63 insertions(+), 59 deletions(-) diff --git a/src/circuit.mjs b/src/circuit.mjs index da780ec..4ead4f3 100644 --- a/src/circuit.mjs +++ b/src/circuit.mjs @@ -12,65 +12,69 @@ import * as transform from './transform.mjs'; import { SynchEngine } from './engines/synch.mjs'; export { cells, tools, engines, transform }; - + +const CELL_TYPES = { + '$not': 'Not', + '$and': 'And', + '$nand': 'Nand', + '$or': 'Or', + '$nor': 'Nor', + '$xor': 'Xor', + '$xnor': 'Xnor', + '$reduce_and': 'AndReduce', + '$reduce_nand': 'NandReduce', + '$reduce_or': 'OrReduce', + '$reduce_nor': 'NorReduce', + '$reduce_xor': 'XorReduce', + '$reduce_xnor': 'XnorReduce', + '$reduce_bool': 'OrReduce', + '$logic_not': 'NorReduce', + '$repeater': 'Repeater', + '$shl': 'ShiftLeft', + '$shr': 'ShiftRight', + '$lt': 'Lt', + '$le': 'Le', + '$eq': 'Eq', + '$ne': 'Ne', + '$gt': 'Gt', + '$ge': 'Ge', + '$constant': 'Constant', + '$neg': 'Negation', + '$pos': 'UnaryPlus', + '$add': 'Addition', + '$sub': 'Subtraction', + '$mul': 'Multiplication', + '$div': 'Division', + '$mod': 'Modulo', + '$pow': 'Power', + '$mux': 'Mux', + '$pmux': 'Mux1Hot', + '$dff': 'Dff', + '$mem': 'Memory', + '$fsm': 'FSM', + '$clock': 'Clock', + '$button': 'Button', + '$lamp': 'Lamp', + '$numdisplay': 'NumDisplay', + '$numentry': 'NumEntry', + '$input': 'Input', + '$output': 'Output', + '$busgroup': 'BusGroup', + '$busungroup': 'BusUngroup', + '$busslice': 'BusSlice', + '$zeroextend': 'ZeroExtend', + '$signextend': 'SignExtend', + '$display7': 'Display7', +}; + +export function getCellTypeStr(tp) { + return CELL_TYPES[tp] ?? null; +} + export function getCellType(tp) { - const types = { - '$not': cells.Not, - '$and': cells.And, - '$nand': cells.Nand, - '$or': cells.Or, - '$nor': cells.Nor, - '$xor': cells.Xor, - '$xnor': cells.Xnor, - '$reduce_and': cells.AndReduce, - '$reduce_nand': cells.NandReduce, - '$reduce_or': cells.OrReduce, - '$reduce_nor': cells.NorReduce, - '$reduce_xor': cells.XorReduce, - '$reduce_xnor': cells.XnorReduce, - '$reduce_bool': cells.OrReduce, - '$logic_not': cells.NorReduce, - '$repeater': cells.Repeater, - '$shl': cells.ShiftLeft, - '$shr': cells.ShiftRight, - '$lt': cells.Lt, - '$le': cells.Le, - '$eq': cells.Eq, - '$ne': cells.Ne, - '$gt': cells.Gt, - '$ge': cells.Ge, - '$constant': cells.Constant, - '$neg': cells.Negation, - '$pos': cells.UnaryPlus, - '$add': cells.Addition, - '$sub': cells.Subtraction, - '$mul': cells.Multiplication, - '$div': cells.Division, - '$mod': cells.Modulo, - '$pow': cells.Power, - '$mux': cells.Mux, - '$pmux': cells.Mux1Hot, - '$dff': cells.Dff, - '$mem': cells.Memory, - '$fsm': cells.FSM, - '$clock': cells.Clock, - '$button': cells.Button, - '$lamp': cells.Lamp, - '$numdisplay': cells.NumDisplay, - '$numentry': cells.NumEntry, - '$input': cells.Input, - '$output': cells.Output, - '$busgroup': cells.BusGroup, - '$busungroup': cells.BusUngroup, - '$busslice': cells.BusSlice, - '$zeroextend': cells.ZeroExtend, - '$signextend': cells.SignExtend, - '$display7': cells.Display7, - }; - if (tp in types) return types[tp]; - else return cells.Subcircuit; + return cells[getCellTypeStr(tp)] ?? cells.Subcircuit; } - + export class HeadlessCircuit { constructor(data, {cellsNamespace = {}, engine = SynchEngine, engineOptions = {}, cellAttributes = {}} = {}) { this._cells = Object.assign(cells, cellsNamespace); diff --git a/src/index.mjs b/src/index.mjs index a4015fa..427bd3d 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -14,7 +14,7 @@ import * as cells from './cells.mjs'; import * as engines from './engines.mjs'; import * as tools from './tools.mjs'; import * as transform from './transform.mjs'; -import { HeadlessCircuit, getCellType } from './circuit.mjs'; +import { HeadlessCircuit, getCellTypeStr, getCellType } from './circuit.mjs'; import { BrowserSynchEngine } from './engines/browsersynch.mjs'; import { MonitorView, Monitor } from './monitor.mjs'; import { IOPanelView } from './iopanel.mjs'; @@ -26,7 +26,7 @@ import './style.css'; // see https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver#Browser_compatibility import ResizeObserver from 'resize-observer-polyfill'; -export { HeadlessCircuit, getCellType, cells, tools, engines, transform, MonitorView, Monitor, IOPanelView }; +export { HeadlessCircuit, getCellTypeStr, getCellType, cells, tools, engines, transform, MonitorView, Monitor, IOPanelView }; export const paperOptions = { async: true,