Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Paul E Bennett Newsgroups: comp.lang.forth Subject: Re: Clarity vs. Efficiency Date: Wed, 15 Oct 2014 22:06:42 +0100 Organization: Hidecs Consultancy Lines: 83 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: individual.net miI4oZV7vBu2FNf2FEXacwszKbgyRtm8GQRtGtI4VdNToiIzWo Cancel-Lock: sha1:mzuVp5TCB+QFfv2bWiKIfuBkpjs= User-Agent: KNode/4.11.4 Xref: csiph.com comp.lang.forth:33927 forthisit@gmail.com wrote: > I recently wrote some code (for an embedded Forth processor) to convert a > user's desired frequency into control register values to be written to > Xilinx's PLL (which they call MMCM, for mixed mode clock manager). In this > way the frequency output can be changed dynamically to any desired value > (within certain resolution limits, of course) without changing the FPGA's > hardware design. It was a fun project. > > Here are two of the words developed along the way: > > \ calculate "M" > : fbdiv ( -- n ) \ aka M (multiplier) > drp_freq $1FF and \ strip off [12:9] > 2 rshift > >606? IF 2/ > ELSE >400? not IF 2/ THEN > THEN ; > > \ calculate value for CLKFBOUT's CLKREG1 register > : fbreg1 ( -- n ) > fbdiv 2/ ( n ) > frac? IF 1 - THEN ( n ) \ fractional divide adds undocumented '2' > dup ( n n )\ (ie. 1 in frac bits gives M of 2.125,not > 0.125) > fbdiv odd? + ( h l )\ if M is odd LOW_TIME = HIGH_TIME + 1 > swap 6 lshift or ( n ) > ; > > I don't want to belabor the point with too many hardware details, but > suffice it to say that the PLL uses many dividers/counters. If you place a > divider in a PLL feedback path it actually functions as a multiplier. The > value for this parameter is labeled "M" in Xilinx's block diagrams of the > MMCM. For the few (maybe none) who care, here are all the gruesome details > Xilinx gives for exercising the MMCM DRP (dynamic reconfiguration port): > http://www.xilinx.com/support/documentation/application_notes/xapp888_7Series_Dy > namicRecon.pdf > > So fbdiv basically retrieves the desired frequency setting, returned by > drp_freq, and calculates and returns "M". The value of "M" is required in > the calculation of more than one control register value. From an > efficiency perspective it would be best to only calculate this once, store > it in a variable or on the stack, and retrieve it as needed. But from a > clarity perspective I found that I liked the expressiveness of just > calling the fbdiv word whenever I needed the value of "M" in the > calculations (as in the fbreg1 word above). It saves variable space, > reduces the so called "stack juggling" that Forth is often criticized for, > and it really helped when checking my words interactively using Gforth as > I coded. > > It got me to thinking that if more code was written like this, especially > when making the case for the acceptance of Forth, the code might be more > easily understood and better appreciated; even though it sacrifices some > efficiency. I felt that this embedded Forth processor running Forth code > is so fast already that I'm willing to sacrifice a little efficiency for > clarity. > > Agree or disagree? I agree also and echo Andrew's constraint that the response time objectives of the system continue to be met. However, there are other ways of making the intent of your code clear through adopting a literal style of code and comment layout. Think of the Glossary Entry (marked with a G: in a comment line) to be the statement of requirements for the functional behaviour of the word and setting out the expected limitations. This will also help in efforts to fully certify the code you generate as, by inspection, functional test and limitations testing you can confirm that your code fully meets that statement of requirements. See my paper "High Integrity Systems C.O.D.E for more on this particular topic (on the EuroForth website when Anton gets to put this years proceedings up). -- ******************************************************************** Paul E. Bennett IEng MIET..... Forth based HIDECS Consultancy............. Mob: +44 (0)7811-639972 Tel: +44 (0)1235-510979 Going Forth Safely ..... EBA. www.electric-boat-association.org.uk.. ********************************************************************