File: long_divider.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 (90 lines) | stat: -rw-r--r-- 2,343 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
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
from __future__ import absolute_import
import myhdl
from myhdl import *

def long_divider(
            quotient,
            ready,
            dividend,
            divisor,
            start,
            clock,
            reset
            ):
    

    M = len(dividend)
    N = len(divisor)
    Q = len(quotient)
    
    assert M-N == Q
    
    st = enum("WAIT_START", "CALC")
    
    @instance
    def proc():
        state = st.WAIT_START
        div = intbv(0)[N+1:]
        divbits = intbv(0)[Q-1:]
        quot = intbv(0)[Q:]
        count = intbv(0, min=0, max=Q)
        while True:
            yield clock.posedge, reset.posedge
            if reset == 1:
                quotient.next = 0
                ready.next = 0
                state = st.WAIT_START
                div[:] = 0
                divbits[:] = 0
                quot[:] = 0
            else:
                if state == st.WAIT_START:
                    if start:
                        state = st.CALC
                        ready.next = 0
                        quot[:] = 0
                        div[N+1:] = dividend[M:Q-1]
                        divbits[:] = dividend[Q-1:]
                        count[:] = 0
                        
                elif state == st.CALC:
                    quot[Q:1] = quot[Q-1:]
                    if div >= divisor:
                        quot[0] = 1
                        div -= divisor
                    else:
                        quot[0] = 0
                    
                    if count == Q-1:
                        ready.next = 1
                        state = st.WAIT_START
                    else:
                        div[N+1:1] = div[N:]
                        div[0] = divbits[Q-2]
                        divbits[Q-1:1] = divbits[Q-2:]
                        count += 1
                        
            quotient.next = quot

    return proc


    
if __name__ == '__main__':
    quotient = Signal(intbv(0)[22:])
    ready = Signal(bool())
    dividend = Signal(intbv(0)[38:])
    divisor = Signal(intbv(0)[16:])
    start = Signal(bool())
    clock = Signal(bool())
    reset = Signal(bool())
    
    toVHDL(long_divider,
           quotient,
           ready,
           dividend,
           divisor,
           start,
           clock,
           reset
           )