Edit Buffers

Introduction

An edit buffer is a place where user input is stored. It can be added to, have something inserted in the middle, have something deleted, and a whole lot more. An example of a text edit buffer would be Notepad. The data being manipulated may be very different, but the general idea is usually close to this.

Fixed-Length Buffers

Fixed length buffers are area in memory defined to hold the user input. Commonly, they are used for high score menus where users can input there name. They are fixed length because they have a defined beginning and ending point, and generally are continuous (no gaps between parts of the buffer).

Setting it up

To set up a buffer, you need to define the beginning or end. Whether you do this in code or just in your head, it doesn't really matter.

;start of our buffer is appbackupscreen, end is appbackupscreen+20
.define bufStart appbackupscreen
.define bufEnd appbackupscreen+20
bufStart:
.db 0,0,0,0,0,0,0,0,0,0
.db 0,0,0,0,0,0,0,0,0,0

We'll also need a buffer pointer. This is where the user input is being stored.

.define bpoint appbackupscreen+21
bpoint:
.db 0
ld b,0

The Code

Buffer Table

So now that we have a block of memory defined for our buffer, we need someway for user input to be stored. Since most buffers hold text inputted from users, we'll create code that allows users to press buttons corresponding with the letter. To do this, we'll use a table of values (for size and speed).

(copied parts from buffer tables)

Table:
 .db 'A'        ;9A        kCapA
 .db 'B'        ;9B        kCapB
 .db 'C'        ;9C        kCapC
 .db 'D'        ;9D        kCapD
 .db 'E'        ;9E        kCapE
 .db 'F'        ;9F        kCapF

 .db 'G'        ;A0        kCapG
 .db 'H'        ;A1        kCapH
 .db 'I'        ;A2        kCapI
 .db 'J'        ;A3        kCapJ
 .db 'K'        ;A4        kCapK
 .db 'L'        ;A5        kCapL
 .db 'M'        ;A6        kCapM
 .db 'N'        ;A7        kCapN
 .db 'O'        ;A8        kCapO
 .db 'P'        ;A9        kCapP
 .db 'Q'        ;AA        kCapQ
 .db 'R'        ;AB        kCapR
 .db 'S'        ;AC        kCapS
 .db 'T'        ;AD        kCapT
 .db 'U'        ;AE        kCapU
 .db 'V'        ;AF        kCapV

 .db 'W'        ;B0        kCapW
 .db 'X'        ;B1        kCapX
 .db 'Y'        ;B2        kCapY
 .db 'Z'        ;B3        kCapZ
 .db 'X'        ;B4        kVarX

Key presses and bound checking

That table works well with the Getkey ROM call. We'll also need a way to check for invalid keypresses. Since all the values greater than $B4 are invalid, and all the values less than $9A are also invalid, and everything else is valid, we'll use these limits as our check. We also need a way to check for special keys (keys that when pressed perform a special action, such as pressing [ENTER] to finish editing, or [CLEAR] to empty the buffer, etc.), and a limit test so we don't overwrite the buffer's bounds.

Keyloop:
 bcall(_GetKey)

 cp kEnter
 jr z,editDone

 ld c,a
 ld a,b
 cp 20
 jr nc,Keyloop
 ld a,c

 cp $B5
 jr nc,Keyloop
 cp $9A
 jr c,keyloop

Data conversion and saving

And now for a way to translate key pressed into the data we want.

ld hl,table
ld e,a
ld d,0
add hl,de
ld a,(hl)

To finish, we'll store this value into the buffer, and advance the buffer counter, and repeat.

ld hl,bufStart
ld e,b
ld d,0
add hl,de
ld (hl),a
jr keyloop

Working fixed-length buffer

And the full code:

start:
 ld b,0                        ;buffer pointer
Keyloop:
 bcall(_GetKey)

 cp kEnter                        ;special key enter
 jr z,editDone

 ld c,a                        ;check to see if buffer is full
 ld a,b
 cp 20
 jr nc,Keyloop
 ld a,c

 cp $B5                        ;check for valid input
 jr nc,Keyloop
 cp $9A
 jr c,keyloop

 ld hl,table                        ;get character
 ld e,a
 ld d,0
 add hl,de
 ld a,(hl)

 ld hl,bufStart                    ;save
 ld e,b
 ld d,0
 add hl,de
 ld (hl),a

 jr keyloop

editDone:
 ret                        ;for now, just quit

bufStart;
.db 0,0,0,0,0,0,0,0,0,0
.db 0,0,0,0,0,0,0,0,0,0
Table:
 .db 'A'        ;9A        kCapA
 .db 'B'        ;9B        kCapB
 .db 'C'        ;9C        kCapC
 .db 'D'        ;9D        kCapD
 .db 'E'        ;9E        kCapE
 .db 'F'        ;9F        kCapF

 .db 'G'        ;A0        kCapG
 .db 'H'        ;A1        kCapH
 .db 'I'        ;A2        kCapI
 .db 'J'        ;A3        kCapJ
 .db 'K'        ;A4        kCapK
 .db 'L'        ;A5        kCapL
 .db 'M'        ;A6        kCapM
 .db 'N'        ;A7        kCapN
 .db 'O'        ;A8        kCapO
 .db 'P'        ;A9        kCapP
 .db 'Q'        ;AA        kCapQ
 .db 'R'        ;AB        kCapR
 .db 'S'        ;AC        kCapS
 .db 'T'        ;AD        kCapT
 .db 'U'        ;AE        kCapU
 .db 'V'        ;AF        kCapV

 .db 'W'        ;B0        kCapW
 .db 'X'        ;B1        kCapX
 .db 'Y'        ;B2        kCapY
 .db 'Z'        ;B3        kCapZ
 .db 'X'        ;B4        kVarX

Dynamic Buffers

Opening the buffer

Writing to the buffer

Insertion/Deleting

Closing the buffer

Conclusion

Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.