2014-04-14 20:53:29 +02:00
|
|
|
require_relative 'helper'
|
2014-04-14 14:58:59 +02:00
|
|
|
|
2014-04-14 20:53:29 +02:00
|
|
|
# try to test that the generation of basic instructions works
|
2014-04-17 13:43:52 +02:00
|
|
|
# one instruction at a time, reverse testing from objdump --demangle -Sfghxp
|
|
|
|
# tests are named as per assembler code, ie test_mov testing mov instruction
|
2014-05-14 09:47:30 +02:00
|
|
|
# adc add and bic eor orr rsb rsc sbc sub mov mvn cmn cmp teq tst b call bx swi strb
|
2014-04-17 13:43:52 +02:00
|
|
|
|
|
|
|
class TestArmAsm < MiniTest::Test
|
2014-05-02 07:14:38 +02:00
|
|
|
# need Assembler and a block (see those classes)
|
2014-04-14 20:53:29 +02:00
|
|
|
def setup
|
2014-05-07 13:58:38 +02:00
|
|
|
@machine = Arm::ArmMachine.new
|
2014-04-14 20:53:29 +02:00
|
|
|
end
|
|
|
|
|
2014-04-17 13:43:52 +02:00
|
|
|
# code is what the generator spits out, at least one instruction worth (.first)
|
|
|
|
# the op code is wat was witten as assembler in the first place and the binary result
|
|
|
|
# is reversed and in 4 bytes as ruby can only do 31 bits and so we can't test with just one int (?)
|
2014-04-25 17:37:19 +02:00
|
|
|
def assert_code code , op , should
|
2014-04-17 13:43:52 +02:00
|
|
|
assert_equal op , code.opcode
|
2014-05-07 13:58:38 +02:00
|
|
|
io = StringIO.new
|
|
|
|
code.assemble(io)
|
|
|
|
binary = io.string
|
2014-05-14 09:47:30 +02:00
|
|
|
assert_equal 4 , binary.length , "code length wrong for #{code.inspect}"
|
2014-04-14 20:53:29 +02:00
|
|
|
index = 0
|
|
|
|
binary.each_byte do |byte |
|
2014-04-17 19:24:37 +02:00
|
|
|
assert_equal should[index] , byte , "byte #{index} 0x#{should[index].to_s(16)} != 0x#{byte.to_s(16)}"
|
2014-04-17 11:54:51 +02:00
|
|
|
index += 1
|
|
|
|
end
|
2014-04-17 14:35:55 +02:00
|
|
|
end
|
|
|
|
def test_adc
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.adc :r1, :right => :r3, :extra => :r5
|
2014-04-18 14:49:23 +02:00
|
|
|
assert_code code , :adc , [0x05,0x10,0xa3,0xe0] #e0 a3 10 05
|
2014-04-17 11:54:51 +02:00
|
|
|
end
|
2014-04-17 19:24:37 +02:00
|
|
|
def test_add
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.add :r1 , :right => :r1, :extra => :r3
|
2014-04-18 14:49:23 +02:00
|
|
|
assert_code code , :add , [0x03,0x10,0x81,0xe0] #e0 81 10 03
|
2014-04-17 19:24:37 +02:00
|
|
|
end
|
|
|
|
def test_and # inst eval doesn't really work with and
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.and( :r1 , :right => :r2 , :extra => :r3)
|
2014-04-18 14:49:23 +02:00
|
|
|
assert_code code , :and , [0x03,0x10,0x02,0xe0] #e0 01 10 03
|
2014-04-17 19:24:37 +02:00
|
|
|
end
|
2014-04-19 18:34:04 +02:00
|
|
|
def test_b
|
|
|
|
# the address is what an assembler calculates (a signed number for the amount of instructions),
|
|
|
|
# ie the relative (to pc) address -8 (pipeline) /4 so save space
|
|
|
|
# so the cpu adds the value*4 and starts loading that (load, decode, execute)
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.b -4 , {} #this jumps to the next instruction
|
2014-04-19 18:34:04 +02:00
|
|
|
assert_code code , :b , [0xff,0xff,0xff,0xea] #ea ff ff fe
|
|
|
|
end
|
2014-05-14 09:47:30 +02:00
|
|
|
def test_call #see comment above. bx not implemented (as it means into thumb, and no thumb here)
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.call -4 ,{} #this jumps to the next instruction
|
2014-05-14 09:47:30 +02:00
|
|
|
assert_code code , :call, [0xff,0xff,0xff,0xeb] #ea ff ff fe
|
2014-04-19 18:34:04 +02:00
|
|
|
end
|
2014-04-17 19:24:37 +02:00
|
|
|
def test_bic
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.bic :r2 , :right => :r2 , :extra => :r3
|
2014-04-18 14:49:23 +02:00
|
|
|
assert_code code , :bic , [0x03,0x20,0xc2,0xe1] #e3 c2 20 44
|
|
|
|
end
|
|
|
|
def test_cmn
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.cmn :r1 , :right => :r2
|
2014-04-18 14:49:23 +02:00
|
|
|
assert_code code , :cmn , [0x02,0x00,0x71,0xe1] #e1 71 00 02
|
|
|
|
end
|
|
|
|
def test_cmp
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.cmp :r1 , :right => :r2
|
2014-04-18 14:49:23 +02:00
|
|
|
assert_code code , :cmp , [0x02,0x00,0x51,0xe1] #e1 51 00 02
|
|
|
|
end
|
|
|
|
def test_eor
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.eor :r2 , :right => :r2 , :extra => :r3
|
2014-04-18 14:49:23 +02:00
|
|
|
assert_code code , :eor , [0x03,0x20,0x22,0xe0] #e0 22 20 03
|
|
|
|
end
|
2014-04-19 18:38:13 +02:00
|
|
|
def test_ldr
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.ldr :r0, :right => :r0
|
2014-04-19 18:38:13 +02:00
|
|
|
assert_code code, :ldr , [0x00,0x00,0x90,0xe5] #e5 90 00 00
|
|
|
|
end
|
2014-04-22 10:58:17 +02:00
|
|
|
def test_ldr2
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.ldr :r0, :right => :r0 , :offset => 4
|
2014-04-22 11:23:55 +02:00
|
|
|
assert_code code, :ldr , [0x04,0x00,0x90,0xe5] #e5 90 00 04
|
2014-04-22 10:58:17 +02:00
|
|
|
end
|
2014-04-19 18:38:13 +02:00
|
|
|
def test_ldrb
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.ldrb :r0, :right => :r0
|
2014-04-19 18:38:13 +02:00
|
|
|
assert_code code, :ldrb , [0x00,0x00,0xd0,0xe5] #e5 d0 00 00
|
|
|
|
end
|
2014-04-18 14:49:23 +02:00
|
|
|
def test_orr
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.orr :r2 , :right => :r2 , :extra => :r3
|
2014-04-18 14:49:23 +02:00
|
|
|
assert_code code , :orr , [0x03,0x20,0x82,0xe1] #e1 82 20 03
|
|
|
|
end
|
2014-04-20 22:48:04 +02:00
|
|
|
def test_push
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.push [:lr] , {}
|
2014-04-20 22:48:04 +02:00
|
|
|
assert_code code , :push , [0x00,0x40,0x2d,0xe9] #e9 2d 40 00
|
|
|
|
end
|
|
|
|
def test_pop
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.pop [:pc] , {}
|
2014-04-20 22:48:04 +02:00
|
|
|
assert_code code , :pop , [0x00,0x80,0xbd,0xe8] #e8 bd 80 00
|
|
|
|
end
|
2014-04-18 14:49:23 +02:00
|
|
|
def test_rsb
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.rsb :r1 , :right => :r2 , :extra => :r3
|
2014-04-18 14:49:23 +02:00
|
|
|
assert_code code , :rsb , [0x03,0x10,0x62,0xe0]#e0 62 10 03
|
|
|
|
end
|
|
|
|
def test_rsc
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.rsc :r2 , :right => :r3 , :extra => :r4
|
2014-04-18 14:49:23 +02:00
|
|
|
assert_code code , :rsc , [0x04,0x20,0xe3,0xe0]#e0 e3 20 04
|
|
|
|
end
|
|
|
|
def test_sbc
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.sbc :r3, :right => :r4 , :extra => :r5
|
2014-04-18 14:49:23 +02:00
|
|
|
assert_code code , :sbc , [0x05,0x30,0xc4,0xe0]#e0 c4 30 05
|
|
|
|
end
|
2014-04-19 18:34:04 +02:00
|
|
|
def test_str
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.str :r0, :right => :r0
|
2014-04-19 18:34:04 +02:00
|
|
|
assert_code code, :str , [0x00,0x00,0x80,0xe5] #e5 81 00 00
|
|
|
|
end
|
2014-04-19 18:38:13 +02:00
|
|
|
def test_strb
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.strb :r0, :right => :r0
|
2014-04-19 18:38:13 +02:00
|
|
|
assert_code code, :strb , [0x00,0x00,0xc0,0xe5] #e5 c0 00 00
|
|
|
|
end
|
2014-04-18 14:49:23 +02:00
|
|
|
def test_sub
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.sub :r2, :right => :r0, :extra => 1
|
2014-04-19 18:34:04 +02:00
|
|
|
assert_code code, :sub , [0x01,0x20,0x40,0xe2] #e2 40 20 01
|
2014-04-18 14:49:23 +02:00
|
|
|
end
|
|
|
|
def test_swi
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.swi 0x05 , {}
|
2014-04-18 14:49:23 +02:00
|
|
|
assert_code code , :swi , [0x05,0x00,0x00,0xef]#ef 00 00 05
|
2014-04-17 19:24:37 +02:00
|
|
|
end
|
2014-04-18 15:04:14 +02:00
|
|
|
def test_teq
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.teq :r1 , :right => :r2
|
2014-04-18 15:04:14 +02:00
|
|
|
assert_code code , :teq , [0x02,0x00,0x31,0xe1] #e1 31 00 02
|
|
|
|
end
|
2014-04-19 18:34:04 +02:00
|
|
|
def test_tst
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.tst :r1 , :right => :r2
|
2014-04-19 18:34:04 +02:00
|
|
|
assert_code code , :tst , [0x02,0x00,0x11,0xe1] #e1 11 00 02
|
|
|
|
end
|
2014-04-17 13:43:52 +02:00
|
|
|
def test_mov
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.mov :r0, :right => 5
|
2014-04-18 14:49:23 +02:00
|
|
|
assert_code code , :mov , [0x05,0x00,0xa0,0xe3] #e3 a0 10 05
|
2014-04-17 13:43:52 +02:00
|
|
|
end
|
2014-04-18 14:49:23 +02:00
|
|
|
def test_mvn
|
2014-05-15 18:41:51 +02:00
|
|
|
code = @machine.mvn :r1, :right => 5
|
2014-04-18 14:49:23 +02:00
|
|
|
assert_code code , :mvn , [0x05,0x10,0xe0,0xe3] #e3 e0 10 05
|
2014-04-14 20:53:29 +02:00
|
|
|
end
|
2014-04-14 14:58:59 +02:00
|
|
|
end
|