1 1.1 riastrad #!/usr/bin/awk -f 2 1.1 riastrad # $NetBSD: mkregtable.awk,v 1.1 2014/07/16 20:59:58 riastradh Exp $ 3 1.1 riastrad # 4 1.1 riastrad # Copyright (c) 2014 The NetBSD Foundation, Inc. 5 1.1 riastrad # All rights reserved. 6 1.1 riastrad # 7 1.1 riastrad # This code is derived from software contributed to The NetBSD Foundation 8 1.1 riastrad # by Taylor R. Campbell. 9 1.1 riastrad # 10 1.1 riastrad # Redistribution and use in source and binary forms, with or without 11 1.1 riastrad # modification, are permitted provided that the following conditions 12 1.1 riastrad # are met: 13 1.1 riastrad # 1. Redistributions of source code must retain the above copyright 14 1.1 riastrad # notice, this list of conditions and the following disclaimer. 15 1.1 riastrad # 2. Redistributions in binary form must reproduce the above copyright 16 1.1 riastrad # notice, this list of conditions and the following disclaimer in the 17 1.1 riastrad # documentation and/or other materials provided with the distribution. 18 1.1 riastrad # 19 1.1 riastrad # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 riastrad # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 riastrad # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 riastrad # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 riastrad # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 riastrad # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 riastrad # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 riastrad # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 riastrad # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 riastrad # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 riastrad # POSSIBILITY OF SUCH DAMAGE. 30 1.1 riastrad 31 1.1 riastrad function howmany(x, n) { 32 1.1 riastrad return int((x + (n - 1)) / n) 33 1.1 riastrad } 34 1.1 riastrad 35 1.1 riastrad function hi(x, n) { 36 1.1 riastrad return int(x / 2^n) 37 1.1 riastrad } 38 1.1 riastrad 39 1.1 riastrad function lo(x, n) { 40 1.1 riastrad return (x % 2^n) 41 1.1 riastrad } 42 1.1 riastrad 43 1.1 riastrad function rcsid(s) { 44 1.1 riastrad sub("^\\$", "", s) 45 1.1 riastrad sub("\\$$", "", s) 46 1.1 riastrad 47 1.1 riastrad return s; 48 1.1 riastrad } 49 1.1 riastrad 50 1.1 riastrad BEGIN { 51 1.1 riastrad state = "INITIAL" 52 1.1 riastrad } 53 1.1 riastrad 54 1.1 riastrad state == "INITIAL" { 55 1.1 riastrad gpu = $1 56 1.1 riastrad maxreg = lastreg = $2 57 1.1 riastrad state = "REGS" 58 1.1 riastrad noffsets = 0 59 1.1 riastrad } 60 1.1 riastrad 61 1.1 riastrad state == "REGS" && 62 1.1 riastrad $1 ~ /0x[0-9a-fA-F]/ && 63 1.1 riastrad $2 ~ /[_a-zA-Z0-9]*/ { 64 1.1 riastrad if (!seen[$1]) { 65 1.1 riastrad seen[$1] = 1 66 1.1 riastrad offset[noffsets++] = $1 67 1.1 riastrad if ($1 > maxreg) 68 1.1 riastrad maxreg = $1 69 1.1 riastrad } 70 1.1 riastrad } 71 1.1 riastrad 72 1.1 riastrad END { 73 1.1 riastrad # We do this in 16-bit arithmetic to avoid overflow in case 74 1.1 riastrad # this ever runs on a system whose awk uses single-precision 75 1.1 riastrad # floats or 32-bit integers or something horrible like that. 76 1.1 riastrad nentries = howmany(hi(maxreg, 2), 32) 77 1.1 riastrad for (i = 0; i < nentries; i++) { 78 1.1 riastrad # No hex numeric literals in awk! 79 1.1 riastrad table_hi[i] = 2^16 - 1 80 1.1 riastrad table_lo[i] = 2^16 - 1 81 1.1 riastrad } 82 1.1 riastrad for (o = 0; o < noffsets; o++) { 83 1.1 riastrad reg = hi(offset[o], 2) 84 1.1 riastrad wi = hi(reg, 5) 85 1.1 riastrad bi = lo(reg, 5) 86 1.1 riastrad # Clear the bit. No bitwise operations, but if the 87 1.1 riastrad # offsets don't overlap, the bit is guaranteed to be 88 1.1 riastrad # set so we can just subtract 2^n. 89 1.1 riastrad if (hi(bi, 4) != 0) 90 1.1 riastrad table_hi[wi] -= 2^lo(bi, 4) 91 1.1 riastrad else 92 1.1 riastrad table_lo[wi] -= 2^bi 93 1.1 riastrad } 94 1.1 riastrad printf("/*\n") 95 1.1 riastrad printf(" *\t%c%s%c\n", "$", "NetBSD", "$") 96 1.1 riastrad printf(" *\n") 97 1.1 riastrad printf(" * Stand back! This file was automagically generated by\n") 98 1.1 riastrad printf(" *\t%s\n", rcsid("$NetBSD: mkregtable.awk,v 1.1 2014/07/16 20:59:58 riastradh Exp $")) 99 1.1 riastrad printf(" */\n") 100 1.1 riastrad printf("\n") 101 1.1 riastrad printf("static const uint32_t %s_reg_safe_bm[%u] = {\n", gpu, nentries) 102 1.1 riastrad for (i = 0; i < nentries; i++) { 103 1.1 riastrad if ((i % 4) == 0) 104 1.1 riastrad printf("\t") 105 1.1 riastrad printf("0x%04X%04X,", table_hi[i], table_lo[i]) 106 1.1 riastrad if (((i % 4) == 3) || (i == (nentries - 1))) 107 1.1 riastrad printf("\n") 108 1.1 riastrad else 109 1.1 riastrad printf(" ") 110 1.1 riastrad } 111 1.1 riastrad printf("};\n") 112 1.1 riastrad } 113