Is is possible for INPUT to have a default value?

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
User avatar
mrtinb
Posts: 1911
Joined: Fri Nov 06, 2015 5:44 pm
Location: Denmark
Contact:

Re: Is is possible for INPUT to have a default value?

Post by mrtinb »

alowe wrote: Wed Apr 12, 2023 2:24 pmAdded a couple more changes and ran out of memory with 48k. :oops: So now have to retype the whole program again after enabling RAM in 8k-16k and figuring out how to use that again.
If you write your program in Basic, and run out of memory, then enabling RAM in 8-16k won't help. Basic does not use RAM in 8-16k.
Martin
https://zx.rtin.be
ZX81, Lambda 8300, Commodore 64, Mac G4 Cube
User avatar
1024MAK
Posts: 5118
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Is is possible for INPUT to have a default value?

Post by 1024MAK »

This is a very simple line editor. It does not have a cursor and the only editing keys are RUBOUT and NEWLINE.

For it to run quickly, it needs to be at the very start of the BASIC program. So replace line 10 with 10 GOTO 100, add 90 RETURN, then call it using GOSUB 20. And put your program after it starting from line 100 onwards.

Code: Select all

10 LET A$=“B=10*2”
20 PRINT AT 10,0;A$;“ ”;
30 LET I$=INKEY$
40 IF I$=“” THEN GOTO 30
50 IF I$<=“Z” THEN LET A$=A$+I$
60 LET I=CODE I$
70 IF I=119 THEN IF LEN A$>0 THEN LET A$=A$( TO LEN A$-1)
80 IF I<>118 THEN GOTO 20
Program listing
Program listing
Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
User avatar
SafePit
Posts: 45
Joined: Mon Feb 20, 2012 7:06 pm
Location: Boise, ID
Contact:

Re: Is is possible for INPUT to have a default value?

Post by SafePit »

alowe wrote: Wed Apr 12, 2023 2:24 pm Yes, I already did this but it was so slow even after optimising it as far as possible. I managed to write a really elegant solution but the problem was that printing the output after each keypress took 90% of the execution time. It made the program unusable so I tried it with INPUT instead and that fixed the problem. I suppose I could figure it out all again (deleted all the notes, duh). It took about a week to perfect it as I work 16 hours a day and barely get the time. Maybe poking the D_FILE directly would speed things up.
Do you use just letters/numbers and math symbols? I found an elegant way to display and edit a line in BASIC. I use a single string and track cursor position so you can move and delete/add within the edit. Works pretty well--but no keywords. Does allow inverted letters using graphics mode. I store the string being edited in a single variable and just print using array slicing. Not as fast as INPUT, but not horrible to edit either and not a ton of code.
alowe wrote: Wed Apr 12, 2023 2:24 pm Just spent 6 hours and 30 minutes retyping the program today after discovering that the printer had somehow become disconnected . :o On EightyOne when you connect the printer it wipes your program. Added a couple more changes and ran out of memory with 48k. :oops: So now have to retype the whole program again after enabling RAM in 8k-16k and figuring out how to use that again.
Oh, goodness. No fun. Yeah, don't change hardware wettings. I usually save between runs just in case. :) I have PTSD from typing in programs in the 80's and them not saving/crashing/disappearing. Although I will say the program is usually better the second time around. LOL.
Image
Image
User avatar
SafePit
Posts: 45
Joined: Mon Feb 20, 2012 7:06 pm
Location: Boise, ID
Contact:

Re: Is is possible for INPUT to have a default value?

Post by SafePit »

I threw this together with examples. Input code is line 1000. Forgot to put a maximum line and needs optimized. I wouldn't touch type with this, but speed is bearable.

Code: Select all

  10 REM **DEF-INPUT**SLR/2023**
  20 REM **SET A DEFAULT VALUE**
  30 GOSUB 750
  40 PRINT "WHAT IS YOUR FAVORITE COLOR?"
  50 GOSUB 1000
  60 PRINT AT 1,0;"YOU SAID: ";D$
  70 PAUSE 4E4
  80 CLS
  90 RUN
 500 SAVE "DEFI#b3"
 510 RUN
 750 REM **RANDOM DEFAULT COLOR**
 760 RAND
 770 LET L=INT (RND*10)
 780 LET D$="BLUE"
 790 IF L=1 THEN LET D$="RED"
 800 IF L=2 THEN LET D$="GREEN"
 810 IF L=3 THEN LET D$="YELLOW"
 820 IF L=4 THEN LET D$="PINK"
 830 IF L=5 THEN LET D$="ORANGE"
 840 IF L=6 THEN LET D$="BLACK"
 850 IF L=7 THEN LET D$="BROWN"
 860 IF L=8 THEN LET D$="WHITE"
 870 IF L=9 THEN LET D$="PURPLE"
 880 RETURN
1000 REM **STRING INPUT**
1010 LET POS=LEN D$
1015 LET G=0
1020 PRINT AT 21,0;D$( TO POS);"#b1#ac"(G+1);D$(POS+1 TO );" "
1025 REM IF POS<LEN D$ THEN PRINT D$(POS+1 TO )
1030 LET K$=INKEY$
1040 IF K$="" THEN GOTO 1020
1045 LET K=CODE K$
1050 IF K=119 THEN GOSUB 1200
1060 IF K=114 THEN LET POS=POS-(POS>0)
1070 IF K=115 THEN LET POS=POS+(POS<LEN D$)
1075 IF K=116 THEN LET G=NOT G
1080 IF K>11 AND K<64 THEN GOSUB 1230
1090 IF K<>118 THEN GOTO 1020
1100 IF G=0 THEN GOTO 1130
1110 LET G=0
1120 GOTO 1030
1130 PRINT AT 21,0;"                                ";
1140 RETURN
1200 IF POS<1 THEN RETURN
1205 LET D$=D$( TO POS-1)+D$(POS+1 TO )
1210 LET POS=POS-1
1220 RETURN
1230 IF G=1 THEN LET K=K+128
1240 LET D$=D$( TO POS)+CHR$ K+D$(POS+1 TO )
1250 LET POS=POS+1
1260 RETURN
Image
Image
alowe
Posts: 19
Joined: Sun Aug 26, 2012 2:58 pm

Re: Is is possible for INPUT to have a default value?

Post by alowe »

mrtinb wrote: Wed Apr 12, 2023 2:50 pm
alowe wrote: Wed Apr 12, 2023 2:24 pmAdded a couple more changes and ran out of memory with 48k. :oops: So now have to retype the whole program again after enabling RAM in 8k-16k and figuring out how to use that again.
If you write your program in Basic, and run out of memory, then enabling RAM in 8-16k won't help. Basic does not use RAM in 8-16k.
Actually, I rewrote parts of the program to use that RAM, so it is possible, if a bit tedious. But then figured out a better solution so that the extra memory wasn't necessary.
alowe
Posts: 19
Joined: Sun Aug 26, 2012 2:58 pm

Re: Is is possible for INPUT to have a default value?

Post by alowe »

SafePit wrote: Fri Apr 14, 2023 3:00 am
alowe wrote: Wed Apr 12, 2023 2:24 pm Yes, I already did this but it was so slow even after optimising it as far as possible. I managed to write a really elegant solution but the problem was that printing the output after each keypress took 90% of the execution time. It made the program unusable so I tried it with INPUT instead and that fixed the problem. I suppose I could figure it out all again (deleted all the notes, duh). It took about a week to perfect it as I work 16 hours a day and barely get the time. Maybe poking the D_FILE directly would speed things up.
Yeah, don't change hardware wettings. I usually save between runs just in case. :) I have PTSD from typing in programs in the 80's and them not saving/crashing/disappearing. Although I will say the program is usually better the second time around. LOL.
Oh, I don't change the settings. The emulator does that for me randomly. :lol:
alowe
Posts: 19
Joined: Sun Aug 26, 2012 2:58 pm

Re: Is is possible for INPUT to have a default value?

Post by alowe »

1024MAK wrote: Wed Apr 12, 2023 4:08 pm This is a very simple line editor. It does not have a cursor and the only editing keys are RUBOUT and NEWLINE.

For it to run quickly, it needs to be at the very start of the BASIC program. So replace line 10 with 10 GOTO 100, add 90 RETURN, then call it using GOSUB 20. And put your program after it starting from line 100 onwards.

Code: Select all

10 LET A$=“B=10*2”
20 PRINT AT 10,0;A$;“ ”;
30 LET I$=INKEY$
40 IF I$=“” THEN GOTO 30
50 IF I$<=“Z” THEN LET A$=A$+I$
60 LET I=CODE I$
70 IF I=119 THEN IF LEN A$>0 THEN LET A$=A$( TO LEN A$-1)
80 IF I<>118 THEN GOTO 20
7B3313DA-A9EE-4673-B361-C96A47243272.jpeg

Mark
Nice clean code.

Well, I finally managed to rewrite it. Started poking to the D_FILE which was problematic because it was too fast. Ironic. Then discovered that if you poke a function character to the D_FILE, the emulation crashes. :lol:

So, in the end was forced to use PRINT anyway. It's a lot more complicated when each keypress creates an unknown length output to the screen which you can't easily track to enable using rubout, but I did it with the help of a bit of PEEKing at S_POSN. Even then, S_POSN doens't update as you'd expect if the last character displayed is a function character.

Looks good now. I even handle the cursor to show when in function mode. The formula entered is stored with function charcodes but what is displayed to the screen is whatever the ZX81 outputs when it prints a function character. There's something in PRINT that converts each function to a string so the D_FILE doesn't get corrupted. Sneaky Z! :roll:

The code runs beautifully, but it looks like a dog's dinner. :lol:
User avatar
1024MAK
Posts: 5118
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Is is possible for INPUT to have a default value?

Post by 1024MAK »

Yeah, only normal character codes should be poked to the D_file. Don’t forget, the binary representation of the bytes in the display file are important. That’s why the character set is arranged as it is.

The expansion of keywords to their characters is one of the reasons that PRINT is slow. In fact, any time the interpreter has to display, list, or print (including on the ZX Printer or equivalent), the ROM routine to expand a keyword token to individual characters.

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Is is possible for INPUT to have a default value?

Post by XavSnap »

Mark's code with a cursor:

Code: Select all

5 LET S$="@D316399*256+@D316398-1"
10 LET A$="B=10*2"
20 PRINT AT 10,0;A$;" ";
25 POKE VAL S$,195
30 LET I=CODE INKEY$
40 IF NOT I THEN GOTO 30
50 IF I<64 THEN LET A$=A$+CHR$ I
60 IF I=119 THEN IF LEN A$>0 THEN LET A$=A$( TO LEN A$-1)
70 POKE VAL S$,(I AND I<64)+128 AND I<118
80 IF I<>118 THEN GOTO 20
In this code, the space character is banned, it will escape the basic program.
You can delete the 70 line to speed up the code...
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Is is possible for INPUT to have a default value?

Post by XavSnap »

Cap0000.jpg
Cap0000.jpg (8.27 KiB) Viewed 3908 times

Code: Select all

5 LET S$="@D316399*256+@D316398-1"
10 LET A$="B=10*2"
20 PRINT AT 10,0;A$;" ";
25 POKE VAL S$,195
30 LET I=CODE INKEY$
40 IF NOT I THEN GOTO 30
50 IF I<64 THEN LET A$=A$+CHR$ I
60 IF I=119 THEN IF LEN A$>0 THEN LET A$=A$( TO LEN A$-1)
70 POKE VAL S$,(I AND I<64)+128 AND I<118
80 IF I<>118 THEN GOTO 20
100 FOR A=1 TO LEN A$-1
110 GOSUB 1000
120 IF F THEN LET A$=A$(1 TO A-1)+F$+A$(A+(2 AND F=66)+(3 AND F=199)+(3 AND F=200) TO)
130 IF F THEN GOTO 100
140 NEXT A

150 PRINT AT 12,0;A$; AT 14,0;"=";VAL A$(3 TO)
160 STOP

1000 LET F=0
1001 LET F$=""
1002 IF A$(A TO A+1)="PI" THEN LET F=66
1003 IF LEN A$<A+2 THEN GOTO 1006
1004 IF A$(A TO A+2)="COS" THEN LET F=200
1005 IF A$(A TO A+2)="SIN" THEN LET F=199
1006 IF F THEN LET F$=CHR$ F
1007 RETURN
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
Post Reply