MINIGRAFIK lineart

Basic and Machine Language

Moderator: Moderators

User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: MINIGRAFIK lineart

Post by MrSterlingBS »

Hi,

I would like to introduce you to another graphic from the book "SPIELE MIT COMPUTER GRAFIK" from Norbert Treitz.
The normal speed, the counter counts up to 15382 . With the float point patch from Mike it runs a little bit faster. 15184
But, if you use SuperNumbers from the POST "Faster VIC20" the speed increases enormously to 9960.
SuperNumbers are of course the pound sign \ = £

Maybe the SuperNumbers could be included in MINIGRAFIK as an integral part?

And sorry again to send a lot of code here. I have some trouble to send the programs as a file, i dont know why?

Code: Select all

0 @on:@clr
5 ti$="000000"
10 forx=0to80:fory=0to96
15 r=x*x+y*y
20 if(r/150and1)then60
35 @1,79+x,95+y
40 @1,80-x,95+y
50 @1,80-x,96-y
55 @1,79+x,96-y
60 next:next
65 te=ti
70 geta$
75 ifa$=""then70
80 @return
85 print"zeit: "te

Code: Select all

0 @on:@clr
5 ti$="000000"
6 \a=79:\b=80:\r=0
7 \c=95:\d=96:\v=150
8 \y=0:\x=0:\z=0:\e=1
10 for\x=\zto\b
12 for\y=\zto\d
15 \r=\x*\x+\y*\y
20 if(\r/\vand\e)then60
35 @1,\a+\x,\c+\y
40 @1,\b-\x,\c+\y
50 @1,\b-\x,\d-\y
55 @1,\a+\x,\d-\y
60 next:next
65 te=ti
70 geta$
75 ifa$=""then70
80 @return
85 print"zeit: "te
Attachments
circle.jpg
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: MINIGRAFIK lineart

Post by Mike »

For a fairer comparison of run times, in the standard version you should assign the most often used constants and variables to 'early' variables in very much the same way as you did with the version using SuperNumbers: (download)

Code: Select all

0 @ON:@CLR
1 X=0:Y=0:E=1
2 A=79:B=80:C=95:D=96
3 F=150
5 TI$="000000"
10 FORX=0TO80:FORY=0TO96
15 R=X*X+Y*Y
20 IF(R/FANDE)THEN60
35 @E,A+X,C+Y
40 @E,B-X,C+Y
50 @E,B-X,D-Y
55 @E,A+X,D-Y
60 NEXT:NEXT
65 TE=TI
70 GETA$
75 IFA$=""THEN70
80 @RETURN
85 PRINT"ZEIT: "TE
This gives 11888 jiffies (original standard version: 15382 or 15184 jiffies, SuperNumbers: 9960 jiffies).

I sorted the variables and constants in line 1..3 roughly to how often they are used in the main loop in lines 10..60. The initialisation of the loop variable is not counted in - FOR stores away a pointer to the loop variable and NEXT already accesses it as fast as with SuperNumbers -, also note start and end values of a FOR loop are only read once and then stored, and thus also not counted in:
  • X,Y: 6 times (explicitly excluding their occurrence as FOR loop variables in line 10)
  • E: 5 times
  • A, B, C, D: 4 times each (explicity excluding their use as end values in the FOR loop)
  • F: only a single time, but 150 already is a 3 digit constant that internally gets parsed as (1*10+5)*10+0 ...

MrSterlingBS wrote:Maybe the SuperNumbers could be included in MINIGRAFIK as an integral part?
I have good reasons not to do this - and it is also not strictly necessary either: both tools already work together out-of-the-box.
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: MINIGRAFIK lineart

Post by MrSterlingBS »

Hi Mike,

thank you for the explanation.

BR Sven
User avatar
javierglez
Vic 20 Hobbyist
Posts: 107
Joined: Sat Jun 03, 2017 3:33 pm

Re: MINIGRAFIK lineart

Post by javierglez »

MrSterlingBS wrote: Sun Aug 27, 2023 2:42 amI have some trouble to send the programs as a file, i dont know why?
I think you have to zip prg files
MrSterlingBS wrote: Sun Aug 27, 2023 2:42 am And sorry again to send a lot of code here.
I find code as text is a comfortable way to share code if there are no PETSCII or control symbols.
In this case it's possible to copy and paste it into VICE with no further edits. Letters must be lowercase as you did.
Especially in this case, I could load Minigrafik by drag and dropping a d64 file onto VICE, and then paste the code.

BTW it's a nice drawing. You can change the line 15 to make it circles instead of ellipses.

Code: Select all

15 r=1.5*x*x+0.67*y*y 
Or

Code: Select all

15 r=2.25*x*x+y*y 
Attachments
vice-screen-20230827211357373975.png
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: MINIGRAFIK lineart

Post by MrSterlingBS »

Thank you Javier, but this code is not mine. It is from the the book "SPIELE MIT COMPUTER GRAFIK" from Norbert Treitz.
I did not found them online.

Just a small correction of the values.
Change the FORX TO 79 and A is B and B is A and the double calculation in the middle is away.

BR
Sven
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: MINIGRAFIK lineart

Post by MrSterlingBS »

Hello,

i like to convert the above BASIC code to assembly.
I work with the FP routines.
I can calculate the result from R=X*X+Y*Y and R/150.
But how do I now compare the calculated number?
In BASIC, the line is IF(R/150AND1)THEN60.

BR
Sven
wimoos
Vic 20 Afficionado
Posts: 348
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: MINIGRAFIK lineart

Post by wimoos »

Hello Sven

If you want yo go ML, then you should consider doing the whole thing in integers and integer calculation. All calculation results will be in the (maybe double) byte range anyway and you wont have the float-to-integer conversions vice versa (which are relatively expensive in terms of CPU).

You would see delay times improve from several minutes to a few seconds.

And even SQR can be done in ML integer.

Regards,

Wim
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: MINIGRAFIK lineart

Post by MrSterlingBS »

Hi Wim,

thanks for your answer.
I know that i can calculate the values with some faster integer multiply routine. The fastest with tables!

But i dont know how to translate the IF statement in ML.

Do you know the answer?

Best regards
Sven
wimoos
Vic 20 Afficionado
Posts: 348
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: MINIGRAFIK lineart

Post by wimoos »

In WimBasic I would use DIVMOD to divide by 150 and then check DQ for oddity. So, check the ML behind DIVMOD.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
javierglez
Vic 20 Hobbyist
Posts: 107
Joined: Sat Jun 03, 2017 3:33 pm

Re: MINIGRAFIK lineart

Post by javierglez »

It's not clear what you mean. Once R/150 is converted to an integer, the rest of the statement just takes bit 0 and branches if set. You don't need to call the ROM, it's straightforward assembler:

Code: Select all

lda result_lowbyte
and #1
bne @label60
or

Code: Select all

lsr result_lowbyte
bcs @label60
I don't see the need to speed up this graphic other than an educational purpose.
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: MINIGRAFIK lineart

Post by MrSterlingBS »

Hi Javier,

yes, to convert the BASIC code to ML is just an exercise for me to handle with the BASIC FP routines.
I know there are a lot of other routines for calculating with integer numbers.

Thank you for your help.

BR
Sven
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: MINIGRAFIK lineart

Post by MrSterlingBS »

The SpeedFactor is 5x with BASIC FP Arithmetic.
This is a huge improvement.
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: MINIGRAFIK lineart

Post by Mike »

From my experience, calling the arithmetic routines in the BASIC interpreter directly (eliminating token dispatch, constant evaluation, operator priority stack handling and variable search) results in a speed up of roughly factor 3 (not 5), see here.

This factor 3 also is about the maximum speed-up you can expect, for basic arithmetic only (addition, subtraction, multiply, division, comparisons, absolute value, sign inversion). The speed-up for the more complex functions (square root, power, logarithm, exponential, sine, cosine, tangent, arc-tangent) will be considerably less, as these call dozens of the basic arithmetic functions already by themselves to perform their work, where the above mentioned overhead from parsing already has been eliminated.

MINIGRAFIK does its duty to accelerate the graphics primitives (point set, line draw, screen clear) to a bearable speed for BASIC, but to accelerate calculations, you would need to pinpoint a significant 'hot spot' or 'core loop' outside the graphics part.

That being said: as long as that demo program featuring the interference pattern is not in any way parametrized (i.e. as is it always calculates the same pattern), the easiest method to 'convert' the program to ML would probably be @SAVEing the result. Once that file has been loaded back (with "...",8 to the normal BASIC start, without MG being active), RUN will bring the picture to screen in a fraction of a second.
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: MINIGRAFIK lineart

Post by MrSterlingBS »

Hi,

I rewrote the Fresnel program in floating point and interger assembler. Unfortunately I'm still missing the following conversion.
In my version i still calculate r=x*x + y*y without constants. For a correct mapping, the formula should be r=3/2*x*x + 2/3*y*y. I still don't have the right trick to do this. I can't quite get the result with the asl and lsr commands. Can anyone help here?
Program.zip
(1.16 KiB) Downloaded 53 times
The program is quite long for this application. This is because I use lookup tables for graphing and multiplication.
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: MINIGRAFIK lineart

Post by MrSterlingBS »

For comparison, the BASIC version again with some small adjustments. The constants 3/2 and 2/3 were added and the constants A and B were swapped (not correct). (double calculation in the first two loop runs) In addition, the calculation x*x is only calculated once per loop run of y.

Solution = 11948 jiffies @ 160x192 pixels!

Code: Select all

0 @on:@clr:x=.:y=.:r=.
1 e=1:g=3/2:h=2/3:z=.
2 a=79:b=80:c=95:d=96
3 f=150
5 ti$="000000"
10 forx=.to79
11 z=g*x*x:fory=.to96
15 r=z+h*y*y
20 if(r/fande)then60
35 @e,b+x,c+y
40 @e,a-x,c+y
50 @e,a-x,d-y
55 @e,b+x,d-y
60 next:next
65 te=ti
70 geta$
75 ifa$=""then70
80 @return
85 print"jiffies: "te
Post Reply