package x86_test

import (
	"testing"

	"git.urbach.dev/cli/q/src/cpu"
	"git.urbach.dev/cli/q/src/x86"
	"git.urbach.dev/go/assert"
)

func TestCall(t *testing.T) {
	assert.DeepEqual(t, x86.Call(nil, 1), []byte{0xE8, 0x01, 0x00, 0x00, 0x00})
	assert.DeepEqual(t, x86.CallAt(nil, 1), []byte{0xFF, 0x15, 0x01, 0x00, 0x00, 0x00})
}

func TestCallRegister(t *testing.T) {
	usagePatterns := []struct {
		Register cpu.Register
		Code     []byte
	}{
		{x86.R0, []byte{0xFF, 0xD0}},
		{x86.R1, []byte{0xFF, 0xD1}},
		{x86.R2, []byte{0xFF, 0xD2}},
		{x86.R3, []byte{0xFF, 0xD3}},
		{x86.SP, []byte{0xFF, 0xD4}},
		{x86.R5, []byte{0xFF, 0xD5}},
		{x86.R6, []byte{0xFF, 0xD6}},
		{x86.R7, []byte{0xFF, 0xD7}},
		{x86.R8, []byte{0x41, 0xFF, 0xD0}},
		{x86.R9, []byte{0x41, 0xFF, 0xD1}},
		{x86.R10, []byte{0x41, 0xFF, 0xD2}},
		{x86.R11, []byte{0x41, 0xFF, 0xD3}},
		{x86.R12, []byte{0x41, 0xFF, 0xD4}},
		{x86.R13, []byte{0x41, 0xFF, 0xD5}},
		{x86.R14, []byte{0x41, 0xFF, 0xD6}},
		{x86.R15, []byte{0x41, 0xFF, 0xD7}},
	}

	for _, pattern := range usagePatterns {
		t.Logf("call %s", pattern.Register)
		code := x86.CallRegister(nil, pattern.Register)
		assert.DeepEqual(t, code, pattern.Code)
	}
}

func TestCallAtMemory(t *testing.T) {
	usagePatterns := []struct {
		Register cpu.Register
		Offset   int8
		Scale    x86.Scale
		Code     []byte
	}{
		{x86.R0, 0, x86.Scale1, []byte{0xFF, 0x10}},
		{x86.R1, 0, x86.Scale1, []byte{0xFF, 0x11}},
		{x86.R2, 0, x86.Scale1, []byte{0xFF, 0x12}},
		{x86.R3, 0, x86.Scale1, []byte{0xFF, 0x13}},
		{x86.SP, 0, x86.Scale1, []byte{0xFF, 0x14, 0x24}},
		{x86.R5, 0, x86.Scale1, []byte{0xFF, 0x55, 0x00}},
		{x86.R6, 0, x86.Scale1, []byte{0xFF, 0x16}},
		{x86.R7, 0, x86.Scale1, []byte{0xFF, 0x17}},
		{x86.R8, 0, x86.Scale1, []byte{0x41, 0xFF, 0x10}},
		{x86.R9, 0, x86.Scale1, []byte{0x41, 0xFF, 0x11}},
		{x86.R10, 0, x86.Scale1, []byte{0x41, 0xFF, 0x12}},
		{x86.R11, 0, x86.Scale1, []byte{0x41, 0xFF, 0x13}},
		{x86.R12, 0, x86.Scale1, []byte{0x41, 0xFF, 0x14, 0x24}},
		{x86.R13, 0, x86.Scale1, []byte{0x41, 0xFF, 0x55, 0x00}},
		{x86.R14, 0, x86.Scale1, []byte{0x41, 0xFF, 0x16}},
		{x86.R15, 0, x86.Scale1, []byte{0x41, 0xFF, 0x17}},

		{x86.R0, 1, x86.Scale1, []byte{0xFF, 0x50, 0x01}},
		{x86.R1, 1, x86.Scale1, []byte{0xFF, 0x51, 0x01}},
		{x86.R2, 1, x86.Scale1, []byte{0xFF, 0x52, 0x01}},
		{x86.R3, 1, x86.Scale1, []byte{0xFF, 0x53, 0x01}},
		{x86.SP, 1, x86.Scale1, []byte{0xFF, 0x54, 0x24, 0x01}},
		{x86.R5, 1, x86.Scale1, []byte{0xFF, 0x55, 0x01}},
		{x86.R6, 1, x86.Scale1, []byte{0xFF, 0x56, 0x01}},
		{x86.R7, 1, x86.Scale1, []byte{0xFF, 0x57, 0x01}},
		{x86.R8, 1, x86.Scale1, []byte{0x41, 0xFF, 0x50, 0x01}},
		{x86.R9, 1, x86.Scale1, []byte{0x41, 0xFF, 0x51, 0x01}},
		{x86.R10, 1, x86.Scale1, []byte{0x41, 0xFF, 0x52, 0x01}},
		{x86.R11, 1, x86.Scale1, []byte{0x41, 0xFF, 0x53, 0x01}},
		{x86.R12, 1, x86.Scale1, []byte{0x41, 0xFF, 0x54, 0x24, 0x01}},
		{x86.R13, 1, x86.Scale1, []byte{0x41, 0xFF, 0x55, 0x01}},
		{x86.R14, 1, x86.Scale1, []byte{0x41, 0xFF, 0x56, 0x01}},
		{x86.R15, 1, x86.Scale1, []byte{0x41, 0xFF, 0x57, 0x01}},
	}

	for _, pattern := range usagePatterns {
		t.Logf("call [%s+%d]", pattern.Register, pattern.Offset)
		code := x86.CallAtMemory(nil, pattern.Register, pattern.Offset, pattern.Scale)
		assert.DeepEqual(t, code, pattern.Code)
	}
}