Porting Some Old GW-BASIC Code to FreeBASIC, Hitting Weird Issues

General FreeBASIC programming questions.
Post Reply
load81
Posts: 6
Joined: Mar 22, 2019 23:21

Porting Some Old GW-BASIC Code to FreeBASIC, Hitting Weird Issues

Post by load81 »

As a learning exercise, I'm porting a program I found printed in a magazine (released into the public domain by the author) to FreeBASIC. My goal is to work up slowly from simple programs to more complex ones and then port a few games from their original platforms to FreeBASIC. In this case, it's a simple program used by Ham Radio operators to calculate RF exposure.

I found that I'm running into some unexpected issues...

I retyped the program — line numbers and all — and ran it in -lang qb mode. Once I fixed my typoes everything worked as expected except for the first line. FreeBASIC didn't like the program's use of the COLOR keyword. I strongly suspect this was written in GW-BASIC back in the day as it uses the COLOR keyword with three arguments instead of two. (If I recall correctly, the third argument is to set the boarder color.) FreeBASIC doesn't support this usage and it's not critical to the program, so I just commented it out. That cleared the first issue. Easy fix.

Next, I replaced the GOTO targets with line labels and deleted the remaining line numbers. The resulting code worked in -lang qb just fine. At this point, I'm thinking "okay, cool time to switch to -lang fblite and start modernizing the code with things like proper modern functions, etc." I re-ran the now labeled code in fblite mode and initially everything seemed to work. But then I noticed computational errors that resulted in some values unexpectedly reading zero. I'm not sure what the deal is with this. What quirk of fblite am I running into?

Also, I ran into something strange when running the code in some terminals. On a typical terminal you'll find in KDE/GNOME/XFCE the program works fine. In Kitty Terminal, a modern terminal with programmer ligature support, everything locks up around the second prompt and won't take any input. It just hangs until I hit <CTRL+C>. I'm not sure what the deal is there, whether it's a FreeBASIC bug or a Kitty Terminal bug.

Can someone point me in the right direction? I'm running FreeBASIC 1.09.0 under Gentoo Linux. The code is below:

Code: Select all

'COLOR 15,1,4:CLS:PRINT:PRINT:PRINT:REM REV. 2.1 10/27/96
PRINT "      MAIN BEAM POWER DENSITY ESTIMATION PROGRAM"
PRINT "    FOR ROUTINE EVALUATION OF RF SAFET COMPLIANCE"
PRINT:PRINT
PRINT "This program uses the formulas given in FCC OET Bulletin No. 65"
PRINT "to estimate power density in the mane lobe of an antenna, with"
PRINT "use of the EPA-recommended ground reflection factor as an option."
PRINT:PRINT "This program is intended for for far field calculations. It may"
PRINT "overestimate the actual field strength of high-gain antennas in"
PRINT "the near field (within several wavelengths of the antenna)."
PRINT "However, it may also underestimate the strength of fields that may"
PRINT "be encountered in 'hot spots' in the near field. No computer"
PRINT "program can predict where wiring or reflective objects may create"
PRINT "hot spots in your particular installation.":PRINT
PRINT "This is a public domain program by Wayne Overbeck, N6NB":PRINT
INPUT "WHAT IS THE AVERAGE POWER AT THE ANTENNA (IN WATTS)"; WATTS
PWR = 1000 * WATTS
PRINT:PRINT "WHAT IS THE ANTENNA GAIN IN DBI?"
INPUT "(Enter 2.2 for dipoles; add 2.2 for antennas rated in DBD): ",GAIN

REM NOW CALCULATING EIRP IN MILLIWATTS
EIRP=PWR*(10^(GAIN/10))
PRINT:INPUT "WHAT IS THE DISTANCE TO AREA OF INTEREST FROM ANTENNA CENTER IN FEET";FT

REM NOW CONVERTING TO CM
DX=FT*30.48

MHZ_PROMPT:
PRINT:INPUT "WHAT IS THE FREQUENCY IN MHZ";F
IF F<1.34 THEN STD1=100:STD2=100:GOTO GROUND_EFFECTS_PROMPT
IF F<3 THEN STD1=100:STD2=180/((F)^2):GOTO GROUND_EFFECTS_PROMPT 
IF F<30 THEN STD1=900/((F)^2):STD2=180/((F)^2):GOTO GROUND_EFFECTS_PROMPT
IF F<300 THEN STD1=1:STD2=.2:GOTO GROUND_EFFECTS_PROMPT
IF F<1500 THEN STD1=F/300:STD2=F/1500:GOTO GROUND_EFFECTS_PROMPT
IF F<100000! THEN STD1=5:STD2=1:GOTO GROUND_EFFECTS_PROMPT 
PRINT "The FCC DOES NOT HAVE EXPOSURE LIMITS ABOVE 100 GHZ":GOTO MHZ_PROMPT

GROUND_EFFECTS_PROMPT:
PRINT:PRINT "NOW, DO YOU WISH TO INCLUDE THE EFFECTS OF GROUND REFLECTIONS?"
PRINT "(Ground effects need not be included in most main-beam calculations"
PRINT "but including them may yield more accurate results with very low"
PRINT "antennas, not-directional antennas, and calculations below the"
PRINT "main lobe of directional antennas.) INCLUDE GROUND EFFECTS (Y/N)";G$
GF=.25:GR$="WITHOUT":IF G$="Y" THEN GF=.64:GR$="WITH"
IF G$="y" THEN GF=.64:GR$="WITH"
PWRDENS=(GF*EIRP)/(3.14159*(DX^2))
PWRDENS=(INT((PWRDENS * 100000) +.5))/10000
DX1=SQR((GF*EIRP)/(STD1*3.14159)):DX1=DX1/30.48:DX1=(INT((DX1*10)+.5))/10
DX2=SQR((GF*EIRP)/(STD1*3.14159)):DX1=DX1/30.48:DX1=(INT((DX1*10)+.5))/10
STD1=(INT((STD1*100)+.5))/100:STD2=(INT((STD2*100)+.5))/100
CLS:PRINT "THE RESULTS ARE AS FOLLOWS:":PRINT
PRINT "WITH";WATTS;"WATTS AND";GAIN;"DBI GAIN ";GR$;" GROUND REFLECTIONS, AT";FT;"FEET"
PRINT "FROM THE ANTENNA CENTER THE ESTIMATED POWER DENSITY IS";PWRDENS;"MW/CM2.":PRINT
PRINT "AT";F;"MHZ, THE MAXIMUM PERMISSIBLE EXPOSURE (MPE) IN `CONTROLLED"
PRINT "ENVIRONMENTS` (SUCH AS YOUR OWN HOUSEHOLD OR CAR) IS";STD1;"MW/CM2."
PRINT "THE MPE IN `UNCONTROLLED ENVIRONMENTS` (SUCH AS NEIGHBORS PROPERTY)"
PRINT "IS";STD2;"MW/CM2. THIS INSTALLATION WOULD MEET THE  CONTROLLED MPE"
PRINT "LIMIT AT";DX1;"FEET AND THE UNCONTROLLED LIMIT AT";DX2;"FEET."
PRINT:PRINT "ALTERNATE CALCULATION METHODS:"
PRINT " 1) Exposure is averaged over six minutes in controlled environments"
PRINT "and 30 minutes in uncontrolled environments. If you never transmit more"
PRINT "than three minutes in any six-minute period, divide the power density"
PRINT "shown above by two then calculating the power density in your own house-"
PRINT "hold. Also divide by two when calculating fields beyond your property"
PRINT "if you never transmit more than 15 minutes in any 30-minute period."
PRINT " 2) If you wish to estimate the power density at a point below the main"
PRINT "lobe of a directional antenna, and if the antenna's vertical pattern is"
PRINT "known, recalculate using the antenna's gain in the relevant direction."
fxm
Moderator
Posts: 11208
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Porting Some Old GW-BASIC Code to FreeBASIC, Hitting Weird Issues

Post by fxm »

The default data type is 'Single' in the -lang qb dialect but 'Integer' in the -lang fblite dialect.
load81
Posts: 6
Joined: Mar 22, 2019 23:21

Re: Porting Some Old GW-BASIC Code to FreeBASIC, Hitting Weird Issues

Post by load81 »

Interesting. So that means my values are getting truncated, right? Is there a list of other changes to default behavior between qb and fblite?
caseih
Posts: 1994
Joined: Feb 26, 2007 5:32

Re: Porting Some Old GW-BASIC Code to FreeBASIC, Hitting Weird Issues

Post by caseih »

@coderJeff posted a link some time back where he had some lists of features and differences in the dialects. Probably should be in the FB wiki.
http://www.execulink.com/~coder/freebasic/compare.html
David Watson
Posts: 37
Joined: May 15, 2013 16:48
Location: England

Re: Porting Some Old GW-BASIC Code to FreeBASIC, Hitting Weird Issues

Post by David Watson »

According to the manual you can put

Code: Select all

defsng a-z
at the start of the program and non-declared variables will default to single type.
angros47
Posts: 2016
Joined: Jun 21, 2005 19:04

Re: Porting Some Old GW-BASIC Code to FreeBASIC, Hitting Weird Issues

Post by angros47 »

@load81

You used "COLOR 15,1,4" (three parameters), while in FreeBasic it takes only 2 parameters. Replace it with "COLOR 15,1"
The third parameter is the border color, that was used in GwBasic, and was ignored in QBasic. In FreeBasic it is not supported at all (nor it can be supported, on LCD screens)

BTW, I assume you started programming on a Commodore 64, with your name.
load81
Posts: 6
Joined: Mar 22, 2019 23:21

Re: Porting Some Old GW-BASIC Code to FreeBASIC, Hitting Weird Issues

Post by load81 »

angros47 wrote: Aug 05, 2022 17:51 @load81

You used "COLOR 15,1,4" (three parameters), while in FreeBasic it takes only 2 parameters. Replace it with "COLOR 15,1"
The third parameter is the border color, that was used in GwBasic, and was ignored in QBasic. In FreeBasic it is not supported at all (nor it can be supported, on LCD screens)

BTW, I assume you started programming on a Commodore 64, with your name.
Right, yes, I just commented out that line so that I didn't have crazy colors in my terminal window. UI standards have changed a lot in 30 years. :D

Correct, it was a mix of C64 and Apple II but I strongly preferred the C64.
Post Reply