Skip to main content

Code formatter for CC65 assembly

Project description

nice65

Code formatter for CC65 assembly (WIP).

Requirements:

Features:

  • Makes ugly code less ugly
  • Fixes indentation and letter cases (mnemonics, registers)
  • Understands weird labels, such as colon-less (C64 style) and unnamed (:, :+++)
  • Support for basic macros
  • Skip files with ; nice65: ignore comment
  • Tested with C64 Kernal/Basic and my 6502-based SBC ROM code

Not implemented yet:

  • Complex macros
  • Proper formatting of arithmetic expressions
  • Better indentation of comments based on deduced context

Notes:

  • Colon-less label mode (-l) breaks macros due to ambiguity of the syntax. Use this option only with legacy code that contains no macros (such as C64 source code).

Installation

pip install nice65

Example usage

# Reformat and print to STDOUT
nice65 samples/example.s

# Modify file in-place
nice65 samples/example.s -m

# Write result to another file
nice65 samples/example.s -o samples/clean.s
# or
nice65 samples/example.s > samples/clean.s

# Recursively reformat all files in directory with extension ".s"
nice65 ./samples/ -r

# Recursively reformat all files in directory with extension ".asm"
nice65 ./samples/ -r -p '*.asm'

Before:

.macro ldax aa, bb ; do stuff
lda aa
ldx bb ; load bb
.endmacro

.macro push_all
    phA
    phX
    PHy
.endmacro

.data
foo:.byte 1

.code
         ;        Fill zeropage with zeroes
fill:
push_all
start: ldax #0, #0
@again: sta     $00   ,x  ;Yeah, we can use stz, but I just need some code to test nice65!
   inx
bne @again  ; Repeat

; Do unnecessary throwaway stuff to test expressions
lda #<($42  +  %10101010- (foo*2))
cmp foo+2
jmp :+
: lda $1234

@ridiculously_long_label_just_for_the_sake_of_it:PLX
pla

end:rts

After:

.macro  ldax aa, bb     ; do stuff
        LDA aa
        LDX bb          ; load bb
.endmacro

.macro  push_all
        PHA
        PHX
        PHY
.endmacro

.data
foo:    .byte 1

.code
; Fill zeropage with zeroes
fill:
        push_all
start:  ldax #0, #0
    @again:
        STA $00, X      ; Yeah, we can use stz, but I just need some code to test nice65!
        INX
        BNE @again      ; Repeat

; Do unnecessary throwaway stuff to test expressions
        LDA #<($42+%10101010-(foo*2))
        CMP foo+2
        JMP :+
    :   LDA $1234

    @ridiculously_long_label_just_for_the_sake_of_it:
        PLX
        PLA

end:    RTS

Using with Vim

:nnoremap <M-r> :%! nice65 -<CR>

Using with NeoVim

If you want to be fancy, here's an example on how to have nice65 configured as code formatter for NeoVim with null-ls:

  1. Make sure you have the following neovim plugins installed:

    • maxbane/vim-asm_ca65 - sets filetype for CA65 buffers
    • jose-elias-alvarez/null-ls.nvim - allows to run custom scripts as language servers
  2. Add configuration:

    local null_ls = require("null-ls")
    null_ls.setup({
        on_attach = on_attach, -- Remove this line if you don't use on_attach
    })
    null_ls.register({
        method = null_ls.methods.FORMATTING,
        filetypes = { 'asm_ca65' },
        generator = null_ls.formatter({
            command = 'nice65',
            args = {'-'},
            to_stdin = true,
            from_stdout = true,
        }),
    })
    
  3. Trigger the formatting:

    :lua vim.lsp.buf.format()
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nice65-0.1.2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

nice65-0.1.2-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file nice65-0.1.2.tar.gz.

File metadata

  • Download URL: nice65-0.1.2.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for nice65-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a50055addda5e0feeac172d4b3fbf86035926fc25786378e5a8a65359bf134c2
MD5 baa75bf52d7bf0a73b62031606087f6e
BLAKE2b-256 e80a965e81458791b413520629c911aa9cdf66961bae4b1dfa628aa90cedd402

See more details on using hashes here.

File details

Details for the file nice65-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: nice65-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for nice65-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 05a39dcceced54e0fe5c54dda0f624db7a161c317392828c2f17c2b9bb2242bd
MD5 74737042ff5e6d3c2ed56d8cf8a3010a
BLAKE2b-256 1da0c9fc51a4c935c727d55e24c58dde195c84dffc9c5c3879ce0527872e11fb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page