egg

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 82caf3cb9d087bb9eef106a9f9be180811eda65c
parent c16bc3d249e256c32b58ce411338d59835a90f78
Author: Gabriel G. de Brito <gabrielgbrito@icloud.com>
Date:   Fri, 19 Apr 2024 12:30:40 -0300

Added Sagui tests

Diffstat:
Msagui/sagui.go | 5+++--
Asagui/sagui_test.go | 109+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asagui/test.asm | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 179 insertions(+), 2 deletions(-)

diff --git a/sagui/sagui.go b/sagui/sagui.go @@ -107,8 +107,8 @@ func (m *Sagui) NextInstruction() (*machine.Call, error) { op := instr >> 4 imm := instr & 0xf - ra := instr & 0x3 - rb := (instr >> 2) & 0x3 + rb := instr & 0x3 + ra := (instr >> 2) & 0x3 rav, _ := m.GetRegister(uint64(ra)) rbv, _ := m.GetRegister(uint64(rb)) @@ -133,6 +133,7 @@ func (m *Sagui) NextInstruction() (*machine.Call, error) { case 0x5: m.SetMemory(rbv, uint8(rav)) case 0x6: + //fmt.Printf("%b (op %b) (ra %b) (rb %b)\n", instr, op, ra, rb) m.SetRegister(uint64(ra), rbv) case 0x7: m.SetRegister(0, uint64(imm<<4)|(r0v&0xf)) diff --git a/sagui/sagui_test.go b/sagui/sagui_test.go @@ -0,0 +1,109 @@ +package sagui + +import ( + _ "embed" + "testing" +) + +//go:embed test.asm +var asm string + +func TestSagui(t *testing.T) { + var m Sagui + code, _, err := m.Assemble(asm) + if err != nil { + t.Fatalf("Couldn't assemble: %v", err) + } + + err = m.LoadProgram(code) + if err != nil { + t.Fatalf("Couldn't load program: %v", err) + } + + assertRegister := func(reg uint64, v uint64, name string) { + r, _ := m.GetRegister(reg) + if r != v { + t.Fatalf("Testing %s failed: register %v expected to be %v, is %v", name, reg, v, r) + } + } + + assertBranch := func(name string) { + pc := m.GetCurrentInstructionAddress() + m.NextInstruction() + if m.GetCurrentInstructionAddress() == pc+1 { + t.Fatalf("Didn't branched on %v", name) + } + } + + assertDontBranch := func(name string) { + pc := m.GetCurrentInstructionAddress() + m.NextInstruction() + if m.GetCurrentInstructionAddress() != pc+1 { + t.Fatalf("Branched on %v", name) + } + } + + m.NextInstruction() + assertRegister(0, 5, "first movl to branch") + + m.NextInstruction() + assertRegister(1, 5, "first movr to branch") + m.NextInstruction() + assertRegister(0, 0, "zeroing r0 to branch") + + assertBranch("brzr") + + m.NextInstruction() + assertBranch("jr") + + m.NextInstruction() + assertBranch("brzi") + + m.NextInstruction() + assertDontBranch("brzi") + assertDontBranch("brzr") + assertBranch("ji") + + m.NextInstruction() + m.NextInstruction() + assertRegister(0, 0x1, "movl to arithmetic") + m.NextInstruction() + assertRegister(1, 0x1, "movr to arithmetic") + m.NextInstruction() + m.NextInstruction() + assertRegister(0, 0xf0, "movh to arithmetic") + + m.NextInstruction() + assertRegister(0, 0xf1, "add") + m.NextInstruction() + assertRegister(0, 0xf0, "sub") + + m.NextInstruction() + assertRegister(0, 0xf1, "or") + m.NextInstruction() + assertRegister(0, 0x1, "and") + + m.NextInstruction() + assertRegister(0, 0, "first not") + m.NextInstruction() + assertRegister(0, 1, "second not") + + m.NextInstruction() + m.NextInstruction() + assertRegister(0, 0x2, "slr") + m.NextInstruction() + assertRegister(0, 0x1, "srr") + + m.NextInstruction() + m.NextInstruction() + m.NextInstruction() + m.NextInstruction() + mem, _ := m.GetMemory(0xfa) + if mem != 0xa { + t.Fatalf("st failed") + } + m.NextInstruction() + assertRegister(1, 0, "zeroing r1 to be sure") + m.NextInstruction() + assertRegister(1, 0xa, "ld") +} diff --git a/sagui/test.asm b/sagui/test.asm @@ -0,0 +1,67 @@ +; Instructions test for EGG Sagui. + + ; movl, prepare for jumps. + movl 5 + + ; Copy addr. + movr r1, r0 + ; Zero r0 to test branching. + sub r0, r0 + + ; The movr should not be performed. + brzr r0, r1 + movr r0, r0 + + movl 8 + jr r0 + ; Again, movr shouldn't be performed. + movr r0, r0 + + ; Again, movr shouldn't be performed. + sub r0, r0 + brzi 2 + movr r0, r0 + ; Now the branches shouldn't be performed. + movl 1 + brzi 3 + brzr r0, r0 + ; Lastly, test ji. + ji 2 + movr r0, r0 + + ; Arithmetic instructions. + sub r0, r0 + movl 0x1 + movr r1, r0 + sub r0, r0 + movh 0xf + + ; r0 shall be 0xf1. + add r0, r1 + ; r0 shall be 0xf0. + sub r0, r1 + + ; r0 shall be 0xf1. + or r0, r1 + ; r0 shall be 0x01. + and r0, r1 + + ; r0 shall be 0x0. + not r0, r0 + ; And 0x1 again. + not r0, r0 + + movr r1, r0 + ; r0 shall be 0x2. + slr r0, r1 + ; Now 0x1 again. + srr r0, r1 + + ; Use a convenient address and value to test load and store. + movl 0xa + movr r1, r0 + movh 0xf + st r1, r0 + ; Zero r1 to be sure. + sub r1, r1 + ld r1, r0