From ac7e3bbe7280e68e4f0aeeb8d8e872c379624154 Mon Sep 17 00:00:00 2001 From: Matthew Berry Date: Sun, 27 Sep 2020 23:32:14 -0700 Subject: [PATCH] thumb alu operations --- src/crab/thumb/alu_operations.cr | 28 ++++++++++++++++++++++++++++ src/crab/thumb/thumb.cr | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/crab/thumb/alu_operations.cr diff --git a/src/crab/thumb/alu_operations.cr b/src/crab/thumb/alu_operations.cr new file mode 100644 index 0000000..c191f9b --- /dev/null +++ b/src/crab/thumb/alu_operations.cr @@ -0,0 +1,28 @@ +module THUMB + def thumb_alu_operations(instr : Word) : Nil + op = bits(instr, 6..9) + rs = bits(instr, 3..5) + rd = bits(instr, 0..2) + case op + when 0b0000 then res = @r[rd] = @r[rd] & @r[rs] + when 0b0001 then res = @r[rd] = @r[rd] ^ @r[rs] + when 0b0010 then res = @r[rd] = lsl(@r[rd], @r[rs], true) + when 0b0011 then res = @r[rd] = lsr(@r[rd], @r[rs], true) + when 0b0100 then res = @r[rd] = asr(@r[rd], @r[rs], true) + when 0b0101 then res = @r[rd] = @r[rd] &+ @r[rs] &+ @cpsr.carry.to_unsafe + when 0b0110 then res = @r[rd] = @r[rd] &- @r[rs] &- ~@cpsr.carry.to_unsafe + when 0b0111 then res = @r[rd] = ror(@r[rd], @r[rs], true) + when 0b1000 then res = @r[rd] & @r[rs] + when 0b1001 then res = @r[rd] = (-@r[rs].to_i32!).to_u32! + when 0b1010 then res = @r[rd] &- @r[rs] + when 0b1011 then res = @r[rd] &+ @r[rs] + 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] + when 0b1111 then res = @r[rd] = ~@r[rs] + else raise "Invalid alu op: #{op}" + end + @cpsr.zero = res == 0 + @cpsr.negative = bit?(res, 31) + end +end diff --git a/src/crab/thumb/thumb.cr b/src/crab/thumb/thumb.cr index 60efe5b..6dd4759 100644 --- a/src/crab/thumb/thumb.cr +++ b/src/crab/thumb/thumb.cr @@ -37,7 +37,7 @@ module THUMB elsif idx & 0b11111100 == 0b01000100 # hi register operations / branch exchange elsif idx & 0b11111100 == 0b01000000 - # alu operations + lut[idx] = ->thumb_alu_operations(Word) elsif idx & 0b11100000 == 0b00100000 lut[idx] = ->thumb_move_compare_add_subtract(Word) elsif idx & 0b11111100 == 0b00011000