New Keywords

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

New Keywords

Post by chysn »

I'm on the lookout for resources about BASIC extensions, specifically of the type that add new BASIC commands. As I see it, I need to intercept three vectors, to

(1) Tokenize the new keywords while parsing a BASIC input line (vector @ $0304)
(2) Detokenize the new keywords during LIST ($0306)
(3) Execute the code associated with each new keyword ($0308)

So, I'm looking at the ROM listing, working on Step 1. I figured I could look for my own keywords first and create my own tokens ($D0 and up), then pass execution along to the original $0304 vector ($C57C). As a quick-and-dirty* proof-of-concept, I redirected $0304 to this.

Code: Select all

   ldx $7a
@1 lda $0200,x
   cmp #"Z"
   bne @2
   lda #$d0
   sta $0200,x
@2 inx
   bpl @1
   jmp $c57c
But after my stuff does its work, the original routine pulls my new "token" out of the input buffer. No SYNTAX ERROR when just entering Z, but the token is gone and the input is shifted away by some code later on in the ROM routine ($C5CB). When I change the token for Z to something like $8A (RUN), I'd hope that Z becomes an alias for RUN. But no.

It seems like I'll need to completely re-implement the tokenization routine, rather than simply patch into it. Is this a correct assessment?

I thought there was a section in one of the Compute! Nth Book of VIC books, but I didn't find it. Maybe I misremember, and I should look at the Books of C64...

Once I get this part figured out, I'll probably use this same topic for the other two steps as I work through my BASIC extension.

________________
* For example, don't care about quotes here
VIC-20 Projects: wAx Assembler, TRBo: Turtle RescueBot, Helix Colony, Sub Med, Trolley Problem, Dungeon of Dance, ZEPTOPOLIS, MIDI KERNAL, The Archivist, Ed for Prophet-5

WIP: MIDIcast BASIC extension

he/him/his
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: New Keywords

Post by chysn »

I was remembering Second Book of 64, which I used to own. I’m having a close look at this article.

And, oooh! I call the ROM routine FIRST! A slight change of order:
Screen Shot 2023-12-29 at 8.01.20 PM.png
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: New Keywords

Post by chysn »

Progress so far... The $0304 (tokenize) vector goes to $1800, and $0306 (detokenize) vector goes to $1815. I'm basically just redefining PRINT as Z here. At this point, you can imagine how everything else is just details; creating a new command table and testing against it on both ends, handling quotes, REM, and DATA.

My next step will be to define the new command Z as something different. I already have somewhat of a handle on this, but I need to learn how to process parameters in my new commands.
Screen Shot 2023-12-29 at 8.48.32 PM.png
Screen Shot 2023-12-29 at 8.40.40 PM.png
User avatar
srowe
Vic 20 Scientist
Posts: 1340
Joined: Mon Jun 16, 2014 3:19 pm

Re: New Keywords

Post by srowe »

You might find the disassembly of the Super Expander useful to learn about this subject

https://eden.mose.org.uk/gitweb/?p=rom- ... sm;hb=HEAD

That uses a two step process when executing commands, first it handles arguments and then has another dispatcher that actually does the operation.

The Programmers' Aid also has new commands, but these are only for immediate mode so the code is different

https://eden.mose.org.uk/gitweb/?p=rom- ... sm;hb=HEAD
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: New Keywords

Post by Mike »

chysn wrote:I'm on the lookout for resources about BASIC extensions, specifically of the type that add new BASIC commands.
srowe wrote:You might find the disassembly of the Super Expander useful to learn about this subject
For what matters it had been exactly the routines in Super Expander I took as blue print when I wrote the BASIC extension for MAXIGRAFIK. :mrgreen:

I might quite as well quote the relevant part of the source here:

Code: Select all

.Vectors
 LDX #8
.Vectors_00
 LDA Vectors_01-1,X
 STA      &0304-1,X
 DEX
 BNE Vectors_00
 RTS
.Vectors_01
 EQUW Tokenize
 EQUW List
 EQUW Exec
 EQUW EvalV

.Tokenize
 LDX &7A
 LDY #&04:STY &0F
.Tokenize_00
 LDA &0200,X:BPL Tokenize_01
 CMP #&FF:BEQ Tokenize_08
 INX:BNE Tokenize_00
.Tokenize_01
 CMP #&20:BEQ Tokenize_08:STA &08
 CMP #&22:BEQ Tokenize_12
 BIT &0F:BVS Tokenize_08
 CMP #&3F:BNE Tokenize_02
 LDA #&99:BNE Tokenize_08
.Tokenize_02
 CMP #&30:BCC Tokenize_03
 CMP #&3C:BCC Tokenize_08
.Tokenize_03
 STY &71
 LDY #&00:STY &0B:DEY
 STX &7A:DEX
.Tokenize_04
 INY:INX
.Tokenize_05
 LDA &0200,X
 SEC:SBC &C09E,Y:BEQ Tokenize_04
 CMP #&80:BNE Tokenize_13
.Tokenize_06
 ORA &0B
.Tokenize_07
 LDY &71
.Tokenize_08
 INX:INY:STA &01FB,Y
 LDA &01FB,Y:BEQ Tokenize_19
 SEC:SBC #&3A:BEQ Tokenize_09
 CMP #&49:BNE Tokenize_10
.Tokenize_09
 STA &0F
.Tokenize_10
 SEC:SBC #&55:BNE Tokenize_00
 STA &08
.Tokenize_11
 LDA &0200,X:BEQ Tokenize_08
 CMP &08:BEQ Tokenize_08
.Tokenize_12
 INY:STA &01FB,Y
 INX:BNE Tokenize_11
.Tokenize_13
 LDX &7A
 INC &0B
.Tokenize_14
 INY:LDA &C09D,Y:BPL Tokenize_14
 LDA &C09E,Y:BNE Tokenize_05
 LDY #&FF
 DEX
.Tokenize_15
 INY:INX
.Tokenize_16
 LDA &0200,X
 SEC:SBC Tokens,Y:BEQ Tokenize_15
 CMP #&80:BNE Tokenize_17:BEQ Tokenize_06
.Tokenize_17
 LDX &7A
 INC &0B
.Tokenize_18
 INY:LDA Tokens-1,Y:BPL Tokenize_18
 LDA Tokens,Y:BNE Tokenize_16
 LDA &0200,X:BPL Tokenize_07
.Tokenize_19
 JMP &C609

.List
 BPL List_07
 CMP #&FF:BEQ List_07
 BIT &0F:BMI List_07
 TAX
 STY &49
 CMP #&CC:BCS List_00
 LDY #&C0:STY &23
 LDY #&9E:STY &22
 BNE List_01
.List_00
 SBC #&4C:TAX
 LDY #Tokens DIV 256:STY &23
 LDY #Tokens MOD 256:STY &22
.List_01
 LDY #&00
 ASL A:BEQ List_06
.List_02
 DEX:BPL List_05
.List_03
 INC &22:BNE List_04:INC &23
.List_04
 LDA (&22),Y:BPL List_03
 BMI List_02
.List_05
 INY
.List_06
 LDA (&22),Y:BMI List_08
 JSR &CB47:BNE List_05
.List_07
 JMP &C6F3
.List_08
 JMP &C6EF

.If
 JSR &0073:JSR &CD9E
 JSR &0079:CMP #&89:BEQ If_00
 LDA #&A7:JSR &CEFF
.If_00
 LDA &61:BNE If_01
 JSR &C909:JMP &C8FB
.If_01
 JSR &0079:BCS Exec_00
 JMP &C8A0

.Exec
 JSR &0073:JSR Exec_00:JMP &C7AE
.Exec_00
 CMP #&8B:BEQ If
 CMP #&CC:BCS Exec_01
 JSR &0079:JMP &C7ED
.Exec_01
 SBC #&CC:CMP #(Exec_04 - Exec_03) DIV 2:BCS Exec_02
 ASL A:TAY
 LDA Exec_03+1,Y:PHA
 LDA Exec_03  ,Y:PHA
 JMP &0073
.Exec_02
 JMP &CF08
.Exec_03
 EQUW Show-1
 EQUW Clear-1
 EQUW Draw-1
 EQUW Circle-1
 EQUW Color-1
 EQUW Multi-1
 EQUW GSave-1
 EQUW GLoad-1
.Exec_04

.Tokens
 EQUS "SHO"+CHR$(128+ASC("W"))
 EQUS "CLEA"+CHR$(128+ASC("R"))
 EQUS "DRA"+CHR$(128+ASC("W"))
 EQUS "CIRCL"+CHR$(128+ASC("E"))
 EQUS "COLO"+CHR$(128+ASC("R"))
 EQUS "MULT"+CHR$(128+ASC("I"))
 EQUS "GSAV"+CHR$(128+ASC("E"))
 EQUS "GLOA"+CHR$(128+ASC("D"))
 EQUS "POIN"+CHR$(128+ASC("T"))
 EQUS "AD"+CHR$(128+ASC("R"))
 EQUB 0

[...]

.EvalV
 LDA #0:STA &0D:JSR &0073
 CMP #&D4:BEQ Point
 CMP #&D5:BEQ Adr
 JSR &0079:JMP &CE8D

[...]
The command dispatcher contains the necessary code to handle the IF shortcut correctly.

I have included the handler for evaluating functions (wedged into $030A) for completeness.

Note the tokenizer is a greedy parser. The original keywords are matched first, then the new keywords, and keyword order is important. If you have two keywords, one of which is an extension of the first, the longer keyword needs to be first in the list (that's why INPUT# and PRINT# come before their non-hash-suffixed originals!).

You can however define such "extended" keywords in another way, by providing the suffix as token, diverting the original command (in much the same way I did with IF) and checking for the suffix token before continuing with either the original command or the new command. BASIC V3.5 and V7 do so for the command GETKEY (which gets tokenized as GET KEY, with KEY just being another keyword) and BASIC V10 allows for both DIR and DIRECTORY by supplying "ECTORY" as suffix token.
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: New Keywords

Post by chysn »

srowe wrote: Sat Dec 30, 2023 1:41 am You might find the disassembly of the Super Expander useful to learn about this subject
Yes! That's so cool, that's exactly the kind of thing I was looking for.
Mike wrote: Sat Dec 30, 2023 7:03 am You can however define such "extended" keywords in another way, by providing the suffix as token\
I have planned to do this, somewhat by necessity since I'm adding NOTEON and ON is already a keyword. So we'll have at least NOTEON (or NOTE ON) and NOTEOFF (or NOTE OFF).

Thanks to you both! This is going to be a fun project. More to come...
User avatar
Soloman
Vic 20 Amateur
Posts: 44
Joined: Fri Sep 22, 2023 1:46 am
Location: Bilthoven, Netherlan
Occupation: Data-analyst

Re: New Keywords

Post by Soloman »

chysn wrote: Fri Dec 29, 2023 6:21 pm I was remembering Second Book of 64, which I used to own. I’m having a close look at this article.

And, oooh! I call the ROM routine FIRST! A slight change of order:

Screen Shot 2023-12-29 at 8.01.20 PM.png
You can download this book for free. Great book.
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: New Keywords

Post by chysn »

Soloman wrote: Sat Dec 30, 2023 10:26 am You can download this book for free. Great book.
Yep, thanks, most of these are on archive.org. Welcome to Denial!
User avatar
Soloman
Vic 20 Amateur
Posts: 44
Joined: Fri Sep 22, 2023 1:46 am
Location: Bilthoven, Netherlan
Occupation: Data-analyst

Re: New Keywords

Post by Soloman »

chysn wrote: Sat Dec 30, 2023 3:36 pm
Soloman wrote: Sat Dec 30, 2023 10:26 am You can download this book for free. Great book.
Yep, thanks, most of these are on archive.org. Welcome to Denial!
Thank you! I felt a little bit lonely busy with the VIC-20 on my own here. Now I found this community.
Post Reply