EN

dsPIC30F5013 simulator

ASM30 assembler, DSP engine, prioritised interrupts, ADC, PWM and UART in the browser

This simulator runs assembly programs for the dsPIC30F5013, a 16-bit digital signal controller from Microchip: a microcontroller with a DSP engine that multiplies and accumulates in a single cycle. It accepts the syntax of the MPLAB ASM30 assembler, counts cycles and shows the registers, accumulators, peripherals and the state of every pin.

Simulator

Program

Click a line number to set or clear a breakpoint. The program is saved in this browser. If the program has «config __FOSC» with XT_PLL4, XT_PLL8 or XT_PLL16, the multiplier is set automatically.

Working registers and accumulators

Board

Output pins (TRIS = 0) light their LED; input pins (TRIS = 1) are push buttons. PORTB pins set as analogue in ADPCFG (bit at 0) become potentiometers for the ADC. On RD0–RD7, with an output compare in PWM mode, the LED brightness follows the duty cycle.

PORTB
PORTD
PORTF
PORTA
PORTC
PORTG

Serial terminal

What the program sends through the UART appears here; what you type reaches the chosen UART at the speed set by UxBRG.


        

Special function registers

Variables

Data memory

Listing: address and size of each instruction

How to use the simulator

  1. Write the program or load an example. It is assembled when you run it and errors are listed with their line number; click one to go to that line.
  2. The program must have the label __reset (declared with .global): the linker puts a jump to it at address 0. The first thing to do is set up the stack: mov #__SP_init, w15 and SPLIM.
  3. Run executes it in real time according to the crystal and the PLL. At a lower speed you will see the executing line highlighted in yellow. Step over runs a CALL or RCALL in one go.
  4. Interrupt routines are named as in ASM30, for example __T1Interrupt or __ADCInterrupt, and the simulator places them in the vector table.
  5. Registers, variables and memory locations that change at each step are highlighted. Labels in data sections are shown as variables with their decimal and Q15 values.

The dsPIC30F5013 in figures

Core16-bit, modified Harvard architecture, up to 30 MIPS
Program memory66 KB of flash: 22,528 instructions of 24 bits (000000h to 00AFFEh)
Data RAM4 KB: X RAM from 0800h to 0FFFh and Y RAM from 1000h to 17FFh
Data EEPROM1 KB
Working registers16 of 16 bits: W0 to W15 (W14 frame pointer, W15 stack pointer)
DSP engine17 × 17-bit multiplier, two 40-bit accumulators (A and B), 40-bit barrel shifter, saturation and rounding
Interrupts41 sources, 7 priority levels and 4 processor traps
Peripherals5 16-bit timers (two 32-bit pairs), 8 input capture and 8 output compare/PWM, 12-bit 200 ksps ADC with 16 inputs, 2 UARTs, 2 SPI, I²C, 2 CAN and DCI for audio codecs
Package80 pins (TQFP); the dsPIC30F5011 is the 64-pin version

Architecture

Everything goes through the 16 W registers. Three-operand instructions such as add w1, w2, w3 read two and write a third, and almost any operand can be an indirect access: [w1], [w1++], [--w1], [w1+w2] or [w1+8]. W0 also acts as the accumulator in file register (f) instructions: inc counter adds 1 in memory and inc counter, WREG leaves the result in W0.

Data memory is byte-addressed, but 16-bit words must be at even addresses: reading a word at an odd address causes the address error trap. Special function registers take 0000h to 07FFh (W0–W15 themselves are at 0000h–001Fh) and RAM goes from 0800h to 17FFh, split into X RAM and Y RAM so that the DSP engine can read two operands at once.

The stack grows upwards with W15: CALL saves the return address in two words and PUSH/POP save registers. If W15 goes past SPLIM, the stack trap fires. The upper half of data space (8000h to FFFFh) can show program memory (PSV), so strings and tables stored in flash are read with a simple mov.b [w1++], w0.

Instruction set

Almost every instruction takes one instruction cycle (Tcy = 4 / Fosc). Taken branches take 2, CALL and GOTO 2, RETURN and RETFIE 3, and those that skip the next instruction 1, 2 or 3 depending on the case. With the .b suffix they work on bytes and with .w (or nothing) on words.

GroupInstructionsWhat they do
Data movementMOV, MOV.B, MOV.D, SWAP, EXCH, SE, ZE, PUSH, POP, LNK, ULNK, TBLRDL, TBLRDHCopy between registers and memory with any addressing mode; MOV.D moves 32 bits, SE and ZE extend a byte with or without sign, LNK and ULNK build a function's stack frame and TBLRD reads program memory.
ArithmeticADD, ADDC, SUB, SUBB, SUBR, SUBBR, INC, INC2, DEC, DEC2, NEG, DAW.B8- or 16-bit additions and subtractions with and without carry; SUBR subtracts the other way round. ADDC and SUBB chain 32-bit operations.
Multiply and divideMUL.SS, MUL.SU, MUL.US, MUL.UU, MUL f, DIV.S, DIV.U, DIV.SD, DIV.UD, DIVFMUL gives a 32-bit result in one cycle, signed or unsigned for each operand. DIV divides 16/16 or 32/16 and leaves the quotient in W0 and the remainder in W1; it needs REPEAT #17 in front.
LogicAND, IOR, XOR, COM, CLR, SETMBitwise operations, complement, clear (CLR) and set to all ones (SETM).
ShiftsSL, LSR, ASR, RLC, RLNC, RRC, RRNCOne-bit shifts and rotations with or without carry, and multi-bit shifts in one go with three-operand SL, LSR and ASR.
BitsBSET, BCLR, BTG, BTST, BTSTS, BSW, FBCL, FF1L, FF1RSet, clear, toggle and test a bit in a register or in memory; FBCL, FF1L and FF1R find the first 1 or the first sign change.
Compare and skipCP, CP0, CPB, CPSEQ, CPSNE, CPSGT, CPSLT, BTSC, BTSSCP subtracts without storing the result and prepares a conditional BRA. CPSEQ and its siblings, BTSC and BTSS skip the next instruction.
Branches and subroutinesBRA, BRA cond, GOTO, CALL, RCALL, RETURN, RETLW, RETFIEBRA branches on signed and unsigned conditions; RCALL and BRA are relative, CALL and GOTO absolute. RETLW returns with a literal in a register.
Hardware loopsREPEAT, DOREPEAT repeats the next instruction up to 16,384 times at no extra cost. DO repeats a block of instructions the given number of times without counting in software.
DSP: multiply-accumulateMAC, MSC, MPY, MPY.N, SQR, SQRAC, ED, EDAC, MOVSAC, CLR AThey multiply two of W4–W7 and add, subtract or load the product into accumulator A or B, while reading the next two operands from X RAM (W8, W9) and Y RAM (W10, W11).
DSP: accumulatorsLAC, SAC, SAC.R, SFTAC, ADD A, SUB A, NEG ALoad, store (rounded with SAC.R), shift, add, subtract and negate the 40-bit accumulators.
ControlNOP, PWRSAV, CLRWDT, RESET, DISIWait, sleep or idle (PWRSAV), reset, clear the watchdog and hold off interrupts for a few cycles (DISI).

BRA conditions: Z, NZ, C, NC, N, NN, OV and NOV look at a single bit; GT, GE, LT and LE compare signed values and GTU, GEU, LTU and LEU unsigned ones, after a CP; OA, OB, SA and SB check accumulator overflow and saturation.

The DSP engine

Digital filters and most signal algorithms are sums of products. The dsPIC works with Q15 fractional numbers: a 16-bit integer n stands for n/32768, between −1 and almost +1. So 4000h is 0.5 and C000h is −0.5. When two Q15 numbers are multiplied, the product is shifted by one bit to end up as Q31 in the accumulator.

\[ x_{Q15} = \frac{n}{32768} \qquad y = \sum_{k=0}^{N-1} h_k\, x_k \]

A single instruction mac w4*w5, A, [w8]+=2, w4, [w10]+=2, w5 multiplies W4 by W5, adds the product to accumulator A and at the same time loads the next two samples from X and Y RAM into W4 and W5 and advances the pointers. With repeat in front, an N-tap FIR filter takes about N cycles.

The accumulators have 40 bits: 32 for the result and 8 guard bits, so intermediate sums can go beyond ±1 without losing anything. With SATA or SATB in CORCON they saturate instead of overflowing, and sac.r A, w0 stores the result rounded to 16 bits (convergent rounding by default). The «Dot product with MAC» example computes a weighted average of 8 samples.

Interrupts and traps

Each source has a flag in IFSx, an enable in IECx and a priority from 0 to 7 in IPCx (4 at reset). An interrupt is serviced if its priority is higher than the CPU's (IPL bits in SR). On entry, the dsPIC saves the address and the low byte of SR on the stack, raises its priority to that of the interrupt and jumps to the routine. The routine must:

A higher-priority interrupt can interrupt a lower-priority one (nesting), unless NSTDIS is set in INTCON1. Traps (address error, stack error and math error, such as dividing by zero) have priority 8 to 15 and cannot be disabled; if the program has no __AddressError, __StackError or __MathError, the simulator stops and explains the cause.

ASM30 syntax

ASM30 is Microchip's GNU assembler for dsPIC30F/33F and PIC24. Numbers are decimal unless they carry 0x (hexadecimal) or 0b (binary), literals take # and comments start with a semicolon. Labels end with a colon and are case-sensitive.

Directive or symbolUse
.include "p30f5013.inc"Register and bit names (T1CON, TON, IFS0, T1IF…), always available in the simulator.
config __FOSC, XT_PLL4Configuration words. The simulator reads the oscillator mode.
.global __resetMakes a name visible to the linker; required for __reset and interrupt routines.
.textCode section in program memory, from 000100h.
.data · .bssVariables in X RAM (.bss without an initial value, .data with one).
.section .xbss, bss, xmemory
.section .ybss, bss, ymemory
Variables in X RAM or Y RAM, to read them in parallel with MAC.
.section .const, psvConstants in program memory, readable from data space through PSV.
.equ NOMBRE, valorGives a name to a value.
.word · .byte · .space · .ascii · .ascizReserve memory or fill it with words, bytes or text (.asciz adds a 0 at the end).
#psvoffset(x) · #psvpage(x)
#tbloffset(x) · #tblpage(x)
Address and page of data in program memory, for PSVPAG and TBLPAG.
__SP_init · __SPLIM_initStart and limit of the stack, computed by the linker.

What it does not simulate

Educational simulator by Winding Tools, not affiliated with Microchip. dsPIC, PIC, MPLAB and ASM30 are trademarks of Microchip Technology.

Related: PIC16F84A simulator, ladder simulator, UART and baud rate, PWM calculator, ADC and DAC conversion, active filters, number systems.