Skip to content

Instantly share code, notes, and snippets.

@hasecilu
Last active October 30, 2023 00:25
Show Gist options
  • Save hasecilu/5c2ffa45ed6ad67ba2e1d52ece102a3b to your computer and use it in GitHub Desktop.
Save hasecilu/5c2ffa45ed6ad67ba2e1d52ece102a3b to your computer and use it in GitHub Desktop.
PIC Assembler syntax and style for latex lstlisting package.
% This code was based on the RISC-V Assembler lstlisting syntax and style from Anton Lydike. Available at: https://gist.github.com/AntonLydike/e339c3c3a4dcab8bc3c620b3fa436cda
%
% PIC Assembler syntax and style for latex lstlisting package
% Features
% 1. The 35 instructions for the PIC16F882/883/884/886/887
% 2. All special function registers
% 3. Some directives and keywords
% 4. Easily extended for other devices
%
% Author: @hasecilu
%
% This code is in the public domain
%
% language definition
\lstdefinelanguage[PIC16F882/883/884/886/887]{Assembler}
{
alsoletter={.}, % allow dots in keywords
alsodigit={0x}, % hex numbers are numbers too!
morekeywords=[1]{ % instructions
% PIC16F882/883/884/886/887 instruction set
% BYTE-ORIENTED FILE REGISTER OPERATIONS
ADDWF, ANDWF, CLRF, CLRW, COMF, DECF, DECFSZ, INCF, INCFSZ, IORWF, MOVF, MOVWF, NOP, RLF, RRF, SUBWF, SWAPF, XORWF,
% BIT-ORIENTED FILE REGISTER OPERATIONS
BCF, BSF, BTFSC, BTFSS,
% LITERAL AND CONTROL OPERATIONS
ADDLW, ANDLW, CALL, CLRWDT, GOTO, IORLW, MOVLW, RETFIE, RETLW, RETURN, SLEEP, SUBLW, XORLW
},
morekeywords=[2]{ % sections of our code and other directives
BANKMASK, END, BANKSEL, PROCESSOR, include
},
morekeywords=[3]{ % registers
% PIC16F886/PIC16F887 SPECIAL FUNCTION REGISTERS
% BANK 0
TMR0, PCL, STATUS, FSR, PORTA, PORTB, PORTC, PORTD, PORTE, PCLATH, INTCON, PIR1, PIR2, TMR1L, TMR1H, T1CON, TMR2, T2CON, SSPBUF, SSPCON, CCPR1L, CCPR1H, CCP1CON, RCSTA, TXREG, RXREG, CCPR2L, CCPR2H, CCP2CON, ADREH, ADCON0,
% BANK 1
OPTION_REG, PCL, STATUS, FSR, TRISA, TRISB, TRISC, TRISD, TRISE, PCLATH, INTCON, PIE1, PIE2, PCON, OSCCON, OSCTUNE, SSPCON2, PR2, SSPADD, SSPSTAT, WPUB, IOCB, VRCON, TXSTA, SPBRG, SPBRGH, PWM1CON, ECCPAS, PSTRCON, ADRESL, ADCON1,
% BANK 2
TMR0, PCL, STATUS, FSR, WDTCON, PORTB, CM1CON0, CM2CON0, CM2CON1, PCLATH, INTCON, EEDAT, EEADR, EEDATH, EEADRH,
% BANK 3
OPTION_REG, PCL, STATUS, FSR, SRCON, TRISB, BAUDCTL, ANSEL, ANSELH, PCLATH, INTCON, EECON1, EECON2
},
morecomment=[l]{;}, % mark ; as line comment start
%morecomment=[l]{\#}, % as well as # (even though it is unconventional)
morestring=[b]", % mark " as string start/end
morestring=[b]' % also mark ' as string start/end
}
% usage example:
% define some basic colors
\definecolor{mauve}{rgb}{0.58,0,0.82}
\definecolor{comment}{RGB}{0,150,0}
\definecolor{instruction}{RGB}{0,0,155}
\definecolor{register}{RGB}{0,155,225}
\definecolor{identifier}{RGB}{153,0,107}
\lstset{
% basicstyle=\tiny\ttfamily, % very small code
basicstyle=\scriptsize\ttfamily, % small code
breaklines=true, % break long lines
commentstyle=\itshape\color{comment}, % comments are green
keywordstyle=[1]\color{instruction}, % instructions are blue
keywordstyle=[2]\color{blue}, % sections/other directives are orange
keywordstyle=[3]\color{register}, % registers are red
stringstyle=\color{mauve}, % strings are from the telekom
identifierstyle=\color{identifier}, % user declared addresses are teal
frame=lr, % black line on the left side of code
language=[PIC16F882/883/884/886/887]Assembler, % all code is PIC assembler
tabsize=4, % indent tabs with 4 spaces
showstringspaces=false % do not replace spaces with weird underlines
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment