Implemented more arm64 instructions

This commit is contained in:
2025-03-13 13:17:58 +01:00
parent dc664dbf5a
commit 5a5061c5d7
23 changed files with 290 additions and 53 deletions

View File

@ -2,8 +2,8 @@ package arm
import "git.urbach.dev/cli/q/src/cpu"
// AddRegisterNumber adds a number to a register
// AddRegisterNumber adds a number to a register.
func AddRegisterNumber(destination cpu.Register, source cpu.Register, number int) uint32 {
number &= 0xFFF
number &= 0b1111_1111_1111
return 0b100100010<<23 | (uint32(number) << 10) | (uint32(source) << 5) | uint32(destination)
}

43
src/arm/Jump.go Normal file
View File

@ -0,0 +1,43 @@
package arm
// Jump continues program flow at the new offset.
func Jump(offset int) uint32 {
offset &= 0b11_1111_1111_1111_1111_1111_1111
return 0b000101<<26 | uint32(offset)
}
// JumpIfLess jumps if the result was less.
func JumpIfLess(offset int) uint32 {
return branchCond(0b1001, offset)
}
// JumpIfLessOrEqual jumps if the result was less or equal.
func JumpIfLessOrEqual(offset int) uint32 {
return branchCond(0b1101, offset)
}
// JumpIfGreater jumps if the result was greater.
func JumpIfGreater(offset int) uint32 {
return branchCond(0b1100, offset)
}
// JumpIfGreaterOrEqual jumps if the result was greater or equal.
func JumpIfGreaterOrEqual(offset int) uint32 {
return branchCond(0b1010, offset)
}
// JumpIfEqual jumps if the result was equal.
func JumpIfEqual(offset int) uint32 {
return branchCond(0b0000, offset)
}
// JumpIfNotEqual jumps if the result was not equal.
func JumpIfNotEqual(offset int) uint32 {
return branchCond(0b0001, offset)
}
// branchCond performs a conditional branch to a PC-relative offset.
func branchCond(cond uint32, offset int) uint32 {
offset &= 0b111_1111_1111_1111_1111
return 0b01010100<<24 | uint32(offset)<<5 | cond
}

View File

@ -3,11 +3,20 @@ package arm
import "git.urbach.dev/cli/q/src/cpu"
// LoadRegister loads from memory into a register.
func LoadRegister(destination cpu.Register, base cpu.Register, offset int16, length byte) uint32 {
if offset < 0 {
offset &= 0xFF
offset |= 1 << 8
func LoadRegister(destination cpu.Register, base cpu.Register, offset int, length byte) uint32 {
offset &= 0b1_1111_1111
common := 1<<22 | uint32(offset)<<12 | uint32(base)<<5 | uint32(destination)
switch length {
case 1:
return 0b00111<<27 | common
case 2:
return 0b01111<<27 | common
case 4:
return 0b10111<<27 | common
case 8:
return 0b11111<<27 | common
}
return 0b11111000010<<21 | uint32(offset)<<12 | uint32(base)<<5 | uint32(destination)
panic("invalid length")
}

View File

@ -7,6 +7,6 @@ import "git.urbach.dev/cli/q/src/cpu"
// This is the post-index version of the instruction so the offset is applied to the base register after the memory access.
func LoadPair(reg1 cpu.Register, reg2 cpu.Register, base cpu.Register, offset int) uint32 {
offset /= 8
offset &= 0b1111111
offset &= 0b111_1111
return 0b1010100011<<22 | (uint32(offset) << 15) | (uint32(reg2) << 10) | (uint32(base) << 5) | uint32(reg1)
}

View File

@ -12,10 +12,18 @@ func TestLoadRegister(t *testing.T) {
usagePatterns := []struct {
Destination cpu.Register
Base cpu.Register
Offset int16
Offset int
Length byte
Code uint32
}{
{arm.X0, arm.X1, -8, 1, 0x385F8020},
{arm.X1, arm.X0, -8, 1, 0x385F8001},
{arm.X0, arm.X1, -8, 2, 0x785F8020},
{arm.X1, arm.X0, -8, 2, 0x785F8001},
{arm.X0, arm.X1, -8, 4, 0xB85F8020},
{arm.X1, arm.X0, -8, 4, 0xB85F8001},
{arm.X0, arm.X1, -8, 8, 0xF85F8020},
{arm.X1, arm.X0, -8, 8, 0xF85F8001},
{arm.X2, arm.X1, -8, 8, 0xF85F8022},
{arm.X2, arm.X1, 0, 8, 0xF8400022},
{arm.X2, arm.X1, 8, 8, 0xF8408022},

24
src/arm/Store.go Normal file
View File

@ -0,0 +1,24 @@
package arm
import (
"git.urbach.dev/cli/q/src/cpu"
)
// StoreRegister writes the contents of the register to a memory address.
func StoreRegister(source cpu.Register, base cpu.Register, offset int, length byte) uint32 {
offset &= 0b1_1111_1111
common := uint32(offset)<<12 | uint32(base)<<5 | uint32(source)
switch length {
case 1:
return 0b00111<<27 | common
case 2:
return 0b01111<<27 | common
case 4:
return 0b10111<<27 | common
case 8:
return 0b11111<<27 | common
}
panic("invalid length")
}

View File

@ -9,6 +9,6 @@ import (
// This is the pre-index version of the instruction so the offset is applied to the base register before the memory access.
func StorePair(reg1 cpu.Register, reg2 cpu.Register, base cpu.Register, offset int) uint32 {
offset /= 8
offset &= 0b1111111
offset &= 0b111_1111
return 0b1010100110<<22 | uint32(offset)<<15 | uint32(reg2)<<10 | uint32(base)<<5 | uint32(reg1)
}

34
src/arm/Store_test.go Normal file
View File

@ -0,0 +1,34 @@
package arm_test
import (
"testing"
"git.urbach.dev/cli/q/src/arm"
"git.urbach.dev/cli/q/src/cpu"
"git.urbach.dev/go/assert"
)
func TestStoreRegister(t *testing.T) {
usagePatterns := []struct {
Source cpu.Register
Base cpu.Register
Offset int
Length byte
Code uint32
}{
{arm.X0, arm.X1, -8, 1, 0x381F8020},
{arm.X1, arm.X0, -8, 1, 0x381F8001},
{arm.X0, arm.X1, -8, 2, 0x781F8020},
{arm.X1, arm.X0, -8, 2, 0x781F8001},
{arm.X0, arm.X1, -8, 4, 0xB81F8020},
{arm.X1, arm.X0, -8, 4, 0xB81F8001},
{arm.X0, arm.X1, -8, 8, 0xF81F8020},
{arm.X1, arm.X0, -8, 8, 0xF81F8001},
}
for _, pattern := range usagePatterns {
t.Logf("stur %s, [%s, #%d] %db", pattern.Source, pattern.Base, pattern.Offset, pattern.Length)
code := arm.StoreRegister(pattern.Source, pattern.Base, pattern.Offset, pattern.Length)
assert.DeepEqual(t, code, pattern.Code)
}
}

9
src/arm/Sub.go Normal file
View File

@ -0,0 +1,9 @@
package arm
import "git.urbach.dev/cli/q/src/cpu"
// SubRegisterNumber subtracts a number from the given register.
func SubRegisterNumber(destination cpu.Register, source cpu.Register, number int) uint32 {
number &= 0b1111_1111_1111
return 0b111100010<<23 | (uint32(number) << 10) | (uint32(source) << 5) | uint32(destination)
}