You are given a GPU firmware header represented as a list of 32-bit unsigned integers called header, where each element is one word of the header.
You are also given a list of field specifications called fields. Each field is a list of three integers [word_index, bit_offset, bit_width] that describes how to extract a value from the header:
-
word_index
: the index into
header
to read from
-
bit_offset
: the starting bit position within that word (0 is the least significant bit)
-
bit_width
: the number of bits to extract
For each field specification, extract the value by taking header[word_index], right-shifting it by bit_offset, and masking it to bit_width bits. Return a list of the extracted integer values in the same order as the field specifications.
This simulates parsing the packed binary structures found in firmware headers, device registers and hardware configuration blocks, where several fields are packed into a single 32-bit word.
Function Signature
def parse_firmware_header(header: list[int], fields: list[list[int]]) -> list[int]:
Rules
-
Bits are numbered from 0 (least significant) to 31 (most significant). A field covers bits
bit_offset
through
bit_offset + bit_width - 1
of its word.
-
Words are unsigned, and every extracted value is returned as a non-negative integer. A 32-bit field with all bits set is
4294967295
, never
-1
.
-
Each field is extracted independently. Several fields may read the same word.
-
The output has one value per field specification, in the order of
fields
.
Constraints
-
1 <= len(header) <= 64
-
0 <= header[i] <= 2^32 - 1
-
1 <= len(fields) <= 100
-
Each field is
[word_index, bit_offset, bit_width]
-
0 <= word_index < len(header)
-
0 <= bit_offset <= 31
-
1 <= bit_width <= 32
-
bit_offset + bit_width <= 32
Examples
Input: header = [4294967295, 305419896, 2863311530], fields = [[0, 0, 8], [0, 8, 8], [1, 0, 16], [1, 16, 16], [2, 0, 32], [2, 1, 1]]
Output: [255, 255, 22136, 4660, 2863311530, 1]
header[0] is 0xFFFFFFFF, so bits 0-7 and bits 8-15 are both 255, and header[1] is 0x12345678, so bits 0-15 are 0x5678 = 22136 and bits 16-31 are 0x1234 = 4660. header[2] is 0xAAAAAAAA (binary 1010...1010), so all 32 bits give 2863311530 and bit 1 alone is 1.
Input: header = [0, 4294967295], fields = [[0, 0, 32], [1, 0, 32], [0, 0, 1], [1, 31, 1]]
Output: [0, 4294967295, 0, 1]
Every bit of header[0] is 0, so both fields read from it return 0. header[1] is 0xFFFFFFFF, so its full 32-bit value is 4294967295 and its top bit (bit 31) is 1.
Input: header = [305419896], fields = [[0, 4, 8], [0, 3, 5]]
Output: [103, 15]
305419896 is 0x12345678; shifting it right by 4 gives 0x1234567, whose low 8 bits are 0x67 = 103. The low byte 0x78 is 01111000 in binary, so bits 3-7 are 01111 = 15.