| 1 | #!/usr/bin/env bash |
| 2 | # SPDX-License-Identifier: BSD-2-Clause |
| 3 | |
| 4 | if [ -z "$1" ] || [ -z "$2" ]; then |
| 5 | printf "Usage: %s <output file> \"<microcode .h files>\"\\n" "$0" |
| 6 | fi |
| 7 | |
| 8 | OUTFILE=$1 |
| 9 | TMPFILE=$(mktemp microcode_XXXX) |
| 10 | cat > "${TMPFILE}.c" << EOF |
| 11 | #include <stdio.h> |
| 12 | unsigned int microcode[] = { |
| 13 | EOF |
| 14 | |
| 15 | include_file() { |
| 16 | if [ "${1: -4}" == ".inc" ]; then |
| 17 | sed '/^;/d' <"$1" | awk '{gsub( /h.*$/, "", $2 ); print "0x" $2 ","; }' \ |
| 18 | >> "${TMPFILE}.c" |
| 19 | else |
| 20 | echo "#include \"$1\"" >> "${TMPFILE}.c" |
| 21 | fi |
| 22 | } |
| 23 | |
| 24 | for UCODE in ${@:2}; do |
| 25 | if [ -d "$UCODE" ]; then |
| 26 | for f in "$UCODE/"*.inc |
| 27 | do |
| 28 | include_file "$f" |
| 29 | done |
| 30 | else |
| 31 | include_file "$UCODE" |
| 32 | fi |
| 33 | done |
| 34 | |
| 35 | cat >> "${TMPFILE}.c" << EOF |
| 36 | }; |
| 37 | int main(void) |
| 38 | { |
| 39 | FILE *f = fopen("$OUTFILE", "wb"); |
| 40 | fwrite(microcode, sizeof(microcode), 1, f); |
| 41 | fclose(f); |
| 42 | return 0; |
| 43 | } |
| 44 | EOF |
| 45 | |
| 46 | gcc -o "$TMPFILE" "${TMPFILE}.c" |
| 47 | [ -f "${TMPFILE}.exe" ] && mv "${TMPFILE}.exe" "$TMPFILE" |
| 48 | "./$TMPFILE" |
| 49 | rm "$TMPFILE" "${TMPFILE}.c" |