1# MIPS simulator testsuite utility functions.
2# Copyright (C) 2004-2023 Free Software Foundation, Inc.
3# Contributed by Chris Demetriou of Broadcom Corporation.
4#
5# This file is part of the GNU simulators.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20
21# $1, $4, $5, %6, are used as temps by the macros defined here.
22
23	.macro writemsg msg
24	la	$5, 901f
25	li	$6, 902f - 901f
26	.data
27901:	.ascii	"\msg\n"
28902:
29	.previous
30	.set push
31	.set noreorder
32	jal	_dowrite
33	li	$4, 0
34	.set pop
35	.endm
36
37
38	# The MIPS simulator uses "break 0x3ff" as the code to exit,
39	# with the return value in $4 (a0).
40	.macro exit rc
41	li	$4, \rc
42	break	0x3ff
43	.endm
44
45
46	.macro setup
47
48	.global _start
49	.global __start
50	.ent _start
51_start:
52__start:
53	.set push
54	.set noreorder
55	j	DIAG
56	nop
57	.set pop
58	.end _start
59
60	.global _fail
61	.ent _fail
62_fail:
63	writemsg "fail"
64	exit 1
65	.end _fail
66
67	.global _pass
68	.ent _pass
69_pass:
70	writemsg "pass"
71	exit 0
72	.end _pass
73
74	# The MIPS simulator can use multiple different monitor types,
75	# so we hard-code the simulator "write" reserved instruction opcode,
76	# rather than jumping to a vector that invokes it.  The operation
77	# expects RA to point to the location at which to continue
78	# after writing.
79	.global _dowrite
80	.ent _dowrite
81_dowrite:
82	# Write opcode (reserved instruction).  See sim_monitor and its
83	# callers in sim/mips/interp.c.
84	.word	0x00000039 | ((8 << 1) << 6)
85	.end _dowrite
86
87	.endm	# setup
88
89
90	.macro pass
91	.set push
92	.set noreorder
93	j	_pass
94	nop
95	.set pop
96	.endm
97
98
99	.macro fail
100	.set push
101	.set noreorder
102	j	_fail
103	nop
104	.set pop
105	.endm
106
107
108	.macro load32 reg, val
109	li	\reg, \val
110	.endm
111
112
113	.macro load64 reg, val
114	dli	\reg, \val
115	.endm
116
117
118	.macro loadaddr reg, addr
119	la	\reg, \addr
120	.endm
121
122
123	.macro checkreg reg, expreg
124	.set push
125	.set noat
126	.set noreorder
127	beq	\expreg, \reg, 901f
128	nop
129	fail
130901:
131	.set pop
132	.endm
133
134
135	.macro check32 reg, val
136	.set push
137	.set noat
138	load32	$1, \val
139	checkreg \reg, $1
140	.set pop
141	.endm
142
143
144	.macro check64 reg, val
145	.set push
146	.set noat
147	load64	$1, \val
148	checkreg \reg, $1
149	.set pop
150	.endm
151
152
153	# Check hi-lo register pair against data stored at base+o1 and base+o2
154	# Clobbers $1 - $5
155	.macro checkpair lo, hi, base, w, o1, o2
156	move  $2, \lo
157	move  $3, \hi
158	.set noat
159	la   $1, \base
160	l\w	$4, \o1($1)
161	l\w	$5, \o2($1)
162	.set at
163	checkreg  $2, $4
164	checkreg  $3, $5
165	.endm
166
167	.macro checkpair_le_d lo, hi, base
168	checkpair \lo, \hi, \base, w, 0, 4
169	.endm
170
171	.macro checkpair_be_d lo, hi, base
172	checkpair \lo, \hi, \base, w, 4, 0
173	.endm
174
175	.macro checkpair_le_q lo, hi, base
176	checkpair \lo, \hi, \base, d, 0, 8
177	.endm
178
179	.macro checkpair_be_q lo, hi, base
180	checkpair \lo, \hi, \base, d, 8, 0
181	.endm
182
183	# Endian-ness for comparison is determined by reading a word at ec
184	.macro checkpair_xendian lo, hi, base, ec, w
185	.set noat
186	lw   $1, \ec
187	andi $1, $1, 0x1
188	# check endianess
189	beqz  $1, 2f
190	.set at
1911: # big endian
192	checkpair_be_\w \lo, \hi, \base
193	b 3f
1942: # little endian
195	checkpair_le_\w \lo, \hi, \base
1963:
197	.endm
198
199	.macro checkpair_qword lo, hi, base, oe
200	checkpair_xendian \lo, \hi, \base, \oe, q
201	.endm
202
203	.macro checkpair_dword lo, hi, base, oe
204	checkpair_xendian \lo, \hi, \base, \oe, d
205	.endm
206