Is there sample VIC20 assembly doing wedges for BASIC?

Basic and Machine Language

Moderator: Moderators

unebonnevie
Vic 20 Drifter
Posts: 35
Joined: Sat Oct 11, 2014 3:25 pm

Is there sample VIC20 assembly doing wedges for BASIC?

Post by unebonnevie »

Looking how to add additional commands to VIC20's basic via wedges. Appreciate for any sample code and an example.

Thanks!
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Is there sample VIC20 assembly doing wedges for BASIC?

Post by Mike »

unebonnevie wrote:Looking how to add additional commands to VIC20's basic via wedges. Appreciate for any sample code and an example.
Hmm - essentially there are three different flavours of doing a BASIC extension on the VIC-20:

- wedging into CHRGET - that's the poor man's version of adding commands,
- wedging into $0308 (character dispatch) with a marker token - that's how, for example, MINIGRAFIK works,
- providing a tokenizer, detokenizer, and the routine vectored into $0308 with correct handling of the IF ... THEN shortcut. The optimum. :)

In any case, adding commands to the interpreter requires innate knowledge of the ROM. Not only of the necessary routines to read parameters and return results. Sadly, there's no well defined interface, and even the processor flags must be handled correctly when re-entering the interpreter loop.

You might take MINIGRAFIK as example; but you most probably need to put a ROM listing aside for cross-referencing and be prepared to invest at least one or two weeks to build an own BASIC extension from scratch.
wimoos
Vic 20 Afficionado
Posts: 345
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: Is there sample VIC20 assembly doing wedges for BASIC?

Post by wimoos »

Take at look at the source code that I provide with WimBasic, see http://wimbasic.webs.com
I have spent some time adding comments to the code.

Regards,

Wim.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
malcontent
Vic 20 Hobbyist
Posts: 129
Joined: Sun Dec 26, 2010 1:51 pm

Re: Is there sample VIC20 assembly doing wedges for BASIC?

Post by malcontent »

Compute's "Programming the Vic" has an example with ML code for a BASIC wedge: P. 281

On DHL's site: http://www.bombjack.org/commodore/books-kim-pet-vic.htm
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Is there sample VIC20 assembly doing wedges for BASIC?

Post by Mike »

malcontent wrote:Compute's "Programming the Vic" has an example with ML code for a BASIC wedge: P. 281
That example wedges into CHRGET, and, as I already wrote, this is a rather poor method to extend BASIC.

CHRGET is called at 22 places within the BASIC interpreter, not only when a new command is executed, but also at various other times when variables, numbers and symbols are parsed. Normally, a CHRGET wedge checks the return address on stack and immediately bails out, when there is some address other than $C7E7 (-1) stored there (which is the CHRGET call in the character dispatch vectored by $0308) so not to disturb and slow down all the other calls. Wedging into $0308 avoids all this trouble as the extension then only is executed at the right time, namely when the next command is going to be parsed and executed.

The CHRGET wedge dates from the 'early' CBM BASIC times, when the vector at $0308 didn't exist and patching CHRGET was the only way to extend BASIC.

@unebonnevie: you should tell us, what kind of new commands you'd want to put into a BASIC extension. It is quite possible, that there already exists a BASIC extension for the VIC-20 that suits your needs. Or, what you think might be missing can actually expressed in a simple way as subroutine (GOSUB) or user defined function (DEF FN) with the commands/functions already available.
unebonnevie
Vic 20 Drifter
Posts: 35
Joined: Sat Oct 11, 2014 3:25 pm

Re: Is there sample VIC20 assembly doing wedges for BASIC?

Post by unebonnevie »

Mike wrote:
malcontent wrote:Compute's "Programming the Vic" has an example with ML code for a BASIC wedge: P. 281
That example wedges into CHRGET, and, as I already wrote, this is a rather poor method to extend BASIC.

CHRGET is called at 22 places within the BASIC interpreter, not only when a new command is executed, but also at various other times when variables, numbers and symbols are parsed. Normally, a CHRGET wedge checks the return address on stack and immediately bails out, when there is some address other than $C7E7 (-1) stored there (which is the CHRGET call in the character dispatch vectored by $0308) so not to disturb and slow down all the other calls. Wedging into $0308 avoids all this trouble as the extension then only is executed at the right time, namely when the next command is going to be parsed and executed.

The CHRGET wedge dates from the 'early' CBM BASIC times, when the vector at $0308 didn't exist and patching CHRGET was the only way to extend BASIC.

@unebonnevie: you should tell us, what kind of new commands you'd want to put into a BASIC extension. It is quite possible, that there already exists a BASIC extension for the VIC-20 that suits your needs. Or, what you think might be missing can actually expressed in a simple way as subroutine (GOSUB) or user defined function (DEF FN) with the commands/functions already available.
Thanks, Michael. Yes, I am looking to do a DIY project using the VIC20's user port to provide an RTCC (Real-time Clock and Calendar) . I thought of adding two BASIC commands, something like GetTime() and SetTime(), via the wedge technique.

Thanks, again. Your answers have been very helpful.
Bryan
Vic 20 Newbie
Posts: 6
Joined: Thu Feb 23, 2023 2:46 pm

Re: Is there sample VIC20 assembly doing wedges for BASIC?

Post by Bryan »

One of the simplest wedge method that I've seen was Aleksi Eeben's plot command wedge. It's great as a learning tool, performs a useful function and efficiently coded.

http://sleepingelephant.com/ipw-web/bul ... ot#p115298
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Is there sample VIC20 assembly doing wedges for BASIC?

Post by Mike »

Bryan wrote:One of the simplest wedge method that I've seen [...]
If anything, the tool you refer to uses the canonical implementation of a BASIC extension, using the ($0308) character dispatch vector.
User avatar
bjonte
Vic 20 Hobbyist
Posts: 108
Joined: Sun Jan 22, 2017 5:47 am
Location: Gothenburg

Re: Is there sample VIC20 assembly doing wedges for BASIC?

Post by bjonte »

I happen to be interested in this topic as well right now. What’s the issue with IF, THEN that needs special handling?
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Is there sample VIC20 assembly doing wedges for BASIC?

Post by Mike »

bjonte wrote:What's the issue with IF, THEN that needs special handling?
After a THEN, the next command is not called over $0308, rather the interpreter makes a direct, unvectored jump. This is done because THEN also accepts a line number, which is then jumped to if the IF .. THEN clause is true.

BASIC extensions that do not handle this shortcut have the common feature that it is necessary to place a colon between THEN and any of the new instructions, as workaround. Otherwise you get a ?SYNTAX error.

Getting this right requires a replacement of the character dispatch routine which also happens to include a full copy of the IF command to remain in control and do that shortcut by own code, like thus:

Code: Select all

 [...]
.If
 JSR $0073:JSR $CD9E              ; evaluate expression after IF
 JSR $0079:CMP #$89:BEQ If_00     ; check for GOTO keyword in place of THEN
 LDA #$A7:JSR $CEFF               ; check for THEN
.If_00
 LDA $61:BNE If_01                ; expression non-zero?
 JSR $C909:JMP $C8FB              ; no, skip rest of line
.If_01
 JSR $0079:BCS Exec_00            ; do shortcut, unless a numeric character has been found
 JMP $C8A0                        ; In the latter case, execute GOTO <line number>

.Exec
 JSR $0073:JSR Exec_00:JMP $C7AE  ; read character, thread token dispatch, re-enter interpreter loop
.Exec_00
 CMP #$8B:BEQ If                  ; IF token? Execute own copy of IF command
 CMP #$CC:BCS Exec_01             ; own commands? Continue at Exec_01
 JSR $0079:JMP $C7ED              ; continue at dispatch of V2 commands
.Exec_01
 [...]
User avatar
bjonte
Vic 20 Hobbyist
Posts: 108
Joined: Sun Jan 22, 2017 5:47 am
Location: Gothenburg

Re: Is there sample VIC20 assembly doing wedges for BASIC?

Post by bjonte »

Mike wrote: Sun Feb 26, 2023 10:11 am After a THEN, the next command is not called over $0308, rather the interpreter makes a direct, unvectored jump. This is done because THEN also accepts a line number, which is then jumped to if the IF .. THEN clause is true.
Ouch, how annoying.

Very helpful information! Thanks!
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 167
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: Is there sample VIC20 assembly doing wedges for BASIC?

Post by MrSterlingBS »

Hello,

I have two questions about wedges,
After testing the Code from codebase 64 and change to VIC Kernal routines. http://codebase64.org/doku.php?id=ba

Why i need a Basic NEW command Running the Program?

How can I
return solutions to the Running Basic Programm?

Example:
10 @iMUL,5,3 ; integer multiplication 5x3=15

Do i need to PEEK it from the memory location?
Or is there another way?

Best regard
Sven
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Is there sample VIC20 assembly doing wedges for BASIC?

Post by Mike »

MrSterlingBS wrote:Why do I need a BASIC NEW command [before] running the program?
Any LOAD of a non-BASIC program file in direct mode 'deranges' 45/46 (start of variables, a.k.a. 'end of program') and it then (likely) points out of range regarding the BASIC area, often resulting in ?OUT OF MEMORY errors afterward or other undefined behaviour. This is fixed by issuing a NEW after LOAD, and in turn this applies to pretty much any absolute loaded machine code executable that comes without BASIC stub.
How can I return [evaluation/function results] to the running BASIC program?
The vector in ($030A) is used for this. MINIGRAFIK implements the @(x,y) function (pixel read) this way.

Alternatively, you could use the USR() function vector in $01/$02, but per default USR() only takes one parameter, which is likely not sufficient for your intended application.

Even if you somehow get that "@iMUL()" function implemented, you probably need to lower your expectations quite a lot: most of the execution time of that function will be spent in float <-> integer conversions and any hoped-for speed gains will be minimal in the BASIC environment.
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 167
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: Is there sample VIC20 assembly doing wedges for BASIC?

Post by MrSterlingBS »

Dear Mike,

thanks a lot for your answer.

My idea was to create an integer Basic expansion. But after thinking again you are right. The time to convert from integer to float point kills the speed.

But your Basic Stub Idea was very interesting.
Is the „NEW“ Token placed just after the „SYS“ Token?

Best regards
Sven

PS: I have downloaded the MINIGRAFIK V 4.02. Is this the latest Version?
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Is there sample VIC20 assembly doing wedges for BASIC?

Post by Mike »

MrSterlingBS wrote:My idea was to create an integer Basic expansion.
If anything, you'd have to implement a whole BASIC interpreter that works with integers instead of floats. Suchalike languages exist for the C64 (I know of at least one 64'er and one RUN issue that features one of those).

Alternatively, a BASIC compiler could contain support for pure integer expressions.
But your Basic Stub Idea was very interesting.
Prefacing machine code with a BASIC stub is pretty much state of the art. It saves one having to remember the necessary SYS call (a RUN instead suffices) and either the machine code runs directly in place or a copy loop copies the machine code to the intended place and protects it from BASIC if necessary.
Is the „NEW“ Token placed just after the „SYS“ Token?
I don't know how this would be relevant for the problem at hand.

When you load a machine program without BASIC stub, in direct mode, you do LOAD"...",8,1, then NEW to correct the BASIC pointers and then SYS to call the machine code. Of course that means any BASIC program in memory will be deleted during that process, and this means the machine code should be loaded first.
PS: I have downloaded [...] MINIGRAFIK V 4.02. Is this the latest Version?
Yes it is.
Post Reply