egg

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

commit 6172167833b5705e54a04462d1f2ff4e95615077
parent 6bf2f603e8d2174eb63aaf3aee59251cdd66fa57
Author: Gabriel G. de Brito <gabrielgbrito@icloud.com>
Date:   Sun, 24 Dec 2023 21:14:30 -0300

Added instructions test for RISC-V

Diffstat:
Mmachine/machine.go | 2+-
Mriscv/riscv.go | 11++++++++++-
Mriscv/riscv_test.go | 229++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mriscv/test-instructions.asm | 158++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
4 files changed, 394 insertions(+), 6 deletions(-)

diff --git a/machine/machine.go b/machine/machine.go @@ -19,7 +19,7 @@ type Machine interface { GetRegister(uint64) (uint64, error) // Address than content. SetRegister(uint64, uint64) error - //GetRegisterNumber(string) uint64 + GetRegisterNumber(string) (uint64, error) Assemble(string) ([]uint8, []assembler.DebuggerToken, error) } diff --git a/riscv/riscv.go b/riscv/riscv.go @@ -434,7 +434,7 @@ func (m *RiscV) NextInstruction() (*machine.Call, error) { return nil, errors.New(fmt.Sprintf("Could not load 4 bytes from address at PC: %x", m.pc)) } - i := uint32(iarr[0]) | uint32(iarr[1]<<8) | uint32(iarr[2]<<16) | uint32(iarr[3]<<24) + i := uint32(iarr[0]) | (uint32(iarr[1]) << 8) | (uint32(iarr[2]) << 16) | (uint32(iarr[3]) << 24) return m.execute(i) } @@ -867,6 +867,15 @@ func translateArgs(arg string) (uint64, error) { return parseRegisterArg(arg) } +func (m *RiscV) GetRegisterNumber(r string) (uint64, error) { + reg, err := translateArgs(r) + if err != nil || reg >= 32 { + return 0, errors.New(fmt.Sprintf("No such register: %v", r)) + } + + return reg, nil +} + func (m *RiscV) Assemble(asm string) ([]uint8, []assembler.DebuggerToken, error) { tokens := assembler.Tokenize(asm) rt, err := assembler.ResolveTokensFixedSize(tokens, 4, translateArgs) diff --git a/riscv/riscv_test.go b/riscv/riscv_test.go @@ -189,5 +189,230 @@ func TestAssembler(t *testing.T) { } } -//func TestInstructions(t *testing.T) { -//} +//go:embed test-instructions.asm +var inst string + +func (m *RiscV) getByName(n string) uint64 { + r, _ := m.GetRegisterNumber(n) + v, _ := m.GetRegister(r) + return v +} + +func (m *RiscV) assertRegister(t *testing.T, v string, r uint64, s string) { + a := m.getByName(v) + if a != r { + t.Fatalf("Wrong result in '%v': %v", s, a) + } +} + +func TestInstructions(t *testing.T) { + + var m RiscV + code, _, err := m.Assemble(inst) + 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) + } + + // + // Arithmetic immediate. + // + m.NextInstruction() + m.assertRegister(t, "t0", 1, "addi t0, zero, 1") + + m.NextInstruction() + m.assertRegister(t, "t0", 4, "xori t0, t0, 5") + + m.NextInstruction() + m.assertRegister(t, "t0", 6, "ori t0, t0, 2") + + m.NextInstruction() + m.assertRegister(t, "t0", 2, "andi t0, t0, 3") + + m.NextInstruction() + m.assertRegister(t, "t0", 8, "slli t0, t0, 2") + + m.NextInstruction() + m.NextInstruction() + m.assertRegister(t, "t2", 536870914, "srli t2, t1, 2") + + m.NextInstruction() + m.assertRegister(t, "t2", 3758096386, "srai t2, t1, 2") + + m.NextInstruction() + m.assertRegister(t, "t3", 1, "slti t3, t2, t0") + + m.NextInstruction() + m.assertRegister(t, "t3", 0, "sltu t3, t2, t0") + + // + // Arithmetic. + // + m.NextInstruction() + m.NextInstruction() + m.NextInstruction() + m.NextInstruction() + + m.NextInstruction() + m.assertRegister(t, "t4", 3, "add t4, t0, t1") + + m.NextInstruction() + m.assertRegister(t, "t4", 1, "sub t4, t2, t1") + + m.NextInstruction() + m.assertRegister(t, "t4", 6, "xor t4, t3, t2") + + m.NextInstruction() + m.assertRegister(t, "t4", 3, "or t4, t0, t1") + + m.NextInstruction() + m.assertRegister(t, "t4", 1, "and t4, t2, t5") + + m.NextInstruction() + m.assertRegister(t, "t4", 2, "sll t4, t0, t1") + + m.NextInstruction() + m.NextInstruction() + m.assertRegister(t, "t4", 536870914, "srl t4, t5, t1") + + m.NextInstruction() + m.assertRegister(t, "t4", 3758096386, "sra t4, t5, t1") + + m.NextInstruction() + m.assertRegister(t, "t4", 1, "slt t4, t5, t2") + + m.NextInstruction() + m.assertRegister(t, "t4", 0, "sltu t4, t5, t2") + + // + // Loads and stores. + // + m.NextInstruction() + m.NextInstruction() + + m.NextInstruction() + m.NextInstruction() + m.assertRegister(t, "t2", 0xaaeeffff, "lw t2, t1, 0") + + m.NextInstruction() + m.NextInstruction() + m.assertRegister(t, "t2", 0xffffffff, "lb t2, t1, 0") + m.NextInstruction() + m.assertRegister(t, "t2", 0x000000ff, "lbu t2, t1, 0") + + m.NextInstruction() + m.NextInstruction() + m.assertRegister(t, "t2", 0xffffffff, "lh t2, t1, 0") + m.NextInstruction() + m.assertRegister(t, "t2", 0x0000ffff, "lhu t2, t1, 0") + + // + // Branches. + // + + m.NextInstruction() + m.NextInstruction() + bges := m.pc + m.NextInstruction() + m.NextInstruction() + if m.pc != bges { + t.Fatalf("bges doesn't branch correctly") + } + m.NextInstruction() + m.NextInstruction() + if m.pc != bges + 8 { + t.Fatalf("bges doesn't pass correctly") + } + + m.NextInstruction() + m.NextInstruction() + beqs := m.pc + m.NextInstruction() + m.NextInstruction() + if m.pc != beqs { + t.Fatalf("beqs doesn't branch correctly") + } + m.NextInstruction() + m.NextInstruction() + if m.pc != beqs + 8 { + t.Fatalf("beqs doesn't pass correctly") + } + + m.NextInstruction() + m.NextInstruction() + blts := m.pc + m.NextInstruction() + m.NextInstruction() + if m.pc != blts { + t.Fatalf("blts doesn't branch correctly") + } + m.NextInstruction() + m.NextInstruction() + if m.pc != blts + 8 { + t.Fatalf("blts doesn't pass correctly") + } + + m.NextInstruction() + m.NextInstruction() + bnes := m.pc + m.NextInstruction() + m.NextInstruction() + if m.pc != bnes { + t.Fatalf("bnes doesn't branch correctly") + } + m.NextInstruction() + m.NextInstruction() + if m.pc != bnes + 8 { + t.Fatalf("bnes doesn't pass correctly") + } + + m.NextInstruction() + m.NextInstruction() + bltu := m.pc + m.NextInstruction() + if m.pc == bltu + 4 { + t.Fatalf("bltu doesn't branch correctly") + } + + bgeu := m.pc + m.NextInstruction() + if m.pc == bgeu + 4 { + t.Fatalf("bgeu doesn't branch correctly") + } + + // + // Jumps. + // + ret := m.pc + m.NextInstruction() + if m.pc == ret + 4 { + t.Fatalf("jal does not jump correctly") + } + m.NextInstruction() + if m.pc != ret + 4 { + t.Fatalf("jalr does not return correctly") + } + + // + // Calls. + // + m.NextInstruction() + call, _ := m.NextInstruction() + if call == nil { + t.Fatalf("call is nil") + } + if call.Number != 2 { + t.Fatalf("call number is not 2") + } + call, _ = m.NextInstruction() + if call == nil { + t.Fatalf("break call is nil") + } + if call.Number != 1 { + t.Fatalf("break is not 1") + } +} diff --git a/riscv/test-instructions.asm b/riscv/test-instructions.asm @@ -1,3 +1,158 @@ ;; ;; Test source file for RISC-V IM 32 bits implementation at EGG. -;; -\ No newline at end of file +;; +;; This implements every instruction, the machine should change to a well +;; defined state after each. +;; + +_start: + ;; + ;; Arithmetic immediate. + ;; + + ;; t0 = 1 + addi t0, zero, 1 + + ;; t0 = t0 ^ 5 :: t0 == 4 + xori t0, t0, 5 + + ;; t0 = t0 | 2 :: t0 == 7 + ori t0, t0, 2 + + ;; t0 = t0 & 3 :: t0 == 2 + andi t0, t0, 3 + + ;; t0 = t0 << 2 :: t0 == 8 + slli t0, t0, 2 + + ;; t1 = 2147483658 + addi t1, zero, 2147483658 + + ;; t2 = t1 >> 2 :: t2 == 536870914 + srli t2, t1, 2 + + ;; t2 = t1 !>> 2 :: t2 == 3758096386 + srai t2, t1, 2 + + ;; t3 = t2 < t0 (signed negative less than 4) :: t3 == 1 + slti t3, t2, t0 + + ;; Same but unsigned :: t3 == 0 + sltu t3, t2, t0 + + ;; + ;; Arithmetic. + ;; + + ;; (Before testing set some constants so we can play). + addi t0, zero, 1 + addi t1, zero, 2 + addi t2, zero, 3 + addi t3, zero, 5 + + ;; t4 = t0 + t1 :: t4 == 3 + add t4, t0, t1 + + ;; t4 = t2 - t1 :: t4 == 1 + sub t4, t2, t1 + + ;; t4 = t3 ^ t2 :: t4 == 6 + xor t4, t3, t2 + + ;; t4 = t0 | t1 :: t4 == 3 + or t4, t0, t1 + + ;; t4 = t2 & t5 :: t4 == 1 + and t4, t2, t5 + + ;; t4 = t0 << t1 :: t4 == 2 + sll t4, t0, t1 + + ;; Negative constant strikes again. + addi t5, zero, 2147483658 + + ;; t4 = t5 >> t1 :: t4 == 536870914 + srl t4, t5, t1 + + ;; t4 = t5 !>> t1 :: t4 == 3758096386 + sra t4, t5, t1 + + ;; t4 = t5 > t2 (signed negative less than 3) :: t4 == 1 + slt t4, t5, t2 + + ;; Same but unsigned :: t4 == 0 + sltu t4, t5, t2 + + ;; + ;; Loads and stores with the word 0xaaeeffff at address 0x1000. + ;; + addi t0, zero, 0x1000 + addi t1, zero, 0xaaeeffff + + sw t0, t1, 0 + ;; t2 == 0xaaeeffff + lw t2, t1, 0 + + sb t0, t1, 0 + ;; t2 == 0xffffffff + lb t2, t1, 0 + ;; t2 == 0x000000ff + lbu t2, t1, 0 + + sh t0, t1, 0 + ;; t2 == 0xffffffff + lh t2, t1, 0 + ;; t2 == 0x0000ffff + lhu t2, t1, 0 + + ;; + ;; Branches. + ;; + + addi t0, zero, 1 + addi t1, zero, 2 +bges: + sub t1, t1, t0 + bge t1, t0, bges + + addi t0, zero, 1 + addi t1, zero, 2 +beqs: + sub t1, t1, t0 + beq t1, t0, beqs + + addi t0, zero, 1 + addi t1, zero, 3 +blts: + sub t1, t1, t0 + blt t0, t1, blts + + addi t0, zero, 1 + addi t1, zero, 3 +bnes: + sub t1, t1, t0 + blt t0, t1, bnes + + ;; Unsigned + addi t0, zero, 0xff000000 + addi t1, zero, 1 + bltu t1, t0, bltue + addi zero, zero, 0 ;; Shouldn't perform this. +bltue: + bgeu t0, t1, bgeue + addi zero, zero, 0 ;; Shouldn't perform this. +bgeue: + + ;; + ;; Jump. + ;; + jal ra, func + ;; It should jump to func at the bottom them return. + + ;; Calls. + addi a7, zero, 2 + ecall + ebreak + +func: + jalr zero, ra, 0