Skip to content

Commit f56173a

Browse files
committed
Use bitwise AND to mask instruction literal value properly
This can't really be done at the microcode level, because it depends on the word and opcode lengths...
1 parent 1896d46 commit f56173a

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lib/Computer.Build.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,10 @@ def initialize(op, *operands)
198198

199199
def opcode
200200
return {
201-
:complement => "101",
202-
:add => "010",
203-
:subtract => "110"}[@op]
201+
:complement => "011",
202+
:and => "001",
203+
:add => "100",
204+
:subtract => "101"}[@op]
204205
end
205206
end
206207

@@ -312,6 +313,10 @@ def add(operand1, operand2)
312313
Computer::ALUOperation.new(:add, operand1, operand2)
313314
end
314315

316+
def bitwise_and(operand1, operand2)
317+
Computer::ALUOperation.new(:and, operand1, operand2)
318+
end
319+
315320
def subtract(operand1, operand2)
316321
Computer::ALUOperation.new(:subtract, operand1, operand2)
317322
end

lib/McCalla.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
end
2323

2424
computer.instruction "add" do |i|
25-
i.move :MA, :IR
25+
i.move :MA, bitwise_and(:IR, 0x0F)
2626
i.move :A, add(:A, :MD)
2727
end
2828

@@ -42,6 +42,6 @@
4242
end
4343

4444
computer.instruction "jmp" do |i|
45-
i.move :pc, :IR
45+
i.move :pc, bitwise_and(:IR, 0x0F)
4646
end
4747
end

0 commit comments

Comments
 (0)