Picoblaze User Guide
Picoblaze User Guide
Embedded Microcontroller
User Guide
for Spartan-3, Virtex-II, and
Virtex-II Pro FPGAs
R
R
"Xilinx" and the Xilinx logo shown above are registered trademarks of Xilinx, Inc. Any rights not expressly granted herein are reserved.
CoolRunner, RocketChips, Rocket IP, Spartan, StateBENCH, StateCAD, Virtex, XACT, XC2064, XC3090, XC4005, and XC5210 are
registered trademarks of Xilinx, Inc.
Version Revision
05/20/04 1.0 Initial Xilinx release.
06/10/04 1.1 Various minor corrections, updates, and enhancements throughout.
PicoBlaze 8-bit Embedded Microcontroller www.xilinx.com UG129 (v1.1.1) November 21, 2005
1-800-255-7778
R
Preface
Limitations
Limited Warranty and Disclaimer
These designs are provided to you “as-is”. Xilinx and its licensors make and you receive no
warranties or conditions, express, implied, statutory or otherwise, and Xilinx specifically
disclaims any implied warranties of merchantability, non-infringement, or fitness for a
particular purpose. Xilinx does not warrant that the functions contained in these designs
will meet your requirements, or that the operation of these designs will be uninterrupted
or error free, or that defects in the Designs will be corrected. Furthermore, Xilinx does not
warrant or make any representations regarding use or the results of the use of the designs
in terms of correctness, accuracy, reliability, or otherwise.
Limitation of Liability
In no event will Xilinx or its licensors be liable for any loss of data, lost profits, cost or
procurement of substitute goods or services, or for any special, incidental, consequential,
or indirect damages arising from the use or operation of the designs or accompanying
documentation, however caused and on any theory of liability. This limitation will apply
even if Xilinx has been advised of the possibility of such damage. This limitation shall
apply not-withstanding the failure of the essential purpose of any limited remedies herein.
Preface: Acknowledgments
Acknowledgments
Xilinx thanks the following individuals for their contribution to the PicoBlaze
microcontroller cause:
• Henk van Kampen, Mediatronix
Developer of the pBlazIDE graphical, integrated development environment.
• Prof. Dr.-Ing. Bernhard Lang, University of Applied Sciences, Osrabrueck,
Germany
Concept of using VHDL simulation variables to display disassembled op-code
instructions.
• Kris Chaplin, Xilinx Ltd.
JTAG-based program loader, update function.
Guide Contents
Guide Contents
This manual contains the following chapters:
• Chapter 1, “Introduction,” describes the features and functional blocks of the
PicoBlaze microcontroller.
• Chapter 2, “PicoBlaze Interface Signals,” defines the PicoBlaze signals.
• Chapter 3, “PicoBlaze Instruction Set,” summarizes the instruction set of the
PicoBlaze microcontrollers.
• Chapter 4, “Interrupts,” describes how the PicoBlaze microcontroller uses interrupts.
• Chapter 5, “Scratchpad RAM,” describes the 64-byte scratchpad RAM.
• Chapter 6, “Input and Output Ports,” describes the input and output ports supported
by the PicoBlaze microcontroller.
• Chapter 7, “Instruction Storage Configurations,” provides several examples of
instruction storage with the PicoBlaze microcontroller.
• Chapter 8, “Performance,”provides performance values for the PicoBlaze
microcontroller.
• Chapter 9, “Using the PicoBlaze Microcontroller in an FPGA Design,” describes the
design flow process with the PicoBlaze microcontroller.
• Chapter 10, “PicoBlaze Development Tools,” describes the available development
tools.
• Chapter 11, “Assembler Directives,” describes the assembler directives that provide
advanced control.
• Chapter 12, “Simulating PicoBlaze Code,” describes the tools that simulate PicoBlaze
code.
• Appendix A, “Related Materials and References,” provides additional resources
useful for the PicoBlaze microcontroller design.
• Appendix B, “Example Program Templates,” provides example KCPSM3 and
pBlazIDE code templates for use in application programs.
• Appendix C, “PicoBlaze Instruction Set and Event Reference,” summarizes the
PicoBlaze instructions and events in alphabetical order.
• Appendix D, “Instruction Codes,” provides the 18-bit instruction codes for all
PicoBlaze instructions.
• Appendix E, “Register and Scratchpad RAM Planning Worksheets,” provides
worksheets to use for the PicoBlaze microcontroller design.
Preface: Limitations
Limited Warranty and Disclaimer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Limitation of Liability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Technical Support Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Preface: Acknowledgments
Chapter 1: Introduction
PicoBlaze Microcontroller Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
PicoBlaze Microcontroller Functional Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
General-Purpose Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1,024-Instruction Program Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Arithmetic Logic Unit (ALU) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
64-Byte Scratchpad RAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Program Counter (PC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Program Flow Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
CALL/RETURN Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Why the PicoBlaze Microcontroller? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Why Use a Microcontroller within an FPGA? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Increment/Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Negate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
No Operation (NOP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Setting and Clearing CARRY Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Clear CARRY Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Set CARRY Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Test and Compare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Compare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Shift and Rotate Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Rotate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Moving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Program Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
JUMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
CALL/RETURN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Chapter 4: Interrupts
Example Interrupt Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Chapter 8: Performance
Input Clock Frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Predicting Executing Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Chapter 1
Introduction
The PicoBlaze™ microcontroller is a compact, capable, and cost-effective fully embedded
8-bit RISC microcontroller core optimized for the Spartan™-3, Virtex™-II, and Virtex-II
Pro™ FPGA families. The PicoBlaze microcontroller provides cost-efficient
microcontroller-based control and simple data processing.
The PicoBlaze microcontroller is optimized for efficiency and low deployment cost. It
occupies just 96 FPGA slices, or only 12.5% of an XC3S50 FPGA and a miniscule 0.3% of an
XC3S5000 FPGA. In typical implementations, a single FPGA block RAM stores up to 1024
program instructions, which are automatically loaded during FPGA configuration. Even
with such resource efficiency, the PicoBlaze microcontroller performs a respectable 44 to
100 million instructions per second (MIPS) depending on the target FPGA family and
speed grade.
The PicoBlaze microcontroller core is totally embedded within the target FPGA and
requires no external resources. The PicoBlaze microcontroller is extremely flexible. The
basic functionality is easily extended and enhanced by connecting additional FPGA logic
to the microcontroller’s input and output ports.
The PicoBlaze microcontroller provides abundant, flexible I/O at much lower cost than
off-the-shelf controllers. Similarly, the PicoBlaze peripheral set can be customized to meet
the specific features, function, and cost requirements of the target application. Because the
PicoBlaze microcontroller is delivered as synthesizable VHDL source code, the core is
future-proof and can be migrated to future FPGA architectures, effectively eliminating
product obsolescence fears. Being integrated within the FPGA, the PicoBlaze
microcontroller reduces board space, design cost, and inventory.
The PicoBlaze FPC is supported by a suite of development tools including an assembler, a
graphical integrated development environment (IDE), a graphical instruction set
simulator, and VHDL source code and simulation models. Similarly, the PicoBlaze
microcontroller is also supported in the Xilinx System Generator development
environment.
The various PicoBlaze code examples throughout this application note are written for the
Xilinx KCPSM3 assembler. The Mediatronix pBlazIDE assembler has a code import
function that reads the KCPSM3 syntax.
Chapter 1: Introduction
• Byte-wide Arithmetic Logic Unit (ALU) with CARRY and ZERO indicator flags
• 64-byte internal scratchpad RAM
• 256 input and 256 output ports for easy expansion and enhancement
• Automatic 31-location CALL/RETURN stack
• Predictable performance, always two clock cycles per instruction, up to 200 MHz or
100 MIPS in a Virtex-II Pro FPGA
• Fast interrupt response; worst-case 5 clock cycles
• Optimized for Xilinx Spartan-3, Virtex-II, and Virtex-II Pro FPGA architectures—just
96 slices and 0.5 to 1 block RAM
• Assembler, instruction-set simulator support
Program Counter
CALL/RETURN
1Kx18 PORT_ID
31x10
Stack
64-Byte
(PC)
Flags
Instruction Constants Z Zero
Decoder
C Carry
INTERRUPT
16 Byte-Wide Registers
Enable Operand 1 ALU
IE s0 s1 s2 s3
s4 s5 s6 s7
IN_PORT s8 s9 sA sB
sC sD sE sF
Operand 2
UG129_c1_01_051204
General-Purpose Registers
The PicoBlaze microcontroller includes 16 byte-wide general-purpose registers,
designated as registers s0 through sF. For better program clarity, registers can be renamed
using an assembler directive. All register operations are completely interchangeable; no
registers are reserved for special tasks or have priority over any other register. There is no
dedicated accumulator; each result is computed in a specified register.
Flags
ALU operations affect the ZERO and CARRY flags. The ZERO flag indicates when the
result of the last operation resulted in zero. The CARRY flag indicates various conditions,
depending on the last instruction executed.
The INTERRUPT_ENABLE flag enables the INTERRUPT input.
Input/Output
The Input/Output ports extend the PicoBlaze microcontroller’s capabilities and allow the
microcontroller to connect to a custom peripheral set or to other FPGA logic. The PicoBlaze
microcontroller supports up to 256 input ports and 256 output ports or a combination of
input/output ports. The PORT_ID output provides the port address. During an INPUT
operation, the PicoBlaze microcontroller reads data from the IN_PORT port to a specified
register, sX. During an OUTPUT operation, the PicoBlaze microcontroller writes the
contents of a specified register, sX, to the OUT_PORT port.
See Chapter 6, “Input and Output Ports,” for more information.
Chapter 1: Introduction
CALL/RETURN Stack
The CALL/RETURN hardware stack stores up to 31 instruction addresses, enabling
nested CALL sequences up to 31 levels deep. Since the stack is also used during an
interrupt operation, at least one of these levels should be reserved when interrupts are
enabled.
The stack is implemented as a separate cyclic buffer. When the stack is full, it overwrites
the oldest value. Consequently, there are no instructions to control the stack or the stack
pointer. No program memory is required for the stack.
Interrupts
The PicoBlaze microcontroller has an optional INTERRUPT input, allowing the PicoBlaze
microcontroller to handle asynchronous external events. In this context, “asynchronous”
relates to interrupts occuring at any time during an instruction cycle. However,
recommended design practice is to synchronize all inputs to the PicoBlaze controller using
the clock input.
The PicoBlaze microcontroller responds to interrupts quickly in just five clock cycles.
See Chapter 4, “Interrupts,” for more information.
Reset
The PicoBlaze microcontroller is automatically reset immediately after the FPGA
configuration process completes. After configuration, the RESET input forces the processor
into the initial state. The PC is reset to address 0, the flags are cleared, interrupts are
disabled, and the CALL/RETURN stack is reset.
The data registers and scratchpad RAM are not affected by Reset.
See “RESET Event” in Appendix C for more information.
Chapter 1: Introduction
Table 1-1: PicoBlaze Microcontroller Embedded within an FPGA Provides the Optimal Balance between
Microcontroller and FPGA Solutions
PicoBlaze Microcontroller FPGA Logic
• Easy to program, excellent for control • Significantly higher performance
and state machine applications • Excellent at parallel operations
• Resource requirements remain constant • Sequential vs. parallel implementation
Strengths with increasing complexity trade-offs optimize performance or cost
• Re-uses logic resources, excellent for • Fast response to multiple, simultaneous
lower-performance functions inputs
• Executes sequentially • Control and state machine applications
• Performance degrades with increasing more difficult to program
complexity • Logic resources grow with increasing
Weaknesses
• Program memory requirements complexity
increase with increasing complexity
• Slower response to simultaneous inputs
Chapter 2
PicoBlaze Microcontroller
IN_PORT[7:0] OUT_PORT[7:0]
INTERRUPT PORT_ID[7:0]
RESET READ_STROBE
WRITE_STROBE
CLK INTERRUPT_ACK
UG129_c2_01_052004
Chapter 3
CALL C, aaa If CARRY flag set, call subroutine at aaa If CARRY=1, {TOS Å PC, - -
PC Å aaa}
CALL NC, aaa If CARRY flag not set, call subroutine at If CARRY=0, {TOS Å PC, - -
aaa PC Å aaa}
CALL NZ, aaa If ZERO flag not set, call subroutine at aaa If ZERO=0, {TOS Å PC, - -
PC Å aaa}
CALL Z, aaa If ZERO flag set, call subroutine at aaa If ZERO=1, {TOS Å PC, - -
PC Å aaa}
COMPARE sX, kk Compare register sX with literal kk. Set If sX=kk, ZERO Å 1 ? ?
(COMP) CARRY and ZERO flags as appropriate. If sX<kk, CARRY Å 1
Registers are unaffected.
COMPARE sX, sY Compare register sX with register sY. Set If sX=sY, ZERO Å 1 ? ?
(COMP) CARRY and ZERO flags as appropriate. If sX<sY, CARRY Å 1
Registers are unaffected.
INPUT sX, (sY) Read value on input port location pointed PORT_ID Å sY - -
(IN sX, sY) to by register sY into register sX sX Å IN_PORT
JUMP NC, aaa If CARRY flag not set, jump to aaa If CARRY=0, PC Å aaa - -
JUMP NZ, aaa If ZERO flag not set, jump to aaa If ZERO=0, PC Å aaa - -
TEST sX, kk Test bits in register sX against literal kk. If (sX AND kk) = 0, ZERO Å 1 ? ?
Update CARRY and ZERO flags. Registers CARRY Å odd parity of (sX
are unaffected. AND kk)
TEST sX, sY Test bits in register sX against register sX. If (sX AND sY) = 0, ZERO Å 1 ? ?
Update CARRY and ZERO flags. Registers CARRY Å odd parity of (sX
are unaffected. AND kk)
ss = 6-bit scratchpad RAM address, specified either as a literal or a two-digit hexadecimal value ranging from 00 to 3F
or specified as a literal
RAM[n] = Contents of scratchpad RAM at location n
Address Spaces
As shown in Table 3-2, the PicoBlaze microcontroller has five distinct address spaces.
Specific instructions operate on each of the address spaces.
Address Spaces
Processing Data
All data processing instructions operate on any of the 16 general-purpose registers. Only
the data processing instructions modify the ZERO or CARRY flags as appropriate for the
instruction. The data processing instructions consists of the following types:
• Logic instructions
• Arithmetic instructions
• Test and Compare instructions
• Shift and Rotate instructions
Logic Instructions
The logic instructions perform a bitwise logical AND, OR, or XOR between two operands.
The first operand is a register location. The second operand is either a register location or
a literal constant. Besides performing pure AND, OR, and XOR operations, the logic
instructions provide a means to:
• complement or invert a register
• clear a register
• set or clear specific bits within a register
AND sX, sY
AND sX, kk
Register sY
7 6 5 4 3 2 1 0
Literal kk
Register sX 7 6 5 4 3 2 1 0
UG129_c3_01_051204
Processing Data
Complement/Invert Register
The PicoBlaze microcontroller does not have a specific instruction to invert individual bits
within register sX. However, the XOR sX,FF instruction performs the equivalent
operation, as shown in Figure 3-2.
complement:
; XOR sX, FF invert all bits in register sX, same as one’s complement
toggle_bit:
; XOR sX, <bit_mask>
Clear Register
The PicoBlaze microcontroller does not have a specific instruction to clear a specific
register. However, the XOR sX,sX instruction performs the equivalent operation. XORing
register sX with itself clears registers sX and sets the ZERO flag, as shown in Figure 3-4.
Set Bit
The PicoBlaze microcontroller does not have a specific instruction to set an individual bit
or bits within a specific register. However, the OR instruction performs the equivalent
operation. ORing register sX with a bit mask sets specific bits, as shown in Figure 3-6. A ‘1’
in the bit mask sets the corresponding bit in register sX. A ‘0’ in the bit mask leaves the
corresponding bit unchanged.
set_bit:
; OR sX, <bit_mask>
Clear Bit
The PicoBlaze microcontroller does not have a specific instruction to clear an individual bit
or bits within a specific register. However, the AND instruction performs the equivalent
operation. ANDing register sX with a bit mask clears specific bits, as shown in Figure 3-7.
A ‘0’ in the bit mask clears the corresponding bit in register sX. A ‘1’ in the bit mask leaves
the corresponding bit unchanged.
clear_bit:
; AND sX, <bit_mask>
Arithmetic Instructions
The PicoBlaze microcontroller provides basic byte-wide addition and subtraction
instructions. Combinations of instructions perform multi-byte arithmetic plus
multiplication and division operations. If the end application requires significant
arithmetic performance, consider using the 32-bit MicroBlaze RISC processor core for
Xilinx FPGAs (see Reference 4).
Processing Data
ADD16:
NAMEREG s0, a_lsb ; rename register s0 as “a_lsb”
NAMEREG s1, a_msb ; rename register s1 as “a_msb”
NAMEREG s2, b_lsb ; rename register s2 as “b_lsb”
NAMEREG s3, b_msb ; rename register s3 as “b_lsb”
SUB16:
NAMEREG s0, a_lsb ; rename register s0 as “a_lsb”
NAMEREG s1, a_msb ; rename register s1 as “a_msb”
NAMEREG s2, b_lsb ; rename register s2 as “b_lsb”
NAMEREG s3, b_msb ; rename register s3 as “b_lsb”
Increment/Decrement
The PicoBlaze microcontroller does not have a dedicated increment or decrement
instruction. However, adding or subtracting one using the ADD or SUB instructions
provides the equivalent operation, as shown in Figure 3-10.
inc_16:
; increment low byte
ADD lo_byte,01
; increment high byte only if CARRY bit set when incrementing low byte
ADDCY hi_byte,00
Negate
The PicoBlaze microcontroller does not have a dedicated instruction to negate a register
value, taking the two’s complement. However, the instructions in Figure 3-12 provide the
equivalent operation.
Negate:
; invert all bits in the register performing a one’s complement
XOR sX,FF
; add one to sX
ADD sX,01
RETURN
Figure 3-12: Destructive Negate (2’s Complement) Function Overwrites Original
Value
Another possible implementation that does not overwrite the value appears in Figure 3-13.
Negate:
NAMEREG sY, value
NAMEREG sX, complement
; Clear ‘complement’ to zero
LOAD complement, 00
; subtract value from 0 to create two’s complement
SUB complement, value
RETURN
Figure 3-13: Non-destructive Negate Function Preserves Original Value
Multiplication
The PicoBlaze microcontroller core does not have a dedicated hardware multiplier.
However, the PicoBlaze microcontroller performs multiplication using the available
arithmetic and shift instructions. Figure 3-14 demonstrates an 8-bit by 8-bit multiply
routine that produces a 16-bit multiplier product in 50 to 57 instruction cycles, or 100 to 114
clock cycles. By contrast, the 8051 microcontroller performs the same multiplication in
eight instruction cycles or 96 clock cycles on a the standard 12-cycle 8051.
Processing Data
18x18 Multiplier
A[17:8]
0 PORT_ID[7:0] [0]
SEL P[7:0]
READ_STROBE B[17:8]
WRITE_STROBE
B[7:0]
EN
UG129_c3_02_052004
Figure 3-15: 8-bit by 8-bit Hardware Multiplier Using the FPGA’s 18x18 Multipliers
Division
The PicoBlaze microcontroller core does not have a dedicated hardware divider. However,
the PicoBlaze microcontroller performs division using the available arithmetic and shift
instructions. Figure 3-17 demonstrates a subroutine that divides an unsigned 8-bit number
by another unsigned 8-bit number to produce an 8-bit quotient and an 8-bit remainder in
60 to 74 instruction cycles, or 120 to 144 clock cycles.
Processing Data
No Operation (NOP)
The PicoBlaze instruction set does not have a specific NOP instruction. Typically, a NOP
instruction is completely benign, does not affect register contents or flags, and performs no
operation other than requiring an instruction cycle to execute. A NOP instruction is
therefore sometimes useful to balance code trees for more predictable execution timing.
There are a few possible implementations of an equivalent NOP operation, as shown in
Figure 3-18 and Figure 3-19. Loading a register with itself does not affect the register value
or the status flags.
nop:
LOAD sX, sX
A similar NOP technique is to simply jump to the next instruction, which is equivalent to
the default program flow. The JUMP instruction consumes an instruction cycle (two clock
cycles) without affecting register contents.
JUMP next
next: <next instruction>
clear_carry_bit:
AND sX, sX ; register sX unaffected, CARRY flag cleared
Figure 3-20: ANDing a Register with Itself Clears the CARRY Flag
set_carry:
LOAD sX, 00
COMPARE sX, 01 ; set CARRY flag and reset ZERO flag
Test
The TEST instruction performs bit testing via a bitwise logical AND operation between
two operands. Unlike the AND instruction, only the ZERO and CARRY flags are affected;
no registers are modified. The ZERO flag is set if all the bitwise AND results are Low, as
shown in Figure 3-22.
Processing Data
Register sY
Literal kk 7 6 5 4 3 2 1 0
Register sX 7 6 5 4 3 2 1 0
Bitwise AND
ZERO UG129_c3_03_051404
Register sY
Literal kk 7 6 5 4 3 2 1 0
Register sX 7 6 5 4 3 2 1 0
CARRY UG129_c3_04_051404
The example in Figure 3-25 demonstrates how to generate parity for all eight bits in a
register.
generate_parity:
TEST sX, FF ; include all bits in parity generation
Figure 3-25: Generate Parity for a Register Using the TEST Instruction
See also “TEST sX, Operand — Test Bit Location in Register sX, Generate Odd Parity,”
page 116.
Compare
The COMPARE instruction performs an 8-bit subtraction of two operands but only affects
the ZERO and CARRY flags, as shown in Table 3-3. No registers are modified.
The ZERO flag is set when both input operands are identical. When set, the CARRY flag
indicates that the second operand is greater than the first operand.
See also “COMPARE sX, Operand — Compare Operand with Register sX,” page 97.
Processing Data
SL0 Shift Left with ‘0’ fill. SR0 Shift Right with ‘0’ fill.
CARRY Register sX Register sX CARRY
7 6 5 4 3 2 1 0 ‘0’ ‘0’ 7 6 5 4 3 2 1 0
SL1 Shift Left with ‘1’ fill. SR1 Shift Right with ‘1’ fill.
CARRY Register sX Register sX CARRY
7 6 5 4 3 2 1 0 ‘1’ ‘1’ 7 6 5 4 3 2 1 0
SLX Shift Left, eXtend bit 0. SRX Shift Right, sign eXtend.
CARRY Register sX Register sX CARRY
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
SLA Shift Left through All bits, including CARRY. SRA Shift Right through All bits, including CARRY.
CARRY Register sX Register sX CARRY
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
See also:
• “SL[ 0 | 1 | X | A ] sX — Shift Left Register sX,” page 110
• “SR[ 0 | 1 | X | A ] sX — Shift Right Register sX,” page 111
Rotate
The rotate instructions, shown in Table 3-5, rotate the contents of the specified register left
or right. The RL sX instruction shifts the contents of register sX left with the most-
significant bit, bit 7, feeding the least-significant bit, bit 0. The most-significant bit, bit 7,
also shifts into the CARRY flag. The RR sX instruction is similar but shifts the contents of
register sX to the right and copies the least-significant bit, bit 0, into the CARRY flag.
See also:
• “RL sX — Rotate Left Register sX,” page 109
• “RR sX — Rotate Right Register sX,” page 109
Moving Data
Data movement between various resources is an essential microcontroller function.
Figure 3-26 shows the various PicoBlaze instructions to move data.
Scratchpad RAM
STORE FETCH
Registers
IN_PORT OUT_PORT
INPUT LOAD sX, sY OUTPUT
PORT_ID
Instruction
Store INPUT sX, kk
OUTPUT sX, kk
UG129_c3_05_060404
The JUMP, CALL, and RETURN instructions are all conditionally executed, depending if a
condition is specified and specifically whether the CARRY or ZERO flags are set or
cleared. Table 3-6 summarizes the possible conditions. The condition is specified as an
instruction operand. The instruction is unconditionally executed if no condition is
specified.
JUMP
The JUMP instruction is conditional and executes only if the specified condition, listed in
Table 3-6, is met. If the condition is false, then the conditional JUMP instruction has no
effect other than requiring two clock cycles to execute. No registers or flags are affected.
If the conditional JUMP instruction is executed, then the PC is loaded with the address of
the specified label, which is computed and assigned by the assembler. The PicoBlaze
processor then executes the instruction at the specified label.
The JUMP instruction does not interact with the CALL/RETURN stack.
Arrow ‘A’ in Figure 3-27 illustrates the program flow during a JUMP instruction. When
the PicoBlaze microcontroller executes the JUMP C, skip_over instruction, it first
checks if the CARRY bit is set. If the CARRY bit is set, then the address of the skip_over
label is loaded into the PC. The PicoBlaze microcontroller then jumps to and executes the
instruction located at that address. Program flow continues normally from that point.
ADDRESS 000
main:
A
CALL my_subroutine Call my_subroutine. Save
ADD s0, s1 the current PC to top of CALL/
RETURN stack. Load the PC with
3 the address of my_subroutine.
skip_over: 1
JUMP main
my_subroutine:
2
Return from my_subroutine.
Load the PC with the top of the CALL/
RETURN RETURN stack plus 1. Execute the
instruction immediately following
the associated CALL instruction.
UG129_c3_06_051404
The JUMP instruction does not affect the ZERO and CARRY flags. All jumps are absolute;
there are no relative jumps. Likewise, computed jumps are not supported.
See also “JUMP [Condition,] Address — Jump to Specified Address, Possibly with
Conditions,” page 103.
CALL/RETURN
The CALL instruction differs from the JUMP instruction in that program flow temporarily
jumps to a subroutine function and then returns to the instruction following the CALL
instruction, as shown in Figure 3-27. The CALL instruction is conditional and executes only
if the specified condition, listed in Table 3-6, is met. If the condition is false, then the
conditional CALL instruction has no effect other than requiring two clock cycles to execute.
No registers or flags are affected.
If the conditional CALL instruction is executed, then the current PC value is pushed on top
of the CALL/RETURN stack. The address of the specified label, which is computed and
assigned by the assembler, is loaded into the PC. The PicoBlaze microcontroller then
executes the instruction at the specified label. See arrow ‘1’ in Figure 3-27.
The PicoBlaze microcontroller continues executing instructions in the subroutine call until
it encounters a RETURN instruction. See arrow ‘2’ in Figure 3-27.
Every CALL instruction should have a corresponding RETURN instruction. The RETURN
instruction is also conditional and executes only if the specified condition, listed in
Table 3-6, is met. The RETURN instruction terminates the subroutine call, pops the top of
the CALL/RETURN stack, increments the value, and loads the value into the PC, which
returns the program flow to the instruction immediately following the original CALL
instruction. See arrow ‘3’ in Figure 3-27.
If the conditional CALL instruction is executed, the ZERO and CARRY flags are potentially
modified by the instructions within the called subroutine, but not directly by the CALL or
RETURN instructions themselves. If the CALL instruction is not executed, then the flags are
unaffected.
See also:
• “CALL [Condition,] Address — Call Subroutine at Specified Address, Possibly with
Conditions,” page 96“
• “RETURN [Condition] — Return from Subroutine Call, Possibly with Conditions,”
page 107
Chapter 4
Interrupts
The PicoBlaze processor provides a single interrupt input signal. If the application requires
multiple interrupt signals, combine the signals using simple FPGA logic to form a single
INTERRUPT input signal. After reset, the INTERRUPT input is disabled and must be
enabled via the ENABLE INTERRUPT instruction. To disable interrupts at any point in the
program, issue a DISABLE INTERRUPT instruction.
Interrupt signal
PicoBlaze Microcontroller
SET 2
D Q INTERRUPT
RST
INTERRUPT_ACK
3
UG129_c4_01_060404
Chapter 4: Interrupts
A special RETURNI command ensures that the end of an interrupt service routine restores
the status of the flags and controls the enable of future interrupts. When the RETURNI
instruction is executed, the PC values saved onto the CALL/RETURN stack is
automatically reloaded to the PC register. Likewise, the ZERO and CARRY flags are
restored and program flow returns to the instruction following the instruction where the
interrupt occurred.
If the application does not require an interrupt, tie the INTERRUPT signal Low.
Consequently, all 1,024 instruction locations are available.
ADDRESS 000
main: 1
ENABLE INTERRUPT The interrupt input is not
recognized until the
INTERRUPT INPUT s0, 00 INTERRUPT_ENABLE flag is set.
input 2 INPUT s1, 01
asserted.
ADD s0, s1
OUTPUT s0, 00
6
CALL critical_timing
JUMP main
3. The PicoBlaze microcontroller recognizes the interrupt and preempts the ADD s0,s1
instruction. The current PC, which points to the ADD s0 s1 instruction, is pushed
onto the CALL/RETURN stack. Likewise, the ZERO and CARRY flags are preserved.
Furthermore, the INTERRUPT_ENABLE flag is cleared disabling any further
interrupts. Finally, the PC is loaded with all ones (3FF) and the PicoBlaze
microcontroller performs an interrupt service routine call to the last location in the
instruction store. If using a 1Kx18 block RAM for instruction store, the last location is
3FF. If using a smaller instruction store, then the interrupt vector is still located in the
last instruction location. The PicoBlaze microcontroller also asserts the
INTERRUPT_ACK output, indicating that the interrupt is being acknowledged.
4. The interrupt vector is always located in the last location in the instruction store. In this
example, the program jumps to the interrupt service routine (ISR) via the JUMP isr
instruction.
5. When completed, exit the interrupt service routine (ISR) using the special RETURNI
instruction. Do not use the RETURN instruction, which is used with normal subroutine
calls. The RETURNI ENABLE instruction returns from the interrupt service routine and
re-enables the INTERRUPT input, which was automatically disabled when the
interrupt was recognized. Using RETURNI DISABLE also returns from the interrupt
service routine but leaves the INTERRUPT input disabled.
6. The RETURNI instruction restores the preserved ZERO and CARRY flags saved
during Step (3). Likewise, the RETURNI instruction pops the top of the
CALL/RETURN stack into the PC, which causes the PicoBlaze microcontroller to
resume program executing the instruction that was preempted by the interrupt, ADD
s0,s1 in this example.
Begin executing
Interrupt interrupt service
recognized routine
5 clock cycles
CLK
PREEMPTED
INSTRUCTION INPUT s1,01 ADD s0,s1 JUMP isr TEST s7,02
Address of 3 4
ADDRESS[9:0] ... ADD s0,s1 3FF isr
INTERRUPT 2
INTERRUPT_ACK
Call to interrupt Jump to interrupt
vector, assert service routine
ADD s0,s1 instruction INTERRUPT_ACK
pre-empted. PC saved to
stack. Flags preserved.
Interrupt disabled.
CALL/RETURN
Stack
Preserved ZERO
ZERO Flag Flag
Preserved CARRY
CARRY Flag Flag
1 0
INTERRUPT_ENABLE UG129_c4_03_051404
Chapter 4: Interrupts
Figure 4-3 shows the same interrupt procedure but as a timing diagram. With the interrupt
enabled, the INTERRUPT input is recognized at Step (2), the same clock cycle where the
ADDRESS bus changes value. The address for the instruction ADD s0,s1 appears on the
ADDRESS bus and is pushed onto the CALL/RETURN stack. Simultaneously, the
interrupt is disabled and the ZERO and CARRY flags are preserved. The ADD s0, s1
instruction is preempted and does not yet execute. Instead, the PicoBlaze microcontroller
performs a call to the interrupt vector at location 0x3FF.
An interrupt is undesirable in timing-critical procedures or when predictable timing is a
must. Temporarily disable the INTERRUPT input using the DISABLE INTERRUPT
instruction, as demonstrated in the critical_timing subroutine in Figure 4-2. Once the
critical procedure completes, re-enable the INTERRUPT input with the ENABLE
INTERRUPT instruction.
Chapter 5
Scratchpad RAM
The PicoBlaze microcontroller contains a 64-byte scratchpad RAM. Two instructions,
STORE and FETCH, move data between any data register and the scratchpad RAM. Both
direct and indirect addressing are supported. The scratchpad RAM is only supported on
PicoBlaze microcontrollers for Spartan-3, Virtex-II, and Virtex-II Pro FPGAs.
The scratchpad RAM is unaffected by a RESET Event.
Address Modes
The STORE and FETCH instructions support both direct and indirect addressing modes to
access scratchpad RAM data.
Direct Addressing
An immediate constant value directly addresses a specific scratchpad RAM location. In the
example in Figure 5-1, register sX directly writes to and reads from scratchpad RAM
location 04.
scratchpad_transfers:
STORE sX, 04 ; Write register sX to RAM location 04
FETCH sX, 04 ; Read RAM location 04 into register sX
Indirect Addressing
Using indirect address, the actual RAM address is the value contained in a specified
register. Whereas direct addressing requires the RAM address to be known before
assembly, indirect addressing provides additional program flexibility. The application
code can compute or modify the RAM address based on other program data. The code in
Figure 5-2, for example, initializes all the scratchpad RAM locations to 0 using a simple
loop.
Figure 5-2: Indirect Addressing Initializes All of RAM with a Simple Subroutine
Stack Operations
Stack Operations
Although the PicoBlaze microcontroller has a CALL/RETURN stack, it does not have a
dedicated data stack. In some controller architectures, register values are preserved during
subroutine calls or interrupts by pushing them or popping them onto a data stack. The
equivalent operation is possible in the PicoBlaze microcontroller by reserving some
locations in scratchpad RAM.
In the example shown in Figure 5-4, the my_subroutine function uses register s0. The
value of register s0 is preserved onto a “stack”, which is emulated using scratchpad RAM.
When the my_subroutine function completes, the preserved value of register s0 is
restored from the stack.
my_subroutine:
; preserve register s0
CALL push_s0
; restore register s0
CALL pop_s0
RETURN
push_s0:
STORE s0, stack_ptr ; preserve register s0 onto “stack”
ADD stack_ptr, 01 ; increment stack pointer
RETURN
pop_s0:
SUB stack_ptr, 01 ; decrement stack pointer
FETCH s0, stack_ptr ; restore register s0 from “stack”
RETURN
Figure 5-4: Use Scratchpad RAM to Emulate PUSH and POP Stack Operations
FIFO Operations
In a similar vein, FIFOs can be created using two separate pointers into scratchpad RAM.
One pointer tracks data being written into RAM; the other tracks data being read from
RAM.
See also:
• “STORE sX, Operand — Write Register sX Value to Scratchpad RAM Location,” page
112.
• “FETCH sX, Operand — Read Scratchpad RAM Location to Register sX,” page 99.
Chapter 6
PORT_ID Port
The 8-bit PORT_ID port supplies the port identifier or port address for the associated
INPUT or OUTPUT operation. The PORT_ID port is valid for two clock cycles, allowing
sufficient time for any interface decoding logic and for connections to asynchronous RAM.
Similarly, the two-cycle operation allows read operations from synchronous RAM, such as
block RAM.
INPUT and OUTPUT operations support both direct and indirect addressing. The port
address is supplied as either as an 8-bit immediate constant or specified indirectly as the
contents of any of the 16 data registers. Indirect addressing is ideal when accessing a block
of memory, either a peripheral at contiguous port addresses or some form of block or
distributed memory within or external to the FPGA.
Adding external peripherals to the PicoBlaze microcontroller is relatively straightforward.
The only challenge is decoding the PORT_ID value using the minimum required logic for
the application. The decoding challenge depends on the number of input, output, or
bidirectional ports, as described in Table 6-1 and subsequent text.
INPUT Operations
An INPUT operation transfers the data supplied on the IN_PORT input port to any one of
the 16 data registers, defined by register sX, as shown in Figure 6-1. The PORT_ID output
port, defined either by register sY or an 8-bit immediate constant, selects the desired input
source. Input sources are generally selected via a multiplexer, using a portion of the bits
from the PORT_ID output port to select a specific source. The size of the multiplexer is
proportional to the number of possible input sources, which has direct implications on
performance.
FPGA Logic
PicoBlaze Microcontroller
8
D Q IN_PORT[7:0] Register sX
m
READ_STROBE
Register sY or
PORT_ID[7:0]
Literal kk 8 n
UG129_c6_01_052004
The INPUT operation asserts the associated READ_STROBE output pulse on the second
cycle of the two-cycle INPUT cycle, as shown in Figure 6-2. The READ_STROBE signal is
seldom used in applications but it indicates that the PicoBlaze microcontroller has
acquired the data. READ_STROBE is critical when reading data from a FIFO,
acknowledging receipt of data as shown in Figure 6-4.
INPUT Operations
0 1 2 3 4
CLK
Contents of
PORT_ID[7:0] register s7
IN_PORT[7:0]
READ_STROBE
IN_C 10
PicoBlaze Microcontroller
IN_PORT[7:0] OUT_PORT[7:0]
PORT_ID[7:0]
IN_B 01
READ_STROBE
WRITE_STROBE
IN_A 00
S0
S1
PORT_ID[0]
PORT_ID[1]
UG129_c6_03_060404
Figure 6-3: Multiplex Multiple Input Sources to Form a Single IN_PORT Port
Failure to include a register anywhere in the path from PORT_ID to IN_PORT is the most
common reason for decreased system clock rates. Consequently, make sure that this path is
registered at some point.
OUTPUT Operations
PicoBlaze Microcontroller
IN_PORT[7:0] OUT_PORT[7:0]
PORT_ID[7:0]
If performance is adequate, 01
remove the flip-flip and combine READ_STROBE
the READ_STROBE and
PORT_ID decode logic. WRITE_STROBE
FIFO
READ DATA_OUT 00
S0
S1
READ_STROBE
PORT_ID[1]
PORT_ID[0]
UG129_c6_04_060404
OUTPUT Operations
As shown in Figure 6-5, an OUTPUT operation presents the contents of any of the 16
registers to the OUT_PORT output port. The PORT_ID output port, defined either by
register sY or an 8-bit immediate constant, selects the desired output destination. The
WRITE_STROBE output pulse indicates that data on the OUT_PORT port is valid and
ready for capture. Typically, the WRITE_STROBE signal, combined with the decoded
PORT_ID port, is used as either a clock enable or a write enable signal to other FPGA logic
that captures the output data.
FPGA Logic
PicoBlaze Microcontroller
m
Register sX OUT_PORT[7:0] D Q
8
WRITE_STROBE
EN
Register sY or
Literal kk PORT_ID[7:0]
8 n
UG129_c6_05_052004
CLK
PORT_ID[7:0] 65
Contents of
OUT_PORT[7:0] Register s0
WRITE_STROBE
OUTPUT Operations
PORT_C
D Q
[2]
EN
PORT_B
D Q
[1]
EN
PORT_A
D Q
[0]
PicoBlaze Microcontroller
EN
IN_PORT[7:0] OUT_PORT[7:0]
PORT_ID[7:0]
READ_STROBE
WRITE_STROBE
UG129_c6_07_052004
Figure 6-7: Simple Address Decoding for Designs with Few Output Destinations
As shown in Figure 6-8, use CONSTANT directives in the program make the code readable
and help ensure that the correct ports are decoded. Because the PORT_ID addresses use
“one-hot” encoding, it is also possible to create a single address that incorporates all the
individual addresses. This way, the PicoBlaze microcontroller can send a broadcast
message to all of the output destinations—in this case, a single instruction clears all
destinations.
DECODE D Q
EN
RAM32X1S (x8)
DECODE D O
WE
A[4:0]
RAM16X1D (x8)
DECODE D SPO
PicoBlaze Microcontroller WE
PORT_ID[7:0]
READ_STROBE
DPO
WRITE_STROBE
DPRA[4:0]
UG129_c6_08_052004
The pipelining registers on the OUT_PORT and PORT_ID signals, shaded in Figure 6-9, are
optional. Both OUT_PORT and PORT_ID are valid for two clock cycles. However,
pipelining them decreases the initial fanout and reduces the routing distance, both of
which improve performance.
During OUTPUT operations, the PicoBlaze microcontroller has no data dependencies and
consequently no dependencies on the FPGA interface logic. If data takes longer than the
two-clock instruction cycle to be captured by the FPGA logic, so be it. The PicoBlaze
microcontroller initiates the OUTPUT operation but does not need to wait while the FPGA
logic captures the data in its ultimate location as long as data is not lost. However,
pipelining INPUT operations can be more complicated. During an INPUT operation, the
PicoBlaze microcontroller requests data from the FPGA logic and must receive the data to
successfully complete the instruction.
Figure 6-10 illustrates the dependency, where the critical timing path is blue. In this
example, the PicoBlaze microcontroller is reading data from a dual-port RAM. This
example assumes that some other function within the FPGA writes data into the dual-port
RAM. When the PicoBlaze microcontroller reads data from the dual-port RAM, the read
address appears on the PORT_ID port. The critical path is the delay from the PORT_ID
port, through the dual-port RAM read path, through the input select multiplexer, to the
setup on the pipelining register. If this path limits performance, add a pipelining register to
improve performance. However, where is the best position for the pipeline register, Point
A or Point B?
RAM16X1D (x8)
D SPO
WE
A[4:0]
00
PicoBlaze Microcontroller
01 B
IN_PORT[7:0] OUT_PORT[7:0] DPO
10 A
PORT_ID[7:0] DPRA[4:0]
11
S0 READ_STROBE
S1
WRITE_STROBE
UG129_c6_09_052004
Figure 6-10: Without Pipelining, the Full Read Path Delay Can Reduce Performance
From Figure 6-2, the read data for INPUT operations must be presented and valid on the
IN_PORT port by the end of the second clock cycle. There is already one layer of pipelining
immediately following the input select multiplexer feeding the IN_PORT port. Adding a
pipelining register at Point A or Point B delays data by an additional clock cycle, too late to
meet the PicoBlaze microcontroller’s requirements.
The best place to position the pipeline register is at Point B, which splits the read path
roughly in half. However, the input select multiplexer structure must be modified to
accommodate the extra register layer, as shown in Figure 6-11.
PicoBlaze Microcontroller
1
0
S2
UG129_c6_10_060404
Chapter 7
KCPSM3
IN_PORT[7:0] OUT_PORT[7:0]
Instruction ROM INTERRUPT PORT_ID[7:0]
(Block RAM)
RESET READ_STROBE
WE OUT[17:0] INSTRUCTION[17:0] WRITE_STROBE
18
ADDR[9:0] INTERRUPT_ACK
ADDRESS[9:0]
10
UG129_c7_01_051504
Figure 7-1: Standard Implementation using a Single 1Kx18 Block RAM as the Instruction Store
ADDR[9:0] ADDRESS[9:0]
10
UG129_c7_02_051504
UART or
JTAG Programmer Block RAM KCPSM3
18 (1Kx18) 18
DIPA[1:0] DOPB[1:0] INSTRUCTION[17:0]
DIA[15:0] DOB[15:0]
WEA
ADDRA[9:0] ADDRB[9:0] ADDRESS[9:0]
10 10
UG129_c7_03_051504
Refer to “Reconfiguring Block RAMs via JTAG” (see Reference 6) for additional details on
implementing this technique.
UG129_c7_04_051804
Two PicoBlaze Microcontrollers with Separate 512x18 Code Images in a Block RAM
Figure 7-5: Two PicoBlaze Microcontrollers with Separate 512-Instruction Memory in one Block RAM
Despite that both PicoBlaze microcontrollers use half the normal instruction store, the
interrupt vectors for both remain the same. When an interrupt occurs, the associated
KCPSM3 block presents all ones on the ADDRESS bus, which is truncated to the last
memory location in its half of the block RAM memory (address 1FF hexadecimal).
Figure 7-5 shows the block RAM split into two equal halves. If one microcontroller requires
more than the other, then tie the upper address lines as appropriate. Practically any
partition is allowed as long as the combined code size is 1,024x18 or less.
Flip-flops to match
Distributed ROM block RAM timing KCPSM3
(<128x18) 18 18
O[17:0] INSTRUCTION[17:0]
ADDRESS[9:6]
ADDR[5:0] ADDRESS[5:0]
6
UG129_c7_06_060404
To maintain compatibility with block RAM, the distributed ROM must have a registered
output using CLB flip-flops.
The CORE Generator software can create all of the above distributed ROM functions using
the coefficients file generated by the PicoBlaze assembler.
Chapter 8
Performance
Input Clock Frequency
Table 8-1 shows the maximum available performance for the PicoBlaze microcontroller
using various FPGA families and speed grades. The Virtex-II and Virtex-II Pro FPGA
families are optimized for maximum performance. The Spartan-3 FPGA family is
optimized for lowest cost.
Unless the end application requires absolute performance, there is no need to operate the
PicoBlaze microcontroller at its maximum clock frequency. In fact, operating slower is
advantageous. Often, the PicoBlaze microcontroller is managing slower peripheral
operations like serial communications or monitoring keyboard buttons, neither of which
stresses the FPGA’s performance. A lower clock frequency reduces the number of idle
instruction cycles and reduces total system power consumption.
The PicoBlaze microcontroller is a fully static design and operates down to DC (0 MHz).
Chapter 8: Performance
Chapter 9
KCPSM3 Module
The KCPSM3 module contains the PicoBlaze ALU, register file, scratchpad, RAM, etc. The
only function not included is the instruction store. The component declaration for the
KCPSM3 module appears in Figure 9-1. Figure 9-2 lists the KCPSM3 component
instantiation.
component KCPSM3
port (
address : out std_logic_vector( 9 downto 0);
instruction : in std_logic_vector(17 downto 0);
port_id : out std_logic_vector( 7 downto 0);
write_strobe : out std_logic;
out_port : out std_logic_vector( 7 downto 0);
read_strobe : out std_logic;
in_port : in std_logic_vector( 7 downto 0);
interrupt : in std_logic;
interrupt_ack : out std_logic;
reset : in std_logic;
clk : in std_logic
);
end component;
processor: kcpsm3
port map(
address => address_signal,
instruction => instruction_signal,
port_id => port_id_signal,
write_strobe => write_strobe_signal,
out_port => out_port_signal,
read_strobe => read_strobe_signal,
in_port => in_port_signal,
interrupt => interrupt_signal,
interrupt_ack => interrupt_ack_signal,
reset => reset_signal,
clk => clk_signal
);
component prog_rom
port (
address : in std_logic_vector( 9 downto 0);
instruction : out std_logic_vector(17 downto 0);
clk : in std_logic
);
end component;
program: prog_rom
port map( address => address_signal,
instruction => instruction_signal,
clk => clk_signal
);
Chapter 10
KCPSM3
Assembler
The KCPSM3 Assembler is provided as a simple DOS executable file together with three
template files. Copy all the files KCPSM3.EXE, ROM_form.vhd, ROM_form.v, and
ROM_form.coe into your working directory.
Programs are best written with either the standard Notepad or Wordpad tools available on
most Windows computers. However, any PC-format text editor is sufficient. Save the
PicoBlaze assembly program with a PSM file extension (eight-character name limit).
Open a DOS box and navigate to the working directory. To assemble the PicoBlaze
program, type:
kcpsm3 <filename>[.psm]
Assembly Errors
The assembler halts as soon as an error is detected. A short message indicates the reason
for any error. The assembler also displays the line that it was analyzing when it detected
the problem. Fix each reported problem in turn and re-execute the assembler.
Since the execution of the assembler is very fast, it is unlikely that you will be able to ‘see’
it making progress, and the display will appear to be immediate. To review everything that
the assembler has written to the screen, the DOS output can be redirected to a text file
using:
kcpsm3 <filename>[.psm] > screen_dump.txt
All five assembler passes are recorded in the pass*.dat output files. Should an error
occur during assembly, these files may contain additional details on the error.
If the source program is free of errors, the KCPSM3 assembler generates an object code
output, formatted for a variety of design flows, based on the initial template files. These
output files generate the code ROM, instantiated as a block RAM, and properly initialized
with the PicoBlaze object code. The assembler also generates equivalent raw decimal and
hexadecimal output files for other utilities.
Mediatronix pBlazIDE
The assembler also produces a log file plus files that show the assignments for various
labels and constants found in the source code. The log file shows the instruction address,
the opcode for each instruction, and the source code instruction and comments for the
associated instruction address. The assigned values for register names, labels, and
constants appear immediately following the associated symbolic name.
Finally, the KCPSM3 assembler generates a formatted version of the source program
(“pretty print” output). The formatted output file formats all labels and comments,
converts all commands and hexadecimal constants to upper case, and consistently spaces
operands.
Mediatronix pBlazIDE
The Mediatronix pBlazIDE software, shown in Figure 12-1, page 85, is a free, graphical,
integrated development environment for Windows-based computers. Its features are as
follows:
• Syntax color highlighting
• Instruction set simulator (ISS)
♦ Breakpoints
♦ Register display
♦ Memory display
• Source code formatter (“pretty print”)
• KCPSM3-to-pBlazIDE import function/syntax conversion
• HTML output, including color highlighting
Download the pBlazIDE software directly from the Mediatronix website:
http://www.mediatronix.com/pBlazeIDE.htm
pBlaze IDE
File Edit View Settings Help
Settings
Picoblaze I
1 a_lsb EQU
2 Picoblaze II
3 switch_lsb PicoblazeDSIN
3
4 Picoblaze CR
5 LOAD
UG129_c10_02_051504
pBlaze IDE
F ile E dit V iew
New Ctrl+N
Recent Files
Import
Export HTML
UG129_c10_03_051504
Figure 10-3: Converting Xilinx Syntax PicoBlaze Source Code to pBlazIDE Syntax
main: main:
; initialize 16-bit counter, enable interrupts ; initialize 16-bit counter, enable interrupts
LOAD count16_lsb, myconstant LOAD count16_lsb, myconstant
ENABLE INTERRUPT EINT
loop: loop:
; continuously increment 16-bit counter ; continuously increment 16-bit counter
CALL increment_count CALL increment_count
JUMP loop JUMP loop
end_main: end_main:
increment_count: increment_count:
; add 1 to LSB of 16-bit counter ; add 1 to LSB of 16-bit counter
ADD count16_lsb, 01 ADD count16_lsb, 1
; only add one to MSB if carry generated by LSB ; only add one to MSB if carry generated by LSB
ADDCY count16_msb, 00 ADDC count16_msb, 0
RETURN RET
isr: isr:
; decrement 16-bit counter by one on interrupt ; decrement 16-bit counter by one on interrupt
; subtract 1 from LSB of 16-bit counter ; subtract 1 from LSB of 16-bit counter
SUB count16_lsb, 01 SUB count16_lsb, 1
; only subtract one from MSB if borrow ; only subtract one from MSB if borrow
; generated by LSB ; generated by LSB
SUBCY count16_msb, 00 SUBC count16_msb, 0
RETURNI ENABLE RETI ENABLE
; interrupt vector is always in last memory location ; interrupt vector is always in last memory location
ADDRESS 3FF ORG $3FF
; jump to interrupt service routing (ISR) ; jump to interrupt service routing (ISR)
JUMP isr JUMP isr
UG129_c10_04_052004
Figure 10-4: Example of How KCPSM Source Code Converts to pBlazIDE Code
Directives
Table 10-3 lists the KCPSM3 and PBlazIDE directives for various functions.
Chapter 11
Assembler Directives
Both the KCPSM3 and pBlazIDE assemblers include directives that provide advanced
control.
In the KCPSM3 assembler, the NAMEREG directive is applied in-line with the code. Before
the NAMEREG directive, the register is named using the ‘sX’ style. Following the directive,
only the new name applies. It is also possible to rename a register again (i.e., NAMEREG
old_regname, new_regname) and only the new name applies in the subsequent
program lines.
Defining Constants
Similar to renaming registers, assign names to constant values. By defining names for
constants, it is easier to understand and document the PicoBlaze code rather than using the
constant values in the code. Similarly, assigning names to registers and constants simplifies
code maintenance. Updating the value assigned to a constant is easier if the constant is
declared just once rather than searching for each occurrence in the application code.
Table 11-3 shows how to define a constant called myconstant and assign the value 80
hexadecimal. Both KCPSM3 and pBlazIDE formats are shown.
KCPSM3
The output files from the KCPSM3 assembler are always named according to the source
program name. For example, an assembly program named myprog.psm produces output
files called myprog.vhd, myprog.v, etc. for the various output formats.
pBlazIDE
The pBlazIDE assembler provides a directive, using the keyword VHDL, to explicitly name
the target output file and the VHDL entity name, as shown in Figure 11-1. The
template.vhd file contains the VHDL template for the program ROM. The target.vhd
file is the output VHDL file, derived from the template.vhd file, which contains the
initialization values created by assembling the PicoBlaze code. Finally, entity_name is
the VHDL entity name used to name program ROM.
Input Ports
The DSIN directive defines the name and the port address (or port identification number)
for a read-only input port. The DSIN directive models an input port that only connects to
the PicoBlaze microcontroller’s IN_PORT port. An optional field specifies a text file
containing input values used during instruction set simulation. Figure 11-2 provides an
example.
$FF
01
02
03
$A5
$5A
During instruction set simulation, pBlazIDE displays the input port as shown in
Figure 11-4. Edit the input port values during simulation by checking the individual bit
values or, to modify the entire byte value, double-click the current port value.
input_port_name
Port address
(Port ID #) $FF $27
7 6 5 4 3 2 1 0
Check or uncheck to
set or clear a bit.
UG129_c11_01_051404
Output Ports
The DSOUT directive defines the name and the port address (or port identification
number) for a write-only output port. The DSOUT directive models an output port that
only connects to the PicoBlaze microcontroller’s OUT_PORT port. An optional field
specifies a text file that records the result of any output operations to this during
instruction set simulation. Figure 11-5 provides an example.
Input/Output Ports
The DSIO directive defines the name and the port address (or port identification number)
for an output port. However, the DSIO directive differs from the DSOUT directive in that
the PicoBlaze microcontroller can also read DSIO output values. The DSIO directive
models an output port that connects to both the PicoBlaze microcontroller’s IN_PORT and
OUT_PORT ports and has the same port address for input and output operations. An
optional field specifies a text file that records the result of any output operations to this
during instruction set simulation. Figure 11-7 provides an example.
During instruction set simulation, pBlazIDE displays the readable output port as shown in
Figure 11-8. The port value can be modified from the graphical interface.
User-defined input/
output port name. Current port value.
Double-click to edit.
input_output_port_name
Port address
(Port ID #) $02 $F0
7 6 5 4 3 2 1 0
Click LED to set or
clear bit for input. UG129_c11_03_060404
Figure 11-8: The pBlazIDE DSIO Directive Defines an Output Port That Can Be
Read Back
Chapter 12
Furthermore, the pBlazIDE ISS offers full single-step and breakpoint support while
viewing the PicoBlaze assembly source code. Evaluate the software timing for end
application. Observe code coverage. Simulate basic FPGA interaction using the pBlazIDE
DSIN, DSOUT, and DSIO directives (see “Defining I/O Ports (pBlazIDE),” page 78 for
more information).
Best of all, the pBlazIDE graphical development environment is free! Download the latest
version directly from the Mediatronix website:
http://www.mediatronix.com/pBlazeIDE.htm
The pBlazIDE ISS does not support full simulation of the PicoBlaze microcontroller
embedded with all the other FPGA logic. Fortunately, the PicoBlaze core source files
support both VHDL and Verilog simulation using the ModelSim simulator. ModelSim
allows the entire design to be simulated, including accurate timing information and textual
disassembly features.
If using the Xilinx System Generator software, there is full development and system-level
simulation support for the PicoBlaze microcontroller. Refer to “Designing PicoBlaze
Microcontroller Application” in the Xilinx System Generator User Guide (see Reference 3) for
more information.
Simulation control
buttons
pBlaze IDE
File Edit View Settings Help
Status
flags Status switches
$00 switches
Constant DSIN 0
$00 $27
Zero $01 declaration
LEDs DSOUT 1
7 6 5 4 3 2 1 0
$02 mailbox DSIO 2
Carry
LEDs
Enable
Register
s3 aliasing input_value EQU
Input, s3
output, and I/O displays $01 $A5
s4 LED_output EQU 7 6 5 4 3 2 1 0
Interrupt and controlss4defined by DSIN,
control for Interrupt DSOUT, and DSIO directives mailbox
simulation $80 threshold_value EQU $80
Steady Defined start $02 $F0
address 7 6 5 4 3 2 1 0
Edge $000 ORG 0
start :
Timer Syntax-highlighted Port
Instruction Instruction assembly code Port ID
50 address
$000 $3C001 code EINT ; enable interrup Number Value
Data Registers poll_loop :
registers Code coverage indicator.
$001 $04300 IN input_value , swit
0 00 00 8 Also, click to set or
1 00 $002 $14380
remove breakpoint COMP input_value , thre
00 9
$003 $31806 CALL C , process_input
2 00 00 A
Next instruction
3 27 00 B $004 $2C401 to be executed OUT LED_output , LEDs
5 00 00 D
$006 $04402 process_input : IN LED_output , mailb
6 00 00 E $007 $2C401 OUT LED_output , LEDs
7 00 00 F
Breakpoint set at Scratchpad RAM display
this instruction only appears if STORE or
Scratchpad RAM FETCH instructions
$00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 appear in application
code
$10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Current Stack
$20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Execution time at Pointer
$30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 specified clock
0 1 2 3 4 5 6 7 8 9 A B C D E F frequency
Status Stack values
window Cursor row and Number of instructions Current Program
Assembler Phase 3:column
building simulation objects
already executed up to Counter
Program is Reset position current code position
Mode: PicoBlaze-3 26: 1 Modified Instructions: 4 Time: 95 ns PC: $006 SP: 1 ($01) Stack: $04
UG129_c12_01_051604
Edit Leave simulator and return to editor. All the simulation control buttons are disabled.
Run Run the program. The program continues running until an active breakpoint is
encountered or if the Reset or Pause button is pressed.
Execute a single instruction. The active register, memory, and I/O displays are updated
Single Step and the cursor arrow advances to the next instruction. The next instruction to be executed
is highlighted in blue and a blue arrow appears to the left of the instruction line. After
executing an instruction, the code coverage indicator changes from blue to green. If
executing a valid CALL instruction, the program steps into the subroutine function.
Step Over Behaves like the Single Step button except that the simulator does not step into CALL
instructions. If executing a valid CALL instruction, the program executes the entire
subroutine and the display advances to the instruction following the CALL instruction.
Run to Cursor Run the program until the program advances to the current cursor location.
Pause Pause a running simulation and display the current state of the PicoBlaze microcontroller.
Continue the simulation using any of the green action buttons.
Set or remove a breakpoint on the instruction at the current cursor location. The instruction
Toggle Breakpoint line is highlighted in red and a breakpoint indicator appears to the left of the line. Multiple
breakpoints may be simultaneously active. If the simulation reaches an instruction with an
active breakpoint, the simulation automatically pauses.
Remove All Clear all active breakpoints.
Breakpoints
Appendix A
Appendix B
KCPSM3 Syntax
Figure B-1 provides a code template for creating PicoBlaze applications using the KCPSM3
assembler.
BEGIN:
; <<< your code here >>>
pBlazIDE Syntax
Figure B-2 provides a code template for creating PicoBlaze applications using the
pBlazIDE assembler.
BEGIN:
; <<< your code here >>>
Appendix C
Register sY or
Literal kk
Carry Out
CARRY Register sX
UG129_aC_01_051604
Pseudocode
sX Å (sX + Operand) mod 256; always an 8-bit result
PC Å PC + 1
Registers/Flags Altered
Registers: sX, PC
Flags: CARRY, ZERO
Register sY or
Literal kk
CARRY Register sX
UG129_aC_02_051604
if (CARRY = 1) then
sX Å (sX + Operand + 1) mod 256; always an 8-bit result
else
sX Å (sX + Operand) mod 256 ; always an 8-bit result
end if
PC Å PC + 1
Registers/Flags Altered
Registers: sX, PC
Flags: CARRY, ZERO
Notes
pBlazIDE Equivalent: ADDC
Register sY
7 6 5 4 3 2 1 0
Literal kk
Register sX 7 6 5 4 3 2 1 0
UG129_aC_06_051604
CARRY Å 0
if (sX = 0) then
ZERO Å 1
else
ZERO Å 0
end if
PC Å PC + 1
Registers/Flags Altered
Registers: sX, PC
Flags: ZERO, CARRY is always 0
Depending on the specified Condition, the program calls the subroutine beginning at the
specified Address. If the specified Condition is not met, the program continues to the next
instruction.
Pseudocode
if (Condition = TRUE) then
; push current PC onto top of the CALL/RETURN stack
; TOS = Top of Stack
TOS Å PC
; load PC with specified Address
PC Å Address
else
PC Å PC + 1
endif
Registers/Flags Altered
Registers: PC, CALL/RETURN stack
Flags: Not affected
Notes
The maximum number of nested subroutine calls is 31 levels, due to the depth of the
CALL/RETURN stack.
COMPARE operation. The second operand is also any register or an 8-bit immediate
constant value. Only the flags are affected by this operation.
Register sX Register sY or
Literal kk
A B A B
A=B? B>A?
ZERO CARRY
UG129_aC_05_051604
Register sX is compared against Operand. The ZERO flag is set when Register sX and
Operand are identical. The CARRY flag is set when Operand is larger than Register sX,
where both Operand and Register sX are evaluated as unsigned integers.
Example
Operand is a register location, sY, or an immediate byte-wide constant, kk.
COMPARE sX, sY; Compare sX against sY.
COMPARE sX, kk; Compare sX against immediate constant, kk.
Pseudocode
if ( Operand > sX ) then
CARRY Å 1
else
CARRY Å 0
endif
if ( sX = Operand ) then
ZERO Å 1
else
ZERO Å 0
endif
PC Å PC + 1
Registers/Flags Altered
Registers: PC only. No data registers affected.
Flags: CARRY, ZERO
Notes
pBlazIDE Equivalent: COMP
The COMPARE instruction is only supported on PicoBlaze microcontrollers for Spartan-3,
Virtex-II, and Virtex-II Pro FPGAs.
PC Å PC + 1
Registers/Flags Altered
Registers: PC
Flags: INTERRUPT_ENABLE
Notes
PBlazIDE Equivalent: DINT
PC Å PC + 1
Registers/Flags Altered
Registers: PC
Flags: INTERRUPT_ENABLE
Notes
PBlazIDE Equivalent: EINT
the least-significant six bits of Operand, bits 5 to bit 0. Consequently, a FETCH operation
from address FF is equivalent to a FETCH operation from address 3F.
FALSE WRITE_ENABLE
[5:0]
Register sY or
ADDRESS[5:0]
Literal kk
[7]
[6] UG129_aC_11_051604
Examples
FETCH sX, (sY) ; Read scratchpad RAM location specified by the
; contents of register sY into register sX
PC Å PC + 1
Registers/Flags Altered
Registers: PC
Flags: None
Notes
pBlazIDE Equivalent: The instruction mnemonic, FETCH, is the same for both KCPSM3
and pBlazIDE. However, the instruction syntax for indirect addressing is slightly different.
The KCPSM3 syntax places parentheses around the indirect address while the pBlazIDE
syntax uses no parentheses.
Interface logic decodes the PORT_ID address to provide the correct value on IN_PORT.
Examples
INPUT sX, sY ; Read the value on IN_PORT into register sX, set PORT_ID
; to the contents of sY
INPUT sX, kk ; Read the value on IN_PORT into register sX, set PORT_ID
; to the immediate constant kk
Pseudocode
PORT_ID Å Operand
sX Å IN_PORT
PC Å PC + 1
Registers/Flags Altered
Registers: sX, PC
Flags: None
Notes
pBlazIDE Equivalent: IN
The READ_STROBE output is asserted during the second CLK cycle of the two-cycle
INPUT operation.
endif
Registers/Flags Altered
Registers: PC, CALL/RETURN stack
Flags: CARRY, ZERO, INTERRUPT_ENABLE
Notes
The PicoBlaze microcontroller asserts the INTERRUPT_ACK output on the second CLK
cycle of the two-cycle Interrupt Event, as shown in Figure 4-3, page 45. This signal is
optionally used to clear any hardware interrupt flags.
The programmer must ensure that a RETURNI instruction is only performed in response to
a previous interrupt. Otherwise, the PC stack may not contain a valid return address.
Do not use the RETURNI instruction to return from a subroutine CALL. Instead, use the
RETURN instruction.
Because an interrupt event may happen at any time, the values of the CARRY and ZERO
flags cannot be predetermined. Consequently, the corresponding Interrupt Service Routine
(ISR) must not depend on specific values for the CARRY and ZERO flags.
Pseudocode
if (Condition = TRUE) then
PC Å Address
else
PC Å PC + 1
endif
Registers/Flags Altered
Registers: PC
Flags: Not affected
PC Å PC + 1
Registers/Flags Altered
Registers: sX, PC
Flags: Not affected
Register sY
7 6 5 4 3 2 1 0
Literal kk
Register sX 7 6 5 4 3 2 1 0
UG129_aC_07_051604
The OR instruction provides a way to force the setting any bit of the specified register,
which can be used to form control signals.
Examples
OR sX, sY ; Logically OR the individual bits of register sX with the
; corresponding bits in register sY
OR sX, kk ; Logically OR the individual bits of register sX with the
; corresponding bits in the immediate constant kk
OUTPUT sX, Operand — Write Register sX Value to OUT_PORT, Set PORT_ID to Operand
Pseudocode
; logically OR the corresponding bits in sX and the Operand
for (i=0; i<= 7; i=i+1)
{
sX(i) Å sX(i) OR Operand(i)
}
CARRY Å 0
if (sX = 0) then
ZERO Å 1
else
ZERO Å 0
end if
PC Å PC + 1
Registers/Flags Altered
Registers: sX, PC
Flags: ZERO, CARRY is always 0
FPGA Logic
PicoBlaze Microcontroller
m
Register sX OUT_PORT[7:0] D Q
8
WRITE_STROBE
EN
Register sY or
Literal kk PORT_ID[7:0]
8 n
UG129_c6_05_052004
Pseudocode
PORT_ID Å Operand
OUT_PORT Å sX
PC Å PC + 1
Registers/Flags Altered
Registers: PC
Flags: None
Notes
pBlazIDE Equivalent: OUT
The WRITE_STROBE output is asserted during the second CLK cycle of the two-cycle
OUTPUT operation.
RESET Event
The reset event is not an instruction but the response of the PicoBlaze microcontroller
when the RESET input is High. A RESET Event restarts the PicoBlaze microcontroller and
clears various hardware elements, as shown in Table C-3.
A RESET Event is automatically generated immediately following FPGA configuration,
initiated by the FPGA’s internal Global Set/Reset (GSR) signal. After configuration, the
FPGA application generates RESET Event by asserting the RESET input before a rising
CLK clock edge.
The general-purpose registers, the scratchpad RAM, and the program store are not affected
by a RESET Event. The CALL/RETURN stack is a circular buffer, although a RESET Event
essentially resets the CALL/RETURN stack pointer.
Pseudocode
if (RESET input = High) then
; clear Program Counter
PC Å 0
Pseudocode
if (Condition = TRUE) then
; pop the top of the CALL/RETURN stack into PC
; TOS = Top of Stack
PC Å TOS + 1; incremented value from Top of Stack
else
PC Å PC + 1
endif
Registers/Flags Altered
Registers: PC, CALL/RETURN stack
Flags: Not affected
Notes
Do not use the RETURN instruction to return from an interrupt. Instead, use the RETURNI
instruction.
PBlazIDE Equivalent: RET, RET C, RET NC, RET Z, RET NZ
RL sX
CARRY Register sX
7 6 5 4 3 2 1 0
Example
RL sX; Rotate left. Bit sX[7] copied into CARRY.
Pseudocode
CARRY Å sX[7]
sX Å { sX[6:0], sX[7]}
if ( sX = 0 ) then
ZERO Å 1
else
ZERO Å 0
endif
PC Å PC + 1
Registers/Flags Altered
Registers: sX, PC
Flags: CARRY, ZERO
RR sX
Register sX CARRY
7 6 5 4 3 2 1 0
Example
RR sX; Rotate right. Bit sX[0] copied into CARRY
Pseudocode
CARRY Å sX[0]
sX Å {sX[0], sX[7:1]}
if ( sX = 0 ) then
ZERO Å 1
else
ZERO Å 0
endif
PC Å PC + 1
Registers/Flags Altered
Registers: sX, PC
Flags: CARRY, ZERO
The ZERO flag is always 0 after executing the SL1 instruction because register sX is never
zero.
Examples
SL0 sX; Shift left. 0 shifts into LSB, MSB shifts into CARRY.
SL1 sX; Shift left. 1 shifts into LSB, MSB shifts into CARRY.
SLX sX; Shift left. LSB shifts into LSB, MSB shifts into CARRY.
SLA sX; Shift left. CARRY shifts into LSB, MSB shifts into CARRY.
Pseudocode
case (INSTRUCTION)
when “SL0”
LSB Å 0
when “SL1”
LSB Å 1
when “SLX”
LSB Å sX(7)
when “SLA”
LSB Å CARRY
end case
CARRY Å sX[7]
sX Å {sX[6:0], LSB}
if ( sX = 0 ) then
ZERO Å 1
else
ZERO Å 0
endif
PC Å PC + 1
Registers/Flags Altered
Registers: sX, PC
Flags: CARRY, ZERO
The ZERO flag is always 0 after executing the SR1 instruction because register sX is never
zero.
Example
SR0 sX; Shift right. 0 shifts into MSB, LSB shifts into CARRY.
SR1 sX; Shift right. 1 shifts into MSB, LSB shifts into CARRY.
SRX sX; Shift right MSB shifts into MSB, LSB shifts into CARRY.
SRA sX; Shift right CARRY shifts into MSB, LSB shifts into CARRY.
Pseudocode
case (INSTRUCTION)
when “SR0”
MSB Å 0
when “SR1”
MSB Å 1
when “SRX”
MSB Å sX(7)
when “SRA”
MSB Å CARRY
end case
CARRY Å sX[0]
sX Å {MSB, sX[7:1]}
if ( sX = 0 ) then
ZERO Å 1
else
ZERO Å 0
endif
PC Å PC + 1
Registers/Flags Altered
Registers: sX, PC
Flags: CARRY, ZERO
TRUE WRITE_ENABLE
[5:0]
Register sY or
ADDRESS[5:0]
Literal kk
[7]
[6] UG129_aC_10_051604
PC Å PC + 1
Registers/Flags Altered
Registers: sX, PC
Flags: None
Notes
pBlazIDE Equivalent: The instruction mnemonic, STORE, is the same for both KCPSM3
and pBlazIDE. However, the instruction syntax for indirect addressing is slightly different.
The KCPSM3 syntax places parentheses around the indirect address while the pBlazIDE
syntax uses no parentheses.
Register sY or
Literal kk
Borrow
CARRY Register sX
UG129_aC_03_051604
PC Å PC + 1
Registers/Flags Altered
Registers: sX, PC
Flags: CARRY, ZERO
Register sY or
Literal kk
Borrow Borrow In
CARRY Register sX
UG129_aC_04_051604
Examples
Operand is a register location, sY, or an immediate byte-wide constant, kk.
SUBCY sX, sY; Subtract register. sX = sX - sY - CARRY
SUBCY sX, kk; Subtract immediate. sX = sX - kk - CARRY
Description
Operand and CARRY flag are subtracted from register sX. The ZERO and CARRY flags are
set appropriately.
Pseudocode
if (CARRY = 1) then
sX Å (sX - Operand – 1) mod 256; always an 8-bit result
else
sX Å (sX – Operand) mod 256 ; always an 8-bit result
endif
PC Å PC + 1
Registers/Flags Altered
Registers: sX
Flags: CARRY, ZERO
Notes
pBlazIDE Equivalent: SUBC
Register sY
Literal kk 7 6 5 4 3 2 1 0
Register sX 7 6 5 4 3 2 1 0
Bitwise AND
ZERO UG129_c3_03_051404
Register sY
Literal kk 7 6 5 4 3 2 1 0
Register sX 7 6 5 4 3 2 1 0
CARRY UG129_c3_04_051404
TEST sX, Operand — Test Bit Location in Register sX, Generate Odd Parity
Examples
TEST sX, sY ; Test register sX using register sY as the test mask
TEST sX, kk ; Test register sX using the immediate constant kk as the
; test mask
Pseudocode
; logically AND the corresponding bits in sX and the Operand
for (i=0; i<= 7; i=i+1)
{
AND_TEST(i) Å sX(i) AND Operand(i)
}
if (AND_TEST = 0) then
ZERO Å 1
else
ZERO Å 0
end if
PC Å PC + 1
Registers/Flags Altered
Registers: PC
Flags: ZERO, CARRY
The TEST instruction is only supported on PicoBlaze microcontrollers for Spartan-3,
Virtex-II, and Virtex-II Pro FPGAs.
Register sY
7 6 5 4 3 2 1 0
Literal kk
Register sX 7 6 5 4 3 2 1 0
UG129_aC_08_051604
CARRY Å 0
if (sX = 0) then
ZERO Å 1
else
ZERO Å 0
end if
PC Å PC + 1
Registers/Flags Altered
Registers: sX, PC
Flags: ZERO, CARRY is always 0
Appendix D
Instruction Codes
Table D-1 provides the 18-bit instruction code for every PicoBlaze instruction.
Appendix E
Registers
Reg. Description
s0
s1
s2
s3
s4
s5
s6
s7
s8
s9
sA
sB
sC
sD
sE
sF
Scratchpad RAM