MPASMx to PIC-AS Porting Insights

Some information on porting from MPASPM to PIC-AS.


What is MPASM ?

MPASMx - v5.87 is the last published version of MPASM.

The last chip I could compile was the 18FxxQ43. So, any chip released after this (Q10s, Q41s etc) will not work.

The software is Windows 32bit. It should therefore work on a Windows 64bit OS with the 32bit support.

A standalone installer for MPASMx - v5.87 can be found here: https://sourceforge.net/projects/gcbasic/files/Support Files/MicrochipCompilers/

What is PIC-AS ?

PIC-AS - v2.32 is the latest version of the PIC-AS assembler driver. This is a standalone driver and you do not need MPLAB-X to assemble your source code.

A standalone installer for PIC-AS- v2.32 can be found here: https://sourceforge.net/projects/gcbasic/files/Support Files/MicrochipCompilers/

My insights

I have completed an extensive project to ensure Great Cow BASIC compiler supports PIC-AS (for LGT/AVR and PICs).

This post covers the PIC port from MPASM to PIC-AS.


Context of Project

Within Great Cow BASIC ( a compiler for LGT/AVR and PICs) we use ABSOLUTE addressing and these insight therefore apply to ABSOLUTE addressing. But, most of these insights are useful in any mode of addressing.

Summary

You can port an existing ASM to PIC-AS.

An automated tool is very practical as these insights are a set of rules that could be digitised.


Insights

1. Source file: The source file has the extension of `.S`. The .S implies the PIC-AS driver uses the -xassembler-with-cpp option. The assembly source code will therefore be preprocessed.

The format of .S source file strict.

A huge issue is the placement of the #include <xc.inc> statement. This state MUST come after all the CONFIG statement. This is order is strict and published as such by Microchip.

2. Case sensitivity: Everything, in the .S source, is case sensitive and you cannot disable case sensitivity.

For the Great Cow BASIC compiler we validate everything by completing a reverse lookup into the PIC-AS source/setup files, and, we use UPPERCASE for the .S source file except of explicit CONFIG items. This approach ensures the registers are correct, however, for register.bits there is a totally new naming convention. So, for register.bit the reverse lookup time overhead is a little too long so we simply use the constant value. –The source still shows the register.bit string name.

Recommend. Using uppercase throughout except for the config items that require case sensitive entries.

The reverse lookup PIC-AS files are very easy to determine. The following is a pic16f877a example. Note the PIC-AS version number and the chipname.

Code:
Code:
; Reverse lookup file(s)
; C:\Program Files\Microchip\xc8\v2.32\pic\include\proc\pic16f877a.inc
; C:\Program Files\Microchip\xc8\v2.32\pic\dat\cfgmap\16f877a.cfgmap
Recommend. Use these file to validate the registers and register.bits.
3. Missing stuff: Depending on your chip architecture certain directives are missing, but, you can put them back to make existing code work. Examples are BANKED, ACCESS, UPPER ...

Add the following code segment to an 18f solution to resolve.

Code:

Code:
;Explicit PIC-AS constants to resolve the crazyness of the PIC-AS syntax
;These are therefore the same as MPASM
#define BANKED b
#define ACCESS a
#define UPPER low highword
Recommend. Just add the code segment shown above.

4. Config: The config definition is strict. You just need to have the configs one per line. As config names are strict - we complete a reverse lookup to ensure the case is correct.

5. PSECTs: Absolute mode is supported and works well. The set of pages using a directive is simple ... once you have it documented. See PSECT details below to specify the absolute mode.

6. Memory: With absolute addressing the memory management for our compiler implementation is easier than using the PIC-AS memory management.

We use the EQU method - make the variable name global (for debugging).

Code:

Code:
;Set aside RAM memory locations for variables. All variables are global.
GLOBAL    DELAYTEMP
 DELAYTEMP                        EQU 112                    ; 0X70
GLOBAL    DELAYTEMP2
 DELAYTEMP2                       EQU 113                    ; 0X71
GLOBAL    SYSWAITTEMPMS
 SYSWAITTEMPMS                    EQU 114                    ; 0X72
GLOBAL    SYSWAITTEMPMS_H
 SYSWAITTEMPMS_H                  EQU 115                    ; 0X73
...
....
The memory usage reports from PIC-AS are not very good and EQUs are not reported in the log but you can verify in the LST file.


6. FCALL and LJMP: The PIC-AS driver is smart. Too smart. It will reject CALL and GOTOs to another page. There is NO level of magic coding (by you) to avoid not using FCALL and LJMP will succeed. These two instructions are automatically expanded by PICAS (into three instructions). You will have to change to FCALL and LJMP.

7. ALIGN: Specific to the 18F you must ensure your code is aligned correctly. See the 18f section below for details.

8. Bugs: A number of bugs existing in the 10f, 12f and 16f PIC-AS solution. See the 10f, 12f and 16fs section below for details.

I understand this is a huge subject but this information has not been published before and these insights are intended to help you.

I can post examples for specific chips if this helps you.

Enjoy

Evan




PIC-AS Command Line

This is the command line we use:

-mcpu=%ChipModel% "%Fn_NoExt%.S" -msummary=-mem,+psect,-class,-hex,-file,-sha1,-sha256,-xml,-xmlfull -Wl -mcallgraph=std -mno-download-hex -o"%Fn_NoExt%.hex" -Wl,-Map="%Fn_NoExt%.map" -Wa,-a

Replace %ChipModel% with the chipname
Replace "%Fn_NoExt%.*" with the location of your .S source file.
Replace "%Fn_NoExt%.hex with your filename

-msummary states the reporting
-Wa,-a is the LST file. Very important.




18F specific Insights

This sections is specific to 18F chips. This covers PSECTS and Alignment of code.


PSECTs

PSECTS - short for program sections - are containers that group and hold related parts of the program, even though the source code for these parts might not be physically adjacent in the source file, or may even be spread over several modules.

A number of parameters are mandated. For an 18F use `delta = 1, abs` and, you need to add the RESETVEC. As follows:

Code:

Code:
;Explicit PIC-AS constants to resolve the crazyness of the PIC-AS syntax
;These are therefore the same as MPASM
#define BANKED b
#define ACCESS a
#define UPPER low highword

PSECT   RESETVEC,delta=1, abs
RESETVEC:

…
lots of code
…

end bit of code

;
; Declare Power-On-Reset entry point
;
END     RESETVEC
Alignment

Unlike MPASMx the alignment of instructions is NOT automatic. You need to ensure an instruction is located in the address space correctly. You need to add ALIGN 2 when appropriate. Typically, you need to add after table data (as they may be an odd number of bytes and this WILL cause misalignment.

Example. In this example the ALIGN 2 above ;SOURCE: PICAS.H (7) ensure the MOVLW 2 is in the correct address space.

Code:
Code:
GLOBAL  STRINGTABLE5
STRINGTABLE5:
  DB  2,45,45

  ALIGN 2;X3

GLOBAL  STRINGTABLE6
STRINGTABLE6:
  DB  33,69,110,100,32,111,102,32,69,69,80,114,111,109,32,80,114,111,103,114,97,109
  DB  32,97,110,100,32,68,105,115,112,108,97,121

  ALIGN 2;X3

;********************************************************************************

;SOURCE: PICAS.H (7)
GLOBAL  FN__HEXPICAS
FN__HEXPICAS:
;_HexPICAS(0) = 2
  MOVLW 2
  BANKSEL SYS_HEXPICAS_0
  MOVWF SYS_HEXPICAS_0,BANKED
Midrange 10f, 12f and 16f specifics

This sections is specific to 10f, 12f and 16f chips. This covers PSECTS and bugs.


PSECTS


The delta = 2, abs mode and you need to add the RESETVEC. As follows:

PSET per page (where a PSECT is added in the chip has that specific page).



Code:
Code:
PSECT   PROGMEM0,delta=2, abs
RESETVEC:
;VECTORS
    ORG    0
    PAGESEL    BASPROGRAMSTART
    GOTO    BASPROGRAMSTART
    ORG    4
    RETFIE

;********************************************************************************
lots of code

;START OF PROGRAM MEMORY PAGE 1
    PSECT    PROGMEM1,CLASS=CODE,SPACE=SPACE_CODE,DELTA=2, ABS, OVRLD
    ORG    2048



…
lots of code
…


;START OF PROGRAM MEMORY PAGE 2
    PSECT    PROGMEM2,CLASS=CODE,SPACE=SPACE_CODE,DELTA=2, ABS, OVRLD
    ORG    4096

…
lots of code
…

;START OF PROGRAM MEMORY PAGE 3
    PSECT    PROGMEM3,CLASS=CODE,SPACE=SPACE_CODE,DELTA=2, ABS, OVRLD
    ORG    6144

…
lots of code
…


;
; Declare Power-On-Reset entry point
;
END     RESETVEC
Bugs

The v2.32 PIC-AS driver has some horrid bugs. The main mid-range bug is the addition of extra instructions (in error). These extra instructions WILL cause your code NOT to fit on a page. You will get page overruns. This is slated for fixing in v2.35

The error report is wrong when the PSECT delta=2. Just divided the memory address that has the error by 2. Annoying.

BANKISEL

BANKISEL is not supported. BANKISEL is used on some chip for indirect addredding. So, you can add a relatively simple macro to restore BANKISEL.

Add this macro if required.

Code:
BANKISEL MACRO reg
  ;BANKISEL macro to restore missing Directive.
  if reg < 0x100 
   ;MESSG "Bankvalue < 256 - bcf"
   bcf STATUS, 7
  else
   ;MESSG "Bankvalue > 256 - bsf"
   bsf STATUS, 7
  endif
ENDM
Debugging

My advice. Add GLOBAL to all labels and variable definitions. This makes debugging a lot easier.

----

:)
Top