Jonathan Bromley
Guest
Mon Dec 14, 2009 11:10 pm
Having spent far too much of this year worrying about
SystemVerilog I decided to take a break not only from
That Other Language but also from digital design.
The following code is offered under the Reindeer
open-sleigh licence and may be freely redistributed
at any time between 23:59 on 24 December and 00:01
on 25 December in any year.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
use std.textio.all;
entity noel is end noel;
architecture gifts of noel is
type giftArray is array(1 to 12) of string(1 to 24);
type countArray is array(1 to 12) of integer;
constant giftList: giftArray := (
1 => "partridge in a pear tree",
2 => "turtle doves ",
3 => "French hens ",
4 => "colley birds ",
5 => "gold rings ",
6 => "geese a-laying ",
7 => "swans a-swimming ",
8 => "maids a-milking ",
9 => "ladies dancing ",
10 => "pipers piping ",
11 => "lords a-leaping ",
12 => "drummers drumming "
);
function trimR(s: in string) return string is
variable i: integer;
begin
i := s'RIGHT;
while (i>=s'LEFT) and (s(i)=' ') loop
i := i-1;
end loop;
return s(s'LEFT to i);
end; -- function trimR
function to_Cardinal(n: in integer) return string is
begin
case n is
when 1 => return "a";
when 2 => return "two";
when 3 => return "three";
when 4 => return "four";
when 5 => return "five";
when 6 => return "six";
when 7 => return "seven";
when 8 => return "eight";
when 9 => return "nine";
when 10 => return "ten";
when 11 => return "eleven";
when 12 => return "twelve";
when others => return integer'image(n);
end case;
end; -- function to_Cardinal
function ordinal_suffix(d: in positive) return string is
begin
if ((d mod 100) / 10) = 1 then
-- In English, *10th-*19th are a special case:
return "th";
else
-- ordinal suffix depends only on final digit:
case d mod 10 is
when 1 => return "st";
when 2 => return "nd";
when 3 => return "rd";
when others => return "th";
end case;
end if;
end; -- function ordinal_suffix
function to_ordinal(d: in positive) return string is
begin
return integer'image(d) & ordinal_suffix(d);
end;
procedure writeDiary(s: inout line; d: in integer) is
begin
write(s, string'("On the "));
write(s, to_ordinal(d));
write(s, string'(" day of Christmas my true love sent to me:"));
end; -- procedure writeDiary
procedure writeGift(s: inout line; g: in integer) is
begin
write(s, to_cardinal(g));
write(s, ' ');
write(s, trimR(giftlist(g)));
if g=1 then
write(s, '.');
else
write(s, ',');
end if;
end; -- procedure writeGift
begin
days: process
variable s: line;
variable giftCount: countArray := (others => 0);
begin
-- Header
write(s,string'("====================================================="));
writeline(output, s);
writeline(output, s);
for day in 1 to 12 loop
-- One day's activity
writeDiary(s, day);
writeline(output, s);
for gift in day downto 1 loop
for indent in day downto gift loop
write(s, ' ');
end loop;
if (day>1) and (gift=1) then
write(s, string'("and "));
end if;
giftCount(gift) := giftCount(gift)+gift;
writeGift(s, gift);
writeline(output, s);
end loop; -- for gift
writeline(output, s);
end loop; -- for day
write(s,string'("====================================================="));
writeline(output, s);
writeline(output, s);
write(s,string'("CUMULATIVE GIFT TOTALS:"));
writeline(output,s);
for gift in 1 to 12 loop
write(s,' ');
write(s,giftList(gift));
write(s,string'(" qty."));
write(s,giftCount(gift), field=>3,justified=>right);
writeline(output,s);
end loop; -- for gift
writeline(output, s);
write(s,string'("====================================================="));
writeline(output, s);
writeline(output, s);
wait;
end process; -- days
end gifts;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
Jonathan Bromley
Brian Drummond
Guest
Tue Dec 15, 2009 1:55 am
On Mon, 14 Dec 2009 23:10:35 +0100, Jonathan Bromley
<jonathan.bromley_at_MYCOMPANY.com> wrote:
Quote:
Having spent far too much of this year worrying about
SystemVerilog I decided to take a break not only from
That Other Language but also from digital design.
The following code is offered under the Reindeer
open-sleigh licence and may be freely redistributed
at any time between 23:59 on 24 December and 00:01
on 25 December in any year.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
use std.textio.all;
entity noel is end noel;
.....
and Jonathan breaks the SQRT(N/ LOG(N)) barrier!
http://www.cs.utexas.edu/users/arvindn/misc/knuth_song_complexity.pdf
Cheers,
- Brian
Mike Treseler
Guest
Tue Dec 15, 2009 1:57 am
Jonathan Bromley wrote:
Quote:
Having spent far too much of this year worrying about
SystemVerilog I decided to take a break not only from
That Other Language but also from digital design.
Thank for the break.
Much more interesting than the code I was slogging through.
And a nice demo of of how to build a readable process.
I put trimR in my stocking, than you very much.
For those without a simulator, I pasted the text out below.
Happy Holidays.
-- Mike Treseler
Spoiler alert. Close now, if you want to sim it.
_____________________________________________
Quote:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- # vsim -c noel
-- # Loading /flip/usr1/modeltech/linux/../std.standard
-- # Loading /flip/usr1/modeltech/linux/../std.textio(body)
-- # Loading work.noel(gifts)
-- VSIM 1> run
-- # =====================================================
-- #
-- # On the 1st day of Christmas my true love sent to me:
-- # a partridge in a pear tree.
-- #
-- # On the 2nd day of Christmas my true love sent to me:
-- # two turtle doves,
-- # and a partridge in a pear tree.
-- #
-- # On the 3rd day of Christmas my true love sent to me:
-- # three French hens,
-- # two turtle doves,
-- # and a partridge in a pear tree.
-- #
-- # On the 4th day of Christmas my true love sent to me:
-- # four colley birds,
-- # three French hens,
-- # two turtle doves,
-- # and a partridge in a pear tree.
-- #
-- # On the 5th day of Christmas my true love sent to me:
-- # five gold rings,
-- # four colley birds,
-- # three French hens,
-- # two turtle doves,
-- # and a partridge in a pear tree.
-- #
-- # On the 6th day of Christmas my true love sent to me:
-- # six geese a-laying,
-- # five gold rings,
-- # four colley birds,
-- # three French hens,
-- # two turtle doves,
-- # and a partridge in a pear tree.
-- #
-- # On the 7th day of Christmas my true love sent to me:
-- # seven swans a-swimming,
-- # six geese a-laying,
-- # five gold rings,
-- # four colley birds,
-- # three French hens,
-- # two turtle doves,
-- # and a partridge in a pear tree.
-- #
-- # On the 8th day of Christmas my true love sent to me:
-- # eight maids a-milking,
-- # seven swans a-swimming,
-- # six geese a-laying,
-- # five gold rings,
-- # four colley birds,
-- # three French hens,
-- # two turtle doves,
-- # and a partridge in a pear tree.
-- #
-- # On the 9th day of Christmas my true love sent to me:
-- # nine ladies dancing,
-- # eight maids a-milking,
-- # seven swans a-swimming,
-- # six geese a-laying,
-- # five gold rings,
-- # four colley birds,
-- # three French hens,
-- # two turtle doves,
-- # and a partridge in a pear tree.
-- #
-- # On the 10th day of Christmas my true love sent to me:
-- # ten pipers piping,
-- # nine ladies dancing,
-- # eight maids a-milking,
-- # seven swans a-swimming,
-- # six geese a-laying,
-- # five gold rings,
-- # four colley birds,
-- # three French hens,
-- # two turtle doves,
-- # and a partridge in a pear tree.
-- #
-- # On the 11th day of Christmas my true love sent to me:
-- # eleven lords a-leaping,
-- # ten pipers piping,
-- # nine ladies dancing,
-- # eight maids a-milking,
-- # seven swans a-swimming,
-- # six geese a-laying,
-- # five gold rings,
-- # four colley birds,
-- # three French hens,
-- # two turtle doves,
-- # and a partridge in a pear tree.
-- #
-- # On the 12th day of Christmas my true love sent to me:
-- # twelve drummers drumming,
-- # eleven lords a-leaping,
-- # ten pipers piping,
-- # nine ladies dancing,
-- # eight maids a-milking,
-- # seven swans a-swimming,
-- # six geese a-laying,
-- # five gold rings,
-- # four colley birds,
-- # three French hens,
-- # two turtle doves,
-- # and a partridge in a pear tree.
-- #
-- # =====================================================
-- #
-- # CUMULATIVE GIFT TOTALS:
-- # partridge in a pear tree qty. 12
-- # turtle doves qty. 22
-- # French hens qty. 30
-- # colley birds qty. 36
-- # gold rings qty. 40
-- # geese a-laying qty. 42
-- # swans a-swimming qty. 42
-- # maids a-milking qty. 40
-- # ladies dancing qty. 36
-- # pipers piping qty. 30
-- # lords a-leaping qty. 22
-- # drummers drumming qty. 12
-- #
-- # =====================================================
-- #
Daku
Guest
Tue Dec 15, 2009 6:37 am
Cool. Reminds me of the time I was up to my neck
in computer graphics. A potential client was going
to come in to review some of our past work, and our
boss Fred told us to choose three of our 'coolest'
products.
A colleague of mine, Herb and I sat down and chose
three, but felt that the third one should not be
just one of our products, but something 'cool'.
So, on the day of the client's visit, the third
demo was this:
The application would open, the screen would go
blank, and then a number of colored spots would
start appearing, and then swirl around on the
screen in geometrical patterns. Then they would
start organizing themselves, become larger, and
finally the following text would appear on the
screen - 'Fred is a great guy'.
The client started laughing and remarked that
this was really good and we did get business
from them.
On Dec 15, 3:10 am, Jonathan Bromley <jonathan.brom...@MYCOMPANY.com>
wrote:
Quote:
Having spent far too much of this year worrying about
SystemVerilog I decided to take a break not only from
That Other Language but also from digital design.
The following code is offered under the Reindeer
open-sleigh licence and may be freely redistributed
at any time between 23:59 on 24 December and 00:01
on 25 December in any year.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
use std.textio.all;
entity noel is end noel;
architecture gifts of noel is
type giftArray is array(1 to 12) of string(1 to 24);
type countArray is array(1 to 12) of integer;
constant giftList: giftArray := (
1 => "partridge in a pear tree",
2 => "turtle doves ",
3 => "French hens ",
4 => "colley birds ",
5 => "gold rings ",
6 => "geese a-laying ",
7 => "swans a-swimming ",
8 => "maids a-milking ",
9 => "ladies dancing ",
10 => "pipers piping ",
11 => "lords a-leaping ",
12 => "drummers drumming "
);
function trimR(s: in string) return string is
variable i: integer;
begin
i := s'RIGHT;
while (i>=s'LEFT) and (s(i)=' ') loop
i := i-1;
end loop;
return s(s'LEFT to i);
end; -- function trimR
function to_Cardinal(n: in integer) return string is
begin
case n is
when 1 => return "a";
when 2 => return "two";
when 3 => return "three";
when 4 => return "four";
when 5 => return "five";
when 6 => return "six";
when 7 => return "seven";
when 8 => return "eight";
when 9 => return "nine";
when 10 => return "ten";
when 11 => return "eleven";
when 12 => return "twelve";
when others => return integer'image(n);
end case;
end; -- function to_Cardinal
function ordinal_suffix(d: in positive) return string is
begin
if ((d mod 100) / 10) = 1 then
-- In English, *10th-*19th are a special case:
return "th";
else
-- ordinal suffix depends only on final digit:
case d mod 10 is
when 1 => return "st";
when 2 => return "nd";
when 3 => return "rd";
when others => return "th";
end case;
end if;
end; -- function ordinal_suffix
function to_ordinal(d: in positive) return string is
begin
return integer'image(d) & ordinal_suffix(d);
end;
procedure writeDiary(s: inout line; d: in integer) is
begin
write(s, string'("On the "));
write(s, to_ordinal(d));
write(s, string'(" day of Christmas my true love sent to me:"));
end; -- procedure writeDiary
procedure writeGift(s: inout line; g: in integer) is
begin
write(s, to_cardinal(g));
write(s, ' ');
write(s, trimR(giftlist(g)));
if g=1 then
write(s, '.');
else
write(s, ',');
end if;
end; -- procedure writeGift
begin
days: process
variable s: line;
variable giftCount: countArray := (others => 0);
begin
-- Header
write(s,string'("====================================================="));
writeline(output, s);
writeline(output, s);
for day in 1 to 12 loop
-- One day's activity
writeDiary(s, day);
writeline(output, s);
for gift in day downto 1 loop
for indent in day downto gift loop
write(s, ' ');
end loop;
if (day>1) and (gift=1) then
write(s, string'("and "));
end if;
giftCount(gift) := giftCount(gift)+gift;
writeGift(s, gift);
writeline(output, s);
end loop; -- for gift
writeline(output, s);
end loop; -- for day
write(s,string'("====================================================="));
writeline(output, s);
writeline(output, s);
write(s,string'("CUMULATIVE GIFT TOTALS:"));
writeline(output,s);
for gift in 1 to 12 loop
write(s,' ');
write(s,giftList(gift));
write(s,string'(" qty."));
write(s,giftCount(gift), field=>3,justified=>right);
writeline(output,s);
end loop; -- for gift
writeline(output, s);
write(s,string'("====================================================="));
writeline(output, s);
writeline(output, s);
wait;
end process; -- days
end gifts;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
Jonathan Bromley
Jonathan Bromley
Guest
Tue Dec 15, 2009 2:21 pm
On Dec 15, 1:57 am, Mike Treseler <mtrese...@gmail.com> wrote:
Quote:
I put trimR in my stocking, than you very much.
Hmmm. I took a pass through the code to check it before
posting, but an astute and paranoid reader should be
able to find the unpleasant flaw in trimR()'s design
(it works fine in the example, but isn't very general).
As usual, exposure to public scrutiny is the best code review.
--
Jonathan Bromley
Martin Thompson
Guest
Tue Dec 15, 2009 2:52 pm
Jonathan Bromley <jonathan.bromley_at_MYCOMPANY.com> writes:
<snip>
Brilliant - thanks!
Quote:
for day in 1 to 12 loop
-- One day's activity
writeDiary(s, day);
writeline(output, s);
....
....
Quote:
writeline(output, s);
Shouldn't there be a
wait for 1 day;
in there

?
Quote:
end loop; -- for day
Cheers,
Martin
--
martin.j.thompson_at_trw.com
TRW Conekt - Consultancy in Engineering, Knowledge and Technology
http://www.conekt.net/electronics.html