1 from enum
import Enum
, unique
4 from os
.path
import dirname
, join
5 from collections
import namedtuple
7 def find_wiki_file(name
):
8 filedir
= os
.path
.dirname(os
.path
.abspath(__file__
))
9 basedir
= dirname(dirname(dirname(filedir
)))
10 tabledir
= join(basedir
, 'libreriscv')
11 tabledir
= join(tabledir
, 'openpower')
12 tabledir
= join(tabledir
, 'isatables')
14 file_path
= join(tabledir
, name
)
19 file_path
= find_wiki_file(name
)
20 with
open(file_path
, 'r') as csvfile
:
21 reader
= csv
.DictReader(csvfile
)
25 # names of the fields in the tables that don't correspond to an enum
26 single_bit_flags
= ['inv A', 'inv out',
27 'cry out', 'BR', 'sgn ext', 'rsrv', '32b',
28 'sgn', 'lk', 'sgl pipe']
30 # default values for fields in the table
31 default_values
= {'unit': "NONE", 'internal op': "OP_ILLEGAL",
32 'in1': "RA", 'in2': 'NONE', 'in3': 'NONE', 'out': 'NONE',
36 'rc': 'NONE', 'cry in': 'ZERO', 'form': 'NONE'}
39 def get_signal_name(name
):
42 return name
.lower().replace(' ', '_')
44 # this corresponds to which Function Unit (pipeline-with-Reservation-Stations)
45 # is to process and guard the operation. they are roughly divided by having
46 # the same register input/output signature (X-Form, etc.)
94 # supported instructions: make sure to keep up-to-date with CSV files
95 # just like everything else
97 "NONE", "add", "addc", "addco", "adde", "addeo", "addi", "addic", "addic.",
98 "addis", "addme", "addmeo", "addo", "addze", "addzeo", "and", "andc",
99 "andi.", "andis.", "attn", "b", "bc", "bcctr", "bclr", "bctar",
100 "bpermd", "cmp", "cmpb", "cmpeqb", "cmpi", "cmpl", "cmpli", "cmprb",
101 "cntlzd", "cntlzw", "cnttzd", "cnttzw", "crand", "crandc", "creqv",
102 "crnand", "crnor", "cror", "crorc", "crxor", "darn", "dcbf", "dcbst",
103 "dcbt", "dcbtst", "dcbz", "divd", "divde", "divdeo", "divdeu",
104 "divdeuo", "divdo", "divdu", "divduo", "divw", "divwe", "divweo",
105 "divweu", "divweuo", "divwo", "divwu", "divwuo", "eqv", "extsb",
106 "extsh", "extsw", "extswsli", "icbi", "icbt", "isel", "isync",
107 "lbarx", "lbz", "lbzu", "lbzux", "lbzx", "ld", "ldarx", "ldbrx",
108 "ldu", "ldux", "ldx", "lha", "lharx", "lhau", "lhaux", "lhax",
109 "lhbrx", "lhz", "lhzu", "lhzux", "lhzx", "lwa", "lwarx", "lwaux",
110 "lwax", "lwbrx", "lwz", "lwzu", "lwzux", "lwzx", "mcrf", "mcrxr",
111 "mcrxrx", "mfcr/mfocrf", "mfmsr", "mfspr", "modsd", "modsw", "modud",
112 "moduw", "mtcrf/mtocrf", "mtmsr", "mtmsrd", "mtspr", "mulhd", "mulhdu",
113 "mulhw", "mulhwu", "mulld", "mulldo", "mulli", "mullw", "mullwo",
114 "nand", "neg", "nego", "nop", "nor", "or", "orc", "ori", "oris",
115 "popcntb", "popcntd", "popcntw", "prtyd", "prtyw", "rfid", "rldcl",
116 "rldcr", "rldic", "rldicl", "rldicr", "rldimi", "rlwimi", "rlwinm",
117 "rlwnm", "setb", "sim_cfg", "sld", "slw", "srad", "sradi", "sraw",
118 "srawi", "srd", "srw", "stb", "stbcx", "stbu", "stbux", "stbx", "std",
119 "stdbrx", "stdcx", "stdu", "stdux", "stdx", "sth", "sthbrx", "sthcx",
120 "sthu", "sthux", "sthx", "stw", "stwbrx", "stwcx", "stwu", "stwux",
121 "stwx", "subf", "subfc", "subfco", "subfe", "subfeo", "subfic",
122 "subfme", "subfmeo", "subfo", "subfze", "subfzeo", "sync", "td",
123 "tdi", "tw", "twi", "xor", "xori", "xoris",
126 # two-way lookup of instruction-to-index and vice-versa
129 for i
, insn
in enumerate(_insns
):
133 # Internal Operation numbering. Add new opcodes here (FPADD, FPMUL etc.)
136 OP_ILLEGAL
= 0 # important that this is zero (see power_decoder.py)
216 RS
= 4 # for some ALU/Logical operations
234 RS
= 13 # for shiftrot (M-Form)
241 RB
= 2 # for shiftrot (M-Form)
262 class LDSTMode(Enum
):
293 class CROutSel(Enum
):
301 # SPRs - Special-Purpose Registers. See V3.0B Figure 18 p971 and
302 # http://libre-riscv.org/openpower/isatables/sprs.csv
303 # http://bugs.libre-riscv.org/show_bug.cgi?id=261
305 spr_csv
= get_csv("sprs.csv")
306 spr_info
= namedtuple('spr_info', 'SPR priv_mtspr priv_mfspr length idx')
310 info
= spr_info(SPR
=row
['SPR'], priv_mtspr
=row
['priv_mtspr'],
311 priv_mfspr
=row
['priv_mfspr'], length
=int(row
['len']),
313 spr_dict
[int(row
['Idx'])] = info
314 spr_byname
[row
['SPR']] = info
315 fields
= [(row
['SPR'], int(row
['Idx'])) for row
in spr_csv
]
316 SPR
= Enum('SPR', fields
)
327 if __name__
== '__main__':
328 # find out what the heck is in SPR enum :)
329 print ("sprs", len(SPR
))
332 print (SPR
.__members
__['TAR'])
334 print (x
, x
.value
, str(x
), x
.name
)