Shop OBEX P1 Docs P2 Docs Learn Events
SX Code Formatter for Forum Submissions — Parallax Forums

SX Code Formatter for Forum Submissions

Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
edited 2007-03-13 23:58 in General Discussion
There was a post on the Support forum that regarded posting readable SX assembly code in the forums. This is hard to do, due to the way the forum code handles tabs. So I've put a code formatter on my website that converts SX code into something that can be displayed here. The web address is: www.phipi.com/sxformat.

Here's an example of code that's gone through the formatter:

        [b]DEVICE[/b]  SX28L,[b]OSCHS3[/b],[b]BOR26[/b],[b]TURBO[/b],STACKX_OPTIONX
        [b]RESET[/b]   START           ;Begin executing at START.
;
OPTDAT  [b]EQU[/b]     %10000110       ;Option register value:
                                ;  Reg $01 is RTCC, not WREG.
                                ;  Enable RTCC roll-over interrupt.
                                ;  RTCC clocked internally.
                                ;  RTCC uses prescaler.
                                ;  RTCC divide rate = 1:32
;
        [b]FREQ[/b]    50_000_000
;
;**********************MODE REGISTER VALUE NAMES**********************          
M_CMP   [b]EQU[/b]     $08
M_WKPND [b]EQU[/b]     $09
M_WKED  [b]EQU[/b]     $0A
M_WKEN  [b]EQU[/b]     $0B
M_ST    [b]EQU[/b]     $0C
M_LVL   [b]EQU[/b]     $0D
M_PLP   [b]EQU[/b]     $0E
M_TRIS  [b]EQU[/b]     $0F
;***********************PROCESS CONSTANTS*****************************
RTCON   [b]EQU[/b]     186             ;Added to RTCC after interrupt.
;               
;****************************MACROS***********************************
BLOCK   [b]MACRO[/b]
        [b]IF[/b]      $ & $100
        [b]ERROR[/b]   'Block begins in upper half of [b]page[/b].'
        [b]ENDIF[/b]
        [b]ENDM[/b]
;
ENDB    [b]MACRO[/b]
        [b]IF[/b]      ($-1) & $100
        [b]ERROR[/b]   'Block ends in upper half of [b]page[/b].'
        [b]ENDIF[/b]
        [b]ENDM[/b]
;
NOP3    [b]MACRO[/b]
        [b]JMP[/b]     $+1
        [b]ENDM[/b]
;
        [b]NOEXPAND[/b]




Enjoy!
-Phil

Update: Added boldfacing for reserved words. 'Looks like I missed a couple, though!

Post Edited (Phil Pilgrim (PhiPi)) : 3/7/2007 6:40:40 AM GMT

Comments

  • PJMontyPJMonty Posts: 983
    edited 2007-03-07 07:08
    Phil,

    Very cool. I wonder if this is something that could be integrated into the forum software. I tried formatting my assembly language template file here:

    [b]ifdef[/b] __SASM
        [b]DEVICE[/b] [b]SX28[/b], [b]turbo[/b], [b]stackx[/b], [b]optionx[/b], [b]OSCHS3[/b]
    [b]else[/b]
        [b]DEVICE[/b] SX28L,[b]turbo[/b],stackx_optionx
    [b]endif[/b]
        [b]FREQ[/b]  50_000_000
        [b]ID[/b]      'DUMMY'   ;Program ID label
        [b]RESET[/b]   GotoMain    ;Set reset/boot address
    
    [b]ifdef[/b] __SASM
        [b]LIST[/b] Q=37;, Q=64 ; This quiets specfic SASM warnings
        [b]irc_cal[/b] [b]IRC_FAST[/b]
    [b]endif[/b]
    
    INT_DURATION  [b]equ[/b] 200 ; Number of RTCC timer counts for interrupt
    
    ; Interrupt code MUST start at address 0
        [b]org[/b] $00
    
        [b]mov[/b]     [b]w[/b], #-INT_DURATION       ;interrupt every INT_DURATION RTCC clocks
        [b]retiw[/b]                           ;exit interrupt
    
    GotoMain
        [b]jmp[/b] @Main
    
    
        [b]ORG[/b] $200
    
    Main
    
    ; Clear all file register memory in the chip
        [b]clr[/b] [b]FSR[/b]             ;Reset all ram starting at 08h
    :zero_ram
        [b]sb[/b] [b]FSR[/b].4            ;Are we on low half of bank?
        [b]setb[/b] [b]FSR[/b].3          ;If so, don't touch regs 0-7
        [b]clr[/b] [b]IND[/b]             ;Clear using indirect addressing
        [b]ijnz[/b] [b]FSR[/b],:zero_ram  ;Repeat until done
    
    
    :Loop4Ever
        [b]jmp[/b] :Loop4Ever
    
    
    



    Looks good except for the conversion of tabs. I set the tabs to "2" before pasting my code, but it looks weird here. I'm not sure if that is an issue with your code, or the way the forum software handles spaces when using FireFox.


    Thanks,
    PeterM
  • tommytommy Posts: 84
    edited 2007-03-07 08:04
    hmm - yeah - I can live with that...·· the biggest trick to a neat clean appearance is the leading spaces, not possible with the forum's copy/pasting...
    MEASURING_THE_TEMPERATURE
    ;
            [b]MOV[/b] FSR,#80                            [color=green];a count-down value[/color]
            [b]MOV[/b] IND,#16
            [b]MOV[/b] W,#0
             [b]INC[/b] FSR                              [color=green] ;fsr 81 - lsb of sixteen answers[/color] 
            [b]MOV[/b] IND,W                             [color=green] ;clears it.
    [/color]         [b]INC[/b] FSR                               [color=green];fsr 82 - msb of sixteen answers
    [/color]       [b] MOV[/b] IND,W                              [color=green];clears it.
    [/color];---
    MEASURE_LOOP                                   [color=green] ;add msb & lsb 16 times - loop
    ;---[/color]
            [b]CALL[/b]    MEASURE_W_LTC_1298             [color=green];loads [u]msb[/u] & [u]lsb[/u]
    ;[/color]
            [b]MOV[/b] FSR,#81                            [color=green];and we add them to our storage place
    [/color]          [b]ADD[/b] IND,LSB           
               [b]INC[/b] FSR                             [color=green];does [i][u]NOT[/u][/i] affect the carry flag[/color]
                [b]SNC[/b]
                [b]INC[/b] IND             [color=green];the carry     ;fsr 81 - sum of sixteen readings - lsb[/color] 
                [b]ADD[/b] IND,MSB                       [color=green] ;fsr 82 - sum of sixteen readings - msb
    ; [/color]
            [b]MOV[/b] FSR,#80                           [color=green] ;our count-down value
    [/color]        [b]DEC[/b] IND
            [b]CJNE[/b]    IND,#0,MEASURE_LOOP
    ;---
    



    · tommy
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2007-03-07 17:01
    Phil,

    Is that the same formatter that you put together for the Propeller code? ... If so, I seem to remember "early on" that you had a stand-alone version. Is that available as well?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-03-07 17:16
    Beau,

    It's a modified version: different keywords and comment character, added tab expander. The Propeller version may have started standalone for testing onsite, before it was converted to CGI. This one was adapted from the Propeller CGI, so has never existed standalone.

    PeterM,

    You have to set the "tabs" value to match whatever you use in the SXKey IDE. Otherwise your code will look different when posted. Also, results will vary if your browser doesn't show a monospaced font for code blocks. I use Opera which, under OS/X and Linux doesn't display the correct font, making code blocks look weird. Opera under WinXP works fine, though.

    -Phil
  • PJMontyPJMonty Posts: 983
    edited 2007-03-07 18:56
    Phil,

    I did set the tabs correctly. In my post, I said, "I set the tabs to "2" before pasting my code." I am referring to setting the tabs to "2" in your web applet before pasting, and a tab setting of "2" is what I use in the IDE.

    It doesn't look like FireFox uses a monospaced tab for displaying code blocks, but it's not like I have an option to change that. I do know that your code sample lines up perfectly, while mine lines up weird. Still, handy gizmo you wrote.

    Thanks,
    PeterM
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-03-07 19:44
    Peter,

    It's not just your browser. Part of the problem comes from the dotNetBB style sheet at forums.parallax.com//forums/styles/dotNetBB/css/style.css. Here's what it uses for code blocks:

    .msgCode {
      background-color  : #EEEEEE;
      color             : #000099;
      font-family       : Lucida Console, Verdana, Tahoma, Arial, Helevetica, Sans-Serif;
      font-size         : 8pt;
      cursor            : default;
      font-weight       : normal;
      padding-left      : 10px;
      padding-right     : 10px;
      padding-bottom    : 5px;
      padding-top       : 5px;
      border            : 1px inset threedshadow;
    }
    
    


    Lucinda Console is the only monospaced font listed. All the others are proportionally-spaced. If you don't have Lucinda Console installed in your system, or if your browser won't use it, you'll get a proportionally-spaced font instead.

    It would be a simple matter to fix it, I think. Just changing the font-family line to the following would do the job:

      font-family        : Parallax, ProggyCleanTT, Lucinda Console, Courier, Monospace;
    
    
    



    With this, your browser has a choice of several monospaced fonts and will use the first available font listed. If none of those exist in the system, it will default to any other "Monospace" font (the last choice), instead of a proportionally-spaced "Sans-Serif" font. This would also allow Spin programs for the Propeller to display in all their glory.

    Perhaps the IT Guy could weigh in on this possibility...

    -Phil
  • PJMontyPJMonty Posts: 983
    edited 2007-03-08 00:08
    Phil,

    At least you provided me with a workaround. I didn't have Lucinda Console installed, but I did have the font on my computer. I installed it and now the formatted code lines up beautifully. I'll post a message to the Forum guy and see if he can do something regarding your suggestion.

    Thanks,
    PeterM
  • John CoutureJohn Couture Posts: 370
    edited 2007-03-13 17:35
    In another post someone mentioned a collection of code snippets and here is a nifty code formatter. Is there a location for neat things like this in one place?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John J. Couture

    San Diego Miramar College
  • James NewtonJames Newton Posts: 329
    edited 2007-03-13 19:22
    http://www.sxlist.com would love to be a collection point for all neat stuff. Adding a link is easy, just use the little form at the bottom of each and every page.

    There is also a code colorizer at sxlist.com...

    http://www.sxlist.com/cgi-bin/colorer.exe ·but it produces actual HTML/CSS rather than BBCode.

    Phil, if you like, I could see about hosting your version as well.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james at sxlist,com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-03-13 23:26
    James,

    That'd be great, since I have a bandwidth-limited account (which, so far, hasn't been an issue). It's a Perl CGI app, though, and requires both the CGI and CGI::Carp modules. Would that fly on your site?

    Thanks,
    Phil
  • James NewtonJames Newton Posts: 329
    edited 2007-03-13 23:58
    Err... Maybe... Send it to me and I'll take a crack at it. my name at sxlist.com

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james at sxlist,com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



Sign In or Register to comment.