Skip to content

More Long Branch Elimination

March 2, 2021

I’ve flogged this horse out onto some pretty thin ice. The goal is to eliminate long branches, not for efficiency but because they upset the 1861 video chip timing. I came up with a scheme that eliminated the branches using a subroutine mechanism but with fairly horrendous overhead. To reduce the subroutine overhead I figured I would try dedicating a register to the branch routines which would take it out f the variable pool. So instead of a long branch being replaced by a 5 byte sequence like:
SEP 4 BRANCHER,TARGET it gets replaced by a 3 byte sequence:
SEP 7 TARGET
That’s fine for unconditional branches but for conditional branches you still need 5 bytes – say for BNZ TARGET:
PAGEFIT 6
BZ +
SEP 7 TARGET
+:
This has the added advantage that if the branch is not taken there’s no overhead at all. The BZ above would not be safe on its own so it’s protected by a new invention, the PAGEFIT nn macro which makes sure there’s nn bytes left on the page(it’s 6 rather than 5 to make sure that the instruction sequence leaves room for another byte to be the branch target).

This worked much better than the subroutine mechanism but it left the compiler with only one register for variables(R6). It occurred to me that i could maybe steal one of the temporary registers if i never used long arithmetic. This is very true. I know I need at least 3 registers so the compiler can, say, add two registers with the result in a third. If I don’t define at least three integer temps the compiler just folds and quits with a code 1. If I do define three temps but use ANY long arithmetic, the compiler actually loops! There are dragons on the thin ice!

So, at the moment, I have several levels of increasing trickiness to choose from. I don’t mind giving up long arithmetic but the setup feels fragile. The basic dedicated register thing is solid though and the pagefit macro is a useful addition. Thanks to Ted Rossin for the innovation.

The pagefit macro and long branch routine look like the following:

pagefit: macro bytesneeded	
	if ($#256)>(255-bytesneeded)
	    align 256,0xe2
	endif
	endm

;************************long branch assist routine follows*********************
  IF	LCCPX=2		;long branch assist routine
  	pagefit 10
$$RET:
	SEP	R3
$$BRSUB:
	lda	R3
	phi	memaddr
	lda	R3
	plo	R3
	ghi	memaddr
	phi	R3
	br	$$RET
  ENDIF

From → Uncategorized

Leave a Comment

Leave a comment