On 11/10/2010 01:04 AM, Kirk Wallace wrote:
> 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.
>
Been a while since I've messed with C programming. When you declare a
variable (new_in) inside a function like you did, it's a "local"
variable, and you are assigning the value of "in", a global variable, to
it. When a variable is local, such as in that function, it's value is
only available to that function. Global variables are generally
available throughout the entire program. Do you need the value of
new_in available outside the function? If so, either declare new_in a
global variable or return the value of new_in along with the values of
out and out_invert. Just beware the traps of global variablesand their
scope, and how changing that variable inside a function can effect other
functions in the memory stack that may use that variable.
The "if" statement isn't saying just "foo && foo". The "if" statement
is evaluating the variables - if "new_in" is not null, and "new_in" is
not equal to last_in, then...
Wiki has a pretty decent write up on short-circuit evaluation.
> http://en.wikipedia.org/wiki/Short-circuit_evaluation
Mark
------------------------------------------------------------------------------
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