General Purpose Hardware: From Special-Purpose FSMD To General-Purpose Microcontroller
General Purpose Hardware: From Special-Purpose FSMD To General-Purpose Microcontroller
MICROCONTROLLER: PicoBlaze
8
PicoBlaze
Basic Features:
• 8-bit data width
• 8-bit ALU with the carry and zero flags
• 16 8-bit general-purpose registers
• 64-byte data memory
• 18-bit instruction width
• 10-bit instruction address (supports a program up to 1024 instructions)
• 31-word call/return stack
• 256 input ports and 256 output ports
• 2 clock cycles per instruction
• 5 clock cycles for interrupt handling
9
PicoBlaze
PicoBlaze
2. Instruction ROM : To store the compiled assembly code to memory in
advance and configure it as a ROM in HDL code
PicoBlaze
The basic PicoBlaze-based development flow in steps:
1. Determine the software-hardware partition
2. Develop the assembly program for the software portion
3. Compile the assembly program to generate an instruction ROM (HDL file)
4. Perform instruction-set-level simulation
5. Derive HDL code for the hardware portion (customized circuits for time-
critical functions and to interface with PicoBlaze)
6. Create the top-level HDL code that combines the codes for the PicoBlaze
core, the instruction ROM, and customized hardware
7. Develop a testbench and perform HDL simulation for the entire system.
8. Synthesize and implement the HDL code and program the FPGA chip on
the proto typing board
PicoBlaze
Functional Blocks/Programming model
PicoBlaze
INSTRUCTION SET
PicoBlaze has 57 instructions. According to the nature of operations these
are divided into following categories:
• Logical instructions
• Arithmetic instructions
• Compare and test instructions
• Shift and rotate instructions
• Data movement instructions
• Program flow control instructions
• Interrupt related instructions
PicoBlaze
PicoBlaze
Instruction format
PicoBalze’s instructions have five formats:
op sX, sY: register-register format.
SX ←SX op SY
op sX, KK: register-constant format.
SX ← SX op KK
op sX: single-register format.
SX ← op SX
op AAA: single-address format. used in jump and call instructions.
AAA - an address of the instruction memory. If the specified condition
is met, AAA is loaded into the PC
op: zero-operand format. used in some instructions that don’t involve any operand
Two assembler programs for PicoBlaze: KCPSM3 from Xilinx
PBlazeIDE from Mediatronix ( )
PicoBlaze
Logical instructions
• Six logical instructions, which support the and, or, and xor operations
• Performs bitwise logical operation between two registers or one register and a
constant
• Carry flag c is always cleared; zero flag reflect the result of the operation
and sX, SY or sX, KK
Macro RTL SX ← sX and sY Macro RTL sX ← sX or KK
c←0 c ←0
and sX, KK xor sX, SY
Macro RTL SX ← sX and KK Macro RTL sX ←sX xor sY
c←0 c ←0
or sX, SY xor sX, KK
Macro RTL SX ← sX or sY Macro RTL SX ←sX xor KK
c←0 c ←0
PicoBlaze
Arithmetic instructions
• Eight arithmetic instructions; support addition and subtraction with or without
the carry
• carry flag and the zero flag reflect the result of operation
subcy sX, sY (subc sX, sY) - subtract with the carry flag (as a borrow bit)
sX ←sX - sY - c
subcy sX, KK (subc sX, KK) - subtract with the carry flag (as a borrow bit)
sX ← sX - KK - c
PicoBlaze
Compare and test instructions
• Examine two registers or one register and constant, and set the carry and zero
flags accordingly; the contents of the registers remain intact
• The instructions are usually used in conjunction with a conditional jump or call
instruction
compare sX, sY (comp sX, sY)- compare two registers and set the flags
if sX = sY then z ← 1 else z ← 0
if sY > sX then c ← 1 else c ← 0
compare sX, KK (comp sX, KK) - compare a register and a constant and set flags
if sX = KK then z ← 1 else z ← 0
if KK > sX then c ← 1 else c ← 0
A test instruction performs an and operation. If the result is 0, zero flag is set to 1.
The result is also checked for the odd parity. Carry flag is set for odd parity. 22
PicoBlaze
test sX, sY - test two registers and set the flags
t ← sX and sY
if t =0 then z ← 1 else z ← 0
c ← t(7) xor t(6) xor . . . xor t(0)
test sX, KK - test a register and a constant and set the flags
t ← sX and KK
if t = 0 then z ← 1 else z ← 0
c ← t(7) xor t(6) xor . . . xor t(0)
PicoBlaze
sl0 sX - shift a register left 1 bit and shift 0 into the LSB
sX ← sX (6 . . 0) & 0
c ← sX(7)
sl1 sX - shift a register left 1 bit and shift 1 into the LSB
sX ← sX (6 . . 0) & 1
c ← sX(7)
slx sX - shift a register left 1 bit and shift sX(0) into LSB
sX ← sX (6 . . 0) & sX(0)
c←sX(7)
sla sX - shift a register left 1 bit and shift c into the LSB
sX ← sX(6 . . 0) & c
c ← sX(7)
rl sX - rotate a register left 1 bit
sX ← sX(6..0) & sX(7)
24
c← sX(7)
PicoBlaze
sr0 sX - shift a register right 1 bit and shift 0 into the MSB
sX ← 0 & sX(7..1)
c ← sX(0)
srl sX - shift a register right 1 bit and shift 1 into the MSB
sX ← 1 & sX(7..1)
c ← sX(0)
srx sX - shift a register right 1 bit and shift sX(7) into MSB
sX ← sX(7) & SX(7.. 1)
c ← sX(0)
sra sX - shift a register right 1 bit and shift c into the MSB
sX ← c & sX(7..1);
c ← sX(0)
rr sX - rotate a register right 1 bit
sX ← sX(0) & sX(7..1)
c ← sX(0)
PicoBlaze
Data movement instructions
• The computation is done via the registers and ALU. The data RAM supplies
additional storage and the I/O ports provide paths to peripherals
• Several instructions to move data between registers, data RAM, and I/O ports.
• Between registers: the load instruction
• Between a register and data RAM: the fetch and store instruction
• Between a register and an I/O port: the input and output instruction
26
PicoBlaze
fetch sX, (sY) (fetch sX, sY) - move data from the data RAM to a register
sX ← RAM [(sY)]
store sX, (sY) (store sX , sY) - move data from a register to the data RAM
RAM [(sY)] ← sX
input sX, (sY) (in sX, sY) - move data from the input port to a register
port_ id ← sY
sX ← in_port 27
PicoBlaze
input sX, KK (in sX, KK) - move data from the input port to a register
port_ id ← KK
sX ←in_port
output sX, (sY) (out sx, sY) - move data from a register to the output port
port_ id ← sY
out_port ← sX
output sX, KK (out sX, KK) - move data from a register to the output port
port_id ← KK
out_ port ← sX
• No explicit instruction to move data to or from the instruction memory
• Many instructions include a field for an immediate constant, it can be considered
as data that is implicitly moved from the instruction memory to a register
28
PicoBlaze
Program flow control instructions
• The PC indicates where to fetch the instruction. By default, execution proceeds
to the next address the PC is implicitly incremented (pc ← pc+1)
• jump, call and return instructions can explicitly load a value to the PC and
modify the program flow
• These instructions can be executed unconditionally or conditionally based on the
values of the carry and zero flags
• A jump instruction loads new value to PC if the corresponding condition is met,
program execution changes the regular flow and branches to the new address
PicoBlaze
jump c, AAA -jump if the carry flag is set
if c = l
then pc ← AAA
else pc ← pc + 1
jump nc, AAA -jump if the carry flag is not set
if c=0
then pc ← AAA
else pc ← pc + 1
jumpz, AAA - jump if the zero flag is set
if z=1
then pc ← AAA
else pc ← pc + 1
jump nz, AAA - jump if the zero flag is not set
if z=0
then pc ← AAA
30
else pc ← pc + 1
PicoBlaze
• The call and return instructions are
used to implement a software
function
• PicoBlaze allows nested function calls
(a function can be called within
another function)
• A stack, which is a last-in-first-out
buffer, is used to store the PC’s
values
PicoBlaze
call AAA - unconditionally call subroutine
tos ← tos + 1
STACK[tos] ← pc
pc ← AAA
call c, AAA - call subroutine if the carry flag is set
if c = l
then tos ← tos + 1
STACK[tosl ← pc
pc ← AAA
else pc ← pc + 1
call nc, AAA - call subroutine if the carry flag is not set
if c=0
then tos ← tos+ 1
STACK[tos] ← pc
pc ← AAA
else pc ← pc + 1
PicoBlaze
call z, AAA - call subroutine if the zero flag is set
if z = 1
then tos ← tos + 1
STACK[tos] ← pc
pc ← AAA
else pc ← pc + 1
call nz, AAA - call subroutine if the zero flag is not set
if z=0
then tos ← tos + 1
STACK[tos] ← pc
pc ← AAA
else pc ← pc + 1
return (ret) - unconditionally return
pc ← STACK[tos] + 1
tos ← tos - 1
PicoBlaze
return c (ret c) - return if the carry flag is set
if c = l
then pc ← STACK[tos] + 1
tos ← tos - 1
else pc ← pc + 1
PicoBlaze
returni disable (reti disable)
- return from interrupt service routine and keep the interrupt flag disabled
pc ← STACKC[tos]
tos ← tos - 1
i←0
c ← preserved c
z ← preserved z
returni enable (reti enable)
- return from interrupt service routine and keep the interrupt flag enabled
37
PicoBlaze
38
PicoBlaze ASSEMBLY CODE DEVELOPMENT
PicoBlaze cannot effectively support high-level programming languages and the
code is generally developed in assembly language.
Review of code development:
• segments of frequently used data and control operations
• use of a subroutine and the derivation of overall program structure
Bit manipulation
Masking a bit
Example-
code segment to set, clear, and toggle the second LSB of the S0 register:
constant SET-MASK , 02 ; mask = 0000-0010
constant CLR-MASK , FD ; mask = 1111-1101
or S0, SET-MASK ; set 2nd LSB to 1
and S0, CLR-MASK ; clear 2nd LSB to 0 39
40
PicoBlaze ASSEMBLY CODE DEVELOPMENT
Multiple-byte manipulation
•A microcontroller sometimes needs to handle wide, multiple-byte data,
such as a large counter
•Since the data width of PicoBlaze is 8 bits, processing this type of data
requires a mechanism to propagate information between two successive
instructions; PicoBlaze uses the carry flag for this purpose
•For the arithmetic instructions, there are two versions for addition and
subtraction, add and addcy instructions
•For the shift and rotate instructions, carry can be shifted into the MSB or
LSB of a register, and vice versa
41
for(i=MAX, i = 0 , i-1)
{
/*loop body statements*/ namereg S0, i ; loop index
} constant MAX, . . . ; loop boundary
load i, MAX ; load loop index
loop-body :
; code for loop body
46
PicoBlaze ASSEMBLY CODE DEVELOPMENT
Example:
Multiplication - the inputs are two 8-bit numbers in unsigned integer format and
the output is a 16-bit product.
Algorithm - simple shift-and-add method
• It iterates through 8 bits of multiplier; in each iteration, the multiplicand is
shifted left one position. If the corresponding multiplier bit is ' l ' , the shifted
multiplicand is added to the partial product.
• The multiplicand and multiplier are stored in the s3 and s4 registers. The
individual bit of multiplier is obtained by repetitively shifting s4 to the right,
which moves the LSB to the carry flag
• Instead of actually shifting the multiplicand to the left, we shift the partial
product (of 2 bytes stored in s5 and s6) to the right
47
48
PicoBlaze ASSEMBLY CODE DEVELOPMENT
PROGRAM DEVELOPMENT
1.Derive the pseudo code of the main program
2.Identify tasks in the main program and define them as subroutines.
If needed, continue refining the complex subroutines and divide
them into smaller routines
3.Determine the register and data RAM use
4.Derive assembly code for the subroutines
Step 3 is unique for assembly code development
49
call initilaization-routine
forever :
call taskl-routine
call task2-rout ine
...
call taskn-rout ine
jump forever
50
PICOBLAZE I/O INTERFACE
• Unlike a regular microcontroller, PicoBlaze has no built-in I/O peripherals
• It provides a simple generic input and output structure for an I/O interface
• I/O peripherals are customized to each application
• PicoBlaze uses the input and output instructions to transfer data between its
internal registers and I/O ports
Interface Signals:
port-id: an 8-bit signal, specifies the port address of an input or output instruction
in-port: an 8-bit signal where PicoBlaze obtains input data for an input instruction
out-port: an 8-bit signal where PicoBlaze places output data for an o/p instruction
read_strobe : a 1-bit signal, asserted in second clock cycle of an input instruction
write_strobe: a 1-bit signal, asserted in second clock cycle of an output instruction
51
•PicoBlaze instruction takes two clock cycles Timing diagram of output s0, 02
•On execution of the instruction,
• content of S0 is placed on out-port and 02 is placed on port-id for two
clock cycles
• write-strobe signal is asserted in the second clock cycle; can be used as an
enable tick to store data in an output register 52
PICOBLAZE I/O INTERFACE
Output interface
• It usually consists of a
decoding circuit and
necessary output buffers
• Decoding circuit decodes
the port_id and generates an
enable tick accordingly
• After the output instruction,
the data will be stored in the
designated buffer
57
58
PICOBLAZE I/O INTERFACE
• Interface for single-
access ports needs a
decoding circuit to
remove the retrieved data
from the buffer in the
end of an input
instruction
• The decoding circuit
decodes the port-id and
read-strobe signals and
output a “removal”
signal, which is asserted
for one clock cycle and
removes the previously
Block diagram of four single-access ports
retrieved data 59
PICOBLAZE
INTERRUPT INTERFACE
Assume : the interrupt signal is asserted in the
middle of the add s0, s3 instruction
PicoBlaze performs following steps
1. Completes execution of current instruction
2. Saves content of PC, clears the interrupt
flag, i, to zero, preserves the zero and carry
flags, and loads the PC with 3FF
3. Executes the jump isr instruction in the
3FF address
4. Performs the service routine
5. Executes the returni instruction, in which
the saved PC and flags are restored
6. Resumes the interrupted program and
executes the sub s5, 01 instruction
Interrupted flow
PICOBLAZE INTERRUPT INTERFACE