xinc_math.asm

  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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//////////////////////////////////////////////////
//	File:		xinc_math.asm
//	Revision:	1.0
//	Date:		Oct. 23, 2004
//	Author:		Michael Broughton
//////////////////////////////////////////////////


#ifndef __MATH32__
#define __MATH32__


//////////////////////////////////////////////////
//	Input Params:
//		r0 = Operand 1 (Low Order)
//		r1 = Operand 1 (High Order)
//		r2 = Operand 2 (Low Order)
//		r3 = Operand 2 (High Order)
//	Output Params:
//		r0 = Result (Low Order)
//		r1 = Result (High Order)
//		r2 = Undefined
//		r3 = Undefined
//////////////////////////////////////////////////
Add32:
	st	r4,sp,0
	mov	r4,0b0000			// Stores the condition code for the result.
	add	r0,r0,r2			// Add the low order bytes.
	bc	CS,AddCarryToHighOrder		// A carry was generated, add to the high order bytes.
	bc	ZC,AddHighOrderBytes
	bis	r4,r4,2				// Indicate that the low order result is zero.
	bra	AddHighOrderBytes
AddCarryToHighOrder:
	bc	ZC,ResultIsNotZero
	bis	r4,r4,2				// Indicate that the low order result is zero.
ResultIsNotZero:
	add	r2,r1,1				// Temporarily add the low order carry to high order operand 1.
	bc	VS,TryOtherOperand		// Overflow occoured Try the other high order operand.
	add	r1,r1,1				// Add the low order carry to high order operand 1.
	bc	CC,AddHighOrderBytes
	bis	r4,r4,0				// Indicate that a carry was generated.
	bra	AddHighOrderBytes
TryOtherOperand:
	add	r3,r3,1				// Add the low order carry to high order operand 2.
	bc	VC,CarryDidNotCarry		// Overflow occoured again. This is a special case.
	mov	r1,-1				// In this special case the high order result is always -1.
	mov	r3,0b1010			// In this special case the condition code is always 1010.
	bra	Add32_Return			// Return the result.
CarryDidNotCarry:
	bc	CC,AddHighOrderBytes
	bis	r4,r4,0				// Indicate that a carry was generated.
AddHighOrderBytes:
	add	r2,r1,r3			// Temporarily add the high order bytes.
	bc	NC,ResultIsNotNegative
	bis	r4,r4,3				// Indicate that the result is negative.
ResultIsNotNegative:
	add	r2,r1,r3			// Temporarily add the high order bytes.
	bc	ZS,ResultIsZero
	bic	r4,r4,2				// Indicate that the result is non-zero.
ResultIsZero:
	add	r2,r1,r3			// Temporarily add the high order bytes.
	bc	VC,ResultDidNotOverflow
	bis	r4,r4,1				// Indicate that overflow occoured.
ResultDidNotOverflow:
	add	r1,r1,r3			// Add the high order bytes.
	bc	VC,ResultDidNotCarry
	bis	r4,r4,0				// Indicate that a carry was generated.
ResultDidNotCarry:
	add	r3,r4,0				// Move the condition code.
//	bra	Add32_Return			// Return the result.
Add32_Return:
	ld	r4,sp,0
	bra	SetConditionCodes


//////////////////////////////////////////////////
//	Input Params:
//		r3 = Source
//		r4 = Shift (-15:15)
//	Output Params:
//		r3 = Result
//		r4 = Undefined
//////////////////////////////////////////////////
Shift16:
	st	r0,sp,0
	st	r1,sp,1
	rol	r3,r3,r4
	add	r0,r3,0
	bc	ZS,Shift16_Return
	and	r0,r4,0x8000
	bc	NS,Shift16Right
Shift16Left:
	mov	r0,0
	add	r1,r4,0
	bra	Shift16Loop
Shift16Right:
	add	r0,r4,16
	mov	r1,16
Shift16Loop:
	bic	r3,r3,r0
	add	r0,r0,1
	sub	r4,r0,r1
	bc	NE,Shift16Loop
Shift16_Return:
	ld	r0,sp,0
	ld	r1,sp,1
	jsr	r6,r6
	bra	Shift16


//////////////////////////////////////////////////
//	Input Params:
//		r0 = Source (Low Order)
//		r1 = Source (High Order)
//		r2 = Shift (-31:31)
//	Output Params:
//		r0 = Result (Low Order)
//		r1 = Result (High Order)
//		r2 = Undefined
//////////////////////////////////////////////////
Shift32:
	st	r3,sp,0
	st	r4,sp,1
	st	r5,sp,2
	st	r6,sp,3
	add	sp,sp,4
	add	r3,r2,0
	bc	ZS,Shift32_Return
	and	r3,r2,0x8000
	bc	NS,Shift32Right
Shift32Left:
	mov	r4,16
	sub	r4,r2,r4
	bc	UGE,Shift32LeftFar
	add	r3,r1,0
	add	r4,r2,0
	jsr	r6,Shift16
	add	r1,r3,0
	add	r3,r0,0
	add	r4,r2,-16
	jsr	r6,Shift16
	add	r1,r1,r3
	add	r3,r0,0
	add	r4,r2,0
	jsr	r6,Shift16
	add	r0,r3,0
	bra	Shift32_Return
Shift32LeftFar:
	add	r3,r0,0
	jsr	r6,Shift16
	mov	r0,0
	add	r1,r3,0
	bra	Shift32_Return
Shift32Right:
	mov	r4,-16
	sub	r4,r2,r4
	bc	LE,Shift32RightFar
	add	r3,r0,0
	add	r4,r2,0
	jsr	r6,Shift16
	add	r0,r3,0
	add	r3,r1,0
	add	r4,r2,16
	jsr	r6,Shift16
	add	r0,r0,r3
	add	r3,r1,0
	add	r4,r2,0
	jsr	r6,Shift16
	add	r1,r3,0
	bra	Shift32_Return
Shift32RightFar:
	add	r3,r1,0
	jsr	r6,Shift16
	mov	r1,0
	add	r0,r3,0
Shift32_Return:
	sub	sp,sp,4
	ld	r6,sp,3
	ld	r5,sp,2
	ld	r4,sp,1
	ld	r3,sp,0
	jsr	r6,r6
	bra	Shift32


//////////////////////////////////////////////////
//	Input Params:
//		r0 = Operand 1
//		r1 = Operand 2
//	Output Params:
//		r0 = Result (Low Order)
//		r1 = Result (High Order)
//////////////////////////////////////////////////
Multiply16:
	st	r2,sp,0
	st	r3,sp,1
	st	r4,sp,2
	st	r5,sp,3
	st	r6,sp,4
	add	sp,sp,5
	mov	r2,0
	mov	r3,0
	mov	r4,0
Multiply16Loop:
	mov	r5,1
	rol	r5,r5,r4
	and	r5,r5,r1
	bc	ZC,Multiply16SkipBit
	st	r0,sp,0
	st	r1,sp,1
	st	r2,sp,2
	add	sp,sp,3
	add	r0,r2,0
	mov	r1,0
	add	r2,r4,0
	jsr	r6,Shift32
	sub	sp,sp,3
	ld	r2,sp,2
	add	r2,r0,r2
	add	r3,r1,r3
	ld	r1,sp,1
	ld	r0,sp,0
Multiply16SkipBit:
	add	r4,r4,1
	mov	r5,16
	sub	r5,r4,r6
	bc	ULT,Multiply16Loop
Multiply16_Return:
	sub	sp,sp,5
	ld	r6,sp,4
	ld	r5,sp,3
	ld	r4,sp,2
	ld	r3,sp,1
	ld	r2,sp,0
	jsr	r6,r6
	bra	Multiply16


//////////////////////////////////////////////////
//	Input Params:
//		r3 = 0b000000000000NZVC
//	Output Params:
//		r3 = Undefined
//	Condition Code Table:
//		NZVC		Example
//		0000		1 + 1
//		0001		32767 + (-1)
//		0011		(-32767) + (-32767)
//		0100		0 + 0
//		0101		1 + (-1)
//		0111		(-32768) + (-32768)
//		1000		1 + (-2)
//		1001		(-1) + (-1)
//		1010		1 + 32767
//////////////////////////////////////////////////
SetConditionCodes:
	and	r3,r3,0b1111
	ld	r3,ConditionCodeTable,r3
	bra	r3
SetConditionCode0000:
	mov	r3,1
	add	r3,r3,1
	bra	SetConditionCodes_Return
SetConditionCode0001:
	mov	r3,32767
	add	r3,r3,-1
	bra	SetConditionCodes_Return
SetConditionCode0011:
	mov	r3,-32767
	add	r3,r3,-32767
	bra	SetConditionCodes_Return
SetConditionCode0100:
	mov	r3,0
	add	r3,r3,0
	bra	SetConditionCodes_Return
SetConditionCode0101:
	mov	r3,1
	add	r3,r3,-1
	bra	SetConditionCodes_Return
SetConditionCode0111:
	mov	r3,-32768
	add	r3,r3,-32768
	bra	SetConditionCodes_Return
SetConditionCode1000:
	mov	r3,1
	add	r3,r3,-2
	bra	SetConditionCodes_Return
SetConditionCode1001:
	mov	r3,-1
	add	r3,r3,-1
	bra	SetConditionCodes_Return
SetConditionCode1010:
	mov	r3,1
	add	r3,r3,32767
	bra	SetConditionCodes_Return
UndefinedConditionCode:
	bra	@
SetConditionCodes_Return:
	jsr	r6,r6
	bra	SetConditionCodes

_code_section = @
@ = _data_section

ConditionCodeTable:
	SetConditionCode0000
	SetConditionCode0001
	UndefinedConditionCode
	SetConditionCode0011
	SetConditionCode0100
	SetConditionCode0101
	UndefinedConditionCode
	SetConditionCode0111
	SetConditionCode1000
	SetConditionCode1001
	SetConditionCode1010
	UndefinedConditionCode
	UndefinedConditionCode
	UndefinedConditionCode
	UndefinedConditionCode
	UndefinedConditionCode

_data_section = @
@ = _code_section


#endif