To learn more about C and .comp, I tried to add comments (the "// ...
bits") to edge.comp. I am still a bit fuzzy on how edge works so I would
appreciate any improvements to my comments below. Thanks.

------- edge.comp -------
component edge                  "Edge detector and single pulse generator.";

pin in bit in                   "Signal with edges to detect";
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 in_edge=TRUE       "Selects the 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";

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

FUNCTION(_){ 
    int new_in = in;                      // make a copy of the current in value
    if(in_edge) new_in = ! new_in;        // If checking for a falling edge, 
invert new-in to adjust it to a rising edge, otherwise go to next line
    if(new_in && new_in != last_in) {     // If new_in is non-zero and 
different than last_in, edge is detected?
        time_left_ns = out_width_ns;      // then reset time_left_ns to the 
selected pulse width value
        out = 1; out_invert = 0;          // and set the outputs to reflect the 
edge detection
    } else if(time_left_ns > 0) {         // otherwise, if there is still some 
time left
        time_left_ns -= period;           // then decrement time_left_ns by the 
value of period (the period length of the home thread?)
        out = 1; out_invert = 0;          // set the outputs to reflect that 
the pulse is continuing
    } else {                              // otherwise, the pulse is done
        time_left_ns = 0;                 // reset time left
        out = 0; out_invert = 1;          // reset outputs to get ready for 
next edge detection
    }
    last_in = new_in;                     // Save a copy of the current edge 
adjusted input to use for the next pass
}
------- edge.comp -------

I would have tended to have made the read only param's as variables, but
I found that variables won't show up in halscope, so I am assuming that
param's were used for this reason? I also would have declared new_in up
in the param section as a variable or param, but I guess "int new_in =
in" kills the declaration and the value assignment with one stone? The
statement "if(new_in && new_in != last_in)" kind of baffles me. "foo &&
foo" seems to be a programming trick called a short circuit? I also
haven't figured out the true meaning of life.
-- 
Kirk Wallace
http://www.wallacecompany.com/machine_shop/
http://www.wallacecompany.com/E45/index.html
California, USA


------------------------------------------------------------------------------
The Next 800 Companies to Lead America's Growth: New Video Whitepaper
David G. Thomson, author of the best-selling book "Blueprint to a 
Billion" shares his insights and actions to help propel your 
business during the next growth cycle. Listen Now!
http://p.sf.net/sfu/SAP-dev2dev
_______________________________________________
Emc-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/emc-users

Reply via email to