abstract addition, set carry/overflow flags for addition

This commit is contained in:
Matthew Berry 2020-09-28 23:33:38 -07:00
parent 21ae86e2ff
commit cf310896b1
6 changed files with 19 additions and 6 deletions

View file

@ -16,14 +16,14 @@ module ARM
when 0x1 then res = @r[rd] = @r[rn] ^ operand_2
when 0x2 then res = @r[rd] = sub(@r[rn], operand_2, set_conditions)
when 0x3 then res = @r[rd] = sub(operand_2, @r[rn], set_conditions)
when 0x4 then res = @r[rd] = @r[rn] &+ operand_2
when 0x4 then res = @r[rd] = add(@r[rn], operand_2, set_conditions)
when 0x5 then res = @r[rd] = @r[rn] &+ operand_2 &+ @cpsr.carry.to_unsafe
when 0x6 then res = @r[rd] = @r[rn] &- operand_2 &+ @cpsr.carry.to_unsafe &- 1
when 0x7 then res = @r[rd] = operand_2 &- @r[rn] &+ @cpsr.carry.to_unsafe &- 1
when 0x8 then res = @r[rn] & operand_2
when 0x9 then res = @r[rn] ^ operand_2
when 0xA then res = sub(@r[rn], operand_2, set_conditions)
when 0xB then res = @r[rn] &+ operand_2
when 0xB then res = add(@r[rn], operand_2, set_conditions)
when 0xC then res = @r[rd] = @r[rn] | operand_2
when 0xD then res = @r[rd] = operand_2
when 0xE then res = @r[rd] = @r[rn] & ~operand_2

View file

@ -123,6 +123,19 @@ class CPU
res
end
# Add two values
def add(operand_1 : Word, operand_2 : Word, set_conditions) : Word
log "add - operand_1:#{hex_str operand_1}, operand_2:#{hex_str operand_2}"
res = operand_1 &+ operand_2
if set_conditions
@cpsr.overflow = bit?(~(operand_1 ^ operand_2) & (operand_2 ^ res), 31)
@cpsr.carry = res < operand_1
@cpsr.zero = res == 0
@cpsr.negative = bit?(res, 31)
end
res
end
def print_state(instr : Word) : Nil
{% if flag? :trace %}
@r.each do |reg|

View file

@ -13,7 +13,7 @@ module THUMB
if sub
@r[rd] = sub(@r[rs], operand, true)
else
@r[rd] = @r[rs] &+ operand
@r[rd] = add(@r[rs], operand, true)
end
# todo handle carry flag on all ops
@cpsr.zero = @r[rd] == 0

View file

@ -16,7 +16,7 @@ module THUMB
when 0b1000 then res = @r[rd] & @r[rs]
when 0b1001 then res = @r[rd] = (-@r[rs].to_i32!).to_u32!
when 0b1010 then res = sub(@r[rd], @r[rs], true)
when 0b1011 then res = @r[rd] &+ @r[rs]
when 0b1011 then res = add(@r[rd], @r[rs], true)
when 0b1100 then res = @r[rd] = @r[rd] | @r[rs]
when 0b1101 then res = @r[rd] = @r[rs] * @r[rd]
when 0b1110 then res = @r[rd] = @r[rd] & ~@r[rs]

View file

@ -10,7 +10,7 @@ module THUMB
rs += 8 if h2
case op
when 0b00 then @r[rd] &+= @r[rs]
when 0b00 then add(@r[rd], @r[rs], true)
when 0b01 then sub(@r[rd], @r[rs], true)
when 0b10 then @r[rd] = @r[rs]
when 0b11

View file

@ -7,7 +7,7 @@ module THUMB
case op
when 0b00 then res = @r[rd] = offset
when 0b01 then res = sub(@r[rd], offset, true)
when 0b10 then res = @r[rd] &+= offset
when 0b10 then res = add(@r[rd], offset, true)
when 0b11 then res = @r[rd] = sub(@r[rd], offset, true)
else raise "Invalid move/compare/add/subtract op: #{op}"
end