Context:
Game has 256 symbols ($00-ff) to map codes and font characters.

We can get $40-90 unmapped codes on average, contiguous if lucky.
Often placed with DTE.

2-byte symbols are typically seen for MTE entries.
Small dictionary entries aren't great for encoders.

; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Upgrade:
Include an 8-bit 'header' packet for every 8 bytes.

ex.
[00011111] = 3 non-DTE bytes + 5 DTE bytes

Overhead is 'ceil( script_codes / 8 )' or 12.5% increase.

Theoretical maximum is now 16 per (8+1) bytes or 16:9 = 56.25% size.

; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The advantage is that we now have 256 DTE entries ($00-ff).
On top of the original 256 alphabet.

So the unmapped region can be used for additional DTE strings.
Or MTE phrases like ' the'.

No icky Huffman bitmath or logarithmic speed penalties.
And it should approach Huffman levels.


Bad:
- Doesn't work with Atlas
 * Custom inserter or upgraded version needed

- DTE tables ($200+)
- Code overhead ($100-200)

- Packet overhead
- Not experimentally verified

- Branching opcodes
 * Use 'on/off' control flags or a 'reset' code

; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Pseudocode:

; Init

lda #$80		; 'empty' packet
sta DTE_PACKET

lda #$00		; no DTE used
sta DTE_FLAG

; -------------------------------------------------------------
; -------------------------------------------------------------

; Execute

Start_Script:
asl DTE_PACKET		; check for empty header
bne Script_Path

jsr Script_Load		; reload header
rol a			; check path (recycle old bit)
sta DTE_PACKET

; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Script_Path:
bcs Script_DTE		; 0 = Raw, 1 = DTE


Script_Raw:
jmp Script_Load

; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Script_DTE:
lda DTE_FLAG		; check 1 or 2

php
jsr Script_Load		; load DTE byte
tay
plp

bne Script_DTE_2


Script_DTE_1:
dex			; restore ptr
bcc Script_DTE_1a
dec $01

Script_DTE_1a:
lda #$80		; raise flag
sta DTE_FLAG

lda $fe00,y		; load script symbol
rts


Script_DTE_2:
lda #$00		; lower flag
sta DTE_FLAG

lda $ff00,y		; load script symbol
rts

; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Script_Load:
lda ($00),x		; load script byte

inx			; bump ptr
bcc Script_Load_2	; 8-bit overflow
inc $01

Script_Load_2:
rts
