Class: Logicuit::DSL
- Inherits:
-
Object
show all
- Defined in:
- lib/logicuit/dsl.rb
Overview
base class for all gates and circuits
Direct Known Subclasses
Circuits::Combinational::FullAdder, Circuits::Combinational::FullAdder4bit, Circuits::Combinational::HalfAdder, Circuits::Combinational::Multiplexer2to1, Circuits::Combinational::Multiplexer4to1, Circuits::Sequential::DFlipFlop, Circuits::Sequential::OneBitCpu, Circuits::Sequential::ProgramCounter, Circuits::Sequential::Register4bit, Circuits::Td4::Cpu, Circuits::Td4::Decoder, Circuits::Td4::Rom, Gates::And, Gates::Nand, Gates::Not, Gates::Or, Gates::Xor
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ DSL
Returns a new instance of DSL.
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/logicuit/dsl.rb', line 9
def initialize(*args)
@input_targets = []
@inputs_as_bool_struct = nil
@output_targets = []
@clock = false
@components = []
inputs(*args)
outputs
assembling
@initialized = true
evaluate
end
|
Instance Attribute Details
#clock ⇒ Object
Returns the value of attribute clock.
27
28
29
|
# File 'lib/logicuit/dsl.rb', line 27
def clock
@clock
end
|
#components ⇒ Object
Returns the value of attribute components.
27
28
29
|
# File 'lib/logicuit/dsl.rb', line 27
def components
@components
end
|
#initialized ⇒ Object
Returns the value of attribute initialized.
27
28
29
|
# File 'lib/logicuit/dsl.rb', line 27
def initialized
@initialized
end
|
Returns the value of attribute input_targets.
27
28
29
|
# File 'lib/logicuit/dsl.rb', line 27
def input_targets
@input_targets
end
|
#output_targets ⇒ Object
Returns the value of attribute output_targets.
27
28
29
|
# File 'lib/logicuit/dsl.rb', line 27
def output_targets
@output_targets
end
|
Class Method Details
.assembling(&block) ⇒ Object
91
92
93
94
95
96
|
# File 'lib/logicuit/dsl.rb', line 91
def self.assembling(&block)
define_method(:assembling) do
ret = instance_eval(&block)
ret.each { @components << _1 } if ret.is_a?(Array)
end
end
|
.diagram(source) ⇒ Object
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/logicuit/dsl.rb', line 98
def self.diagram(source)
define_method(:to_s) do
source_ = @input_targets.reduce(source) do |result, input|
result.gsub(/\(#{input}\)/i, "(#{instance_variable_get("@#{input}")})#{"-" * (input.size - 1)}")
end
@output_targets.reduce(source_) do |result, output|
result.gsub(/\(#{output}\)/i, "#{"-" * (output.size - 1)}(#{instance_variable_get("@#{output}")})")
end
end
end
|
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/logicuit/dsl.rb', line 29
def self.inputs(*args, **kwargs)
args.each do |arg|
attr_reader(arg) unless instance_methods.include?(arg)
end
define_method(:inputs) do |*instance_method_args|
@clock = true if kwargs&.key?(:clock)
args.each_with_index do |input, index|
signal = Signals::Signal.new(instance_method_args[index] == 1)
signal >> self unless clock
instance_variable_set("@#{input}", signal)
@input_targets << input
end
Signals::Clock >> self if clock
@inputs_as_bool_struct = Struct.new(*@input_targets)
end
end
|
.outputs(*args, **kwargs) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/logicuit/dsl.rb', line 59
def self.outputs(*args, **kwargs)
(args + kwargs.keys).each do |arg|
attr_reader(arg) unless instance_methods.include?(arg)
end
define_method(:outputs) do
(args + kwargs.keys).each do |output|
instance_variable_set("@#{output}", Signals::Signal.new(false))
@output_targets << output
end
end
return if kwargs.empty?
define_method(:evaluate) do |*override_args|
return unless initialized
kwargs.each do |output, evaluator|
ret = if override_args.empty?
instance_exec(&evaluator)
else
o = @inputs_as_bool_struct.new(*override_args)
o.instance_exec(&evaluator)
end
send(output).send(ret.current ? :on : :off)
end
end
end
|
.run(opts = {}) ⇒ Object
181
182
183
|
# File 'lib/logicuit/dsl.rb', line 181
def self.run(opts = {})
::Logicuit.run(new, **opts)
end
|
.truth_table(source) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/logicuit/dsl.rb', line 109
def self.truth_table(source)
define_method(:truth_table) do
rows = source.strip.split("\n").map { |row| row.gsub(/#.*$/, "") }
= rows.shift.split("|").map(&:strip).reject(&:empty?).map(&:downcase).map(&:to_sym)
rows.shift table = rows.map do |row|
row.split("|").map(&:strip).reject(&:empty?).map(&:downcase).map do |v|
case v
when "^"
:clock
when "x"
:any
when "1"
true
when "0"
false
else
raise "Invalid value in truth table: #{v}" unless .include?(v.to_sym)
[:ref, v.to_sym]
end
end
end.select do |values|
.size == values.size
end.map do |values|
array = [values]
while array.any? { _1.any? { |v| v == :any } }
target_index = array.find_index { _1.any? { |v| v == :any } }
next if target_index.nil?
target = array[target_index]
prop_index = target.find_index { |v| v == :any }
array.delete_at(target_index)
array.insert(target_index, *[true, false].map do |v|
target.dup.tap do |a|
a[prop_index] = v
end
end)
end
array
end.flatten!(1).map do |values|
.zip(values).to_h
end
table
end
end
|
.verify_against_truth_table ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/logicuit/dsl.rb', line 156
def self.verify_against_truth_table
new.truth_table.each do |row|
args = row.values_at(*new.input_targets).map { _1 ? 1 : 0 }
subject = new(*args)
previous_values = row.reject do |_k, v|
v == :clock
end.keys.reduce({}) { |acc, key| acc.merge(key => subject.send(key).current) }
Signals::Clock.tick if row.values.find :clock
row.each do |key, value|
next if value == :clock
if value.is_a?(Array) && value.first == :ref
expected = previous_values[value.last]
raise "#{self}.new(#{args.join(", ")}).#{key} should be #{expected ? 1 : 0}" unless expected == subject.send(key).current
else
raise "#{self}.new(#{args.join(", ")}).#{key} should be #{value ? 1 : 0}" unless value == subject.send(key).current
end
end
end
end
|
Instance Method Details
#[](*keys) ⇒ Object
49
50
51
52
53
54
55
56
57
|
# File 'lib/logicuit/dsl.rb', line 49
def [](*keys)
if keys.size == 1
send(keys.first)
elsif keys.size > 1
Signals::SignalGroup.new(*(keys.map { |key| send(key) }))
else
raise ArgumentError, "Invalid number of arguments"
end
end
|
#assembling ⇒ Object
24
|
# File 'lib/logicuit/dsl.rb', line 24
def assembling; end
|
#evaluate(*args) ⇒ Object
25
|
# File 'lib/logicuit/dsl.rb', line 25
def evaluate(*args); end
|
22
|
# File 'lib/logicuit/dsl.rb', line 22
def inputs(*args); end
|
#outputs ⇒ Object
23
|
# File 'lib/logicuit/dsl.rb', line 23
def outputs; end
|