BASIC conversion into and out of VICE in UNIX

You need an actual VIC.

Moderator: Moderators

Richard James
Vic 20 Devotee
Posts: 269
Joined: Mon Feb 04, 2008 6:06 am

BASIC conversion into and out of VICE in UNIX

Post by Richard James »

Before I waste time writing a tool that already exists. I want to know if anyone can get BASIC programs into or out of VICE under UNIX. I use Linux and to put BASIC into VICE I have to type it in which can be hard since the PC keyboard does not map well to the VIC one. I cannot get programs out of VICE except for copying character by character by hand.

Is there an easier way to do this?

My solution would be to write a program that can read and write D64, tokenized BASIC entries. Also it would put the program in a special editor so you could see all the codes like C=A or CTRL-1. Maybe also it could have a special keyboard down the bottom that would display the keys as on a real VIC20 and you could type with the mouse.

Another thought occurs to me. I often want to place BASIC programs onto place like this ASCII forum but it does not support the CBM charset so I write codes like this <shift><home>. Is there any convention for that? I mean do people prefer <shift> or [SHIFT] or what?
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post by carlsson »

There is a command line tool petcat that comes with VICE. It will convert from text to binary or vice versa. In order to read or write binary files from/to a D64 image, you can use the command line tool c1541 that comes with VICE.

So, you can type in a Basic listing in your favorite text editor, use petcat to eventually get a binary (tokenized) version, store it on a D64 image and off you go. Actually, VICE can read directly from your hard disk so the D64 isn't strictly neccessary.

Petcat is a bit tricky to get running, and it lacks detailed instructions, but after a bit of fiddling you will figure out the parameters. Otherwise ask, I have played around a bit with it lately. There are other "3rd party" programs that do the same thing; see tok64, bastext and so on.

Schlowski on this forum a few years ago wrote a Windows text editor that lets you enter a listing and save it in tokenized form. I'm unsure if it also could import/export pure text files, but since you're running Linux it isn't quite as interesting to you.
Anders Carlsson

Image Image Image Image Image
Richard James
Vic 20 Devotee
Posts: 269
Joined: Mon Feb 04, 2008 6:06 am

Post by Richard James »

got petcat to work. Important NOTE: make sure your listing is all in lower case. I did not have that and could not work out why all my letters got tokenised.
so I have a file like such

Code: Select all

10 print"A";
20 for x=0to21
30 for y=0to22
40 rem seed random number generator
50 a=rnd(-(x*13+y*43))
60 of=x+y*22
70 poke7680+of,160
80 r%=rnd(1)*4+2
90 poke38400+of,r%
100 nexty
110 nextx
Save that as test.txt

Then I run

Code: Select all

petcat -w2 -o a.prg -l 1001 -- test.txt
and then activate VICE like this

Code: Select all

xvic a.prg
Except I can't get it to see control codes like <home> or <clr>. So this is not exactly what I want.

Reading the source code I see it uses {clr} not <clr> as stated in the manpage. Also available are the following codes:
{SHIFT-SPACE}, {CBM-K}, {CBM-I}, {CBM-T}, {CBM-@}, {CBM-G}, {CBM-+},{CBM-M}, "{CBM-POUND}","{SHIFT-POUND}", "{CBM-N}", "{CBM-Q}", "{CBM-D}", "{CBM-Z}", "{CBM-S}", "{CBM-P}", "{CBM-A}", "{CBM-E}", "{CBM-R}", "{CBM-W}", "{CBM-H}", "{CBM-J}", "{CBM-L}", "{CBM-Y}","{CBM-U}", "{CBM-O}", "{SHIFT-@}", "{CBM-F}", "{CBM-C}", "{CBM-X}", "{CBM-V}", "{CBM-B}"
"{SHIFT-*}","{SHIFT-A}","{SHIFT-B}","{SHIFT-C}","{SHIFT-D}","{SHIFT-E}","{SHIFT-F}","{SHIFT-G}", "{SHIFT-H}","{SHIFT-I}","{SHIFT-J}","{SHIFT-K}","{SHIFT-L}","{SHIFT-M}","{SHIFT-N}","{SHIFT-O}", "{SHIFT-P}","{SHIFT-Q}","{SHIFT-R}","{SHIFT-S}","{SHIFT-T}","{SHIFT-U}","{SHIFT-V}","{SHIFT-W}",
"{SHIFT-X}","{SHIFT-Y}","{SHIFT-Z}","{SHIFT-+}","{CBM--}","{SHIFT--}","{SHIFT-^}","{CBM-*}"

Which pretty much makes petcat a feature complete tokeniser and I can write code like this:

Code: Select all

10 print"{clr}{CBM-A}{SHIFT-A}"
Now my only problem is remembering the different codes and what they produce. So maybe I might make that program but I can use petcat to work out the token values.
nippur72
de Lagash
Posts: 574
Joined: Thu Sep 07, 2006 8:35 am

Post by nippur72 »

talking of CBM basic, is there any "basic-optimizer" ? I think there are a wide range of optimizations that can be done to make it faster (but less readable), for example:

- putting as many statements as possible into a single line (separating by semicolons)
- renaming all variables so to make 1-letter names
- trimming spaces between statements
- removing variable after "next"
- solve partial expressions
- turn "0" into "."
- remove all REMs
MacbthPSW
Vic 20 Afficionado
Posts: 478
Joined: Wed Apr 06, 2005 1:56 pm

Post by MacbthPSW »

nippur72 wrote:talking of CBM basic, is there any "basic-optimizer" ?
There are some C64 programs that do some of these things. They could possibly be adapted easily to work on the VIC, or if not, the VIC programs could be run through on the C64 and then brought back to the VIC.

I remember a neat one that would profile BASIC's use of variables. I think it was called "Faster 64" and was published in Ahoy! magazine. I believe it would wedge itself into BASIC, and then you'd run your BASIC program for a while. It would record how many times each variable was accessed, and give you a sorted list of the variables used the most. If you defined these variables first, the program could realize a fairly substantial improvement, because of the way BASIC looks up variables.
Richard James
Vic 20 Devotee
Posts: 269
Joined: Mon Feb 04, 2008 6:06 am

Post by Richard James »

Why is BASIC so slow? is there no optimisation written into it? is there any way to speed up the interpretation? I wonder if we could rewrite BASIC to take some advantage of modern Virtual Machine techniques to speed up commonly used code? Or is there not enough memory in the VIC20 to do that?
nippur72
de Lagash
Posts: 574
Joined: Thu Sep 07, 2006 8:35 am

Post by nippur72 »

Basic is slow becase is interpreted (ok, nothing new), for example each time you reference to a variable, it has to look up for it in the table of used variables and retrieve its value. Or, when you reference to an expression like "INT(RND(0)*500)" it has to dig in the hell of nested parenthesis, and so on.

The solution to this is compiled basic, where each of these operations are done once for all at compile time, e.g. variable names are substituted with their # in the lookup table and expressions are solved using polish notation (stackwise).

The problem with compiled basic, I guess, is that the result of compiled code is longer than the original interpreted program. So with only 3,5 K of free space, it might happen that your compiled is longer than available memory.

Modern virtual machines you are talking about do compile the code in real time (the so called just-in-time compiling) but I don't think there is enough space to do something like that in Vic20. The compiler itself I think eats most of free memory.

Talking of basic compilers, what is available for the VIC20 ? Are C64 basic compilers good too? And, is there a PC-based compiler for cross-developing?

Happy easter.
User avatar
Schlowski
NoMess!
Posts: 892
Joined: Tue Jun 08, 2004 12:20 pm

Post by Schlowski »

- (1) putting as many statements as possible into a single line (separating by semicolons)
- (2) renaming all variables so to make 1-letter names
- (3) trimming spaces between statements
- (4) removing variable after "next"
- (5) solve partial expressions
- (6) turn "0" into "."
- (7) remove all REMs
I've done (1) and if memory serves well (3) and (7) in my Windows based BasEdit. This includes a renumber with starting by 1 and increasing in steps by 1, thereby shortening line numbers and saving space, since line numbers after GOTO, GOSUB and THEN are not crunched to 16 bit integers but stay as PETSCII numbers in source. I never use (4) so I had no need to remove them :-) (6) could be done very easily, but (5) is a more difficult part, I think...
nippur72
de Lagash
Posts: 574
Joined: Thu Sep 07, 2006 8:35 am

Post by nippur72 »

Schlowski wrote:but (5) is a more difficult part, I think...
and it's not even said it improves, e.g. solving "22/7" avoids the call to floating point division but produces a long petscii costant "3.1428571"
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post by carlsson »

Richard James wrote:Now my only problem is remembering the different codes and what they produce.
As a side note, you can write things such as {10 DOWN} in your listing. It is the same syntax as used by COMPUTE! if I recall correctly.
Anders Carlsson

Image Image Image Image Image
Richard James
Vic 20 Devotee
Posts: 269
Joined: Mon Feb 04, 2008 6:06 am

Post by Richard James »

carlsson wrote:
Richard James wrote:Now my only problem is remembering the different codes and what they produce.
As a side note, you can write things such as {10 DOWN} in your listing. It is the same syntax as used by COMPUTE! if I recall correctly.
I meant the codes for the special symbols like lines, blocks and circles. My other option is to have a card with that printed on it and have that next to my monitor. It is very hard to work it out by looking up in a book or holding a Vic20 up on your desk. If I had the money I would buy one of those keyboards with the OLED display in each key then I could just program the keys to match a commodore keyboard.

I used to have three images of the keyboard, Normal, Shift and C=. That made it somewhat easier.
Richard James
Vic 20 Devotee
Posts: 269
Joined: Mon Feb 04, 2008 6:06 am

Post by Richard James »

nippur72 wrote:Or, when you reference to an expression like "INT(RND(0)*500)" it has to dig in the hell of nested parenthesis, and so on.
I have a book about implementing BASIC on my bookshelf. I had forgotten about the time it takes to un-nest formulas.

in the example above it saves time if you make R an integer
R%=RND(0)*500

It does not save time if you do this
R=RND(0)*500:R=INT(R)

Also dropping the variable off of a NEXT statement does speed a loop up. Just not by very much.

Writing a compiler would be pretty simple. Having the time to do so not simple. If I saw there was a great need for it I would do it. At the moment I am led to do other things.
User avatar
e5frog
Vic 20 Nerd
Posts: 551
Joined: Sat Feb 17, 2007 5:46 pm
Website: http://channelf.se
Location: Sweden
Occupation: Service Engineer

Post by e5frog »

Richard James wrote:I cannot get programs out of VICE except for copying character by character by hand.
To get programs OUT of VICE you can perhaps use the printer.

Someone here can probably scribble down the commands to do so.

I think you need to open a file and then direct the screen output to that, and then LIST.
My other interest: http://channelf.se
Richard James
Vic 20 Devotee
Posts: 269
Joined: Mon Feb 04, 2008 6:06 am

Post by Richard James »

HOWTO get BASIC programs out of VICE

Type in your program or load it.
In the VICE menu select the monitor
type
m 2b 2e
This will print out the start and end address of the BASIC program
remember the VIC stores 16bit addresses as low byte high byte
e.g.
>C:002b 01 10 03 10
start is the first two numbers 01 10 which is 1001
end is the second two numbers 03 10 which is 1003
type
save "test.dat" 0 1001 1003
or whatever numbers are the start and end of BASIC for your program
then in a console
petcat test.dat > test.bas
to convert the tokens into readable form
edit test.bas to your liking
User avatar
e5frog
Vic 20 Nerd
Posts: 551
Joined: Sat Feb 17, 2007 5:46 pm
Website: http://channelf.se
Location: Sweden
Occupation: Service Engineer

Post by e5frog »

What about this, in BASIC after program is loaded (and stopped):

Code: Select all

OPEN1,4:CMD1:LIST:CLOSE1


But you need to do this first (once):

The output file is set in "Peripheral settings", "Printer 4", tick "Use IEC Device", "Printer Emulation - File system", "Printer driver - ASCII", "Output - Text", "Output to file # - 1", "Output file #1 name - name_of_your_choice"

If you haven't already. ;-)


What about that output? Doesn't look to bad in a hexeditor. Usually you need to close VICE to be able to open the file.
My other interest: http://channelf.se
Post Reply