-
Notifications
You must be signed in to change notification settings - Fork 1
/
SevenSegment.scala
176 lines (145 loc) · 4.45 KB
/
SevenSegment.scala
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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package MySpinalHardware
import spinal.core._
import spinal.lib.{Counter, Timeout}
import spinal.core.sim._
import scala.util.control.Breaks
/*
Segments to bits:
Bit 0 - a A
Bit 1 - b ____
Bit 2 - c F| |B
Bit 3 - d -G--
Bit 4 - e E| |C
Bit 5 - f ----
Bit 6 - g D *DP
Bit 7 - dp
*/
object SevenSegmentDriver {
def apply(NumberOfDisplays: BigInt, CycleSpeed: BigInt) : SevenSegmentDriver = new SevenSegmentDriver(NumberOfDisplays, CycleSpeed)
def apply(NumberOfDisplays: BigInt, time: TimeNumber) : SevenSegmentDriver = new SevenSegmentDriver(NumberOfDisplays, (time*ClockDomain.current.frequency.getValue).toBigInt())
}
class SevenSegmentDriver(val NumberOfDisplays: BigInt = 1, val CycleSpeed: BigInt = 1) extends ImplicitArea[Bits] { // extends Component {
val decPoint = Reg(UInt(log2Up(NumberOfDisplays) bit)) init (0)
val dp = Bool
val segments = Bits(8 bit)
val displays = Bits(NumberOfDisplays + 1 bit)
val timer = Timeout(CycleSpeed)
val displayCounter = Counter(0, 1 + (NumberOfDisplays << 1))
val digits = Vec(Reg(Bits(4 bit)) init(0), NumberOfDisplays.toInt + 1)
val digit = Bits(4 bit)
val digit2segments = Vec(
Seq(
B"0111111", //0
B"0000110", //1
B"1011011", //2
B"1001111", //3
B"1100110", //4
B"1101101", //5
B"1111101", //6
B"0000111", //7
B"1111111", //8
B"1101111", //9
B"1110111", //A
B"1111100", //B
B"0111001", //C
B"1011110", //D
B"1111001", //E
B"1110001" //F
).map(c => B(c,7 bit))
)
digit := digits(displayCounter >> 1)
when(decPoint =/= 0){
dp := decPoint-1 === (displayCounter >> 1)
}otherwise {
dp := False
}
when(!displayCounter(0)){
displays := (B"1" << (displayCounter >> 1)).resized
segments := Cat(dp, digit2segments(digit.asUInt))
} otherwise {
displays := 0
segments := 0
}
when(timer) {
timer.clear()
displayCounter.increment()
}
def retset(): Unit = {digits.map(_ := 0)}
def setDigit(digit: Int, number: Bits): Unit = {
digits(digit) := number.resize(4)
}
def setDigits(digit: Int, number: Bits): Unit = {
setDigit(digit, number & 0x0F)
setDigit(digit+1, (number & 0xF0) >> 4)
}
def setDecPoint(digit: Int): Unit ={
decPoint := digit
}
override def implicitValue: Bits = Cat(displays, segments)
}
class SevenSegment() extends ImplicitArea[Bits] { // extends Component {
val segments = Bits(7 bit)
val digit = Bits(4 bit)
val digit2segments = Vec(
Seq(
B"0111111", //0
B"0000110", //1
B"1011011", //2
B"1001111", //3
B"1100110", //4
B"1101101", //5
B"1111101", //6
B"0000111", //7
B"1111111", //8
B"1101111", //9
B"1110111", //A
B"1111100", //B
B"0111001", //C
B"1011110", //D
B"1111001", //E
B"1110001" //F
).map(c => B(c,7 bit))
)
segments := digit2segments(digit.asUInt)
def retset(): Unit = {digit := 0}
def setDigit(number: UInt): Unit = {
digit := number.asBits.resize(4)
}
override def implicitValue: Bits = segments
}
class SevenSegmentDriverTest extends Component {
val io = new Bundle {
val c = in Bits(16 bit)
}
val ssd = SevenSegmentDriver(3, 4)
when(io.c === 0){
ssd.retset();
}
when(io.c === 10){
ssd.setDecPoint(1)
ssd.setDigit(2, 5)
ssd.setDigit(3, 8)
}
}
object SevenSegmentTest {
def main(args: Array[String]) {
SimConfig.withWave.compile{
val dut = new SevenSegmentDriverTest()
dut
}.doSim { dut =>
//Fork a process to generate the reset and the clock on the dut
dut.clockDomain.forkStimulus(period = 10)
dut.clockDomain.waitRisingEdge()
var c = 0;
val loop = new Breaks;
loop.breakable {
while (true) {
dut.clockDomain.waitRisingEdge()
dut.io.c #= c
if(c == 500) loop.break;
c+=1
}
}
}
}
}