Skip to content

Long Branch Elimination In LCC1802

February 6, 2021

The compiler output is littered with long branches. These are a problem only when using the 1861 pixie video. I had an approach that would pull out most long branches but because we’re trying to adapt some of the crosslib games to pixie targets, I needed a complete solution.

My solution was to run a python script on the compiler output running my earlier long-branch reduction process but, where the branch can’t be shortened, I replace it with a perversion based on the subroutine call mechanism. In the simplest case LBR <target> gets replaced by XBR <target> which generates “CALL XBRSUB” followed by “DW <target>”. The XBRSUB routine picks up the target address and puts it into the main program counter before returning. The more complicated case like XBZSUB either does the same if D is 0 or just increments the return address to bypass the target. These branches are bulkier – 5 bytes vs 3, but i replace most of the 3 byte branches with short ones so that’s probably not a big loss. Worse though, we’re executing probably 20-30 instructions extra for each long branch. Possibly I could shorten that by not using the standard return routine but we’ll see how bad it is.

One extra fillip, i had some long skips in the arithmetic routines, I’ve provisionally replaced them with guarded short branches as in:
LSNF
XRI 01
becomes
ALIGN 4,0xE2
BNF +
XRI 01
+:
I’m hopeful that will work but there is some odd thing that happens when you’re right near the end of a page. if it’s a problem the assembler will catch it.

xbr:	macro	target
	sep	RCALL
	dw	xbrsub
	dw	target
	endm
xbz:	macro	target
	sep	RCALL
	dw	xbzsub
	dw	target
	endm
xbnz:	macro	target
	sep	RCALL
	dw	xbnzsub
	dw	target
	endm
xbdf:	macro	target
	sep	RCALL
	dw	xbdfsub
	dw	target
	endm
xbnf:	macro	target
	sep	RCALL
	dw	xbnfsub
	dw	target
	endm

	align 32
xbrsub:
	lda	r6
	phi	memaddr
	lda	r6
	plo	r6
	ghi	memaddr
	phi	r6
	sep	rret
xbzsub:
	bz	xbrsub
nbrsub:
	inc r6
	inc r6
	sep	rret
xbnzsub:
	bnz	xbrsub
	br	nbrsub
xbdfsub:
	bdf	xbrsub
	br	nbrsub
xbnfsub:
	bnf	xbrsub
	br	nbrsub

From → Uncategorized

One Comment

Trackbacks & Pingbacks

  1. More Long Branch Elimination | olduino

Leave a comment