SlideShare una empresa de Scribd logo
1 de 9
Descargar para leer sin conexión
HELLO WORLD
c Copyright 2005–2006 Bluespec, Inc. All Rights Reserved.
                    March 30, 2007




                           1
1       Introduction
You have just finished the Bluespec training course, and wish to do your first design in
Bluespec. So you are sitting looking at a blank sheet of paper, or a blank computer screen,
and wondering what on earth to write. The aim of this tutorial is to get you over this initial
hurdle, and start you writing real Bluespec.
   The first program which software people traditionally write in a new language is one
which simply writes “Hello world” on the printer. Let us do the same. This means, of
course, that our design is not going to be intended to turn into real hardware: it is merely
going to produce output from a simulator. In this respect it is like a top-level test program,
rather than a hardware device. (We’ll have a design which produces real hardware as our
next example.)


2       Version 1
A Bluespec design is a collection of packages—but in this simple design there will be only
one package. So we begin by saying that we are writing a package1 called “FirstAttempt”;
and it is good practice to add a comment explaining what it is about. So we write:

                 package FirstAttempt;
                    // My first design in the cool Bluespec language
                 endpackage

The compiler will object if the name of the package is not the same as the file it is stored in:
that is, the package “FirstAttempt” must be stored in the file “FirstAttempt.bsv”.
   Type that into the computer, and run the Bluespec compiler on it:

                    bsc FirstAttempt.bsv

It should compile successfully, without any warnings.
    A package may typically contain a few initial definitions of types and constants, and some
module definitions. As an example of this, let us give a name to the string we are going to
output:

                 package FirstAttempt;
                    // My first design in the cool Bluespec language

                    String s = quot;Hello worldquot;;
                 endpackage

   Now let us think about the main module in this design. We must first consider how it is
going to communicate with other hardware—either other modules in the design or hardware
external to the design. That is, we must decide on the type of its interface. In this case the
    1
     Actually the package/endpackage lines are not strictly necessary, but they are strongly recommended
for all but the tiniest temporary test programs.



                                                   2
answer is easy: the module is not going to communicate with other hardware, so there is no
interface to define.
    By convention, the names of modules start “mk”, to distinguish them from their instan-
tiations; so we can type in the shell of the module definition as follows:
               package FirstAttempt;
                  // My first design in the cool Bluespec language

                   String s = quot;Hello worldquot;;

                  module mkAttempt;
                  endmodule
               endpackage
Perhaps it is worth checking that this also compiles successfully.
    Finally we add the rules, which define the module’s internal behavior (normally we
would also have to add the interface methods, which define its external interactions, but in
this example there aren’t any). In this example we have just one rule, which we shall call
“say_hello”; this will use the system command “$display” to do the printing.
    Since this module is to be “synthesized”, either to hardware or (in this case) to a simulator
executable, we add the “synthesize” attribute; so the final first draft of our package is as
follows.
               package FirstAttempt;
                  // My first design in the cool Bluespec language

                   String s = quot;Hello worldquot;;

                  (* synthesize *)
                  module mkAttempt(Empty);
                     rule say_hello;
                        $display(s);
                     endrule
                  endmodule
               endpackage


3    Simulation
First we run the BSV compiler tool bsc, to convert our package to Verilog RTL:
                   bsc -verilog FirstAttempt.bsv
This produces an RTL file called mkAttempt.v. From this we create and run the simulator
executable:
                   bsc -o sim -e mkAttempt mkAttempt.v
                   ./sim

                                               3
Here, as explained in the User Guide, the “-o sim” specifies that the executable is to be
called sim, and the “-e mkAttempt” says that mkAttempt is the top-level module in the
design.
    Try it and see what happens.
    You will see that the rule executes on every clock cycle, so that there is an endless stream
output of lines saying “Hello world”.


4    Version 2
It is easy to make the output happen only once: we simply add a “$finish” command to
the rule.
               package FirstAttempt;
                  // My first design in the cool Bluespec language

                   String s = quot;Hello worldquot;;

                  (* synthesize *)
                  module mkAttempt(Empty);
                     rule say_hello;
                        $display(s);
                        $finish(0);
                     endrule
                  endmodule
               endpackage

Note that the actions within a rule execute simultaneously in hardware; but system tasks
within a rule are performed by a simulator in the order they are written (for otherwise it
would be very difficult to achieve the desired output). Thus the “$display” happens before
the “$finish”, as desired.


5    Version 3
The next version of this design is intended to output “Hello world” precisely five times. For
this we shall need a register to act as a counter. This is declared in the module, before
the rule. The counter must be able to count up to 5, so 3 bits will be sufficient, and the
appropriate type is accordingly UInt#(3)—that is, an unsigned integer represented in three
bits. We shall specify 0 as the register’s initial value (which is restored whenever the design
is reset). We add the register definition to the module as follows:




                                               4
package FirstAttempt;
                 // My first design in the cool Bluespec language

                  String s = quot;Hello worldquot;;

                  (* synthesize *)
                  module mkAttempt(Empty);
                     Reg#(UInt#(3)) ctr <- mkReg(0);

                    rule say_hello;
                       $display(s);
                       $finish(0);
                    endrule
                 endmodule
              endpackage

The rule must now be revised to increment the counter, and to finish at the appropriate
time.
              package FirstAttempt;
                 // My first design in the cool Bluespec language

                  String s = quot;Hello worldquot;;

                  (* synthesize *)
                  module mkAttempt(Empty);
                     Reg#(UInt#(3)) ctr <- mkReg(0);

                    rule say_hello;
                       ctr <= ctr + 1;
                       $display(s);
                       if (ctr==4) $finish(0);
                    endrule
                 endmodule
              endpackage

Remember that all actions within a rule happen simultaneously. So, even though the state-
ment incrementing the counter is written above the if-statement, the latter sees the “old”
value of ctr—thus, in order to produce five lines of output, the finish has to occur when
that old value is 4.


6    Other Versions
This section shows a few alternative versions, some of which work and some of which don’t.


                                            5
6.1    Version 4
Another way of writing this is to use two rules, as follows:

               package FirstAttempt;
                  // My first design in the cool Bluespec language

                   String s = quot;Hello worldquot;;

                   (* synthesize *)
                   module mkAttempt(Empty);
                      Reg#(UInt#(3)) ctr <- mkReg(0);

                      rule end_run (ctr==5);
                         $finish(0);
                      endrule

                     rule say_hello (ctr<5);
                        ctr <= ctr + 1;
                        $display(s);
                     endrule
                  endmodule
               endpackage

Check that these two versions give the same results.

6.2    Version 5
We might next consider splitting the rules still further, separating incrementing the counter
into a rule of its own. This would lead to the following design:




                                              6
package FirstAttempt;
                   // My first design in the cool Bluespec language

                    String s = quot;Hello worldquot;;

                    (* synthesize *)
                    module mkAttempt(Empty);
                       Reg#(UInt#(3)) ctr <- mkReg(0);

                        rule end_run (ctr==5);
                           $finish(0);
                        endrule

                        rule say_hello (ctr<5);
                           $display(s);
                        endrule

                      rule inc_ctr;
                         ctr <= ctr + 1;
                      endrule
                   endmodule
                endpackage

What would be the effect of doing this? In fact, it would continue to give the correct result;
but notice that we are beginning to live dangerously. If the increment and the display were
in the same rule, then we can be sure that they happen either together or not at all. If the
increment is in a rule of its own, it is counting clock cycles, not lines of output. If the display
rule were held up for any reason (admittedly unlikely in this particular example), the test
would finish after the prescribed number of cycles, however many lines had been output by
then.
    The moral is that things which are required to happen together should be put into the
same rule—simultaneous operations should not be achieved by relying on two rules firing in
the same clock cycle.

6.3    Version 6
In our next version we get still more sloppy—we omit the condition on the rule “say_hello”.
We reason that the “end_run” rule will cause the test to terminate at the appropriate time,
so there is no need to cease counting explicitly.




                                                7
package FirstAttempt;
                  // My first design in the cool Bluespec language

                   String s = quot;Hello worldquot;;

                   (* synthesize *)
                   module mkAttempt(Empty);
                      Reg#(UInt#(3)) ctr <- mkReg(0);

                       rule end_run (ctr==5);
                          $finish(0);
                       endrule

                       rule say_hello;
                          $display(s);
                       endrule

                     rule inc_ctr;
                        ctr <= ctr + 1;
                     endrule
                  endmodule
               endpackage

Now the result is unpredictable. As we know, the behavior of the design always corresponds
to the rules being executed “atomically”—that is to say, it is as if the rules executed in a
single clock cycle were actually executed one at a time, one after the other. In this example
all three rules fire in the final cycle, and the compiler tool has to choose whether “end_run”
is to go before “say_hello” or after it. With the design as written above, and today’s
version of the tool, the “say_hello” rule will go first, and we shall get six lines of output
instead of the desired five. It could be that rearranging the order of the rules would lead to a
different outcome—but of course it would be foolish to rely on this, and this version should
be considered bad.

6.4    Version 6
Finally we try the two-rule version with the condition of “say_hello” omitted:




                                              8
package FirstAttempt;
                    // My first design in the cool Bluespec language

                     String s = quot;Hello worldquot;;

                     (* synthesize *)
                     module mkAttempt(Empty);
                        Reg#(UInt#(3)) ctr <- mkReg(0);

                         rule end_run (ctr==5);
                            $finish(0);
                         endrule

                       rule say_hello;
                          ctr <= ctr + 1;
                          $display(s);
                       endrule
                    endmodule
                 endpackage

This version will in fact always give the right result—the order in which the two rules appear
to fire “within” the final cycle is no longer arbitrary. But the reason for this is somewhat
subtle2 , and so this version should also be regarded as bad, just because its correctness is
unnecessarily difficult to understand.




   2
     The rule “say hello” writes to the register ctr, whereas “end run” merely reads it; so the scheduler
must arrange that the overall effect must be as if “end run” executed first (for otherwise, in the actual
hardware, where all the reads of the clock cycle happen before any of the writes, “end run” would see a
stale value of ctr. Thus the “$finish” happens before the extra “$display” would have happened, so the
number of lines output is the same as with the previous version.


                                                   9

Más contenido relacionado

La actualidad más candente

Java util concurrent
Java util concurrentJava util concurrent
Java util concurrentRoger Xia
 
Антон Нонко, Классические строки в C++
Антон Нонко, Классические строки в C++Антон Нонко, Классические строки в C++
Антон Нонко, Классические строки в C++Sergey Platonov
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_functiontimotheeg
 
Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded ProgrammingSri Prasanna
 
Java synchronizers
Java synchronizersJava synchronizers
Java synchronizersts_v_murthy
 
Php and threads ZTS
Php and threads ZTSPhp and threads ZTS
Php and threads ZTSjulien pauli
 
Modern Objective-C @ Pragma Night
Modern Objective-C @ Pragma NightModern Objective-C @ Pragma Night
Modern Objective-C @ Pragma NightGiuseppe Arici
 
Mc Squared
Mc SquaredMc Squared
Mc Squaredsganga
 
Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기Ji Hun Kim
 
OPM Recipe designer notes
OPM Recipe designer notesOPM Recipe designer notes
OPM Recipe designer notested-xu
 

La actualidad más candente (13)

Motor de Regras @ TDC2010
Motor de Regras @ TDC2010Motor de Regras @ TDC2010
Motor de Regras @ TDC2010
 
Java util concurrent
Java util concurrentJava util concurrent
Java util concurrent
 
Joel Falcou, Boost.SIMD
Joel Falcou, Boost.SIMDJoel Falcou, Boost.SIMD
Joel Falcou, Boost.SIMD
 
Антон Нонко, Классические строки в C++
Антон Нонко, Классические строки в C++Антон Нонко, Классические строки в C++
Антон Нонко, Классические строки в C++
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_function
 
Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded Programming
 
Java synchronizers
Java synchronizersJava synchronizers
Java synchronizers
 
What is recursion?
What is recursion? What is recursion?
What is recursion?
 
Php and threads ZTS
Php and threads ZTSPhp and threads ZTS
Php and threads ZTS
 
Modern Objective-C @ Pragma Night
Modern Objective-C @ Pragma NightModern Objective-C @ Pragma Night
Modern Objective-C @ Pragma Night
 
Mc Squared
Mc SquaredMc Squared
Mc Squared
 
Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기
 
OPM Recipe designer notes
OPM Recipe designer notesOPM Recipe designer notes
OPM Recipe designer notes
 

Destacado

1032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.21032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.2Stanley Ho
 
用Raspberry PI學Linux驅動程式
用Raspberry PI學Linux驅動程式用Raspberry PI學Linux驅動程式
用Raspberry PI學Linux驅動程式Stanley Ho
 
看日記學Git
看日記學Git看日記學Git
看日記學GitStanley Ho
 
How to write shared libraries!
How to write shared libraries!How to write shared libraries!
How to write shared libraries!Stanley Ho
 

Destacado (6)

1032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.21032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.2
 
用Raspberry PI學Linux驅動程式
用Raspberry PI學Linux驅動程式用Raspberry PI學Linux驅動程式
用Raspberry PI學Linux驅動程式
 
看日記學Git
看日記學Git看日記學Git
看日記學Git
 
Riffmci
RiffmciRiffmci
Riffmci
 
Modern c
Modern cModern c
Modern c
 
How to write shared libraries!
How to write shared libraries!How to write shared libraries!
How to write shared libraries!
 

Similar a Bluespec Tutorial Helloworld

Eff Plsql
Eff PlsqlEff Plsql
Eff Plsqlafa reg
 
Tutorial de forms 10g
Tutorial de forms 10gTutorial de forms 10g
Tutorial de forms 10gmiguel
 
Lab3 testbench tutorial (1)
Lab3 testbench tutorial (1)Lab3 testbench tutorial (1)
Lab3 testbench tutorial (1)Abhishek Bose
 
Congfigure python as_ide
Congfigure python as_ideCongfigure python as_ide
Congfigure python as_ideLingfei Kong
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.gerrell
 
CUDA by Example : Advanced Atomics : Notes
CUDA by Example : Advanced Atomics : NotesCUDA by Example : Advanced Atomics : Notes
CUDA by Example : Advanced Atomics : NotesSubhajit Sahu
 
PL/SQL User-Defined Functions in the Read World
PL/SQL User-Defined Functions in the Read WorldPL/SQL User-Defined Functions in the Read World
PL/SQL User-Defined Functions in the Read WorldMichael Rosenblum
 
Javascript TDD with Jasmine, Karma, and Gulp
Javascript TDD with Jasmine, Karma, and GulpJavascript TDD with Jasmine, Karma, and Gulp
Javascript TDD with Jasmine, Karma, and GulpAll Things Open
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping newaprilyyy
 
Lecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationLecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationMohammed Farrag
 
Switch case and looping kim
Switch case and looping kimSwitch case and looping kim
Switch case and looping kimkimberly_Bm10203
 
Buffer overflow tutorial
Buffer overflow tutorialBuffer overflow tutorial
Buffer overflow tutorialhughpearse
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from insidejulien pauli
 
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres OpenBruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres OpenPostgresOpen
 

Similar a Bluespec Tutorial Helloworld (20)

Eff Plsql
Eff PlsqlEff Plsql
Eff Plsql
 
Tutorial de forms 10g
Tutorial de forms 10gTutorial de forms 10g
Tutorial de forms 10g
 
Lab3 testbench tutorial (1)
Lab3 testbench tutorial (1)Lab3 testbench tutorial (1)
Lab3 testbench tutorial (1)
 
Congfigure python as_ide
Congfigure python as_ideCongfigure python as_ide
Congfigure python as_ide
 
Computer Networks Omnet
Computer Networks OmnetComputer Networks Omnet
Computer Networks Omnet
 
Operating System Assignment Help
Operating System Assignment HelpOperating System Assignment Help
Operating System Assignment Help
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
 
Bluespec @waseda
Bluespec @wasedaBluespec @waseda
Bluespec @waseda
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
 
CUDA by Example : Advanced Atomics : Notes
CUDA by Example : Advanced Atomics : NotesCUDA by Example : Advanced Atomics : Notes
CUDA by Example : Advanced Atomics : Notes
 
PL/SQL User-Defined Functions in the Read World
PL/SQL User-Defined Functions in the Read WorldPL/SQL User-Defined Functions in the Read World
PL/SQL User-Defined Functions in the Read World
 
Javascript TDD with Jasmine, Karma, and Gulp
Javascript TDD with Jasmine, Karma, and GulpJavascript TDD with Jasmine, Karma, and Gulp
Javascript TDD with Jasmine, Karma, and Gulp
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping new
 
Lecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationLecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administration
 
Switch case and looping kim
Switch case and looping kimSwitch case and looping kim
Switch case and looping kim
 
Buffer overflow tutorial
Buffer overflow tutorialBuffer overflow tutorial
Buffer overflow tutorial
 
Demanding scripting
Demanding scriptingDemanding scripting
Demanding scripting
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from inside
 
Scala to assembly
Scala to assemblyScala to assembly
Scala to assembly
 
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres OpenBruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
 

Más de Stanley Ho

2006 CIC 電子報
2006 CIC 電子報2006 CIC 電子報
2006 CIC 電子報Stanley Ho
 
Linux kernel 2.6 document
Linux kernel 2.6 documentLinux kernel 2.6 document
Linux kernel 2.6 documentStanley Ho
 
LSP Cn Alpha(Revision 77)
LSP Cn Alpha(Revision 77)LSP Cn Alpha(Revision 77)
LSP Cn Alpha(Revision 77)Stanley Ho
 
ACPI In Linux CN
ACPI In Linux CNACPI In Linux CN
ACPI In Linux CNStanley Ho
 
Interrupt In Linux 1.1
Interrupt In Linux 1.1Interrupt In Linux 1.1
Interrupt In Linux 1.1Stanley Ho
 
USB In A Nutshell - Making Sense of the USB Standard.
USB In A Nutshell - Making Sense of the USB Standard.USB In A Nutshell - Making Sense of the USB Standard.
USB In A Nutshell - Making Sense of the USB Standard.Stanley Ho
 
USB Discussion
USB DiscussionUSB Discussion
USB DiscussionStanley Ho
 
2002 5 1 Introduction To Amba Bus System
2002 5 1 Introduction To Amba Bus System2002 5 1 Introduction To Amba Bus System
2002 5 1 Introduction To Amba Bus SystemStanley Ho
 

Más de Stanley Ho (9)

2006 CIC 電子報
2006 CIC 電子報2006 CIC 電子報
2006 CIC 電子報
 
Linux kernel 2.6 document
Linux kernel 2.6 documentLinux kernel 2.6 document
Linux kernel 2.6 document
 
LSP Cn Alpha(Revision 77)
LSP Cn Alpha(Revision 77)LSP Cn Alpha(Revision 77)
LSP Cn Alpha(Revision 77)
 
E Book Mems
E Book MemsE Book Mems
E Book Mems
 
ACPI In Linux CN
ACPI In Linux CNACPI In Linux CN
ACPI In Linux CN
 
Interrupt In Linux 1.1
Interrupt In Linux 1.1Interrupt In Linux 1.1
Interrupt In Linux 1.1
 
USB In A Nutshell - Making Sense of the USB Standard.
USB In A Nutshell - Making Sense of the USB Standard.USB In A Nutshell - Making Sense of the USB Standard.
USB In A Nutshell - Making Sense of the USB Standard.
 
USB Discussion
USB DiscussionUSB Discussion
USB Discussion
 
2002 5 1 Introduction To Amba Bus System
2002 5 1 Introduction To Amba Bus System2002 5 1 Introduction To Amba Bus System
2002 5 1 Introduction To Amba Bus System
 

Último

Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 

Último (20)

Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 

Bluespec Tutorial Helloworld

  • 1. HELLO WORLD c Copyright 2005–2006 Bluespec, Inc. All Rights Reserved. March 30, 2007 1
  • 2. 1 Introduction You have just finished the Bluespec training course, and wish to do your first design in Bluespec. So you are sitting looking at a blank sheet of paper, or a blank computer screen, and wondering what on earth to write. The aim of this tutorial is to get you over this initial hurdle, and start you writing real Bluespec. The first program which software people traditionally write in a new language is one which simply writes “Hello world” on the printer. Let us do the same. This means, of course, that our design is not going to be intended to turn into real hardware: it is merely going to produce output from a simulator. In this respect it is like a top-level test program, rather than a hardware device. (We’ll have a design which produces real hardware as our next example.) 2 Version 1 A Bluespec design is a collection of packages—but in this simple design there will be only one package. So we begin by saying that we are writing a package1 called “FirstAttempt”; and it is good practice to add a comment explaining what it is about. So we write: package FirstAttempt; // My first design in the cool Bluespec language endpackage The compiler will object if the name of the package is not the same as the file it is stored in: that is, the package “FirstAttempt” must be stored in the file “FirstAttempt.bsv”. Type that into the computer, and run the Bluespec compiler on it: bsc FirstAttempt.bsv It should compile successfully, without any warnings. A package may typically contain a few initial definitions of types and constants, and some module definitions. As an example of this, let us give a name to the string we are going to output: package FirstAttempt; // My first design in the cool Bluespec language String s = quot;Hello worldquot;; endpackage Now let us think about the main module in this design. We must first consider how it is going to communicate with other hardware—either other modules in the design or hardware external to the design. That is, we must decide on the type of its interface. In this case the 1 Actually the package/endpackage lines are not strictly necessary, but they are strongly recommended for all but the tiniest temporary test programs. 2
  • 3. answer is easy: the module is not going to communicate with other hardware, so there is no interface to define. By convention, the names of modules start “mk”, to distinguish them from their instan- tiations; so we can type in the shell of the module definition as follows: package FirstAttempt; // My first design in the cool Bluespec language String s = quot;Hello worldquot;; module mkAttempt; endmodule endpackage Perhaps it is worth checking that this also compiles successfully. Finally we add the rules, which define the module’s internal behavior (normally we would also have to add the interface methods, which define its external interactions, but in this example there aren’t any). In this example we have just one rule, which we shall call “say_hello”; this will use the system command “$display” to do the printing. Since this module is to be “synthesized”, either to hardware or (in this case) to a simulator executable, we add the “synthesize” attribute; so the final first draft of our package is as follows. package FirstAttempt; // My first design in the cool Bluespec language String s = quot;Hello worldquot;; (* synthesize *) module mkAttempt(Empty); rule say_hello; $display(s); endrule endmodule endpackage 3 Simulation First we run the BSV compiler tool bsc, to convert our package to Verilog RTL: bsc -verilog FirstAttempt.bsv This produces an RTL file called mkAttempt.v. From this we create and run the simulator executable: bsc -o sim -e mkAttempt mkAttempt.v ./sim 3
  • 4. Here, as explained in the User Guide, the “-o sim” specifies that the executable is to be called sim, and the “-e mkAttempt” says that mkAttempt is the top-level module in the design. Try it and see what happens. You will see that the rule executes on every clock cycle, so that there is an endless stream output of lines saying “Hello world”. 4 Version 2 It is easy to make the output happen only once: we simply add a “$finish” command to the rule. package FirstAttempt; // My first design in the cool Bluespec language String s = quot;Hello worldquot;; (* synthesize *) module mkAttempt(Empty); rule say_hello; $display(s); $finish(0); endrule endmodule endpackage Note that the actions within a rule execute simultaneously in hardware; but system tasks within a rule are performed by a simulator in the order they are written (for otherwise it would be very difficult to achieve the desired output). Thus the “$display” happens before the “$finish”, as desired. 5 Version 3 The next version of this design is intended to output “Hello world” precisely five times. For this we shall need a register to act as a counter. This is declared in the module, before the rule. The counter must be able to count up to 5, so 3 bits will be sufficient, and the appropriate type is accordingly UInt#(3)—that is, an unsigned integer represented in three bits. We shall specify 0 as the register’s initial value (which is restored whenever the design is reset). We add the register definition to the module as follows: 4
  • 5. package FirstAttempt; // My first design in the cool Bluespec language String s = quot;Hello worldquot;; (* synthesize *) module mkAttempt(Empty); Reg#(UInt#(3)) ctr <- mkReg(0); rule say_hello; $display(s); $finish(0); endrule endmodule endpackage The rule must now be revised to increment the counter, and to finish at the appropriate time. package FirstAttempt; // My first design in the cool Bluespec language String s = quot;Hello worldquot;; (* synthesize *) module mkAttempt(Empty); Reg#(UInt#(3)) ctr <- mkReg(0); rule say_hello; ctr <= ctr + 1; $display(s); if (ctr==4) $finish(0); endrule endmodule endpackage Remember that all actions within a rule happen simultaneously. So, even though the state- ment incrementing the counter is written above the if-statement, the latter sees the “old” value of ctr—thus, in order to produce five lines of output, the finish has to occur when that old value is 4. 6 Other Versions This section shows a few alternative versions, some of which work and some of which don’t. 5
  • 6. 6.1 Version 4 Another way of writing this is to use two rules, as follows: package FirstAttempt; // My first design in the cool Bluespec language String s = quot;Hello worldquot;; (* synthesize *) module mkAttempt(Empty); Reg#(UInt#(3)) ctr <- mkReg(0); rule end_run (ctr==5); $finish(0); endrule rule say_hello (ctr<5); ctr <= ctr + 1; $display(s); endrule endmodule endpackage Check that these two versions give the same results. 6.2 Version 5 We might next consider splitting the rules still further, separating incrementing the counter into a rule of its own. This would lead to the following design: 6
  • 7. package FirstAttempt; // My first design in the cool Bluespec language String s = quot;Hello worldquot;; (* synthesize *) module mkAttempt(Empty); Reg#(UInt#(3)) ctr <- mkReg(0); rule end_run (ctr==5); $finish(0); endrule rule say_hello (ctr<5); $display(s); endrule rule inc_ctr; ctr <= ctr + 1; endrule endmodule endpackage What would be the effect of doing this? In fact, it would continue to give the correct result; but notice that we are beginning to live dangerously. If the increment and the display were in the same rule, then we can be sure that they happen either together or not at all. If the increment is in a rule of its own, it is counting clock cycles, not lines of output. If the display rule were held up for any reason (admittedly unlikely in this particular example), the test would finish after the prescribed number of cycles, however many lines had been output by then. The moral is that things which are required to happen together should be put into the same rule—simultaneous operations should not be achieved by relying on two rules firing in the same clock cycle. 6.3 Version 6 In our next version we get still more sloppy—we omit the condition on the rule “say_hello”. We reason that the “end_run” rule will cause the test to terminate at the appropriate time, so there is no need to cease counting explicitly. 7
  • 8. package FirstAttempt; // My first design in the cool Bluespec language String s = quot;Hello worldquot;; (* synthesize *) module mkAttempt(Empty); Reg#(UInt#(3)) ctr <- mkReg(0); rule end_run (ctr==5); $finish(0); endrule rule say_hello; $display(s); endrule rule inc_ctr; ctr <= ctr + 1; endrule endmodule endpackage Now the result is unpredictable. As we know, the behavior of the design always corresponds to the rules being executed “atomically”—that is to say, it is as if the rules executed in a single clock cycle were actually executed one at a time, one after the other. In this example all three rules fire in the final cycle, and the compiler tool has to choose whether “end_run” is to go before “say_hello” or after it. With the design as written above, and today’s version of the tool, the “say_hello” rule will go first, and we shall get six lines of output instead of the desired five. It could be that rearranging the order of the rules would lead to a different outcome—but of course it would be foolish to rely on this, and this version should be considered bad. 6.4 Version 6 Finally we try the two-rule version with the condition of “say_hello” omitted: 8
  • 9. package FirstAttempt; // My first design in the cool Bluespec language String s = quot;Hello worldquot;; (* synthesize *) module mkAttempt(Empty); Reg#(UInt#(3)) ctr <- mkReg(0); rule end_run (ctr==5); $finish(0); endrule rule say_hello; ctr <= ctr + 1; $display(s); endrule endmodule endpackage This version will in fact always give the right result—the order in which the two rules appear to fire “within” the final cycle is no longer arbitrary. But the reason for this is somewhat subtle2 , and so this version should also be regarded as bad, just because its correctness is unnecessarily difficult to understand. 2 The rule “say hello” writes to the register ctr, whereas “end run” merely reads it; so the scheduler must arrange that the overall effect must be as if “end run” executed first (for otherwise, in the actual hardware, where all the reads of the clock cycle happen before any of the writes, “end run” would see a stale value of ctr. Thus the “$finish” happens before the extra “$display” would have happened, so the number of lines output is the same as with the previous version. 9