egg

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

reduxK_test.go (4311B)


      1 package reduxK
      2 
      3 import (
      4 	"testing"
      5 
      6 	"github.com/gboncoffee/egg/assembler"
      7 	"github.com/gboncoffee/egg/machine"
      8 )
      9 
     10 func TestReduxK(t *testing.T) {
     11 	machine.InterCtx.Init()
     12 	_ = machine.InterCtx.AutoSetPreferedLocale()
     13 	assembler.InterCtx = &machine.InterCtx
     14 
     15 	m := ReduxK()
     16 	code, _, err := m.Assemble("test.asm")
     17 	if err != nil {
     18 		t.Fatalf("Couldn't assemble: %v", err)
     19 	}
     20 
     21 	err = m.LoadProgram(code)
     22 	if err != nil {
     23 		t.Fatalf("Couldn't load program: %v", err)
     24 	}
     25 
     26 	assertRegister := func(reg uint64, v uint64, name string) {
     27 		r, _ := m.GetRegister(reg)
     28 		if r != v {
     29 			t.Fatalf("Testing %s failed: register %v expected to be %v, is %v", name, reg, v, r)
     30 		}
     31 	}
     32 
     33 	assertBranch := func(name string) {
     34 		pc := m.GetCurrentInstructionAddress()
     35 		_, _ = m.NextInstruction()
     36 		if m.GetCurrentInstructionAddress() == pc+1 {
     37 			t.Fatalf("Didn't branched on %v", name)
     38 		}
     39 	}
     40 
     41 	assertDontBranch := func(name string) {
     42 		pc := m.GetCurrentInstructionAddress()
     43 		_, _ = m.NextInstruction()
     44 		if m.GetCurrentInstructionAddress() != pc+1 {
     45 			t.Fatalf("Branched on %v", name)
     46 		}
     47 	}
     48 
     49 	_, _ = m.NextInstruction()
     50 	assertRegister(0, 5, "prepare for jump")
     51 
     52 	_, _ = m.NextInstruction()
     53 	assertRegister(1, 5, "copy addr")
     54 
     55 	_, _ = m.NextInstruction()
     56 	assertRegister(0, 0, "zero r0 for branching")
     57 
     58 	assertBranch("brzr")
     59 
     60 	_, _ = m.NextInstruction()
     61 	assertRegister(0, 1, "addi 1 for branching")
     62 
     63 	assertDontBranch("brzr")
     64 
     65 	assertBranch("ji")
     66 
     67 	_, _ = m.NextInstruction()
     68 	assertRegister(0, 0, "sub test")
     69 
     70 	_, _ = m.NextInstruction()
     71 	assertRegister(0, 1, "not test")
     72 
     73 	_, _ = m.NextInstruction()
     74 	assertRegister(0, 2, "add test")
     75 
     76 	_, _ = m.NextInstruction()
     77 	assertRegister(1, 0, "sub for logical test")
     78 
     79 	_, _ = m.NextInstruction()
     80 	assertRegister(1, 1, "not for logical test")
     81 
     82 	_, _ = m.NextInstruction()
     83 	assertRegister(0, 3, "or test")
     84 
     85 	_, _ = m.NextInstruction()
     86 	assertRegister(0, 1, "and test")
     87 
     88 	_, _ = m.NextInstruction()
     89 	assertRegister(0, 2, "sll test")
     90 
     91 	_, _ = m.NextInstruction()
     92 	assertRegister(0, 3, "xor test")
     93 
     94 	_, _ = m.NextInstruction()
     95 	assertRegister(0, 1, "srr test")
     96 
     97 	_, _ = m.NextInstruction()
     98 	assertRegister(0, 0, "sub r0 for load/store")
     99 
    100 	_, _ = m.NextInstruction()
    101 	assertRegister(1, 0, "sub r1 for load/store")
    102 
    103 	_, _ = m.NextInstruction()
    104 	assertRegister(0, 0xff, "addi address")
    105 
    106 	_, _ = m.NextInstruction()
    107 	assertRegister(1, 0xff, "add r1, r0 address")
    108 
    109 	_, _ = m.NextInstruction()
    110 	assertRegister(0, 0, "zero r0 for value")
    111 
    112 	_, _ = m.NextInstruction()
    113 	assertRegister(0, 0xfe, "addi value")
    114 
    115 	_, _ = m.NextInstruction()
    116 	mem, _ := m.GetMemory(0xff)
    117 	if mem != 0xfe {
    118 		t.Fatalf("st failed")
    119 	}
    120 
    121 	_, _ = m.NextInstruction()
    122 	assertRegister(0, 0, "sub for load test")
    123 
    124 	_, _ = m.NextInstruction()
    125 	assertRegister(0, 0xfe, "ld")
    126 
    127 	call, _ := m.NextInstruction()
    128 	if call == nil || call.Number != machine.SYS_BREAK {
    129 		t.Fatalf("Break failed: %v", call)
    130 	}
    131 
    132 	_, _ = m.NextInstruction()
    133 	_, _ = m.NextInstruction()
    134 	_, _ = m.NextInstruction()
    135 	_, _ = m.NextInstruction()
    136 	_, _ = m.NextInstruction()
    137 	_, _ = m.NextInstruction()
    138 	_, _ = m.NextInstruction()
    139 	_, _ = m.NextInstruction()
    140 	_, _ = m.NextInstruction()
    141 
    142 	call, _ = m.NextInstruction()
    143 	if call == nil || call.Number != 0 || call.Arg1 != 1 || call.Arg2 != 2 {
    144 		t.Fatalf("Call failed: %v", call)
    145 	}
    146 
    147 	_, _ = m.NextInstruction()
    148 	_, _ = m.NextInstruction()
    149 	_, _ = m.NextInstruction()
    150 
    151 	_, _ = m.NextInstruction()
    152 	for x := 0; x < 4; x++ {
    153 		assertRegister(uint64(x), 0x1, "inc on r0")
    154 	}
    155 
    156 	_, _ = m.NextInstruction()
    157 	for x := 1; x < 4; x++ {
    158 		mem, _ := m.GetMemory(uint64(x))
    159 		if mem != uint8(x) {
    160 			t.Fatalf("loadv failed")
    161 		}
    162 	}
    163 
    164 	_, _ = m.NextInstruction()
    165 	assertRegister(2, 0x5, "inc")
    166 
    167 	for x := 4; x < 7; x++ {
    168 		_, _ = m.NextInstruction()
    169 		mem, _ := m.GetMemory(uint64(x))
    170 		if mem != uint8(x+1) {
    171 			t.Fatalf("loadv failed")
    172 		}
    173 	}
    174 
    175 	_, _ = m.NextInstruction()
    176 	_, _ = m.NextInstruction()
    177 	_, _ = m.NextInstruction()
    178 	_, _ = m.NextInstruction()
    179 	_, _ = m.NextInstruction()
    180 	_, _ = m.NextInstruction()
    181 	_, _ = m.NextInstruction()
    182 	_, _ = m.NextInstruction()
    183 
    184 	_, _ = m.NextInstruction()
    185 	mem, _ = m.GetMemory(0x07)
    186 	if mem != 6 {
    187 		t.Fatalf("loadv failed")
    188 	}
    189 	mem, _ = m.GetMemory(0x08)
    190 	if mem != 8 {
    191 		t.Fatalf("loadv failed")
    192 	}
    193 	mem, _ = m.GetMemory(0x09)
    194 	if mem != 10 {
    195 		t.Fatalf("loadv failed")
    196 	}
    197 
    198 }