Jump to content
IGNORED

BASIC Translation Help Requested


Recommended Posts

This is more of a fun side thing for me, but I've been playing in AppleSoft BASIC and built a simple looping program that draws and redraws sine waves across my screen.  That's it.  Nothing special.  Just having a little fun.  But now I'm wondering if I can get this to work in something other than AppleSoft?  Maybe Integer? Atari? Commodore? Radio Shack? CoCo?  Ti? Heck, even Sinclair!  I just want to see how this all translates across various dialects of BASIC.

 

The program is as follows:

 

5 HGR2

10 HCOLOR=3

20 FOR X1=0 TO 279

30 LET Y1=INT(-96*SIN(X1*3.14/56))+96

40 HPLOT X1,Y1

50 NEXT X1

60 FOR X2=279 TO 0 STEP -1

70 LET Y2=INT(96*SIN(X1*3.14/56))+96

80 HPLOT X2,Y1

90 NEXT X2

100 HCOLOR=4

110 FOR X3=0 TO 279

120 LET Y3=INT(-96*SIN(X1*3.14/56))+96

130 HPLOT X3,Y3

140 NEXT X3

150 FOR X4=279 TO 0 STEP -1

160 LET Y4=INT(96*SIN(X1*3.14/56))+96

170 HPLOT X4,Y4

180 NEXT X4

190 GOTO 10

 

Obviously, HGR2, HCOLOR, and HPLOT are all AppleSoft specific commands.  And the various X ranges and Y values will need to be adjusted to fit the screen resolution of whatever machine...  Still, any help steering me in the right direction would be appreciated!

 

Side question: If anyone has any ideas on how to simplify this program in AppleSoft, that too would be useful.  I've tried the same lines for X1,Y1 and X2,Y2, plotting them, and changing the color, but I always get an undefined variable error.  

Link to comment
Share on other sites

Ehh.. in all four lines 30, 70, 120, 160 you use X1 for the SIN calculation yet the FOR loops use different X variables.

 

Also you could pre-calculate 3.14/56 which I think is a way to convert from radians 0 .. 2*pi to degrees 0 .. 359? Yet 280 pixels don't equal 360 degrees, so if you want the full SIN curve I would use SIN(X1/280 * Pi * 2). Obviously Pi*2/280 could be pre-calculated to 0.02244. This is where different resolution comes into play, e.g. on C64 in high resolution you would use Pi*2/320.

 

Most of your program should look the same on all BASIC dialects with the exception for how you plot the pixels, as you already figured out.

  • Like 2
Link to comment
Share on other sites

15 minutes ago, carlsson said:

Ehh.. in all four lines 30, 70, 120, 160 you use X1 for the SIN calculation yet the FOR loops use different X variables.

Eek!  Sorry.  Typo.

 

This should read: 

 

5 HGR2

10 HCOLOR=3

20 FOR X1=0 TO 279

30 LET Y1=INT(-96*SIN(X1*3.14/56))+96

40 HPLOT X1,Y1

50 NEXT X1

60 FOR X2=279 TO 0 STEP -1

70 LET Y2=INT(96*SIN(X2*3.14/56))+96

80 HPLOT X2,Y1

90 NEXT X2

100 HCOLOR=4

110 FOR X3=0 TO 279

120 LET Y3=INT(-96*SIN(X3*3.14/56))+96

130 HPLOT X3,Y3

140 NEXT X3

150 FOR X4=279 TO 0 STEP -1

160 LET Y4=INT(96*SIN(X4*3.14/56))+96

170 HPLOT X4,Y4

180 NEXT X4

190 GOTO 10

 

 

18 minutes ago, carlsson said:

Also you could pre-calculate 3.14/56 which I think is a way to convert from radians 0 .. 2*pi to degrees 0 .. 359? Yet 280 pixels don't equal 360 degrees, so if you want the full SIN curve I would use SIN(X1/280 * Pi * 2). Obviously Pi*2/280 could be pre-calculated to 0.02244. This is where different resolution comes into play, e.g. on C64 in high resolution you would use Pi*2/320.

Trying this out.  There has always been a bit of a gap between the first and second wave on the right of the screen that I've never been able to close.

 

20 minutes ago, carlsson said:

Most of your program should look the same on all BASIC dialects with the exception for how you plot the pixels, as you already figured out.

Yeah, that's the big question.  I understand that Commodore BASIC lacks the graphics routines of AppleSoft.  I do have a Simon's BASIC cart though.  But I'm not sure if that helps.  Maybe the Super Expander?  I understand that it added graphics routines to the VIC-20.

 

The real hard one (at least for me) will be the TRS-80.  My Model III has a high-res card installed, but I have no idea how to use it.  I'm not even sure if BASIC can access it (at least the non-disk BASIC in ROM).  

Link to comment
Share on other sites

The Internet Archive has a couple of BASIC conversion guides. The one listed as edited by Bill Crider covers Apple II, C64, TRS-80 Model III/4, and Coco and should be a good guide for US based computers. I don't know of a matching book to convert to the higher resolution systems sold in Europe. 

 

HGR equals Screen 2* on the PC or PMODE 4 on Coco. *Screen 6 is the color high resolution mode for the PCJr; Screen 8 or 9 should give the high resolution modes for PCs with EGA or VGA. 

 

HPLOT matches with PSET (x,y) on the PC or SET (h,v,c) on the Coco.

 

Let me take a few minutes and copy the BASIC program over to PCBASIC.  I commented out the color values because I would have to look up colors and SCREEN modes. It seems to have drawn the sin wave but it is scrunched over to the left and some changes would be needed to the values for the results to look correctly. Okay, got the colors solved and the only problem is that it uses the upper left quarter of the screen only. 

 

1 CLS

5 SCREEN 8

10 COLOR 3

20 FOR X1=0 TO 639

30 LET Y1=INT(-96*SIN(X1*3.14/56))+96

40 PSET (X1,Y1)

50 NEXT X1

60 FOR X2=639 TO 0 STEP -1

70 LET Y2=INT(96*SIN(X2*3.14/56))+96

80 PSET (X2,Y2)

90 NEXT X2

100 COLOR 4

110 FOR X3=0 TO 639

120 LET Y3=INT(-96*SIN(X3*3.14/56))+96

130 PSET (X3,Y3)

140 NEXT X3

150 FOR X4=639 TO 0 STEP -1

160 LET Y4=INT(96*SIN(X4*3.14/56))+96

170 PSET (X4,Y4)

180 NEXT X4

190 GOTO 10 

 

That should be the code base for an IBM PC with EGA. Alternates sine waves between red and blue. Made another change setting to SCREEN 8 and 640 horizontal pixels and the screen above the Function Key line is now filled. 

Edited by Krebizfan
had a mistake
Link to comment
Share on other sites

Some of the 8-bits should be easy targets. BBC Micro, Spectrum, and Amstrad CPC all use PLOT for single pixels. MSX uses PSET similarly to the IBM PC. Commodore 128 has pixels done through the DRAW command. All of these have graphics modes about as good as the Apple II high-res if not better since I doubt dropping from 280x192 to 256x192 will radically alter the program. 

Link to comment
Share on other sites

13 hours ago, Krebizfan said:

The Internet Archive has a couple of BASIC conversion guides. The one listed as edited by Bill Crider covers Apple II, C64, TRS-80 Model III/4, and Coco and should be a good guide for US based computers. I don't know of a matching book to convert to the higher resolution systems sold in Europe. 

 

Thank you for this!  I've already downloaded the Bill Crider one and have been working on the Coco translation.  So far, all I get is a green screen with a small black backwards L shape in the upper left corner, but no syntax errors!  So it's a start!

 

13 hours ago, Krebizfan said:

Let me take a few minutes and copy the BASIC program over to PCBASIC.  I commented out the color values because I would have to look up colors and SCREEN modes. It seems to have drawn the sin wave but it is scrunched over to the left and some changes would be needed to the values for the results to look correctly. Okay, got the colors solved and the only problem is that it uses the upper left quarter of the screen only. 

 

1 CLS

5 SCREEN 8

10 COLOR 3

20 FOR X1=0 TO 639

30 LET Y1=INT(-96*SIN(X1*3.14/56))+96

40 PSET (X1,Y1)

50 NEXT X1

60 FOR X2=639 TO 0 STEP -1

70 LET Y2=INT(96*SIN(X2*3.14/56))+96

80 PSET (X2,Y2)

90 NEXT X2

100 COLOR 4

110 FOR X3=0 TO 639

120 LET Y3=INT(-96*SIN(X3*3.14/56))+96

130 PSET (X3,Y3)

140 NEXT X3

150 FOR X4=639 TO 0 STEP -1

160 LET Y4=INT(96*SIN(X4*3.14/56))+96

170 PSET (X4,Y4)

180 NEXT X4

190 GOTO 10 

 

That should be the code base for an IBM PC with EGA. Alternates sine waves between red and blue. Made another change setting to SCREEN 8 and 640 horizontal pixels and the screen above the Function Key line is now filled. 

I've never tried BASIC on a PC.  But I do have a 486!  However, if I'm correct, then the X variable you're using is for a 640X480 screen, so I think tweaking the Y values to [line] LET Y[?]=INT([+-]240*SIN(X[?]*3.14/128))+240, should make the wave fill the entire screen?  

 

Also, what are the limits of PCBASIC?  AppleSoft seems to have problems with more than four X and Y variables, which has limited the program to two colors.  Not a problem on a monochrome screen, but a 486 is a far more capable machine.  So is it possible to expand this to use all the colors available, or change line 10 to either cycle through them, or select a color at random?  If so, then all I need are the initial forward and reverse waves (lines 20 through 90) and this thing can be cut from 20 lines to 10.

 

1 hour ago, Krebizfan said:

Some of the 8-bits should be easy targets. BBC Micro, Spectrum, and Amstrad CPC all use PLOT for single pixels. MSX uses PSET similarly to the IBM PC. Commodore 128 has pixels done through the DRAW command. All of these have graphics modes about as good as the Apple II high-res if not better since I doubt dropping from 280x192 to 256x192 will radically alter the program. 

Sadly, i don't have a BBC Micro, Amstrad CPC, C128 or MSX machine at the moment...  I'm actively looking for them, but have yet to find.  However, I do have a ZX Spectrum!  

 

Also, just curious, but... What about the ZX81?

 

13 hours ago, OLD CS1 said:

The standard TI BASIC and TI Extended BASIC languages do not have equivalents to HGR2 or HPLOT, so such a translation will be extremely convoluted, though not impossible.

Agreed.  And I have a feeling that the C64, VIC-20 and TRS-80 (Model I or III) will have similar translation issues, since as far as I know, they too lack the designated graphics commands found in AppleSoft.  Still, each is a variant of Microsoft BASIC.  So it should at least be possible.

Also, my experience with TI BASIC (Extended or otherwise) has been very slow.  However, I'm not sure if that's the result of the hardware, or the BASIC interpreter Texas Instruments was using?

I do have a copy of Simon's BASIC for the C64 that might help?  I'm also looking into getting a super expander for my VIC-20.

 

Link to comment
Share on other sites

I forgot how low resolution the Apple II's high resolution mode was. I could have saved time by using SCREEN 1 and left the pixel calculation logic alone. Sure, the program would only have used 280x192 out of the 320x200 CGA mode but that is workable enough for a first draft. I used the 640 by 200 16 color EGA mode by mistake. Standard CGA supports 4 colors at 320 by 200 but 16 colors are available with EGA or PCJr and Tandy modes. Slight changes to the positioning logic would be necessary. 

 

PCBASIC is an emulation of GWBASIC (IBM Advanced BASIC) that runs under Windows. Makes it easy for me to run a quick test for the 90% of BASIC software that does not need PEEK or POKE.  You could have a lot more variables with names up to 40 characters long with GWBASIC though given the code design it would probably be easy to use the same variables through each set of loops. 

 

I have been contemplating converting the program over to the Basicode 3 standard just to see how much overhead that entails. If it pans out, then it would allow the program to run on many other systems using the included GOSUB 610 point drawing logic. It would be quite slow but given the demo, that might be a good thing letting each pixel change have visible delay. 

Link to comment
Share on other sites

BBC Basic version :

 

    5 MODE 1
   10 GCOL 0,1
   20 FOR X1=0 TO 1280
   30 LET Y1=INT(-96*SIN(X1*3.14/56))+96
   40 PLOT 69,X1,Y1
   50 NEXT X1
   60 FOR X2=1280 TO 0 STEP -1     
   70 LET Y2=INT(96*SIN(X2*3.14/56))+96
   80 PLOT 69,X2,Y2
   90 NEXT X2
  100 GCOL 0,2
  110 FOR X3=0 TO 1280
  120 LET Y3=INT(-96*SIN(X3*3.14/56))+96
  130 PLOT69,X3,Y3
  140 NEXT X3
  150 FOR X4=1280 TO 0 STEP -1 
  160 LET Y4=INT(96*SIN(X4*3.14/56))+96
  170 PLOT69,X4,Y4
  180 NEXT X4
  190 GOTO 10

Had to change some of the loop parameters to 1280 due to how BBC basic handles graphics units.

 

You can run in it MODE 0 but it would be then just black and white.

 

Link to comment
Share on other sites

Commodore 128 BASIC 7 version.

 

5 COLOR 4,1
6 COLOR 0,1
7 COLOR 1,2
10 GRAPHIC 1,1
20 FOR X1=0 TO 319
30 LET Y1=INT(-96*SIN(X1*3.14/56))+96
40 DRAW 1,X1,Y1
50 NEXT X1
60 FOR X2=319 TO 0 STEP-1
70 LET Y2=INT(96*SIN(X2*3.14/56))+96
80 DRAW 1,X2,Y2
90 NEXT X2
100 COLOR 1,8
110 FOR X3=0 TO 319
120 LET Y3=INT(-96*SIN(X3*3.14/56))+96
130 DRAW 1,X3,Y3
140 NEXT X3
150 FOR X4=319 TO 0 STEP -1
160 LET Y4=INT(96*SIN(X4*3.14/56))+96
170 DRAW 1,X4,Y4
180 NEXT X4
190 GOTO 10

 

Link to comment
Share on other sites

13 hours ago, Muddyfunster said:

BBC Basic version :

 

12 hours ago, Muddyfunster said:

Commodore 128 BASIC 7 version.

Thanks for these!  I'll have to test them in emulators for the time being, but the Commodore one does shed some potential light on the issue I'm having with the Coco translation.

Edited by DistantStar001
  • Like 1
Link to comment
Share on other sites

The following block of code is the BASICODE 3C implementation. It requires being merged with the BASICODE translator piece for the specific system which contains the lines before 1000. I tested it with the PC version not others. 

HG is the number of horizontal pixels in graphics mode. VG is the number of vertical pixels in graphics mode. HO and VE have the horizontal and vertical position for the screen location. CN=0 uses foreground color; CN=1 uses background color and erases the pixel. GOSUB 600 puts the program in graphics mode. GOSUB 620 draws the pixels. One interesting note is that BASICODE requires pixel positioning values to be between 0 and 1 which are then calculated based on the screen dimensions to the proper positioning which should explain the extra divisions for HO and VE. Compared to the GW-BASIC version, this runs a lot slower since each of the thousands of PSET calls is wrapped in a GOSUB. 

 

1000 A=100:GOTO 20:REM BASICODE START

1010 HB=HO:VB=VE:UW=1-1/HG:P1=3.14159:MT=0:TM=0:IV=255

1020 IT=0:RA=0:RB=0:RC=0:RD=0:PA=0:PB=0:PC=0:PD=0

1030 GOSUB 100:REM CLEAR SCREEN

1040 GOSUB 600:REM GRAPHICS MODE

1050 CC[0]=3:CN=0

1060 FOR X1=0 TO HG-1

1070 LET Y1=INT(-(VG/2)*SIN(X1*3.14/56))+(VG/2)

1080 HO=X1/HG:VE=Y1/VG

1090 GOSUB 620

1100 NEXT X1

1110 FOR X2=HG-1 TO 0 STEP -1

1120 LET Y2=INT((VG/2)*SIN(X2*3.14/56))+(VG/2)

1130 HO=X2/HG:VE=Y2/VG

1140 GOSUB 620

1150 NEXT X2

1160 CC[0]=1:CN=1

1170 FOR X3=0 TO HG-1

1180 LET Y3=INT(-(VG/2)*SIN(X3*3.14/56))+(VG/2)

1190 HO=X3/HG:VE=Y3/VG

1200 GOSUB 620

1210 NEXT X3

1220 FOR X4=HG-1 TO 0 STEP -1

1230 LET Y4=INT((VG/2)*SIN(X4*3.14/56))+(VG/2)

1240 HO=X4/HG:VE=Y4/VG

1250 GOSUB 620

1260 NEXT X4

1270 GOTO 1050

1280 GOTO 950:REM BASICODE EXIT

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...