Axi4.scala 6 KB
Newer Older
Jens Korinth's avatar
Jens Korinth committed
1
2
3
4
5
6
7
8
9
10
package chisel.axi
import  chisel3._
import  chisel3.util._

object Axi4 {
  class Configuration(val addrWidth:   AddrWidth,
                      val dataWidth:   DataWidth,
                      val idWidth:     IdWidth     = IdWidth(1),
                      val userWidth:   UserWidth   = UserWidth(0),
                      val regionWidth: RegionWidth = RegionWidth(0),
11
12
13
14
                      val hasQoS:      Boolean     = false) {
    override def toString: String =
      s"Axi4($addrWidth $dataWidth $idWidth $userWidth $regionWidth $hasQoS)"
  }
Jens Korinth's avatar
Jens Korinth committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

  object Configuration {
    def apply(addrWidth:   AddrWidth,
              dataWidth:   DataWidth,
              idWidth:     IdWidth     = IdWidth(1),
              userWidth:   UserWidth   = UserWidth(0),
              regionWidth: RegionWidth = RegionWidth(0),
              hasQoS:      Boolean     = false): Configuration =
      new Configuration(addrWidth, dataWidth, idWidth, userWidth, regionWidth, hasQoS)

    def unapply(c: Configuration): Option[Tuple6[AddrWidth, DataWidth, IdWidth, UserWidth, RegionWidth, Boolean]] =
      Some((c.addrWidth, c.dataWidth, c.idWidth, c.userWidth, c.regionWidth, c.hasQoS))
  }

  implicit class fromIntToSize(size: Int) {
    import Burst.Size._
    def BSZ: UInt = size match {
      case 1   => s1
      case 4   => s4
      case 8   => s8
      case 16  => s16
      case 32  => s32
      case 64  => s64
      case 128 => s128
      case _   => throw new Exception("invalid size, only 0 < powers of 2 <= 128")
    }
  }

  class Burst extends Bundle {
    val burst = UInt(2.W)
    val len   = UInt(8.W)
    val size  = UInt(3.W)
  }

  object Burst extends Bundle {
    object Type {
      val fixed :: incr :: wrap :: Nil = Enum(3)
    }
    object Size {
      val s1 :: s2 :: s4 :: s8 :: s16 :: s32 :: s64 :: s128 :: Nil = Enum(8)
    }
  }

  class Lock extends Bundle {
    val lock = UInt(2.W)
  }

  object Lock extends Bundle {
    object Access {
      val normal :: exclusive :: Nil = Enum(2)
    }
  }

  class Cache extends Bundle {
    val cache = UInt(4.W)
  }

  object Cache {
    object Read {
      final val DEVICE_NON_BUFFERABLE: UInt               = "b0000".U(4.W)
      final val DEVICE_BUFFERABLE: UInt                   = "b0001".U(4.W)
      final val NORMAL_NON_CACHEABLE_NON_BUFFERABLE: UInt = "b0010".U(4.W)
      final val NORMAL_NON_CACHEABLE_BUFFERABLE: UInt     = "b0011".U(4.W)
      final val WRITE_THROUGH_NO_ALLOCATE: UInt           = "b0110".U(4.W)
      final val WRITE_THROUGH_READ_ALLOCATE: UInt         = "b0110".U(4.W)
      final val WRITE_THROUGH_WRITE_ALLOCATE: UInt        = "b0110".U(4.W)
      final val WRITE_THROUGH_RW_ALLOCATE: UInt           = "b1110".U(4.W)
      final val WRITE_BACK_NO_ALLOCATE: UInt              = "b0111".U(4.W)
      final val WRITE_BACK_READ_ALLOCATE: UInt            = "b0111".U(4.W)
      final val WRITE_BACK_WRITE_ALLOCATE: UInt           = "b1111".U(4.W)
      final val WRITE_BACK_RW_ALLOCATE: UInt              = "b1111".U(4.W)
    }

    object Write {
      final val DEVICE_NON_BUFFERABLE: UInt               = "b0000".U(4.W)
      final val DEVICE_BUFFERABLE: UInt                   = "b0001".U(4.W)
      final val NORMAL_NON_CACHEABLE_NON_BUFFERABLE: UInt = "b0010".U(4.W)
      final val NORMAL_NON_CACHEABLE_BUFFERABLE: UInt     = "b0011".U(4.W)
      final val WRITE_THROUGH_NO_ALLOCATE: UInt           = "b1010".U(4.W)
      final val WRITE_THROUGH_READ_ALLOCATE: UInt         = "b1110".U(4.W)
      final val WRITE_THROUGH_WRITE_ALLOCATE: UInt        = "b1010".U(4.W)
      final val WRITE_THROUGH_RW_ALLOCATE: UInt           = "b1110".U(4.W)
      final val WRITE_BACK_NO_ALLOCATE: UInt              = "b1011".U(4.W)
      final val WRITE_BACK_READ_ALLOCATE: UInt            = "b1111".U(4.W)
      final val WRITE_BACK_WRITE_ALLOCATE: UInt           = "b1011".U(4.W)
      final val WRITE_BACK_RW_ALLOCATE: UInt              = "b1111".U(4.W)
    }
  }

  class Address(implicit cfg: Configuration) extends Bundle {
    val id     = UInt(cfg.idWidth)
    val addr   = UInt(cfg.addrWidth)
    val burst  = new Burst
    val lock   = new Lock
    val cache  = new Cache
    val prot   = new Protection
    val qos    = UInt(if (cfg.hasQoS) 4.W else 0.W)
    val region = UInt(cfg.regionWidth)
    val user   = UInt(cfg.userWidth)

    override def cloneType = { new Address()(cfg).asInstanceOf[this.type] }
  }

  object Data {
    abstract private[axi] class DataChannel(implicit cfg: Configuration) extends Bundle {
      val id    = UInt(cfg.idWidth)
      val data  = UInt(cfg.dataWidth)
      val last  = Bool()
      val user  = UInt(cfg.userWidth)
    }

    class Read(implicit cfg: Configuration) extends DataChannel {
      val resp  = UInt(2.W)

      override def cloneType = { new Read()(cfg).asInstanceOf[this.type] }
    }

    class Write(implicit cfg: Configuration) extends DataChannel {
Jens Korinth's avatar
Jens Korinth committed
133
      val strb  = new Strobe(cfg.dataWidth)
Jens Korinth's avatar
Jens Korinth committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147

      override def cloneType = { new Write()(cfg).asInstanceOf[this.type] }
    }
  }

  class WriteResponse(implicit cfg: Configuration) extends Bundle {
    val bid   = UInt(cfg.idWidth)
    val buser = UInt(cfg.userWidth)
    val bresp = UInt(2.W)

    override def cloneType = { new WriteResponse()(cfg).asInstanceOf[this.type] }
  }

  class Master private (implicit cfg: Configuration) extends Bundle {
148
149
150
151
152
    val writeAddr = Irrevocable(new Address)
    val writeData = Irrevocable(new Data.Write)
    val writeResp = Flipped(Irrevocable(new WriteResponse))
    val readAddr  = Irrevocable(new Address)
    val readData  = Flipped(Irrevocable(new Data.Read))
Jens Korinth's avatar
Jens Korinth committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

    override def cloneType = { new Master()(cfg).asInstanceOf[this.type] }
  }

  object Master {
    def apply(implicit cfg: Configuration): Master = new Master
  }

  object Slave {
    def apply(implicit cfg: Configuration) = Flipped(Master(cfg))
  }

  class Dummy extends Module {
    private implicit val cfg = Configuration(AddrWidth(32), DataWidth(64), IdWidth(3), UserWidth(1), RegionWidth(2))
    val io = IO(new Bundle {
      val m_axi = Master(cfg)
      val s_axi = Slave(cfg)
    })
    io.s_axi <> io.m_axi
  }
}