aboutsummaryrefslogtreecommitdiff
path: root/VexRiscv/src/main/scala/vexriscv/demo/CustomCsrDemoPlugin.scala
blob: a763c837ed0094de86150c463c80d9fd6a9c0f7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
package vexriscv.demo

import spinal.core._
import spinal.lib.io.TriStateArray
import spinal.lib.{Flow, master}
import vexriscv.plugin.{CsrInterface, Plugin}
import vexriscv.{DecoderService, Stageable, VexRiscv}



class CustomCsrDemoPlugin extends Plugin[VexRiscv]{
  override def build(pipeline: VexRiscv): Unit = {
    import pipeline._
    import pipeline.config._

    pipeline plug new Area{
      val instructionCounter = Reg(UInt(32 bits))
      val cycleCounter = Reg(UInt(32 bits))

      cycleCounter := cycleCounter + 1
      when(writeBack.arbitration.isFiring) {
        instructionCounter := instructionCounter + 1
      }

      val csrService = pipeline.service(classOf[CsrInterface])
      csrService.rw(0xB04, instructionCounter)
      csrService.r(0xB05, cycleCounter)
      csrService.onWrite(0xB06){
        instructionCounter := 0
      }
      csrService.onRead(0xB07){
        instructionCounter := 0x40000000
      }
    }
  }
}


class CustomCsrDemoGpioPlugin extends Plugin[VexRiscv]{
  var gpio : TriStateArray = null


  override def setup(pipeline: VexRiscv): Unit = {
    gpio = master(TriStateArray(32 bits)).setName("gpio")
  }

  override def build(pipeline: VexRiscv): Unit = {
    import pipeline._
    import pipeline.config._

    pipeline plug new Area{
      val writeReg, writeEnableReg = Reg(Bits(32 bits))

      val csrService = pipeline.service(classOf[CsrInterface])
      csrService.rw(0xB08, writeReg)
      csrService.rw(0xB09, writeEnableReg)
      csrService.r(0xB0A, gpio.read)

      gpio.writeEnable := writeEnableReg
      gpio.write := writeReg
    }
  }
}