## Copyright (C) 2020 deesix <deesix@tuta.io>
## Copyright (C) 2020 Sanne Wouda
## This file is part of stage0.
##
## stage0 is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## stage0 is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with stage0.  If not, see <http://www.gnu.org/licenses/>.

DEFINE NULL 0000000000000000

# Stack (x18 as SP, 64 bits per element)
DEFINE PUSH_X0 408e1ff8
DEFINE PUSH_X1 418e1ff8
DEFINE PUSH_X16 508e1ff8
DEFINE PUSH_BP 518e1ff8
DEFINE PUSH_LR 5e8e1ff8

DEFINE POP_X0 408640f8
DEFINE POP_X1 418640f8
DEFINE POP_X16 508640f8
DEFINE POP_BP 518640f8
DEFINE POP_LR 5e8640f8

DEFINE INIT_SP f2030091 # mov x18, sp


# Jump/branch/call/return
DEFINE BR_X16 00021fd6
DEFINE BLR_X16 00023fd6
DEFINE RETURN c0035fd6

DEFINE CBZ_X0_PAST_BR a00000b4
DEFINE CBNZ_X0_PAST_BR a00000b5

DEFINE SKIP_INST_EQ 40000054
DEFINE SKIP_INST_NE 41000054
DEFINE SKIP_INST_LT 4b000054
DEFINE SKIP_INST_LE 4d000054
DEFINE SKIP_INST_GT 4c000054
DEFINE SKIP_INST_GE 4a000054

DEFINE SKIP_INST_LO 43000054
DEFINE SKIP_INST_LS 49000054
DEFINE SKIP_INST_HS 42000054
DEFINE SKIP_INST_HI 48000054

DEFINE SKIP_32_DATA 02000014


# Load literals (PC-relative)
DEFINE LOAD_W0_AHEAD 40000098 # FIXME: 40000018 ????
DEFINE LOAD_W1_AHEAD 41000018
DEFINE LOAD_W2_AHEAD 42000018
DEFINE LOAD_W16_AHEAD 50000018


# Load/store/dereference
DEFINE LDR_X0_[SP] 400240f9
DEFINE STR_X0_[X1] 200000f9
DEFINE STR_BYTE_W0_[X1] 20000039
DEFINE DEREF_X0 000040f9
DEFINE DEREF_X0_BYTE 00004039


# Move data between registers
DEFINE SET_X0_FROM_BP e00311aa
DEFINE SET_X1_FROM_X0 e10300aa
DEFINE SET_X1_FROM_SP e10312aa
DEFINE SET_X2_FROM_X0 e20300aa
DEFINE SET_X3_FROM_X0 e30300aa
DEFINE SET_X4_FROM_X0 e40300aa
DEFINE SET_X5_FROM_X0 e50300aa
DEFINE SET_X6_FROM_X0 e60300aa
DEFINE SET_X16_FROM_X0 f00300aa
DEFINE SET_X16_FROM_SP f00312aa
DEFINE SET_BP_FROM_X16 f10310aa
DEFINE SET_BP_FROM_SP f10312aa


# Move constant to register
DEFINE SET_X0_TO_0 000080d2
DEFINE SET_X0_TO_1 200080d2
DEFINE SET_X0_TO_17 200280d2
DEFINE SET_X0_TO_MINUS_1 00008092
DEFINE SET_W0_TO_MINUS_1 00008012
DEFINE SET_X1_TO_0 010080d2
DEFINE SET_X1_TO_2 410080d2
DEFINE SET_X1_TO_8 010180d2
DEFINE SET_X2_TO_1 220080d2
DEFINE SET_X0_TO_FCNTL_H_AT_FDCWD 600c8092


# Arith/logic/relational
DEFINE ADD_X0_X1_X0 2000008b
DEFINE ADD_X0_BP_X0 2002008b
DEFINE ADD_X1_SP_8 41220091

DEFINE SUB_X0_X1_X0 200000cb
DEFINE SUB_X0_X0_X1 000001cb
DEFINE SUB_X0_8 002000d1
DEFINE SUB_X0_16 004000d1
DEFINE SUB_X0_24 006000d1
DEFINE MSUB_X0_X0_X2_X1 0084029b

DEFINE MUL_X0_X1_X0 207c009b
DEFINE SDIV_X0_X1_X0 200cc09a
DEFINE SDIV_X2_X1_X0 220cc09a
DEFINE UDIV_X0_X1_X0 2008c09a
DEFINE UDIV_X2_X1_X0 2208c09a

DEFINE LSHIFT_X0_X0_X2 0020c29a
DEFINE LSHIFT_X0_X1_X0 2020c09a
DEFINE LOGICAL_RSHIFT_X0_X1_X0 2024c09a
DEFINE ARITH_RSHIFT_X0_X1_X0 2028c09a

DEFINE MVN_X0 e00320aa
DEFINE AND_X0_X1_X0 2000008a
DEFINE OR_X0_X1_X0 200000aa
DEFINE XOR_X0_X1_X0 000001ca

DEFINE CMP_X1_X0 3f0000eb


# Syscall
DEFINE SET_X8_TO_SYS_BRK c81a80d2
DEFINE SET_X8_TO_SYS_CHDIR 280680d2
DEFINE SET_X8_TO_SYS_CLONE 881b80d2
DEFINE SET_X8_TO_SYS_CLOSE 280780d2
DEFINE SET_X8_TO_SYS_EXECVE a81b80d2
DEFINE SET_X8_TO_SYS_EXIT a80b80d2
DEFINE SET_X8_TO_SYS_FACCESSAT 080680d2
DEFINE SET_X8_TO_SYS_FCHDIR 480680d2
DEFINE SET_X8_TO_SYS_FCHMODAT a80680d2
DEFINE SET_X8_TO_SYS_GETCWD 280280d2
DEFINE SET_X8_TO_SYS_LSEEK c80780d2
DEFINE SET_X8_TO_SYS_OPENAT 080780d2
DEFINE SET_X8_TO_SYS_READ e80780d2
DEFINE SET_X8_TO_SYS_UNAME 081480d2
DEFINE SET_X8_TO_SYS_WAIT4 882080d2
DEFINE SET_X8_TO_SYS_WRITE 080880d2
DEFINE SYSCALL 010000d4
