Skip to content

Commit

Permalink
support negative offsets to PlusRegister
Browse files Browse the repository at this point in the history
Also sign extend in PlusRegister#to_s.
  • Loading branch information
rlane committed Apr 12, 2012
1 parent 21dab79 commit 14bf423
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/rcpu.rb
Expand Up @@ -190,6 +190,10 @@ def +(n)
PlusRegister.new(self, n)
end

def -(n)
self + -n
end

def execute(emu)
case name
when *REAL
Expand Down Expand Up @@ -227,7 +231,13 @@ def code

class PlusRegister < Struct.new(:register, :value)
def to_s
"[#{register}+#{value}]"
v = if value.is_a? Fixnum
# Sign extend from 16 bits
value | (-value[15] & ~0xFFFF)
else
value
end
"[%s%+d]" % [register, v]
end

def code
Expand Down
6 changes: 5 additions & 1 deletion test/test_emulator.rb
Expand Up @@ -276,6 +276,8 @@ def test_format
block :main do
SET a, 1
ADD [a], 0x1000
ADD [a+0xFFFF], 0x1000
ADD [a-2], 0x1000
SUB [a+1], [0x10]
SET push, o
SET x, pop
Expand All @@ -290,12 +292,14 @@ def test_format
res = [
"SET a, 0x1",
"ADD [a], 0x1000",
"ADD [a-1], 0x1000",
"ADD [a-2], 0x1000",
"SUB [a+1], [0x10]",
"SET push, o",
"SET x, pop",
"SET x, peek",
"SET x, pc",
"JSR 0xC",
"JSR 0x12",
"SUB pc, 0x1"
]

Expand Down

0 comments on commit 14bf423

Please sign in to comment.