EN

PIC16F84A simulator

Write assembly, step through it and watch the registers, LEDs and display

This simulator runs assembly programs for the PIC16F84A, the microcontroller that has been used for years to teach programming. It accepts MPASM syntax, counts clock cycles exactly and shows the registers and the state of every pin at all times.

Simulator

Program

Click a line number to set or clear a breakpoint. The program is saved in this browser.

Board

Pins configured as outputs (TRIS = 0) light their LED. Inputs (TRIS = 1) are push buttons you can press.

PORTA
PORTB

Special function registers

Data memory (0Ch to 4Fh)

Listing: machine code of each instruction

How to use the simulator

  1. Write your program or load an example. It is assembled when you run it, and errors are shown with their line number; click one to jump to that line.
  2. Run executes in real time for the selected crystal. At a lower speed you will see the current line highlighted in yellow.
  3. Step executes a single instruction. Step over runs the whole subroutine of a CALL at once, which is handy with delays.
  4. Registers and memory locations that change on each step are highlighted. CBLOCK variables are shown with their names.
  5. The .hex file can be burned into a real PIC16F84A with a programmer such as a PICkit.

The PIC16F84A in numbers

Program memory1024 words of 14 bits (flash)
Data RAM68 bytes (0Ch to 4Fh), plus the special function registers
Data EEPROM64 bytes
Stack8 levels, for return addresses only
Inputs and outputs13: RA0 to RA4 and RB0 to RB7 (RA4 is open drain)
Timer8-bit TMR0 with a 1:2 to 1:256 prescaler
InterruptsTMR0, RB0/INT, change on RB4 to RB7 and EEPROM write complete
ClockUp to 20 MHz; each instruction cycle is 4 clock periods (1 µs at 4 MHz)
Current per pin25 mA

Each instruction takes one word and one instruction cycle, except jumps (GOTO, CALL, returns and conditional skips that are taken), which take two. So, with a known crystal, you can work out exactly how long any piece of code takes.

Instruction set

f is a register, d the destination (W = 0 puts the result in W; F = 1, back in the register), b a bit from 0 to 7 and k an 8-bit literal, or an 11-bit address in GOTO and CALL. Cycles in brackets apply when the skip is taken.

InstructionWhat it doesSTATUS bitsCycles
ADDWF f,dW + f → dC, DC, Z1
ANDWF f,dW AND f → dZ1
CLRF f0 → fZ1
CLRW0 → WZ1
COMF f,dComplement of f → dZ1
DECF f,df − 1 → dZ1
DECFSZ f,df − 1 → d; skip next if result is 0–1 (2)
INCF f,df + 1 → dZ1
INCFSZ f,df + 1 → d; skip next if result is 0–1 (2)
IORWF f,dW OR f → dZ1
MOVF f,df → d (with d = F it only updates Z)Z1
MOVWF fW → f–1
NOPNo operation–1
RLF f,dRotate f left through CC1
RRF f,dRotate f right through CC1
SUBWF f,df − W → dC, DC, Z1
SWAPF f,dSwap the two nibbles of f → d–1
XORWF f,dW XOR f → dZ1
BCF f,bClear bit b of f–1
BSF f,bSet bit b of f–1
BTFSC f,bSkip next if bit b is 0–1 (2)
BTFSS f,bSkip next if bit b is 1–1 (2)
ADDLW kk + W → WC, DC, Z1
ANDLW kk AND W → WZ1
CALL kCall a subroutine, saving the return address on the stack–2
CLRWDTClear the watchdog timerTO, PD1
GOTO kJump to address k–2
IORLW kk OR W → WZ1
MOVLW kk → W–1
RETFIEReturn from interrupt and set GIE–2
RETLW kReturn from subroutine with k in W–2
RETURNReturn from subroutine–2
SLEEPLow power mode until an interrupt or resetTO, PD1
SUBLW kk − W → WC, DC, Z1
XORLW kk XOR W → WZ1

Memory banks: why BSF STATUS,RP0

The registers are split into two banks. Bank 0 holds PORTA, PORTB and TMR0; bank 1 holds, at the same positions, TRISA, TRISB and OPTION_REG. The RP0 bit of STATUS selects the bank: to configure the ports you set RP0 to 1, write TRIS and go back to bank 0. RAM from 0Ch to 4Fh looks the same from both banks. The BANKSEL directive does this switch for you.

In TRIS, a 1 makes the pin an input (I for Input) and a 0 an output (O for Output). At power-up, all pins are inputs.

Software delays

Without a timer, the simplest way to wait is to repeat a loop a known number of times. In the examples, the inner loop (NOP, NOP, DECFSZ and GOTO) takes 5 cycles and runs 200 times, so with a 4 MHz crystal it lasts 1 ms:

\[ t = N \cdot \frac{4}{f_{osc}} \qquad 200 \times 5 \times 1\,\mu\text{s} = 1\,\text{ms} \]

Repeated 250 times it gives 250 ms, and the espera subroutine repeats that as many times as W says. The count is not exact to the microsecond, because the load instructions and the last jump add a few cycles, but the simulator's cycle counter gives you the real duration. With another crystal the same program scales: at 20 MHz it is five times faster.

Interrupts

When an enabled interrupt occurs and GIE is 1, the PIC pushes the current address onto the stack, clears GIE and jumps to address 0004h. The interrupt routine must:

That is why programs with interrupts start with a GOTO at address 0000h: it jumps over the interrupt vector.

MPASM numbers and directives

Unless told otherwise, MPASM reads numbers as hexadecimal: MOVLW 10 loads 16, not 10. To avoid surprises, always write the format:

FormatExampleValue
DecimalD'25' · .2525
HexadecimalH'1F' · 0x1F · 1Fh31
BinaryB'00011111'31
Character'A'65

Supported directives: LIST (with R=DEC to change the radix), #include <p16F84A.inc>, __CONFIG, ORG, EQU, CBLOCK and ENDC, #define, DT, BANKSEL, RADIX and END. Also the pseudo-instructions MOVFW, TSTF, CLRC, SETC, CLRZ, SETZ, SKPZ, SKPNZ, SKPC, SKPNC, B, BZ, BNZ, BC and BNC. Register and bit names from p16F84A.inc are always available.

What it does not simulate

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

Related: dsPIC30F5013 simulator, ladder simulator, number systems, BCD to 7-segment decoder, pull-up and pull-down resistors, push button debouncing and LED resistor.