egg

Multi-architecture emulator.
Log | Files | Refs | README | LICENSE

riscv_test.go (10138B)


      1 package riscv
      2 
      3 import (
      4 	"math"
      5 	"reflect"
      6 	"testing"
      7 
      8 	"github.com/gboncoffee/egg/assembler"
      9 	"github.com/gboncoffee/intergo"
     10 )
     11 
     12 func TestRegister(t *testing.T) {
     13 	var m RiscV
     14 	_ = m.SetRegister(1, 39)
     15 	v, err := m.GetRegister(1)
     16 	if v != 39 || err != nil {
     17 		t.Fatalf("Error getting register 1: %d, %v", v, err)
     18 	}
     19 	_ = m.SetRegister(0, 39)
     20 	v, err = m.GetRegister(0)
     21 	if v != 0 || err != nil {
     22 		t.Fatalf("Error getting register 0: %d, %v", v, err)
     23 	}
     24 	_ = m.SetRegister(31, 42)
     25 	v, err = m.GetRegister(31)
     26 	if v != 42 || err != nil {
     27 		t.Fatalf("Error getting register 31: %d, %v", v, err)
     28 	}
     29 	_, err = m.GetRegister(32)
     30 	if err == nil {
     31 		t.Fatalf("No error getting register 32")
     32 	}
     33 }
     34 
     35 func TestMem(t *testing.T) {
     36 	var m RiscV
     37 
     38 	err := m.SetMemory(0, 69)
     39 	if err != nil {
     40 		t.Fatalf("Error setting addr 0: %v", err)
     41 	}
     42 
     43 	v, err := m.GetMemory(0)
     44 	if err != nil || v != 69 {
     45 		t.Fatalf("Error getting addr 0: %v - %v", err, v)
     46 	}
     47 
     48 	err = m.SetMemory(math.MaxUint32, 42)
     49 	if err != nil {
     50 		t.Fatalf("Error setting addr %v: %v", math.MaxUint32, err)
     51 	}
     52 
     53 	v, err = m.GetMemory(math.MaxUint32)
     54 	if err != nil || v != 42 {
     55 		t.Fatalf("Error getting addr %v: %v - %v", math.MaxUint32, err, v)
     56 	}
     57 
     58 	err = m.SetMemory(math.MaxUint32+1, 39)
     59 	if err == nil {
     60 		t.Fatalf("Did not failed in trying to set more than %v", math.MaxUint32)
     61 	}
     62 
     63 	v, err = m.GetMemory(math.MaxUint32 + 1)
     64 	if err == nil {
     65 		t.Fatalf("Did not failed in trying to set more than %v: %v", math.MaxUint32, v)
     66 	}
     67 }
     68 
     69 func TestChunkedMem(t *testing.T) {
     70 	var m RiscV
     71 	arr := []uint8{69, 42, 39}
     72 
     73 	err := m.SetMemoryChunk(0, arr)
     74 	if err != nil {
     75 		t.Fatalf("Error setting addr 0: %v", err)
     76 	}
     77 
     78 	v, err := m.GetMemoryChunk(0, 3)
     79 	if err != nil || !reflect.DeepEqual(v, arr) {
     80 		t.Fatalf("Error getting addr 0: %v - %v", err, v)
     81 	}
     82 
     83 	err = m.SetMemoryChunk(math.MaxUint32-2, arr)
     84 	if err != nil {
     85 		t.Fatalf("Error setting addr %v: %v", math.MaxUint32, err)
     86 	}
     87 
     88 	v, err = m.GetMemoryChunk(math.MaxUint32-2, 3)
     89 	if err != nil || !reflect.DeepEqual(v, arr) {
     90 		t.Fatalf("Error getting addr %v: %v - %v", math.MaxUint32, err, v)
     91 	}
     92 
     93 	err = m.SetMemoryChunk(math.MaxUint32-1, arr)
     94 	if err == nil {
     95 		t.Fatalf("Did not failed in trying to set more than %v", math.MaxUint32)
     96 	}
     97 
     98 	v, err = m.GetMemoryChunk(math.MaxUint32-1, 3)
     99 	if err == nil {
    100 		t.Fatalf("Did not failed in trying to set more than %v: %v", math.MaxUint32, v)
    101 	}
    102 }
    103 
    104 func TestAssembler(t *testing.T) {
    105 
    106 	// We must setup the interctx so it does not panics with nil pointer
    107 	// dereferencing. Of course let's not bother with the languages.
    108 	assembler.InterCtx = &intergo.InterContext{}
    109 	assembler.InterCtx.Init()
    110 
    111 	var m RiscV
    112 	code, _, err := m.Assemble("test.asm")
    113 	if err != nil {
    114 		t.Fatalf("Assembling failed with '%v'", err)
    115 	}
    116 
    117 	// Build by RARS.
    118 	correctCode := []uint32{
    119 		0x007281b3,
    120 		0x407281b3,
    121 		0x0072c1b3,
    122 		0x0072e1b3,
    123 		0x0072f1b3,
    124 		0x007291b3,
    125 		0x0072d1b3,
    126 		0x4072d1b3,
    127 		0x0072a1b3,
    128 		0x0072b1b3,
    129 		0x02a28193,
    130 		0x02a2c193,
    131 		0x02a2e193,
    132 		0x02a2f193,
    133 		0x00329193,
    134 		0x0032d193,
    135 		0x4032d193,
    136 		0x02a2a193,
    137 		0x02a2b193,
    138 		0x02a28183,
    139 		0x02a29183,
    140 		0x02a2a183,
    141 		0x02a2c183,
    142 		0x02a2d183,
    143 		0x003281a3,
    144 		0x003291a3,
    145 		0x0032a1a3,
    146 		0xfe328ea3,
    147 		0xfe329ea3,
    148 		0xfe32aea3,
    149 		0x06518c63,
    150 		0x06519a63,
    151 		0x0651c863,
    152 		0x0651d663,
    153 		0x0651e463,
    154 		0x0651f263,
    155 		0xf65188e3,
    156 		0xf65196e3,
    157 		0xf651c4e3,
    158 		0xf651d2e3,
    159 		0xf651e0e3,
    160 		0xf451fee3,
    161 		0x048001ef,
    162 		0xf55ff1ef,
    163 		0x003281e7,
    164 		0xffd281e7,
    165 		0x000051b7,
    166 		0x00005197,
    167 		0xffffb1b7,
    168 		0xffffb197,
    169 		0x00000073,
    170 		0x00100073,
    171 		0x027281b3,
    172 		0x027291b3,
    173 		0x0272a1b3,
    174 		0x0272b1b3,
    175 		0x0272c1b3,
    176 		0x0272d1b3,
    177 		0x0272e1b3,
    178 		0x0272f1b3,
    179 	}
    180 
    181 	for i := 0; i < len(correctCode); i++ {
    182 		asmi := uint32(code[i*4])
    183 		asmi = asmi | uint32(code[i*4+1])<<8
    184 		asmi = asmi | uint32(code[i*4+2])<<16
    185 		asmi = asmi | uint32(code[i*4+3])<<24
    186 		if asmi != correctCode[i] {
    187 			t.Logf("Byte 0: %x Byte 1: %x Byte 2: %x Byte 3: %x",
    188 				code[i*4],
    189 				code[i*4+1],
    190 				code[i*4+2],
    191 				code[i*4+3])
    192 			t.Fatalf("Incorrect instruction: %x (expected %x) at address %x", asmi, correctCode[i], i*4)
    193 		}
    194 	}
    195 }
    196 
    197 func (m *RiscV) getByName(n string) uint64 {
    198 	r, _ := m.GetRegisterNumber(n)
    199 	v, _ := m.GetRegister(r)
    200 	return v
    201 }
    202 
    203 func (m *RiscV) assertRegister(t *testing.T, v string, r uint64, s string) {
    204 	a := m.getByName(v)
    205 	if a != r {
    206 		t.Fatalf("Wrong result in '%v': %v", s, a)
    207 	}
    208 }
    209 
    210 func (m *RiscV) ensureBranch(t *testing.T, i string) {
    211 	pc := m.pc
    212 	_, _ = m.NextInstruction()
    213 	if pc == m.pc-4 {
    214 		t.Fatalf("%s didn't branch correctly: %d", i, m.pc)
    215 	}
    216 }
    217 
    218 func (m *RiscV) ensureDontBranch(t *testing.T, i string) {
    219 	pc := m.pc
    220 	_, _ = m.NextInstruction()
    221 	if pc != m.pc-4 {
    222 		t.Fatalf("%s didn't pass correctly: %x", i, m.pc)
    223 	}
    224 }
    225 
    226 func TestInstructions(t *testing.T) {
    227 
    228 	var m RiscV
    229 	code, _, err := m.Assemble("test-instructions.asm")
    230 	if err != nil {
    231 		t.Fatalf("Couldn't assemble: %v", err)
    232 	}
    233 
    234 	err = m.LoadProgram(code)
    235 	if err != nil {
    236 		t.Fatalf("Couldn't load program: %v", err)
    237 	}
    238 
    239 	//
    240 	// Arithmetic immediate.
    241 	//
    242 	_, _ = m.NextInstruction()
    243 	m.assertRegister(t, "t0", 1, "addi t0, zero, 1")
    244 
    245 	_, _ = m.NextInstruction()
    246 	m.assertRegister(t, "t0", 4, "xori t0, t0, 5")
    247 
    248 	_, _ = m.NextInstruction()
    249 	m.assertRegister(t, "t0", 6, "ori t0, t0, 2")
    250 
    251 	_, _ = m.NextInstruction()
    252 	m.assertRegister(t, "t0", 2, "andi t0, t0, 3")
    253 
    254 	_, _ = m.NextInstruction()
    255 	m.assertRegister(t, "t0", 8, "slli t0, t0, 2")
    256 
    257 	_, _ = m.NextInstruction()
    258 	_, _ = m.NextInstruction()
    259 	_, _ = m.NextInstruction()
    260 	_, _ = m.NextInstruction()
    261 	m.assertRegister(t, "t2", 536870914, "srli t2, t1, 2")
    262 
    263 	_, _ = m.NextInstruction()
    264 	m.assertRegister(t, "t2", 3758096386, "srai t2, t1, 2")
    265 
    266 	_, _ = m.NextInstruction()
    267 	m.assertRegister(t, "t3", 1, "slti t3, t2, t0")
    268 
    269 	_, _ = m.NextInstruction()
    270 	m.assertRegister(t, "t3", 0, "sltu t3, t2, t0")
    271 
    272 	//
    273 	// Arithmetic.
    274 	//
    275 	_, _ = m.NextInstruction()
    276 	_, _ = m.NextInstruction()
    277 	_, _ = m.NextInstruction()
    278 	_, _ = m.NextInstruction()
    279 
    280 	_, _ = m.NextInstruction()
    281 	m.assertRegister(t, "t4", 3, "add t4, t0, t1")
    282 
    283 	_, _ = m.NextInstruction()
    284 	m.assertRegister(t, "t4", 1, "sub t4, t2, t1")
    285 
    286 	_, _ = m.NextInstruction()
    287 	m.assertRegister(t, "t4", 6, "xor t4, t3, t2")
    288 
    289 	_, _ = m.NextInstruction()
    290 	m.assertRegister(t, "t4", 3, "or t4, t0, t1")
    291 
    292 	_, _ = m.NextInstruction()
    293 	m.assertRegister(t, "t4", 1, "and t4, t2, t3")
    294 
    295 	_, _ = m.NextInstruction()
    296 	m.assertRegister(t, "t4", 4, "sll t4, t0, t1")
    297 
    298 	_, _ = m.NextInstruction()
    299 	_, _ = m.NextInstruction()
    300 	_, _ = m.NextInstruction()
    301 	_, _ = m.NextInstruction()
    302 	m.assertRegister(t, "t4", 536870914, "srl t4, t5, t1")
    303 
    304 	_, _ = m.NextInstruction()
    305 	m.assertRegister(t, "t4", 3758096386, "sra t4, t5, t1")
    306 
    307 	_, _ = m.NextInstruction()
    308 	m.assertRegister(t, "t4", 1, "slt t4, t5, t2")
    309 
    310 	_, _ = m.NextInstruction()
    311 	m.assertRegister(t, "t4", 0, "sltu t4, t5, t2")
    312 
    313 	//
    314 	// Loads and stores.
    315 	//
    316 	_, _ = m.NextInstruction()
    317 	_, _ = m.NextInstruction()
    318 	_, _ = m.NextInstruction()
    319 	_, _ = m.NextInstruction()
    320 	_, _ = m.NextInstruction()
    321 	_, _ = m.NextInstruction()
    322 	_, _ = m.NextInstruction()
    323 	_, _ = m.NextInstruction()
    324 	_, _ = m.NextInstruction()
    325 	_, _ = m.NextInstruction()
    326 	_, _ = m.NextInstruction()
    327 	_, _ = m.NextInstruction()
    328 	m.assertRegister(t, "t1", 0xaaeeffff, "test value moving")
    329 
    330 	_, _ = m.NextInstruction()
    331 	_, _ = m.NextInstruction()
    332 	m.assertRegister(t, "t2", 0xaaeeffff, "lw t2, t0, 0")
    333 
    334 	_, _ = m.NextInstruction()
    335 	_, _ = m.NextInstruction()
    336 	m.assertRegister(t, "t2", 0xffffffff, "lb t2, t0, 0")
    337 	_, _ = m.NextInstruction()
    338 	m.assertRegister(t, "t2", 0x000000ff, "lbu t2, t0, 0")
    339 
    340 	_, _ = m.NextInstruction()
    341 	_, _ = m.NextInstruction()
    342 	m.assertRegister(t, "t2", 0xffffffff, "lh t2, t0, 0")
    343 	_, _ = m.NextInstruction()
    344 	m.assertRegister(t, "t2", 0x0000ffff, "lhu t2, t0, 0")
    345 
    346 	//
    347 	// Jumps.
    348 	//
    349 	ret := m.pc
    350 	_, _ = m.NextInstruction()
    351 	if m.pc == ret+4 {
    352 		t.Fatalf("jal does not jump correctly")
    353 	}
    354 	_, _ = m.NextInstruction()
    355 	if m.pc != ret+4 {
    356 		t.Fatalf("jalr does not return correctly")
    357 	}
    358 
    359 	//
    360 	// Calls.
    361 	//
    362 	_, _ = m.NextInstruction()
    363 	call, _ := m.NextInstruction()
    364 	if call == nil {
    365 		t.Fatalf("call is nil")
    366 	}
    367 	if call.Number != 2 {
    368 		t.Fatalf("call number is not 2")
    369 	}
    370 	call, _ = m.NextInstruction()
    371 	if call == nil {
    372 		t.Fatalf("break call is nil")
    373 	}
    374 	if call.Number != 1 {
    375 		t.Fatalf("break is not 1")
    376 	}
    377 
    378 	//
    379 	// Branches.
    380 	//
    381 	_, _ = m.NextInstruction()
    382 	_, _ = m.NextInstruction()
    383 
    384 	m.ensureDontBranch(t, "beq")
    385 	m.ensureBranch(t, "beq")
    386 
    387 	m.ensureDontBranch(t, "bne")
    388 	m.ensureBranch(t, "bne")
    389 
    390 	m.ensureDontBranch(t, "blt")
    391 	m.ensureDontBranch(t, "blt")
    392 	m.ensureBranch(t, "blt")
    393 
    394 	m.ensureDontBranch(t, "bge")
    395 	m.ensureBranch(t, "bge")
    396 	m.ensureBranch(t, "bge")
    397 
    398 	m.ensureDontBranch(t, "bltu")
    399 	m.ensureDontBranch(t, "bltu")
    400 	m.ensureBranch(t, "bltu")
    401 
    402 	m.ensureDontBranch(t, "bgeu")
    403 	m.ensureBranch(t, "bgeu")
    404 	m.ensureBranch(t, "bgeu")
    405 
    406 	//
    407 	// auipc/lui
    408 	//
    409 	_, _ = m.NextInstruction()
    410 	m.assertRegister(t, "t0", 0xaaaaa000, "lui t0, 0xaaaaa")
    411 	pc := m.pc
    412 	_, _ = m.NextInstruction()
    413 	m.assertRegister(t, "t0", uint64(0xaaaaa000+pc), "auipc t0, 0xaaaaa")
    414 
    415 	//
    416 	// Multiplication extension.
    417 	//
    418 	_, _ = m.NextInstruction()
    419 	_, _ = m.NextInstruction()
    420 
    421 	_, _ = m.NextInstruction()
    422 	m.assertRegister(t, "t2", uint64(0x1fffd000), "mul t2, t0, t1")
    423 	_, _ = m.NextInstruction()
    424 	m.assertRegister(t, "t2", uint64(0x1), "mulh t2, t0, t1")
    425 	_, _ = m.NextInstruction()
    426 	m.assertRegister(t, "t2", uint64(0x1ffffaaa), "div t2, t0, t1")
    427 
    428 	_, _ = m.NextInstruction()
    429 
    430 	_, _ = m.NextInstruction()
    431 	m.assertRegister(t, "t2", uint64(0xE0003000), "mul t2, t0, t1 (neg)")
    432 	_, _ = m.NextInstruction()
    433 	m.assertRegister(t, "t2", uint64(0xFFFFFFFE), "mulh t2, t0, t1 (neg)")
    434 	_, _ = m.NextInstruction()
    435 	// -536869545 but without the 4 top bytes.
    436 	m.assertRegister(t, "t2", uint64(3758097750), "div t2, t0, t1 (neg)")
    437 
    438 	_, _ = m.NextInstruction()
    439 	_, _ = m.NextInstruction()
    440 
    441 	_, _ = m.NextInstruction()
    442 	m.assertRegister(t, "t2", uint64(4294965248), "mulsu t2, t0, t1")
    443 	_, _ = m.NextInstruction()
    444 	m.assertRegister(t, "t2", uint64(0x7FFFF800), "mulu t2, t0, t1")
    445 	_, _ = m.NextInstruction()
    446 	m.assertRegister(t, "t2", uint64(0x7FFFF000), "remu t2, t0, t1")
    447 }