Keyboard input routine BASIC
Posted: Fri Dec 29, 2023 5:10 am
Edit - Improvements in my post of January 1st.
So, I'm writing software for the unexpanded VIC-20 as a hobby (VICE emulator). I am used to the Commodore 64 and MBASIC (CP/m) where I have plenty of memory. For me it all started with MBASIC in CP/M in the early eighties. But writing programs for the VIC-20 with only 3583 bytes free is for me an attractive challenge. I'm learning 6502-assembler and the whole VIC-20-machine with all his chips-registers. Time by time I wil submit here my VIC-20-programs. To begin with, something simple and very basic, my Input-routine, to avoid the "cursed" INPUT-statement.
Excuse me when I make grammatical errors. English is not my native language (Dutch it is, Dutchmen/-women learn English at school).
Attached you find the program on a .d64-diskette. I couldn't find out how to make a .prg-file. Furthermore a listing of the subroutine. But my VICE refuses to make a viceprint.out-file. I did my settings for the printer, and I get normal reactions on OPEN 4,4 CMD 4 LIST PRINT#4 CLOSE 4, but the file isn't created. So I did a couple screenprints in a RTF-file (you can open this in WRITE or WORD).
I love to program "neat". With many REMS and thorough structural programming. But this subroutine is "crunched". I first developed it in MBASIC (32K free) with plenty of remarks. Then I converted it for the VIC20-unexpanded. I removed the REMs and spaces. A subroutine of something more than 500 byte is the result.
Using the subroutine in your program.
It starts at line 1700 and ends at 1990. I choose these lines, because I use a fixed program-"format": 10-90 Information (REMs), 100-990 Main - subroutines: 1000-1990 Init and input 2000-2990 working up the data 3000-3990 Output 4000-4990 DATA
So you call the subroutine by GOSUB 1700. But before you do that:
Parameter-variables to be filled:
B0% Maximum length of the field
B1% Type: 0=Alphanumeric, 1=Integer (No flow-type yet)
Then GOSUB. The subroutine outputs:
B0 Entered integer-field
B0$ Entered alphanumeric field
B9% Stop-flag. User wants to exit the program where the subroutine makes part of. 0=Flag down, user wants to continue, 1=Flag up, user wants to stop.
Variables used in the subroutine:
B2$ Key pressed
B2% ASCII-value key pressed
B2 Dummyvariable for garbage collection (must be done because every key added causes a new field in the memory)
Explanation of the subroutine:
1750 Init
1760 Cursor
1770 Keypress
1780-1785 Ooops, a double! I see that now.
1790 Processing the RETURN-key
1800 Processing the BACKSPACE-key
1810 Processing the key pressed by user to put up the "Exit"-flag. Now it is the `-key, under the Esc-key of the PC. But you can choose any key you like.
1820 Determine if user is trying to type further than the length wished
1830 Keys 0-9, they are always ok.
1840 If integer, minus-sign is accepted, provided the cursor is on the position of the first character
1850 In case of integer, pressed-key-examination is ready, subroutine can continue with next key
1860 Keys A-Z, ok in case of alphanumeric (programpointer in case of integer already passed)
1870-1880 (1889). These are keys that are accepted. You can modify lines between 1870 and 1889 yourself like adding keys. But beware: If you write sequential files in your program, don't accept the , for file-variables, because the comma is a seperator in datafiles.
1890 Key-examination is ready, now goto next pressing key.
1900-1920 Backspace-key further processing.
1930 Add character to string
1940 Print character
1950 Goto next key press
1960 Terminating after Enter-key
1970 Garbage collection
Entering data with the subroutine while running the program:
In case of integer you can only input numbers and an initial minus sign. De routine doesn't care if you exceed 32767. That's why the outputfield is not an integer-variable.
You can only enter characters that are permitted in the subroutne. You cannot exceed the maximum length given in the subroutine.
Only Backspace works for the editing. The `-key immidiately ends the entering without showing the character. The programs knows that the `-key is pressed by the "Exit-flag", that is up. In your program you can take the action what you want.
Weak point is that you can break with the RUN/STOP key. You can anticipate that by putting POKE 808,100 in the initialisation of your program. But then SAVE and LIST are prohibited also. With POKE 808,112 everything goes to normal again.
Attached are the listing and the subroutine on disk. When you are interested of the CP/M Mbasic version (Commodore 128), just say it.
My next contribution will be "Programming text-adventure games on the unexpanded VIC-20". I am finishing my first adventure game for the VIC, it is a battle against bytes.
When the program is finished, you will read about it.
Kind greetings, Soloman
So, I'm writing software for the unexpanded VIC-20 as a hobby (VICE emulator). I am used to the Commodore 64 and MBASIC (CP/m) where I have plenty of memory. For me it all started with MBASIC in CP/M in the early eighties. But writing programs for the VIC-20 with only 3583 bytes free is for me an attractive challenge. I'm learning 6502-assembler and the whole VIC-20-machine with all his chips-registers. Time by time I wil submit here my VIC-20-programs. To begin with, something simple and very basic, my Input-routine, to avoid the "cursed" INPUT-statement.
Excuse me when I make grammatical errors. English is not my native language (Dutch it is, Dutchmen/-women learn English at school).
Attached you find the program on a .d64-diskette. I couldn't find out how to make a .prg-file. Furthermore a listing of the subroutine. But my VICE refuses to make a viceprint.out-file. I did my settings for the printer, and I get normal reactions on OPEN 4,4 CMD 4 LIST PRINT#4 CLOSE 4, but the file isn't created. So I did a couple screenprints in a RTF-file (you can open this in WRITE or WORD).
I love to program "neat". With many REMS and thorough structural programming. But this subroutine is "crunched". I first developed it in MBASIC (32K free) with plenty of remarks. Then I converted it for the VIC20-unexpanded. I removed the REMs and spaces. A subroutine of something more than 500 byte is the result.
Using the subroutine in your program.
It starts at line 1700 and ends at 1990. I choose these lines, because I use a fixed program-"format": 10-90 Information (REMs), 100-990 Main - subroutines: 1000-1990 Init and input 2000-2990 working up the data 3000-3990 Output 4000-4990 DATA
So you call the subroutine by GOSUB 1700. But before you do that:
Parameter-variables to be filled:
B0% Maximum length of the field
B1% Type: 0=Alphanumeric, 1=Integer (No flow-type yet)
Then GOSUB. The subroutine outputs:
B0 Entered integer-field
B0$ Entered alphanumeric field
B9% Stop-flag. User wants to exit the program where the subroutine makes part of. 0=Flag down, user wants to continue, 1=Flag up, user wants to stop.
Variables used in the subroutine:
B2$ Key pressed
B2% ASCII-value key pressed
B2 Dummyvariable for garbage collection (must be done because every key added causes a new field in the memory)
Explanation of the subroutine:
1750 Init
1760 Cursor
1770 Keypress
1780-1785 Ooops, a double! I see that now.
1790 Processing the RETURN-key
1800 Processing the BACKSPACE-key
1810 Processing the key pressed by user to put up the "Exit"-flag. Now it is the `-key, under the Esc-key of the PC. But you can choose any key you like.
1820 Determine if user is trying to type further than the length wished
1830 Keys 0-9, they are always ok.
1840 If integer, minus-sign is accepted, provided the cursor is on the position of the first character
1850 In case of integer, pressed-key-examination is ready, subroutine can continue with next key
1860 Keys A-Z, ok in case of alphanumeric (programpointer in case of integer already passed)
1870-1880 (1889). These are keys that are accepted. You can modify lines between 1870 and 1889 yourself like adding keys. But beware: If you write sequential files in your program, don't accept the , for file-variables, because the comma is a seperator in datafiles.
1890 Key-examination is ready, now goto next pressing key.
1900-1920 Backspace-key further processing.
1930 Add character to string
1940 Print character
1950 Goto next key press
1960 Terminating after Enter-key
1970 Garbage collection
Entering data with the subroutine while running the program:
In case of integer you can only input numbers and an initial minus sign. De routine doesn't care if you exceed 32767. That's why the outputfield is not an integer-variable.
You can only enter characters that are permitted in the subroutne. You cannot exceed the maximum length given in the subroutine.
Only Backspace works for the editing. The `-key immidiately ends the entering without showing the character. The programs knows that the `-key is pressed by the "Exit-flag", that is up. In your program you can take the action what you want.
Weak point is that you can break with the RUN/STOP key. You can anticipate that by putting POKE 808,100 in the initialisation of your program. But then SAVE and LIST are prohibited also. With POKE 808,112 everything goes to normal again.
Attached are the listing and the subroutine on disk. When you are interested of the CP/M Mbasic version (Commodore 128), just say it.
My next contribution will be "Programming text-adventure games on the unexpanded VIC-20". I am finishing my first adventure game for the VIC, it is a battle against bytes.
When the program is finished, you will read about it.
Kind greetings, Soloman