631-640
Addresses 631-640 (Hex: $0277-$0280, name KEYD) is the keyboard buffer used by the Commodore KERNAL .
When a key is pressed, the new character is placed in memory at position 631+PEEK(198), then the content of address 198 is incremented to report the presence of the key.
When a key is retrieved from the buffer, the character at address 631 is read and handled. The other characters are then shifted onto the previous character, then the content of address 198 is decremented to make room for one more character.
Use for self-modifying programs[edit | edit source]
The keyboard buffer can be used to write self-modifying BASIC programs. The following example generates DATA statements corresponding to the contents of a region of memory
5 input"start address";i 10 input"end address";n 15 input"first line number";l 20 ? chr$(147);chr$(17);chr$(17);chr$(17); 25 ? l;"fori=";i;"to";n;":ready:pokei,y:next" 30 poke631,13:l=l+1:j=632 35 m=0:n=n+1:goto 45 40 ? chr$(147);chr$(17);chr$(17);chr$(17);:j=631 45 ? l;:s$="data" 50 if i+m=n then 70 60 ? s$;mid$(str$(peek(i+m)),2,3);:s$="," 65 m=m+1:if m and 15 then 50 70 ?:pokej,13:j=j+1:l=l+1:if j<640 and i+m<n then 45 80 if i+m<n then ?"i=";i;":n=";n;":l=";l;":m=";m;":goto40":pokej,13:j=j+1 90 poke 198,j-631:?chr$(19):end
RETURN keypresses are entered in the keyboard at lines 30 and 70 (after writing a line of the generated program) as well as 80 (after writing a GOTO statement that reenters the generator at line 40). Before exiting at line 90, the program moves the cursor to the top of the screen, so that the fake RETURN keypresses will execute statements from the top of the screen to the bottom.
Other uses include writing LOAD statements to chain execution from one program to the next, as well as to load data files into memory.