Skip to content

Commit de1fc3d

Browse files
committed
feat: add LogicTable
- refactor logic gate tables with LogicTable
1 parent 4160668 commit de1fc3d

File tree

3 files changed

+90
-39
lines changed

3 files changed

+90
-39
lines changed

src/Electrical/Digital/tables.jl

Lines changed: 75 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,55 @@
1+
struct LogicTable{N} <: AbstractArray{Logic, N}
2+
logic::Array{Logic, N}
3+
function LogicTable(l::Array{Logic})
4+
any(i -> i != 9, size(l)) &&
5+
throw(ArgumentError("Incorrect number of logic values are passed. A variable of type
6+
`LogicTable` must have nine entries corresponding to 9 logic levels"))
7+
new{ndims(l)}(l)
8+
end
9+
end
10+
11+
Base.size(l::LogicTable) = size(l.logic)
12+
13+
Base.axes(l::LogicTable) = axes(l.logic)
14+
15+
getindex(s::LogicTable, l::Logic) = getindex(s.logic, get_logic_level(l))
16+
function Base.getindex(s::LogicTable, i1::Logic, i2::Logic)
17+
getindex(s.logic, get_logic_level(i1), get_logic_level(i2))
18+
end
19+
function Base.getindex(s::LogicTable, i1::Logic, i2::Logic,
20+
I::Logic...)
21+
getindex(s.logic, get_logic_level(i1), get_logic_level(i2), get_logic_level(I...)...)
22+
end
23+
24+
getindex(s::LogicTable, l::Int) = getindex(s.logic, l)
25+
function getindex(s::LogicTable, i1::Int, i2::Int, I::Int...)
26+
getindex(s.logic, i1, i2, I...)
27+
end
28+
29+
function Base.setindex!(A::LogicTable, x::Logic, i1::Int)
30+
setindex!(A.logic, x, i1)
31+
end
32+
function Base.setindex!(A::LogicTable, x::Logic, i1::Int, i2::Int, I::Int...)
33+
setindex!(A.logic, x, i1, i2, I...)
34+
end
35+
36+
get_logic_level(l::LogicTable) = Int.(l.logic)
37+
138
# AND gate
2-
const AndTable = [
3-
# U X F0 F1 Z W L H DC
4-
U U F0 U U U F0 U U # U
5-
U X F0 X X X F0 X X # X
6-
F0 F0 F0 F0 F0 F0 F0 F0 F0 # F0
7-
U X F0 F1 X X F0 F1 X # F1
8-
U X F0 X X X F0 X X # Z
9-
U X F0 X X X F0 X X # W
10-
F0 F0 F0 F0 F0 F0 F0 F0 F0 # L
11-
U X F0 F1 X X F0 F1 X # H
12-
U X F0 X X X F0 X X] # DC
39+
const AndTable = LogicTable([
40+
# U X F0 F1 Z W L H DC
41+
U U F0 U U U F0 U U # U
42+
U X F0 X X X F0 X X # X
43+
F0 F0 F0 F0 F0 F0 F0 F0 F0 # F0
44+
U X F0 F1 X X F0 F1 X # F1
45+
U X F0 X X X F0 X X # Z
46+
U X F0 X X X F0 X X # W
47+
F0 F0 F0 F0 F0 F0 F0 F0 F0 # L
48+
U X F0 F1 X X F0 F1 X # H
49+
U X F0 X X X F0 X X]) # DC
1350

1451
function _and2(a::Logic, b::Logic)
15-
AndTable[get_logic_level(a), get_logic_level(b)]
52+
AndTable[a, b]
1653
end
1754
_and2(a::Number, b::Logic) = _and2(convert(Logic, a), b)
1855
_and2(a::Logic, b::Number) = _and2(a, convert(Logic, b))
@@ -29,28 +66,28 @@ end
2966
@register_symbolic _and(a, b)
3067

3168
# NOT gate
32-
const NotTable = [U, X, F1, F0, X, X, F1, F0, X]
69+
const NotTable = LogicTable([U, X, F1, F0, X, X, F1, F0, X])
3370

34-
_not(x::Logic) = NotTable[get_logic_level(x)]
71+
_not(x::Logic) = NotTable[x]
3572
_not(x::Number) = _not(convert(Logic, x))
3673

3774
@register_symbolic _not(x)
3875

3976
# OR gate
40-
const OrTable = [
41-
# U X F0 F1 Z W L H DC
42-
U U U F1 U U U F1 U # U
43-
U X X F1 X X X F1 X # X
44-
U X F0 F1 X X F0 F1 X # F0
45-
F1 F1 F1 F1 F1 F1 F1 F1 F1 # F1
46-
U X X F1 X X X F1 X # Z
47-
U X X F1 X X X F1 X # W
48-
U X F0 F1 X X F0 F1 X # L
49-
F1 F1 F1 F1 F1 F1 F1 F1 F1 # H
50-
U X X F1 X X X F1 X] # DC
77+
const OrTable = LogicTable([
78+
# U X F0 F1 Z W L H DC
79+
U U U F1 U U U F1 U # U
80+
U X X F1 X X X F1 X # X
81+
U X F0 F1 X X F0 F1 X # F0
82+
F1 F1 F1 F1 F1 F1 F1 F1 F1 # F1
83+
U X X F1 X X X F1 X # Z
84+
U X X F1 X X X F1 X # W
85+
U X F0 F1 X X F0 F1 X # L
86+
F1 F1 F1 F1 F1 F1 F1 F1 F1 # H
87+
U X X F1 X X X F1 X]) # DC
5188

5289
function _or2(a::Logic, b::Logic)
53-
OrTable[get_logic_level(a), get_logic_level(b)]
90+
OrTable[a, b]
5491
end
5592
_or2(a::Number, b::Logic) = _or2(convert(Logic, a), b)
5693
_or2(a::Logic, b::Number) = _or2(a, convert(Logic, b))
@@ -67,20 +104,20 @@ end
67104
@register_symbolic _or(a, b)
68105

69106
# XOR gate
70-
const XorTable = [
71-
# U X F0 F1 Z W L H DC
72-
U U U U U U U U U # U
73-
U X X X X X X X X # X
74-
U X F0 F1 X X F0 F1 X # F0
75-
U X F1 F0 X X F1 F0 X # F1
76-
U X X X X X X X X # Z
77-
U X X X X X X X X # W
78-
U X F0 F1 X X F0 F1 X # L
79-
U X F1 F0 X X F1 F0 X # H
80-
U X X X X X X X X] # DC
107+
const XorTable = LogicTable([
108+
# U X F0 F1 Z W L H DC
109+
U U U U U U U U U # U
110+
U X X X X X X X X # X
111+
U X F0 F1 X X F0 F1 X # F0
112+
U X F1 F0 X X F1 F0 X # F1
113+
U X X X X X X X X # Z
114+
U X X X X X X X X # W
115+
U X F0 F1 X X F0 F1 X # L
116+
U X F1 F0 X X F1 F0 X # H
117+
U X X X X X X X X]) # DC
81118

82119
function _xor2(a::Logic, b::Logic)
83-
XorTable[get_logic_level(a), get_logic_level(b)]
120+
XorTable[a, b]
84121
end
85122
_xor2(a::Number, b::Logic) = _xor2(convert(Logic, a), b)
86123
_xor2(a::Logic, b::Number) = _xor2(a, convert(Logic, b))

src/Electrical/Electrical.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export StdLogicVector, StdULogicVector,
4141
get_logic_level
4242
include("Digital/logic_vectors.jl")
4343

44+
export LogicTable,
45+
AndTable, OrTable, NotTable, XorTable
4446
include("Digital/tables.jl")
4547

4648
end

test/Electrical/digital.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,19 @@ using ModelingToolkitStandardLibrary.Electrical: get_logic_level
5959
@test_throws "3 isn't a valid `Logic` value" convert(Logic, 3)
6060
end
6161

62-
@testset "Logic tables and logic gate helpers" begin
62+
@testset "Logic Tables" begin
63+
# LogicTable vec-or-mat and helpers
64+
test_not_logic_table = LogicTable([U, X, F1, F0, X, X, F1, F0, X])
65+
@test test_not_logic_table[1] == U
66+
@test test_not_logic_table[F1] == F0
67+
68+
test_not_logic_table[1] = X
69+
@test test_not_logic_table[1] == X
70+
71+
@test_throws ArgumentError LogicTable([U; U])
72+
end
73+
74+
@testset "Gate tables and logic gate helpers" begin
6375
# logic tables and logic gate helpers
6476
@test size(AndTable) == size(OrTable) == size(XorTable) == (9, 9)
6577
@test size(NotTable) == (9,)

0 commit comments

Comments
 (0)