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.
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.
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.
What the program sends through the UART appears here; what you type reaches the chosen UART at the speed set by UxBRG.
mov #__SP_init, w15 and SPLIM.| Core | 16-bit, modified Harvard architecture, up to 30 MIPS |
|---|---|
| Program memory | 66 KB of flash: 22,528 instructions of 24 bits (000000h to 00AFFEh) |
| Data RAM | 4 KB: X RAM from 0800h to 0FFFh and Y RAM from 1000h to 17FFh |
| Data EEPROM | 1 KB |
| Working registers | 16 of 16 bits: W0 to W15 (W14 frame pointer, W15 stack pointer) |
| DSP engine | 17 × 17-bit multiplier, two 40-bit accumulators (A and B), 40-bit barrel shifter, saturation and rounding |
| Interrupts | 41 sources, 7 priority levels and 4 processor traps |
| Peripherals | 5 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 |
| Package | 80 pins (TQFP); the dsPIC30F5011 is the 64-pin version |
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.
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.
| Group | Instructions | What they do |
|---|---|---|
| Data movement | MOV, MOV.B, MOV.D, SWAP, EXCH, SE, ZE, PUSH, POP, LNK, ULNK, TBLRDL, TBLRDH | Copy 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. |
| Arithmetic | ADD, ADDC, SUB, SUBB, SUBR, SUBBR, INC, INC2, DEC, DEC2, NEG, DAW.B | 8- 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 divide | MUL.SS, MUL.SU, MUL.US, MUL.UU, MUL f, DIV.S, DIV.U, DIV.SD, DIV.UD, DIVF | MUL 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. |
| Logic | AND, IOR, XOR, COM, CLR, SETM | Bitwise operations, complement, clear (CLR) and set to all ones (SETM). |
| Shifts | SL, LSR, ASR, RLC, RLNC, RRC, RRNC | One-bit shifts and rotations with or without carry, and multi-bit shifts in one go with three-operand SL, LSR and ASR. |
| Bits | BSET, BCLR, BTG, BTST, BTSTS, BSW, FBCL, FF1L, FF1R | Set, 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 skip | CP, CP0, CPB, CPSEQ, CPSNE, CPSGT, CPSLT, BTSC, BTSS | CP subtracts without storing the result and prepares a conditional BRA. CPSEQ and its siblings, BTSC and BTSS skip the next instruction. |
| Branches and subroutines | BRA, BRA cond, GOTO, CALL, RCALL, RETURN, RETLW, RETFIE | BRA branches on signed and unsigned conditions; RCALL and BRA are relative, CALL and GOTO absolute. RETLW returns with a literal in a register. |
| Hardware loops | REPEAT, DO | REPEAT 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-accumulate | MAC, MSC, MPY, MPY.N, SQR, SQRAC, ED, EDAC, MOVSAC, CLR A | They 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: accumulators | LAC, SAC, SAC.R, SFTAC, ADD A, SUB A, NEG A | Load, store (rounded with SAC.R), shift, add, subtract and negate the 40-bit accumulators. |
| Control | NOP, PWRSAV, CLRWDT, RESET, DISI | Wait, 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.
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.
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.
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:
bclr IFS0, #T1IF). Otherwise it is entered again as soon as it returns.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 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 symbol | Use |
|---|---|
| .include "p30f5013.inc" | Register and bit names (T1CON, TON, IFS0, T1IF…), always available in the simulator. |
| config __FOSC, XT_PLL4 | Configuration words. The simulator reads the oscillator mode. |
| .global __reset | Makes a name visible to the linker; required for __reset and interrupt routines. |
| .text | Code section in program memory, from 000100h. |
| .data · .bss | Variables 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, psv | Constants in program memory, readable from data space through PSV. |
| .equ NOMBRE, valor | Gives a name to a value. |
| .word · .byte · .space · .ascii · .asciz | Reserve 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_init | Start and limit of the stack, computed by the linker. |
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.