mpaudio.s
11.5 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
* File: mpaudio.s
*
*/
#include <rsp.h>
#include <rcp.h>
#include <os.h>
#include <sptask.h>
#include <mpaudtask.h>
#include "mpaud.h"
.text TASKBASE # this is coordinated with rspboot.s
.data eval(DCACHEBASE + RSP_PDATA_OFFSET)
#include "mpaud_init.h"
#include "mpaud_regs.h"
######################################################################
#
# Begin task initialization
#
# Register $1 holds the task header address
# format for the task header is defined in os.h
.ent mpmain
.name tskhd, $1
#
# get the command list pointer and size out of the task header
#
lw inp, OS_TASK_OFF_DATA(tskhd)
lw gcount, OS_TASK_OFF_DATA_SZ(tskhd)
#
# start the DMA of the command list into DMEM.
#
jal LoadCommand
nop
.unname tskhd
#
# while DMA is going on, do some initializations:
#
# lqv vconst, VCONST_OFFSET(zero)
lqv vconst[0], 0($0)
########################################################################
#
# THIS CAN BE simplified for the MPEG decoding task -
########################################################################
#########################################################################
#
# Note command processing:
#
# - 'inp' always points to the "next" command in DRAM. This
# is the pointer used to retrieve the next block of commands.
#
# - 'gcount' parallels inp, counting down the remaining commands.
#
# - 'dinp' is the local pointer in DMEM to the part of the commands
# in DMEM.
#
# - 'dlcount' parallels dinp, counting down the remaining commands
# in DMEM.
#
# So when (dlcount == 0) we need to fetch more commands, when
# (gcount == 0), we are done.
#
#########################################################################
DMAWaitCL: mfc0 $2, DMA_BUSY
bne $2, zero, DMAWaitCL
# Delay slot
# process the commands
.name dlcmd, $1
addi dinp, zero, RSP_COMMAND_OFFSET
# Clear the semaphore
mtc0 $0, SP_RESERVED
DecodeCom:
addi inp, inp, RSP_MPAUD_COMMAND_SIZE8
addi gcount, gcount, -RSP_MPAUD_COMMAND_SIZE8
addi dlcount, dlcount, -RSP_MPAUD_COMMAND_SIZE8
ContDecode:
# Jump to command processing
j mpegDecode
nop
break # if we got here, it's an error...
.unname dlcmd
# mpegDecode returns here when its done
AudDone:
bgtz dlcount, DecodeCom
addi dinp, dinp, RSP_MPAUD_COMMAND_SIZE8
# if there are more commands to process, load them, otherwise done
DoneTest: blez gcount, TaskDone
nop
jal LoadCommand
nop
j DMAWaitCL # continue processing
nop
TaskDone: # Done, interrupt the CPU
# Set signal to indicate task done and break
ori $1, zero, SP_SET_TASKDONE
mtc0 $1, SP_STATUS
break
nop
# Spin
MainWait: beq zero, zero, MainWait
nop
#########################################################################
#
# Entry point for the main decoder which decodes a number of sub-frames
# into a buffer.
#
# This code references items from the mpeg decode command pointed to by
# dinp.
#
# NOTES:
# ------
# - Basic steps are:
# 1. Load up the various pointers
# 2. Load up the OLA buffer from DRAM
# 3. Loop over required number of sub-frames
# a. Load coefficients (C,D,scalefactors,input)
# for one sub-frame
# b. Synthesise output
# c. Store output to DRAM
# 4. Save OLA buffer to DRAM
# 5. Done.
#
.name crAddr, $5
.name drAddr, $6
.name scrAddr, $7
mpegDecode:
# Load buffer pointers from the command
lw inAddr, MPEG_IN(dinp)
lw crAddr, MPEG_C_COEF(dinp)
lw drAddr, MPEG_D_COEF(dinp)
lw scrAddr,MPEG_SCALE_COEF(dinp)
lw fCount, MPEG_FRAME_COUNT(dinp)
lw outAddr,MPEG_OUT(dinp)
# Load the OLA buffer and initialise the pointer
# Load the OLA buffer, but put the OLA buffer address somewhere safe
# (the command gets blown away in processing)
lw $2, MPEG_OLA(dinp)
addi $1, zero, RSP_PAR_OFFSET
sw $2, OLA_DRAMBUFF_OFFSET($1)
addi $1, zero, RSP_OLABUFF_HIGH_OFFSET
jal DMAread
addi $3, zero, eval(RSP_OLABUFF_SIZE8*2 - 1) # delay slot
DMAWaitOLA: mfc0 $2, DMA_BUSY
bne $2, zero, DMAWaitOLA
nop
# Clear the semaphore
mtc0 $0, SP_RESERVED
#
# Set up the OLA buffer parameters
#
addi $1, zero, RSP_PAR_OFFSET # point to parameters
addi $2, zero, eval(RSP_OLABUFF_LOW_OFFSET+RSP_OLABUFF_SIZE8-64)
# Only update the low buffer pointer this is checked before updating and
# it will be updated to the beginning first time through
sh $2, 2($1) # Low - points to end
#######################################################
#
# Load up the data for this frame
#
# NOTES:
# ------
# - Inefficient, maybe I can overlay this with code somewhere else
#
# C coefficients
addi $1, zero, RSP_IQC_OFFSET
add $2, zero, crAddr
jal DMAread
addi $3, zero, eval(RSP_IQC_SIZE8-1)
DMAWaitC: mfc0 $2, DMA_BUSY
bne $2, zero, DMAWaitC
nop
# Clear the semaphore
mtc0 $0, SP_RESERVED
addi crAddr, crAddr, RSP_IQC_SIZE8
# D coefficients
addi $1, zero, RSP_IQD_OFFSET
add $2, zero, drAddr
jal DMAread
addi $3, zero, eval(RSP_IQD_SIZE8-1)
DMAWaitD: mfc0 $2, DMA_BUSY
bne $2, zero, DMAWaitD
nop
# Clear the semaphore
mtc0 $0, SP_RESERVED
# Scalefactors
addi $1, zero, RSP_IQSCALE_OFFSET
add $2, zero, scrAddr
jal DMAread
addi $3, zero, eval(RSP_IQSCALE_SIZE8-1)
DMAWaitScale: mfc0 $2, DMA_BUSY
bne $2, zero, DMAWaitScale
nop
# Clear the semaphore
mtc0 $0, SP_RESERVED
nop
.unname crAddr
.unname drAddr
.unname scrAddr
################################################################
#
# Decode sub-frames
#
# NOTES:
# ------
# - At the moment this can usefully do a maximum of 12 sub-frames, since
# after that the Scalefactors (at least) must be updated). I should
# integrate the scale factor updating so that it can do more sub-frames
# in a command. This will save DMAing the OLA buffer to often.
mpegLoop:
beq fCount, zero, mpegDone
addi fCount, fCount, -1
# Load the input coefficients
addi $1, zero, RSP_IQIN_OFFSET
add $2, zero, inAddr
jal DMAread
addi $3, zero, eval(RSP_IQIN_SIZE8-1)
DMAWaitin: mfc0 $2, DMA_BUSY
bne $2, zero, DMAWaitin
nop
# Clear the semaphore
mtc0 $0, SP_RESERVED
# Update input pointer
addi inAddr, inAddr, RSP_IQIN_SIZE8
# Decode this sub-frame
jal IQuant # Entry point - other modules
# are chained at end
nop
# Save the output to DRAM
# Rescaled output has been put at RSP_DCTIN_OFFSET
decodeDone:
addi $1, zero, RSP_DCTIN_OFFSET
add $2, zero, outAddr
jal DMAwrite
addi $3, zero, eval(64 - 1) # delay slot
DMAWaitOut: mfc0 $2, DMA_BUSY
bne $2, zero, DMAWaitOut
nop
# Clear the semaphore
mtc0 $0, SP_RESERVED
addi outAddr, outAddr, 64
j mpegLoop
nop
# Save the OLA buffer to DRAM.
# Break it into two parts and save it so that it can be restored
# as a contiguous buffer into DMEM (saves storing the buffer pointer
# somewhere in DRAM)
.name pBase, $5
.name bAddrh, $6
.name bAddrl, $7
.name bEnd, $8
.name tmp0, $9
.name tmp1, $10
mpegDone:
addi pBase, zero, RSP_PAR_OFFSET # point to parameters
lhu bAddrh, 0(pBase)
lhu bAddrl, 2(pBase)
addi bEnd, zero, eval(RSP_OLABUFF_LOW_OFFSET+RSP_OLABUFF_SIZE8-1)
addi bAddrl, bAddrl, 64
sub tmp0, bEnd, bAddrl
bgtz tmp0, SaveCont
addi bAddrh, bAddrh, 64 # delay slot
addi bAddrl, zero, RSP_OLABUFF_LOW_OFFSET
addi bAddrh, zero, RSP_OLABUFF_HIGH_OFFSET
SaveCont:
# The OLA buffer is stored as separate high and low buffers with
# the high buffer stored first and the low buffer straight after it
lw $2, OLA_DRAMBUFF_OFFSET(pBase)
add $1, zero, bAddrh
sub $3, bEnd, bAddrl # NOTE: bEnd has -1 subtracted
jal DMAwrite
nop
# Note the bEnd parameter is in terms of the low buffer address
DMAWaitOLAS1: mfc0 $1, DMA_BUSY
bne $1, zero, DMAWaitOLAS1
nop
# Clear the semaphore
mtc0 $0, SP_RESERVED
add $1, zero, bAddrl
# Point to the low buffer - NOTE: $2 and $3 must be saved from
# the previous loads
jal DMAwrite
addi $2, $2, RSP_OLABUFF_SIZE8
DMAWaitOLAS2: mfc0 $1, DMA_BUSY
bne $1, zero, DMAWaitOLAS2
nop
mtc0 $0, SP_RESERVED
# Now store the second halves of the buffers (if necessary)
addi $3, $3, 1 # Fix it up to the real length
addi tmp0, zero, RSP_OLABUFF_SIZE8
sub tmp0, tmp0, $3
beq tmp0, zero, OLASaveDone
nop
# Restore buffer address to the beginning of the buffer
# tmp0 now has the length which should be saved, $3
# has the length which has just been saved
addi bAddrl, zero, RSP_OLABUFF_LOW_OFFSET
addi bAddrh, zero, RSP_OLABUFF_HIGH_OFFSET
lw tmp1, OLA_DRAMBUFF_OFFSET(pBase)
add tmp1, tmp1, $3
add $1, zero, bAddrh
add $2, tmp1, zero
add $3, zero, tmp0
jal DMAwrite
addi $3, $3, -1 # delay slot
# Note the bEnd parameter is in terms of the low buffer address
DMAWaitOLAS3: mfc0 $1, DMA_BUSY
bne $1, zero, DMAWaitOLAS3
nop
mtc0 $0, SP_RESERVED
add $1, zero, bAddrl
# Point to the low buffer - NOTE: $2 and $3 must be saved from
# the previous loads
jal DMAwrite
addi $2, $2, RSP_OLABUFF_SIZE8 # delay slot
DMAWaitOLAS4: mfc0 $2, DMA_BUSY
bne $2, zero, DMAWaitOLAS4
nop
mtc0 $0, SP_RESERVED
nop
# Done
OLASaveDone:
j AudDone
nop
break
.unname pBase
.unname bAddrh
.unname bAddrl
.unname bEnd
.unname tmp0
.unname tmp1
.end mpmain
#include "iquant.s"
#include "dct32.s"
#include "remap.s"
#include "ola.s"
#########################################################################
#
# start the DMA of the commands into DMEM.
#
# Registers upon call:
# inp pointer to read from
# gcount max size to read (bytes)
# return where to go when we're done
#
# Registers upon return:
# dlcount size actually read (bytes)
# dinp pointer to data we read in
#
# Registers used:
# $1, $2, $3, $4, $5, return, gcount, inp, dlcount, dinp
#
.ent LoadCommand
.name dmaddr, $1
.name draddr, $2
.name dma_size, $3
.name tmp, $4
.name save_ret, $5
###### CAN be simplified to always fill the local command buffer....
LoadCommand: addi save_ret, return, 0
add draddr, zero, inp
addi dma_size, gcount, 0
addi tmp, dma_size, eval(-1 * RSP_COMMAND_SIZE8)
blez tmp, WillFit
addi dmaddr, zero, RSP_COMMAND_OFFSET
addi dma_size, zero, eval(RSP_COMMAND_SIZE8)
WillFit: addi dlcount, dma_size, 0
jal DMAread
addi dma_size, dma_size, -1
addi dinp, zero, RSP_COMMAND_OFFSET
jr save_ret
nop
.unname dmaddr
.unname draddr
.unname dma_size
.unname tmp
.unname save_ret
.end LoadCommand
#
#
#
#########################################################################
#########################################################################
#
# DMA read and write routines. These routines initiate a DMA read or
# write and then return. It is the responsibility of the calling routine
# to wait on the read finished and to release the semaphore
#
# Passed parameters: $1 DMEM address
# $2 DRAM address
# $3 Read/Write length
#
#########################################################################
.name tmp, $4
###################
# DMA read
###################
.ent DMAread
# Get a semaphore
DMAread: mfc0 tmp, SP_RESERVED
bne tmp, $0, DMAread
nop
# Wait till DMA not full
DRFull1: mfc0 tmp, DMA_FULL
bne tmp, $0, DRFull1
nop
# Initiate the read
mtc0 $1, DMA_CACHE
mtc0 $2, DMA_DRAM
mtc0 $3, DMA_READ_LENGTH
jr return
nop
.end DMAread
###################
# DMA write
###################
.ent DMAwrite
# Get a semaphore
DMAwrite: mfc0 tmp, SP_RESERVED
bne tmp, $0, DMAwrite
nop
# Wait till DMA not full
DRFull2: mfc0 tmp, DMA_FULL
bne tmp, $0, DRFull2
nop
# Initiate the read
mtc0 $1, DMA_CACHE
mtc0 $2, DMA_DRAM
mtc0 $3, DMA_WRITE_LENGTH
jr return
nop
.end DMAwrite
.unname tmp
.unname vconst