XXIIVV

Lin6 is a linter for 6502 assembly code.

The linter is a utility to format assembly code to be used with the asm6 assembler. Lin6 was used in the creation of Donsol, Spacetime and NesPaint.

Styleguide

;; Variables

	.enum $0000                        ; Directive comment
	pos_x                   .dsb 1     ; Variable comment
	pos_y                   .dsb 1     
	.ende  

;; Constants

	SPRITE_Y                .equ $0200 ; Constant comment
	SPRITE_XY               .equ $0203 

RESET: 
	NOP 

Forever: 
	JMP Forever

NMI: 
	RTI 

;; Routines

CheckCollision: ; Routine comment
	LDA pos_y
	CMP #$88                           ; Opcode comment
	BCC @done
	; Inline comment
	LDA #$88
	STA pos_y
@done: 
	RTS 

;; Tables

Table: 
	.db $40,$46,$4c,$52,$58,$5e,$63,$68 

;; Vectors

	.pad $FFFA 
	.dw NMI 
	.dw RESET 
	.dw 0 
	.incbin "src/sprite.chr" 
— Found a mistake? Submit an edit to lin6style.s.txt(45 lines)

Source

/* 
  Lin6
  Version 1.3
  https://wiki.xxiivv.com/lin6

  Variable names must always be lowercase.
  Variable names are always padded to tab1.
  Variable length are always padded to col25.
  Variable comments are always padded to col32.
  Constant names must always be uppercase.
  Constant names are always padded to tab1.
  Constant length are always padded to col21.
  Constant comments are always padded to col32.
  Label names must always be capitalized.
  Label names always end with :.
  Label names are always preceeded with a linebreak.
  Label comments are never padded.
  Directive names are always padded to tab1.
  Directive names are always lowercase.
  Directive comments are never padded.
  Opcode names are always uppercase.
  Opcode names are always padded to tab1.
  Opcode comments are always padded to col32.
  Inline comments are always padded to tab1.
  Spacing comments are always preceeded and followed with a linebreak.
*/

#include <stdio.h>

#define VERSION "1.3"
#define BUFLEN 512

char* OPCODES[] = {"ADC", "AND", "ASL", "BCC", "BCS", "BEQ", "BIT", "BMI",
				   "BNE", "BPL", "BRK", "BVC", "BVS", "CLC", "CLD", "CLI",
				   "CLV", "CMP", "CPX", "CPY", "DEC", "DEX", "DEY", "EOR",
				   "INC", "INX", "INY", "JMP", "JSR", "LDA", "LDX", "LDY",
				   "LSR", "NOP", "ORA", "PHA", "PHP", "PLA", "PLP", "ROL",
				   "ROR", "RTI", "RTS", "SBC", "SEC", "SED", "SEI", "STA",
				   "STX", "STY", "TAX", "TAY", "TSX", "TXA", "TXS", "TYA"};

int skipped = 0;

int
cisp(char c)
{
	return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}

int
clca(int c)
{
	return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c;
}

int
cuca(char c)
{
	return c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c;
}

int
ciuc(char c)
{
	return c == cuca(c);
}

int
cilc(char c)
{
	return c == clca(c);
}

char*
sstr(char* src, char* dest, int from, int to)
{
	int i;
	char *a = (char*)src + from, *b = (char*)dest;
	for(i = 0; i < to; i++)
		b[i] = a[i];
	dest[to] = '\0';
	return dest;
}

int
slen(char* s)
{
	int n = 0;
	while(s[n] != '\0' && s[++n])
		;
	return n;
}

int
silc(char* s)
{
	int i;
	for(i = 0; i < slen(s); i++)
		if(!cilc(s[i]))
			return 0;
	return 1;
}

int
siuc(char* s)
{
	int i;
	for(i = 0; i < slen(s); i++)
		if(!ciuc(s[i]))
			return 0;
	return 1;
}

int
cpos(char* s, char c)
{
	int i;
	for(i = 0; i < slen(s); i++)
		if(s[i] == c)
			return i;
	return -1;
}

char*
scat(char* dest, const char* src)
{
	char* ptr = dest + slen(dest);
	while(*src != '\0')
		*ptr++ = *src++;
	*ptr = '\0';
	return dest;
}

int
scmp(char* a, char* b)
{
	int i, l = slen(a);
	if(l != slen(b))
		return 0;
	for(i = 0; i < l; ++i)
		if(a[i] != b[i])
			return 0;
	return 1;
}

char*
suca(char* s)
{
	int i;
	for(i = 0; i < slen(s); i++)
		s[i] = cuca(s[i]);
	return s;
}

char*
slca(char* s)
{
	int i;
	for(i = 0; i < slen(s); i++)
		s[i] = clca(s[i]);
	return s;
}

char*
strm(char* s)
{
	char* end;
	while(cisp(*s))
		s++;
	if(*s == 0)
		return s;
	end = s + slen(s) - 1;
	while(end > s && cisp(*end))
		end--;
	end[1] = '\0';
	return s;
}

int
spos(char* a, char* b)
{
	int i, j, alen = slen(a), blen = slen(b);
	for(i = 0; i < alen; i++) {
		for(j = 0; j < blen; j++) {
			if(a[i + j] == '\0')
				return -1;
			if(a[i + j] != b[j])
				break;
			if(j == blen - 1)
				return i;
		}
	}
	return -1;
}

char*
scpy(char* src, char* dest)
{
	int i = 0;
	while((dest[i] = src[i]) != '\0')
		i++;
	return dest;
}

void
setkey(char* src, char* dst)
{
	sstr(src, dst, 0, cpos(src, ' ') > 0 ? cpos(src, ' ') : slen(src));
}

void
setval(char* src, char* dst)
{
	int step = 0, i, comment = cpos(src, ';'), len = slen(src);
	if(comment > -1)
		len = comment;
	for(i = 0; i < len; i++) {
		if(src[i] != ' ' && step == 1)
			break;
		else if(src[i] == ' ')
			step = 1;
	}
	sstr(src, dst, i, len - i);
	scpy(strm(dst), dst);
}

void
setcmt(char* src, char* dst)
{
	if(cpos(src, ';') > 0)
		sstr(src, dst, cpos(src, ';'), slen(src) - cpos(src, ';'));
	else
		scpy("", dst);
	scpy(strm(dst), dst);
}

int
islabel(char* line)
{
	int i;
	for(i = 0; i < slen(line); i++) {
		if(line[i] == ' ' || line[i] == ';')
			return 0;
		if(line[i] == ':')
			return 1;
	}
	return 0;
}

int
isopcode(char* line)
{
	int i;
	char opcode[4];
	suca(sstr(line, opcode, 0, 3));
	for(i = 0; i < 56; i++)
		if(scmp(OPCODES[i], opcode))
			return 1;
	return 0;
}

int
iscomment(char* line)
{
	return line[0] == ';';
}

int
isdirective(char* line)
{
	return line[0] == '.';
}

int
isvariable(char* line)
{
	return spos(line, ".dsb") > 0;
}

int
isconstant(char* line)
{
	return spos(line, ".equ") > 0;
}

void
dolabel(char* src)
{
	char key[BUFLEN], cmt[BUFLEN];
	setkey(src, key);
	setcmt(src, cmt);
	key[0] = cuca(key[0]);
	if(key[0] == '@' || skipped)
		printf("%s %s\n", key, cmt);
	else
		printf("\n%s %s\n", key, cmt);
	skipped = 0;
}

void
doopcode(char* src)
{
	char key[BUFLEN], val[BUFLEN], cmt[BUFLEN];
	setkey(src, key);
	suca(key);
	setval(src, val);
	setcmt(src, cmt);
	if(slen(cmt) > 1)
		printf("\t%s %-30s %s\n", key, val, cmt);
	else
		printf("\t%s %s\n", key, val);
	skipped = 0;
}

void
docomment(char* src)
{
	if(src[0] == src[1]) {
		printf("\n%s\n\n", src);
		skipped = 1;
	} else {
		printf("\t%s\n", src);
		skipped = 0;
	}
}

void
dodirective(char* src)
{
	char key[BUFLEN], val[BUFLEN], cmt[BUFLEN];
	setkey(src, key);
	slca(key);
	setval(src, val);
	setcmt(src, cmt);
	if(slen(cmt) > 1)
		printf("\t%s %s %s\n", key, val, cmt);
	else
		printf("\t%s %s\n", key, val);
	skipped = 0;
}

void
dovariable(char* src)
{
	char key[BUFLEN], val[BUFLEN], cmt[BUFLEN];
	setkey(src, key);
	slca(key);
	setval(src, val);
	setcmt(src, cmt);
	if(slen(cmt) > 1)
		printf("\t%-23s %-10s %s\n", key, val, cmt);
	else
		printf("\t%-23s %s\n", key, val);
	skipped = 0;
}

void
doconstant(char* src)
{
	char key[BUFLEN], val[BUFLEN], cmt[BUFLEN];
	setkey(src, key);
	suca(key);
	setval(src, val);
	setcmt(src, cmt);
	printf("\t%-23s %-10s %s\n", key, val, cmt);
	skipped = 0;
}

void
lint(FILE* f)
{
	char line[BUFLEN], trimed[BUFLEN];
	while(fgets(line, BUFLEN, f)) {
		scpy(strm(line), trimed);
		if(slen(trimed) < 3)
			continue;
		if(islabel(trimed))
			dolabel(trimed);
		else if(isopcode(trimed))
			doopcode(trimed);
		else if(iscomment(trimed))
			docomment(trimed);
		else if(isdirective(trimed))
			dodirective(trimed);
		else if(isvariable(trimed))
			dovariable(trimed);
		else if(isconstant(trimed))
			doconstant(trimed);
		else
			printf("%s\n", line);
	}
	fclose(f);
}

int
version(void)
{
	puts("Lin6 " VERSION);
	return 0;
}

int
help(void)
{
	version();
	puts("");
	puts("A tool to format 6502 assembly code.\n");
	puts("Usage:  Lin6 [-options] [<file> ...]\n");
	puts("\t-?\tShow this help");
	puts("\t-v\tShow version");
	puts("See README for more info.\n");
	return 0;
}

int
main(int argc, char* argv[])
{
	FILE* f;
	int i;
	if(argc < 2)
		return help();
	for(i = 1; i < argc; i++) {
		if(argv[i][1] == '?')
			return help();
		else if(argv[i][1] == 'v')
			return version();
		else {
			f = fopen(argv[i], "r");
			if(f != NULL)
				lint(f);
		}
	}
	return 0;
}
— Found a mistake? Submit an edit to lin6.c.txt(433 lines)

incoming(3): nespaint donsol famicom spacetime 6502

Last update on 20T01, edited 5 times. +15/27fh-----+