File: timer.py

package info (click to toggle)
myhdl 0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,728 kB
  • sloc: python: 25,033; ansic: 2,203; makefile: 243; sh: 206
file content (43 lines) | stat: -rw-r--r-- 956 bytes parent folder | download | duplicates (2)
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
from __future__ import absolute_import
import myhdl
from myhdl import *

def timer_sig(flag, clock, reset, MAXVAL):

    count = Signal(intbv(0, min=0, max=MAXVAL+1))

    @always (clock.posedge, reset.posedge)
    def logic():
        if reset == 1:
            count.next = 0
        else:
            flag.next = 0
            if count == MAXVAL:
                flag.next = 1
                count.next = 0
            else:
                count.next = count + 1

    return logic


def timer_var(flag, clock, reset, MAXVAL):

    @instance
    def logic():
        count = intbv(0, min=0, max=MAXVAL+1)
        while True:
            yield clock.posedge, reset.posedge
            if reset == 1:
                count[:] = 0
            else:
                flag.next = 0
                if count == MAXVAL:
                    flag.next = 1
                    count[:] = 0
                else:
                    count += 1

    return logic