xref: /XiangShan/macros/src/main/scala/CSRMacros.scala (revision 01cdded87283f55be427ca849d18baa3e9459c2d)
1package xiangshan.macros
2
3import scala.annotation.compileTimeOnly
4import scala.language.experimental.macros
5import scala.reflect.macros.blackbox.Context
6
7object CSRMacros {
8  object CSRFieldsImpl {
9    private def calcuWidth(c: Context)(msb: c.Expr[Int], lsb: c.Expr[Int]): Int = {
10      import c.universe._
11
12      val i_msb = c.eval(msb)
13      val i_lsb = c.eval(lsb)
14
15      i_msb - i_lsb + 1
16    }
17
18    @compileTimeOnly("")
19    def CSRROFieldRangeWithReset(c: Context)(msb: c.Expr[Int], lsb: c.Expr[Int], rfn: c.Tree, resetVal: c.Tree): c.Tree = {
20      c.parse(s"CSRDefines.CSRField${calcuWidth(c)(msb, lsb)}Bits.RO(${c.eval(msb)}, ${c.eval(lsb)}, $rfn, $resetVal)")
21    }
22
23    @compileTimeOnly("")
24    def CSRROFieldRange(c: Context)(msb: c.Expr[Int], lsb: c.Expr[Int], rfn: c.Tree): c.Tree = {
25      CSRROFieldRangeWithReset(c)(msb, lsb, rfn, null)
26    }
27
28    @compileTimeOnly("")
29    def CSRROFieldBit(c: Context)(bit: c.Expr[Int], rfn: c.Tree): c.Tree = {
30      CSRROFieldRangeWithReset(c)(bit, bit, rfn, null)
31    }
32
33    @compileTimeOnly("")
34    def CSRROFieldRangeNoFn(c: Context)(msb: c.Expr[Int], lsb: c.Expr[Int]): c.Tree = {
35      CSRROFieldRangeWithReset(c)(msb, lsb, null, null)
36    }
37
38    @compileTimeOnly("")
39    def CSRROFieldRangeNoFnWithReset(c: Context)(msb: c.Expr[Int], lsb: c.Expr[Int], resetVal: c.Tree): c.Tree = {
40      CSRROFieldRangeWithReset(c)(msb, lsb, null, resetVal)
41    }
42
43    @compileTimeOnly("")
44    def CSRROFieldBitNoFn(c: Context)(bit: c.Expr[Int]): c.Tree = {
45      CSRROFieldRangeWithReset(c)(bit, bit, null, null)
46    }
47
48    @compileTimeOnly("")
49    def CSRWARLFieldRange(c: Context)(msb: c.Expr[Int], lsb: c.Expr[Int], fn: c.Tree): c.Tree = {
50      c.parse(s"CSRDefines.CSRField${calcuWidth(c)(msb, lsb)}Bits.WARL(${c.eval(msb)}, ${c.eval(lsb)}, $fn)")
51    }
52
53    @compileTimeOnly("")
54    def CSRWARLFieldBit(c: Context)(bit: c.Expr[Int], fn: c.Tree): c.Tree = {
55      CSRWARLFieldRange(c)(bit, bit, fn)
56    }
57
58    @compileTimeOnly("")
59    def CSRRWFieldRange(c: Context)(msb: c.Expr[Int], lsb: c.Expr[Int]): c.Tree = {
60      c.parse(s"CSRDefines.CSRField${calcuWidth(c)(msb, lsb)}Bits.RW(" +
61        s"${c.eval(msb)}, " +
62        s"${c.eval(lsb)}" +
63        s")"
64      )
65    }
66
67    @compileTimeOnly("")
68    def CSRRWFieldBit(c: Context)(bit: c.Expr[Int]): c.Tree = {
69      CSRRWFieldRange(c)(bit, bit)
70    }
71
72    @compileTimeOnly("")
73    def CSRRWFieldRangeWithReset(c: Context)(msb: c.Expr[Int], lsb: c.Expr[Int], resetVal: c.Tree): c.Tree = {
74      c.parse(s"CSRDefines.CSRField${calcuWidth(c)(msb, lsb)}Bits.RW(" +
75        s"${c.eval(msb)}, " +
76        s"${c.eval(lsb)}, " +
77        s"${resetVal}" +
78        s")"
79      )
80    }
81
82    @compileTimeOnly("")
83    def CSRRWFieldBitWithReset(c: Context)(bit: c.Expr[Int], resetVal: c.Tree): c.Tree = {
84      CSRRWFieldRangeWithReset(c)(bit, bit, resetVal)
85    }
86
87    @compileTimeOnly("")
88    def CSRWLRLFieldRange(c: Context)(msb: c.Expr[Int], lsb: c.Expr[Int], fn: c.Tree): c.Tree = {
89      c.parse(s"CSRDefines.CSRField${calcuWidth(c)(msb, lsb)}Bits.WARL(${c.eval(msb)}, ${c.eval(lsb)}, $fn)")
90    }
91
92    @compileTimeOnly("")
93    def CSRWLRLFieldBit(c: Context)(bit: c.Expr[Int], fn: c.Tree): c.Tree = {
94      CSRWLRLFieldRange(c)(bit, bit, fn)
95    }
96
97    @compileTimeOnly("")
98    def CSRRefWARLFieldRange(c: Context)(ref: c.Tree, msb: c.Expr[Int], lsb: c.Expr[Int], wfn: c.Tree): c.Tree = {
99      c.parse(s"CSRDefines.CSRField${calcuWidth(c)(msb, lsb)}Bits.RefWARL($ref, ${c.eval(msb)}, ${c.eval(lsb)}, $wfn)")
100    }
101
102    @compileTimeOnly("")
103    def CSRRefWARLFieldBit(c: Context)(ref: c.Tree, bit: c.Expr[Int], wfn: c.Tree): c.Tree = {
104      CSRRefWARLFieldRange(c)(ref, bit, bit, wfn)
105    }
106  }
107}
108
109
110