egg

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

sagui_test.go (2614B)


      1 package sagui
      2 
      3 import (
      4 	"testing"
      5 
      6 	"github.com/gboncoffee/egg/machine"
      7 )
      8 
      9 func TestSagui(t *testing.T) {
     10 	var m Sagui
     11 	code, _, err := m.Assemble("test.asm")
     12 	if err != nil {
     13 		t.Fatalf("Couldn't assemble: %v", err)
     14 	}
     15 
     16 	err = m.LoadProgram(code)
     17 	if err != nil {
     18 		t.Fatalf("Couldn't load program: %v", err)
     19 	}
     20 
     21 	assertRegister := func(reg uint64, v uint64, name string) {
     22 		r, _ := m.GetRegister(reg)
     23 		if r != v {
     24 			t.Fatalf("Testing %s failed: register %v expected to be %v, is %v", name, reg, v, r)
     25 		}
     26 	}
     27 
     28 	assertBranch := func(name string) {
     29 		pc := m.GetCurrentInstructionAddress()
     30 		_, _ = m.NextInstruction()
     31 		if m.GetCurrentInstructionAddress() == pc+1 {
     32 			t.Fatalf("Didn't branched on %v", name)
     33 		}
     34 	}
     35 
     36 	assertDontBranch := func(name string) {
     37 		pc := m.GetCurrentInstructionAddress()
     38 		_, _ = m.NextInstruction()
     39 		if m.GetCurrentInstructionAddress() != pc+1 {
     40 			t.Fatalf("Branched on %v", name)
     41 		}
     42 	}
     43 
     44 	_, _ = m.NextInstruction()
     45 	assertRegister(0, 5, "first movl to branch")
     46 
     47 	_, _ = m.NextInstruction()
     48 	assertRegister(1, 5, "first movr to branch")
     49 	_, _ = m.NextInstruction()
     50 	assertRegister(0, 0, "zeroing r0 to branch")
     51 
     52 	assertBranch("brzr")
     53 
     54 	_, _ = m.NextInstruction()
     55 	assertBranch("jr")
     56 
     57 	_, _ = m.NextInstruction()
     58 	assertBranch("brzi")
     59 
     60 	_, _ = m.NextInstruction()
     61 	assertDontBranch("brzi")
     62 	assertDontBranch("brzr")
     63 	assertBranch("ji")
     64 
     65 	_, _ = m.NextInstruction()
     66 	_, _ = m.NextInstruction()
     67 	assertRegister(0, 0x1, "movl to arithmetic")
     68 	_, _ = m.NextInstruction()
     69 	assertRegister(1, 0x1, "movr to arithmetic")
     70 	_, _ = m.NextInstruction()
     71 	_, _ = m.NextInstruction()
     72 	assertRegister(0, 0xf0, "movh to arithmetic")
     73 
     74 	_, _ = m.NextInstruction()
     75 	assertRegister(0, 0xf1, "add")
     76 	_, _ = m.NextInstruction()
     77 	assertRegister(0, 0xf0, "sub")
     78 
     79 	_, _ = m.NextInstruction()
     80 	assertRegister(0, 0xf1, "or")
     81 	_, _ = m.NextInstruction()
     82 	assertRegister(0, 0x1, "and")
     83 
     84 	_, _ = m.NextInstruction()
     85 	assertRegister(0, 0, "first not")
     86 	_, _ = m.NextInstruction()
     87 	assertRegister(0, 1, "second not")
     88 
     89 	_, _ = m.NextInstruction()
     90 	_, _ = m.NextInstruction()
     91 	assertRegister(0, 0x2, "slr")
     92 	_, _ = m.NextInstruction()
     93 	assertRegister(0, 0x1, "srr")
     94 
     95 	_, _ = m.NextInstruction()
     96 	_, _ = m.NextInstruction()
     97 	_, _ = m.NextInstruction()
     98 	_, _ = m.NextInstruction()
     99 	mem, _ := m.GetMemory(0xfa)
    100 	if mem != 0xa {
    101 		t.Fatalf("st failed")
    102 	}
    103 	_, _ = m.NextInstruction()
    104 	assertRegister(1, 0, "zeroing r1 to be sure")
    105 	_, _ = m.NextInstruction()
    106 	assertRegister(1, 0xa, "ld")
    107 
    108 	call, _ := m.NextInstruction()
    109 	if call == nil || call.Number != machine.SYS_BREAK {
    110 		t.Fatalf("Break failed: %v", call)
    111 	}
    112 }