You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> <str<strong>on</strong>g>Memory</str<strong>on</strong>g> <strong>on</strong> Altera’s <strong>DE2</strong> <strong>Board</strong><br />
This tutorial explains how <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> chip <strong>on</strong> Altera’s <strong>DE2</strong> Development and Educati<strong>on</strong> board can be used<br />
with a Nios II system implemented by using <str<strong>on</strong>g>the</str<strong>on</strong>g> Altera SOPC Builder. The discussi<strong>on</strong> is based <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> assumpti<strong>on</strong><br />
that <str<strong>on</strong>g>the</str<strong>on</strong>g> reader has access to a <strong>DE2</strong> board and is familiar with <str<strong>on</strong>g>the</str<strong>on</strong>g> material in <str<strong>on</strong>g>the</str<strong>on</strong>g> tutorial Introducti<strong>on</strong> to <str<strong>on</strong>g>the</str<strong>on</strong>g> Altera<br />
SOPC Builder.<br />
The screen captures in <str<strong>on</strong>g>the</str<strong>on</strong>g> tutorial were obtained using <str<strong>on</strong>g>the</str<strong>on</strong>g> Quartus II versi<strong>on</strong> 5.1; if o<str<strong>on</strong>g>the</str<strong>on</strong>g>r versi<strong>on</strong>s of <str<strong>on</strong>g>the</str<strong>on</strong>g><br />
software are used, some of <str<strong>on</strong>g>the</str<strong>on</strong>g> images may be slightly different.<br />
C<strong>on</strong>tents:<br />
Example Nios II System<br />
The <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> Interface<br />
<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> SOPC Builder to Generate <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II System<br />
Integrati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II System into <str<strong>on</strong>g>the</str<strong>on</strong>g> Quartus II Project<br />
<str<strong>on</strong>g>Using</str<strong>on</strong>g> a Phase-Locked Loop<br />
1
The introductory tutorial Introducti<strong>on</strong> to <str<strong>on</strong>g>the</str<strong>on</strong>g> Altera SOPC Builder explains how <str<strong>on</strong>g>the</str<strong>on</strong>g> memory in <str<strong>on</strong>g>the</str<strong>on</strong>g> Cycl<strong>on</strong>e II<br />
FPGA chip can be used in <str<strong>on</strong>g>the</str<strong>on</strong>g> c<strong>on</strong>text of a simple Nios II system. For practical applicati<strong>on</strong>s it is necessary to have<br />
a much larger memory. The Altera <strong>DE2</strong> board c<strong>on</strong>tains an <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> chip that can store 8 Mbytes of data. This<br />
memory is organized as 1M x 16 bits x 4 banks. The <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> chip requires careful timing c<strong>on</strong>trol. To provide<br />
access to <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> chip, <str<strong>on</strong>g>the</str<strong>on</strong>g> SOPC Builder implements an <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> C<strong>on</strong>troller circuit. This circuit generates<br />
<str<strong>on</strong>g>the</str<strong>on</strong>g> signals needed to deal with <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> chip.<br />
1 Example Nios II System<br />
As an illustrative example, we will add <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> to <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II system described in <str<strong>on</strong>g>the</str<strong>on</strong>g> Introducti<strong>on</strong> to <str<strong>on</strong>g>the</str<strong>on</strong>g><br />
Altera SOPC Builder tutorial. Figure 1 gives <str<strong>on</strong>g>the</str<strong>on</strong>g> block diagram of our example system.<br />
Host computer<br />
Reset_n<br />
Clock<br />
USB-Blaster<br />
interface<br />
Nios II processor<br />
JTAG Debug<br />
module<br />
JTAG UART<br />
interface<br />
Cycl<strong>on</strong>e II<br />
FPGA chip<br />
Aval<strong>on</strong> switch fabric<br />
On-chip<br />
memory<br />
<str<strong>on</strong>g>SDRAM</str<strong>on</strong>g><br />
c<strong>on</strong>troller<br />
Switches<br />
parallel input<br />
interface<br />
LEDs<br />
parallel output<br />
interface<br />
<str<strong>on</strong>g>SDRAM</str<strong>on</strong>g><br />
chip<br />
SW7 SW0 LEDG7 LEDG0<br />
Figure 1. Example Nios II system implemented <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> board.<br />
The system realizes a trivial task. Eight toggle switches <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> board, SW7−0, are used to turn <strong>on</strong> or off<br />
<str<strong>on</strong>g>the</str<strong>on</strong>g> eight green LEDs, LEDG7 − 0. The switches are c<strong>on</strong>nected to <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II system by means of a parallel I/O<br />
2
interface c<strong>on</strong>figured to act as an input port. The LEDs are driven by <str<strong>on</strong>g>the</str<strong>on</strong>g> signals from ano<str<strong>on</strong>g>the</str<strong>on</strong>g>r parallel I/O interface<br />
c<strong>on</strong>figured to act as an output port. To achieve <str<strong>on</strong>g>the</str<strong>on</strong>g> desired operati<strong>on</strong>, <str<strong>on</strong>g>the</str<strong>on</strong>g> eight-bit pattern corresp<strong>on</strong>ding to <str<strong>on</strong>g>the</str<strong>on</strong>g><br />
state of <str<strong>on</strong>g>the</str<strong>on</strong>g> switches has to be sent to <str<strong>on</strong>g>the</str<strong>on</strong>g> output port to activate <str<strong>on</strong>g>the</str<strong>on</strong>g> LEDs. This will be d<strong>on</strong>e by having <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II<br />
processor execute an applicati<strong>on</strong> program. C<strong>on</strong>tinuous operati<strong>on</strong> is required, such that as <str<strong>on</strong>g>the</str<strong>on</strong>g> switches are toggled<br />
<str<strong>on</strong>g>the</str<strong>on</strong>g> lights change accordingly.<br />
The introductory tutorial showed how we can use <str<strong>on</strong>g>the</str<strong>on</strong>g> SOPC Builder to design <str<strong>on</strong>g>the</str<strong>on</strong>g> hardware needed to implement<br />
this task, assuming that <str<strong>on</strong>g>the</str<strong>on</strong>g> applicati<strong>on</strong> program which reads <str<strong>on</strong>g>the</str<strong>on</strong>g> state of <str<strong>on</strong>g>the</str<strong>on</strong>g> toggle switches and sets <str<strong>on</strong>g>the</str<strong>on</strong>g><br />
green LEDs accordingly is loaded into a memory block in <str<strong>on</strong>g>the</str<strong>on</strong>g> FPGA chip. In this tutorial, we will explain how <str<strong>on</strong>g>the</str<strong>on</strong>g><br />
<str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> chip <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> board can be included in <str<strong>on</strong>g>the</str<strong>on</strong>g> system in Figure 1, so that our applicati<strong>on</strong> program can be<br />
run from <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> ra<str<strong>on</strong>g>the</str<strong>on</strong>g>r than from <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>on</strong>-chip memory.<br />
Doing this tutorial, <str<strong>on</strong>g>the</str<strong>on</strong>g> reader will learn about:<br />
• <str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> SOPC Builder to include an <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> interface for a Nios II-based system<br />
• Timing issues with respect to <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> board<br />
• <str<strong>on</strong>g>Using</str<strong>on</strong>g> a phase-locked loop (PLL) to c<strong>on</strong>trol <str<strong>on</strong>g>the</str<strong>on</strong>g> clock timing<br />
2 The <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> Interface<br />
The <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> chip <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> board has <str<strong>on</strong>g>the</str<strong>on</strong>g> capacity of 64 Mbits (8 Mbytes). It is organized as 1M x 16 bits x<br />
4 banks. The signals needed to communicate with this chip are shown in Figure 2. All of <str<strong>on</strong>g>the</str<strong>on</strong>g> signals, except <str<strong>on</strong>g>the</str<strong>on</strong>g><br />
clock, can be provided by <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> C<strong>on</strong>troller that can be generated by using <str<strong>on</strong>g>the</str<strong>on</strong>g> SOPC Builder. The clock<br />
signal is provided separately. It has to meet <str<strong>on</strong>g>the</str<strong>on</strong>g> clock-skew requirements as explained in secti<strong>on</strong> 5. Note that some<br />
signals are active low, which is denoted by <str<strong>on</strong>g>the</str<strong>on</strong>g> suffix N.<br />
Clock<br />
<str<strong>on</strong>g>SDRAM</str<strong>on</strong>g><br />
c<strong>on</strong>troller<br />
Clock Enable<br />
Address<br />
Bank Address 1<br />
Bank Address 0<br />
Chip Select<br />
Column Address Strobe<br />
Row Address Strobe<br />
Write Enable<br />
Data<br />
High-byte Data Mask<br />
Low-byte Data Mask<br />
CLK<br />
CKE_N<br />
ADDR[11:0]<br />
BA1<br />
BA0<br />
CS_N<br />
CAS_N<br />
RAS_N<br />
WE_N<br />
DQ[15:0]<br />
UDQM<br />
LDQM<br />
<str<strong>on</strong>g>SDRAM</str<strong>on</strong>g><br />
chip<br />
Figure 2. The <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> signals.<br />
3
3 <str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> SOPC Builder to Generate <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II System<br />
Our starting point will be <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II system discussed in <str<strong>on</strong>g>the</str<strong>on</strong>g> Introducti<strong>on</strong> to <str<strong>on</strong>g>the</str<strong>on</strong>g> Altera SOPC Builder tutorial,<br />
which we implemented in a project called lights. We specified <str<strong>on</strong>g>the</str<strong>on</strong>g> system shown in Figure 3.<br />
Figure 3. The Nios II system defined in <str<strong>on</strong>g>the</str<strong>on</strong>g> introductory tutorial.<br />
If you saved <str<strong>on</strong>g>the</str<strong>on</strong>g> lights project, <str<strong>on</strong>g>the</str<strong>on</strong>g>n open this project in <str<strong>on</strong>g>the</str<strong>on</strong>g> Quartus II software and <str<strong>on</strong>g>the</str<strong>on</strong>g>n open <str<strong>on</strong>g>the</str<strong>on</strong>g> SOPC<br />
Builder. O<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise, you need to create and implement <str<strong>on</strong>g>the</str<strong>on</strong>g> project, as explained in <str<strong>on</strong>g>the</str<strong>on</strong>g> introductory tutorial, to<br />
obtain <str<strong>on</strong>g>the</str<strong>on</strong>g> system shown in <str<strong>on</strong>g>the</str<strong>on</strong>g> figure.<br />
To add <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g>, in <str<strong>on</strong>g>the</str<strong>on</strong>g> window of Figure 3 select Aval<strong>on</strong> Comp<strong>on</strong>ents > <str<strong>on</strong>g>Memory</str<strong>on</strong>g> > <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> C<strong>on</strong>troller<br />
and click Add. A window depicted in Figure 4 appears. Set <str<strong>on</strong>g>the</str<strong>on</strong>g> Data Width parameter to 16 bits and leave<br />
<str<strong>on</strong>g>the</str<strong>on</strong>g> default values for <str<strong>on</strong>g>the</str<strong>on</strong>g> rest. Since we will not simulate <str<strong>on</strong>g>the</str<strong>on</strong>g> system in this tutorial, do not select <str<strong>on</strong>g>the</str<strong>on</strong>g> opti<strong>on</strong><br />
Include a functi<strong>on</strong>al memory model in <str<strong>on</strong>g>the</str<strong>on</strong>g> system testbench. Click Finish. Now, in <str<strong>on</strong>g>the</str<strong>on</strong>g> window of Figure 3,<br />
<str<strong>on</strong>g>the</str<strong>on</strong>g>re will be an sdram_0 module added to <str<strong>on</strong>g>the</str<strong>on</strong>g> design. Since <str<strong>on</strong>g>the</str<strong>on</strong>g>re is <strong>on</strong>ly <strong>on</strong>e <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> board, change<br />
<str<strong>on</strong>g>the</str<strong>on</strong>g> name of this module to simply sdram. Then, <str<strong>on</strong>g>the</str<strong>on</strong>g> expanded system is defined as indicated in Figure 5. Observe<br />
that <str<strong>on</strong>g>the</str<strong>on</strong>g> SOPC Builder assigned <str<strong>on</strong>g>the</str<strong>on</strong>g> base address 0x00800000 to <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g>. Leave <str<strong>on</strong>g>the</str<strong>on</strong>g> addresses of all modules<br />
as assigned in <str<strong>on</strong>g>the</str<strong>on</strong>g> figure and regenerate <str<strong>on</strong>g>the</str<strong>on</strong>g> system.<br />
4
Figure 4. Add <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> C<strong>on</strong>troller.<br />
Figure 5. The expanded Nios II system.<br />
The augmented Verilog module generated by <str<strong>on</strong>g>the</str<strong>on</strong>g> SOPC Builder is in <str<strong>on</strong>g>the</str<strong>on</strong>g> file nios_system.v in <str<strong>on</strong>g>the</str<strong>on</strong>g> directory<br />
of <str<strong>on</strong>g>the</str<strong>on</strong>g> project. Figure 6 depicts <str<strong>on</strong>g>the</str<strong>on</strong>g> porti<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> code that defines <str<strong>on</strong>g>the</str<strong>on</strong>g> input and output signals for <str<strong>on</strong>g>the</str<strong>on</strong>g> module<br />
nios_system. As in our initial system that we developed in <str<strong>on</strong>g>the</str<strong>on</strong>g> introductory tutorial, <str<strong>on</strong>g>the</str<strong>on</strong>g> 8-bit vector that<br />
is <str<strong>on</strong>g>the</str<strong>on</strong>g> input to <str<strong>on</strong>g>the</str<strong>on</strong>g> parallel port Switches is called in_port_to_<str<strong>on</strong>g>the</str<strong>on</strong>g>_Switches. The 8-bit output vector is called<br />
out_port_from_<str<strong>on</strong>g>the</str<strong>on</strong>g>_LEDs. The clock and reset signals are called clk and reset_n, respectively. A new module,<br />
called sdram, is included. It involves <str<strong>on</strong>g>the</str<strong>on</strong>g> signals indicated in Figure 2. For example, <str<strong>on</strong>g>the</str<strong>on</strong>g> address lines are referred<br />
to as <str<strong>on</strong>g>the</str<strong>on</strong>g> output vector zs_addr_from_<str<strong>on</strong>g>the</str<strong>on</strong>g>_sdram[11:0]. The data lines are referred to as <str<strong>on</strong>g>the</str<strong>on</strong>g> inout vector<br />
zs_dq_to_and_from_<str<strong>on</strong>g>the</str<strong>on</strong>g>_sdram[15:0]. This is a vector of <str<strong>on</strong>g>the</str<strong>on</strong>g> inout type because <str<strong>on</strong>g>the</str<strong>on</strong>g> data lines are bidirecti<strong>on</strong>al.<br />
5
Figure 6. A part of <str<strong>on</strong>g>the</str<strong>on</strong>g> generated Verilog module.<br />
4 Integrati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II System into <str<strong>on</strong>g>the</str<strong>on</strong>g> Quartus II Project<br />
Now, we have to instantiate <str<strong>on</strong>g>the</str<strong>on</strong>g> expanded Nios II system in <str<strong>on</strong>g>the</str<strong>on</strong>g> top-level Verilog module, as we have d<strong>on</strong>e in <str<strong>on</strong>g>the</str<strong>on</strong>g><br />
tutorial Introducti<strong>on</strong> to <str<strong>on</strong>g>the</str<strong>on</strong>g> Altera SOPC Builder. The module is named lights, because this is <str<strong>on</strong>g>the</str<strong>on</strong>g> name of <str<strong>on</strong>g>the</str<strong>on</strong>g><br />
top-level design entity in our Quartus II project.<br />
A first attempt at creating <str<strong>on</strong>g>the</str<strong>on</strong>g> new module is presented in Figure 7. The input and output ports of <str<strong>on</strong>g>the</str<strong>on</strong>g> module<br />
use <str<strong>on</strong>g>the</str<strong>on</strong>g> pin names for <str<strong>on</strong>g>the</str<strong>on</strong>g> 50-MHz clock, CLOCK_50, pushbutt<strong>on</strong> switches, KEY, toggle switches, SW, and<br />
green LEDs, LEDG, as used in our original design. They also use <str<strong>on</strong>g>the</str<strong>on</strong>g> pin names DRAM_CLK, DRAM_CKE_N,<br />
DRAM_ADDR, DRAM_BA_1, DRAM_BA_0, DRAM_CS_N, DRAM_CAS_N, DRAM_RAS_N, DRAM_WE_N,<br />
DRAM_DQ, DRAM_UDQM, and DRAM_LDQM, which corresp<strong>on</strong>d to <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> signals indicated in Figure 2.<br />
All of <str<strong>on</strong>g>the</str<strong>on</strong>g>se names are those specified in <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> User Manual, which allows us to make <str<strong>on</strong>g>the</str<strong>on</strong>g> pin assignments by<br />
importing <str<strong>on</strong>g>the</str<strong>on</strong>g>m from <str<strong>on</strong>g>the</str<strong>on</strong>g> file called <strong>DE2</strong>_pin_assignments.csv in <str<strong>on</strong>g>the</str<strong>on</strong>g> directory <strong>DE2</strong>_tutorials\design_files, which<br />
is included <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> CD-ROM that accompanies <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> board and can also be found <strong>on</strong> Altera’s <strong>DE2</strong> web pages.<br />
Observe that <str<strong>on</strong>g>the</str<strong>on</strong>g> two Bank Address signals are treated by <str<strong>on</strong>g>the</str<strong>on</strong>g> SOPC Builder as a two-bit vector called<br />
zs_ba_from_<str<strong>on</strong>g>the</str<strong>on</strong>g>_sdram[1:0], as seen in Figure 6. However, in <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong>_pin_assignments.csv file <str<strong>on</strong>g>the</str<strong>on</strong>g>se signals<br />
are given as scalars DRAM_BA_1 and DRAM_BA_0. Therefore, in our Verilog module, we c<strong>on</strong>catenated <str<strong>on</strong>g>the</str<strong>on</strong>g>se<br />
signals as {DRAM_BA_1, DRAM_BA_0}. Similarly, <str<strong>on</strong>g>the</str<strong>on</strong>g> vector zs_dqm_from_<str<strong>on</strong>g>the</str<strong>on</strong>g>_sdram[1:0] corresp<strong>on</strong>ds to<br />
{DRAM_UDQM, DRAM_LDQM}.<br />
Finally, note that we tried an obvious approach of using <str<strong>on</strong>g>the</str<strong>on</strong>g> 50-MHz system clock, CLOCK_50, as <str<strong>on</strong>g>the</str<strong>on</strong>g> clock<br />
signal, DRAM_CLK, for <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> chip. This is specified by <str<strong>on</strong>g>the</str<strong>on</strong>g> assign statement in <str<strong>on</strong>g>the</str<strong>on</strong>g> code. This approach<br />
leads to a potential timing problem caused by <str<strong>on</strong>g>the</str<strong>on</strong>g> clock skew <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> board, which can be fixed as explained<br />
in secti<strong>on</strong> 5.<br />
6
Implements <str<strong>on</strong>g>the</str<strong>on</strong>g> augmented Nios II system for <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> board.<br />
// Inputs: SW7−0 are parallel port inputs to <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II system.<br />
// CLOCK_50 is <str<strong>on</strong>g>the</str<strong>on</strong>g> system clock.<br />
// KEY0 is <str<strong>on</strong>g>the</str<strong>on</strong>g> active-low system reset.<br />
// Outputs: LEDG7−0 are parallel port outputs from <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II system.<br />
// <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> ports corresp<strong>on</strong>d to <str<strong>on</strong>g>the</str<strong>on</strong>g> signals in Figure 2; <str<strong>on</strong>g>the</str<strong>on</strong>g>ir names are those<br />
// used in <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> User Manual.<br />
module lights (SW, KEY, CLOCK_50, LEDG, DRAM_CLK, DRAM_CKE_N,<br />
DRAM_ADDR, DRAM_BA_1, DRAM_BA_0, DRAM_CS_N, DRAM_CAS_N, DRAM_RAS_N,<br />
DRAM_WE_N, DRAM_DQ, DRAM_UDQM, DRAM_LDQM);<br />
input [7:0] SW;<br />
input [0:0] KEY;<br />
input CLOCK_50;<br />
output [7:0] LEDG;<br />
output [11:0] DRAM_ADDR;<br />
output DRAM_BA_1, DRAM_BA_0, DRAM_CAS_N, DRAM_RAS_N, DRAM_CLK;<br />
output DRAM_CKE, DRAM_CS_N, DRAM_WE_N, DRAM_UDQM, DRAM_LDQM;<br />
inout [15:0] DRAM_DQ;<br />
// Instantiate <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II system module generated by <str<strong>on</strong>g>the</str<strong>on</strong>g> SOPC Builder<br />
nios_system NiosII (<br />
CLOCK_50,<br />
KEY[0],<br />
LEDG,<br />
SW,<br />
DRAM_ADDR,<br />
{DRAM_BA_1, DRAM_BA_0},<br />
DRAM_CAS_N,<br />
DRAM_CKE,<br />
DRAM_CS_N,<br />
DRAM_DQ,<br />
{DRAM_UDQM, DRAM_LDQM},<br />
DRAM_RAS_N,<br />
DRAM_WE_N);<br />
assign DRAM_CLK = CLOCK_50;<br />
endmodule<br />
Figure 7. A first attempt at instantiating <str<strong>on</strong>g>the</str<strong>on</strong>g> expanded Nios II system.<br />
As an experiment, you can enter <str<strong>on</strong>g>the</str<strong>on</strong>g> code in Figure 7 into a file called lights.v. Add this file and all <str<strong>on</strong>g>the</str<strong>on</strong>g> *.v<br />
files produced by <str<strong>on</strong>g>the</str<strong>on</strong>g> SOPC Builder to your Quartus II project. Compile <str<strong>on</strong>g>the</str<strong>on</strong>g> code and download <str<strong>on</strong>g>the</str<strong>on</strong>g> design into<br />
<str<strong>on</strong>g>the</str<strong>on</strong>g> Cycl<strong>on</strong>e II FPGA <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> board. Use <str<strong>on</strong>g>the</str<strong>on</strong>g> applicati<strong>on</strong> program from <str<strong>on</strong>g>the</str<strong>on</strong>g> tutorial Introducti<strong>on</strong> to <str<strong>on</strong>g>the</str<strong>on</strong>g> Altera<br />
SOPC Builder, which is shown in Figure 8.<br />
7
.include "nios_macros.s"<br />
.equ Switches, 0x00001800<br />
.equ LEDs, 0x00001810<br />
GFUNC _start<br />
movia r2, Switches<br />
movia r3, LEDs<br />
loop: ldbio r4, 0(r2)<br />
stbio r4, 0(r3)<br />
br loop<br />
BREAK<br />
Figure 8. Assembly language code to c<strong>on</strong>trol <str<strong>on</strong>g>the</str<strong>on</strong>g> lights.<br />
Use <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II Debug Client, which is described in <str<strong>on</strong>g>the</str<strong>on</strong>g> tutorial Nios II Debug Client, to assemble, download,<br />
and run this applicati<strong>on</strong> program. If successful, <str<strong>on</strong>g>the</str<strong>on</strong>g> lights <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> board will resp<strong>on</strong>d to <str<strong>on</strong>g>the</str<strong>on</strong>g> operati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g><br />
toggle switches.<br />
Due to <str<strong>on</strong>g>the</str<strong>on</strong>g> clock skew problem menti<strong>on</strong>ed above, <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II processor may be unable to properly access <str<strong>on</strong>g>the</str<strong>on</strong>g><br />
<str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> chip. A possible indicati<strong>on</strong> of this may be given by <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II Debug Client, which may display <str<strong>on</strong>g>the</str<strong>on</strong>g><br />
message depicted in Figure 9. To solve <str<strong>on</strong>g>the</str<strong>on</strong>g> problem, it is necessary to modify <str<strong>on</strong>g>the</str<strong>on</strong>g> design as indicated in <str<strong>on</strong>g>the</str<strong>on</strong>g> next<br />
secti<strong>on</strong>.<br />
Figure 9. An error message.<br />
8
5 <str<strong>on</strong>g>Using</str<strong>on</strong>g> a Phase-Locked Loop<br />
The clock skew depends <strong>on</strong> physical characteristics of <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> board. For proper operati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> chip,<br />
it is necessary that its clock signal, DRAM_CLK, leads <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II system clock, CLOCK_50, by 3 nanosec<strong>on</strong>ds.<br />
This can be accomplished by using a phase-locked loop (PLL) circuit. There exists a Quartus II Megafuncti<strong>on</strong>,<br />
called ALTPLL, which can be used to generate <str<strong>on</strong>g>the</str<strong>on</strong>g> desired circuit. The circuit can be created, by using <str<strong>on</strong>g>the</str<strong>on</strong>g> Quartus<br />
II MegaWizard Plug-In Manager, as follows:<br />
1. Select Tools > MegaWizard Plug-In Manager. This leads to <str<strong>on</strong>g>the</str<strong>on</strong>g> window in Figure 10. Choose <str<strong>on</strong>g>the</str<strong>on</strong>g> acti<strong>on</strong><br />
Create a new custom megafuncti<strong>on</strong> variati<strong>on</strong> and click Next.<br />
Figure 10. The MegaWizard.<br />
2. In <str<strong>on</strong>g>the</str<strong>on</strong>g> window in Figure 11, specify that Cycl<strong>on</strong>e II is <str<strong>on</strong>g>the</str<strong>on</strong>g> device family used and that <str<strong>on</strong>g>the</str<strong>on</strong>g> circuit should be<br />
defined in Verilog HDL. Also, specify that <str<strong>on</strong>g>the</str<strong>on</strong>g> generated output (Verilog) file should be called sdram_pll.v.<br />
From <str<strong>on</strong>g>the</str<strong>on</strong>g> list of megafuncti<strong>on</strong>s in <str<strong>on</strong>g>the</str<strong>on</strong>g> left box select I/O > ALTPLL. Click Next.<br />
Figure 11. Select <str<strong>on</strong>g>the</str<strong>on</strong>g> megafuncti<strong>on</strong> and name <str<strong>on</strong>g>the</str<strong>on</strong>g> output file.<br />
9
3. In Figure 12, specify that <str<strong>on</strong>g>the</str<strong>on</strong>g> frequency of <str<strong>on</strong>g>the</str<strong>on</strong>g> inclock0 input is 50 MHz. Leave <str<strong>on</strong>g>the</str<strong>on</strong>g> o<str<strong>on</strong>g>the</str<strong>on</strong>g>r parameters as<br />
given by default. Click Next to reach <str<strong>on</strong>g>the</str<strong>on</strong>g> window in Figure 13.<br />
Figure 12. Define <str<strong>on</strong>g>the</str<strong>on</strong>g> clock frequency.<br />
Figure 13. Remove unnecessary signals.<br />
10
4. We are interested <strong>on</strong>ly in <str<strong>on</strong>g>the</str<strong>on</strong>g> input signal inclock0 and <str<strong>on</strong>g>the</str<strong>on</strong>g> output signal c0. Remove <str<strong>on</strong>g>the</str<strong>on</strong>g> o<str<strong>on</strong>g>the</str<strong>on</strong>g>r two signals<br />
shown in <str<strong>on</strong>g>the</str<strong>on</strong>g> block diagram in <str<strong>on</strong>g>the</str<strong>on</strong>g> figure by de-selecting <str<strong>on</strong>g>the</str<strong>on</strong>g> opti<strong>on</strong>al input areset as well as <str<strong>on</strong>g>the</str<strong>on</strong>g> locked<br />
output, as indicated in <str<strong>on</strong>g>the</str<strong>on</strong>g> figure. Click Next <strong>on</strong> this page as well as <strong>on</strong> page 5, until you reach page 6 which<br />
is shown in Figure 14.<br />
Figure 14. Specify <str<strong>on</strong>g>the</str<strong>on</strong>g> phase shift.<br />
5. The shifted clock signal is called c0. Specify that a phase shift of −3 ns is required, as indicated in <str<strong>on</strong>g>the</str<strong>on</strong>g><br />
figure. Click Finish, which advances to page 9.<br />
6. In <str<strong>on</strong>g>the</str<strong>on</strong>g> summary window in Figure 15 click Finish to complete <str<strong>on</strong>g>the</str<strong>on</strong>g> process.<br />
11
Figure 15. The summary page.<br />
The desired PLL circuit is now defined as a Verilog module in <str<strong>on</strong>g>the</str<strong>on</strong>g> file sdram_pll.v, which is placed in <str<strong>on</strong>g>the</str<strong>on</strong>g><br />
project directory. Add this file to <str<strong>on</strong>g>the</str<strong>on</strong>g> lights project. Figure 16 shows <str<strong>on</strong>g>the</str<strong>on</strong>g> module ports, c<strong>on</strong>sisting of signals inclk0<br />
and c0.<br />
Figure 16. The generated PLL module.<br />
Next, we have to fix <str<strong>on</strong>g>the</str<strong>on</strong>g> top-level Verilog module, given in Figure 7, to include <str<strong>on</strong>g>the</str<strong>on</strong>g> PLL circuit. The desired<br />
code is shown in Figure 17. The PLL circuit c<strong>on</strong>nects <str<strong>on</strong>g>the</str<strong>on</strong>g> shifted clock output c0 to <str<strong>on</strong>g>the</str<strong>on</strong>g> pin DRAM_CLK.<br />
12
Implements <str<strong>on</strong>g>the</str<strong>on</strong>g> augmented Nios II system for <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> board.<br />
// Inputs: SW7−0 are parallel port inputs to <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II system.<br />
// CLOCK_50 is <str<strong>on</strong>g>the</str<strong>on</strong>g> system clock.<br />
// KEY0 is <str<strong>on</strong>g>the</str<strong>on</strong>g> active-low system reset.<br />
// Outputs: LEDG7−0 are parallel port outputs from <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II system.<br />
// <str<strong>on</strong>g>SDRAM</str<strong>on</strong>g> ports corresp<strong>on</strong>d to <str<strong>on</strong>g>the</str<strong>on</strong>g> signals in Figure 2; <str<strong>on</strong>g>the</str<strong>on</strong>g>ir names are those<br />
// used in <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> User Manual.<br />
module lights (SW, KEY, CLOCK_50, LEDG, DRAM_CLK, DRAM_CKE_N,<br />
DRAM_ADDR, DRAM_BA_1, DRAM_BA_0, DRAM_CS_N, DRAM_CAS_N, DRAM_RAS_N,<br />
DRAM_WE_N, DRAM_DQ, DRAM_UDQM, DRAM_LDQM);<br />
input [7:0] SW;<br />
input [0:0] KEY;<br />
input CLOCK_50;<br />
output [7:0] LEDG;<br />
output [11:0] DRAM_ADDR;<br />
output DRAM_BA_1, DRAM_BA_0, DRAM_CAS_N, DRAM_RAS_N, DRAM_CLK;<br />
output DRAM_CKE, DRAM_CS_N, DRAM_WE_N, DRAM_UDQM, DRAM_LDQM;<br />
inout [15:0] DRAM_DQ;<br />
// Instantiate <str<strong>on</strong>g>the</str<strong>on</strong>g> Nios II system module generated by <str<strong>on</strong>g>the</str<strong>on</strong>g> SOPC Builder<br />
nios_system NiosII (<br />
CLOCK_50,<br />
KEY[0],<br />
LEDG,<br />
SW,<br />
DRAM_ADDR,<br />
{DRAM_BA_1, DRAM_BA_0},<br />
DRAM_CAS_N,<br />
DRAM_CKE,<br />
DRAM_CS_N,<br />
DRAM_DQ,<br />
{DRAM_UDQM, DRAM_LDQM},<br />
DRAM_RAS_N,<br />
DRAM_WE_N);<br />
// Instantiate <str<strong>on</strong>g>the</str<strong>on</strong>g> module sdram_pll (inclk0, c0)<br />
sdram_pll neg_3ns (CLOCK_50, DRAM_CLK);<br />
endmodule<br />
Figure 17. Proper instantiati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> expanded Nios II system.<br />
Compile <str<strong>on</strong>g>the</str<strong>on</strong>g> code and download <str<strong>on</strong>g>the</str<strong>on</strong>g> design into <str<strong>on</strong>g>the</str<strong>on</strong>g> Cycl<strong>on</strong>e II FPGA <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>DE2</strong> board. Use <str<strong>on</strong>g>the</str<strong>on</strong>g> applicati<strong>on</strong><br />
program in Figure 8 to test <str<strong>on</strong>g>the</str<strong>on</strong>g> circuit.<br />
13
Copyright c○2006 Altera Corporati<strong>on</strong>. All rights reserved. Altera, The Programmable Soluti<strong>on</strong>s Company, <str<strong>on</strong>g>the</str<strong>on</strong>g><br />
stylized Altera logo, specific device designati<strong>on</strong>s, and all o<str<strong>on</strong>g>the</str<strong>on</strong>g>r words and logos that are identified as trademarks<br />
and/or service marks are, unless noted o<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise, <str<strong>on</strong>g>the</str<strong>on</strong>g> trademarks and service marks of Altera Corporati<strong>on</strong> in<br />
<str<strong>on</strong>g>the</str<strong>on</strong>g> U.S. and o<str<strong>on</strong>g>the</str<strong>on</strong>g>r countries. All o<str<strong>on</strong>g>the</str<strong>on</strong>g>r product or service names are <str<strong>on</strong>g>the</str<strong>on</strong>g> property of <str<strong>on</strong>g>the</str<strong>on</strong>g>ir respective holders.<br />
Altera products are protected under numerous U.S. and foreign patents and pending applicati<strong>on</strong>s, mask work<br />
rights, and copyrights. Altera warrants performance of its semic<strong>on</strong>ductor products to current specificati<strong>on</strong>s in<br />
accordance with Altera’s standard warranty, but reserves <str<strong>on</strong>g>the</str<strong>on</strong>g> right to make changes to any products and services at<br />
any time without notice. Altera assumes no resp<strong>on</strong>sibility or liability arising out of <str<strong>on</strong>g>the</str<strong>on</strong>g> applicati<strong>on</strong> or use of any<br />
informati<strong>on</strong>, product, or service described herein except as expressly agreed to in writing by Altera Corporati<strong>on</strong>.<br />
Altera customers are advised to obtain <str<strong>on</strong>g>the</str<strong>on</strong>g> latest versi<strong>on</strong> of device specificati<strong>on</strong>s before relying <strong>on</strong> any published<br />
informati<strong>on</strong> and before placing orders for products or services.<br />
This document is being provided <strong>on</strong> an “as-is” basis and as an accommodati<strong>on</strong> and <str<strong>on</strong>g>the</str<strong>on</strong>g>refore all warranties, representati<strong>on</strong>s<br />
or guarantees of any kind (whe<str<strong>on</strong>g>the</str<strong>on</strong>g>r express, implied or statutory) including, without limitati<strong>on</strong>, warranties<br />
of merchantability, n<strong>on</strong>-infringement, or fitness for a particular purpose, are specifically disclaimed.<br />
14