egg

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

commit 79f4a9d0f5dd8fa2d940979cafa304056a906bf7
parent ec5b50f5895b63e6108ce485fed55047e4cdc1c1
Author: Gabriel G. de Brito (AKA 96) <gabrielgbrito@icloud.com>
Date:   Sun, 20 Oct 2024 19:35:02 -0300

Merge pull request #27 from gboncoffee:mips-testing

Make MIPS stable
Diffstat:
MREADME-pt.md | 2+-
MREADME.md | 3++-
Minternationalization.go | 4++--
Mmain.go | 4++--
Amips/mips-test.asm | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmips/mips.go | 142++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Amips/mips_test.go | 576+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 716 insertions(+), 72 deletions(-)

diff --git a/README-pt.md b/README-pt.md @@ -16,7 +16,7 @@ processador, criado para fins educacionais. O pacote `egg` provém uma interface para interagir com máquinas, assim provendo suporte a diferentes backends provendo arquiteturas. No momento, há backends de -RISC-V IM de 32 bits e MIPS32 (experimental). +RISC-V IM de 32 bits, MIPS32 e uma arquitetura fantasia de 8 bits, "Sagui". O pacote `egg/assembler` provém uma pequena biblioteca para criação de assemblers e o suporte ao debugger do EGG. diff --git a/README.md b/README.md @@ -17,7 +17,8 @@ educational purpouses. The `egg` package itself provides only an interface for interacting with machines, thus supporting different architeture backends. Currently, there are -RISC-V IM 32 bits and a MIPS32 (experimental) backends implemented. +RISC-V IM 32 bits, MIPS32 and a fantasy 8 bit architecture "Sagui" backends +implemented. `egg/assembler` also provides a small library for creating assemblers, and the support for EGG's debugger. diff --git a/internationalization.go b/internationalization.go @@ -69,10 +69,10 @@ var brazilian = map[string]string{ // Infos. `Currently supported architetures: 'riscv' - RISC-V IM, 32 bits -'mips' - Subset of MIPS32 (experimental) +'mips' - Subset of MIPS32 'sagui' - Fantasy 8 bit RISC`: `Arquiteturas suportadas: 'riscv' - RISC-V IM, 32 bits -'mips' - Subconjunto de MIPS32 (experimental) +'mips' - Subconjunto de MIPS32 'sagui' - RISC fantasia de 8 bits`, "EGG - Emulador Genérico do Gabriel - version ": "EGG - Emulador Genérico do Gabriel - versão ", // Main execution loop. diff --git a/main.go b/main.go @@ -15,13 +15,13 @@ import ( "github.com/gboncoffee/egg/sagui" ) -const VERSION = "3.0.1" +const VERSION = "3.1.0" // Put new architetures here... (main.go:/switch architeture) func listArchs() { fmt.Println(machine.InterCtx.Get(`Currently supported architetures: 'riscv' - RISC-V IM, 32 bits -'mips' - Subset of MIPS32 (experimental) +'mips' - Subset of MIPS32 'sagui' - Fantasy 8 bit RISC`)) } diff --git a/mips/mips-test.asm b/mips/mips-test.asm @@ -0,0 +1,57 @@ +branches_test: +add t0, t1, t2 +addi t0, t1, 2 +addiu t0, t1, 3 +addu t0, t1, t2 +clo t0, t1 +clz t0, t1 +lui t0, 0xffff +seb t0, t1 +seh t0, t1 +sub t0, t1, t2 +subu t0, t1, t2 +sll t0, t1, 2 +sllv t0, t1, t2 +sra t0, t1, 2 +srav t0, t1, t2 +srl t0, t1, 2 +srlv t0, t1, t2 +and t0, t1, t2 +andi t0, t1, 0xff +nor t0, t1, t2 +or t0, t1, t2 +ori t0, t1, 0xff +xor t0, t1, t2 +xori t0, t1, 0xff +movn t0, t1, t2 +movz t0, t1, t2 +slt t0, t1, t2 +slti t0, t1, 0xff +sltiu t0, t1, 0xff +sltu t0, t1, t2 +div t0, t1 +mult t0, t1 +mfhi t0 +mflo t0 +mthi t0 +mtlo t0 +beq t0, t1, branches_test +bgez t0, branches_test +bgtz t0, branches_test +blez t0, branches_test +bltz t0, branches_test +bne t0, t1, branches_test +break +syscall +j 8 +jal 8 +jalr t0 +jr t0 +lb t0, t1, 0 +lbu t0, t1, 0 +lh t0, t1, 0 +lhu t0, t1, 0 +lw t0, t1, 0 +sb t0, t1, 0 +sh t0, t1, 0 +sw t0, t1, 0 diff --git a/mips/mips.go b/mips/mips.go @@ -73,6 +73,7 @@ type Mips struct { // break DONE ASM // syscall DONE ASM // j DONE ASM +// jal DONE ASM // jalr DONE ASM // jr DONE ASM // lb DONE ASM @@ -87,9 +88,9 @@ type Mips struct { // sw DONE ASM // -func signExtend64(n uint32) uint64 { - sign := n >> 31 - sign64 := uint64(^(sign - 1)) << 32 +func signExtend16(n uint16) uint64 { + sign := n >> 15 + sign64 := uint64(^(sign - 1)) << 16 return uint64(n) | sign64 } @@ -149,7 +150,7 @@ func (m *Mips) execSpecial(rs, rt, rd, shamt, funct uint8) { r = int32(m.pc + 4) fallthrough case 8: - m.pc = uint32(rsv) + m.pc = uint32(rsv) - 4 // add addu // sub subu // slt sltu @@ -192,17 +193,17 @@ func (m *Mips) execSpecial(rs, rt, rd, shamt, funct uint8) { // srl // srlv case 0: - r = rtv << shamt + r = rsv << shamt case 4: - r = rtv << rsv + r = rsv << rtv case 3: - r = rtv >> shamt + r = rsv >> shamt case 7: - r = rtv >> rsv + r = rsv >> rtv case 2: - r = int32(uint32(rtv) >> uint32(shamt)) + r = int32(uint32(rsv) >> uint32(shamt)) case 6: - r = int32(uint32(rtv) >> uint32(rsv)) + r = int32(uint32(rsv) >> uint32(rtv)) // mult // div case 0x18: @@ -274,7 +275,7 @@ func (m *Mips) execSpecial3(rs, rd, shamt, funct uint8) { sign = (^(sign - 1)) << 8 r = int32(rsvb | sign) case 24: - rsvb := rsv & 0xff + rsvb := rsv & 0xffff sign := rsvb >> 15 sign = (^(sign - 1)) << 16 r = int32(rsvb | sign) @@ -286,17 +287,18 @@ func (m *Mips) execSpecial3(rs, rd, shamt, funct uint8) { func (m *Mips) execRegimm(rs, shamt uint8, imm uint32) { rsv64, _ := m.GetRegister(uint64(rs)) + off := int32(signExtend16(uint16(imm)) << 2) switch shamt { // bltz case 0: if int32(rsv64) < 0 { - m.pc = uint32(int32(m.pc) + int32(imm<<2) - 4) + m.pc = uint32(int32(m.pc) + off - 4) } // bgez case 1: if int32(rsv64) >= 0 { - m.pc = uint32(int32(m.pc) + int32(imm<<2) - 4) + m.pc = uint32(int32(m.pc) + off - 4) } } } @@ -304,30 +306,37 @@ func (m *Mips) execRegimm(rs, shamt uint8, imm uint32) { func (m *Mips) executeBeq(rs, rt uint8, imm uint32) { rsv64, _ := m.GetRegister(uint64(rs)) rtv64, _ := m.GetRegister(uint64(rt)) + + off := int32(signExtend16(uint16(imm)) << 2) if rsv64 == rtv64 { - m.pc = uint32(int32(m.pc) + int32(imm<<2) - 4) + m.pc = uint32(int32(m.pc) + off - 4) } } func (m *Mips) executeBne(rs, rt uint8, imm uint32) { rsv64, _ := m.GetRegister(uint64(rs)) rtv64, _ := m.GetRegister(uint64(rt)) + off := int32(signExtend16(uint16(imm)) << 2) if rsv64 != rtv64 { - m.pc = uint32(int32(m.pc) + int32(imm<<2) - 4) + m.pc = uint32(int32(m.pc) + off - 4) } } func (m *Mips) executeBgtz(rs uint8, imm uint32) { rsv64, _ := m.GetRegister(uint64(rs)) + + off := int32(signExtend16(uint16(imm)) << 2) if int32(rsv64) > 0 { - m.pc = uint32(int32(m.pc) + int32(imm<<2) - 4) + m.pc = uint32(int32(m.pc) + off - 4) } } func (m *Mips) executeBlez(rs uint8, imm uint32) { rsv64, _ := m.GetRegister(uint64(rs)) + + off := int32(signExtend16(uint16(imm)) << 2) if int32(rsv64) <= 0 { - m.pc = uint32(int32(m.pc) + int32(imm<<2) - 4) + m.pc = uint32(int32(m.pc) + off - 4) } } @@ -397,9 +406,8 @@ func (m *Mips) executeJal(imm uint32) { } func (m *Mips) executeLb(rs, rt uint8, off uint32) { - offs := int32(off) rsv64, _ := m.GetRegister(uint64(rs)) - mem, _ := m.GetMemory(uint64(int32(rsv64) + offs)) + mem, _ := m.GetMemory(uint64(uint32(rsv64) + off)) memb := mem & 0xff sign := uint64(memb >> 7) @@ -410,18 +418,17 @@ func (m *Mips) executeLb(rs, rt uint8, off uint32) { } func (m *Mips) executeLbu(rs, rt uint8, off uint32) { - offs := int32(off) rsv64, _ := m.GetRegister(uint64(rs)) - mem, _ := m.GetMemory(uint64(int32(rsv64) + offs)) + mem, _ := m.GetMemory(uint64(uint32(rsv64) + off)) m.SetRegister(uint64(rt), uint64(mem)) } func (m *Mips) executeLh(rs, rt uint8, off uint32) { - offs := int32(off) rsv64, _ := m.GetRegister(uint64(rs)) - mem, _ := m.GetMemory(uint64(int32(rsv64) + offs)) - mem2, _ := m.GetMemory(uint64(int32(rsv64) + offs)) + memSlice, _ := m.GetMemoryChunk(uint64(uint32(rsv64)+off), 2) + mem := memSlice[0] + mem2 := memSlice[1] memb := uint64(mem) | (uint64(mem2) << 8) sign := memb >> 15 @@ -432,71 +439,68 @@ func (m *Mips) executeLh(rs, rt uint8, off uint32) { } func (m *Mips) executeLhu(rs, rt uint8, off uint32) { - offs := int32(off) rsv64, _ := m.GetRegister(uint64(rs)) - mem, _ := m.GetMemory(uint64(int32(rsv64) + offs)) - mem2, _ := m.GetMemory(uint64(int32(rsv64)+offs) + 1) + memSlice, _ := m.GetMemoryChunk(uint64(uint32(rsv64)+off), 2) + mem := memSlice[0] + mem2 := memSlice[1] m.SetRegister(uint64(rt), uint64(mem)|(uint64(mem2)<<8)) } func (m *Mips) executeLw(rs, rt uint8, off uint32) { - offs := int32(off) rsv64, _ := m.GetRegister(uint64(rs)) - mem, _ := m.GetMemory(uint64(int32(rsv64) + offs)) - mem2, _ := m.GetMemory(uint64(int32(rsv64)+offs) + 1) - mem3, _ := m.GetMemory(uint64(int32(rsv64)+offs) + 2) - mem4, _ := m.GetMemory(uint64(int32(rsv64)+offs) + 3) + memSlice, _ := m.GetMemoryChunk(uint64(uint32(rsv64)+off), 4) + mem := memSlice[0] + mem2 := memSlice[1] + mem3 := memSlice[2] + mem4 := memSlice[3] m.SetRegister(uint64(rt), uint64(mem)|(uint64(mem2)<<8)|(uint64(mem3)<<16)|(uint64(mem4)<<24)) } func (m *Mips) executeLwl(rs, rt uint8, off uint32) { - offs := int32(off) rsv64, _ := m.GetRegister(uint64(rs)) rtv64, _ := m.GetRegister(uint64(rt)) - mem, _ := m.GetMemory(uint64(int32(rsv64)+offs) + 2) - mem2, _ := m.GetMemory(uint64(int32(rsv64)+offs) + 3) + memSlice, _ := m.GetMemoryChunk(uint64(uint32(rsv64)+off), 2) + mem := memSlice[0] + mem2 := memSlice[1] m.SetRegister(uint64(rt), ((uint64(mem)|(uint64(mem2)<<8))<<16)|(rtv64&0xffff)) } func (m *Mips) executeLwr(rs, rt uint8, off uint32) { - offs := int32(off) rsv64, _ := m.GetRegister(uint64(rs)) rtv64, _ := m.GetRegister(uint64(rt)) - mem, _ := m.GetMemory(uint64(int32(rsv64) + offs)) - mem2, _ := m.GetMemory(uint64(int32(rsv64)+offs) + 1) + memSlice, _ := m.GetMemoryChunk(uint64(uint32(rsv64)+off), 2) + mem := memSlice[0] + mem2 := memSlice[1] m.SetRegister(uint64(rt), (uint64(mem)|(uint64(mem2)<<8))|(rtv64&0xffff0000)) } func (m *Mips) executeSb(rs, rt uint8, off uint32) { - offs := int32(off) rsv64, _ := m.GetRegister(uint64(rs)) rtv64, _ := m.GetRegister(uint64(rt)) - m.SetMemory(uint64(int32(rsv64)+offs), uint8(rtv64&0xff)) + m.SetMemory(uint64(uint32(rsv64)+off), uint8(rtv64&0xff)) } func (m *Mips) executeSh(rs, rt uint8, off uint32) { - offs := int32(off) rsv64, _ := m.GetRegister(uint64(rs)) rtv64, _ := m.GetRegister(uint64(rt)) - m.SetMemory(uint64(int32(rsv64)+offs), uint8(rtv64&0xff)) - m.SetMemory(uint64(int32(rsv64)+offs)+1, uint8((rtv64&0xff00)>>8)) + m.SetMemory(uint64(uint32(rsv64)+off), uint8(rtv64&0xff)) + m.SetMemory(uint64(uint32(rsv64)+off)+1, uint8((rtv64&0xff00)>>8)) } func (m *Mips) executeSw(rs, rt uint8, off uint32) { - offs := int32(off) rsv64, _ := m.GetRegister(uint64(rs)) rtv64, _ := m.GetRegister(uint64(rt)) - m.SetMemory(uint64(int32(rsv64)+offs), uint8(rtv64&0xff)) - m.SetMemory(uint64(int32(rsv64)+offs)+1, uint8((rtv64&0xff00)>>8)) - m.SetMemory(uint64(int32(rsv64)+offs)+2, uint8((rtv64&0xff0000)>>16)) - m.SetMemory(uint64(int32(rsv64)+offs)+3, uint8((rtv64&0xff000000)>>24)) + m.SetMemory(uint64(uint32(rsv64)+off), uint8(rtv64&0xff)) + m.SetMemory(uint64(uint32(rsv64)+off)+1, uint8((rtv64&0xff00)>>8)) + m.SetMemory(uint64(uint32(rsv64)+off)+2, uint8((rtv64&0xff0000)>>16)) + m.SetMemory(uint64(uint32(rsv64)+off)+3, uint8((rtv64&0xff000000)>>24)) } func (m *Mips) execute(i uint32) (*machine.Call, error) { @@ -533,7 +537,7 @@ func (m *Mips) execute(i uint32) (*machine.Call, error) { rs, _, rd, _, funct := parseR(i) m.execSpecial2(rs, rd, funct) case 31: - rs, _, rd, shamt, funct := parseR(i) + _, rs, rd, shamt, funct := parseR(i) m.execSpecial3(rs, rd, shamt, funct) // beq case 4: @@ -879,7 +883,7 @@ func assembleSpecial3(t assembler.ResolvedToken) (uint32, error) { shamt = 24 } - code := uint32(28 << 26) + code := uint32(31 << 26) code = code | (uint32(rt) << 16) code = code | (uint32(rd) << 11) code = code | (uint32(shamt) << 6) @@ -901,7 +905,7 @@ func assembleRegimm(t assembler.ResolvedToken, addr int) (uint32, error) { funct = 1 } - br := uint64(signExtend64(uint32(t.Args[1])) - uint64(addr)) + br := (signExtend16(uint16(t.Args[1])) - uint64(addr)) >> 2 code := uint32(1 << 26) code = code | (uint32(rs) << 21) code = code | (uint32(funct) << 16) @@ -993,7 +997,7 @@ func assembleLui(t assembler.ResolvedToken) (uint32, error) { } rt := t.Args[0] << 16 - op := uint64(12 << 26) + op := uint64(15 << 26) return uint32(op | rt | t.Args[1]), nil } @@ -1005,7 +1009,7 @@ func assembleBeq(t assembler.ResolvedToken, addr int) (uint32, error) { rt := t.Args[1] off := t.Args[2] - br := uint64(signExtend64(uint32(off)) - uint64(addr)) + br := (signExtend16(uint16(off)) - uint64(addr)) >> 2 code := uint32(4 << 26) code = code | (uint32(rs) << 21) code = code | (uint32(rt) << 16) @@ -1015,34 +1019,30 @@ func assembleBeq(t assembler.ResolvedToken, addr int) (uint32, error) { } func assembleBgtz(t assembler.ResolvedToken, addr int) (uint32, error) { - if len(t.Args) != 3 { + if len(t.Args) != 2 { return 0, fmt.Errorf(machine.InterCtx.Get("wrong number of arguments for instruction '%s', expected 2 arguments"), t.Value) } rs := t.Args[0] - rt := t.Args[1] - off := t.Args[2] + off := t.Args[1] - br := uint64(signExtend64(uint32(off)) - uint64(addr)) + br := (signExtend16(uint16(off)) - uint64(addr)) >> 2 code := uint32(7 << 26) code = code | (uint32(rs) << 21) - code = code | (uint32(rt) << 16) code = code | uint32(br&0xffff) return code, nil } func assembleBlez(t assembler.ResolvedToken, addr int) (uint32, error) { - if len(t.Args) != 3 { + if len(t.Args) != 2 { return 0, fmt.Errorf(machine.InterCtx.Get("wrong number of arguments for instruction '%s', expected 2 arguments"), t.Value) } rs := t.Args[0] - rt := t.Args[1] - off := t.Args[2] + off := t.Args[1] - br := uint64(signExtend64(uint32(off)) - uint64(addr)) + br := (signExtend16(uint16(off)) - uint64(addr)) >> 2 code := uint32(6 << 26) code = code | (uint32(rs) << 21) - code = code | (uint32(rt) << 16) code = code | uint32(br&0xffff) return code, nil @@ -1056,7 +1056,7 @@ func assembleBne(t assembler.ResolvedToken, addr int) (uint32, error) { rt := t.Args[1] off := t.Args[2] - br := uint64(signExtend64(uint32(off)) - uint64(addr)) + br := (signExtend16(uint16(off)) - uint64(addr)) >> 2 code := uint32(5 << 26) code = code | (uint32(rs) << 21) code = code | (uint32(rt) << 16) @@ -1070,7 +1070,15 @@ func assembleJ(t assembler.ResolvedToken) (uint32, error) { return 0, fmt.Errorf(machine.InterCtx.Get("wrong number of arguments for instruction '%s', expected 1 argument"), t.Value) } - return uint32(2<<26) | uint32(t.Args[0]&0xfffffff), nil + return uint32(2<<26) | uint32((t.Args[0]>>2)&0xfffffff), nil +} + +func assembleJal(t assembler.ResolvedToken) (uint32, error) { + if len(t.Args) != 1 { + return 0, fmt.Errorf(machine.InterCtx.Get("wrong number of arguments for instruction '%s', expected 1 argument"), t.Value) + } + + return uint32(3<<26) | uint32((t.Args[0]>>2)&0xfffffff), nil } func assembleLb(t assembler.ResolvedToken) (uint32, error) { @@ -1190,7 +1198,7 @@ func assembleSw(t assembler.ResolvedToken) (uint32, error) { code = code | uint32(t.Args[0]<<16) code = code | uint32(t.Args[1]<<21) - return code | (0x2a << 26), nil + return code | (0x2b << 26), nil } func assembleInstruction(code []uint8, addr int, t assembler.ResolvedToken) error { @@ -1236,6 +1244,8 @@ func assembleInstruction(code []uint8, addr int, t assembler.ResolvedToken) erro bin = 12 case "j": bin, err = assembleJ(t) + case "jal": + bin, err = assembleJal(t) case "lb": bin, err = assembleLb(t) case "lbu": diff --git a/mips/mips_test.go b/mips/mips_test.go @@ -0,0 +1,576 @@ +package mips + +import ( + "reflect" + "testing" + + "github.com/gboncoffee/egg/machine" + "github.com/gboncoffee/egg/assembler" +) + +func TestIsa(t *testing.T) { + var m Mips + + // Assembling will fail if we don't initialize the InterCtx. + machine.InterCtx.Init() + machine.InterCtx.AutoSetPreferedLocale() + assembler.InterCtx = &machine.InterCtx + + code, _, err := m.Assemble("mips-test.asm") + if err != nil { + t.Fatalf("Could not assemble file: %v", err) + } + + m.LoadProgram(code) + + // add t0, t1, t2 + m.SetRegister(1+8, 3) + m.SetRegister(2+8, 2) + m.NextInstruction() + v, _ := m.GetRegister(0 + 8) + if v != 5 { + t.Fatalf("add failed: %v", v) + } + + // addi t0, t1, 2 + m.SetRegister(1+8, 3) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 5 { + t.Fatalf("addi failed: %v", v) + } + + // addiu t0, t1, 3 + m.SetRegister(1+8, 0xf0_00_00_00) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xf0000003 { + t.Fatalf("addiu failed: %v", v) + } + + // addu t0, t1, t2 + m.SetRegister(1+8, 3) + m.SetRegister(2+8, 0xf0_00_00_00) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xf0000003 { + t.Fatalf("addu failed: %v", v) + } + + // clo t0, t1 + m.SetRegister(1+8, 0xf0_00_00_00) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 4 { + t.Fatalf("clo failed: %v", v) + } + + // clz t0, t1 + m.SetRegister(1+8, 3) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 30 { + t.Fatalf("clz failed: %v", v) + } + + // lui t0, 0xffff + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xffff0000 { + t.Fatalf("lui failed: %v", v) + } + + // seb t0, t1 + m.SetRegister(1+8, 0xff) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xffffffff { + t.Fatalf("seb failed: %v", v) + } + + // seh t0, t1 + m.SetRegister(1+8, 0xff_ff) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xffffffff { + t.Fatalf("seh failed: %v", v) + } + + // sub t0, t1, t2 + m.SetRegister(1+8, 3) + m.SetRegister(2+8, 2) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 1 { + t.Fatalf("sub failed: %v", v) + } + + // subu t0, t1, t2 + m.SetRegister(1+8, 0x80_00_00_03) + m.SetRegister(2+8, 2) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0x80_00_00_01 { + t.Fatalf("subu failed: %v", v) + } + + // sll t0, t1, 2 + m.SetRegister(1+8, 1) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 4 { + t.Fatalf("sll failed: %v", v) + } + + // sllv t0, t1, t2 + m.SetRegister(1+8, 1) + m.SetRegister(2+8, 2) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 4 { + t.Fatalf("sllv failed: %v", v) + } + + // sra t0, t1, 2 + m.SetRegister(1+8, 0x80_00_00_00) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xe0_00_00_00 { + t.Fatalf("sra failed: %v", v) + } + + // srav t0, t1, t2 + m.SetRegister(1+8, 0x80_00_00_00) + m.SetRegister(2+8, 2) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xe0_00_00_00 { + t.Fatalf("srav failed: %v", v) + } + + // srl t0, t1, 2 + m.SetRegister(1+8, 0x80_00_00_00) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0x20_00_00_00 { + t.Fatalf("srl failed: %v", v) + } + + // srlv t0, t1, t2 + m.SetRegister(1+8, 0x80_00_00_00) + m.SetRegister(2+8, 2) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0x20_00_00_00 { + t.Fatalf("srlv failed: %v", v) + } + + // and t0, t1, t2 + m.SetRegister(1+8, 0xffff) + m.SetRegister(2+8, 0xff) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xff { + t.Fatalf("and failed: %v", v) + } + + // andi t0, t1 0xff + m.SetRegister(1+8, 0xffff) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xff { + t.Fatalf("andi failed: %v", v) + } + + // nor t0, t1, t2 + m.SetRegister(1+8, 0xff_ff_00_00) + m.SetRegister(2+8, 0x00_ff_ff_00) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0x00_00_00_ff { + t.Fatalf("nor failed: %v", v) + } + + // or t0, t1, t2 + m.SetRegister(1+8, 0xff_ff_00_00) + m.SetRegister(2+8, 0x00_ff_ff_00) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xff_ff_ff_00 { + t.Fatalf("or failed: %v", v) + } + + // ori t0, t1, 0xff + m.SetRegister(1+8, 0xff_00) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xff_ff { + t.Fatalf("ori failed: %v", v) + } + + // xor t0, t1, t2 + m.SetRegister(1+8, 0xff_ff_00_00) + m.SetRegister(2+8, 0x00_ff_ff_00) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xff_00_ff_00 { + t.Fatalf("xor failed: %v", v) + } + + // xori t0, t1, 0xff + m.SetRegister(1+8, 0xff_01) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xff_fe { + t.Fatalf("xori failed: %v", v) + } + + // movn t0, t1, t2 + m.SetRegister(0+8, 0) + m.SetRegister(1+8, 0xff) + m.SetRegister(2+8, 0) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0 { + t.Fatalf("movn (not) failed: %v", v) + } + m.pc -= 4 + m.SetRegister(2+8, 1) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xff { + t.Fatalf("movn (yes) failed: %v", v) + } + + // movz t0, t1, t2 + m.SetRegister(0+8, 0) + m.SetRegister(1+8, 0xff) + m.SetRegister(2+8, 1) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0 { + t.Fatalf("movz (not) failed: %v", v) + } + m.pc -= 4 + m.SetRegister(2+8, 0) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xff { + t.Fatalf("movz (yes) failed: %v", v) + } + + // slt t0, t1, t2 + m.SetRegister(1+8, 1) + m.SetRegister(2+8, 2) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 1 { + t.Fatalf("slt failed: %v", v) + } + m.pc -= 4 + m.SetRegister(1+8, 2) + m.SetRegister(2+8, 1) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0 { + t.Fatalf("slt failed: %v", v) + } + + // slti t0, t1, 0xff + m.SetRegister(1+8, 1) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 1 { + t.Fatalf("slti failed: %v", v) + } + m.pc -= 4 + m.SetRegister(1+8, 0xff) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0 { + t.Fatalf("slti failed: %v", v) + } + + // sltiu t0, t1, 0xff + m.SetRegister(1+8, 1) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 1 { + t.Fatalf("sltiu failed: %v", v) + } + m.pc -= 4 + m.SetRegister(1+8, 0xff) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0 { + t.Fatalf("sltiu failed: %v", v) + } + + // sltu t0, t1, t2 + m.SetRegister(1+8, 1) + m.SetRegister(2+8, 2) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 1 { + t.Fatalf("sltu failed: %v", v) + } + m.pc -= 4 + m.SetRegister(1+8, 2) + m.SetRegister(2+8, 1) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0 { + t.Fatalf("sltu failed: %v", v) + } + + // div t0, t1 + m.SetRegister(0+8, 10) + m.SetRegister(1+8, 2) + m.NextInstruction() + v, _ = m.GetRegister(LO) + if v != 5 { + t.Fatalf("div failed (lo): %v", v) + } + v, _ = m.GetRegister(HI) + if v != 0 { + t.Fatalf("div failed (hi): %v", v) + } + + // mult t0, t1 + m.SetRegister(0+8, 2) + m.SetRegister(1+8, 3) + m.NextInstruction() + v, _ = m.GetRegister(LO) + if v != 6 { + t.Fatalf("mult failed: %v", v) + } + v, _ = m.GetRegister(HI) + if v != 0 { + t.Fatalf("mult failed: %v", v) + } + + // mfhi t0 + m.SetRegister(HI, 0x12345678) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0x12345678 { + t.Fatalf("mfhi failed: %v", v) + } + + // mflo t0 + m.SetRegister(LO, 0x12345678) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0x12345678 { + t.Fatalf("mflo failed: %v", v) + } + + // mthi t0 + m.SetRegister(1+8, 0x12345678) + m.NextInstruction() + v, _ = m.GetRegister(HI) + if v != 0x12345678 { + t.Fatalf("mthi failed: %v", v) + } + + // mtlo t0 + m.SetRegister(1+8, 0x12345678) + m.NextInstruction() + v, _ = m.GetRegister(LO) + if v != 0x12345678 { + t.Fatalf("mtlo failed: %v", v) + } + + // beq t0, t1, 8 + m.SetRegister(0+8, 1) + m.SetRegister(1+8, 1) + savedPC := m.pc + m.NextInstruction() + if m.pc != 0 { + t.Fatalf("beq failed: %v to %v", savedPC, m.pc) + } + m.pc = savedPC + 4 + + // bgez t0, 8 + m.SetRegister(0+8, 1) + savedPC = m.pc + m.NextInstruction() + if m.pc != 0 { + t.Fatalf("bgez failed: %v to %v", savedPC, m.pc) + } + m.pc = savedPC + 4 + + // bgtz t0, 8 + m.SetRegister(0+8, 1) + savedPC = m.pc + m.NextInstruction() + if m.pc != 0 { + t.Fatalf("bgtz failed: %v to %v", savedPC, m.pc) + } + m.pc = savedPC + 4 + + // blez t0, 8 + m.SetRegister(0+8, 0) + savedPC = m.pc + m.NextInstruction() + if m.pc != 0 { + t.Fatalf("blez failed: %v to %v", savedPC, m.pc) + } + m.pc = savedPC + 4 + + // bltz t0, 8 + m.SetRegister(0+8, 0xff_ff_ff_ff) + savedPC = m.pc + m.NextInstruction() + if m.pc != 0 { + t.Fatalf("bltz failed: %v to %v", savedPC, m.pc) + } + m.pc = savedPC + 4 + + // bne t0, t1, 8 + m.SetRegister(0+8, 1) + m.SetRegister(1+8, 2) + savedPC = m.pc + m.NextInstruction() + if m.pc != 0 { + t.Fatalf("bne failed: %v to %v", savedPC, m.pc) + } + m.pc = savedPC + 4 + + // break + call, _ := m.NextInstruction() + if call.Number != machine.SYS_BREAK { + t.Fatalf("break failed: %v", call.Number) + } + + // syscall + m.SetRegister(2, machine.SYS_READ) + m.SetRegister(4, 1) + m.SetRegister(5, 2) + call, _ = m.NextInstruction() + expectedCall := machine.Call{ + Number: machine.SYS_READ, + Arg1: 1, + Arg2: 2, + } + if *call != expectedCall { + t.Fatalf("syscall failed: %v", call) + } + + // j 8 + savedPC = m.pc + m.NextInstruction() + if m.pc != (savedPC&0xf0000000)|8 { + t.Fatalf("j failed: %v", m.pc) + } + m.pc = savedPC + 4 + + // jal 8 + savedPC = m.pc + m.NextInstruction() + if m.pc != (savedPC&0xf0000000)|8 { + t.Fatalf("jal failed: %v", m.pc) + } + v, _ = m.GetRegister(31) + if v != uint64(savedPC+4) { + t.Fatalf("jal failed to set return address: %v", v) + } + m.pc = savedPC + 4 + + // jalr t0 + m.SetRegister(0+8, 0x12345678) + savedPC = m.pc + m.NextInstruction() + if m.pc != 0x12345678 { + t.Fatalf("jalr failed: %v", m.pc) + } + v, _ = m.GetRegister(31) + if v != uint64(savedPC+4) { + t.Fatalf("jalr failed to set return address: %v", v) + } + m.pc = savedPC + 4 + + // jr t0 + m.SetRegister(0+8, 0x12345678) + savedPC = m.pc + m.NextInstruction() + if m.pc != 0x12345678 { + t.Fatalf("jr failed: %v", m.pc) + } + m.pc = savedPC + 4 + + // lb t0, t1, 0 + m.SetMemoryChunk(0xcafebabe, []uint8{0xff, 2, 3, 4}) + m.SetRegister(1 + 8, 0xcafebabe) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xff_ff_ff_ff { + t.Fatalf("lb failed: %v", v) + } + + // lbu t0, t1, 0 + m.SetMemoryChunk(0xcafebabe, []uint8{0xff, 2, 3, 4}) + m.SetRegister(1 + 8, 0xcafebabe) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xff { + t.Fatalf("lbu failed: %v", v) + } + + // lh t0, t1, 0 + m.SetMemoryChunk(0xcafebabe, []uint8{0xff, 0xff, 3, 4}) + m.SetRegister(1 + 8, 0xcafebabe) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xff_ff_ff_ff { + t.Fatalf("lh failed: %v", v) + } + + // lhu t0, t1, 0 + m.SetMemoryChunk(0xcafebabe, []uint8{0xff, 0xff, 3, 4}) + m.SetRegister(1 + 8, 0xcafebabe) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0xff_ff { + t.Fatalf("lhu failed: %v", v) + } + + // lw t0, t1, 0 + m.SetMemoryChunk(0xcafebabe, []uint8{1, 2, 3, 4}) + m.SetRegister(1 + 8, 0xcafebabe) + m.NextInstruction() + v, _ = m.GetRegister(0 + 8) + if v != 0x04_03_02_01 { + t.Fatalf("lw failed: %v", v) + } + + // sb t0, t1, 0 + m.SetMemoryChunk(0xcafebabe, []uint8{0, 0, 0, 0}) + m.SetRegister(0 + 8, 0xdeadbeef) + m.SetRegister(1 + 8, 0xcafebabe) + m.NextInstruction() + slice, _ := m.GetMemoryChunk(0xcafebabe, 4) + if !reflect.DeepEqual(slice, []uint8{0xef, 0, 0, 0}) { + t.Fatalf("sb failed: %v", slice) + } + + // sh t0, t1, 0 + m.SetMemoryChunk(0xcafebabe, []uint8{0, 0, 0, 0}) + m.SetRegister(0 + 8, 0xdeadbeef) + m.SetRegister(1 + 8, 0xcafebabe) + m.NextInstruction() + slice, _ = m.GetMemoryChunk(0xcafebabe, 4) + if !reflect.DeepEqual(slice, []uint8{0xef, 0xbe, 0, 0}) { + t.Fatalf("sh failed: %v", slice) + } + + // sw t0, t1, 0 + m.SetMemoryChunk(0xcafebabe, []uint8{0, 0, 0, 0}) + m.SetRegister(0 + 8, 0xdeadbeef) + m.SetRegister(1 + 8, 0xcafebabe) + m.NextInstruction() + slice, _ = m.GetMemoryChunk(0xcafebabe, 4) + if !reflect.DeepEqual(slice, []uint8{0xef, 0xbe, 0xad, 0xde}) { + t.Fatalf("sw failed: %v", slice) + } +}