UnderwareDesign
July 29, 2010, 06:53:41 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Asteroids Public Test by MonkeyBot (2nd,May,2010)
 
   Home   Help Login Register  
Pages: [1] 2 3 4
  Print  
Author Topic: PlayBasic V1.64k WIP Gallery  (Read 4636 times)
kevin
Development Team
Administrator
Hero Member
*****
Offline Offline

Posts: 9341



WWW
« on: October 19, 2009, 09:25:13 AM »


 PlayBasic V1.64k  WIP  Gallery

      This thread will document the changes being made to the PlayBasic 1.64K upgrade...


 Math Short Cuts
 
      The compiler/parser now supports various C styled math short cuts in assignments.  Including  ++, -- , +=, -=, *=, /=

 

Code:
Dim Table(2000)

Table(110)+=1000
Table(110)-=2222

print Table(110)

Dim Table#(1000)
Table#(10)=1000

Table#(10)/=50
print Table#(10)

Dim Table$(200)
Table$(10)="Yeah"

Table$(10)+="Dude"
print Table$(10)

a$="Cool"
print a$

c$="DUDEDDD"
b$="YESAH"
A$=C$+b$
print a$

a$++
print a$

Type Test
Aaa
X
Y#
z$
EndType

Dim Dude as test

Dude.x +=100
Dude.x +=100
Dude.x +=100
Print Dude.x

Dude.y +=200
Dude.y +=200
Print Dude.y

Dude.z +="aaa"
Dude.z +="bbb"
Print Dude.z



Dim Qrr(10,5) as test
print "Typed Array"

Qrr(10,5).x =45

Qrr(10,5).x +=100
Qrr(10,5).x +=100
Qrr(10,5).x +=100
Print Qrr(10,5).x

Sync
waitkey


 
         
 


 
Logged

kevin
Development Team
Administrator
Hero Member
*****
Offline Offline

Posts: 9341



WWW
« Reply #1 on: October 19, 2009, 11:37:17 PM »

 Extended the Math short cuts so they can be applied to Typed Arrays/Variables and Pointer Writes

Code:

Type Test
Aaa
X
Y#
z$
EndType

Dim Dude as test

Dude.x +=100
Dude.x +=100
Dude.x +=100
Print Dude.x

Dude.y +=200
Dude.y +=200
Print Dude.y

Dude.z +="aaa"
Dude.z +="bbb"
Print Dude.z

Dim Qrr(10,5) as test
print "Typed Array"

Qrr(10,5).x =45

Qrr(10,5).x +=100
Qrr(10,5).x +=100
Qrr(10,5).x +=100
Print Qrr(10,5).x




Dim You2 as integer pointer

Dim You as integer pointer
Bank=NewBank(1000)
addr=GetBankPtr(Bank)
You= addr

*You = 45
*You += 100
*You *=2
*You /=10
print *You


Dim Me as test pointer
Me = new test

me.x =13
print me.x

me.x *=4
print me.x

me.x /=13
print me.x

print "Y test"

me.y +=22
print me.y
me.y *=4
print me.y
me.y /=13
print me.y


me.z +="HellO"
me.z +="HellO"
me.z +="HellO"
print me.z

Sync
waitkey



  I should point out that shorts cuts are to help simplify user code, your unlikely to gain any runtime performance  over the equivalent long hand syntax. 

  Bench mark(Requires PB1.64K)

Code:


Dim Table(1000)

Max=10000

BumpValue=10

Do

Cls 0

frames++

T=timer()
Table(pos)=0
for lp=1 to max
Table(pos)=Table(pos)+BumpValue
next
tt1#+=(Timer()-t)
print Table(pos)
print "long Hand:"+str$(TT1#/frames)


T=timer()
Table(pos)=0
for lp=1 to max
// use the +=
Table(pos)+=BumpValue
next

tt2#+=(Timer()-t)
print Table(pos)
print "Short Cut:"+str$(TT2#/frames)

pos =mod(pos+1,1000)


Sync
loop

 

  Both loops run at identical speeds.
« Last Edit: October 24, 2009, 07:52:28 AM by kevin » Logged

Makeii Runoru
Full Member
***
Offline Offline

Posts: 116



WWW
« Reply #2 on: October 20, 2009, 09:21:06 PM »

THANK YOU! This will really help shorten code lines!
Logged

This signature is boring, and could be improved. However, the user of this signature doesn't need a fancy signature because he or she doesn't really care for one.
kevin
Development Team
Administrator
Hero Member
*****
Offline Offline

Posts: 9341



WWW
« Reply #3 on: October 22, 2009, 08:58:34 AM »


 PlayBasic V1.64k  - Byte + Word Fields in Types

      I've moved onto adding support from some more field types in UDT structures.  Starting with BYTE  and WORD data types.   While internally PB has no concept of anything as small as a byte or word,  supporting byte & word fields in UDTs will let us communicate better with 3rd party structures.    Which can be handy when calling windows functions or your own dll's perhaps.   You could of course use them in your programs, but the PB runtime is optimized for 32bit integers.   So it's often best to keep things as integers or floats, rather than mixing the datatypes.


 Sample

 
Code:


Type TestType
a as integer
b as float
c as byte
d as byte
EndType


DIm me as TestType

me.a =12345678
me.b =123.456
me.c= $aa
me.d =$bb
print me.a
print me.b
print hex$(me.c)
print hex$(me.d)

print Sizeof(TestType)
sync
waitkey

 


 Outputs

Code:
12345678
123.456
$000000AA
$000000BB
10




Logged

kevin
Development Team
Administrator
Hero Member
*****
Offline Offline

Posts: 9341



WWW
« Reply #4 on: October 24, 2009, 08:02:18 AM »

 PlayBasic V1.64k  Beta 5 - Math Short Cuts Cont.

    While the new math operation shorts cut can used on the majority of the datatypes/structures in PB, there's still a few structures that they couldn't be applied to.   Namely  array fields in user defined type structures.    However this ommission has been implemented in the BETA 5 of V1.64k. 

     Accessing array fields requires PB to resolve multiple levels to actually get to the element you require, as such,  these are the slowest types of data to access.  However math short cuts and certainly help improve the performance of operations between them.
 
Code:

Type TestType
a as integer
b as float
c as byte
z as word
d as byte
Stuff(100)
Stuff2#(100)
yeah$
EndType

Dim me(100) as TestType


Max=10000

BumpValue=1

Do

Cls 0

frames++
T=timer()
Me(50).stuff(pos)=0.0
for lp=1 to max
Me(50).stuff(pos)=me(50).stuff(pos)+BumpValue
next
tt1#+=(Timer()-t)
print Me(50).stuff(pos)
print "long Hand:"+str$(TT1#/frames)


T=timer()
Me(50).stuff(pos)=0
for lp=1 to max
// use the +=
Me(50).stuff(pos)+=BumpValue
next

tt2#+=(Timer()-t)
print Me(50).stuff(pos)
print "Short Cut:"+str$(TT2#/frames)

pos =mod(pos+1,100)

Sync
loop
   
     In this example we're adding a value to a nested integer array.  This time using the new += operator gives up about as %30 speed boost over the long hand version.   
 

   
« Last Edit: October 28, 2009, 09:20:59 PM by kevin » Logged

kevin
Development Team
Administrator
Hero Member
*****
Offline Offline

Posts: 9341



WWW
« Reply #5 on: October 25, 2009, 12:00:19 PM »

  PlayBASIC V1.64k  Beta 5b - Bit wise operator Math Short Cuts.

  Added support for bit AND (&=), OR(|=) and XOR(~=) as short cuts, as well as fixing a few casting issues short cuts in the previous beta.
   

Code:
a=$aabb

a&=$ff

print hex$(a)


Dim Test(1)

Test(1)= $aabbccdd

Test(1)&=$ff0000ff
print hex$(Test(1))

Sync
waitkey

« Last Edit: October 25, 2009, 12:03:27 PM by kevin » Logged

kevin
Development Team
Administrator
Hero Member
*****
Offline Offline

Posts: 9341



WWW
« Reply #6 on: October 25, 2009, 10:25:54 PM »


  PlayBASIC V1.64k  Beta 5b - Cont.
 
   This is just some test/bench code to see if there's any extra operations (casts) creeping into math shorts operations upon variables, when they're not the same data type.. But thankfully there's not now.  If there was, there'd be a performance penalty for using the short cut in this case..   
 
Code:
Max=10000

BumpValue#=10

Do

Cls 0

frames++

T=timer()
IntVar=0
for lp=1 to max
IntVar=IntVar+BumpValue#
next
tt1#+=(Timer()-t)
print IntVar
print "long Hand:"+str$(TT1#/frames)


T=timer()
IntVar=0
for lp=1 to max
IntVar+=BumpValue#
next
tt2#+=(Timer()-t)
print IntVar
print "Short Cut:"+str$(TT2#/frames)
Sync
loop

Logged

kevin
Development Team
Administrator
Hero Member
*****
Offline Offline

Posts: 9341



WWW
« Reply #7 on: October 26, 2009, 02:07:13 AM »


 PlayBASIC V1.64k  Beta 6 - Cont.
 
   If you're read the blog in the maintenance area you'll have no doubt noticed that i wanted to make a few more tweaks to VM1's performance, namely with how some the low level opcodes we're set up..  So this afternoon i've been going through the rather laborious process of restructuring how the VM executes the pointer and stack opcodes.   

   The results  have been fairly pleasing,  with approximately a 10% speed improvement on function calls and around a %40 speed improvement on writing into  array fields.. More tweaks to come

Code:

Type Stuff
a
b#
c$
Array(1000)
EndType


Dim me as stuff



Tests=10000

Do
cls 0
inc frames

print "Writing to Typed Fields Performance Test"

// ===================================
// USer Functions VS Projected Subroutines
// ===================================

// ==========
// Test #1
// ==========

T=timer()
For LP=0 to Tests
me.a=255
next
tt1#=tt1#+(timer()-t)
Print "Write Integer Average Time:"+Str$(tt1#/frames)



T=timer()
For LP=0 to Tests
me.b=255
next
tt2#=tt2#+(timer()-t)
Print "Write Float Average Time:"+Str$(tt2#/frames)


T=timer()
For LP=0 to Tests
me.c="255"
next
tt3#=tt3#+(timer()-t)
Print "Write String Average Time:"+Str$(tt3#/frames)


T=timer()
For LP=0 to Tests
me.array(10)=255
next
tt4#=tt4#+(timer()-t)
Print "Integer Array Average Time:"+Str$(tt4#/frames)

Sync
loop

   
Logged

Makeii Runoru
Full Member
***
Offline Offline

Posts: 116



WWW
« Reply #8 on: October 26, 2009, 11:39:18 AM »

I'm pleased with this. : )

Maybe you can add multi-dimensional support to arrays in types?
Logged

This signature is boring, and could be improved. However, the user of this signature doesn't need a fancy signature because he or she doesn't really care for one.
kevin
Development Team
Administrator
Hero Member
*****
Offline Offline

Posts: 9341



WWW
« Reply #9 on: October 26, 2009, 11:59:17 PM »

    Multi dimensional array fields are a possibility, and It's something that'd id like to add at some point..  The problem now is, that this type addressing comes with a lot more runtime overhead..  The more dimensions you have,  that more work it requires to locate the field being addressed.   

    For  Example.
Code:
     Type Stuff
               x,y,z
               SomeArray(20,40)
               SomeOtherArray(20,40)

     EndType
    Dim Obejcts(100) as stuff
    Objects(50) = new stuff
    Objects(50).SomeArray(10,5)  = 45
    Objects(50).SomeOtherArray(10)  = 12

     To actually read/write into this a field, we've first got to locate the structure first within the object array (ie mult and peek it out),  then work the filed offset within this structure.     To get the field offset (the element within the array)  we multiply the first (or second field) by what the number of elements is, then  add the other field..

      FieldOffset =  (FirstDim * 41) + SecondDim

      So to access one field there's possibly as many of 5/6 (low and high level) opcodes  that need to  pass through the VM.     So the cost operating upon such fields would be rather high. 

      Often it's best to export a pointer from the sub field and write directly into that parent structure yourself, which removes the need for the parent to be constantly dereferenced.   Which pretty much emulates what the With / EndWith statement pairs do in Visual Basic for example. 

      The best solution really is to make the embedded array a reference.  But the way the current parser works, this isn't really practical in PB atm.    What this would mean is that you could have fields that allow to attach arrays (dynamically sized) to them..   These could then be passing into handler functions that can receive this data type..  So rather and access the nested fields through the parent constantly,  you could dim the array, then pass it to a handler. 


      NOTE: This is CONCEPT ONLY sample code...

Code:
     Type Stuff
               x,y,z
               SomeArray()
               SomeOtherArray()
     EndType
      Dim Obejcts(100) as stuff
      Objects(50) = new stuff
      Dim Objects(50).SomeArray(20,40)   // alloc this field and set it up as a 2d array
      Dim Objects(50).SomeOtherArray(105)   // alloc this field and set it up as a 1d array
      Fill1DArray(Object(50).SomeOtherArray(),666)

Function Fill1DArray(Me(),Value)
       for lp=0 to getarrayelements(me(),1)
            me(lp)=Value
       next
EndFunction

     Due to how arrays work in VM1 (PB runtime), adding this type of functionality  PB isn't so easy.  However, such functionality is supported in the VM2 runtime ( PBFX) , which is designed to allow  such controls and more. 
 
Logged

Makeii Runoru
Full Member
***
Offline Offline

Posts: 116



WWW
« Reply #10 on: October 27, 2009, 01:53:20 AM »

Well, Alright. I understand. And it's not terribly hard to find array indices for a 1D array, so multi-dimensional arrays aren't really needed. I didn't know how much work would be put into adding them.

Is there a possibility of making classes in PB, like C++ does? The only reason I ask is because sometimes I'd like functions not to be global, but rather inside of an object, preferably a type.

Code:
constant NORTH = 1
constant EAST = 2
constant SOUTH = 3
constant WEST = 4

type t_player
   x#
   y#

   Function Move(dir)
      ...
   Endfunction
endtype

Dim Player as t_player
Player.Move(WEST)
Logged

This signature is boring, and could be improved. However, the user of this signature doesn't need a fancy signature because he or she doesn't really care for one.
kevin
Development Team
Administrator
Hero Member
*****
Offline Offline

Posts: 9341



WWW
« Reply #11 on: October 27, 2009, 08:40:00 AM »


 
Quote
Is there a possibility of making classes in PB, like C++ does?

     Yes & No.   Eventually some limited Object Orientation   will appear,  but this is really isn't practical in PB today.  Comparably,  implementing OO support makes multi dimensional arrays look like a cake wake.  So it's not something you can just drop in quickly here.
 
Logged

Makeii Runoru
Full Member
***
Offline Offline

Posts: 116



WWW
« Reply #12 on: October 28, 2009, 06:36:57 PM »

Alrighty. But that's nice that OO support is going to be supported. I mean, I write code in such a way that it presents itself in an OO manner, but it's not the same as some other languages out there. Usually I make types and Dimension them right after, and then in a separate tab create the functions or "events" that the object uses.
Logged

This signature is boring, and could be improved. However, the user of this signature doesn't need a fancy signature because he or she doesn't really care for one.
kevin
Development Team
Administrator
Hero Member
*****
Offline Offline

Posts: 9341



WWW
« Reply #13 on: October 29, 2009, 12:33:10 AM »

 PlayBASIC V1.64k  Beta 7 - Stacks opt's = Better recursion speed.

   Been working on the stack controls the past couple of days,  made a few improvements in previous beta's, which turned out to be hindrances also, but ya get that !  -  The stack is really a hidden bottle neck in how various parts of PB runtime (VM1) perform.  So improving the stack functionality can make the overall runtime performance that little bit snappier.  Which means your programs can run faster. 

  The current changes have been mostly relating to how a change of scope occurs (function calling).   The old version is very polite and includes a wealth of error trapping and type matching that isn't always needed (however, some times it is!) -  So, what I've been doing is restructuring the call function stack usage ,  to remove as much of nanny code as possible. 

   It's taken a lot longer than i'd originally expected to get working, but finally the scope change caller is yielding almost universally better results.  For a simple function calls (small functions) it's only slightly faster though.    In fact,  BEta 7 is slower than previous betas in this regard.  However, previous beta's relies upon a few too many assumptions, in others words,  they don't actually work correctly Smiley 
 
   Beta 7 is quicker than previous PBV1.64j retail editions and thats what matters, but only marginally (by a couple of %), where it starts to shine is in calling larger scopes (big fat functions with lots of locals) and in particular recursive function calls.   

   In the example bellow we have a parent/child structure that's designed to use recursion.   V1.64k Beta 7 runs this example approximately %30 faster then V1.64J Retail.    Which is nothing to sneeze at..


Code:

Constant MaxChildren=255

Type tObjectParent
x#,y#
TranslatedX#,TranslatedY#
Angle#
ScaleX#
ScaleY#
Colour
Size
Parent
ChildCount ; max kids
Children(MaxChildren) // This obejct can have a 255 children attached to it
EndType
 
  Type tObjectChild as TobjectParent
  Endtype


// This array holds the entire data structure of parents and their children
    Dim Ball(0) as tobjectParent


   TestParent=CreateParent(100,200,45)

// Append Child to the parent
  Child2=AddChildToParent(testParent,100,100,00)

// append a bunch of kids to the Child
Kids=50
Dim Children(Kids)
  for lp=1 to Kids
angle#=(360.0/Kids)*lp
x#=cos(angle#)*150
y#=sin(angle#)*150
    Children(lp)=AddChildToParent(Child2,x#,y#,0)
  next
     
   
; setfps 100


Do
cls $204080

TurnObject(TestParent,2.25)

PositionObject(TestParent,mousex(),mousey())

sangle#=wrapangle(sangle#,1)
s#=2+cos(sangle#)*1
s#=1
ScaleObject(Child2,s#,s#)


// loop through and process all the parent objects
lockbuffer
t=timer()
for lp=1 to GetArrayElements(Ball(),1)
if TypeOf(Ball(lp))=tObjectParent
// process a parent object

ParentX#=Ball(lp).x#
ParentY#=Ball(lp).y#
Angle# =Ball(lp).angle#
ScaleX# =Ball(lp).scalex#
ScaleY# =Ball(lp).scaley#
circlec parentx#,parenty#,Ball(lp).size,true,Ball(lp).colour
// DRaw it's kids
DrawChildren(lp,ParentX#,ParentY#,angle#,scaleX#,scaleY#)
endif
next
tt#=tt#+(Timer()-t)
unlockbuffer

inc frames
print tt#/frames
Sync
loop






Function CreateParent(X#,y#,angle#)
Index=GetFreeCell(Ball())
Ball(index).x#=x#
Ball(index).y#=y#
Ball(index).ScaleX# =1
Ball(index).ScaleY# =1
Ball(index).Angle#=angle#
Ball(index).size =20
Ball(index).colour =$ff0000
EndFunction Index



Function TurnObject(Index,AngleStep#)
Ball(index).angle#=wrapangle(Ball(index).angle#,AngleStep#)

EndFunction Index



Function ScaleObject(Index,ScaleX#,scaley#)
Ball(index).ScaleX# =ScaleX#
Ball(Index).ScaleY# =ScaleX#
EndFunction


Function PositionObject(Index,X#,y#)
Ball(index).x#=x#
Ball(index).y#=y#
EndFunction Index



Function AddChildToParent(ParentIndex,X#,y#,angle#)

ChildIndexInParent=FindFreeChildInPArent(ParentIndex)
if ChildIndexInParent

ChildIndex=GetFreeCell(Ball())

Ball(Parentindex).ChildCount=Ball(Parentindex).ChildCount+1

// store the index of this child in the parents local array
Ball(Parentindex).Children(ChildIndexInParent) =ChildIndex

// create the child.  The child is local to the parent   
Ball(childIndex) = new tObjectChild
Ball(Childindex).Parent =ParentIndex
Ball(Childindex).x# =x#
Ball(Childindex).y# =y#
Ball(Childindex).Angle# =angle#
Ball(Childindex).ScaleX# =1
Ball(Childindex).ScaleY# =1
Ball(Childindex).size = 10
Ball(Childindex).colour =255

endif
EndFunction ChildIndex






Function DrawChildren(ThisParent,ParentX#,ParentY#,Angle#,ScaleX#,ScaleY#)

ca#=Cos(angle#)
sa#=Sin(angle#)

for lp=1 to MaxChildren-1
ThisChild=Ball(ThisParent).Children(lp)
if ThisChild

Colour=Ball(ThisChild).Colour
Size =Ball(ThisChild).size
X#=Ball(ThisChild).x#*ScaleX#
Y#=Ball(ThisChild).y#*ScaleY#
rotatedX#=ParentX#+( (ca#*x#)-(sa#*y#))
rotatedY#=ParentY#+( (ca#*y#)+(sa#*x#))
Ball(ThisChild).TranslatedX#=RotatedX#
Ball(ThisChild).TranslatedY#=RotatedY#
circlec RotatedX#,RotatedY#,size,true,colour
endif
next

// draw children
if Ball(ThisParent).ChildCount
for lp=1 to MaxChildren-1
ThisChild=Ball(ThisParent).Children(lp)
if ThisChild
x# =Ball(ThisChild).TranslatedX#
y# =Ball(ThisChild).TranslatedY#
DrawChildren(ThisChild,X#,Y#,wrapangle(Ball(ThisChild).Angle#,Angle#),Ball(ThisChild).ScaleX#,Ball(ThisChild).ScaleY#)
endif
next
endif

EndFunction


function LocateAbsoluteParentOffset(ThisChild)
repeat
parent =Ball(ThisChild).Parent
x#=x#+Ball(ThisChild).x#
y#=y#+Ball(ThisChild).y#
until Parent=0
EndFunction x#,y#



Function FindFreeChildInPArent(ThisParent)
for lp=1 to MaxChildren
ChildIndex=Ball(ThisParent).Children(lp)
if ChildIndex=0
      exitfunction lp
endif
next
EndFunction Index





 
« Last Edit: October 29, 2009, 11:43:28 PM by kevin » Logged

kevin
Development Team
Administrator
Hero Member
*****
Offline Offline

Posts: 9341



WWW
« Reply #14 on: October 29, 2009, 11:48:39 PM »

 PlayBASIC V1.64k  Beta 8 - Mismatched Assignment Casting.

  I hadn't noticed this before, but PB doesn't seem to like assigning pointers to integer/float variables.  From the parsers perspective this is perfectly legal, but the runtime doesn't actually feature the require opcodes.  So in older version of PB you'll get a unknown opcode error.   As such I've dropped in some support for this.   Seems to work ok..

  eg.

Code:

; declare pointer (generic)
Dim a as pointer

;  assign the Integer variable PTR the integer value of 5
ptr=5


// move integer to pointer
a = ptr

// move pointer to integer
i=a
print i


// Add  pointer to integer
i+=a
print i

sync
waitkey



  Edit: Should work on all the main structures now.

Code:

// declare pointer (generic)
Dim a as pointer

//  assign the Integer variable PTR the integer value of 5
ptr=5

// move integer to pointer
a = ptr

// move pointer to integer
i=a
print i

// Add  pointer to integer
i+=a
print i



Dim Stuff(100)
Dim Stuff#(100)


Stuff(1) +=a
Stuff(1) +=a
Stuff(1) +=a
print Stuff(1)

Stuff#(1) +=a
print Stuff#(1)


Type SomeData
x,y,z
f#
endtype

Dim Y as SomeData
Dim W(10) as somedata



y.x +=a
y.f +=a
y.x +=a
y.f +=a

print "typed variable"
print y.x
print y.f

w(1).x =a
w(1).f =a
w(1).x *=a
w(1).f *=a

print "typed array"
print w(1).x
print w(1).f

sync
waitkey


« Last Edit: October 30, 2009, 08:26:45 AM by kevin » Logged

Pages: [1] 2 3 4
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!