Skip to content

Nintendo Entertainment System · Source Code

Automatic Line-Wrap Example ASM

By KingMike

About this project

This is some source code to a automatic word-wrap hack from KingMike's Translations Shell Monsters Story translation.
It was useful as the translation had used some long names and it would have been difficult to format the line-breaks used in the original code to make good use of available window space with all possible situations.

It can be useful if a game buffers a full text window to RAM before it outputs to the screen, as this routine shifts around the text in the time between.

It was found in 2014 on a floppy disk from 2003, then rewritten to use as example code.
It is uncertain if this code is the final tested code, as KingMike tended to plan his NES ASM hacks in text documents but insert in hex, and may not have updated all changes to his code.
Still this should be good at least for concept-understanding purposes.

Original game requirements

See the release instructions.

Creator credits

KingMike
Listed creator

RELEASE 1.0

Readme & instructions

[679][16]wordwrapcommented.txt

WordWrapIndex = $E9 Position in the buffer where the text that needs wrapping is BufferRead = $EA (16-bit) Text window buffer read position for moves BufferWrite = $EC (16-bit) Text window buffer write position for moves MoveLength = $EE TextBufferIndex = $0163 Position to next insert text in buffer TextBuffer = $04A0 (64 bytes. In the original game it was 16-char x 4 lines in the translation it is (24 char + 8 overflow) x 3 lines I think the first line of a string might have been limited by 1 less character as a "stop" marker at the beginning for when the game scrolls the text window up.) This routine expects that text in the RAM buffer…

Read moreRead less
WordWrapIndex	= $E9			Position in the buffer where the text that needs wrapping is
BufferRead	= $EA	(16-bit)	Text window buffer read position for moves
BufferWrite	= $EC	(16-bit)	Text window buffer write position for moves
MoveLength	= $EE

TextBufferIndex	= $0163	Position to next insert text in buffer

TextBuffer	= $04A0	(64 bytes. In the original game it was 16-char x 4 lines
			in the translation it is (24 char + 8 overflow) x 3 lines
			I think the first line of a string might have been limited by 1 less character
			as a "stop" marker at the beginning for when the game scrolls the text window up.)

This routine expects that text in the RAM buffer will be formatted as such:

 AAAAAAAAAAAAAAAA
 AAAAAAAA........
 BBBBBBBBBBBBBBBB
 BBBBBBBB........
 CCCCCCCCCCCCCCCC
 CCCCCCCC........

 Where A, B and C are lines A, B and C on the screen. (24 char by 3 lines)

 If it isn't, then it will detect the start of the word and shift that and everything else down.

Character map:
	$FB	Toggles Auto-line break
	$FC	The original game's space code
	$FD	Do word wrap
	$FE	New space code
	$FF	End of text code

	lda MoveLength
	bne ScrollRoutine
	ldx #$00		;text buffer index 0
CheckFormats:
	lda TextBuffer,x
	cmp #$FF		;end string flag
	beq EndFormatAdjust
	cmp #$FD		;line break flag
	beq DoWordWrap
	cmp #$FB		;auto-break flag toggle
	beq UseScrollingText
	inx
	cpx #$60
	bne CheckFormats
	stx TextBufferIndex
EndFormatAdjust:
	rts

UseScrollingText:
	lda #$FE		;replace Scroll Toggle flag with hard-space
	sta TextBuffer,x
	inx
	lda MoveLength
	beq SetScrollFlagOn
	lda #$00
	sta MoveLength
	beq CheckFormats	;a=0, so this will branch always to CheckFormats, next iteration of loop
SetScrollFlagOn:
	lda #$01
	sta MoveLength
	bne CheckFormats	;a=1, so this will branch always to the the next iteration of the loop

DoWordWrap:
	lda #$FE
	sta TextBuffer,x	;replace line break with hard space
	lda TextBuffer+0x19	;check if character 25 of line 1 is hard-space already
	cmp #$FE
	beq CheckLine1B		;if it is, skip wrapping line 1
	ldx #$19
	jsr WordWrap
	beq WrapLine2
CheckLine1B:
	lda TextBuffer+0x1A
	cmp #$FE
	beq WrapLine2
	ldx #$1A
	jsr WordWrap
WrapLine2:
	lda TextBuffer+0x39
	cmp #$FE
	beq CheckLine2B
	ldx #$39
	jsr WordWrap
	beq EndWordWrap
CheckLine2B:
	lda TextBuffer+0x3A
	cmp #$FE
	beq EndWordWrap
	ldx #$3A
	jsr WordWrap
EndWordWrap:
	rts

WordWrap:
	lda TextBuffer,x
	cmp #$FE		;check if we have a hard space
	beq CheckForEOL
	dex
	bne WordWrap		;keep going back until we find the last hard-space
	beq EndFormatAdjust	;if we get to the start of the window, stop

CheckForEOL:
	txa
	pha			;backup index of space
	lda #$00
	sta MoveLength		;initialize Move Length
FindEOL:
	inx
	txa
	and #$1F
	beq MoveText		;if the text spilled over a line, then we need to break it
	lda TextBuffer,x
	cmp #$FE		;current text byte a space? Check if it's the EOL.
	beq IsItEOL
	inc MoveLength
	bne FindEOL		;since text buffer isn't 256 chars long, we can use this as branch always

IsItEOL:
	inx
	lda TextBuffer,x
	cmp #$FE
	beq MoveText		;assuming 2 spaces in a row is probably an EOL. Move all text forward MoveLength bytes
	inc MoveLength		;it wasn't, so let's assume a space and continue checking
	dex
	bne FindEOL

MoveText:
	pla			;retrieve index of space character
	sta WordWrapIndex
	and #$60		;A = start of line index is on
	clc
	adc #$C0		;convert to address in buffer
	tay			;Y = index of beginning of line with space
	ldx #$00
	lda #$FF
	sta BufferWrite		;Write to $04FF (last byte of buffer)
	sec
	sbc MoveLength
	sta BufferRead		;Read from $04FF - MoveLength
	lda #04
	sta BufferWrite+1
	sta BufferRead+1	;setting the high bytes of the pointers
MoveTextLoop:
	lda (BufferRead,x)
	sta (BufferWrite,x)
	dec BufferRead
	dec BufferWrite		;transfer string backwards
	cpy BufferRead
	bcs MoveTextLoop	;repeat until we've copied through the current line

	inc BufferRead
	lda BufferRead
	sta BufferWrite		;we're going to insert text and the current point
	lda WordWrapIndex
	clc
	adc #$A0		;low byte of address of TextBuffer
	sta BufferRead		;set read position to where the text is that needs to be word-wrapped
WriteWrapText:
	lda (BufferRead,x)
	sta (BufferWrite,x)
	lda #$FE
	sta (BufferRead,x)	;now let's delete the string
	inc BufferRead
	inc BufferWrite
	dec MoveLength
	lda MoveLength
	bne WriteWrapText
	rts

Download [679][16]wordwrapcommented.txt ↓

FROM THE COMMUNITY

Player reviews & discussion

0 reviews
No ratings yet

Played this project? Tell other players how it holds up.

Your take on Automatic Line-Wrap Example ASM

Spam has been overwhelming. To help us approve your review, sign up and introduce yourself in the Introductions forum so we know you’re not a bot.

Sign in with your Retro Replay account to rate this project and share your experience.

Sign in to review