summaryrefslogtreecommitdiff
path: root/src/hal/components/edge.comp
blob: 7f382e8d8844b18e7444cb07ff92bca7a97ad8d4 (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
//   This is a component for EMC2 HAL
//   Copyright 2006 Jeff Epler <jepler@unpythonic.net>
//
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
component edge "Edge detector";

pin in bit in;
pin out bit out "Goes high when the desired edge is seen on 'in'";
pin out bit out_invert "Goes low when the desired edge is seen on 'in'";

param rw bit both=FALSE "If TRUE, selects both edges.  Otherwise, selects one edge according to in-edge";
param rw bit in_edge=TRUE "If both is FALSE, selects the one desired edge: TRUE means falling, FALSE means rising";
param rw signed out_width_ns=0 "Time in nanoseconds of the output pulse";

param r signed time_left_ns "Time left in this output pulse";
param r bit last_in "Previous input value";
variable int first = 1;

function _ nofp "Produce output pulses from input edges";
license "GPL";
;;

FUNCTION(_){ 
    int new_in = in;
    if (time_left_ns > 0) {
        time_left_ns -= period;
    }
    if ( !first ) {
        int rise = new_in && !last_in;
        int fall = !new_in && last_in;
        int desired_edge =
            both ? rise || fall
                    : in_edge ? fall : rise;
        if(desired_edge) {
            time_left_ns = out_width_ns;
            out = 1;
        } else if(time_left_ns > 0) {
            out = 1;
        } else {
            time_left_ns = 0;
            out = 0;
        }
    } else {
        first = 0;
    }
    last_in = new_in;
    out_invert = !out;
}