aboutsummaryrefslogtreecommitdiff
path: root/libpsn00b/psxgpu/resetgraph.s
blob: 189b1ad91865540879fe5b14e793e47d2ddea07e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
.set noreorder
.set noat

.include "hwregs_a.h"

.section .text


.global ResetGraph						# Resets the GPU and installs a
.type ResetGraph, @function				# VSync event handler
ResetGraph:
	addiu	$sp, -8						# C style stack allocation (required if
	sw		$ra, 0($sp)					# you call BIOS functions from asm)
	sw		$a0, 4($sp)

	la		$v0, _hooks_installed		# Skip installing hooks if this function
	lbu		$v0, 0($v0)					# has already been called before once
	nop
	bnez	$v0, .skip_hook_init
	nop

	# Temporary, may help improve compatibility?
	#jal		SetDefaultExitFromException
	#nop

	jal		ChangeClearPAD				# Remove pad handler left by the BIOS
	move	$a0, $0

	li		$a0, 1
	jal		ChangeClearRCnt				# Remove RCnt handler
	move	$a1, $0

	jal		_96_remove					# Remove CD handling left by the BIOS
	nop

	lui		$a3, 0x1f80					# Base address for I/O

	lui		$v0, 0x3b33					# Enables DMA channel 6 (for ClearOTag)
	ori		$v0, 0x3b33					# Enables DMA channel 2
	sw		$v0, DPCR($a3)
	sw		$0 , DICR($a3)				# Clear DICR (not needed)

	li		$v0, 0x9					# Enable IRQ0 (vblank)
	sw		$v0, IMASK($a3)

	# Set an event handler

	li		$a0, 0xf2000003				# RCntCNT3 (vsync class)
	li		$a1, 0x2
	li		$a2, 0x1000
	la		$a3, _vsync_func			# VSync event handler

	jal		OpenEvent					# Open a VSync event handler
										# (PSXSDK style vsync handler)
	addiu	$sp, -16
	addiu	$sp, 16

	la		$v1, _vsync_event_desc		# Save event descriptor
	sw		$v0, 0($v1)

	move	$a0, $v0
	jal		EnableEvent					# Enable the opened event
	addiu	$sp, -4
	addiu	$sp, 4

	la		$v0, _hooks_installed		# Set installed flag
	li		$v1, 0x1
	sb		$v1, 0($v0)

	la		$v0, _vsync_counter			# Clear VSync counter
	sw		$0 , 0($v0)

	la		$v0, _vsync_callback_func	# Clear callback function
	sw		$0 , 0($v0)

	la		$a0, _custom_exit
	jal		SetCustomExitFromException
	addiu	$sp, -4
	addiu	$sp, 4
	
	jal		ExitCriticalSection			# Re-enable interrupts
	nop

.skip_hook_init:
	
	lui		$a3, 0x1f80

	lw		$v0, GP1($a3)				# Get video standard
	lui		$v1, 0x0010
	and		$v0, $v1
	la		$v1, _gpu_standard
	beqz	$v0, .not_pal
	sw		$0 , 0($v1)
	li		$v0, 1
	sw		$v0, 0($v1)
.not_pal:

	lw		$a0, 4($sp)				# Get argument value

	lui		$a3, 0x1f80					# Set base I/O again (likely destroyed
										# by previous calls)

	li		$v0, 0x1d00					# Configure timer 1 as Hblank counter
	sw		$v0, T1_MODE($a3)			# Set timer 1 value

	li		$at, 1
	beq		$a0, $at, .gpu_init_1
	nop
	li		$at, 3
	beq		$a0, $at, .gpu_init_3
	nop

	sw		$0 , GP1($a3)				# Reset the GPU

	b		.init_done
	nop

.gpu_init_1:

	sw		$0 , D2_CHCR($a3)			# Stop any DMA

.gpu_init_3:

	li		$v0, 0x1					# Reset the command buffer
	sw		$v0, GP1($a3)

.init_done:

	lw		$ra, 0($sp)
	lw		$a0, 4($sp)					# Return
	jr		$ra
	addiu	$sp, 8


.global _vsync_func						# VSync event handler, executed on
.type _vsync_func, @function			# every VBlank
_vsync_func:

	la		$gp, _gp

	lui		$at, 0x1f80					# Check if there's a VSync IRQ
	lw		$v0, IMASK($at)
	nop
	andi	$v0, $v0, 0x1
	beqz	$v0, .exit
	nop

	lw		$v1, ISTAT($at)
	nop
	andi	$v0, $v1, 0x1
	beqz	$v0, .exit
	nop
	
	#xori	$v1, $v1, 0x1				# Acknowledge the IRQ
	#sw		$v1, ISTAT($at)				# Commented out as it breaks BIOS pads

	la		$v1, _vsync_counter			# Increment VSync counter
	lw		$v0, 0($v1)
	nop
	addiu	$v0, 1
	sw		$v0, 0($v1)

	la		$v0, _vsync_callback_func	# Check if a callback function is set
	lw		$v0, 0($v0)
	nop
	beqz	$v0, .exit
	nop

	addiu	$sp, -0x20					# Save return address
	sw		$ra, 28($sp)

	jalr	$v0							# Execute user function
	nop

	lw		$ra, 28($sp)				# Restore previous return address
	addiu	$sp, 0x20

.exit:
	jr      $ra
	nop


.global _vsync_func_2
.type _vsync_func_2, @function
_vsync_func_2:
	
	lui		$at, 0x1f80					# Check if there's a VSync IRQ
	lw		$v0, IMASK($at)
	nop
	andi	$v0, $v0, 0x1
	beqz	$v0, .exit_2
	nop

	lw		$v1, ISTAT($at)
	nop
	andi	$v0, $v1, 0x1
	beqz	$v0, .exit_2
	nop
	
	xori	$v1, $v1, 0x1				# Acknowledge the IRQ
	sw		$v1, ISTAT($at)

.exit_2:
	
	j		ReturnFromException
	nop


.global VSync							# VSync function
.type VSync, @function
VSync:
	addiu	$sp, -4
	sw		$ra, 0($sp)

	la		$a1, _vsync_counter
	lw		$v0, 0($a1)
	nop
.loop:
	lw		$v1, 0($a1)
	nop
	beq		$v0, $v1, .loop
	nop

	la		$v0, _gpu_current_field		# Get last field value
	lbu		$v1, 0($v0)
.wait_field:							# Wait for field bit to change
	jal		ReadGPUstat
	nop
	srl		$v0, 31
	beq		$v0, $v1, .wait_field
	nop

	la		$v1, _gpu_current_field		# Store new field value
	sb		$v0, 0($v1)

	lw		$ra, 0($sp)
	addiu	$sp, 4
	jr		$ra
	nop


.section .data

.global library_credits
.type library_credits, @object
library_credits:
	.string "psxgpu programs by Lameguy64"


.type _custom_exit, @object
_custom_exit:
	.word _vsync_func_2		# pc
	.word _vsync_stack		# sp
	.word 0					# fp
	.word 0					# s0
	.word 0					# s1
	.word 0					# s2
	.word 0					# s3
	.word 0					# s4
	.word 0					# s5
	.word 0					# s6
	.word 0					# s7
	.word _gp				# gp

	.fill 60
_vsync_stack:
	.fill 4
	
.type _vsync_counter, @object
_vsync_counter:
	.word 0

.comm	_vsync_callback_func, 4, 4
.comm	_vsync_event_desc, 4, 4

.comm	_gpu_standard, 4, 4
.comm	_gpu_current_field, 4, 4
.comm	_hooks_installed, 4, 4