forked from amaranth-lang/amaranth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_lib_scheduler.py
96 lines (78 loc) · 3.11 KB
/
test_lib_scheduler.py
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
# amaranth: UnusedElaboratable=no
import unittest
from amaranth.hdl import *
from amaranth.asserts import *
from amaranth.sim import *
from amaranth.lib.scheduler import *
from .utils import *
class RoundRobinTestCase(unittest.TestCase):
def test_count(self):
dut = RoundRobin(count=32)
self.assertEqual(dut.count, 32)
self.assertEqual(len(dut.requests), 32)
self.assertEqual(len(dut.grant), 5)
def test_wrong_count(self):
with self.assertRaisesRegex(ValueError, r"Count must be a non-negative integer, not 'foo'"):
dut = RoundRobin(count="foo")
with self.assertRaisesRegex(ValueError, r"Count must be a non-negative integer, not -1"):
dut = RoundRobin(count=-1)
class RoundRobinSimulationTestCase(unittest.TestCase):
def test_count_one(self):
dut = RoundRobin(count=1)
sim = Simulator(dut)
def process():
yield dut.requests.eq(0)
yield; yield Delay(1e-8)
self.assertEqual((yield dut.grant), 0)
self.assertFalse((yield dut.valid))
yield dut.requests.eq(1)
yield; yield Delay(1e-8)
self.assertEqual((yield dut.grant), 0)
self.assertTrue((yield dut.valid))
sim.add_sync_process(process)
sim.add_clock(1e-6)
with sim.write_vcd("test.vcd"):
sim.run()
def test_transitions(self):
dut = RoundRobin(count=3)
sim = Simulator(dut)
def process():
yield dut.requests.eq(0b111)
yield; yield Delay(1e-8)
self.assertEqual((yield dut.grant), 1)
self.assertTrue((yield dut.valid))
yield dut.requests.eq(0b110)
yield; yield Delay(1e-8)
self.assertEqual((yield dut.grant), 2)
self.assertTrue((yield dut.valid))
yield dut.requests.eq(0b010)
yield; yield Delay(1e-8)
self.assertEqual((yield dut.grant), 1)
self.assertTrue((yield dut.valid))
yield dut.requests.eq(0b011)
yield; yield Delay(1e-8)
self.assertEqual((yield dut.grant), 0)
self.assertTrue((yield dut.valid))
yield dut.requests.eq(0b001)
yield; yield Delay(1e-8)
self.assertEqual((yield dut.grant), 0)
self.assertTrue((yield dut.valid))
yield dut.requests.eq(0b101)
yield; yield Delay(1e-8)
self.assertEqual((yield dut.grant), 2)
self.assertTrue((yield dut.valid))
yield dut.requests.eq(0b100)
yield; yield Delay(1e-8)
self.assertEqual((yield dut.grant), 2)
self.assertTrue((yield dut.valid))
yield dut.requests.eq(0b000)
yield; yield Delay(1e-8)
self.assertFalse((yield dut.valid))
yield dut.requests.eq(0b001)
yield; yield Delay(1e-8)
self.assertEqual((yield dut.grant), 0)
self.assertTrue((yield dut.valid))
sim.add_sync_process(process)
sim.add_clock(1e-6)
with sim.write_vcd("test.vcd"):
sim.run()