Wednesday, February 23, 2011

Using RS232 on NiosII processor

Prerequisite Knowledge
This tutorial requires the following hardware and software:
1.      A PC running the XP operating systems.
2.      QuartusII sotware version 7.2 sp3.
3.      NiosII EDS version 7.2 sp3.
4.      MegaCore IP Library version 7.2 sp3.
5.      A Cyclone II DE2 NiosII Develepmont Board.
This tutorial assumes that you are familiar with Quartus II software, Nios II Development Board device and peripherals, NiosII SOPC Builder and fundamental of C programming.
Create the SOPC Builder with the following components.


With these setting parameters:
SDRAM controller:


Cpu:

And copy the Top_Level.v

Open NIOSII IDE and create these following setting.
For main.c


Set the system library properties as


and set the target connection like

Open HyperTerminal , define your own port settings based on computer hardware.

So Run your program and the hyperterminal screen will display. Make sure all RS232 cable already connected into board and computer.
You can key in test characters via keyboard or sending a file.
Click TransferàSend Text File

Browse any file .txt from any folder. They will display all those sample content character in .txt file.


Congratulation!

Wednesday, February 9, 2011

The Hardware acceleration of Pythagoras equations using Altera NiosII Processor Custom Instruction.

Prerequisite Knowledge
This tutorial requires the following hardware and software:
1.      A PC running the XP operating systems.
2.      QuartusII sotware version 7.2 sp3.
3.      NiosII EDS version 7.2 sp3.
4.      MegaCore IP Library version 7.2 sp3.
5.      A Cyclone II DE2 NiosII Develepmont Board.
This tutorial assumes that you are familiar with Quartus II software, Nios II Development Board device and peripherals, NiosII SOPC Builder and fundamental of C programming.
Pythagorean Theorem
The theorem can be written as an equation relating the lengths of the sides a, b and c, often called the Pythagorean equation:
a2+b2=c2
where c represents the length of the hypotenuse, and a and b represent the lengths of the other two sides.
or, solved for c:
c=sqrt(a2+b2)

Block Diagram of Pythagorean Formula

Figure 1: Block Diagram of Pythagorean Formula
Based on Figure 1, this Pythagorean formula uses 2 multipliers, an adder, and  a square root. All these components are floating point unit. In this exercise, we do not to design all these module but we will use MegaWizard LPM IP  on Altera QuartusII software.

Create a new project in Quartus II
1.      Open QuartusII and create a new project.
2.      Click ToolsàMegaWizard Plug-In Manager to Create  a new custom megafunction variation. Then click Next.

4.      Select a megafunction from Arithmetic folder and select ALTFP_MULT,  name it as FP_MULT.v. This wizard will create floating point multiplier 32-bit single precision. Click Next.
4.      Remove unnecessary input/signal output for simplicity of the design. 
5.      Click Finish button to end this wizard. Repeat again from step 2 for module ALTFP_ADD_SUB ( floating point of adder),  and ALTFP _SQRT ( square root).
6.      In this tutorial, some additional components needs to add such as Counter and Register. These component act as control unit. The Counter setting in LPM as follow:



Build a Pythagorean module
1.       Create a new HDL file.


2.       Copy the following code and paste it into a new verilog file, name it as pythagoras.v.

3.       Copy the following code for register , name it as regs.v
4.       Compile pythagoras.v  and observe the compilation report shows utilization resources.

Verify the Pythagorean module design
1.       Lets say we have a simple Pythagorean equation , c = sqrt(a2+b2) with a=3, b=4. Therefore the expected result will be c=5.

These value are floating point representation, but in our Pythagorean module receives value in HEX number.
Floating point number
Hex number  (32-bit)
3
40400000
4
40800000
5
40A00000

2.       Lets perform waveform simulation on Pythagorean module based on test vector (value Hex) in the previous table.
3.       Create a new Vector Waveform File.

4.       Verify the output waveform should appear the expected result at port result in Simulation Report.

Create SOPC System
  1. Click ToolsàSOPC Builder to create new system.

2.       Create this system that consist of SDRAM Controller, Nios II processor, JTAG, and timer. 
      3.   The SOPC system structure is shown in figure below.


      4.     Now we will add new component. Click Fileà New Component. Then Component Editor windows appear, click HDL Files tab. Then Click Add HDl File… button and browse to pythagoras.v file from your work folder. Then Top Level Module assign to Pythagoras.


5.       Then click Next button or Signal tab, change all interface and signal type into nios_custom_instruction_slave and respective signal as shown in following figure. Then click Next button.

6.         Click Remove Interfaces With No Signals then change operands 2, NPort 0 and clockCycleType  to Multicycle. Then click Next.



7.        Add Component Display Name as Pythagoras and Component Group to User Custom Instruction. Then click Finish button.

8.     Then click Yes, Save button.


9.       Double click on cpu component (Nios II Processor). Then click  Custom Instructions tab. Click Pythagoras, then click Add button. Now you can change Name to pythagoras_inst. Click Finish button.

10.      If there is error, just double click the error prompt, and change width to 32.


11.           Finally click Generate button. Then click Exit button.

Compile SOPC System
1.       Create PLL module using MegaWizard function (pls refer previous tutorial/technical report to create PLL module) and copy the following code into a new HDL file. Name it as Top_Level.v

2.       Import DE2_pin_assigment.csv before compile it.
Design the Embedded Software System
1.       Open NiosII IDE program to start NiosII IDE. Create a new NIOSII C/C++ Application.
2.       Browse the ptf file reside in your work folder.
3.       Create a new Source File , name it as main.c
4.       Copy and paste the following c code and save it.


       
5.      Debug and run as NiosII Hardware.
6.      Verify the functionality and observe the performance


Observation and Conclusion
  1. Lets benchmarked some example in the table below:


Pythagoras Equation
c=sqrt(a2+b2)
Test No
Input
Output
a
c
c
1
3
4
58
2
-53
34
62.968
3
0.256
0.614
0.6652
4
-5
-9.6
10.82


  1. Result on NiosII Console as shown in figure below.
Test No 1:
Test No 2:

Test No 3:

Test No 4:


3.     Result for performance
Test No
No of Software ticks
No of Hardware  (Custom Instruction)ticks
Speed Gain
1
9581
313
30
2
11031
307
35
3
11883
312
38
4
11392
292
39


4.        As conclusion from this experiment shows that the custom instruction can speed the pythagoras 32-bit single precision floating point up to 40X compare with software based calculations.