Nios An189 Simulating Designs
Nios An189 Simulating Designs
Nios An189 Simulating Designs
Processor Designs
Altera Corporation 1
AN-189-1.1
AN 189: Simulating Nios Embedded Processor Designs
Before You The instructions in this document assume that you have a working
knowledge of the SOPC Builder, which you use to create Nios processor
Begin designs, and the ModelSim simulator. Additionally, you should have the
following software installed:
Simulation The following sections walk you through the simulation process for an
example Nios design, including:
Walkthrough
■ “Simulation Settings” on page 2
■ “Getting Started in ModelSim” on page 9
■ “Analyzing the Simulation Results” on page 14
Simulation Settings
When you build a Nios system with the SOPC Builder, you can specify the
options for generating simulation-specific files in each module. This
section describes some of the available options using an example 32-bit
Nios system with internal RAM, ROM, and UART peripherals. This
example design is similar to the Nios 32-bit reference design, which is
provided when you install the software.
3. Browse to \altera\excalibur\sopc_builder_2_5\tutorials\
Sim_Tutorial.
4. Select Sim_Tutorial.quartus.
5. Click Open.
Figure 1 on page 3 shows the SOPC Builder System Contents tab for this
example system.
2 Altera Corporation
AN 189: Simulating Nios Embedded Processor Designs
f Refer to the SOPC Builder Data Sheet or Nios Tutorial for more information
on setting options in the SOPC Builder.
Memory Initialization
To display the code execution during simulation, the ModelSim software
must be able to read the memory that contains the code. If you use the on-
chip memory peripheral (RAM or ROM), the SOPC Builder can create
memory initialization files (.dat files) in your Nios simulation directory.
These files contain code and data in Hexadecimal format. The ModelSim
software automatically reads the files when it opens the HDL code that
represents the on-chip memory that you are using.
Altera Corporation 3
AN 189: Simulating Nios Embedded Processor Designs
4 Altera Corporation
AN 189: Simulating Nios Embedded Processor Designs
When you generate a system that contains off-chip memory that you want
to simulate, the SOPC Builder also generates Verilog HDL or VHDL
simulation models for the off-chip memory. Because timing specifications
vary for each memory peripheral, these models are not intended to be
cycle accurate. However, they let you easily simulate a system in which
code or data is accessed from off-chip memory. To obtain cycle-accurate
simulation results of off-chip memory, you must provide your own
model; many memory manufacturers currently provide Verilog HDL or
VHDL models of their memory devices for this purpose. Once you obtain
a model, you can use it in place of the simple models that the SOPC
Builder generates.
f For more details about simulation of off-chip memory and using third-
party memory models, refer to “Off-Chip Memory Simulation” on
page 23.
Additionally, you can customize the data stream transmitted to the UART,
which is useful for simulating operation of an application that normally
requires a user to type text that is sent to the Nios application (e.g., via the
GERMS monitor). You can predefine text, which is sent to the UART one
character at a time (the Nios processor acknowledges receipt of the
characters). The SOPC Builder converts the predefined text to ASCII-
encoded Hexadecimal data and writes it to the <UART peripheral
name>_input_data_stream.dat file in your Nios simulation directory.
Altera Corporation 5
AN 189: Simulating Nios Embedded Processor Designs
m1000-1020
m1120:4D6F6E6B
m1122:00006579
6 Altera Corporation
AN 189: Simulating Nios Embedded Processor Designs
c. Click OK.
Altera Corporation 7
AN 189: Simulating Nios Embedded Processor Designs
You are finished making simulation settings; you can now generate your
system. The SOPC Builder generates the simulation files, including a
ModelSim Project File (.mpf), and places them in your Nios simulation
directory.
f Refer to the SOPC Builder Data Sheet and Nios Tutorial for more
information on generating your design.
8 Altera Corporation
AN 189: Simulating Nios Embedded Processor Designs
The wave_presets.do file generates a list of default signals that are displayed in the waveform
window.
The virtuals.do file sets up a virtual signal that translates Nios operation codes (or opcodes)
into instructions, allowing you to view assembly instructions (e.g., MOV, ADDI) during
simulation.
.dat Memory initialization files in Hexadecimal format. These files are used for simulation only. You
can modify the file contents without altering the files used in synthesis or place-and-route.
The SOPC Builder creates .dat files for any on-chip memory peripheral(s) in your system. The
files have a name that is similar to the peripheral name. For example, an on-chip ROM named
Boot_ROM has an initialization file named Boot_ROM_lane0.dat. Memory peripherals that
are byte-addressable have a .dat file for each addressable byte. These files have _lanex
appended to the name, where x is a number. For example, a 32-bit ROM has four .dat files with
_lane0 through _lane3.
To simulate an input character stream to the UART peripheral (as described in “UART
Peripheral Simulation Settings” on page 5), the SOPC Builder creates a separate .dat file with
Hexadecimal data representing the input character stream.
Altera Corporation 9
AN 189: Simulating Nios Embedded Processor Designs
Perform the following steps to begin simulation using the SOPC Builder-
generated ModelSim Project File (.mpf) located in your Nios simulation
directory.
If the welcome dialog box does not display, choose Open > Project
(File menu).
5. Click Open.
6. Execute the setup_sim.do file (see Figure 5), which initializes the
macros described in Table 2, by typing the following command at the
ModelSim prompt:
do setup_sim.do r
These macros make it easy for you to load the design and recompile
it after making code changes. To execute a macro, type the letter
corresponding to the macro at the ModelSim prompt.
10 Altera Corporation
AN 189: Simulating Nios Embedded Processor Designs
Macro Function
s Recompiles the Nios and peripheral source code and then reloads the design into the ModelSim work
library for simulation. This macro resets the entire simulation.
c Recompiles all software source code and regenerates the software development kit (SDK) directory
within your Nios project without altering your processors or peripherals. This option is useful during
software development.
w Loads the wave_presets.do file, which contains predefined ModelSim waveform window information.
The wave_presets.do file loads the common signals from all of the processors and peripherals that
reside on-chip and displays the ModelSim waveform window.
h Help. Displays the available macros and their function.
The example signals are a starting point for your analysis of the Nios
system behavior. You can add or remove signals as described in
“Adding & Removing Waveform Signals” on page 22. Table 3
summarizes the types of signals displayed during simulation.
Altera Corporation 11
AN 189: Simulating Nios Embedded Processor Designs
12 Altera Corporation
AN 189: Simulating Nios Embedded Processor Designs
The wait, read, data valid, and flush signals show additional information on the status
of the processor as it executes code (e.g., the read signal is asserted while the
instruction master actively fetches code from memory).
Data Master (1) Signals related to reading and writing data to and from memory and memory-mapped
peripherals in the Nios system. The read and write data buses show incoming and
outgoing data to/from the CPU. These signals cannot be tri-stated because they are
signals inside of the Altera device.
The address, read, write, and byte enable signals, like those of the instruction master
bus signals, operate according to the Avalon bus specification. The signals display
all bus transactions that the Nios data master initiates to its Avalon slaves.
Interrupt Control (1) Displays the interrupt request (IRQ) and the IRQ number of the most recent request.
General Purpose RAM Signals depicting the operation of the on-chip RAM. These signals are almost
Peripheral identical to those of the Nios data master, however, they include a chip select, which
(gen_purpose_RAM) (2) is a result of address decoding in the RAM peripheral.
UART1 Bus Interface (2) Signals displaying the UART bus interface. These signals are similar to those of the
RAM peripheral, however, the read and write lines are omitted for clarity (you can add
them back in manually).
UART1 Internals (2) Internal UART signals showing the UART transmit (TX) and receive (RX) data
registers. The signals decode the 8-bit TX and RX registers to ASCII text so that you
can view the characters in the simulation waveform. The TX ready and RX character
ready signals are also shown.
Boot ROM (boot_rom) (2) Read address and data out signals for the on-chip ROM peripheral.
Notes:
(1) These signals are generated and displayed for each Nios CPU.
(2) These signal groups appear because the example design has ROM, RAM, and UART peripherals. Whenever you
add a Nios peripheral to your design, a corresponding set of simulation signals are generated and added to the
default waveform. The signal groups have names that are similar to the peripheral name in the SOPC Builder (e.g.
gen_purpose_RAM). These signals reflect the I/O to/from each peripheral’s slave port. Therefore, the signals may
not share the same value as the corresponding signals in the Nios instruction/data master port unless a valid bus
transaction has been initiated between the Nios processor and a specific peripheral.
Altera Corporation 13
AN 189: Simulating Nios Embedded Processor Designs
You have completed the basic simulation setup steps. You can use the
ModelSim software to perform simulation as you would for any other
Altera design.
After you have loaded the design, you can simulate and analyze the
operation of your Nios CPU(s), peripherals, and source code. You can
start simulation using the commands in the Run menu or by executing the
run command at the ModelSim prompt (e.g., use run 300µs to run the
simulation through 300 microseconds of time). The software supports
standard time units such as ns, µs, and ms.
14 Altera Corporation
AN 189: Simulating Nios Embedded Processor Designs
After the Nios CPU receives the m character, the UART receives additional
characters. Figure 8 shows this operation in detail. At the solid vertical
time bar, the GERMS monitor source code reads the UART’s receive
register (shown as the data master d_read signal being asserted while the
UART1 bus interface chipselect signal is asserted, indicating a read
cycle to the UART). At this time, the readdata registers for the Nios data
master and UART bus interface are set to 0x006D, which is the ASCII
code for the m character.
Near the end of the waveform, the UART has received the next character,
1, and the signal rx_char_ready is asserted (dotted vertical time bar).
The UART’s readdata register is set to 0x0031 (ASCII code for the 1
character).
Altera Corporation 15
AN 189: Simulating Nios Embedded Processor Designs
After the entire string is entered, the ModelSim software displays the
results of the GERMS command. This display is the same as the one you
would observe if you had a terminal window open and connected to the
UART peripheral while running the program on a Nios CPU:
# +m1000-1020
#
#1000: 0000 0000 0000 0000 0000 0000 0000 0000
#1010: 0000 0000 0000 0000 0000 0000 0000 0000
# +m1120:4D6F6E6B
#
# +m1122:00006579
16 Altera Corporation
AN 189: Simulating Nios Embedded Processor Designs
Unlike the previous example, this design does not run the GERMS
monitor and does not use a UART for communication. Instead, the ROM
is initialized with the port.c file shown below.
/* Filename: port.c
* Example code for Nios simulation.
* This program performs a continuous loop in which
* an integer is incremented and then written to a
* 32-bit PIO port at address 0x2000
* (c) 2001 Altera Corporation
*/
Altera Corporation 17
AN 189: Simulating Nios Embedded Processor Designs
The following excerpt is from the port.objdump file that was generated
for port.c:
00000000 <nr_jumptostart>:
0: 00 98 pfx %hi(0x0)
2: 40 36 movi %g0,0x12
4: 00 98 pfx %hi(0x0)
6: 00 6c movhi %g0,0x0
8: c0 7f jmp %g0
a: 00 30 nop
c: 4e 69 ext16d %sp,%o2
e: 6f 73 usr0 %o7,%i3
00000010 <main>:
18 Altera Corporation
AN 189: Simulating Nios Embedded Processor Designs
The object dump file contains additional code such as startup routines,
interrupt initialization, and other code that was brought in during the
linking stage of compilation. In the object dump excerpt, addresses 0x0
through 0xE define the nr_jumptostart routine, which was linked
after compilation. This routine calls the _start routine that appears later
in the object dump listing (not shown).
Using the object dump listing you can view the correlation between the
object dump file and execution in the Nios processor. In Figure 9, the
beginning of the code execution is shown.
If you refer to the first few lines of the object dump listing, you can view
the instructions that are fetched from memory:
00000000 <nr_jumptostart>:
0: 00 98 pfx %hi(0x0)
2: 40 36 movi %g0,0x12
The Nios CPU is little endian, but object dump listings are big endian .
While at address 0x0, the Nios instruction master i_read_data signal is
set to 9800, which correlates to 00 98 in the object dump file. The object
dump shows that 00 98 is the opcode for a prefix instruction pfx
%hi(0x0). In the waveform, the instruction master
i_readdata_opcode automatically decodes 00 98 to indicate a prefix
instruction.
Altera Corporation 19
AN 189: Simulating Nios Embedded Processor Designs
Figure 10 shows the waveform after the code execution has advanced to
the point where the main routine is entered at address 0x10 and the PIO
output begins to change. The vertical time bar shows the location in which
the instruction fetch address is 0x10, i.e., the beginning of the main
routine as shown in the object dump (the in-line C code is removed for
clarity.):
Next, the movi, bgen, inc, and stp instructions are fetched as shown by
i_readdata_opcode. There is an initial delay following the branch to
the main routine while the CPU pipeline is filled, resulting in a latency of
three instructions before movi, bgen, inc, and stp appear in the
instruction_opcode signal. Once the loop is established, the pipeline
remains filled and there is no additional latency.
20 Altera Corporation
AN 189: Simulating Nios Embedded Processor Designs
Figure 11 shows the PIO write loop once it has been established.
Subsequent execution of the loop requires only three instructions—addi,
stp, and br—to increment the value being written to the PIO, perform
the write, and branch to the beginning of the loop again, respectively.
Altera Corporation 21
AN 189: Simulating Nios Embedded Processor Designs
Adding & To add or remove signals, use the Structure and Signals windows (View
menu), which hierarchically divide your design for easy reference. The
Removing Structure window displays individual entities in your design and any
sub-entities that they call. The Signals window shows the signals
Waveform available for simulation. Figure 12 shows the Structure and Signals
Signals windows.
The Structure window shows the top-level Nios system module (the Nios
CPU and internal peripherals). Each item in the Structure window
corresponds to a design sub-entity. For example, the UART peripheral is
listed as the_uart1:uart1. Click the sub-entity name to display its
signals in the Signals window.
You can drag and drop signals from the Signals window to the waveform
window to add them to the waveform. After you add a signal, you can
edit it to display a different alias, radix (for buses), etc. Refer to the
ModelSim documentation for instructions on making these settings.
You can delete a signal or group of signals from the waveform by selecting
the signals(s) and then choosing Delete (Edit menu).
22 Altera Corporation
AN 189: Simulating Nios Embedded Processor Designs
Off-Chip With the Nios processor version 2.1, you can simulate designs that use off-
chip memory. The SOPC Builder can generate memory models for off-
Memory chip memory such as the SRAM included on the Nios development board.
You can also generate memory models for custom memory interfaces
Simulation created using the User-Defined Interface wizard in the SOPC Builder.
There are two methods to simulate off-chip memory devices:
1 You should not use the Verilog HDL or VHDL simulation model
files for synthesis or design operation. You should use them for
functional simulation only.
4. Edit the Peripheral Template File (.ptf) for your system according to
the instructions in the following sections. You will indicate which
model to use (an automatically generated one or a custom model)
and the memory contents.
5. Open the design again in the SOPC Builder (choose to edit the
existing megafunction variation you made in steps 1 and 2 in the
MegaWizard Plug-In Manager).
Altera Corporation 23
AN 189: Simulating Nios Embedded Processor Designs
SYSTEM_BUILDER_INFO
{
Instantiate_In_System_Module = "1";
Is_Enabled = "1";
Date_Modified = "--unknown--";
Make_Memory_Model = "1";
}
When you generate your system, the SOPC Builder creates a simulation
model that is instantiated into the top level of your system. The
instantiation is black boxed for synthesis so that only the simulation
software instantiates the model.
24 Altera Corporation
AN 189: Simulating Nios Embedded Processor Designs
HDL_INFO
{
Verilog_Sim_Model_Files = "my_model.v";
}
When you generate your system, the SOPC Builder instantiates the
appropriate model into the top level of your system. The instantiation is
black boxed for synthesis so that only the simulation software instantiates
the model.
Altera Corporation 25
AN 189: Simulating Nios Embedded Processor Designs
CONTENTS srec
{
Kind = "build";
Build_Info = "my_contents.srec";
Command_Info = "";
Textfile_Info = "";
String_Info = "";
}
Conclusion Design verification is one of the most critical portions of any design cycle.
With the SOPC design using the Nios embedded processor, you now have
the ability to probe signals within the Nios CPU, its peripherals, and bus
architecture simultaneously for system-level verification.
■ Nios Tutorial
■ ModelSim Start Here Guide
■ ModelSim Tutorial
Documentation Altera values your feedback. If you would like to provide feedback on this
document—e.g., clarification requests, inaccuracies, or inconsistencies—
Feedback send e-mail to nios_docs@altera.com.
26 Altera Corporation
AN 189: Simulating Nios Embedded Processor Designs
Copyright 2002 Altera Corporation. Altera, The Programmable Solutions Company, the stylized Altera logo,
specific device designations, and all other words and logos that are identified as trademarks and/or service
101 Innovation Drive marks are, unless noted otherwise, the trademarks and service marks of Altera Corporation in the U.S. and
other countries. All other product or service names are the property of their respective holders. Altera products
San Jose, CA 95134 are protected under numerous U.S. and foreign patents and pending applications, maskwork rights, and
(408) 544-7000 copyrights. Altera warrants performance of its semiconductor products to current
http://www.altera.com specifications in accordance with Altera’s standard warranty, but reserves the right to
make changes to any products and services at any time without notice. Altera assumes no
Applications Hotline: responsibility or liability arising out of the application or use of any information, product,
(800) 800-EPLD or service described herein except as expressly agreed to in writing by Altera Corporation.
Literature Services: Altera customers are advised to obtain the latest version of device specifications before
relying on any published information and before placing orders for products or services.
lit_req@altera.com All rights reserved.
27 Altera Corporation