Skip to content

Commit

Permalink
Truth Table
Browse files Browse the repository at this point in the history
  • Loading branch information
LHandFPGA authored and tomcl committed Jan 11, 2025
1 parent ef81c7f commit 915e0b2
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 8 deletions.
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
{
"target": "dmg",
"arch": "x64"
}, {
},
{
"target": "dmg",
"arch": "arm64"
}
Expand All @@ -82,16 +83,17 @@
"@electron/remote": "^2",
"async-mutex": "^0.4",
"bulma": "^0.9",
"uuid": "10.0.0",
"bulma-tooltip": "^3",
"core-js": "^3",
"cross-zip": "^3",
"font-awesome": "^4",
"katex": "^0.16.19",
"nearley-unparse": "^1.0.1",
"npm": "^10.2.0",
"react-tooltip": "^5",
"source-map-support": "^0.5",
"usb": "^2.4.3"
"usb": "^2.4.3",
"uuid": "10.0.0"
},
"devDependencies": {
"@electron/remote": "^2",
Expand Down
86 changes: 86 additions & 0 deletions src/Renderer/Simulator/SimGraphTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module rec SimGraphTypes

open Fable.Core
open CommonTypes
open Fable.Core.JsInterop
importSideEffects "katex/dist/katex.min.css"
let katex: obj = importDefault "katex"

/// Binary data used in simulation
type Bit =
Expand Down Expand Up @@ -476,6 +479,89 @@ let expToString exp =
else
expS

let rec expToKatex (exp: FastAlgExp) : string =
let rec expToKatex' exp =
match exp with
| SingleTerm (_, label, _) ->
// Variable
string label

| DataLiteral { Dat = Word w; Width = _ } ->
// Num to string
string w

| DataLiteral { Dat = BigWord w; Width = _ } ->
string w

| UnaryExp (NegOp, e) ->
// -
sprintf "(-%s)" (expToKatex' e)

| UnaryExp (NotOp, e) ->
// not, ~, use \overline
sprintf "\\overline{%s}" (expToKatex' e)

| UnaryExp (BitRangeOp (low, up), e) ->
// A[u:l] to A_{[u:l]}
let baseStr = expToKatex' e
if low = up then
sprintf "%s_{[%d]}" baseStr up
else
sprintf "%s_{[%d:%d]}" baseStr up low

| UnaryExp (CarryOfOp, e) ->
sprintf "\\mathrm{carry}\\bigl(%s\\bigr)" (expToKatex' e)

| BinaryExp (e1, AddOp, e2) ->
// +
sprintf "\\bigl(%s + %s\\bigr)" (expToKatex' e1) (expToKatex' e2)

| BinaryExp (e1, SubOp, e2) ->
// -
sprintf "\\bigl(%s - %s\\bigr)" (expToKatex' e1) (expToKatex' e2)

| BinaryExp (e1, BitAndOp, e2) ->
// AND, \cdot
sprintf "%s \\cdot %s" (expToKatex' e1) (expToKatex' e2)

| BinaryExp (e1, BitOrOp, e2) ->
// OR, +
sprintf "%s + %s" (expToKatex' e1) (expToKatex' e2)

| BinaryExp (e1, BitXorOp, e2) ->
// XOR, \oplus
sprintf "%s \\oplus %s" (expToKatex' e1) (expToKatex' e2)

| ComparisonExp (e, Equals, x) ->
// =
sprintf "\\bigl(%s = %s\\bigr)" (expToKatex' e) (string x)

| AppendExp exps ->
exps
|> List.map expToKatex'
|> String.concat "\\Vert "
|> sprintf "\\bigl(%s\\bigr)"

let rec arithmeticToKatex exp =
// change it to LaTeX
exp
|> flattenNestedArithmetic
|> List.mapi (fun i expr ->
match i, expr with
| 0, e -> expToKatex' e
| _, UnaryExp(NegOp, e) -> sprintf "- %s" (expToKatex' e)
| _, e -> sprintf "+ %s" (expToKatex' e))
|> String.concat " "

let katexStr = expToKatex' exp

// delete the outermost parentheses
if katexStr.StartsWith "(" && katexStr.EndsWith ")" then
katexStr[1 .. katexStr.Length - 2]
else
katexStr


/// Recursively evaluates an expression to reduce it to its simplest form
let rec evalExp exp =
match exp with
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/Simulator/TruthTableTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ open SimTypes

type CellData =
| Bits of wd: WireData
| Algebra of var: string
| Algebra of var: FastAlgExp
| DC //Don't Care

type CellIO =
Expand Down
9 changes: 6 additions & 3 deletions src/Renderer/UI/TruthTable/TruthTableCreate.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ open SynchronousUtils
open NumberHelpers
open FastExtract
open Helpers
open Fable.Core.JsInterop
importSideEffects "katex/dist/katex.min.css"
let katex: obj = importDefault "katex"

/// Wraps SimulationIO and Viewer types in the CellIO DU Type
let toCellIO simIOs viewers =
Expand Down Expand Up @@ -96,7 +99,7 @@ let tableLHS (inputs: SimulationIO list) (ttt: ModelType.TTType) :
algebraInputs
|> List.map (fun io ->
let (_,label,_) = io
{IO = SimIO io; Data = Algebra (string label)})
{IO = SimIO io; Data = Algebra (SingleTerm io)})

if numericInputs.IsEmpty then
// All inputs are algebraic, so only one row in table with all algebra
Expand Down Expand Up @@ -137,13 +140,13 @@ let simulateInputCombination
|> List.map (fun (comp,out) ->
match out with
| IData wd -> {IO = SimIO comp; Data = Bits (convertFastDataToWireData wd)}
| IAlg exp -> {IO = SimIO comp; Data = Algebra (expToString exp)})
| IAlg exp -> {IO = SimIO comp; Data = Algebra exp})
let viewerRow =
FastExtract.extractViewers simData
|> List.map (fun ((l,f),w,fs) ->
match fs with
| IData wd -> {IO = Viewer ((l,f),w); Data = Bits (convertFastDataToWireData wd)}
| IAlg exp -> {IO = Viewer ((l,f),w); Data = Algebra (expToString exp)})
| IAlg exp -> {IO = Viewer ((l,f),w); Data = Algebra exp})
outputRow @ viewerRow

/// Create a Truth Table from Simulation Data, taking into account
Expand Down
18 changes: 17 additions & 1 deletion src/Renderer/UI/TruthTable/TruthTableView.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ open Fulma
open Fulma.Extensions.Wikiki
open Fable.React
open Fable.React.Props
open Fable.Core.JsInterop

open NumberHelpers
open Helpers
Expand All @@ -37,6 +38,8 @@ open SymbolUpdate
open Sheet.SheetInterface
open DrawModelType

let katex: obj = importDefault "katex"

/// Updates MergeWires and SplitWire Component labels to MWx/SWx.
/// Previous Issie versions had empty labels for these components.
// This change is necessary as all components must have labels for automatic IO
Expand Down Expand Up @@ -668,7 +671,20 @@ let viewRowAsData numBase styleInfo i (row: TruthTableCell list) =
let width = List.length bits
let value = viewFilledNum width numBase <| convertWireDataToInt bits
div [cellStyle] [str value]
| Algebra a -> div [cellStyle] [str <| a]
// | Algebra a -> div [cellStyle] [str <| a]
| Algebra a ->
let katexString = expToKatex a
let renderedHtml =
katex?renderToString(
katexString,
createObj[
"throwOnError" ==> false
]
)
div [
cellStyle
DangerouslySetInnerHTML { __html = renderedHtml }
] []
| DC -> div [cellStyle] [str <| "X"]

let cells =
Expand Down

0 comments on commit 915e0b2

Please sign in to comment.