Guest User

Untitled

a guest
Mar 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. package spinal.lib.misc
  2. import spinal.core._
  3. import spinal.lib._
  4. import spinal.lib.bus.misc.BusSlaveFactory
  5.  
  6.  
  7. case class Timer(width : Int) extends Component{
  8. val io = new Bundle {
  9. val tick = in Bool
  10. val clear = in Bool
  11. val limit = in UInt (width bits)
  12.  
  13. val full = out Bool
  14. val value = out UInt (width bits)
  15. }
  16. val counter = Reg(UInt(width bits))
  17. val limitHit = counter === io.limit
  18. val inhibitFull = RegInit(False)
  19. when(io.tick){
  20. inhibitFull := limitHit
  21. counter := counter + (!limitHit).asUInt
  22. }
  23. when(io.clear){
  24. counter := 0
  25. inhibitFull := False
  26. }
  27. io.full := limitHit && io.tick && !inhibitFull
  28. io.value := counter
  29.  
  30.  
  31. def driveFrom(busCtrl : BusSlaveFactory,baseAddress : BigInt)
  32. (ticks : Seq[Bool],clears : Seq[Bool]) = new Area {
  33. //Address 0 => clear/tick masks + bus
  34. val ticksEnable = busCtrl.createReadAndWrite(Bits(ticks.length bits) ,baseAddress + 0,0) init(0)
  35. val clearsEnable = busCtrl.createReadAndWrite(Bits(clears.length bits),baseAddress + 0,16) init(0)
  36. val busClearing = False
  37.  
  38. //Address 4 => read/write limit (+ auto clear)
  39. busCtrl.driveAndRead(io.limit,baseAddress + 4)
  40. busClearing.setWhen(busCtrl.isWriting(baseAddress + 4))
  41.  
  42. //Address 8 => read timer value / write => clear timer value
  43. busCtrl.read(io.value,baseAddress + 8)
  44. busClearing.setWhen(busCtrl.isWriting(baseAddress + 8))
  45.  
  46. io.clear := (clearsEnable & clears.asBits).orR | busClearing
  47. io.tick := (ticksEnable & ticks.asBits ).orR
  48. }
  49. }
Add Comment
Please, Sign In to add comment