📄 bitdcods.s
字号:
; this gives the number of bits free in the top-up word after the remainder of the decoding
; word has been refilled (which is performed on return)
MOV pc, lr ; return to caller
MEND
;---------------------------------------------------------------
; finish tree walk
;
; tree traversal is completed updating the number of free bits in
; the top-up word after refilling the decoding word and the top-up
; word is refilled if necessary
;
; $codebits register: the number of bits that have been decoded
; $bits register: the number of bits left in the top-up before exhausted
; $datasize the size of the data items being decoded - 8, 16 or 32 bits
;
; $codebits and $bits must be distinct
;
;---------------------------------------------------------------
MACRO
FINISHTREEWALK $codebits, $bits, $datasize
; check registers are distinct
DISTINCT $bits, $codebits
ADDS $bits, $bits, $codebits ; update the number of free bits in the top-up word after it has refilled the decoding word
; the final code word length is set as (255-codeword length) to enable one instruction
; to restore the bits value to that prior to 255 being subtracted for the tree walk
; thus adding the codeword length given adds 255 and then subtracts the actual codeword
; length
MOVLT $codebits, #0 ; if $bits < 0 a refill is going to be performed which will include a shift of the decoding
; word to clear out the codeword data, however this has been performed already thus
; the shift should have no effect, hence clear the length to shift by
IF $datasize = 8
BLT refill_buffer_byte ; if $bits < 0, top-up word needs refilling and return from the top up...
ELSE
IF $datasize = 16
BLT refill_buffer_hword
ELSE
BLT refill_buffer_word
ENDIF
ENDIF
RETURN "","","","" ; ...else return to caller (no rlist, sp, lr, no condition)
MEND
;---------------------------------------------------------------
; decoded current tree node
;
; get the next bit for decoding, load a tree location for this bit
; and if a symbol is reached end tree walk
;
; $srcword register: current 32 bits being decoded
; $symbol register: to hold the decoded symbol
; $lenwalk register: address of decoding length tree
; $symwalk register: address of decoding symbol tree
; $codebits register: to hold number of bits of codeword decoded
; $datasize the size of the data items being decoded - 8, 16 or 32 bits
;
; $srcword and $symbol must be distinct
; $symwalk must be distinct from $symbol
; $srcword and $codebits must be distinct
;
;---------------------------------------------------------------
MACRO
CHECKTREENODE $srcword, $symbol, $lenwalk, $symwalk, $codebits, $datasize
; check registers distinct
DISTINCT $srcword, $symbol
MOVS $srcword, $srcword, LSL #1 ; decode the next bit from the decoding word shifting the bit out into the carry
; and moving all remaining bits up
ADC $symbol, $symbol, $symbol ; $symbol at this point contains the next node pair offset so to actually access
; the correct node, double the offset given and add in the carry which if set
; determines that the right hand node from current parent should be taken
; else take the left hand node
; load the next length and symbol or node offset from current node
LOADSYMBOLANDLENGTH $lenwalk, $symwalk, $symbol, $codebits, $symbol, $datasize, 0
CMP $codebits, #255 ; check if node is internal ($codebits = 255) or leaf node (otherwise)
IF $datasize = 8
BNE finished_walk_byte ; if $codebits <> 255, then found leaf node with decoded data, so complete walk
ELSE
IF $datasize = 16
BNE finished_walk_hword
ELSE
BNE finished_walk_word
ENDIF
ENDIF
MEND
;---------------------------------------------------------------
; tree walk
;
; traverse the tree decoding a codeword one bit at a time until
; a symbol is reached
;
; $lenwalk register: address of decoding length tree
; $symwalk register: address of decoding symbol tree
; $srcword register: current 32 bits being decoded
; $symbol register: to hold the decoded symbol
; $codebits register: to hold number of bits of codeword decoded
; $bits register: the number of bits left in the top-up word before exhausted
; $datasize the size of the data items being decoded - 8, 16 or 32 bits
;
; $codebits and $bits must be distinct
;
;---------------------------------------------------------------
MACRO
TREEWALK $lenwalk, $symwalk, $srcword, $symbol, $codebits, $bits, $datasize
ASSERT ( $datasize = 8 :LOR: $datasize = 16 :LOR: $datasize = 32 ) ; check size of data given
MOV $srcword, $srcword, LSL #TABLEBITS ; the top TABLEBITS of the decoding word have been used to determine the branch
; to this point to perform a tree walk, so clear these out moving the remaining
; bits to the top of the word
; while a symbol (leaf node) has not been reached, traverse the tree one bit a time
; the decoding word will not need refilling during the traversal since initially it was packed with 32 bits and a codeword
; can have a maximum length of 27 bits
0 ; tree_walk_check_nodes
CHECKTREENODE $srcword, $symbol, $lenwalk, $symwalk, $codebits, $datasize
CHECKTREENODE $srcword, $symbol, $lenwalk, $symwalk, $codebits, $datasize
CHECKTREENODE $srcword, $symbol, $lenwalk, $symwalk, $codebits, $datasize
CHECKTREENODE $srcword, $symbol, $lenwalk, $symwalk, $codebits, $datasize
B %BT0 ; tree_walk_check_nodes ; loop until codeword decoded
MEND
; --------------------
;
; function entry point
;
; --------------------
;---------------------------------------------------------------
; general bit decoding
;
; call the appropriate bit decoding function for given size of data
;
; streamstr register: pointer to a structure that must contain
; an unsigned character array as first entry that is
; the bitstream to decode and an
; unsigned integer as second entry that is the
; bit position in this array to start from
; n register: the number of data items to decode
; dest register: the destination array to hold byte,
; halfword or word symbols
; lentable register: decoding length table
; symtable stacked: decoding symbol table
; datatype stacked 8, 16 or 32 as size of symbol data
;
;---------------------------------------------------------------
IF GENBITDECODE = 1
; define the general bit decoding function as necessary which requires all other bit decoding functions to be defined
EXPORT BitDecodeSymbols
BitDecodeSymbols
LDR R12, [ sp, #4 ] ; get "datatype" variable from stack, leaving stack pointer as reference
; when called which is for the fifth parameter, symtable
CMP R12, #8 ; if "datatype" is 8 then the data is bytes
BEQ BitDecodeByteSymbols
CMP R12, #16 ; if "datatype" is 16 then the data is halfwords
BEQ BitDecodeHalfWordSymbols
CMP R12, #32 ; if "datatype" is 32 then the data is words
; this check is made to ensure that no other size can be given
BEQ BitDecodeWordSymbols
; if this point is reached the value of "datatype" was incorrect so just return
MOV fin_stream, streamstr ; expected return is the state structure so move this into the return register
RETURN "","","","" ; return (no rlist, sp, lr, no condition)
ENDIF
;---------------------------------------------------------------
; byte bit decoding
;
; bit decode variable length codewords to byte symbols
;
; streamstr register: pointer to a structure that must contain
; an unsigned character array as first entry that is
; the bitstream to decode and an
; unsigned integer as second entry that is the
; bit position in this array to start from
; n register: the number of data items to decode
; dest register: the destination array to hold byte symbols
; lentable register: decoding length table
; symtable stacked: decoding symbol table
;
;---------------------------------------------------------------
IF GENBITDECODE = 1 :LOR: BYTEBITDECODE = 1
; define the byte decoding function as necessary
EXPORT BitDecodeByteSymbols
BitDecodeByteSymbols
; initialise the bit decoder - save registers, setup memory addresses and initialise decoding and stream words
INITIALISEBITDECODING streamstr, lentable, symtable, lenwalk, symwalk, bits, source, srcword, word, t, 8
; process the coded data calling the bit decoding for each codeword to get bytes
DECODETOBYTES n, dest, dstword, lentable, symtable, srcword, word, bits, codebits, symbol, t
; end the bit decoder - ensure all data is saved, restore the registers and return
ENDBITDECODING bits, source, fin_stream, t
tree_or_refill_byte
; determine if tree should be traversed for decoding or if top-up word needs refilling
TREEORREFILL bits, 8
refill_buffer_byte
; use the remaining bits in the top-up word then refill it
REFILLBUFFER source, srcword, word, codebits, bits, t
tree_walk_byte
; traverse the tree until the codeword has been decoded to a symbol
TREEWALK lenwalk, symwalk, srcword, symbol, codebits, bits, 8
finished_walk_byte
; end the tree traversal refilling the top-up word as necessary
FINISHTREEWALK codebits, bits, 8
ENDIF
;---------------------------------------------------------------
; halfword bit decoding
;
; bit decode variable length codewords to halfword symbols
;
; streamstr register: pointer to a structure that must contain
; an unsigned character array as first entry that is
; the bitstream to decode and an
; unsigned integer as second entry that is the
; bit position in this array to start from
; n register: the number of data items to decode
; dest register: the destination array to hold halfword symbols
; lentable register: decoding length table
; symtable stacked: decoding symbol table
;
;---------------------------------------------------------------
IF GENBITDECODE = 1 :LOR: HWORDBITDECODE = 1
; define the halfword decoding function as necessary
EXPORT BitDecodeHalfWordSymbols
BitDecodeHalfWordSymbols
; initialise the bit decoder - save registers, setup memory addresses and initialise decoding and stream words
INITIALISEBITDECODING streamstr, lentable, symtable, lenwalk, symwalk, bits, source, srcword, word, t, 16
; process the coded data calling the bit decoding for each codeword to get halfwords
DECODETOHWORDS n, dest, dstword, lentable, symtable, srcword, word, bits, codebits, symbol, t
; end the bit decoder - ensure all data is saved, restore the registers and return
ENDBITDECODING bits, source, fin_stream, t
tree_or_refill_hword
; determine if tree should be traversed for decoding or if top-up word needs refilling
TREEORREFILL bits, 16
refill_buffer_hword
; use the remaining bits in the top-up word then refill it
REFILLBUFFER source, srcword, word, codebits, bits, t
tree_walk_hword
; traverse the tree until the codeword has been decoded to a symbol
TREEWALK lenwalk, symwalk, srcword, symbol, codebits, bits, 16
finished_walk_hword
; end the tree traversal refilling the top-up word as necessary
FINISHTREEWALK codebits, bits, 16
ENDIF
;---------------------------------------------------------------
; word bit decoding
;
; bit decode variable length codewords to word symbols
;
; streamstr register: pointer to a structure that must contain
; an unsigned character array as first entry that is
; the bitstream to decode and an
; unsigned integer as second entry that is the
; bit position in this array to start from
; n register: the number of data items to decode
; dest register: the destination array to hold word symbols
; lentable register: decoding length table
; symtable stacked: decoding symbol table
;
;---------------------------------------------------------------
IF GENBITDECODE = 1 :LOR: WORDBITDECODE = 1
; define the word decoding function as necessary
EXPORT BitDecodeWordSymbols
BitDecodeWordSymbols
; initialise the bit decoder - save registers, setup memory addresses and initialise decoding and stream words
INITIALISEBITDECODING streamstr, lentable, symtable, lenwalk, symwalk, bits, source, srcword, word, t, 32
; process the coded data calling the bit decoding for each codeword to get words
DECODETOWORDS n, dest, dstword, lentable, symtable, srcword, word, bits, codebits, symbol, t
; end the bit decoder - ensure all data is saved, restore the registers and return
ENDBITDECODING bits, source, fin_stream, t
tree_or_refill_word
; determine if tree should be traversed for decoding or if top-up word needs refilling
TREEORREFILL bits, 32
refill_buffer_word
; use the remaining bits in the top-up word then refill it
REFILLBUFFER source, srcword, word, codebits, bits, t
tree_walk_word
; traverse the tree until the codeword has been decoded to a symbol
TREEWALK lenwalk, symwalk, srcword, symbol, codebits, bits, 32
finished_walk_word
; end the tree traversal refilling the top-up word as necessary
FINISHTREEWALK codebits, bits, 32
ENDIF
END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -