summaryrefslogtreecommitdiff
path: root/hdl_lab/hdl/reset_sync.py
blob: 3116b9ed3025ad1710906b6d889626328944342a (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
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
from typing import Callable
from myhdl import *
from myhdl_wrap import Myhdl_Wrapper

import random
from random import randint

random.seed(13)

class ResetSync(Myhdl_Wrapper):
    def __init__(self):
        super().__init__()

    # Main code, this is the actual logic
    @staticmethod
    @block
    def ResetSync(clk : Signal, async_reset : Signal, sync_reset : Signal):  # this must be the same name as the class name

        sync = Signal(False)
        guard = sync_reset

        @instance
        def logic():
            while True:
                yield clk.posedge, async_reset.negedge

                if not async_reset:
                    guard.next = False
                    sync.next = False
                else:
                    guard.next = sync
                    sync.next = True

        return logic


    @block
    def tb(self, func: Callable):
        async_reset = Signal(False)
        sync_reset = Signal(False)
        clk = Signal(bool(0))

        dut = func(clk=clk, async_reset=async_reset, sync_reset=sync_reset)

        @always(delay(2))
        def clock_gen():
            clk.next = not clk

        @instance
        def monitor_async():
            while True:
                yield async_reset.negedge
                yield delay(1)
                assert sync_reset == False, 'Reset not asynchronous on falling edge'

        @instance
        def monitor_sync():
            while True:
                yield async_reset.posedge

                assert sync_reset == False, 'sync Reset did not wait for first clock positive edge'

                yield clk.posedge, async_reset.negedge
                if async_reset == True:
                    assert sync_reset.next == False, 'sync Reset did not wait for second clock positive edge'

                    yield clk.posedge, async_reset.negedge
                    if async_reset == True:
                        assert sync_reset.next == True, 'sync Reset did not set'



        @instance
        def stimulus():
            for _ in range(500):
                yield delay(randint(1,20))

                if (now()+2) % 4 == 0:  # do not create rising edge of reset and clk at the same time
                    yield(delay(1))

                async_reset.next = True
                yield delay(randint(1, 20))
                async_reset.next = False

            raise StopSimulation


        return dut, clock_gen, monitor_async, monitor_sync, stimulus

    def export(self):
        async_reset = Signal(False)
        sync_reset = Signal(False)
        clk = Signal(bool(0))

        # assigning signals, kargs only
        self._export(clk=clk, async_reset=async_reset, sync_reset=sync_reset)


def test_reset_sync_sim():
    hdl = ResetSync()
    hdl.sim()

def test_reset_sync_cosim():
    hdl = ResetSync()
    hdl.export()
    # hdl.cosim()

# test_reset_sync_sim()
test_reset_sync_cosim()